From f40885765dff49c996dcde35aaa1ee963b1b89b5 Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Thu, 18 Aug 2022 00:42:05 +0800 Subject: [PATCH] update: xproxy init process --- Dockerfile | 5 ++--- cmd/asset/update.go | 11 ++++++++++- cmd/controller.go | 45 ++++++++++++++++++++++++++++++++++++++++++ cmd/network/main.go | 4 +--- cmd/xproxy.go | 32 ++++++++++++++++++++++++++++++ main.go | 48 --------------------------------------------- src/main.go | 28 -------------------------- 7 files changed, 90 insertions(+), 83 deletions(-) create mode 100644 cmd/controller.go create mode 100644 cmd/xproxy.go delete mode 100644 main.go diff --git a/Dockerfile b/Dockerfile index c9ce636..c18aa34 100644 --- a/Dockerfile +++ b/Dockerfile @@ -14,8 +14,7 @@ RUN wget https://github.com/XTLS/Xray-core/archive/refs/tags/v${XRAY_VERSION}.ta tar xf v${XRAY_VERSION}.tar.gz WORKDIR ./Xray-core-${XRAY_VERSION}/ RUN go mod download -x -RUN env CGO_ENABLED=0 go build -v -o xray -trimpath -ldflags "-s -w" ./main && \ - mv ./xray /tmp/ +RUN env CGO_ENABLED=0 go build -v -o xray -trimpath -ldflags "-s -w" ./main && mv ./xray /tmp/ COPY --from=upx /upx/ /usr/ RUN upx -9 /tmp/xray @@ -24,7 +23,7 @@ RUN apk add xz WORKDIR /tmp/ RUN wget "https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geoip.dat" RUN wget "https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geosite.dat" -RUN mkdir -p /asset/etc/xproxy/ && tar cJf /asset/etc/xproxy/assets.tar.xz ./geo*.dat +RUN mkdir -p /asset/ && tar cJf /asset/assets.tar.xz ./*.dat COPY --from=xray /tmp/xray /asset/usr/bin/ FROM alpine:3.16 diff --git a/cmd/asset/update.go b/cmd/asset/update.go index cb70172..c78a91b 100644 --- a/cmd/asset/update.go +++ b/cmd/asset/update.go @@ -2,10 +2,11 @@ package asset import ( "XProxy/cmd/common" + "github.com/robfig/cron" log "github.com/sirupsen/logrus" ) -func UpdateAssets(urls map[string]string, assetDir string) { +func updateAssets(urls map[string]string, assetDir string) { if len(urls) != 0 { log.Info("Start update assets") for file, url := range urls { @@ -13,3 +14,11 @@ func UpdateAssets(urls map[string]string, assetDir string) { } } } + +func AutoUpdate(updateCron string, updateUrls map[string]string, assetDir string) { + autoUpdate := cron.New() + _ = autoUpdate.AddFunc(updateCron, func() { + updateAssets(updateUrls, assetDir) + }) + autoUpdate.Start() +} diff --git a/cmd/controller.go b/cmd/controller.go new file mode 100644 index 0000000..66864da --- /dev/null +++ b/cmd/controller.go @@ -0,0 +1,45 @@ +package main + +import ( + "XProxy/cmd/asset" + "XProxy/cmd/config" + "XProxy/cmd/network" + "XProxy/cmd/proxy" +) + +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 loadProxy(settings *config.Config) { + proxy.Load(configDir, exposeDir, proxy.Config{ + Sniff: settings.EnableSniff, + Redirect: settings.EnableRedirect, + 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) +} diff --git a/cmd/network/main.go b/cmd/network/main.go index 1bb33a7..2f6c2c0 100644 --- a/cmd/network/main.go +++ b/cmd/network/main.go @@ -11,12 +11,10 @@ type Config struct { } func Load(dns []string, ipv4 Config, ipv6 Config) { - loadDns(dns) // init dns server - + 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()) diff --git a/cmd/xproxy.go b/cmd/xproxy.go new file mode 100644 index 0000000..2520dc0 --- /dev/null +++ b/cmd/xproxy.go @@ -0,0 +1,32 @@ +package main + +import ( + "XProxy/cmd/config" + "fmt" + log "github.com/sirupsen/logrus" +) + +var version = "dev" + +var v4RouteTable = 100 +var v6RouteTable = 106 +var v4TProxyPort = 7288 +var v6TProxyPort = 7289 + +var exposeDir = "/xproxy" +var configDir = "/etc/xproxy" +var assetFile = "/assets.tar.xz" +var assetDir = exposeDir + "/assets" +var configFile = exposeDir + "/config.yml" + +func main() { + log.SetLevel(log.DebugLevel) + fmt.Println("XProxy start -> version =", version) + + settings := config.Load(configFile) + loadNetwork(&settings) + loadProxy(&settings) + loadAsset(&settings) + + // TODO: start xray service +} diff --git a/main.go b/main.go deleted file mode 100644 index a4f8e95..0000000 --- a/main.go +++ /dev/null @@ -1,48 +0,0 @@ -package main - -import ( - "XProxy/cmd/config" - "XProxy/cmd/network" - "fmt" - 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() { - log.SetLevel(log.DebugLevel) - fmt.Println("XProxy start") - - settings := config.Load(exposeDir + "/config.yml") - fmt.Println(settings) - - loadNetwork(&settings) - - // TODO: load proxy - // TODO: load asset - - // TODO: start xray service -} diff --git a/src/main.go b/src/main.go index f043845..743125c 100644 --- a/src/main.go +++ b/src/main.go @@ -1,26 +1,13 @@ package main import ( - "github.com/robfig/cron" log "github.com/sirupsen/logrus" "os" "os/signal" "syscall" ) -var logLevel = "warning" - var preScript []string -var updateCron string -var updateUrls map[string]string - -var enableSniff bool -var enableRedirect bool -var httpInbounds map[string]int -var socksInbounds map[string]int -var addOnInbounds []interface{} - -var assetFile = "/etc/xproxy/assets.tar.xz" func main() { defer func() { @@ -29,21 +16,6 @@ func main() { } }() - log.SetLevel(log.DebugLevel) - log.Warning("XProxy start") - loadConfig("/xproxy/config.yml") - loadProxy("/etc/xproxy/config", "/xproxy") - loadGeoSite("/xproxy/assets") - loadGeoIp("/xproxy/assets") - autoUpdate := cron.New() - _ = autoUpdate.AddFunc(updateCron, func() { - updateAssets("/xproxy/assets") - }) - autoUpdate.Start() - - loadDns() - loadNetwork() - loadTProxy() for _, script := range preScript { log.Infof("Run script command -> %s", script) runCommand("sh", "-c", script)