diff --git a/cmd/asset/asset.go b/cmd/asset/asset.go new file mode 100644 index 0000000..0d4ec82 --- /dev/null +++ b/cmd/asset/asset.go @@ -0,0 +1,21 @@ +package asset + +import ( + "XProxy/cmd/common" + log "github.com/sirupsen/logrus" +) + +func extractFile(archive string, geoFile string, targetDir string) { + if common.IsFileExist(targetDir + "/" + geoFile) { + log.Debugf("Asset %s exist -> skip extract", geoFile) + return + } + log.Infof("Extract asset file -> %s", targetDir+"/"+geoFile) + common.RunCommand("tar", "xvf", archive, "./"+geoFile, "-C", targetDir) +} + +func Load(assetFile string, assetDir string) { + common.CreateFolder(assetDir) + extractFile(assetFile, "geoip.dat", assetDir) + extractFile(assetFile, "geosite.dat", assetDir) +} diff --git a/cmd/asset/geofile.go b/cmd/asset/geofile.go deleted file mode 100644 index 1a0db34..0000000 --- a/cmd/asset/geofile.go +++ /dev/null @@ -1,25 +0,0 @@ -package asset - -import ( - "XProxy/cmd/common" - log "github.com/sirupsen/logrus" -) - -func extractGeoFile(archivePath string, geoFile string, targetDir string) { - if common.IsFileExist(targetDir + "/" + geoFile) { - log.Debugf("Asset %s exist -> skip extract", geoFile) - return - } - log.Infof("Extract asset file -> %s", targetDir+"/"+geoFile) - common.RunCommand("tar", "xvf", archivePath, "./"+geoFile, "-C", targetDir) -} - -func LoadGeoIp(assetFile string, assetDir string) { - common.CreateFolder(assetDir) - extractGeoFile(assetFile, "geoip.dat", assetDir) -} - -func LoadGeoSite(assetFile string, assetDir string) { - common.CreateFolder(assetDir) - extractGeoFile(assetFile, "geosite.dat", assetDir) -} diff --git a/cmd/asset/update.go b/cmd/asset/update.go index c78a91b..3526874 100644 --- a/cmd/asset/update.go +++ b/cmd/asset/update.go @@ -1,24 +1,29 @@ package asset import ( - "XProxy/cmd/common" - "github.com/robfig/cron" - log "github.com/sirupsen/logrus" + "XProxy/cmd/common" + "github.com/robfig/cron" + log "github.com/sirupsen/logrus" ) -func updateAssets(urls map[string]string, assetDir string) { - if len(urls) != 0 { - log.Info("Start update assets") - for file, url := range urls { - common.DownloadFile(url, assetDir+"/"+file) - } - } +type Config struct { + Cron string `yaml:"cron" json:"cron"` + Url map[string]string `yaml:"url" json:"url"` } -func AutoUpdate(updateCron string, updateUrls map[string]string, assetDir string) { - autoUpdate := cron.New() - _ = autoUpdate.AddFunc(updateCron, func() { - updateAssets(updateUrls, assetDir) - }) - autoUpdate.Start() +func updateAsset(urls map[string]string, assetDir string) { + if len(urls) != 0 { + log.Info("Start update assets") + for file, url := range urls { + common.DownloadFile(url, assetDir+"/"+file) + } + } +} + +func AutoUpdate(update *Config, assetDir string) { + autoUpdate := cron.New() + _ = autoUpdate.AddFunc(update.Cron, func() { + updateAsset(update.Url, assetDir) + }) + autoUpdate.Start() } diff --git a/cmd/config/decode.go b/cmd/config/decode.go index 8b8fab5..bc6fccc 100644 --- a/cmd/config/decode.go +++ b/cmd/config/decode.go @@ -1,6 +1,7 @@ package config import ( + "XProxy/cmd/asset" "XProxy/cmd/common" "XProxy/cmd/radvd" log "github.com/sirupsen/logrus" @@ -13,12 +14,9 @@ type yamlNetConfig struct { } type yamlConfig struct { - Custom []string `yaml:"custom"` - Update struct { - Cron string `yaml:"cron"` - Url map[string]string `yaml:"url"` - } `yaml:"update"` - Proxy struct { + Custom []string `yaml:"custom"` + Update asset.Config `yaml:"update"` + Proxy struct { Log string `yaml:"log"` Sniff struct { Enable bool `yaml:"enable"` @@ -129,12 +127,10 @@ func decodeRadvd(rawConfig *yamlConfig, config *Config) { log.Debugf("Radvd DNSSL -> %v", config.Radvd.DNSSL) } -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 decodeUpdate(rawConfig *yamlConfig, config *Config) { + config.Update = rawConfig.Update + log.Debugf("Update cron -> %s", config.Update.Cron) + log.Debugf("Update urls -> %v", config.Update.Url) } func decodeCustom(rawConfig *yamlConfig) []string { @@ -151,7 +147,7 @@ func decode(rawConfig yamlConfig) Config { config.V4Address, config.V4Gateway = decodeIPv4(&rawConfig) config.V6Address, config.V6Gateway = decodeIPv6(&rawConfig) decodeProxy(&rawConfig, &config) - config.UpdateCron, config.UpdateUrls = decodeUpdate(&rawConfig) + decodeUpdate(&rawConfig, &config) config.Script = decodeCustom(&rawConfig) decodeRadvd(&rawConfig, &config) return config diff --git a/cmd/config/main.go b/cmd/config/main.go index 2b6c545..de4db71 100644 --- a/cmd/config/main.go +++ b/cmd/config/main.go @@ -1,6 +1,7 @@ package config import ( + "XProxy/cmd/asset" "XProxy/cmd/common" "XProxy/cmd/radvd" log "github.com/sirupsen/logrus" @@ -16,10 +17,8 @@ type Config struct { V6Address string V6Gateway string - Script []string - LogLevel string - UpdateCron string - UpdateUrls map[string]string + Script []string + LogLevel string EnableSniff bool EnableRedirect bool @@ -28,10 +27,8 @@ type Config struct { SocksInbounds map[string]int AddOnInbounds []interface{} - Radvd radvd.Config - //RadvdEnable bool - //RadvdOptions map[string]string - //RadvdPrefix map[string]map[string]string + Update asset.Config + Radvd radvd.Config } func Load(configFile string) Config { diff --git a/cmd/controller.go b/cmd/controller.go index 02f14c8..dceb46d 100644 --- a/cmd/controller.go +++ b/cmd/controller.go @@ -10,9 +10,8 @@ import ( ) func loadAsset(settings *config.Config) { - asset.LoadGeoSite(assetFile, assetDir) - asset.LoadGeoIp(assetFile, assetDir) - asset.AutoUpdate(settings.UpdateCron, settings.UpdateUrls, assetDir) + asset.Load(assetFile, assetDir) + asset.AutoUpdate(&settings.Update, assetDir) } func loadProxy(settings *config.Config) {