From c9b917663b189994d07e385fad32570ffc585093 Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Sun, 28 Aug 2022 01:15:34 +0800 Subject: [PATCH] update: `disable` option for asset --- cmd/asset/update.go | 17 ++++++++++------- cmd/config/decode.go | 17 +++++++++-------- cmd/config/default.go | 11 ++++++----- cmd/config/main.go | 2 +- cmd/controller.go | 16 +++++++++++++--- 5 files changed, 39 insertions(+), 24 deletions(-) diff --git a/cmd/asset/update.go b/cmd/asset/update.go index 55aca79..4420926 100644 --- a/cmd/asset/update.go +++ b/cmd/asset/update.go @@ -8,9 +8,12 @@ import ( ) type Config struct { - Proxy string `yaml:"proxy" json:"proxy"` - Cron string `yaml:"cron" json:"cron"` - Url map[string]string `yaml:"url" json:"url"` + Disable bool `yaml:"disable" json:"disable"` + Update struct { + Proxy string `yaml:"proxy" json:"proxy"` + Cron string `yaml:"cron" json:"cron"` + Url map[string]string `yaml:"url" json:"url"` + } } func updateAsset(urls map[string]string, assetDir string, updateProxy string) { // download new assets @@ -27,11 +30,11 @@ func updateAsset(urls map[string]string, assetDir string, updateProxy string) { } } -func AutoUpdate(update *Config, assetDir string) { // set cron task for auto update - if update.Cron != "" { +func AutoUpdate(config *Config, assetDir string) { // set cron task for auto update + if config.Update.Cron != "" { autoUpdate := cron.New() - _ = autoUpdate.AddFunc(update.Cron, func() { // cron function - updateAsset(update.Url, assetDir, update.Proxy) + _ = autoUpdate.AddFunc(config.Update.Cron, func() { // cron function + updateAsset(config.Update.Url, assetDir, config.Update.Proxy) }) autoUpdate.Start() } diff --git a/cmd/config/decode.go b/cmd/config/decode.go index c3dcb78..48594e8 100644 --- a/cmd/config/decode.go +++ b/cmd/config/decode.go @@ -18,7 +18,7 @@ type NetConfig struct { } type RawConfig struct { - Update asset.Config `yaml:"update" json:"update"` + Asset asset.Config `yaml:"asset" json:"asset"` Radvd radvd.Config `yaml:"radvd" json:"radvd"` Proxy proxy.Config `yaml:"proxy" json:"proxy"` Custom custom.Config `yaml:"custom" json:"custom"` @@ -164,16 +164,17 @@ func decodeRadvd(rawConfig *RawConfig, config *Config) { } func decodeUpdate(rawConfig *RawConfig, config *Config) { - config.Update = rawConfig.Update - if config.Update.Proxy != "" { - _, err := url.Parse(config.Update.Proxy) // check proxy info + config.Asset = rawConfig.Asset + if config.Asset.Update.Proxy != "" { + _, err := url.Parse(config.Asset.Update.Proxy) // check proxy info if err != nil { - log.Panicf("Invalid update proxy -> %s", config.Update.Proxy) + log.Panicf("Invalid asset update proxy -> %s", config.Asset.Update.Proxy) } } - log.Debugf("Update proxy -> %s", config.Update.Proxy) - log.Debugf("Update cron -> %s", config.Update.Cron) - log.Debugf("Update urls -> %v", config.Update.Url) + log.Debugf("Asset disable -> %t", config.Asset.Disable) + log.Debugf("Asset update proxy -> %s", config.Asset.Update.Proxy) + log.Debugf("Asset update cron -> %s", config.Asset.Update.Cron) + log.Debugf("Asset update urls -> %v", config.Asset.Update.Url) } func decodeCustom(rawConfig *RawConfig, config *Config) { diff --git a/cmd/config/default.go b/cmd/config/default.go index eca72c0..8388ec6 100644 --- a/cmd/config/default.go +++ b/cmd/config/default.go @@ -13,9 +13,10 @@ network: - fe80::/10 - ff00::/8 -update: - cron: "0 0 4 * * *" - url: - geoip.dat: "https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geoip.dat" - geosite.dat: "https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geosite.dat" +asset: + update: + cron: "0 0 4 * * *" + url: + geoip.dat: "https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geoip.dat" + geosite.dat: "https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geosite.dat" ` diff --git a/cmd/config/main.go b/cmd/config/main.go index 8d448f3..449ed59 100644 --- a/cmd/config/main.go +++ b/cmd/config/main.go @@ -18,7 +18,7 @@ type Config struct { IPv4 network.Config IPv6 network.Config Proxy proxy.Config - Update asset.Config + Asset asset.Config Radvd radvd.Config Custom custom.Config } diff --git a/cmd/controller.go b/cmd/controller.go index a9cc7f5..792e05b 100644 --- a/cmd/controller.go +++ b/cmd/controller.go @@ -29,12 +29,20 @@ func blockWait() { } func loadRadvd(settings *config.Config) { - radvd.Load(&settings.Radvd) + if settings.Radvd.Enable { + radvd.Load(&settings.Radvd) + } else { + log.Infof("Skip loading radvd") + } } func loadAsset(settings *config.Config) { - asset.Load(assetFile, assetDir) - asset.AutoUpdate(&settings.Update, assetDir) + if settings.Asset.Disable { + log.Infof("Skip loading asset") + } else { + asset.Load(assetFile, assetDir) + asset.AutoUpdate(&settings.Asset, assetDir) + } } func loadNetwork(settings *config.Config) { @@ -72,5 +80,7 @@ func runRadvd(settings *config.Config) { radvdCmd = append(radvdCmd, "--debug", strconv.Itoa(settings.Radvd.Log)) } runProcess(nil, radvdCmd...) + } else { + log.Infof("Skip running radvd") } }