Browse Source

feat: radvd configure

v1.x.x
dnomd343 2 years ago
parent
commit
97d9641a69
  1. 15
      cmd/config/decode.go
  2. 4
      cmd/config/main.go
  3. 5
      cmd/controller.go
  4. 33
      cmd/radvd/main.go
  5. 19
      cmd/xproxy.go

15
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
}

4
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 {

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

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

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

Loading…
Cancel
Save