2023-08-28 13:07:21 +03:30
|
|
|
package main
|
|
|
|
|
|
2023-10-27 14:36:00 +03:30
|
|
|
import (
|
2024-03-03 04:15:19 +01:00
|
|
|
pb "github.com/hiddify/libcore/hiddifyrpc"
|
|
|
|
|
v2 "github.com/hiddify/libcore/v2"
|
2023-10-27 14:36:00 +03:30
|
|
|
"github.com/sagernet/sing-box/experimental/libbox"
|
|
|
|
|
"github.com/sagernet/sing-box/log"
|
2024-03-03 04:15:19 +01:00
|
|
|
|
2023-10-27 14:36:00 +03:30
|
|
|
)
|
2023-08-28 13:07:21 +03:30
|
|
|
|
|
|
|
|
var commandServer *libbox.CommandServer
|
|
|
|
|
|
2023-10-27 16:39:48 +03:30
|
|
|
type CommandServerHandler struct {
|
|
|
|
|
logger log.Logger
|
|
|
|
|
}
|
2023-08-28 13:07:21 +03:30
|
|
|
|
|
|
|
|
func (csh *CommandServerHandler) ServiceReload() error {
|
2023-10-27 16:39:48 +03:30
|
|
|
csh.logger.Trace("Reloading service")
|
2024-03-03 04:15:19 +01:00
|
|
|
propagateStatus(pb.CoreState_STARTING)
|
2023-09-10 20:18:33 +03:30
|
|
|
if commandServer != nil {
|
|
|
|
|
commandServer.SetService(nil)
|
|
|
|
|
commandServer = nil
|
|
|
|
|
}
|
2024-03-03 04:15:19 +01:00
|
|
|
if v2.Box != nil {
|
|
|
|
|
v2.Box.Close()
|
|
|
|
|
v2.Box = nil
|
2023-09-10 20:18:33 +03:30
|
|
|
}
|
2023-10-27 14:36:00 +03:30
|
|
|
return startService(true)
|
2023-08-28 13:07:21 +03:30
|
|
|
}
|
|
|
|
|
|
2023-09-05 19:00:24 +03:30
|
|
|
func (csh *CommandServerHandler) GetSystemProxyStatus() *libbox.SystemProxyStatus {
|
2023-10-27 16:39:48 +03:30
|
|
|
csh.logger.Trace("Getting system proxy status")
|
2023-09-05 19:00:24 +03:30
|
|
|
return &libbox.SystemProxyStatus{Available: true, Enabled: false}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (csh *CommandServerHandler) SetSystemProxyEnabled(isEnabled bool) error {
|
2023-10-27 16:39:48 +03:30
|
|
|
csh.logger.Trace("Setting system proxy status, enabled? ", isEnabled)
|
2023-10-27 14:36:00 +03:30
|
|
|
return csh.ServiceReload()
|
2023-09-05 19:00:24 +03:30
|
|
|
}
|
|
|
|
|
|
2023-10-27 16:39:48 +03:30
|
|
|
func startCommandServer(logFactory log.Factory) error {
|
|
|
|
|
logger := logFactory.NewLogger("[Command Server Handler]")
|
|
|
|
|
logger.Trace("Starting command server")
|
|
|
|
|
commandServer = libbox.NewCommandServer(&CommandServerHandler{logger: logger}, 300)
|
2023-08-28 13:07:21 +03:30
|
|
|
return commandServer.Start()
|
|
|
|
|
}
|