Browse Source

update: complete radvd package

v1.x.x
dnomd343 2 years ago
parent
commit
032a88e835
  1. 21
      cmd/config/decode.go
  2. 8
      cmd/config/main.go
  3. 5
      cmd/controller.go
  4. 98
      cmd/radvd/radvd.go
  5. 26
      cmd/xproxy.go

21
cmd/config/decode.go

@ -2,6 +2,7 @@ package config
import ( import (
"XProxy/cmd/common" "XProxy/cmd/common"
"XProxy/cmd/radvd"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )
@ -34,11 +35,7 @@ type yamlConfig struct {
IPv4 yamlNetConfig `yaml:"ipv4"` // ipv4 network configure IPv4 yamlNetConfig `yaml:"ipv4"` // ipv4 network configure
IPv6 yamlNetConfig `yaml:"ipv6"` // ipv6 network configure IPv6 yamlNetConfig `yaml:"ipv6"` // ipv6 network configure
} `yaml:"network"` } `yaml:"network"`
Radvd struct { Radvd radvd.Config `yaml:"radvd"`
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 { func yamlDecode(raw []byte) yamlConfig {
@ -122,12 +119,14 @@ func decodeProxy(rawConfig *yamlConfig, config *Config) {
} }
func decodeRadvd(rawConfig *yamlConfig, config *Config) { func decodeRadvd(rawConfig *yamlConfig, config *Config) {
config.RadvdEnable = rawConfig.Radvd.Enable config.Radvd = rawConfig.Radvd
log.Debugf("Radvd enable -> %t", config.RadvdEnable) log.Debugf("Radvd enable -> %t", config.Radvd.Enable)
config.RadvdOptions = rawConfig.Radvd.Options log.Debugf("Radvd options -> %v", config.Radvd.Option)
log.Debugf("Radvd options -> %v", config.RadvdOptions) log.Debugf("Radvd prefix -> %v", config.Radvd.Prefix)
config.RadvdPrefix = rawConfig.Radvd.Prefix log.Debugf("Radvd route -> %v", config.Radvd.Route)
log.Debugf("Radvd prefix -> %v", config.RadvdPrefix) log.Debugf("Radvd clients -> %v", config.Radvd.Client)
log.Debugf("Radvd RDNSS -> %v", config.Radvd.RDNSS)
log.Debugf("Radvd DNSSL -> %v", config.Radvd.DNSSL)
} }
func decodeUpdate(rawConfig *yamlConfig) (string, map[string]string) { func decodeUpdate(rawConfig *yamlConfig) (string, map[string]string) {

8
cmd/config/main.go

@ -2,6 +2,7 @@ package config
import ( import (
"XProxy/cmd/common" "XProxy/cmd/common"
"XProxy/cmd/radvd"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"os" "os"
) )
@ -27,9 +28,10 @@ type Config struct {
SocksInbounds map[string]int SocksInbounds map[string]int
AddOnInbounds []interface{} AddOnInbounds []interface{}
RadvdEnable bool Radvd radvd.Config
RadvdOptions map[string]string //RadvdEnable bool
RadvdPrefix map[string]map[string]string //RadvdOptions map[string]string
//RadvdPrefix map[string]map[string]string
} }
func Load(configFile string) Config { func Load(configFile string) Config {

5
cmd/controller.go

@ -6,7 +6,6 @@ import (
"XProxy/cmd/config" "XProxy/cmd/config"
"XProxy/cmd/network" "XProxy/cmd/network"
"XProxy/cmd/proxy" "XProxy/cmd/proxy"
"XProxy/cmd/radvd"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
@ -47,10 +46,6 @@ func loadNetwork(settings *config.Config) {
network.Load(settings.DNS, v4Settings, v6Settings) network.Load(settings.DNS, v4Settings, v6Settings)
} }
func loadRadvd(settings *config.Config) {
radvd.Load(settings.RadvdOptions, settings.RadvdPrefix)
}
func runScript(settings *config.Config) { func runScript(settings *config.Config) {
for _, script := range settings.Script { for _, script := range settings.Script {
log.Infof("Run script command -> %s", script) log.Infof("Run script command -> %s", script)

98
cmd/radvd/radvd.go

@ -6,28 +6,92 @@ import (
"strings" "strings"
) )
func optionList(options map[string]string, intendNum int) string { type Config struct {
var result string Enable bool `yaml:"enable" json:"enable"`
intend := strings.Repeat(" ", intendNum) Client []string `yaml:"client" json:"client"`
Option map[string]string `yaml:"option" json:"option"`
Route struct {
Cidr string `yaml:"cidr" json:"cidr"`
Option map[string]string `yaml:"option" json:"option"`
} `yaml:"route" json:"route"`
Prefix struct {
Cidr string `yaml:"cidr" json:"cidr"`
Option map[string]string `yaml:"option" json:"option"`
} `yaml:"prefix" json:"prefix"`
DNSSL struct { // DNS Search List
Suffix []string `yaml:"suffix" json:"suffix"`
Option map[string]string `yaml:"option" json:"option"`
} `yaml:"dnssl" json:"dnssl"`
RDNSS struct { // Recursive DNS Server
IP []string `yaml:"ip" json:"ip"`
Option map[string]string `yaml:"option" json:"option"`
} `yaml:"rdnss" json:"rdnss"`
}
func genSpace(num int) string {
return strings.Repeat(" ", num)
}
func loadOption(options map[string]string, intend int) string { // load options into radvd config format
var ret string
for option, value := range options { for option, value := range options {
result += intend + option + " " + value + ";\n" ret += genSpace(intend) + option + " " + value + ";\n"
} }
return result return ret
} }
func loadPrefix(prefix string, options map[string]string) string { func loadClient(clients []string) string {
result := " prefix " + prefix + " {\n" if len(clients) == 0 { // without client settings
result += optionList(options, 8) return ""
return result + " };\n" }
ret := genSpace(4) + "clients {\n"
for _, client := range clients {
ret += genSpace(8) + client + ";\n"
}
return ret + genSpace(4) + "};\n"
}
func loadPrefix(prefix string, option map[string]string) string { // load radvd prefix configure
if prefix == "" { // without prefix settings
return ""
}
header := genSpace(4) + "prefix " + prefix + " {\n"
return header + loadOption(option, 8) + genSpace(4) + "};\n"
} }
func Load(options map[string]string, prefixes map[string]map[string]string) { func loadRoute(cidr string, option map[string]string) string { // load radvd route configure
radvdConfig := "interface eth0 {\n" if cidr == "" { // without route settings
radvdConfig += optionList(options, 4) return ""
for prefix, prefixOptions := range prefixes {
radvdConfig += loadPrefix(prefix, prefixOptions)
} }
radvdConfig += "};\n" header := genSpace(4) + "route " + cidr + " {\n"
log.Debugf("Radvd configure -> \n%s", radvdConfig) return header + loadOption(option, 8) + genSpace(4) + "};\n"
common.WriteFile("/etc/radvd.conf", radvdConfig, true) }
func loadRdnss(ip []string, option map[string]string) string {
if len(ip) == 0 { // without rdnss settings
return ""
}
header := genSpace(4) + "RDNSS " + strings.Join(ip, " ") + " {\n"
return header + loadOption(option, 8) + genSpace(4) + "};\n"
}
func loadDnssl(suffix []string, option map[string]string) string {
if len(suffix) == 0 { // without dnssl settings
return ""
}
header := genSpace(4) + "DNSSL " + strings.Join(suffix, " ") + " {\n"
return header + loadOption(option, 8) + genSpace(4) + "};\n"
}
func Load(Radvd *Config) {
config := "interface eth0 {\n"
config += loadOption(Radvd.Option, 4)
config += loadPrefix(Radvd.Prefix.Cidr, Radvd.Prefix.Option)
config += loadRoute(Radvd.Route.Cidr, Radvd.Route.Option)
config += loadClient(Radvd.Client)
config += loadRdnss(Radvd.RDNSS.IP, Radvd.RDNSS.Option)
config += loadDnssl(Radvd.DNSSL.Suffix, Radvd.DNSSL.Option)
config += "};\n"
log.Debugf("Radvd configure -> \n%s", config)
common.WriteFile("/etc/radvd.conf", config, true)
} }

26
cmd/xproxy.go

@ -3,6 +3,7 @@ package main
import ( import (
"XProxy/cmd/config" "XProxy/cmd/config"
"XProxy/cmd/process" "XProxy/cmd/process"
"XProxy/cmd/radvd"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"os" "os"
"os/signal" "os/signal"
@ -24,18 +25,11 @@ var configFile = exposeDir + "/config.yml"
var subProcess []*process.Process var subProcess []*process.Process
func runProxy() { func runProcess(command ...string) {
proxy := process.New("xray", "-confdir", configDir) sub := process.New(command...)
proxy.Run(true) sub.Run(true)
proxy.Daemon() sub.Daemon()
subProcess = append(subProcess, proxy) subProcess = append(subProcess, sub)
}
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() { func blockWait() {
@ -58,12 +52,12 @@ func main() {
loadNetwork(&settings) loadNetwork(&settings)
loadProxy(&settings) loadProxy(&settings)
loadAsset(&settings) loadAsset(&settings)
loadRadvd(&settings) radvd.Load(&settings.Radvd)
runScript(&settings) runScript(&settings)
runProxy() runProcess("xray", "-confdir", configDir)
if settings.RadvdEnable { if settings.Radvd.Enable {
runRadvd() runProcess("radvd", "-n", "-m", "logfile", "-l", exposeDir+"/log/radvd.log")
} }
blockWait() blockWait()
process.Exit(subProcess...) process.Exit(subProcess...)

Loading…
Cancel
Save