From cd07df9b8f5b7ee117c5bf2c334b4ea899570bd0 Mon Sep 17 00:00:00 2001 From: Hiddify Date: Mon, 18 Mar 2024 20:38:09 +0100 Subject: [PATCH] fix; standalone issue and add secret --- config/config.go | 2 ++ config/option.go | 4 +++- v2/coreinfo.go | 13 +++++++----- v2/custom.go | 50 ++++++++++++++++++++++++-------------------- v2/logproto.go | 5 ++++- v2/standalone.go | 15 +++++++------ v2/tunnel_service.go | 2 +- 7 files changed, 54 insertions(+), 37 deletions(-) diff --git a/config/config.go b/config/config.go index 3506cbf..e574913 100644 --- a/config/config.go +++ b/config/config.go @@ -73,7 +73,9 @@ func BuildConfig(opt ConfigOptions, input option.Options) (*option.Options, erro options.Experimental = &option.ExperimentalOptions{ ClashAPI: &option.ClashAPIOptions{ ExternalController: fmt.Sprintf("%s:%d", "127.0.0.1", opt.ClashApiPort), + Secret: opt.ClashApiSecret, }, + CacheFile: &option.CacheFileOptions{ Enabled: true, Path: "clash.db", diff --git a/config/option.go b/config/option.go index 9d32f96..1df1f46 100644 --- a/config/option.go +++ b/config/option.go @@ -9,6 +9,7 @@ type ConfigOptions struct { LogLevel string `json:"log-level"` EnableClashApi bool `json:"enable-clash-api"` ClashApiPort uint16 `json:"clash-api-port"` + ClashApiSecret string `json:"clash-api-secret"` GeoIPPath string `json:"geoip-path"` GeoSitePath string `json:"geosite-path"` Rules []Rule `json:"rules"` @@ -117,9 +118,10 @@ func DefaultConfigOptions() *ConfigOptions { BypassLAN: false, AllowConnectionFromLAN: false, }, - LogLevel: "info", + LogLevel: "warn", EnableClashApi: true, ClashApiPort: 6756, + ClashApiSecret: "hiddify", GeoIPPath: "geoip.db", GeoSitePath: "geosite.db", Rules: []Rule{}, diff --git a/v2/coreinfo.go b/v2/coreinfo.go index 2d9daca..ec6204a 100644 --- a/v2/coreinfo.go +++ b/v2/coreinfo.go @@ -9,12 +9,15 @@ import ( pb "github.com/hiddify/libcore/hiddifyrpc" ) -var EnableBridge = true var coreInfoObserver = NewObserver[pb.CoreInfoResponse](10) var CoreState = pb.CoreState_STOPPED func SetCoreStatus(state pb.CoreState, msgType pb.MessageType, message string) pb.CoreInfoResponse { - Log(pb.LogLevel_INFO, pb.LogType_CORE, fmt.Sprintf("%s: %s %s", state.String(), msgType.String(), message)) + msg := fmt.Sprintf("%s: %s %s", state.String(), msgType.String(), message) + if msgType == pb.MessageType_EMPTY { + msg = fmt.Sprintf("%s: %s", state.String(), message) + } + Log(pb.LogLevel_INFO, pb.LogType_CORE, msg) CoreState = state info := pb.CoreInfoResponse{ CoreState: state, @@ -22,7 +25,7 @@ func SetCoreStatus(state pb.CoreState, msgType pb.MessageType, message string) p Message: message, } coreInfoObserver.Emit(info) - if EnableBridge { + if useFlutterBridge { msg, _ := json.Marshal(StatusMessage{Status: convert2OldState(CoreState)}) bridge.SendStringToPort(statusPropagationPort, string(msg)) } @@ -41,9 +44,9 @@ func (s *CoreService) CoreInfoListener(stream pb.Core_CoreInfoListenerServer) er for { select { case <-stream.Context().Done(): - break + return nil case <-stopch: - break + return nil case info := <-coreSub: stream.Send(&info) case <-time.After(500 * time.Millisecond): diff --git a/v2/custom.go b/v2/custom.go index 21151cb..eb218f5 100644 --- a/v2/custom.go +++ b/v2/custom.go @@ -15,10 +15,13 @@ import ( "github.com/sagernet/sing-box/log" ) -var Box *libbox.BoxService -var configOptions *config.ConfigOptions -var activeConfigPath *string -var coreLogFactory log.Factory +var ( + Box *libbox.BoxService + configOptions *config.ConfigOptions + activeConfigPath string + coreLogFactory log.Factory + useFlutterBridge bool = true +) func StopAndAlert(msgType pb.MessageType, message string) { SetCoreStatus(pb.CoreState_STOPPED, msgType, message) @@ -33,7 +36,7 @@ func StopAndAlert(msgType pb.MessageType, message string) { if oldCommandServer != nil { oldCommandServer.Close() } - if EnableBridge { + if useFlutterBridge { alert := msgType.String() msg, _ := json.Marshal(StatusMessage{Status: convert2OldState(CoreState), Alert: &alert, Message: &message}) bridge.SendStringToPort(statusPropagationPort, string(msg)) @@ -57,9 +60,8 @@ func Start(in *pb.StartRequest) (*pb.CoreInfoResponse, error) { MessageType: pb.MessageType_INSTANCE_NOT_STOPPED, }, fmt.Errorf("instance not stopped") } - Log(pb.LogLevel_INFO, pb.LogType_CORE, "Starting1111") - SetCoreStatus(pb.CoreState_STARTING, pb.MessageType_EMPTY, "Starting222") - Log(pb.LogLevel_INFO, pb.LogType_CORE, "Starting2") + Log(pb.LogLevel_DEBUG, pb.LogType_CORE, "Starting Core") + SetCoreStatus(pb.CoreState_STARTING, pb.MessageType_EMPTY, "") libbox.SetMemoryLimit(!in.DisableMemoryLimit) resp, err := StartService(in) return resp, err @@ -68,14 +70,12 @@ func (s *CoreService) StartService(ctx context.Context, in *pb.StartRequest) (*p return StartService(in) } func StartService(in *pb.StartRequest) (*pb.CoreInfoResponse, error) { - Log(pb.LogLevel_INFO, pb.LogType_CORE, "Starting3") + Log(pb.LogLevel_DEBUG, pb.LogType_CORE, "Starting Core Service") content := in.ConfigContent if content == "" { - if in.ConfigPath != "" { - activeConfigPath = &in.ConfigPath - } - fileContent, err := os.ReadFile(*activeConfigPath) + activeConfigPath = in.ConfigPath + fileContent, err := os.ReadFile(activeConfigPath) if err != nil { Log(pb.LogLevel_FATAL, pb.LogType_CORE, err.Error()) resp := SetCoreStatus(pb.CoreState_STOPPED, pb.MessageType_ERROR_READING_CONFIG, err.Error()) @@ -84,10 +84,10 @@ func StartService(in *pb.StartRequest) (*pb.CoreInfoResponse, error) { } content = string(fileContent) } - Log(pb.LogLevel_INFO, pb.LogType_CORE, "Starting4") - Log(pb.LogLevel_INFO, pb.LogType_CORE, content) + Log(pb.LogLevel_DEBUG, pb.LogType_CORE, "Parsing Config") + parsedContent, err := parseConfig(content) - Log(pb.LogLevel_INFO, pb.LogType_CORE, "Parsed") + Log(pb.LogLevel_DEBUG, pb.LogType_CORE, "Parsed") if err != nil { Log(pb.LogLevel_FATAL, pb.LogType_CORE, err.Error()) @@ -96,20 +96,24 @@ func StartService(in *pb.StartRequest) (*pb.CoreInfoResponse, error) { return &resp, err } if !in.EnableRawConfig { - Log(pb.LogLevel_INFO, pb.LogType_CORE, "Building config") + Log(pb.LogLevel_DEBUG, pb.LogType_CORE, "Building config") parsedContent_tmp, err := config.BuildConfig(*configOptions, parsedContent) - parsedContent = *parsedContent_tmp if err != nil { Log(pb.LogLevel_FATAL, pb.LogType_CORE, err.Error()) resp := SetCoreStatus(pb.CoreState_STOPPED, pb.MessageType_ERROR_BUILDING_CONFIG, err.Error()) StopAndAlert(pb.MessageType_UNEXPECTED_ERROR, err.Error()) return &resp, err } + parsedContent = *parsedContent_tmp + } + Log(pb.LogLevel_DEBUG, pb.LogType_CORE, "Saving config") + currentBuildConfigPath := filepath.Join(sWorkingPath, "current-config.json") + config.SaveCurrentConfig(currentBuildConfigPath, parsedContent) + if activeConfigPath == "" { + activeConfigPath = currentBuildConfigPath } - Log(pb.LogLevel_INFO, pb.LogType_CORE, "Saving Contnet") - config.SaveCurrentConfig(filepath.Join(sWorkingPath, "current-config.json"), parsedContent) if in.EnableOldCommandServer { - Log(pb.LogLevel_INFO, pb.LogType_CORE, "Starting Command Server") + Log(pb.LogLevel_DEBUG, pb.LogType_CORE, "Starting Command Server") err = startCommandServer() if err != nil { Log(pb.LogLevel_FATAL, pb.LogType_CORE, err.Error()) @@ -119,7 +123,7 @@ func StartService(in *pb.StartRequest) (*pb.CoreInfoResponse, error) { } } - Log(pb.LogLevel_INFO, pb.LogType_CORE, "Stating Service ") + Log(pb.LogLevel_DEBUG, pb.LogType_CORE, "Stating Service ") instance, err := NewService(parsedContent) if err != nil { @@ -128,7 +132,7 @@ func StartService(in *pb.StartRequest) (*pb.CoreInfoResponse, error) { StopAndAlert(pb.MessageType_UNEXPECTED_ERROR, err.Error()) return &resp, err } - Log(pb.LogLevel_INFO, pb.LogType_CORE, "Service.. started") + Log(pb.LogLevel_DEBUG, pb.LogType_CORE, "Service.. started") if in.DelayStart { <-time.After(250 * time.Millisecond) } diff --git a/v2/logproto.go b/v2/logproto.go index fbfe3ee..e9c80c1 100644 --- a/v2/logproto.go +++ b/v2/logproto.go @@ -15,7 +15,10 @@ func NewObserver[T any](listenerBufferSize int) *observable.Observer[T] { var logObserver = NewObserver[pb.LogMessage](10) func Log(level pb.LogLevel, typ pb.LogType, message string) { - fmt.Printf("%s %s %s\n", level, typ, message) + if level != pb.LogLevel_DEBUG { + fmt.Printf("%s %s %s\n", level, typ, message) + + } logObserver.Emit(pb.LogMessage{ Level: level, Type: typ, diff --git a/v2/standalone.go b/v2/standalone.go index 5cbb5c9..6cce729 100644 --- a/v2/standalone.go +++ b/v2/standalone.go @@ -21,6 +21,7 @@ import ( func RunStandalone(hiddifySettingPath string, configPath string) error { fmt.Println("Running in standalone mode") + useFlutterBridge = false current, err := readAndBuildConfig(hiddifySettingPath, configPath) if err != nil { fmt.Printf("Error in read and build config %v", err) @@ -28,17 +29,19 @@ func RunStandalone(hiddifySettingPath string, configPath string) error { } go StartService(&pb.StartRequest{ - ConfigPath: configPath, + ConfigContent: current.Config, EnableOldCommandServer: false, DelayStart: false, + EnableRawConfig: true, }) go updateConfigInterval(current, hiddifySettingPath, configPath) - fmt.Printf("Press CTRL+C to stop\n") - fmt.Printf("Open http://localhost:6756/?secret=hiddify in your browser\n") + sigChan := make(chan os.Signal, 1) signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM) + fmt.Printf("Waiting for CTRL+C to stop\n") <-sigChan + fmt.Printf("CTRL+C recived-->stopping\n") _, err = Stop() return err @@ -63,6 +66,7 @@ func readAndBuildConfig(hiddifySettingPath string, configPath string) (ConfigRes return result, err } } + configOptions = hiddifyconfig result.Config, err = buildConfig(result.Config, *hiddifyconfig) if err != nil { return result, err @@ -161,9 +165,7 @@ func buildConfig(configContent string, options config.ConfigOptions) (string, er finalconfig.Experimental.ClashAPI.ExternalController = "127.0.0.1:6756" } - if finalconfig.Experimental.ClashAPI.Secret == "" { - // finalconfig.Experimental.ClashAPI.Secret = "hiddify" - } + fmt.Printf("Open http://localhost:6756/ui/?secret=%s in your browser\n", finalconfig.Experimental.ClashAPI.Secret) if err := Setup("./", "./", "./tmp", 0, false); err != nil { return "", fmt.Errorf("failed to set up global configuration: %w", err) @@ -195,6 +197,7 @@ func updateConfigInterval(current ConfigResult, hiddifySettingPath string, confi DelayStart: false, EnableOldCommandServer: false, DisableMemoryLimit: false, + EnableRawConfig: true, }) } current = new diff --git a/v2/tunnel_service.go b/v2/tunnel_service.go index 14f6f3c..e1eddd4 100644 --- a/v2/tunnel_service.go +++ b/v2/tunnel_service.go @@ -13,7 +13,7 @@ func (s *TunnelService) Start(ctx context.Context, in *pb.TunnelStartRequest) (* if in.ServerPort == 0 { in.ServerPort = 2334 } - EnableBridge = false + useFlutterBridge = false res, err := Start(&pb.StartRequest{ ConfigContent: makeTunnelConfig(in.Ipv6, in.ServerPort, in.StrictRoute, in.EndpointIndependentNat, in.Stack), EnableOldCommandServer: false,