fix: issue with singbox 1.8.9

This commit is contained in:
Hiddify
2024-03-18 10:53:54 +01:00
parent aba593322e
commit aab998fae9
5 changed files with 51 additions and 38 deletions

View File

@@ -18,20 +18,20 @@ import (
var Box *libbox.BoxService var Box *libbox.BoxService
var configOptions *config.ConfigOptions var configOptions *config.ConfigOptions
var activeConfigPath *string var activeConfigPath *string
var logFactory *log.Factory var coreLogFactory log.Factory
func StopAndAlert(msgType pb.MessageType, message string) { func StopAndAlert(msgType pb.MessageType, message string) {
SetCoreStatus(pb.CoreState_STOPPED, msgType, message) SetCoreStatus(pb.CoreState_STOPPED, msgType, message)
config.DeactivateTunnelService() config.DeactivateTunnelService()
if commandServer != nil { if oldCommandServer != nil {
commandServer.SetService(nil) oldCommandServer.SetService(nil)
} }
if Box != nil { if Box != nil {
Box.Close() Box.Close()
Box = nil Box = nil
} }
if commandServer != nil { if oldCommandServer != nil {
commandServer.Close() oldCommandServer.Close()
} }
if EnableBridge { if EnableBridge {
alert := msgType.String() alert := msgType.String()
@@ -79,6 +79,7 @@ func StartService(in *pb.StartRequest) (*pb.CoreInfoResponse, error) {
if err != nil { if err != nil {
Log(pb.LogLevel_FATAL, pb.LogType_CORE, err.Error()) Log(pb.LogLevel_FATAL, pb.LogType_CORE, err.Error())
resp := SetCoreStatus(pb.CoreState_STOPPED, pb.MessageType_ERROR_READING_CONFIG, err.Error()) resp := SetCoreStatus(pb.CoreState_STOPPED, pb.MessageType_ERROR_READING_CONFIG, err.Error())
StopAndAlert(pb.MessageType_UNEXPECTED_ERROR, err.Error())
return &resp, err return &resp, err
} }
content = string(fileContent) content = string(fileContent)
@@ -91,6 +92,7 @@ func StartService(in *pb.StartRequest) (*pb.CoreInfoResponse, error) {
if err != nil { if err != nil {
Log(pb.LogLevel_FATAL, pb.LogType_CORE, err.Error()) Log(pb.LogLevel_FATAL, pb.LogType_CORE, err.Error())
resp := SetCoreStatus(pb.CoreState_STOPPED, pb.MessageType_ERROR_PARSING_CONFIG, err.Error()) resp := SetCoreStatus(pb.CoreState_STOPPED, pb.MessageType_ERROR_PARSING_CONFIG, err.Error())
StopAndAlert(pb.MessageType_UNEXPECTED_ERROR, err.Error())
return &resp, err return &resp, err
} }
if !in.EnableRawConfig { if !in.EnableRawConfig {
@@ -100,16 +102,19 @@ func StartService(in *pb.StartRequest) (*pb.CoreInfoResponse, error) {
if err != nil { if err != nil {
Log(pb.LogLevel_FATAL, pb.LogType_CORE, err.Error()) Log(pb.LogLevel_FATAL, pb.LogType_CORE, err.Error())
resp := SetCoreStatus(pb.CoreState_STOPPED, pb.MessageType_ERROR_BUILDING_CONFIG, err.Error()) resp := SetCoreStatus(pb.CoreState_STOPPED, pb.MessageType_ERROR_BUILDING_CONFIG, err.Error())
StopAndAlert(pb.MessageType_UNEXPECTED_ERROR, err.Error())
return &resp, err return &resp, err
} }
} }
Log(pb.LogLevel_INFO, pb.LogType_CORE, "Saving Contnet") Log(pb.LogLevel_INFO, pb.LogType_CORE, "Saving Contnet")
config.SaveCurrentConfig(filepath.Join(sWorkingPath, "current-config.json"), parsedContent) config.SaveCurrentConfig(filepath.Join(sWorkingPath, "current-config.json"), parsedContent)
if in.EnableOldCommandServer { if in.EnableOldCommandServer {
err = startCommandServer(*logFactory) Log(pb.LogLevel_INFO, pb.LogType_CORE, "Starting Command Server")
err = startCommandServer()
if err != nil { if err != nil {
Log(pb.LogLevel_FATAL, pb.LogType_CORE, err.Error()) Log(pb.LogLevel_FATAL, pb.LogType_CORE, err.Error())
resp := SetCoreStatus(pb.CoreState_STOPPED, pb.MessageType_START_COMMAND_SERVER, err.Error()) resp := SetCoreStatus(pb.CoreState_STOPPED, pb.MessageType_START_COMMAND_SERVER, err.Error())
StopAndAlert(pb.MessageType_UNEXPECTED_ERROR, err.Error())
return &resp, err return &resp, err
} }
} }
@@ -120,6 +125,7 @@ func StartService(in *pb.StartRequest) (*pb.CoreInfoResponse, error) {
if err != nil { if err != nil {
Log(pb.LogLevel_FATAL, pb.LogType_CORE, err.Error()) Log(pb.LogLevel_FATAL, pb.LogType_CORE, err.Error())
resp := SetCoreStatus(pb.CoreState_STOPPED, pb.MessageType_CREATE_SERVICE, err.Error()) resp := SetCoreStatus(pb.CoreState_STOPPED, pb.MessageType_CREATE_SERVICE, err.Error())
StopAndAlert(pb.MessageType_UNEXPECTED_ERROR, err.Error())
return &resp, err return &resp, err
} }
Log(pb.LogLevel_INFO, pb.LogType_CORE, "Service.. started") Log(pb.LogLevel_INFO, pb.LogType_CORE, "Service.. started")
@@ -131,11 +137,12 @@ func StartService(in *pb.StartRequest) (*pb.CoreInfoResponse, error) {
if err != nil { if err != nil {
Log(pb.LogLevel_FATAL, pb.LogType_CORE, err.Error()) Log(pb.LogLevel_FATAL, pb.LogType_CORE, err.Error())
resp := SetCoreStatus(pb.CoreState_STOPPED, pb.MessageType_START_SERVICE, err.Error()) resp := SetCoreStatus(pb.CoreState_STOPPED, pb.MessageType_START_SERVICE, err.Error())
StopAndAlert(pb.MessageType_UNEXPECTED_ERROR, err.Error())
return &resp, err return &resp, err
} }
Box = instance Box = instance
if in.EnableOldCommandServer { if in.EnableOldCommandServer {
commandServer.SetService(Box) oldCommandServer.SetService(Box)
} }
resp := SetCoreStatus(pb.CoreState_STARTED, pb.MessageType_EMPTY, "") resp := SetCoreStatus(pb.CoreState_STARTED, pb.MessageType_EMPTY, "")
@@ -263,8 +270,8 @@ func Stop() (*pb.CoreInfoResponse, error) {
} }
SetCoreStatus(pb.CoreState_STOPPING, pb.MessageType_EMPTY, "") SetCoreStatus(pb.CoreState_STOPPING, pb.MessageType_EMPTY, "")
config.DeactivateTunnelService() config.DeactivateTunnelService()
if commandServer != nil { if oldCommandServer != nil {
commandServer.SetService(nil) oldCommandServer.SetService(nil)
} }
err := Box.Close() err := Box.Close()
@@ -276,16 +283,16 @@ func Stop() (*pb.CoreInfoResponse, error) {
}, fmt.Errorf("Error while stopping the service.") }, fmt.Errorf("Error while stopping the service.")
} }
Box = nil Box = nil
if commandServer != nil { if oldCommandServer != nil {
err = commandServer.Close() err = oldCommandServer.Close()
if err != nil { if err != nil {
return &pb.CoreInfoResponse{ return &pb.CoreInfoResponse{
CoreState: CoreState, CoreState: CoreState,
MessageType: pb.MessageType_UNEXPECTED_ERROR, MessageType: pb.MessageType_UNEXPECTED_ERROR,
Message: "Error while Closing the comand server.", Message: "Error while Closing the comand server.",
}, fmt.Errorf("Error while Closing the comand server.") }, fmt.Errorf("error while Closing the comand server.")
} }
commandServer = nil oldCommandServer = nil
} }
resp := SetCoreStatus(pb.CoreState_STOPPED, pb.MessageType_EMPTY, "") resp := SetCoreStatus(pb.CoreState_STOPPED, pb.MessageType_EMPTY, "")
return &resp, nil return &resp, nil

View File

@@ -6,6 +6,7 @@ import (
"github.com/hiddify/libcore/bridge" "github.com/hiddify/libcore/bridge"
"github.com/sagernet/sing-box/experimental/libbox" "github.com/sagernet/sing-box/experimental/libbox"
"github.com/sagernet/sing-box/log"
) )
var ( var (
@@ -13,24 +14,24 @@ var (
) )
type OldCommandClientHandler struct { type OldCommandClientHandler struct {
port int64 port int64
// logger log.Logger logger log.Logger
} }
func (cch *OldCommandClientHandler) Connected() { func (cch *OldCommandClientHandler) Connected() {
// cch.logger.Debug("CONNECTED") cch.logger.Debug("CONNECTED")
} }
func (cch *OldCommandClientHandler) Disconnected(message string) { func (cch *OldCommandClientHandler) Disconnected(message string) {
// cch.logger.Debug("DISCONNECTED: ", message) cch.logger.Debug("DISCONNECTED: ", message)
} }
func (cch *OldCommandClientHandler) ClearLog() { func (cch *OldCommandClientHandler) ClearLog() {
// cch.logger.Debug("clear log") cch.logger.Debug("clear log")
} }
func (cch *OldCommandClientHandler) WriteLog(message string) { func (cch *OldCommandClientHandler) WriteLog(message string) {
// cch.logger.Debug("log: ", message) cch.logger.Debug("log: ", message)
} }
func (cch *OldCommandClientHandler) WriteStatus(message *libbox.StatusMessage) { func (cch *OldCommandClientHandler) WriteStatus(message *libbox.StatusMessage) {
@@ -44,7 +45,7 @@ func (cch *OldCommandClientHandler) WriteStatus(message *libbox.StatusMessage) {
"downlink-total": message.DownlinkTotal, "downlink-total": message.DownlinkTotal,
}, },
) )
// cch.logger.Debug("Memory: ", libbox.FormatBytes(message.Memory), ", Goroutines: ", message.Goroutines) cch.logger.Debug("Memory: ", libbox.FormatBytes(message.Memory), ", Goroutines: ", message.Goroutines)
if err != nil { if err != nil {
bridge.SendStringToPort(cch.port, fmt.Sprintf("error: %e", err)) bridge.SendStringToPort(cch.port, fmt.Sprintf("error: %e", err))
} else { } else {
@@ -83,11 +84,11 @@ func (cch *OldCommandClientHandler) WriteGroups(message libbox.OutboundGroupIter
} }
func (cch *OldCommandClientHandler) InitializeClashMode(modeList libbox.StringIterator, currentMode string) { func (cch *OldCommandClientHandler) InitializeClashMode(modeList libbox.StringIterator, currentMode string) {
// cch.logger.Debug("initial clash mode: ", currentMode) cch.logger.Debug("initial clash mode: ", currentMode)
} }
func (cch *OldCommandClientHandler) UpdateClashMode(newMode string) { func (cch *OldCommandClientHandler) UpdateClashMode(newMode string) {
// cch.logger.Debug("update clash mode: ", newMode) cch.logger.Debug("update clash mode: ", newMode)
} }
type OutboundGroup struct { type OutboundGroup struct {

View File

@@ -7,7 +7,7 @@ import (
"github.com/sagernet/sing-box/log" "github.com/sagernet/sing-box/log"
) )
var commandServer *libbox.CommandServer var oldCommandServer *libbox.CommandServer
type CommandServerHandler struct { type CommandServerHandler struct {
logger log.Logger logger log.Logger
@@ -17,9 +17,9 @@ func (csh *CommandServerHandler) ServiceReload() error {
csh.logger.Trace("Reloading service") csh.logger.Trace("Reloading service")
SetCoreStatus(pb.CoreState_STARTING, pb.MessageType_EMPTY, "") SetCoreStatus(pb.CoreState_STARTING, pb.MessageType_EMPTY, "")
if commandServer != nil { if oldCommandServer != nil {
commandServer.SetService(nil) oldCommandServer.SetService(nil)
commandServer = nil oldCommandServer = nil
} }
if Box != nil { if Box != nil {
Box.Close() Box.Close()
@@ -45,9 +45,9 @@ func (csh *CommandServerHandler) SetSystemProxyEnabled(isEnabled bool) error {
func (csh *CommandServerHandler) PostServiceClose() { func (csh *CommandServerHandler) PostServiceClose() {
} }
func startCommandServer(logFactory log.Factory) error { func startCommandServer() error {
logger := logFactory.NewLogger("[Command Server Handler]") logger := coreLogFactory.NewLogger("[Command Server Handler]")
logger.Trace("Starting command server") logger.Trace("Starting command server")
commandServer = libbox.NewCommandServer(&CommandServerHandler{logger: logger}, 300) oldCommandServer = libbox.NewCommandServer(&CommandServerHandler{logger: logger}, 300)
return commandServer.Start() return oldCommandServer.Start()
} }

View File

@@ -15,8 +15,8 @@ func StartCommand(command int32, port int64) error {
case libbox.CommandStatus: case libbox.CommandStatus:
oldStatusClient = libbox.NewCommandClient( oldStatusClient = libbox.NewCommandClient(
&OldCommandClientHandler{ &OldCommandClientHandler{
port: port, port: port,
// logger: logFactory.NewLogger("[Status Command Client]"), logger: coreLogFactory.NewLogger("[Status Command Client]"),
}, },
&libbox.CommandClientOptions{ &libbox.CommandClientOptions{
Command: libbox.CommandStatus, Command: libbox.CommandStatus,
@@ -27,8 +27,8 @@ func StartCommand(command int32, port int64) error {
case libbox.CommandGroup: case libbox.CommandGroup:
oldGroupClient = libbox.NewCommandClient( oldGroupClient = libbox.NewCommandClient(
&OldCommandClientHandler{ &OldCommandClientHandler{
port: port, port: port,
// logger: logFactory.NewLogger("[Group Command Client]"), logger: coreLogFactory.NewLogger("[Group Command Client]"),
}, },
&libbox.CommandClientOptions{ &libbox.CommandClientOptions{
Command: libbox.CommandGroup, Command: libbox.CommandGroup,
@@ -39,8 +39,8 @@ func StartCommand(command int32, port int64) error {
case libbox.CommandGroupInfoOnly: case libbox.CommandGroupInfoOnly:
oldGroupInfoOnlyClient = libbox.NewCommandClient( oldGroupInfoOnlyClient = libbox.NewCommandClient(
&OldCommandClientHandler{ &OldCommandClientHandler{
port: port, port: port,
// logger: logFactory.NewLogger("[GroupInfoOnly Command Client]"), logger: coreLogFactory.NewLogger("[GroupInfoOnly Command Client]"),
}, },
&libbox.CommandClientOptions{ &libbox.CommandClientOptions{
Command: libbox.CommandGroupInfoOnly, Command: libbox.CommandGroupInfoOnly,

View File

@@ -45,9 +45,14 @@ func Setup(basePath string, workingPath string, tempPath string, statusPort int6
log.Options{ log.Options{
DefaultWriter: defaultWriter, DefaultWriter: defaultWriter,
BaseTime: time.Now(), BaseTime: time.Now(),
Observable: false, Observable: true,
// Options: option.LogOptions{
// Disabled: false,
// Level: "trace",
// Output: "stdout",
// },
}) })
logFactory = &factory coreLogFactory = factory
return err return err
} }