Browse Source

update: complete config package

v1.x.x
dnomd343 2 years ago
parent
commit
e75c8dada5
  1. 36
      cmd/config/decode.go
  2. 19
      cmd/config/main.go
  3. 61
      main.go
  4. 38
      src/config.go
  5. 5
      src/main.go

36
cmd/config/decode.go

@ -80,7 +80,7 @@ func decodeIPv4(rawConfig *yamlConfig) (string, string) {
if v4Gateway != "" && !common.IsIPv4(v4Gateway, false) { if v4Gateway != "" && !common.IsIPv4(v4Gateway, false) {
log.Panicf("Invalid IPv4 gateway -> %s", v4Gateway) log.Panicf("Invalid IPv4 gateway -> %s", v4Gateway)
} }
log.Infof("IPv4 -> address = %s | gateway = %s", v4Address, v4Gateway) log.Debugf("IPv4 -> address = %s | gateway = %s", v4Address, v4Gateway)
return v4Address, v4Gateway return v4Address, v4Gateway
} }
@ -93,17 +93,45 @@ func decodeIPv6(rawConfig *yamlConfig) (string, string) {
if v6Gateway != "" && !common.IsIPv6(v6Gateway, false) { if v6Gateway != "" && !common.IsIPv6(v6Gateway, false) {
log.Panicf("Invalid IPv6 gateway -> %s", v6Gateway) log.Panicf("Invalid IPv6 gateway -> %s", v6Gateway)
} }
log.Infof("IPv6 -> address = %s | gateway = %s", v6Address, v6Gateway) log.Debugf("IPv6 -> address = %s | gateway = %s", v6Address, v6Gateway)
return v6Address, v6Gateway return v6Address, v6Gateway
} }
func decodeProxy(rawConfig *yamlConfig, config *Config) {
config.EnableSniff = rawConfig.Proxy.Sniff
log.Debugf("Connection sniff -> %v", config.EnableSniff)
config.EnableRedirect = rawConfig.Proxy.Redirect
log.Debugf("Connection redirect -> %v", config.EnableRedirect)
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)
}
func decodeUpdate(rawConfig *yamlConfig) (string, map[string]string) {
updateCron := rawConfig.Update.Cron
log.Debugf("Update cron -> %s", updateCron)
updateUrls := rawConfig.Update.Url
log.Debugf("Update urls -> %v", updateUrls)
return updateCron, updateUrls
}
func decodeCustom(rawConfig *yamlConfig) []string {
customScript := rawConfig.Custom
log.Debugf("Custom script -> %v", customScript)
return customScript
}
func decode(rawConfig yamlConfig) Config { func decode(rawConfig yamlConfig) Config {
var config Config var config Config
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)
config.V6Address, config.V6Gateway = decodeIPv6(&rawConfig) config.V6Address, config.V6Gateway = decodeIPv6(&rawConfig)
decodeProxy(&rawConfig, &config)
config.UpdateCron, config.UpdateUrls = decodeUpdate(&rawConfig)
config.Script = decodeCustom(&rawConfig)
return config return config
} }

19
cmd/config/main.go

@ -7,22 +7,23 @@ import (
) )
type Config struct { type Config struct {
// asset update
DNS []string DNS []string
V4Bypass []string
V6Bypass []string
V4Address string V4Address string
V4Gateway string V4Gateway string
V4Bypass []string
V6Address string V6Address string
V6Gateway string V6Gateway string
V6Bypass []string
// httpInbounds Script []string
// socksInbounds UpdateCron string
// addOnInbounds UpdateUrls map[string]string
EnableSniff bool
EnableRedirect bool
HttpInbounds map[string]int
SocksInbounds map[string]int
AddOnInbounds []interface{}
} }
func Load(configFile string) Config { func Load(configFile string) Config {

61
main.go

@ -1,39 +1,48 @@
package main package main
import ( import (
"XProxy/cmd/config"
"XProxy/cmd/network" "XProxy/cmd/network"
"XProxy/cmd/proxy"
"fmt" "fmt"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
var exposeDir = "/xproxy"
var v4RouteTable = 100
var v6RouteTable = 106
var v4TProxyPort = 7288
var v6TProxyPort = 7289
func loadNetwork(settings *config.Config) {
v4Settings := network.Config{
RouteTable: v4RouteTable,
TProxyPort: v4TProxyPort,
Address: settings.V4Address,
Gateway: settings.V4Gateway,
Bypass: settings.V4Bypass,
}
v6Settings := network.Config{
RouteTable: v6RouteTable,
TProxyPort: v6TProxyPort,
Address: settings.V6Address,
Gateway: settings.V6Gateway,
Bypass: settings.V6Bypass,
}
network.Load(settings.DNS, v4Settings, v6Settings)
}
func main() { func main() {
log.SetLevel(log.DebugLevel) log.SetLevel(log.DebugLevel)
fmt.Println("XProxy start") fmt.Println("XProxy start")
network.Load(nil, network.Config{ settings := config.Load(exposeDir + "/config.yml")
RouteTable: 100, fmt.Println(settings)
TProxyPort: 7288,
Address: "192.168.2.2", loadNetwork(&settings)
Gateway: "192.168.2.1",
Bypass: make([]string, 0), // TODO: load proxy
}, network.Config{ // TODO: load asset
RouteTable: 106,
TProxyPort: 7289, // TODO: start xray service
Address: "fc00::2",
Gateway: "fc00::1",
Bypass: make([]string, 0),
})
proxy.Load("/etc/xproxy", "/xproxy", proxy.Config{
Sniff: true,
Redirect: true,
V4TProxyPort: 7288,
V6TProxyPort: 7289,
LogLevel: "debug",
HttpInbounds: nil,
SocksInbounds: nil,
AddOnInbounds: nil,
})
} }

38
src/config.go

@ -1,38 +0,0 @@
package main
import (
log "github.com/sirupsen/logrus"
)
var v4Bypass []string
var v6Bypass []string
var dnsServer []string
var v4Gateway string
var v4Address string
var v6Gateway string
var v6Address string
func loadConfig(configFile string) {
config := Config{}
enableSniff = config.Proxy.Sniff
log.Infof("Connection sniff -> %v", enableSniff)
enableRedirect = config.Proxy.Redirect
log.Infof("Connection redirect -> %v", enableRedirect)
httpInbounds = config.Proxy.Http
log.Infof("Http inbounds -> %v", httpInbounds)
socksInbounds = config.Proxy.Socks
log.Infof("Socks5 inbounds -> %v", socksInbounds)
addOnInbounds = config.Proxy.AddOn
log.Infof("Add-on inbounds -> %v", addOnInbounds)
updateCron = config.Update.Cron
log.Infof("Update cron -> %s", updateCron)
updateUrls = config.Update.Url
log.Infof("Update url -> %v", updateUrls)
preScript = config.Script
log.Infof("Pre-script -> %v", preScript)
}

5
src/main.go

@ -10,11 +10,6 @@ import (
var logLevel = "warning" var logLevel = "warning"
var v4RouteTable = 100
var v6RouteTable = 106
var v4TProxyPort = 7288
var v6TProxyPort = 7289
var preScript []string var preScript []string
var updateCron string var updateCron string
var updateUrls map[string]string var updateUrls map[string]string

Loading…
Cancel
Save