Browse Source

feat: pre and post period script

v1.x.x
dnomd343 2 years ago
parent
commit
0b04fde1a2
  1. 14
      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

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

3
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) {

8
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)

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 (
"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!")
}

Loading…
Cancel
Save