Files
umbrix-libcore/custom/commands.go

56 lines
1.2 KiB
Go
Raw Normal View History

2023-08-28 13:08:43 +03:30
package main
2023-10-27 16:39:48 +03:30
import (
"github.com/sagernet/sing-box/experimental/libbox"
"github.com/sagernet/sing-box/log"
)
2023-08-28 13:08:43 +03:30
var (
2023-10-23 19:22:37 +03:30
statusClient *libbox.CommandClient
groupClient *libbox.CommandClient
2023-08-28 13:08:43 +03:30
)
2023-10-27 16:39:48 +03:30
func StartCommand(command int32, port int64, logFactory log.Factory) error {
2023-08-28 13:08:43 +03:30
switch command {
case libbox.CommandStatus:
2023-10-23 19:22:37 +03:30
statusClient = libbox.NewCommandClient(
2023-10-27 16:39:48 +03:30
&CommandClientHandler{
port: port,
logger: logFactory.NewLogger("[Status Command Client]"),
},
2023-08-28 13:08:43 +03:30
&libbox.CommandClientOptions{
Command: libbox.CommandStatus,
StatusInterval: 1000000000,
},
)
2023-10-23 19:22:37 +03:30
return statusClient.Connect()
2023-08-29 19:10:14 +03:30
case libbox.CommandGroup:
2023-10-23 19:22:37 +03:30
groupClient = libbox.NewCommandClient(
2023-10-27 16:39:48 +03:30
&CommandClientHandler{
port: port,
logger: logFactory.NewLogger("[Group Command Client]"),
},
2023-08-29 19:10:14 +03:30
&libbox.CommandClientOptions{
Command: libbox.CommandGroup,
StatusInterval: 1000000000,
},
)
2023-10-23 19:22:37 +03:30
return groupClient.Connect()
2023-08-28 13:08:43 +03:30
}
return nil
}
func StopCommand(command int32) error {
switch command {
case libbox.CommandStatus:
2023-10-23 19:22:37 +03:30
err := statusClient.Disconnect()
statusClient = nil
2023-08-28 13:08:43 +03:30
return err
2023-08-29 19:10:14 +03:30
case libbox.CommandGroup:
2023-10-23 19:22:37 +03:30
err := groupClient.Disconnect()
groupClient = nil
2023-08-29 19:10:14 +03:30
return err
2023-08-28 13:08:43 +03:30
}
return nil
}