Browse Source

update: setting up network

v1.x.x
dnomd343 2 years ago
parent
commit
f013fa28a4
  1. 2
      cmd/controller.go
  2. 28
      cmd/network/main.go
  3. 60
      cmd/network/network.go
  4. 1
      cmd/xproxy.go

2
cmd/controller.go

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

28
cmd/network/main.go

@ -2,8 +2,6 @@ package network
import ( import (
"XProxy/cmd/common" "XProxy/cmd/common"
log "github.com/sirupsen/logrus"
"time"
) )
type Config struct { type Config struct {
@ -17,30 +15,10 @@ type Config struct {
var run = common.RunCommand var run = common.RunCommand
func Load(dns []string, ipv4 *Config, ipv6 *Config, dev string) { func Load(dns []string, dev string, ipv4 *Config, ipv6 *Config) {
loadDns(dns) // init dns server loadDns(dns) // init dns server
delay := false enableIpForward()
setV4 := ipv4.Address != "" || ipv4.Gateway != "" loadNetwork(dev, ipv4, ipv6)
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()) loadV4TProxy(ipv4, getV4Cidr())
loadV6TProxy(ipv6, getV6Cidr()) loadV6TProxy(ipv6, getV6Cidr())
} }

60
cmd/network/network.go

@ -3,9 +3,10 @@ package network
import ( import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"regexp" "regexp"
"time"
) )
func getV4Cidr() []string { func getV4Cidr() []string { // fetch ipv4 network range
var v4Cidr []string var v4Cidr []string
_, output := run("ip", "-4", "addr") _, output := run("ip", "-4", "addr")
for _, temp := range regexp.MustCompile(`inet (\S+)`).FindAllStringSubmatch(output, -1) { for _, temp := range regexp.MustCompile(`inet (\S+)`).FindAllStringSubmatch(output, -1) {
@ -14,7 +15,7 @@ func getV4Cidr() []string {
return v4Cidr return v4Cidr
} }
func getV6Cidr() []string { func getV6Cidr() []string { // fetch ipv6 network range
var v6Cidr []string var v6Cidr []string
_, output := run("ip", "-6", "addr") _, output := run("ip", "-6", "addr")
for _, temp := range regexp.MustCompile(`inet6 (\S+)`).FindAllStringSubmatch(output, -1) { for _, temp := range regexp.MustCompile(`inet6 (\S+)`).FindAllStringSubmatch(output, -1) {
@ -23,31 +24,26 @@ func getV6Cidr() []string {
return v6Cidr return v6Cidr
} }
func flushNetwork(dev string) { func enableIpForward() { // enable ip forward function
log.Info("Flush system IP configure") log.Info("Enabled IPv4 forward")
run("ip", "link", "set", dev, "down") run("sysctl", "-w", "net.ipv4.ip_forward=1")
run("ip", "-4", "addr", "flush", "dev", dev) log.Info("Enabled IPv6 forward")
run("ip", "-6", "addr", "flush", "dev", dev) run("sysctl", "-w", "net.ipv6.conf.all.forwarding=1")
run("ip", "link", "set", dev, "up")
} }
func flushV4Network(dev string) { func flushNetwork(dev string, isV4 bool, isV6 bool) { // flush ipv4 and ipv6 network
log.Info("Flush system IPv4 configure") log.Info("Flush system IP configure")
run("ip", "link", "set", dev, "down") run("ip", "link", "set", dev, "down")
if isV4 {
run("ip", "-4", "addr", "flush", "dev", dev) run("ip", "-4", "addr", "flush", "dev", dev)
run("ip", "link", "set", dev, "up") }
} if isV6 {
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", "-6", "addr", "flush", "dev", dev)
}
run("ip", "link", "set", dev, "up") run("ip", "link", "set", dev, "up")
} }
func loadV4Network(v4 *Config, dev string) { func loadV4Network(v4 *Config, dev string) { // setting up ipv4 network
log.Info("Enabled IPv4 forward")
run("sysctl", "-w", "net.ipv4.ip_forward=1")
log.Info("Setting up system IPv4 configure") log.Info("Setting up system IPv4 configure")
if v4.Address != "" { if v4.Address != "" {
run("ip", "-4", "addr", "add", v4.Address, "dev", dev) run("ip", "-4", "addr", "add", v4.Address, "dev", dev)
@ -57,9 +53,7 @@ func loadV4Network(v4 *Config, dev string) {
} }
} }
func loadV6Network(v6 *Config, dev string) { func loadV6Network(v6 *Config, dev string) { // setting up ipv6 network
log.Info("Enabled IPv6 forward")
run("sysctl", "-w", "net.ipv6.conf.all.forwarding=1")
log.Info("Setting up system IPv6 configure") log.Info("Setting up system IPv6 configure")
if v6.Address != "" { if v6.Address != "" {
run("ip", "-6", "addr", "add", v6.Address, "dev", dev) run("ip", "-6", "addr", "add", v6.Address, "dev", dev)
@ -68,3 +62,25 @@ func loadV6Network(v6 *Config, dev string) {
run("ip", "-6", "route", "add", "default", "via", v6.Gateway, "dev", dev) run("ip", "-6", "route", "add", "default", "via", v6.Gateway, "dev", dev)
} }
} }
func loadNetwork(dev string, v4 *Config, v6 *Config) {
setV4 := v4.Address != "" || v4.Gateway != ""
setV6 := v6.Address != "" || v6.Gateway != ""
if setV4 && setV6 { // load both ipv4 and ipv6
flushNetwork(dev, true, true)
loadV4Network(v4, dev)
loadV6Network(v6, dev)
} else if setV4 { // only load ipv4 network
flushNetwork(dev, true, false)
loadV4Network(v4, dev)
} else if setV6 { // only load ipv6 network
flushNetwork(dev, false, true)
loadV6Network(v6, dev)
} else { // skip network settings
log.Infof("Skip system IP configure")
}
if setV6 {
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
}
}

1
cmd/xproxy.go

@ -35,6 +35,7 @@ func logInit(isDebug bool, logDir string) {
if isDebug { if isDebug {
log.SetLevel(log.DebugLevel) log.SetLevel(log.DebugLevel)
} }
common.CreateFolder(logDir) // confirm log folder exist
logFile, err := os.OpenFile(path.Join(logDir, "xproxy.log"), os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644) logFile, err := os.OpenFile(path.Join(logDir, "xproxy.log"), os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
if err != nil { if err != nil {
log.Errorf("Unable to open log file -> %s", path.Join(logDir, "xproxy.log")) log.Errorf("Unable to open log file -> %s", path.Join(logDir, "xproxy.log"))

Loading…
Cancel
Save