Browse Source

update: complete network package

v1.x.x
dnomd343 2 years ago
parent
commit
832f513054
  1. 26
      cmd/network/dns.go
  2. 27
      cmd/network/main.go
  3. 34
      cmd/network/network.go
  4. 32
      cmd/network/tproxy.go
  5. 8
      main.go

26
cmd/network/dns.go

@ -1,19 +1,19 @@
package network
import (
"XProxy/cmd/common"
log "github.com/sirupsen/logrus"
"XProxy/cmd/common"
log "github.com/sirupsen/logrus"
)
func loadDns(dnsServer []string) {
if len(dnsServer) == 0 {
log.Info("Using system DNS server")
return
}
log.Infof("Setting up DNS server -> %v", dnsServer)
dnsConfig := ""
for _, address := range dnsServer {
dnsConfig += "nameserver " + address + "\n"
}
common.WriteFile("/etc/resolv.conf", dnsConfig, true)
func loadDns(dns []string) {
if len(dns) == 0 { // without dns server
log.Info("Using system DNS server")
return
}
log.Infof("Setting up DNS server -> %v", dns)
dnsConfig := ""
for _, dnsAddr := range dns {
dnsConfig += "nameserver " + dnsAddr + "\n"
}
common.WriteFile("/etc/resolv.conf", dnsConfig, true)
}

27
cmd/network/main.go

@ -1,18 +1,23 @@
package network
import "time"
type Config struct {
IPv4 struct {
Address string
Gateway string
RouteTable int
TProxyPort int
}
V4RouteTable int
V6RouteTable int
V4TProxyPort int
V6TProxyPort int
RouteTable int
TProxyPort int
Address string
Gateway string
Bypass []string
}
func Load() {
func Load(dns []string, ipv4 Config, ipv6 Config) {
loadDns(dns) // init dns server
flushNetwork() // clear network settings
loadV4Network(ipv4)
loadV6Network(ipv6)
time.Sleep(time.Second) // wait 1s for ipv6 (ND protocol)
loadV4TProxy(ipv4, getV4Cidr())
loadV6TProxy(ipv6, getV6Cidr())
}

34
cmd/network/network.go

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

32
cmd/network/tproxy.go

@ -6,42 +6,38 @@ import (
"strconv"
)
type tproxyConfig struct {
routeTable int
tproxyPort int
bypassCidr []string
}
func loadV4TProxy(config tproxyConfig) {
func loadV4TProxy(v4 Config, v4SysCidr []string) {
log.Info("Setting up TProxy of IPv4")
tableNum := strconv.Itoa(config.routeTable)
tableNum := strconv.Itoa(v4.RouteTable)
v4Bypass := append(v4SysCidr, v4.Bypass...)
common.RunCommand("ip", "-4", "rule", "add", "fwmark", "1", "table", tableNum)
common.RunCommand("ip", "-4", "route", "add", "local", "0.0.0.0/0", "dev", "lo", "table", tableNum)
common.RunCommand("iptables", "-t", "mangle", "-N", "XPROXY")
log.Infof("Setting up IPv4 bypass CIDR -> %v", config.bypassCidr)
for _, cidr := range config.bypassCidr {
log.Infof("Setting up IPv4 bypass CIDR -> %v", v4Bypass)
for _, cidr := range v4Bypass {
common.RunCommand("iptables", "-t", "mangle", "-A", "XPROXY", "-d", cidr, "-j", "RETURN")
}
common.RunCommand("iptables", "-t", "mangle", "-A", "XPROXY",
"-p", "tcp", "-j", "TPROXY", "--on-port", strconv.Itoa(config.tproxyPort), "--tproxy-mark", "1")
"-p", "tcp", "-j", "TPROXY", "--on-port", strconv.Itoa(v4.TProxyPort), "--tproxy-mark", "1")
common.RunCommand("iptables", "-t", "mangle", "-A", "XPROXY",
"-p", "udp", "-j", "TPROXY", "--on-port", strconv.Itoa(config.tproxyPort), "--tproxy-mark", "1")
"-p", "udp", "-j", "TPROXY", "--on-port", strconv.Itoa(v4.TProxyPort), "--tproxy-mark", "1")
common.RunCommand("iptables", "-t", "mangle", "-A", "PREROUTING", "-j", "XPROXY")
}
func loadV6TProxy(config tproxyConfig) {
func loadV6TProxy(v6 Config, v6SysCidr []string) {
log.Info("Setting up TProxy of IPv6")
tableNum := strconv.Itoa(config.routeTable)
tableNum := strconv.Itoa(v6.RouteTable)
v6Bypass := append(v6SysCidr, v6.Bypass...)
common.RunCommand("ip", "-6", "rule", "add", "fwmark", "1", "table", tableNum)
common.RunCommand("ip", "-6", "route", "add", "local", "::/0", "dev", "lo", "table", tableNum)
common.RunCommand("ip6tables", "-t", "mangle", "-N", "XPROXY6")
log.Infof("Setting up IPv6 bypass CIDR -> %v", config.bypassCidr)
for _, cidr := range config.bypassCidr {
log.Infof("Setting up IPv6 bypass CIDR -> %v", v6Bypass)
for _, cidr := range v6Bypass {
common.RunCommand("ip6tables", "-t", "mangle", "-A", "XPROXY6", "-d", cidr, "-j", "RETURN")
}
common.RunCommand("ip6tables", "-t", "mangle", "-A", "XPROXY6",
"-p", "tcp", "-j", "TPROXY", "--on-port", strconv.Itoa(config.tproxyPort), "--tproxy-mark", "1")
"-p", "tcp", "-j", "TPROXY", "--on-port", strconv.Itoa(v6.TProxyPort), "--tproxy-mark", "1")
common.RunCommand("ip6tables", "-t", "mangle", "-A", "XPROXY6",
"-p", "udp", "-j", "TPROXY", "--on-port", strconv.Itoa(config.tproxyPort), "--tproxy-mark", "1")
"-p", "udp", "-j", "TPROXY", "--on-port", strconv.Itoa(v6.TProxyPort), "--tproxy-mark", "1")
common.RunCommand("ip6tables", "-t", "mangle", "-A", "PREROUTING", "-j", "XPROXY6")
}

8
main.go

@ -1,7 +1,6 @@
package main
import (
"XProxy/cmd/network"
"fmt"
log "github.com/sirupsen/logrus"
)
@ -10,10 +9,5 @@ func main() {
log.SetLevel(log.DebugLevel)
fmt.Println("xproxy start")
//common.CreateFolder("/tmp/test")
//fmt.Println(common.IsFileExist("/tmp/1.jpg"))
//fmt.Println(common.ListFiles("/xproxy/config", ".json"))
net = network.Config{
V4RouteTable: 12,
}
}

Loading…
Cancel
Save