Browse Source

feat: extract geo assets

v1.x.x
dnomd343 2 years ago
parent
commit
7dddb380c6
  1. 2
      Dockerfile
  2. 21
      src/load.go
  3. 25
      src/main.go

2
Dockerfile

@ -29,5 +29,5 @@ COPY --from=xray /tmp/xray /asset/usr/bin/
FROM alpine:3.16 FROM alpine:3.16
COPY --from=asset /asset/ / COPY --from=asset /asset/ /
ENV XRAY_LOCATION_ASSET=/etc/xproxy/assets ENV XRAY_LOCATION_ASSET=/xproxy/assets
RUN apk add --no-cache iptables ip6tables RUN apk add --no-cache iptables ip6tables

21
src/load.go

@ -90,7 +90,7 @@ func isFileExist(filePath string) bool {
} }
func createFolder(folderPath string) { func createFolder(folderPath string) {
log.Debugf("Loading folder -> %s", folderPath) log.Debugf("Create folder -> %s", folderPath)
err := os.MkdirAll(folderPath, 0755) err := os.MkdirAll(folderPath, 0755)
if err != nil { if err != nil {
log.Panicf("Failed to create folder -> %s", folderPath) log.Panicf("Failed to create folder -> %s", folderPath)
@ -231,3 +231,22 @@ func loadProxy(configDir string, exposeDir string) {
copyFile(exposeDir+"/config/"+configFile, configDir+"/"+configFile) 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)
}

25
src/main.go

@ -2,7 +2,6 @@ package main
import ( import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"os"
) )
var logLevel = "warning" var logLevel = "warning"
@ -18,22 +17,26 @@ var httpInbounds map[string]int
var socksInbounds map[string]int var socksInbounds map[string]int
var addOnInbounds []interface{} var addOnInbounds []interface{}
var assetFile = "/etc/xproxy/assets.tar.xz"
func main() { func main() {
log.SetLevel(log.DebugLevel) log.SetLevel(log.DebugLevel)
log.Warning("XProxy start") log.Warning("XProxy start")
content, err := os.ReadFile("test.yml") //content, err := os.ReadFile("test.yml")
if err != nil { //if err != nil {
panic(err) // panic(err)
} //}
loadConfig(content) //loadConfig(content)
loadProxy("/etc/xproxy/config", "/xproxy") //loadProxy("/etc/xproxy/config", "/xproxy")
// TODO: load geo assets loadGeoIp("/xproxy/assets")
loadGeoSite("/xproxy/assets")
// TODO: auto-update assets file (by cron command)
loadDns() //loadDns()
loadNetwork() //loadNetwork()
loadTProxy() //loadTProxy()
// TODO: running custom script // TODO: running custom script
// TODO: start xray service // TODO: start xray service

Loading…
Cancel
Save