Browse Source

update: interface refactor

v1.x.x
dnomd343 2 years ago
parent
commit
80f2a95463
  1. 20
      cmd/config/decode.go
  2. 13
      cmd/config/main.go
  3. 28
      cmd/controller.go
  4. 21
      cmd/xproxy.go

20
cmd/config/decode.go

@ -114,21 +114,7 @@ func decodeUpdate(rawConfig *yamlConfig, config *Config) {
log.Debugf("Update urls -> %v", config.Update.Url) log.Debugf("Update urls -> %v", config.Update.Url)
} }
func decodeCustom(rawConfig *yamlConfig) []string { func decodeCustom(rawConfig *yamlConfig, config *Config) {
customScript := rawConfig.Custom config.Script = rawConfig.Custom
log.Debugf("Custom script -> %v", customScript) log.Debugf("Custom script -> %v", config.Script)
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
} }

13
cmd/config/main.go

@ -14,15 +14,13 @@ type Config struct {
DNS []string DNS []string
IPv4 network.Config IPv4 network.Config
IPv6 network.Config IPv6 network.Config
Script []string Script []string
Proxy proxy.Config Proxy proxy.Config
Update asset.Config Update asset.Config
Radvd radvd.Config Radvd radvd.Config
} }
func Load(configFile string) Config { func Load(configFile string, config *Config) {
if !common.IsFileExist(configFile) { // configure not exist -> load default if !common.IsFileExist(configFile) { // configure not exist -> load default
log.Infof("Load default configure -> %s", configFile) log.Infof("Load default configure -> %s", configFile)
common.WriteFile(configFile, defaultConfig, false) common.WriteFile(configFile, defaultConfig, false)
@ -32,5 +30,12 @@ func Load(configFile string) Config {
log.Panicf("Failed to open %s -> %v", configFile, err) log.Panicf("Failed to open %s -> %v", configFile, err)
} }
rawConfig := yamlDecode(raw) // decode yaml content 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)
} }

28
cmd/controller.go

@ -14,6 +14,19 @@ import (
"syscall" "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) { func loadRadvd(settings *config.Config) {
radvd.Load(&settings.Radvd) radvd.Load(&settings.Radvd)
} }
@ -44,15 +57,12 @@ func runScript(settings *config.Config) {
} }
} }
func runProcess(command ...string) { func runProxy(settings *config.Config) {
sub := process.New(command...) runProcess("xray", "-confdir", configDir)
sub.Run(true)
sub.Daemon()
subProcess = append(subProcess, sub)
} }
func blockWait() { func runRadvd(settings *config.Config) {
sigExit := make(chan os.Signal, 1) if settings.Radvd.Enable {
signal.Notify(sigExit, syscall.SIGINT, syscall.SIGTERM) // wait until get exit signal runProcess("radvd", "-n", "-m", "logfile", "-l", exposeDir+"/log/radvd.log")
<-sigExit }
} }

21
cmd/xproxy.go

@ -6,7 +6,7 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
var version = "0.0.9" var version = "0.1.0"
var v4RouteTable = 100 var v4RouteTable = 100
var v6RouteTable = 106 var v6RouteTable = 106
@ -21,27 +21,32 @@ var configFile = exposeDir + "/config.yml"
var subProcess []*process.Process 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() { func main() {
defer func() { defer func() {
if err := recover(); err != nil { if err := recover(); err != nil {
log.Errorf("Panic exit -> %v", err) log.Errorf("Panic exit -> %v", err)
} }
}() }()
xproxyInit()
log.SetLevel(log.DebugLevel) var settings config.Config
log.Infof("XProxy %s start", version) log.Infof("XProxy %s start", version)
config.Load(configFile, &settings)
settings := config.Load(configFile)
loadNetwork(&settings) loadNetwork(&settings)
loadProxy(&settings) loadProxy(&settings)
loadAsset(&settings) loadAsset(&settings)
loadRadvd(&settings) loadRadvd(&settings)
runScript(&settings) runScript(&settings)
runProcess("xray", "-confdir", configDir) runProxy(&settings)
if settings.Radvd.Enable { runRadvd(&settings)
runProcess("radvd", "-n", "-m", "logfile", "-l", exposeDir+"/log/radvd.log")
}
blockWait() blockWait()
process.Exit(subProcess...) process.Exit(subProcess...)
} }

Loading…
Cancel
Save