From 80f2a954634ce871ffb17afd5651918624b77a86 Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Thu, 18 Aug 2022 21:53:16 +0800 Subject: [PATCH] update: interface refactor --- cmd/config/decode.go | 20 +++----------------- cmd/config/main.go | 19 ++++++++++++------- cmd/controller.go | 28 +++++++++++++++++++--------- cmd/xproxy.go | 21 +++++++++++++-------- 4 files changed, 47 insertions(+), 41 deletions(-) diff --git a/cmd/config/decode.go b/cmd/config/decode.go index 8446a14..fe31904 100644 --- a/cmd/config/decode.go +++ b/cmd/config/decode.go @@ -114,21 +114,7 @@ func decodeUpdate(rawConfig *yamlConfig, config *Config) { log.Debugf("Update urls -> %v", config.Update.Url) } -func decodeCustom(rawConfig *yamlConfig) []string { - customScript := rawConfig.Custom - log.Debugf("Custom script -> %v", customScript) - return customScript -} - -func decode(rawConfig yamlConfig) Config { - var config Config - decodeDns(&rawConfig, &config) - decodeBypass(&rawConfig, &config) - decodeIPv4(&rawConfig, &config) - decodeIPv6(&rawConfig, &config) - decodeProxy(&rawConfig, &config) - decodeUpdate(&rawConfig, &config) - config.Script = decodeCustom(&rawConfig) - decodeRadvd(&rawConfig, &config) - return config +func decodeCustom(rawConfig *yamlConfig, config *Config) { + config.Script = rawConfig.Custom + log.Debugf("Custom script -> %v", config.Script) } diff --git a/cmd/config/main.go b/cmd/config/main.go index 9743bd7..4039f6e 100644 --- a/cmd/config/main.go +++ b/cmd/config/main.go @@ -11,18 +11,16 @@ import ( ) type Config struct { - DNS []string - IPv4 network.Config - IPv6 network.Config - + DNS []string + IPv4 network.Config + IPv6 network.Config Script []string - Proxy proxy.Config Update asset.Config Radvd radvd.Config } -func Load(configFile string) Config { +func Load(configFile string, config *Config) { if !common.IsFileExist(configFile) { // configure not exist -> load default log.Infof("Load default configure -> %s", configFile) common.WriteFile(configFile, defaultConfig, false) @@ -32,5 +30,12 @@ func Load(configFile string) Config { log.Panicf("Failed to open %s -> %v", configFile, err) } rawConfig := yamlDecode(raw) // decode yaml content - return decode(rawConfig) + decodeDns(&rawConfig, config) + decodeBypass(&rawConfig, config) + decodeIPv4(&rawConfig, config) + decodeIPv6(&rawConfig, config) + decodeProxy(&rawConfig, config) + decodeUpdate(&rawConfig, config) + decodeCustom(&rawConfig, config) + decodeRadvd(&rawConfig, config) } diff --git a/cmd/controller.go b/cmd/controller.go index 4ad6e96..979b280 100644 --- a/cmd/controller.go +++ b/cmd/controller.go @@ -14,6 +14,19 @@ import ( "syscall" ) +func runProcess(command ...string) { + sub := process.New(command...) + sub.Run(true) + sub.Daemon() + subProcess = append(subProcess, sub) +} + +func blockWait() { + sigExit := make(chan os.Signal, 1) + signal.Notify(sigExit, syscall.SIGINT, syscall.SIGTERM) // wait until get exit signal + <-sigExit +} + func loadRadvd(settings *config.Config) { radvd.Load(&settings.Radvd) } @@ -44,15 +57,12 @@ func runScript(settings *config.Config) { } } -func runProcess(command ...string) { - sub := process.New(command...) - sub.Run(true) - sub.Daemon() - subProcess = append(subProcess, sub) +func runProxy(settings *config.Config) { + runProcess("xray", "-confdir", configDir) } -func blockWait() { - sigExit := make(chan os.Signal, 1) - signal.Notify(sigExit, syscall.SIGINT, syscall.SIGTERM) // wait until get exit signal - <-sigExit +func runRadvd(settings *config.Config) { + if settings.Radvd.Enable { + runProcess("radvd", "-n", "-m", "logfile", "-l", exposeDir+"/log/radvd.log") + } } diff --git a/cmd/xproxy.go b/cmd/xproxy.go index f2c798a..9049b4e 100644 --- a/cmd/xproxy.go +++ b/cmd/xproxy.go @@ -6,7 +6,7 @@ import ( log "github.com/sirupsen/logrus" ) -var version = "0.0.9" +var version = "0.1.0" var v4RouteTable = 100 var v6RouteTable = 106 @@ -21,27 +21,32 @@ var configFile = exposeDir + "/config.yml" var subProcess []*process.Process +func xproxyInit() { + // log format + // TODO: set log level + log.SetLevel(log.DebugLevel) + // read tproxy port / route table num from env +} + func main() { defer func() { if err := recover(); err != nil { log.Errorf("Panic exit -> %v", err) } }() + xproxyInit() - log.SetLevel(log.DebugLevel) + var settings config.Config log.Infof("XProxy %s start", version) - - settings := config.Load(configFile) + config.Load(configFile, &settings) loadNetwork(&settings) loadProxy(&settings) loadAsset(&settings) loadRadvd(&settings) runScript(&settings) - runProcess("xray", "-confdir", configDir) - if settings.Radvd.Enable { - runProcess("radvd", "-n", "-m", "logfile", "-l", exposeDir+"/log/radvd.log") - } + runProxy(&settings) + runRadvd(&settings) blockWait() process.Exit(subProcess...) }