From 97d9641a6977ceeac8d2a5e140d3e192263fb9bc Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Thu, 18 Aug 2022 14:09:51 +0800 Subject: [PATCH] feat: radvd configure --- cmd/config/decode.go | 15 +++++++++++++++ cmd/config/main.go | 4 ++++ cmd/controller.go | 5 +++++ cmd/radvd/main.go | 33 +++++++++++++++++++++++++++++++++ cmd/xproxy.go | 19 +++++++++++++++---- 5 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 cmd/radvd/main.go diff --git a/cmd/config/decode.go b/cmd/config/decode.go index b357218..bfc7a89 100644 --- a/cmd/config/decode.go +++ b/cmd/config/decode.go @@ -34,6 +34,11 @@ type yamlConfig struct { IPv4 yamlNetConfig `yaml:"ipv4"` // ipv4 network configure IPv6 yamlNetConfig `yaml:"ipv6"` // ipv6 network configure } `yaml:"network"` + Radvd struct { + Enable bool `yaml:"enable"` + Options map[string]string `yaml:"options"` + Prefix map[string]map[string]string `yaml:"prefix"` + } `yaml:"radvd"` } func yamlDecode(raw []byte) yamlConfig { @@ -116,6 +121,15 @@ func decodeProxy(rawConfig *yamlConfig, config *Config) { log.Debugf("Add-on inbounds -> %v", config.AddOnInbounds) } +func decodeRadvd(rawConfig *yamlConfig, config *Config) { + config.RadvdEnable = rawConfig.Radvd.Enable + log.Debugf("Radvd enable -> %t", config.RadvdEnable) + config.RadvdOptions = rawConfig.Radvd.Options + log.Debugf("Radvd options -> %v", config.RadvdOptions) + config.RadvdPrefix = rawConfig.Radvd.Prefix + log.Debugf("Radvd prefix -> %v", config.RadvdPrefix) +} + func decodeUpdate(rawConfig *yamlConfig) (string, map[string]string) { updateCron := rawConfig.Update.Cron log.Debugf("Update cron -> %s", updateCron) @@ -140,5 +154,6 @@ func decode(rawConfig yamlConfig) Config { decodeProxy(&rawConfig, &config) config.UpdateCron, config.UpdateUrls = decodeUpdate(&rawConfig) config.Script = decodeCustom(&rawConfig) + decodeRadvd(&rawConfig, &config) return config } diff --git a/cmd/config/main.go b/cmd/config/main.go index eaa51d0..92fc863 100644 --- a/cmd/config/main.go +++ b/cmd/config/main.go @@ -26,6 +26,10 @@ type Config struct { HttpInbounds map[string]int SocksInbounds map[string]int AddOnInbounds []interface{} + + RadvdEnable bool + RadvdOptions map[string]string + RadvdPrefix map[string]map[string]string } func Load(configFile string) Config { diff --git a/cmd/controller.go b/cmd/controller.go index 02f14c8..49331dd 100644 --- a/cmd/controller.go +++ b/cmd/controller.go @@ -6,6 +6,7 @@ import ( "XProxy/cmd/config" "XProxy/cmd/network" "XProxy/cmd/proxy" + "XProxy/cmd/radvd" log "github.com/sirupsen/logrus" ) @@ -46,6 +47,10 @@ func loadNetwork(settings *config.Config) { network.Load(settings.DNS, v4Settings, v6Settings) } +func loadRadvd(settings *config.Config) { + radvd.Load(settings.RadvdOptions, settings.RadvdPrefix) +} + func runScript(settings *config.Config) { for _, script := range settings.Script { log.Infof("Run script command -> %s", script) diff --git a/cmd/radvd/main.go b/cmd/radvd/main.go new file mode 100644 index 0000000..48e2715 --- /dev/null +++ b/cmd/radvd/main.go @@ -0,0 +1,33 @@ +package radvd + +import ( + "XProxy/cmd/common" + log "github.com/sirupsen/logrus" + "strings" +) + +func optionList(options map[string]string, intendNum int) string { + var result string + intend := strings.Repeat(" ", intendNum) + for option, value := range options { + result += intend + option + " " + value + ";\n" + } + return result +} + +func loadPrefix(prefix string, options map[string]string) string { + result := " prefix " + prefix + " {\n" + result += optionList(options, 8) + return result + " };\n" +} + +func Load(options map[string]string, prefixes map[string]map[string]string) { + radvdConfig := "interface eth0 {\n" + radvdConfig += optionList(options, 4) + for prefix, prefixOptions := range prefixes { + radvdConfig += loadPrefix(prefix, prefixOptions) + } + radvdConfig += "};\n" + log.Debugf("Radvd configure -> \n%s", radvdConfig) + common.WriteFile("/etc/radvd.conf", radvdConfig, true) +} diff --git a/cmd/xproxy.go b/cmd/xproxy.go index 4cea9ce..70a579f 100644 --- a/cmd/xproxy.go +++ b/cmd/xproxy.go @@ -32,6 +32,13 @@ func runProxy() { subProcess = append(subProcess, proxy) } +func runRadvd() { + radvd := process.New("radvd", "-n", "-m", "logfile", "-l", exposeDir+"/log/radvd.log") + radvd.Run(true) + radvd.Daemon() + subProcess = append(subProcess, radvd) +} + func blockWait() { sigExit := make(chan os.Signal, 1) signal.Notify(sigExit, syscall.SIGINT, syscall.SIGTERM) // wait until get exit signal @@ -49,12 +56,16 @@ func main() { fmt.Println("XProxy start -> version =", version) settings := config.Load(configFile) - loadNetwork(&settings) - loadProxy(&settings) - loadAsset(&settings) + //loadNetwork(&settings) + //loadProxy(&settings) + //loadAsset(&settings) + loadRadvd(&settings) + runScript(&settings) runProxy() - + if settings.RadvdEnable { + runRadvd() + } blockWait() process.Exit(subProcess...) }