diff --git a/cmd.sh b/cmd.sh index c547a19..c6a17f6 100755 --- a/cmd.sh +++ b/cmd.sh @@ -1,4 +1,4 @@ go mod tidy TAGS=with_gvisor,with_quic,with_wireguard,with_ech,with_utls,with_clash_api,with_grpc # TAGS=with_dhcp,with_low_memory,with_conntrack -go run --tags $TAGS ./cli $@ \ No newline at end of file +go run --tags $TAGS ./cmd/main $@ \ No newline at end of file diff --git a/go.mod b/go.mod index 6ef3cb7..7f9bd87 100644 --- a/go.mod +++ b/go.mod @@ -162,8 +162,6 @@ toolchain go1.22.3 require ( github.com/bepass-org/warp-plus v1.2.4 - github.com/fatih/color v1.16.0 - github.com/hiddify/hiddify-app-demo-extension v0.0.0-20241001070003-26039f960ad6 github.com/improbable-eng/grpc-web v0.15.0 github.com/jellydator/validation v1.1.0 github.com/kardianos/service v1.2.2 @@ -181,6 +179,7 @@ require ( require ( github.com/google/uuid v1.6.0 + github.com/hectane/go-acl v0.0.0-20230122075934-ca0b05cb1adb github.com/syndtr/goleveldb v1.0.1-0.20200815110645-5c35d600f0ca github.com/tendermint/tm-db v0.6.7 ) @@ -198,8 +197,7 @@ require ( github.com/golang/protobuf v1.5.4 // indirect github.com/golang/snappy v0.0.1 // indirect github.com/jmhodges/levigo v1.0.0 // indirect - github.com/mattn/go-colorable v0.1.13 // indirect - github.com/mattn/go-isatty v0.0.20 // indirect + github.com/json-iterator/go v1.1.12 // indirect github.com/metacubex/tfo-go v0.0.0-20240821025650-e9be0afd5e7d // indirect github.com/pkg/errors v0.9.1 // indirect github.com/rs/cors v1.7.0 // indirect diff --git a/go.sum b/go.sum index 82be9a6..424b109 100644 --- a/go.sum +++ b/go.sum @@ -106,8 +106,6 @@ github.com/facebookgo/stack v0.0.0-20160209184415-751773369052/go.mod h1:UbMTZqL github.com/facebookgo/subset v0.0.0-20200203212716-c811ad88dec4 h1:7HZCaLC5+BZpmbhCOZJ293Lz68O7PYrF2EzeiFMwCLk= github.com/facebookgo/subset v0.0.0-20200203212716-c811ad88dec4/go.mod h1:5tD+neXqOorC30/tWg0LCSkrqj/AR6gu8yY8/fpw1q0= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= -github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM= -github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE= github.com/franela/goblin v0.0.0-20200105215937-c9ffbefa60db/go.mod h1:7dvUGVsVBjqR7JHJk0brhHOZYGmfBYOrK0ZhYMEtBr4= github.com/franela/goreq v0.0.0-20171204163338-bcd34c9993f8/go.mod h1:ZhphrRTfi2rbfLwlschooIH4+wKKDR4Pdxhh+TRoA20= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= @@ -242,8 +240,8 @@ github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2p github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc= github.com/hashicorp/yamux v0.1.1 h1:yrQxtgseBDrq9Y652vSRDvsKCJKOUD+GzTS4Y0Y8pvE= github.com/hashicorp/yamux v0.1.1/go.mod h1:CtWFDAQgb7dxtzFs4tWbplKIe2jSi3+5vKbgIO0SLnQ= -github.com/hiddify/hiddify-app-demo-extension v0.0.0-20241001070003-26039f960ad6 h1:ZErxaLRV5iWCBAR8qsyoNemEKntE4WSvK00Ts4zbS84= -github.com/hiddify/hiddify-app-demo-extension v0.0.0-20241001070003-26039f960ad6/go.mod h1:1F56GeIkSjUJF0VP/zPS9rJhVc97TjEQsDTFhmr9Ddc= +github.com/hectane/go-acl v0.0.0-20230122075934-ca0b05cb1adb h1:PGufWXXDq9yaev6xX1YQauaO1MV90e6Mpoq1I7Lz/VM= +github.com/hectane/go-acl v0.0.0-20230122075934-ca0b05cb1adb/go.mod h1:QiyDdbZLaJ/mZP4Zwc9g2QsfaEA4o7XvvgZegSci5/E= github.com/hiddify/hiddify-sing-box v1.8.9-0.20241019134406-9079d5024936 h1:VvKwjJVvfkMxRtKtKsnlzPER3kKQNAJEQBCY1sB0aps= github.com/hiddify/hiddify-sing-box v1.8.9-0.20241019134406-9079d5024936/go.mod h1:6apAGQPqJIb58BIYsr66tu49S5UcCNyuv1lVX+72eQA= github.com/hiddify/ray2sing v0.0.0-20240928221833-190b549d5222 h1:+MFxFxoWCA44WhqIixqL/Zkt4DwnqhQvafS0Dm4+dKM= @@ -324,12 +322,9 @@ github.com/logrusorgru/aurora v2.0.3+incompatible/go.mod h1:7rIyQOR62GCctdiQpZ/z github.com/lyft/protoc-gen-validate v0.0.13/go.mod h1:XbGvPuh87YZc5TdIa2/I4pLk0QoUACkjt2znoq26NVQ= github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= -github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= -github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= -github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= @@ -353,8 +348,9 @@ github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= -github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI= github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f h1:KUppIJq7/+SVif2QVs3tOP0zanoHgBEVAwHxUSIzRqU= github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= @@ -708,6 +704,7 @@ golang.org/x/sys v0.0.0-20190403152447-81d4e9dc473e/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190529164535-6a60838ec259/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190826190057-c7b8b68b1456/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -734,7 +731,6 @@ golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220622161953-175b2fd9d664/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/v2/config/config.go b/v2/config/config.go index 7a27fee..d3c1af4 100644 --- a/v2/config/config.go +++ b/v2/config/config.go @@ -59,8 +59,6 @@ func BuildConfigJson(configOpt HiddifyOptions, input option.Options) (string, er // TODO include selectors func BuildConfig(opt HiddifyOptions, input option.Options) (*option.Options, error) { - fmt.Printf("config options: %++v\n", opt) - var options option.Options if opt.EnableFullConfig { options.Inbounds = input.Inbounds diff --git a/v2/config/warp.go b/v2/config/warp.go index 0038e65..3e413ab 100644 --- a/v2/config/warp.go +++ b/v2/config/warp.go @@ -150,7 +150,7 @@ func getOrGenerateWarpLocallyIfNeeded(warpOptions *WarpOptions) WarpWireguardCon table := db.GetTable[WarpOptions]() dbWarpOptions, err := table.Get(warpOptions.Id) if err == nil && dbWarpOptions.WireguardConfig.PrivateKey != "" { - return warpOptions.WireguardConfig + return dbWarpOptions.WireguardConfig } license := "" if len(warpOptions.Id) == 26 { // warp key is 26 characters long diff --git a/v2/hcore/command_client.go b/v2/hcore/command_client.go index f1a88b0..1b0cffe 100644 --- a/v2/hcore/command_client.go +++ b/v2/hcore/command_client.go @@ -2,29 +2,26 @@ package hcore import ( "github.com/sagernet/sing-box/experimental/libbox" - "github.com/sagernet/sing-box/log" ) var _ libbox.CommandClientHandler = (*CommandClientHandler)(nil) -type CommandClientHandler struct { - logger log.Logger -} +type CommandClientHandler struct{} func (cch *CommandClientHandler) Connected() { - cch.logger.Debug("CONNECTED") + Log(LogLevel_DEBUG, LogType_CORE, "CONNECTED") } func (cch *CommandClientHandler) Disconnected(message string) { - cch.logger.Debug("DISCONNECTED: ", message) + Log(LogLevel_DEBUG, LogType_CORE, "DISCONNECTED: ", message) } func (cch *CommandClientHandler) ClearLog() { - cch.logger.Debug("clear log") + Log(LogLevel_DEBUG, LogType_CORE, "clear log") } func (cch *CommandClientHandler) WriteLog(message string) { - cch.logger.Debug("log: ", message) + Log(LogLevel_DEBUG, LogType_CORE, "log: ", message) } func (cch *CommandClientHandler) WriteStatus(message *libbox.StatusMessage) { @@ -38,7 +35,7 @@ func (cch *CommandClientHandler) WriteStatus(message *libbox.StatusMessage) { Memory: message.Memory, Goroutines: message.Goroutines, }) - cch.logger.Debug("Memory: ", libbox.FormatBytes(message.Memory), ", Goroutines: ", message.Goroutines) + Log(LogLevel_DEBUG, LogType_CORE, "Memory: ", libbox.FormatBytes(message.Memory), ", Goroutines: ", message.Goroutines) } func (cch *CommandClientHandler) WriteGroups(message libbox.OutboundGroupIterator) { @@ -68,9 +65,9 @@ func (cch *CommandClientHandler) WriteGroups(message libbox.OutboundGroupIterato } func (cch *CommandClientHandler) InitializeClashMode(modeList libbox.StringIterator, currentMode string) { - cch.logger.Debug("initial clash mode: ", currentMode) + Log(LogLevel_DEBUG, LogType_CORE, "initial clash mode: ", currentMode) } func (cch *CommandClientHandler) UpdateClashMode(newMode string) { - cch.logger.Debug("update clash mode: ", newMode) + Log(LogLevel_DEBUG, LogType_CORE, "update clash mode: ", newMode) } diff --git a/v2/hcore/command_server.go b/v2/hcore/command_server.go new file mode 100644 index 0000000..4b9dc9e --- /dev/null +++ b/v2/hcore/command_server.go @@ -0,0 +1,44 @@ +package hcore + +import ( + "github.com/sagernet/sing-box/experimental/libbox" +) + +var commandServer *libbox.CommandServer + +type CommandServerHandler struct{} + +func (csh *CommandServerHandler) ServiceReload() error { + Log(LogLevel_DEBUG, LogType_CORE, "Reloading service") + + Stop() + _, err := StartService(&StartRequest{ + EnableOldCommandServer: true, + DelayStart: true, + }, nil) + return err +} + +func (csh *CommandServerHandler) GetSystemProxyStatus() *libbox.SystemProxyStatus { + Log(LogLevel_DEBUG, LogType_CORE, "Getting system proxy status") + return &libbox.SystemProxyStatus{Available: true, Enabled: false} +} + +func (csh *CommandServerHandler) SetSystemProxyEnabled(isEnabled bool) error { + Log(LogLevel_DEBUG, LogType_CORE, "Setting system proxy status, enabled? ", isEnabled) + return csh.ServiceReload() +} + +func (csh *CommandServerHandler) PostServiceClose() { + if commandServer != nil { + commandServer.Close() + commandServer.SetService(nil) + } + commandServer = nil +} + +func startCommandServer(sbox *libbox.BoxService) error { + commandServer = libbox.NewCommandServer(&CommandServerHandler{}, 300) + commandServer.SetService(sbox) + return commandServer.Start() +} diff --git a/v2/hcore/commands.go b/v2/hcore/commands.go index 340ee87..8bc0197 100644 --- a/v2/hcore/commands.go +++ b/v2/hcore/commands.go @@ -24,7 +24,7 @@ func (s *CoreService) GetSystemInfo(req *common.Empty, stream grpc.ServerStreami if statusClient == nil { statusClient = libbox.NewCommandClient( &CommandClientHandler{ - logger: coreLogFactory.NewLogger("[SystemInfo Command Client]"), + // port: s.port, }, &libbox.CommandClientOptions{ @@ -59,7 +59,6 @@ func (s *CoreService) OutboundsInfo(req *common.Empty, stream grpc.ServerStreami if groupClient == nil { groupClient = libbox.NewCommandClient( &CommandClientHandler{ - logger: coreLogFactory.NewLogger("[OutboundsInfo Command Client]"), // port: s.port, }, &libbox.CommandClientOptions{ @@ -95,7 +94,6 @@ func (s *CoreService) MainOutboundsInfo(req *common.Empty, stream grpc.ServerStr if groupInfoOnlyClient == nil { groupInfoOnlyClient = libbox.NewCommandClient( &CommandClientHandler{ - logger: coreLogFactory.NewLogger("[MainOutboundsInfo Command Client]"), // port: s.port, }, &libbox.CommandClientOptions{ diff --git a/v2/hcore/config.go b/v2/hcore/config.go index ecfd137..58c9687 100644 --- a/v2/hcore/config.go +++ b/v2/hcore/config.go @@ -3,7 +3,6 @@ package hcore import ( "context" "encoding/json" - "fmt" "os" "path/filepath" @@ -45,7 +44,12 @@ func BuildConfig(in *StartRequest) (*option.Options, error) { } if !in.EnableRawConfig { - Log(LogLevel_DEBUG, LogType_CORE, "Building config "+fmt.Sprintf("%++v", HiddifyOptions)) + hcontent, err := json.MarshalIndent(HiddifyOptions, "", " ") + if err != nil { + return nil, err + } + + Log(LogLevel_DEBUG, LogType_CORE, "Building config ", string(hcontent)) return config.BuildConfig(*HiddifyOptions, parsedContent) } diff --git a/v2/hcore/start.go b/v2/hcore/start.go index 0eca88a..1296939 100644 --- a/v2/hcore/start.go +++ b/v2/hcore/start.go @@ -65,8 +65,12 @@ func StartService(in *StartRequest, platformInterface libbox.PlatformInterface) } instance.GetInstance().AddPostService("hiddifyMainServiceManager", &hiddifyMainServiceManager{}) - err = instance.Start() - if err != nil { + + if err := startCommandServer(instance); err != nil { + return errorWrapper(MessageType_START_COMMAND_SERVER, err) + } + + if err := instance.Start(); err != nil { return errorWrapper(MessageType_START_SERVICE, err) } Box = instance