Browse Source

fix: network device in netns

v1.x.x
dnomd343 2 years ago
parent
commit
462c46d5ca
  1. 17
      cmd/config/decode.go
  2. 2
      cmd/config/main.go
  3. 2
      cmd/controller.go
  4. 31
      cmd/network/main.go
  5. 36
      cmd/network/network.go
  6. 3
      cmd/radvd/radvd.go

17
cmd/config/decode.go

@ -22,6 +22,7 @@ type RawConfig struct {
Radvd radvd.Config `yaml:"radvd" json:"radvd"`
Proxy proxy.Config `yaml:"proxy" json:"proxy"`
Network struct {
Dev string `yaml:"dev" json:"dev"`
DNS []string `yaml:"dns" json:"dns"`
ByPass []string `yaml:"bypass" json:"bypass"`
Exclude []string `yaml:"exclude" json:"exclude"`
@ -46,6 +47,18 @@ func configDecode(raw []byte, fileSuffix string) RawConfig {
return config
}
func decodeDev(rawConfig *RawConfig, config *Config) {
config.Dev = rawConfig.Network.Dev
if config.Dev == "" {
setV4 := rawConfig.Network.IPv4.Address != "" || rawConfig.Network.IPv4.Gateway != ""
setV6 := rawConfig.Network.IPv6.Address != "" || rawConfig.Network.IPv6.Gateway != ""
if setV4 || setV6 {
log.Panicf("Missing dev option in network settings")
}
}
log.Debugf("Network device -> %s", config.Dev)
}
func decodeDns(rawConfig *RawConfig, config *Config) {
for _, address := range rawConfig.Network.DNS { // dns options
if common.IsIPv4(address, false) || common.IsIPv6(address, false) {
@ -135,7 +148,11 @@ func decodeProxy(rawConfig *RawConfig, config *Config) {
func decodeRadvd(rawConfig *RawConfig, config *Config) {
config.Radvd = rawConfig.Radvd
if config.Radvd.Enable && config.Radvd.Dev == "" {
log.Panicf("Radvd enabled without dev option")
}
log.Debugf("Radvd log level -> %d", config.Radvd.Log)
log.Debugf("Radvd network dev -> %s", config.Radvd.Dev)
log.Debugf("Radvd enable -> %t", config.Radvd.Enable)
log.Debugf("Radvd options -> %v", config.Radvd.Option)
log.Debugf("Radvd prefix -> %v", config.Radvd.Prefix)

2
cmd/config/main.go

@ -12,6 +12,7 @@ import (
)
type Config struct {
Dev string
DNS []string
IPv4 network.Config
IPv6 network.Config
@ -31,6 +32,7 @@ func Load(configFile string, config *Config) {
log.Panicf("Failed to open %s -> %v", configFile, err)
}
rawConfig := configDecode(raw, path.Ext(configFile)) // decode configure content
decodeDev(&rawConfig, config)
decodeDns(&rawConfig, config)
decodeBypass(&rawConfig, config)
decodeExclude(&rawConfig, config)

2
cmd/controller.go

@ -43,7 +43,7 @@ func loadNetwork(settings *config.Config) {
settings.IPv4.TProxyPort = v4TProxyPort
settings.IPv6.RouteTable = v6RouteTable
settings.IPv6.TProxyPort = v6TProxyPort
network.Load(settings.DNS, &settings.IPv4, &settings.IPv6)
network.Load(settings.DNS, &settings.IPv4, &settings.IPv6, settings.Dev)
}
func loadProxy(settings *config.Config) {

31
cmd/network/main.go

@ -17,13 +17,30 @@ type Config struct {
var run = common.RunCommand
func Load(dns []string, ipv4 *Config, ipv6 *Config) {
loadDns(dns) // init dns server
flushNetwork() // clear network settings
loadV4Network(ipv4)
loadV6Network(ipv6)
log.Info("Wait 1s for IPv6 setting up")
time.Sleep(time.Second) // wait for ipv6 setting up (ND protocol) -> RA should reply less than 0.5s
func Load(dns []string, ipv4 *Config, ipv6 *Config, dev string) {
loadDns(dns) // init dns server
delay := false
setV4 := ipv4.Address != "" || ipv4.Gateway != ""
setV6 := ipv6.Address != "" || ipv6.Gateway != ""
if setV4 && setV6 { // clear network settings
delay = true
flushNetwork(dev)
loadV4Network(ipv4, dev)
loadV6Network(ipv6, dev)
} else if setV6 {
delay = true
flushV6Network(dev)
loadV6Network(ipv6, dev)
} else if setV4 {
flushV4Network(dev)
loadV4Network(ipv4, dev)
} else {
log.Infof("Skip system IP configure")
}
if delay {
log.Info("Wait 1s for IPv6 setting up")
time.Sleep(time.Second) // wait for ipv6 setting up (ND protocol) -> RA should reply less than 0.5s
}
loadV4TProxy(ipv4, getV4Cidr())
loadV6TProxy(ipv6, getV6Cidr())
}

36
cmd/network/network.go

@ -23,34 +23,48 @@ func getV6Cidr() []string {
return v6Cidr
}
func flushNetwork() {
func flushNetwork(dev string) {
log.Info("Flush system IP configure")
run("ip", "link", "set", "eth0", "down")
run("ip", "-4", "addr", "flush", "dev", "eth0")
run("ip", "-6", "addr", "flush", "dev", "eth0")
run("ip", "link", "set", "eth0", "up")
run("ip", "link", "set", dev, "down")
run("ip", "-4", "addr", "flush", "dev", dev)
run("ip", "-6", "addr", "flush", "dev", dev)
run("ip", "link", "set", dev, "up")
}
func loadV4Network(v4 *Config) {
func flushV4Network(dev string) {
log.Info("Flush system IPv4 configure")
run("ip", "link", "set", dev, "down")
run("ip", "-4", "addr", "flush", "dev", dev)
run("ip", "link", "set", dev, "up")
}
func flushV6Network(dev string) {
log.Info("Flush system IPv6 configure")
run("ip", "link", "set", dev, "down")
run("ip", "-6", "addr", "flush", "dev", dev)
run("ip", "link", "set", dev, "up")
}
func loadV4Network(v4 *Config, dev string) {
log.Info("Enabled IPv4 forward")
run("sysctl", "-w", "net.ipv4.ip_forward=1")
log.Info("Setting up system IPv4 configure")
if v4.Address != "" {
run("ip", "-4", "addr", "add", v4.Address, "dev", "eth0")
run("ip", "-4", "addr", "add", v4.Address, "dev", dev)
}
if v4.Gateway != "" {
run("ip", "-4", "route", "add", "default", "via", v4.Gateway, "dev", "eth0")
run("ip", "-4", "route", "add", "default", "via", v4.Gateway, "dev", dev)
}
}
func loadV6Network(v6 *Config) {
func loadV6Network(v6 *Config, dev string) {
log.Info("Enabled IPv6 forward")
run("sysctl", "-w", "net.ipv6.conf.all.forwarding=1")
log.Info("Setting up system IPv6 configure")
if v6.Address != "" {
run("ip", "-6", "addr", "add", v6.Address, "dev", "eth0")
run("ip", "-6", "addr", "add", v6.Address, "dev", dev)
}
if v6.Gateway != "" {
run("ip", "-6", "route", "add", "default", "via", v6.Gateway, "dev", "eth0")
run("ip", "-6", "route", "add", "default", "via", v6.Gateway, "dev", dev)
}
}

3
cmd/radvd/radvd.go

@ -8,6 +8,7 @@ import (
type Config struct {
Log int `yaml:"log" json:"log"`
Dev string `yaml:"dev" json:"dev"`
Enable bool `yaml:"enable" json:"enable"`
Client []string `yaml:"client" json:"client"`
Option map[string]string `yaml:"option" json:"option"`
@ -85,7 +86,7 @@ func loadDnssl(suffix []string, option map[string]string) string { // load radvd
}
func Load(Radvd *Config) {
config := "interface eth0 {\n"
config := "interface " + Radvd.Dev + " {\n"
config += loadOption(Radvd.Option, 4)
config += loadPrefix(Radvd.Prefix.Cidr, Radvd.Prefix.Option)
config += loadRoute(Radvd.Route.Cidr, Radvd.Route.Option)

Loading…
Cancel
Save