Browse Source

refactor: asset update options

v1.x.x
dnomd343 2 years ago
parent
commit
06d5db95f9
  1. 21
      cmd/asset/asset.go
  2. 25
      cmd/asset/geofile.go
  3. 13
      cmd/asset/update.go
  4. 18
      cmd/config/decode.go
  5. 7
      cmd/config/main.go
  6. 5
      cmd/controller.go

21
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)
}

25
cmd/asset/geofile.go

@ -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)
}

13
cmd/asset/update.go

@ -6,7 +6,12 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
func updateAssets(urls map[string]string, assetDir string) { type Config struct {
Cron string `yaml:"cron" json:"cron"`
Url map[string]string `yaml:"url" json:"url"`
}
func updateAsset(urls map[string]string, assetDir string) {
if len(urls) != 0 { if len(urls) != 0 {
log.Info("Start update assets") log.Info("Start update assets")
for file, url := range urls { for file, url := range urls {
@ -15,10 +20,10 @@ func updateAssets(urls map[string]string, assetDir string) {
} }
} }
func AutoUpdate(updateCron string, updateUrls map[string]string, assetDir string) { func AutoUpdate(update *Config, assetDir string) {
autoUpdate := cron.New() autoUpdate := cron.New()
_ = autoUpdate.AddFunc(updateCron, func() { _ = autoUpdate.AddFunc(update.Cron, func() {
updateAssets(updateUrls, assetDir) updateAsset(update.Url, assetDir)
}) })
autoUpdate.Start() autoUpdate.Start()
} }

18
cmd/config/decode.go

@ -1,6 +1,7 @@
package config package config
import ( import (
"XProxy/cmd/asset"
"XProxy/cmd/common" "XProxy/cmd/common"
"XProxy/cmd/radvd" "XProxy/cmd/radvd"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@ -14,10 +15,7 @@ type yamlNetConfig struct {
type yamlConfig struct { type yamlConfig struct {
Custom []string `yaml:"custom"` Custom []string `yaml:"custom"`
Update struct { Update asset.Config `yaml:"update"`
Cron string `yaml:"cron"`
Url map[string]string `yaml:"url"`
} `yaml:"update"`
Proxy struct { Proxy struct {
Log string `yaml:"log"` Log string `yaml:"log"`
Sniff struct { Sniff struct {
@ -129,12 +127,10 @@ func decodeRadvd(rawConfig *yamlConfig, config *Config) {
log.Debugf("Radvd DNSSL -> %v", config.Radvd.DNSSL) log.Debugf("Radvd DNSSL -> %v", config.Radvd.DNSSL)
} }
func decodeUpdate(rawConfig *yamlConfig) (string, map[string]string) { func decodeUpdate(rawConfig *yamlConfig, config *Config) {
updateCron := rawConfig.Update.Cron config.Update = rawConfig.Update
log.Debugf("Update cron -> %s", updateCron) log.Debugf("Update cron -> %s", config.Update.Cron)
updateUrls := rawConfig.Update.Url log.Debugf("Update urls -> %v", config.Update.Url)
log.Debugf("Update urls -> %v", updateUrls)
return updateCron, updateUrls
} }
func decodeCustom(rawConfig *yamlConfig) []string { func decodeCustom(rawConfig *yamlConfig) []string {
@ -151,7 +147,7 @@ func decode(rawConfig yamlConfig) Config {
config.V4Address, config.V4Gateway = decodeIPv4(&rawConfig) config.V4Address, config.V4Gateway = decodeIPv4(&rawConfig)
config.V6Address, config.V6Gateway = decodeIPv6(&rawConfig) config.V6Address, config.V6Gateway = decodeIPv6(&rawConfig)
decodeProxy(&rawConfig, &config) decodeProxy(&rawConfig, &config)
config.UpdateCron, config.UpdateUrls = decodeUpdate(&rawConfig) decodeUpdate(&rawConfig, &config)
config.Script = decodeCustom(&rawConfig) config.Script = decodeCustom(&rawConfig)
decodeRadvd(&rawConfig, &config) decodeRadvd(&rawConfig, &config)
return config return config

7
cmd/config/main.go

@ -1,6 +1,7 @@
package config package config
import ( import (
"XProxy/cmd/asset"
"XProxy/cmd/common" "XProxy/cmd/common"
"XProxy/cmd/radvd" "XProxy/cmd/radvd"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@ -18,8 +19,6 @@ type Config struct {
Script []string Script []string
LogLevel string LogLevel string
UpdateCron string
UpdateUrls map[string]string
EnableSniff bool EnableSniff bool
EnableRedirect bool EnableRedirect bool
@ -28,10 +27,8 @@ type Config struct {
SocksInbounds map[string]int SocksInbounds map[string]int
AddOnInbounds []interface{} AddOnInbounds []interface{}
Update asset.Config
Radvd radvd.Config Radvd radvd.Config
//RadvdEnable bool
//RadvdOptions map[string]string
//RadvdPrefix map[string]map[string]string
} }
func Load(configFile string) Config { func Load(configFile string) Config {

5
cmd/controller.go

@ -10,9 +10,8 @@ import (
) )
func loadAsset(settings *config.Config) { func loadAsset(settings *config.Config) {
asset.LoadGeoSite(assetFile, assetDir) asset.Load(assetFile, assetDir)
asset.LoadGeoIp(assetFile, assetDir) asset.AutoUpdate(&settings.Update, assetDir)
asset.AutoUpdate(settings.UpdateCron, settings.UpdateUrls, assetDir)
} }
func loadProxy(settings *config.Config) { func loadProxy(settings *config.Config) {

Loading…
Cancel
Save