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 (
|
2024-02-06 11:14:32 +01:00
|
|
|
statusClient *libbox.CommandClient
|
|
|
|
|
groupClient *libbox.CommandClient
|
|
|
|
|
groupInfoOnlyClient *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,
|
2024-02-13 03:47:34 +01:00
|
|
|
StatusInterval: 1000000000, //1000ms debounce
|
2023-08-28 13:08:43 +03:30
|
|
|
},
|
|
|
|
|
)
|
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,
|
2024-02-13 03:47:34 +01:00
|
|
|
StatusInterval: 300000000, //300ms debounce
|
2023-08-29 19:10:14 +03:30
|
|
|
},
|
|
|
|
|
)
|
2023-10-23 19:22:37 +03:30
|
|
|
return groupClient.Connect()
|
2024-02-06 11:14:32 +01:00
|
|
|
case libbox.CommandGroupInfoOnly:
|
|
|
|
|
groupInfoOnlyClient = libbox.NewCommandClient(
|
|
|
|
|
&CommandClientHandler{
|
|
|
|
|
port: port,
|
|
|
|
|
logger: logFactory.NewLogger("[GroupInfoOnly Command Client]"),
|
|
|
|
|
},
|
|
|
|
|
&libbox.CommandClientOptions{
|
|
|
|
|
Command: libbox.CommandGroupInfoOnly,
|
2024-02-13 03:47:34 +01:00
|
|
|
StatusInterval: 300000000, //300ms debounce
|
2024-02-06 11:14:32 +01:00
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
return groupInfoOnlyClient.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
|
2024-02-06 11:14:32 +01:00
|
|
|
case libbox.CommandGroupInfoOnly:
|
|
|
|
|
err := groupInfoOnlyClient.Disconnect()
|
|
|
|
|
groupInfoOnlyClient = nil
|
|
|
|
|
return err
|
2023-08-28 13:08:43 +03:30
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|