Browse Source

update: interface refactor

v1.x.x
dnomd343 2 years ago
parent
commit
80f2a95463
  1. 20
      cmd/config/decode.go
  2. 19
      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)
}
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)
}

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

28
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")
}
}

21
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...)
}

Loading…
Cancel
Save