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

22
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

13
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 {

5
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) {

Loading…
Cancel
Save