mirror of
https://gitclone.com/github.com/MetaCubeX/Clash.Meta
synced 2025-02-23 12:03:14 +08:00
feat: Allows passing in base64-encoded configuration strings
This commit is contained in:
parent
43f21c0b41
commit
faaa90f8a6
12
hub/hub.go
12
hub/hub.go
@ -64,8 +64,16 @@ func applyRoute(cfg *config.Config) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Parse call at the beginning of mihomo
|
// Parse call at the beginning of mihomo
|
||||||
func Parse(options ...Option) error {
|
func Parse(configBytes []byte, options ...Option) error {
|
||||||
cfg, err := executor.Parse()
|
var cfg *config.Config
|
||||||
|
var err error
|
||||||
|
|
||||||
|
if len(configBytes) != 0 {
|
||||||
|
cfg, err = executor.ParseWithBytes(configBytes)
|
||||||
|
} else {
|
||||||
|
cfg, err = executor.Parse()
|
||||||
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
65
main.go
65
main.go
@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/base64"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
@ -28,6 +29,8 @@ var (
|
|||||||
geodataMode bool
|
geodataMode bool
|
||||||
homeDir string
|
homeDir string
|
||||||
configFile string
|
configFile string
|
||||||
|
configString string
|
||||||
|
configBytes []byte
|
||||||
externalUI string
|
externalUI string
|
||||||
externalController string
|
externalController string
|
||||||
externalControllerUnix string
|
externalControllerUnix string
|
||||||
@ -37,6 +40,7 @@ var (
|
|||||||
func init() {
|
func init() {
|
||||||
flag.StringVar(&homeDir, "d", os.Getenv("CLASH_HOME_DIR"), "set configuration directory")
|
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(&configFile, "f", os.Getenv("CLASH_CONFIG_FILE"), "specify configuration file")
|
||||||
|
flag.StringVar(&configString, "config", os.Getenv("CLASH_CONFIG_STRING"), "specify base64-encoded configuration string")
|
||||||
flag.StringVar(&externalUI, "ext-ui", os.Getenv("CLASH_OVERRIDE_EXTERNAL_UI_DIR"), "override external ui directory")
|
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(&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")
|
flag.StringVar(&externalControllerUnix, "ext-ctl-unix", os.Getenv("CLASH_OVERRIDE_EXTERNAL_CONTROLLER_UNIX"), "override external controller unix address")
|
||||||
@ -73,29 +77,46 @@ func main() {
|
|||||||
C.SetHomeDir(homeDir)
|
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 {
|
if geodataMode {
|
||||||
C.GeodataMode = true
|
C.GeodataMode = true
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := config.Init(C.Path.HomeDir()); err != nil {
|
if configString != "" {
|
||||||
log.Fatalln("Initial configuration directory error: %s", err.Error())
|
var err error
|
||||||
|
configBytes, err = base64.StdEncoding.DecodeString(configString)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln("Initial configuration error: %s", err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} 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 testConfig {
|
||||||
if _, err := executor.Parse(); err != nil {
|
if len(configBytes) != 0 {
|
||||||
log.Errorln(err.Error())
|
if _, err := executor.ParseWithBytes(configBytes); err != nil {
|
||||||
fmt.Printf("configuration file %s test failed\n", C.Path.Config())
|
log.Errorln(err.Error())
|
||||||
os.Exit(1)
|
fmt.Println("configuration test failed")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if _, err := executor.Parse(); err != nil {
|
||||||
|
log.Errorln(err.Error())
|
||||||
|
fmt.Printf("configuration file %s test failed\n", C.Path.Config())
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
fmt.Printf("configuration file %s test is successful\n", C.Path.Config())
|
fmt.Printf("configuration file %s test is successful\n", C.Path.Config())
|
||||||
return
|
return
|
||||||
@ -115,7 +136,7 @@ func main() {
|
|||||||
options = append(options, hub.WithSecret(secret))
|
options = append(options, hub.WithSecret(secret))
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := hub.Parse(options...); err != nil {
|
if err := hub.Parse(configBytes, options...); err != nil {
|
||||||
log.Fatalln("Parse config error: %s", err.Error())
|
log.Fatalln("Parse config error: %s", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -134,11 +155,19 @@ func main() {
|
|||||||
case <-termSign:
|
case <-termSign:
|
||||||
return
|
return
|
||||||
case <-hupSign:
|
case <-hupSign:
|
||||||
if cfg, err := executor.ParseWithPath(C.Path.Config()); err == nil {
|
var cfg *config.Config
|
||||||
|
var err error
|
||||||
|
if configString != "" {
|
||||||
|
cfg, err = executor.ParseWithBytes(configBytes)
|
||||||
|
} else {
|
||||||
|
cfg, err = executor.ParseWithPath(C.Path.Config())
|
||||||
|
}
|
||||||
|
if err == nil {
|
||||||
hub.ApplyConfig(cfg)
|
hub.ApplyConfig(cfg)
|
||||||
} else {
|
} else {
|
||||||
log.Errorln("Parse config error: %s", err.Error())
|
log.Errorln("Parse config error: %s", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user