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

10
cmd/config/main.go

@ -4,6 +4,7 @@ import (
"XProxy/cmd/asset"
"XProxy/cmd/common"
"XProxy/cmd/network"
"XProxy/cmd/proxy"
"XProxy/cmd/radvd"
log "github.com/sirupsen/logrus"
"os"
@ -15,15 +16,8 @@ type Config struct {
IPv6 network.Config
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
Radvd radvd.Config
}

37
cmd/controller.go

@ -5,10 +5,19 @@ import (
"XProxy/cmd/common"
"XProxy/cmd/config"
"XProxy/cmd/network"
"XProxy/cmd/process"
"XProxy/cmd/proxy"
"XProxy/cmd/radvd"
log "github.com/sirupsen/logrus"
"os"
"os/signal"
"syscall"
)
func loadRadvd(settings *config.Config) {
radvd.Load(&settings.Radvd)
}
func loadAsset(settings *config.Config) {
asset.Load(assetFile, assetDir)
asset.AutoUpdate(&settings.Update, assetDir)
@ -19,20 +28,13 @@ 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)
}
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,
})
settings.Proxy.V4TProxyPort = v4TProxyPort
settings.Proxy.V6TProxyPort = v6TProxyPort
proxy.Load(configDir, exposeDir, &settings.Proxy)
}
func runScript(settings *config.Config) {
@ -41,3 +43,16 @@ func runScript(settings *config.Config) {
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
func Load(dns []string, ipv4 Config, ipv6 Config) {
func Load(dns []string, ipv4 *Config, ipv6 *Config) {
loadDns(dns) // init dns server
flushNetwork() // clear network settings
loadV4Network(ipv4)

4
cmd/network/network.go

@ -31,7 +31,7 @@ func flushNetwork() {
run("ip", "link", "set", "eth0", "up")
}
func loadV4Network(v4 Config) {
func loadV4Network(v4 *Config) {
log.Info("Enabled IPv4 forward")
run("sysctl", "-w", "net.ipv4.ip_forward=1")
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")
run("sysctl", "-w", "net.ipv6.conf.all.forwarding=1")
log.Info("Setting up system IPv6 configure")

4
cmd/network/tproxy.go

@ -5,7 +5,7 @@ import (
"strconv"
)
func loadV4TProxy(v4 Config, v4SysCidr []string) {
func loadV4TProxy(v4 *Config, v4SysCidr []string) {
log.Info("Setting up TProxy of IPv4")
tableNum := strconv.Itoa(v4.RouteTable)
v4Bypass := append(v4SysCidr, v4.Bypass...)
@ -23,7 +23,7 @@ func loadV4TProxy(v4 Config, v4SysCidr []string) {
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")
tableNum := strconv.Itoa(v6.RouteTable)
v6Bypass := append(v6SysCidr, v6.Bypass...)

34
cmd/proxy/main.go

@ -5,15 +5,17 @@ import (
)
type Config struct {
Sniff bool
Redirect bool
Log string `yaml:"log" json:"json"`
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
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) {
@ -21,23 +23,23 @@ func saveConfig(configDir string, caption string, content string, overwrite bool
common.WriteFile(filePath, content+"\n", overwrite)
}
func loadInbounds(config Config) string {
func loadInbounds(config *Config) string {
sniff := sniffObject{
Enabled: config.Sniff,
RouteOnly: !config.Redirect,
Enabled: config.Sniff.Enable,
RouteOnly: !config.Sniff.Redirect,
DestOverride: []string{"http", "tls", "quic"},
DomainsExcluded: config.SniffExclude,
DomainsExcluded: config.Sniff.Exclude,
}
var inbounds []interface{}
inbounds = append(inbounds, loadTProxyConfig("tproxy", config.V4TProxyPort, 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))
}
for tag, port := range config.SocksInbounds {
for tag, port := range config.Socks {
inbounds = append(inbounds, loadSocksConfig(tag, port, sniff))
}
for _, addon := range config.AddOnInbounds {
for _, addon := range config.AddOn {
inbounds = append(inbounds, addon)
}
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 + "/config")
common.CreateFolder(configDir)
@ -53,7 +55,7 @@ func Load(configDir string, exposeDir string, config Config) {
saveConfig(exposeDir+"/config", "route", routeConfig, false)
saveConfig(exposeDir+"/config", "outbounds", outboundsConfig, false)
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") {
common.CopyFile(exposeDir+"/config/"+configFile, configDir+"/"+configFile)
}

19
cmd/xproxy.go

@ -3,11 +3,7 @@ package main
import (
"XProxy/cmd/config"
"XProxy/cmd/process"
"XProxy/cmd/radvd"
log "github.com/sirupsen/logrus"
"os"
"os/signal"
"syscall"
)
var version = "0.0.9"
@ -25,19 +21,6 @@ var configFile = exposeDir + "/config.yml"
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() {
defer func() {
if err := recover(); err != nil {
@ -52,7 +35,7 @@ func main() {
loadNetwork(&settings)
loadProxy(&settings)
loadAsset(&settings)
radvd.Load(&settings.Radvd)
loadRadvd(&settings)
runScript(&settings)
runProcess("xray", "-confdir", configDir)

Loading…
Cancel
Save