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. 37
      cmd/asset/update.go
  4. 22
      cmd/config/decode.go
  5. 13
      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)
}

37
cmd/asset/update.go

@ -1,24 +1,29 @@
package asset package asset
import ( import (
"XProxy/cmd/common" "XProxy/cmd/common"
"github.com/robfig/cron" "github.com/robfig/cron"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
func updateAssets(urls map[string]string, assetDir string) { type Config struct {
if len(urls) != 0 { Cron string `yaml:"cron" json:"cron"`
log.Info("Start update assets") Url map[string]string `yaml:"url" json:"url"`
for file, url := range urls {
common.DownloadFile(url, assetDir+"/"+file)
}
}
} }
func AutoUpdate(updateCron string, updateUrls map[string]string, assetDir string) { func updateAsset(urls map[string]string, assetDir string) {
autoUpdate := cron.New() if len(urls) != 0 {
_ = autoUpdate.AddFunc(updateCron, func() { log.Info("Start update assets")
updateAssets(updateUrls, assetDir) for file, url := range urls {
}) common.DownloadFile(url, assetDir+"/"+file)
autoUpdate.Start() }
}
}
func AutoUpdate(update *Config, assetDir string) {
autoUpdate := cron.New()
_ = autoUpdate.AddFunc(update.Cron, func() {
updateAsset(update.Url, assetDir)
})
autoUpdate.Start()
} }

22
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"
@ -13,12 +14,9 @@ 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"` Proxy struct {
Url map[string]string `yaml:"url"`
} `yaml:"update"`
Proxy struct {
Log string `yaml:"log"` Log string `yaml:"log"`
Sniff struct { Sniff struct {
Enable bool `yaml:"enable"` Enable bool `yaml:"enable"`
@ -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

13
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"
@ -16,10 +17,8 @@ type Config struct {
V6Address string V6Address string
V6Gateway string V6Gateway string
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{}
Radvd radvd.Config Update asset.Config
//RadvdEnable bool Radvd radvd.Config
//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