Browse Source

update: some fixes and upgrades

v1.x.x
dnomd343 2 years ago
parent
commit
716b0adaec
  1. 2
      cmd/config/decode.go
  2. 2
      cmd/config/default.go
  3. 1
      cmd/config/main.go
  4. 43
      cmd/controller.go
  5. 7
      cmd/network/main.go
  6. 25
      cmd/network/network.go
  7. 29
      cmd/network/tproxy.go
  8. 1
      cmd/xproxy.go
  9. 7
      src/main.go

2
cmd/config/decode.go

@ -12,6 +12,7 @@ type yamlNetConfig struct {
} }
type yamlConfig struct { type yamlConfig struct {
Log string `yaml:"log"`
Custom []string `yaml:"custom"` Custom []string `yaml:"custom"`
Update struct { Update struct {
Cron string `yaml:"cron"` Cron string `yaml:"cron"`
@ -126,6 +127,7 @@ func decodeCustom(rawConfig *yamlConfig) []string {
func decode(rawConfig yamlConfig) Config { func decode(rawConfig yamlConfig) Config {
var config Config var config Config
config.LogLevel = rawConfig.Log
config.DNS = decodeDns(&rawConfig) config.DNS = decodeDns(&rawConfig)
config.V4Bypass, config.V6Bypass = decodeBypass(&rawConfig) config.V4Bypass, config.V6Bypass = decodeBypass(&rawConfig)
config.V4Address, config.V4Gateway = decodeIPv4(&rawConfig) config.V4Address, config.V4Gateway = decodeIPv4(&rawConfig)

2
cmd/config/default.go

@ -1,6 +1,8 @@
package config package config
var defaultConfig = `# default configure file for xproxy var defaultConfig = `# default configure file for xproxy
log: debug
proxy: proxy:
sniff: true sniff: true
redirect: true redirect: true

1
cmd/config/main.go

@ -16,6 +16,7 @@ type Config struct {
V6Gateway string V6Gateway string
Script []string Script []string
LogLevel string
UpdateCron string UpdateCron string
UpdateUrls map[string]string UpdateUrls map[string]string

43
cmd/controller.go

@ -2,11 +2,32 @@ package main
import ( import (
"XProxy/cmd/asset" "XProxy/cmd/asset"
"XProxy/cmd/common"
"XProxy/cmd/config" "XProxy/cmd/config"
"XProxy/cmd/network" "XProxy/cmd/network"
"XProxy/cmd/proxy" "XProxy/cmd/proxy"
log "github.com/sirupsen/logrus"
) )
func loadAsset(settings *config.Config) {
asset.LoadGeoSite(assetFile, assetDir)
asset.LoadGeoIp(assetFile, assetDir)
asset.AutoUpdate(settings.UpdateCron, settings.UpdateUrls, assetDir)
}
func loadProxy(settings *config.Config) {
proxy.Load(configDir, exposeDir, proxy.Config{
Sniff: settings.EnableSniff,
Redirect: settings.EnableRedirect,
V4TProxyPort: v4TProxyPort,
V6TProxyPort: v6TProxyPort,
LogLevel: settings.LogLevel,
HttpInbounds: settings.HttpInbounds,
SocksInbounds: settings.SocksInbounds,
AddOnInbounds: settings.AddOnInbounds,
})
}
func loadNetwork(settings *config.Config) { func loadNetwork(settings *config.Config) {
v4Settings := network.Config{ v4Settings := network.Config{
RouteTable: v4RouteTable, RouteTable: v4RouteTable,
@ -25,21 +46,9 @@ func loadNetwork(settings *config.Config) {
network.Load(settings.DNS, v4Settings, v6Settings) network.Load(settings.DNS, v4Settings, v6Settings)
} }
func loadProxy(settings *config.Config) { func runScript(settings *config.Config) {
proxy.Load(configDir, exposeDir, proxy.Config{ for _, script := range settings.Script {
Sniff: settings.EnableSniff, log.Infof("Run script command -> %s", script)
Redirect: settings.EnableRedirect, common.RunCommand("sh", "-c", script)
V4TProxyPort: v4TProxyPort, }
V6TProxyPort: v6TProxyPort,
LogLevel: "warning",
HttpInbounds: settings.HttpInbounds,
SocksInbounds: settings.SocksInbounds,
AddOnInbounds: settings.AddOnInbounds,
})
}
func loadAsset(settings *config.Config) {
asset.LoadGeoSite(assetFile, assetDir)
asset.LoadGeoIp(assetFile, assetDir)
asset.AutoUpdate(settings.UpdateCron, settings.UpdateUrls, assetDir)
} }

7
cmd/network/main.go

@ -1,6 +1,9 @@
package network package network
import "time" import (
"XProxy/cmd/common"
"time"
)
type Config struct { type Config struct {
RouteTable int RouteTable int
@ -10,6 +13,8 @@ type Config struct {
Bypass []string Bypass []string
} }
var run = common.RunCommand
func Load(dns []string, ipv4 Config, ipv6 Config) { func Load(dns []string, ipv4 Config, ipv6 Config) {
loadDns(dns) // init dns server loadDns(dns) // init dns server
flushNetwork() // clear network settings flushNetwork() // clear network settings

25
cmd/network/network.go

@ -1,14 +1,13 @@
package network package network
import ( import (
"XProxy/cmd/common"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"regexp" "regexp"
) )
func getV4Cidr() []string { func getV4Cidr() []string {
var v4Cidr []string var v4Cidr []string
_, output := common.RunCommand("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) {
v4Cidr = append(v4Cidr, temp[1]) v4Cidr = append(v4Cidr, temp[1])
} }
@ -17,7 +16,7 @@ func getV4Cidr() []string {
func getV6Cidr() []string { func getV6Cidr() []string {
var v6Cidr []string var v6Cidr []string
_, output := common.RunCommand("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) {
v6Cidr = append(v6Cidr, temp[1]) v6Cidr = append(v6Cidr, temp[1])
} }
@ -26,32 +25,32 @@ func getV6Cidr() []string {
func flushNetwork() { func flushNetwork() {
log.Info("Flush system IP configure") log.Info("Flush system IP configure")
common.RunCommand("ip", "link", "set", "eth0", "down") run("ip", "link", "set", "eth0", "down")
common.RunCommand("ip", "-4", "addr", "flush", "dev", "eth0") run("ip", "-4", "addr", "flush", "dev", "eth0")
common.RunCommand("ip", "-6", "addr", "flush", "dev", "eth0") run("ip", "-6", "addr", "flush", "dev", "eth0")
common.RunCommand("ip", "link", "set", "eth0", "down") run("ip", "link", "set", "eth0", "down")
} }
func loadV4Network(v4 Config) { func loadV4Network(v4 Config) {
log.Info("Enabled IPv4 forward") log.Info("Enabled IPv4 forward")
common.RunCommand("sysctl", "-w", "net.ipv4.ip_forward=1") 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 != "" {
common.RunCommand("ip", "-4", "addr", "add", v4.Address, "dev", "eth0") run("ip", "-4", "addr", "add", v4.Address, "dev", "eth0")
} }
if v4.Gateway != "" { if v4.Gateway != "" {
common.RunCommand("ip", "-4", "route", "add", "default", "via", v4.Gateway) run("ip", "-4", "route", "add", "default", "via", v4.Gateway)
} }
} }
func loadV6Network(v6 Config) { func loadV6Network(v6 Config) {
log.Info("Enabled IPv6 forward") log.Info("Enabled IPv6 forward")
common.RunCommand("sysctl", "-w", "net.ipv6.conf.all.forwarding=1") 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 != "" {
common.RunCommand("ip", "-6", "addr", "add", v6.Address, "dev", "eth0") run("ip", "-6", "addr", "add", v6.Address, "dev", "eth0")
} }
if v6.Gateway != "" { if v6.Gateway != "" {
common.RunCommand("ip", "-6", "route", "add", "default", "via", v6.Gateway) run("ip", "-6", "route", "add", "default", "via", v6.Gateway)
} }
} }

29
cmd/network/tproxy.go

@ -1,7 +1,6 @@
package network package network
import ( import (
"XProxy/cmd/common"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"strconv" "strconv"
) )
@ -10,34 +9,34 @@ func loadV4TProxy(v4 Config, v4SysCidr []string) {
log.Info("Setting up TProxy of IPv4") log.Info("Setting up TProxy of IPv4")
tableNum := strconv.Itoa(v4.RouteTable) tableNum := strconv.Itoa(v4.RouteTable)
v4Bypass := append(v4SysCidr, v4.Bypass...) v4Bypass := append(v4SysCidr, v4.Bypass...)
common.RunCommand("ip", "-4", "rule", "add", "fwmark", "1", "table", tableNum) run("ip", "-4", "rule", "add", "fwmark", "1", "table", tableNum)
common.RunCommand("ip", "-4", "route", "add", "local", "0.0.0.0/0", "dev", "lo", "table", tableNum) run("ip", "-4", "route", "add", "local", "0.0.0.0/0", "dev", "lo", "table", tableNum)
common.RunCommand("iptables", "-t", "mangle", "-N", "XPROXY") run("iptables", "-t", "mangle", "-N", "XPROXY")
log.Infof("Setting up IPv4 bypass CIDR -> %v", v4Bypass) log.Infof("Setting up IPv4 bypass CIDR -> %v", v4Bypass)
for _, cidr := range v4Bypass { for _, cidr := range v4Bypass {
common.RunCommand("iptables", "-t", "mangle", "-A", "XPROXY", "-d", cidr, "-j", "RETURN") run("iptables", "-t", "mangle", "-A", "XPROXY", "-d", cidr, "-j", "RETURN")
} }
common.RunCommand("iptables", "-t", "mangle", "-A", "XPROXY", run("iptables", "-t", "mangle", "-A", "XPROXY",
"-p", "tcp", "-j", "TPROXY", "--on-port", strconv.Itoa(v4.TProxyPort), "--tproxy-mark", "1") "-p", "tcp", "-j", "TPROXY", "--on-port", strconv.Itoa(v4.TProxyPort), "--tproxy-mark", "1")
common.RunCommand("iptables", "-t", "mangle", "-A", "XPROXY", run("iptables", "-t", "mangle", "-A", "XPROXY",
"-p", "udp", "-j", "TPROXY", "--on-port", strconv.Itoa(v4.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") run("iptables", "-t", "mangle", "-A", "PREROUTING", "-j", "XPROXY")
} }
func loadV6TProxy(v6 Config, v6SysCidr []string) { func loadV6TProxy(v6 Config, v6SysCidr []string) {
log.Info("Setting up TProxy of IPv6") log.Info("Setting up TProxy of IPv6")
tableNum := strconv.Itoa(v6.RouteTable) tableNum := strconv.Itoa(v6.RouteTable)
v6Bypass := append(v6SysCidr, v6.Bypass...) v6Bypass := append(v6SysCidr, v6.Bypass...)
common.RunCommand("ip", "-6", "rule", "add", "fwmark", "1", "table", tableNum) run("ip", "-6", "rule", "add", "fwmark", "1", "table", tableNum)
common.RunCommand("ip", "-6", "route", "add", "local", "::/0", "dev", "lo", "table", tableNum) run("ip", "-6", "route", "add", "local", "::/0", "dev", "lo", "table", tableNum)
common.RunCommand("ip6tables", "-t", "mangle", "-N", "XPROXY6") run("ip6tables", "-t", "mangle", "-N", "XPROXY6")
log.Infof("Setting up IPv6 bypass CIDR -> %v", v6Bypass) log.Infof("Setting up IPv6 bypass CIDR -> %v", v6Bypass)
for _, cidr := range v6Bypass { for _, cidr := range v6Bypass {
common.RunCommand("ip6tables", "-t", "mangle", "-A", "XPROXY6", "-d", cidr, "-j", "RETURN") run("ip6tables", "-t", "mangle", "-A", "XPROXY6", "-d", cidr, "-j", "RETURN")
} }
common.RunCommand("ip6tables", "-t", "mangle", "-A", "XPROXY6", run("ip6tables", "-t", "mangle", "-A", "XPROXY6",
"-p", "tcp", "-j", "TPROXY", "--on-port", strconv.Itoa(v6.TProxyPort), "--tproxy-mark", "1") "-p", "tcp", "-j", "TPROXY", "--on-port", strconv.Itoa(v6.TProxyPort), "--tproxy-mark", "1")
common.RunCommand("ip6tables", "-t", "mangle", "-A", "XPROXY6", run("ip6tables", "-t", "mangle", "-A", "XPROXY6",
"-p", "udp", "-j", "TPROXY", "--on-port", strconv.Itoa(v6.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") run("ip6tables", "-t", "mangle", "-A", "PREROUTING", "-j", "XPROXY6")
} }

1
cmd/xproxy.go

@ -27,6 +27,7 @@ func main() {
loadNetwork(&settings) loadNetwork(&settings)
loadProxy(&settings) loadProxy(&settings)
loadAsset(&settings) loadAsset(&settings)
runScript(&settings)
// TODO: start xray service // TODO: start xray service
} }

7
src/main.go

@ -7,8 +7,6 @@ import (
"syscall" "syscall"
) )
var preScript []string
func main() { func main() {
defer func() { defer func() {
if err := recover(); err != nil { if err := recover(); err != nil {
@ -16,11 +14,6 @@ func main() {
} }
}() }()
for _, script := range preScript {
log.Infof("Run script command -> %s", script)
runCommand("sh", "-c", script)
}
xray := newProcess("xray", "-confdir", "/etc/xproxy/config") xray := newProcess("xray", "-confdir", "/etc/xproxy/config")
xray.startProcess(true, true) xray.startProcess(true, true)
subProcess = append(subProcess, xray) subProcess = append(subProcess, xray)

Loading…
Cancel
Save