Browse Source

feat: pre and post period script

v1.x.x
dnomd343 2 years ago
parent
commit
0b04fde1a2
  1. 8
      cmd/config/decode.go
  2. 3
      cmd/config/main.go
  3. 8
      cmd/controller.go
  4. 25
      cmd/custom/main.go
  5. 7
      cmd/xproxy.go

8
cmd/config/decode.go

@ -3,6 +3,7 @@ package config
import ( import (
"XProxy/cmd/asset" "XProxy/cmd/asset"
"XProxy/cmd/common" "XProxy/cmd/common"
"XProxy/cmd/custom"
"XProxy/cmd/proxy" "XProxy/cmd/proxy"
"XProxy/cmd/radvd" "XProxy/cmd/radvd"
"encoding/json" "encoding/json"
@ -17,10 +18,10 @@ type NetConfig struct {
} }
type RawConfig struct { type RawConfig struct {
Custom []string `yaml:"custom" json:"custom"`
Update asset.Config `yaml:"update" json:"update"` Update asset.Config `yaml:"update" json:"update"`
Radvd radvd.Config `yaml:"radvd" json:"radvd"` Radvd radvd.Config `yaml:"radvd" json:"radvd"`
Proxy proxy.Config `yaml:"proxy" json:"proxy"` Proxy proxy.Config `yaml:"proxy" json:"proxy"`
Custom custom.Config `yaml:"custom" json:"custom"`
Network struct { Network struct {
Dev string `yaml:"dev" json:"dev"` Dev string `yaml:"dev" json:"dev"`
DNS []string `yaml:"dns" json:"dns"` DNS []string `yaml:"dns" json:"dns"`
@ -176,6 +177,7 @@ func decodeUpdate(rawConfig *RawConfig, config *Config) {
} }
func decodeCustom(rawConfig *RawConfig, config *Config) { func decodeCustom(rawConfig *RawConfig, config *Config) {
config.Script = rawConfig.Custom config.Custom = rawConfig.Custom
log.Debugf("Custom script -> %v", config.Script) log.Debugf("Custom pre-script -> %v", config.Custom.Pre)
log.Debugf("Custom post-script -> %v", config.Custom.Post)
} }

3
cmd/config/main.go

@ -3,6 +3,7 @@ package config
import ( import (
"XProxy/cmd/asset" "XProxy/cmd/asset"
"XProxy/cmd/common" "XProxy/cmd/common"
"XProxy/cmd/custom"
"XProxy/cmd/network" "XProxy/cmd/network"
"XProxy/cmd/proxy" "XProxy/cmd/proxy"
"XProxy/cmd/radvd" "XProxy/cmd/radvd"
@ -16,10 +17,10 @@ type Config struct {
DNS []string DNS []string
IPv4 network.Config IPv4 network.Config
IPv6 network.Config IPv6 network.Config
Script []string
Proxy proxy.Config Proxy proxy.Config
Update asset.Config Update asset.Config
Radvd radvd.Config Radvd radvd.Config
Custom custom.Config
} }
func Load(configFile string, config *Config) { func Load(configFile string, config *Config) {

8
cmd/controller.go

@ -2,7 +2,6 @@ package main
import ( import (
"XProxy/cmd/asset" "XProxy/cmd/asset"
"XProxy/cmd/common"
"XProxy/cmd/config" "XProxy/cmd/config"
"XProxy/cmd/network" "XProxy/cmd/network"
"XProxy/cmd/process" "XProxy/cmd/process"
@ -52,13 +51,6 @@ func loadProxy(settings *config.Config) {
proxy.Load(configDir, exposeDir, &settings.Proxy) 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) { func runProxy(settings *config.Config) {
if settings.Proxy.Core == "xray" { // xray-core if settings.Proxy.Core == "xray" { // xray-core
runProcess([]string{"XRAY_LOCATION_ASSET=" + assetDir}, "xray", "-confdir", configDir) runProcess([]string{"XRAY_LOCATION_ASSET=" + assetDir}, "xray", "-confdir", configDir)

25
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)
}
}

7
cmd/xproxy.go

@ -3,6 +3,7 @@ package main
import ( import (
"XProxy/cmd/common" "XProxy/cmd/common"
"XProxy/cmd/config" "XProxy/cmd/config"
"XProxy/cmd/custom"
"XProxy/cmd/process" "XProxy/cmd/process"
"flag" "flag"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@ -83,7 +84,6 @@ func main() {
} }
}() }()
xproxyInit() xproxyInit()
var settings config.Config var settings config.Config
log.Infof("XProxy %s start (%s)", version, goVersion) log.Infof("XProxy %s start (%s)", version, goVersion)
// TODO: load dhcp configure // TODO: load dhcp configure
@ -92,11 +92,12 @@ func main() {
loadProxy(&settings) loadProxy(&settings)
loadAsset(&settings) loadAsset(&settings)
loadRadvd(&settings) loadRadvd(&settings)
custom.RunPreScript(&settings.Custom)
runScript(&settings)
// TODO: run dhcp service // TODO: run dhcp service
runRadvd(&settings) runRadvd(&settings)
runProxy(&settings) runProxy(&settings)
blockWait() blockWait()
process.Exit(subProcess...) process.Exit(subProcess...)
custom.RunPostScript(&settings.Custom)
log.Infof("All done, goodbye!")
} }

Loading…
Cancel
Save