Browse Source

refactor: proxy configure decode

v1.x.x
dnomd343 2 years ago
parent
commit
96380c04cd
  1. 40
      cmd/config/decode.go
  2. 10
      cmd/config/main.go
  3. 37
      cmd/controller.go
  4. 2
      cmd/network/main.go
  5. 4
      cmd/network/network.go
  6. 4
      cmd/network/tproxy.go
  7. 34
      cmd/proxy/main.go
  8. 19
      cmd/xproxy.go

40
cmd/config/decode.go

@ -3,6 +3,7 @@ package config
import ( import (
"XProxy/cmd/asset" "XProxy/cmd/asset"
"XProxy/cmd/common" "XProxy/cmd/common"
"XProxy/cmd/proxy"
"XProxy/cmd/radvd" "XProxy/cmd/radvd"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
@ -14,26 +15,16 @@ type yamlNetConfig struct {
} }
type yamlConfig struct { type yamlConfig struct {
Custom []string `yaml:"custom"` Custom []string `yaml:"custom" json:"custom"`
Update asset.Config `yaml:"update"` Update asset.Config `yaml:"update" json:"update"`
Proxy struct { Radvd radvd.Config `yaml:"radvd" json:"radvd"`
Log string `yaml:"log"` Proxy proxy.Config `yaml:"proxy" json:"proxy"`
Sniff struct {
Enable bool `yaml:"enable"`
Redirect bool `yaml:"redirect"`
Exclude []string `yaml:"exclude"`
} `yaml:"sniff"`
Http map[string]int `yaml:"http"`
Socks map[string]int `yaml:"socks"`
AddOn []interface{} `yaml:"addon"`
} `yaml:"proxy"`
Network struct { Network struct {
DNS []string `yaml:"dns" json:"dns"` DNS []string `yaml:"dns" json:"dns"`
ByPass []string `yaml:"bypass" json:"bypass"` ByPass []string `yaml:"bypass" json:"bypass"`
IPv4 yamlNetConfig `yaml:"ipv4" json:"ipv4"` IPv4 yamlNetConfig `yaml:"ipv4" json:"ipv4"`
IPv6 yamlNetConfig `yaml:"ipv6" json:"ipv6"` IPv6 yamlNetConfig `yaml:"ipv6" json:"ipv6"`
} `yaml:"network" json:"network"` } `yaml:"network" json:"network"`
Radvd radvd.Config `yaml:"radvd" json:"radvd"`
} }
func yamlDecode(raw []byte) yamlConfig { func yamlDecode(raw []byte) yamlConfig {
@ -96,18 +87,14 @@ func decodeIPv6(rawConfig *yamlConfig, config *Config) {
} }
func decodeProxy(rawConfig *yamlConfig, config *Config) { func decodeProxy(rawConfig *yamlConfig, config *Config) {
config.EnableSniff = rawConfig.Proxy.Sniff.Enable config.Proxy = rawConfig.Proxy
log.Debugf("Connection sniff -> %t", config.EnableSniff) log.Debugf("Proxy log level -> %s", config.Proxy.Log)
config.EnableRedirect = rawConfig.Proxy.Sniff.Redirect log.Debugf("Http inbounds -> %v", config.Proxy.Http)
log.Debugf("Connection redirect -> %t", config.EnableRedirect) log.Debugf("Socks5 inbounds -> %v", config.Proxy.Socks)
config.SniffExclude = rawConfig.Proxy.Sniff.Exclude log.Debugf("Add-on inbounds -> %v", config.Proxy.AddOn)
log.Debugf("Connection sniff exlcude -> %v", config.SniffExclude) log.Debugf("Connection sniff -> %t", config.Proxy.Sniff.Enable)
config.HttpInbounds = rawConfig.Proxy.Http log.Debugf("Connection redirect -> %t", config.Proxy.Sniff.Redirect)
log.Debugf("Http inbounds -> %v", config.HttpInbounds) log.Debugf("Connection sniff exlcude -> %v", config.Proxy.Sniff.Exclude)
config.SocksInbounds = rawConfig.Proxy.Socks
log.Debugf("Socks5 inbounds -> %v", config.SocksInbounds)
config.AddOnInbounds = rawConfig.Proxy.AddOn
log.Debugf("Add-on inbounds -> %v", config.AddOnInbounds)
} }
func decodeRadvd(rawConfig *yamlConfig, config *Config) { func decodeRadvd(rawConfig *yamlConfig, config *Config) {
@ -135,7 +122,6 @@ func decodeCustom(rawConfig *yamlConfig) []string {
func decode(rawConfig yamlConfig) Config { func decode(rawConfig yamlConfig) Config {
var config Config var config Config
config.LogLevel = rawConfig.Proxy.Log
decodeDns(&rawConfig, &config) decodeDns(&rawConfig, &config)
decodeBypass(&rawConfig, &config) decodeBypass(&rawConfig, &config)
decodeIPv4(&rawConfig, &config) decodeIPv4(&rawConfig, &config)

10
cmd/config/main.go

@ -4,6 +4,7 @@ import (
"XProxy/cmd/asset" "XProxy/cmd/asset"
"XProxy/cmd/common" "XProxy/cmd/common"
"XProxy/cmd/network" "XProxy/cmd/network"
"XProxy/cmd/proxy"
"XProxy/cmd/radvd" "XProxy/cmd/radvd"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"os" "os"
@ -15,15 +16,8 @@ type Config struct {
IPv6 network.Config IPv6 network.Config
Script []string Script []string
LogLevel string
EnableSniff bool
EnableRedirect bool
SniffExclude []string
HttpInbounds map[string]int
SocksInbounds map[string]int
AddOnInbounds []interface{}
Proxy proxy.Config
Update asset.Config Update asset.Config
Radvd radvd.Config Radvd radvd.Config
} }

37
cmd/controller.go

@ -5,10 +5,19 @@ import (
"XProxy/cmd/common" "XProxy/cmd/common"
"XProxy/cmd/config" "XProxy/cmd/config"
"XProxy/cmd/network" "XProxy/cmd/network"
"XProxy/cmd/process"
"XProxy/cmd/proxy" "XProxy/cmd/proxy"
"XProxy/cmd/radvd"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"os"
"os/signal"
"syscall"
) )
func loadRadvd(settings *config.Config) {
radvd.Load(&settings.Radvd)
}
func loadAsset(settings *config.Config) { func loadAsset(settings *config.Config) {
asset.Load(assetFile, assetDir) asset.Load(assetFile, assetDir)
asset.AutoUpdate(&settings.Update, assetDir) asset.AutoUpdate(&settings.Update, assetDir)
@ -19,20 +28,13 @@ 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) network.Load(settings.DNS, &settings.IPv4, &settings.IPv6)
} }
func loadProxy(settings *config.Config) { func loadProxy(settings *config.Config) {
proxy.Load(configDir, exposeDir, proxy.Config{ settings.Proxy.V4TProxyPort = v4TProxyPort
Sniff: settings.EnableSniff, settings.Proxy.V6TProxyPort = v6TProxyPort
Redirect: settings.EnableRedirect, proxy.Load(configDir, exposeDir, &settings.Proxy)
V4TProxyPort: v4TProxyPort,
V6TProxyPort: v6TProxyPort,
LogLevel: settings.LogLevel,
HttpInbounds: settings.HttpInbounds,
SocksInbounds: settings.SocksInbounds,
AddOnInbounds: settings.AddOnInbounds,
})
} }
func runScript(settings *config.Config) { func runScript(settings *config.Config) {
@ -41,3 +43,16 @@ func runScript(settings *config.Config) {
common.RunCommand("sh", "-c", script) common.RunCommand("sh", "-c", script)
} }
} }
func runProcess(command ...string) {
sub := process.New(command...)
sub.Run(true)
sub.Daemon()
subProcess = append(subProcess, sub)
}
func blockWait() {
sigExit := make(chan os.Signal, 1)
signal.Notify(sigExit, syscall.SIGINT, syscall.SIGTERM) // wait until get exit signal
<-sigExit
}

2
cmd/network/main.go

@ -16,7 +16,7 @@ type Config struct {
var run = common.RunCommand 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
loadV4Network(ipv4) loadV4Network(ipv4)

4
cmd/network/network.go

@ -31,7 +31,7 @@ func flushNetwork() {
run("ip", "link", "set", "eth0", "up") run("ip", "link", "set", "eth0", "up")
} }
func loadV4Network(v4 Config) { func loadV4Network(v4 *Config) {
log.Info("Enabled IPv4 forward") log.Info("Enabled IPv4 forward")
run("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")
@ -43,7 +43,7 @@ func loadV4Network(v4 Config) {
} }
} }
func loadV6Network(v6 Config) { func loadV6Network(v6 *Config) {
log.Info("Enabled IPv6 forward") log.Info("Enabled IPv6 forward")
run("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")

4
cmd/network/tproxy.go

@ -5,7 +5,7 @@ import (
"strconv" "strconv"
) )
func loadV4TProxy(v4 Config, v4SysCidr []string) { 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...)
@ -23,7 +23,7 @@ func loadV4TProxy(v4 Config, v4SysCidr []string) {
run("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...)

34
cmd/proxy/main.go

@ -5,15 +5,17 @@ import (
) )
type Config struct { type Config struct {
Sniff bool Log string `yaml:"log" json:"json"`
Redirect bool Http map[string]int `yaml:"http" json:"http"`
Socks map[string]int `yaml:"socks" json:"socks"`
AddOn []interface{} `yaml:"addon" json:"addon"`
Sniff struct {
Enable bool `yaml:"enable" json:"enable"`
Redirect bool `yaml:"redirect" json:"redirect"`
Exclude []string `yaml:"exclude" json:"exclude"`
} `yaml:"sniff" json:"sniff"`
V4TProxyPort int V4TProxyPort int
V6TProxyPort int V6TProxyPort int
LogLevel string
SniffExclude []string
HttpInbounds map[string]int
SocksInbounds map[string]int
AddOnInbounds []interface{}
} }
func saveConfig(configDir string, caption string, content string, overwrite bool) { func saveConfig(configDir string, caption string, content string, overwrite bool) {
@ -21,23 +23,23 @@ func saveConfig(configDir string, caption string, content string, overwrite bool
common.WriteFile(filePath, content+"\n", overwrite) common.WriteFile(filePath, content+"\n", overwrite)
} }
func loadInbounds(config Config) string { func loadInbounds(config *Config) string {
sniff := sniffObject{ sniff := sniffObject{
Enabled: config.Sniff, Enabled: config.Sniff.Enable,
RouteOnly: !config.Redirect, RouteOnly: !config.Sniff.Redirect,
DestOverride: []string{"http", "tls", "quic"}, DestOverride: []string{"http", "tls", "quic"},
DomainsExcluded: config.SniffExclude, DomainsExcluded: config.Sniff.Exclude,
} }
var inbounds []interface{} var inbounds []interface{}
inbounds = append(inbounds, loadTProxyConfig("tproxy", config.V4TProxyPort, sniff)) inbounds = append(inbounds, loadTProxyConfig("tproxy", config.V4TProxyPort, sniff))
inbounds = append(inbounds, loadTProxyConfig("tproxy6", config.V6TProxyPort, sniff)) inbounds = append(inbounds, loadTProxyConfig("tproxy6", config.V6TProxyPort, sniff))
for tag, port := range config.HttpInbounds { for tag, port := range config.Http {
inbounds = append(inbounds, loadHttpConfig(tag, port, sniff)) inbounds = append(inbounds, loadHttpConfig(tag, port, sniff))
} }
for tag, port := range config.SocksInbounds { for tag, port := range config.Socks {
inbounds = append(inbounds, loadSocksConfig(tag, port, sniff)) inbounds = append(inbounds, loadSocksConfig(tag, port, sniff))
} }
for _, addon := range config.AddOnInbounds { for _, addon := range config.AddOn {
inbounds = append(inbounds, addon) inbounds = append(inbounds, addon)
} }
return common.JsonEncode(inboundsObject{ return common.JsonEncode(inboundsObject{
@ -45,7 +47,7 @@ func loadInbounds(config Config) string {
}) })
} }
func Load(configDir string, exposeDir string, config Config) { func Load(configDir string, exposeDir string, config *Config) {
common.CreateFolder(exposeDir + "/log") common.CreateFolder(exposeDir + "/log")
common.CreateFolder(exposeDir + "/config") common.CreateFolder(exposeDir + "/config")
common.CreateFolder(configDir) common.CreateFolder(configDir)
@ -53,7 +55,7 @@ func Load(configDir string, exposeDir string, config Config) {
saveConfig(exposeDir+"/config", "route", routeConfig, false) saveConfig(exposeDir+"/config", "route", routeConfig, false)
saveConfig(exposeDir+"/config", "outbounds", outboundsConfig, false) saveConfig(exposeDir+"/config", "outbounds", outboundsConfig, false)
saveConfig(configDir, "inbounds", loadInbounds(config), true) saveConfig(configDir, "inbounds", loadInbounds(config), true)
saveConfig(configDir, "log", loadLogConfig(config.LogLevel, exposeDir+"/log"), true) saveConfig(configDir, "log", loadLogConfig(config.Log, exposeDir+"/log"), true)
for _, configFile := range common.ListFiles(exposeDir+"/config", ".json") { for _, configFile := range common.ListFiles(exposeDir+"/config", ".json") {
common.CopyFile(exposeDir+"/config/"+configFile, configDir+"/"+configFile) common.CopyFile(exposeDir+"/config/"+configFile, configDir+"/"+configFile)
} }

19
cmd/xproxy.go

@ -3,11 +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/signal"
"syscall"
) )
var version = "0.0.9" var version = "0.0.9"
@ -25,19 +21,6 @@ var configFile = exposeDir + "/config.yml"
var subProcess []*process.Process var subProcess []*process.Process
func runProcess(command ...string) {
sub := process.New(command...)
sub.Run(true)
sub.Daemon()
subProcess = append(subProcess, sub)
}
func blockWait() {
sigExit := make(chan os.Signal, 1)
signal.Notify(sigExit, syscall.SIGINT, syscall.SIGTERM) // wait until get exit signal
<-sigExit
}
func main() { func main() {
defer func() { defer func() {
if err := recover(); err != nil { if err := recover(); err != nil {
@ -52,7 +35,7 @@ func main() {
loadNetwork(&settings) loadNetwork(&settings)
loadProxy(&settings) loadProxy(&settings)
loadAsset(&settings) loadAsset(&settings)
radvd.Load(&settings.Radvd) loadRadvd(&settings)
runScript(&settings) runScript(&settings)
runProcess("xray", "-confdir", configDir) runProcess("xray", "-confdir", configDir)

Loading…
Cancel
Save