From da853f781a541e78c9a8fa103cddde3611bee51c Mon Sep 17 00:00:00 2001 From: xishang0128 Date: Tue, 3 Sep 2024 17:14:18 +0800 Subject: [PATCH] feat: Allow passing in configuration strings --- hub/executor/executor.go | 5 +++- hub/hub.go | 4 +-- main.go | 54 +++++++++++++++++++++++++++------------- 3 files changed, 43 insertions(+), 20 deletions(-) diff --git a/hub/executor/executor.go b/hub/executor/executor.go index c83b254c9..54ec67ff7 100644 --- a/hub/executor/executor.go +++ b/hub/executor/executor.go @@ -58,7 +58,10 @@ func readConfig(path string) ([]byte, error) { } // Parse config with default config path -func Parse() (*config.Config, error) { +func Parse(config string) (*config.Config, error) { + if config != "" { + return ParseWithBytes([]byte(config)) + } return ParseWithPath(C.Path.Config()) } diff --git a/hub/hub.go b/hub/hub.go index d439d32e3..7a4afb2c5 100644 --- a/hub/hub.go +++ b/hub/hub.go @@ -64,8 +64,8 @@ func applyRoute(cfg *config.Config) { } // Parse call at the beginning of mihomo -func Parse(options ...Option) error { - cfg, err := executor.Parse() +func Parse(config string, options ...Option) error { + cfg, err := executor.Parse(config) if err != nil { return err } diff --git a/main.go b/main.go index c7a7acbc3..e3004529c 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "encoding/base64" "flag" "fmt" "os" @@ -28,6 +29,7 @@ var ( geodataMode bool homeDir string configFile string + configString string externalUI string externalController string externalControllerUnix string @@ -37,6 +39,7 @@ var ( func init() { flag.StringVar(&homeDir, "d", os.Getenv("CLASH_HOME_DIR"), "set configuration directory") flag.StringVar(&configFile, "f", os.Getenv("CLASH_CONFIG_FILE"), "specify configuration file") + flag.StringVar(&configString, "config", os.Getenv("CLASH_CONFIG_STRING"), "specify configuration base64 string") flag.StringVar(&externalUI, "ext-ui", os.Getenv("CLASH_OVERRIDE_EXTERNAL_UI_DIR"), "override external ui directory") flag.StringVar(&externalController, "ext-ctl", os.Getenv("CLASH_OVERRIDE_EXTERNAL_CONTROLLER"), "override external controller address") flag.StringVar(&externalControllerUnix, "ext-ctl-unix", os.Getenv("CLASH_OVERRIDE_EXTERNAL_CONTROLLER_UNIX"), "override external controller unix address") @@ -73,26 +76,35 @@ func main() { C.SetHomeDir(homeDir) } - if configFile != "" { - if !filepath.IsAbs(configFile) { - currentDir, _ := os.Getwd() - configFile = filepath.Join(currentDir, configFile) - } - } else { - configFile = filepath.Join(C.Path.HomeDir(), C.Path.Config()) - } - C.SetConfig(configFile) - if geodataMode { C.GeodataMode = true } - if err := config.Init(C.Path.HomeDir()); err != nil { - log.Fatalln("Initial configuration directory error: %s", err.Error()) + if configString != "" { + decodedBytes, err := base64.StdEncoding.DecodeString(configString) + if err != nil { + log.Fatalln("Initial configuration error: %s", err.Error()) + return + } + configString = string(decodedBytes) + } else { + if configFile != "" { + if !filepath.IsAbs(configFile) { + currentDir, _ := os.Getwd() + configFile = filepath.Join(currentDir, configFile) + } + } else { + configFile = filepath.Join(C.Path.HomeDir(), C.Path.Config()) + } + C.SetConfig(configFile) + + if err := config.Init(C.Path.HomeDir()); err != nil { + log.Fatalln("Initial configuration directory error: %s", err.Error()) + } } if testConfig { - if _, err := executor.Parse(); err != nil { + if _, err := executor.Parse(configString); err != nil { log.Errorln(err.Error()) fmt.Printf("configuration file %s test failed\n", C.Path.Config()) os.Exit(1) @@ -115,7 +127,7 @@ func main() { options = append(options, hub.WithSecret(secret)) } - if err := hub.Parse(options...); err != nil { + if err := hub.Parse(configString, options...); err != nil { log.Fatalln("Parse config error: %s", err.Error()) } @@ -134,10 +146,18 @@ func main() { case <-termSign: return case <-hupSign: - if cfg, err := executor.ParseWithPath(C.Path.Config()); err == nil { - hub.ApplyConfig(cfg) + if configString != "" { + if cfg, err := executor.ParseWithBytes([]byte(configString)); err == nil { + hub.ApplyConfig(cfg) + } else { + log.Errorln("Parse config error: %s", err.Error()) + } } else { - log.Errorln("Parse config error: %s", err.Error()) + if cfg, err := executor.ParseWithPath(C.Path.Config()); err == nil { + hub.ApplyConfig(cfg) + } else { + log.Errorln("Parse config error: %s", err.Error()) + } } } }