From 0b04fde1a2e0c1e032117deb99351c012bc5a467 Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Sat, 27 Aug 2022 19:58:00 +0800 Subject: [PATCH] feat: pre and post period script --- cmd/config/decode.go | 14 ++++++++------ cmd/config/main.go | 3 ++- cmd/controller.go | 8 -------- cmd/custom/main.go | 25 +++++++++++++++++++++++++ cmd/xproxy.go | 7 ++++--- 5 files changed, 39 insertions(+), 18 deletions(-) create mode 100644 cmd/custom/main.go diff --git a/cmd/config/decode.go b/cmd/config/decode.go index cfe2381..c3dcb78 100644 --- a/cmd/config/decode.go +++ b/cmd/config/decode.go @@ -3,6 +3,7 @@ package config import ( "XProxy/cmd/asset" "XProxy/cmd/common" + "XProxy/cmd/custom" "XProxy/cmd/proxy" "XProxy/cmd/radvd" "encoding/json" @@ -17,10 +18,10 @@ type NetConfig struct { } type RawConfig struct { - Custom []string `yaml:"custom" json:"custom"` - Update asset.Config `yaml:"update" json:"update"` - Radvd radvd.Config `yaml:"radvd" json:"radvd"` - Proxy proxy.Config `yaml:"proxy" json:"proxy"` + Update asset.Config `yaml:"update" json:"update"` + Radvd radvd.Config `yaml:"radvd" json:"radvd"` + Proxy proxy.Config `yaml:"proxy" json:"proxy"` + Custom custom.Config `yaml:"custom" json:"custom"` Network struct { Dev string `yaml:"dev" json:"dev"` DNS []string `yaml:"dns" json:"dns"` @@ -176,6 +177,7 @@ func decodeUpdate(rawConfig *RawConfig, config *Config) { } func decodeCustom(rawConfig *RawConfig, config *Config) { - config.Script = rawConfig.Custom - log.Debugf("Custom script -> %v", config.Script) + config.Custom = rawConfig.Custom + log.Debugf("Custom pre-script -> %v", config.Custom.Pre) + log.Debugf("Custom post-script -> %v", config.Custom.Post) } diff --git a/cmd/config/main.go b/cmd/config/main.go index df9b451..8d448f3 100644 --- a/cmd/config/main.go +++ b/cmd/config/main.go @@ -3,6 +3,7 @@ package config import ( "XProxy/cmd/asset" "XProxy/cmd/common" + "XProxy/cmd/custom" "XProxy/cmd/network" "XProxy/cmd/proxy" "XProxy/cmd/radvd" @@ -16,10 +17,10 @@ type Config struct { DNS []string IPv4 network.Config IPv6 network.Config - Script []string Proxy proxy.Config Update asset.Config Radvd radvd.Config + Custom custom.Config } func Load(configFile string, config *Config) { diff --git a/cmd/controller.go b/cmd/controller.go index 9d84f9d..a9cc7f5 100644 --- a/cmd/controller.go +++ b/cmd/controller.go @@ -2,7 +2,6 @@ package main import ( "XProxy/cmd/asset" - "XProxy/cmd/common" "XProxy/cmd/config" "XProxy/cmd/network" "XProxy/cmd/process" @@ -52,13 +51,6 @@ func loadProxy(settings *config.Config) { proxy.Load(configDir, exposeDir, &settings.Proxy) } -func runScript(settings *config.Config) { - for _, script := range settings.Script { - log.Infof("Run script command -> %s", script) - common.RunCommand("sh", "-c", script) - } -} - func runProxy(settings *config.Config) { if settings.Proxy.Core == "xray" { // xray-core runProcess([]string{"XRAY_LOCATION_ASSET=" + assetDir}, "xray", "-confdir", configDir) diff --git a/cmd/custom/main.go b/cmd/custom/main.go new file mode 100644 index 0000000..fa65275 --- /dev/null +++ b/cmd/custom/main.go @@ -0,0 +1,25 @@ +package custom + +import ( + "XProxy/cmd/common" + log "github.com/sirupsen/logrus" +) + +type Config struct { + Pre []string `yaml:"pre" json:"pre"` + Post []string `yaml:"post" json:"post"` +} + +func RunPreScript(config *Config) { + for _, script := range config.Pre { + log.Infof("Run pre-script command -> %s", script) + common.RunCommand("sh", "-c", script) + } +} + +func RunPostScript(config *Config) { + for _, script := range config.Post { + log.Infof("Run post-script command -> %s", script) + common.RunCommand("sh", "-c", script) + } +} diff --git a/cmd/xproxy.go b/cmd/xproxy.go index c8fa252..62a8de0 100644 --- a/cmd/xproxy.go +++ b/cmd/xproxy.go @@ -3,6 +3,7 @@ package main import ( "XProxy/cmd/common" "XProxy/cmd/config" + "XProxy/cmd/custom" "XProxy/cmd/process" "flag" log "github.com/sirupsen/logrus" @@ -83,7 +84,6 @@ func main() { } }() xproxyInit() - var settings config.Config log.Infof("XProxy %s start (%s)", version, goVersion) // TODO: load dhcp configure @@ -92,11 +92,12 @@ func main() { loadProxy(&settings) loadAsset(&settings) loadRadvd(&settings) - - runScript(&settings) + custom.RunPreScript(&settings.Custom) // TODO: run dhcp service runRadvd(&settings) runProxy(&settings) blockWait() process.Exit(subProcess...) + custom.RunPostScript(&settings.Custom) + log.Infof("All done, goodbye!") }