mirror of
https://github.com/shtorm-7/sing-box-extended.git
synced 2026-10-07 22:59:59 +00:00
Add api command
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/sagernet/sing-box/daemon"
|
||||
"github.com/sagernet/sing/common"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var commandAPIConnection = &cobra.Command{
|
||||
Use: "connection",
|
||||
Short: "Manage connections",
|
||||
}
|
||||
|
||||
func init() {
|
||||
commandAPIRoot.AddCommand(commandAPIConnection)
|
||||
}
|
||||
|
||||
func fetchConnections(client daemon.StartedServiceClient) ([]*daemon.Connection, error) {
|
||||
stream, err := client.SubscribeConnections(globalCtx, &daemon.SubscribeConnectionsRequest{Interval: int64(time.Second)})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
events, err := stream.Recv()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
connections := common.FilterNotNil(common.Map(events.GetEvents(), func(it *daemon.ConnectionEvent) *daemon.Connection {
|
||||
return it.GetConnection()
|
||||
}))
|
||||
common.SortBy(connections, func(it *daemon.Connection) int64 {
|
||||
return it.GetCreatedAt()
|
||||
})
|
||||
return connections, nil
|
||||
}
|
||||
Reference in New Issue
Block a user