From 7dddb380c663fa7cde69611c42caeeb39addc642 Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Tue, 16 Aug 2022 13:38:49 +0800 Subject: [PATCH] feat: extract geo assets --- Dockerfile | 2 +- src/load.go | 21 ++++++++++++++++++++- src/main.go | 25 ++++++++++++++----------- 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/Dockerfile b/Dockerfile index ddadbd2..c9ce636 100644 --- a/Dockerfile +++ b/Dockerfile @@ -29,5 +29,5 @@ COPY --from=xray /tmp/xray /asset/usr/bin/ FROM alpine:3.16 COPY --from=asset /asset/ / -ENV XRAY_LOCATION_ASSET=/etc/xproxy/assets +ENV XRAY_LOCATION_ASSET=/xproxy/assets RUN apk add --no-cache iptables ip6tables diff --git a/src/load.go b/src/load.go index 28fe3da..195322a 100644 --- a/src/load.go +++ b/src/load.go @@ -90,7 +90,7 @@ func isFileExist(filePath string) bool { } func createFolder(folderPath string) { - log.Debugf("Loading folder -> %s", folderPath) + log.Debugf("Create folder -> %s", folderPath) err := os.MkdirAll(folderPath, 0755) if err != nil { log.Panicf("Failed to create folder -> %s", folderPath) @@ -231,3 +231,22 @@ func loadProxy(configDir string, exposeDir string) { copyFile(exposeDir+"/config/"+configFile, configDir+"/"+configFile) } } + +func extractGeoFile(archivePath string, geoFile string, targetDir string) { + if isFileExist(targetDir + "/" + geoFile) { + log.Debugf("Asset %s exist -> skip extract", geoFile) + return + } + log.Infof("Extract asset file -> %s", targetDir+"/"+geoFile) + runCommand("tar", "xvf", archivePath, "./"+geoFile, "-C", targetDir) +} + +func loadGeoIp(assetDir string) { + createFolder(assetDir) + extractGeoFile(assetFile, "geoip.dat", assetDir) +} + +func loadGeoSite(assetDir string) { + createFolder(assetDir) + extractGeoFile(assetFile, "geosite.dat", assetDir) +} diff --git a/src/main.go b/src/main.go index 7bb367b..307f9f7 100644 --- a/src/main.go +++ b/src/main.go @@ -2,7 +2,6 @@ package main import ( log "github.com/sirupsen/logrus" - "os" ) var logLevel = "warning" @@ -18,22 +17,26 @@ var httpInbounds map[string]int var socksInbounds map[string]int var addOnInbounds []interface{} +var assetFile = "/etc/xproxy/assets.tar.xz" + func main() { log.SetLevel(log.DebugLevel) log.Warning("XProxy start") - content, err := os.ReadFile("test.yml") - if err != nil { - panic(err) - } - loadConfig(content) - loadProxy("/etc/xproxy/config", "/xproxy") + //content, err := os.ReadFile("test.yml") + //if err != nil { + // panic(err) + //} + //loadConfig(content) + //loadProxy("/etc/xproxy/config", "/xproxy") - // TODO: load geo assets + loadGeoIp("/xproxy/assets") + loadGeoSite("/xproxy/assets") + // TODO: auto-update assets file (by cron command) - loadDns() - loadNetwork() - loadTProxy() + //loadDns() + //loadNetwork() + //loadTProxy() // TODO: running custom script // TODO: start xray service