From 979a9b971c51005493eb05d92e8d10f1167099ff Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Sat, 20 Aug 2022 00:41:57 +0800 Subject: [PATCH] update: merge path with `path.Join` --- cmd/asset/asset.go | 8 +++++--- cmd/asset/update.go | 9 +++++---- cmd/config/decode.go | 4 ++-- cmd/config/default.go | 1 + cmd/controller.go | 3 ++- cmd/process/exit.go | 4 ---- cmd/proxy/config.go | 5 +++-- cmd/proxy/main.go | 19 ++++++++++--------- cmd/xproxy.go | 12 +++++++----- 9 files changed, 35 insertions(+), 30 deletions(-) diff --git a/cmd/asset/asset.go b/cmd/asset/asset.go index 0d4ec82..a2f1dd1 100644 --- a/cmd/asset/asset.go +++ b/cmd/asset/asset.go @@ -3,14 +3,16 @@ package asset import ( "XProxy/cmd/common" log "github.com/sirupsen/logrus" + "path" ) -func extractFile(archive string, geoFile string, targetDir string) { - if common.IsFileExist(targetDir + "/" + geoFile) { +func extractFile(archive string, geoFile string, targetDir string) { // extract `.dat` file into targetDir + filePath := path.Join(targetDir, geoFile) + if common.IsFileExist(filePath) { log.Debugf("Asset %s exist -> skip extract", geoFile) return } - log.Infof("Extract asset file -> %s", targetDir+"/"+geoFile) + log.Infof("Extract asset file -> %s", filePath) common.RunCommand("tar", "xvf", archive, "./"+geoFile, "-C", targetDir) } diff --git a/cmd/asset/update.go b/cmd/asset/update.go index 3526874..50d10a4 100644 --- a/cmd/asset/update.go +++ b/cmd/asset/update.go @@ -4,6 +4,7 @@ import ( "XProxy/cmd/common" "github.com/robfig/cron" log "github.com/sirupsen/logrus" + "path" ) type Config struct { @@ -11,18 +12,18 @@ type Config struct { Url map[string]string `yaml:"url" json:"url"` } -func updateAsset(urls map[string]string, assetDir string) { +func updateAsset(urls map[string]string, assetDir string) { // download new assets if len(urls) != 0 { log.Info("Start update assets") for file, url := range urls { - common.DownloadFile(url, assetDir+"/"+file) + common.DownloadFile(url, path.Join(assetDir, file)) // maybe override old asset } } } -func AutoUpdate(update *Config, assetDir string) { +func AutoUpdate(update *Config, assetDir string) { // set cron task for auto update autoUpdate := cron.New() - _ = autoUpdate.AddFunc(update.Cron, func() { + _ = autoUpdate.AddFunc(update.Cron, func() { // cron function updateAsset(update.Url, assetDir) }) autoUpdate.Start() diff --git a/cmd/config/decode.go b/cmd/config/decode.go index ce3654d..e8318ce 100644 --- a/cmd/config/decode.go +++ b/cmd/config/decode.go @@ -29,11 +29,11 @@ type yamlConfig struct { func yamlDecode(raw []byte) yamlConfig { var config yamlConfig - log.Debugf("Decode yaml content -> \n%s", string(raw)) + log.Debugf("Config raw content -> \n%s", string(raw)) if err := yaml.Unmarshal(raw, &config); err != nil { // yaml (or json) decode log.Panicf("Decode config file error -> %v", err) } - log.Debugf("Decoded config -> %v", config) + log.Debugf("Decoded configure -> %v", config) return config } diff --git a/cmd/config/default.go b/cmd/config/default.go index f1ecad9..eca72c0 100644 --- a/cmd/config/default.go +++ b/cmd/config/default.go @@ -2,6 +2,7 @@ package config var defaultConfig = `# default configure file for xproxy proxy: + core: xray log: warning network: diff --git a/cmd/controller.go b/cmd/controller.go index 7bbdd58..a5685f9 100644 --- a/cmd/controller.go +++ b/cmd/controller.go @@ -11,6 +11,7 @@ import ( log "github.com/sirupsen/logrus" "os" "os/signal" + "path" "syscall" ) @@ -71,6 +72,6 @@ func runProxy(settings *config.Config) { func runRadvd(settings *config.Config) { if settings.Radvd.Enable { - runProcess("radvd", "-n", "-m", "logfile", "-l", exposeDir+"/log/radvd.log") + runProcess("radvd", "-n", "-m", "logfile", "-l", path.Join(exposeDir, "log/radvd.log")) } } diff --git a/cmd/process/exit.go b/cmd/process/exit.go index 1c2d9f7..3cbbd37 100644 --- a/cmd/process/exit.go +++ b/cmd/process/exit.go @@ -10,21 +10,17 @@ var exitFlag bool func Exit(subProcess ...*Process) { exitFlag = true // setting up exit flag -> exit daemon mode log.Warningf("Start exit process") - for _, sub := range subProcess { if sub.process != nil { log.Infof("Send kill signal to process %s", sub.name) sub.Signal(syscall.SIGTERM) } } - log.Info("Wait all sub process exit") for _, sub := range subProcess { if sub.process != nil { _ = sub.process.Wait() } } - log.Infof("Exit complete") - } diff --git a/cmd/proxy/config.go b/cmd/proxy/config.go index 05e49c8..b1b52e9 100644 --- a/cmd/proxy/config.go +++ b/cmd/proxy/config.go @@ -3,6 +3,7 @@ package proxy import ( "XProxy/cmd/common" log "github.com/sirupsen/logrus" + "path" ) var dnsConfig = `{ @@ -72,8 +73,8 @@ func loadLogConfig(logLevel string, logDir string) string { } logConfig := logObject{} logConfig.Log.Loglevel = logLevel - logConfig.Log.Access = logDir + "/access.log" - logConfig.Log.Error = logDir + "/error.log" + logConfig.Log.Access = path.Join(logDir, "access.log") + logConfig.Log.Error = path.Join(logDir, "error.log") return common.JsonEncode(logConfig) } diff --git a/cmd/proxy/main.go b/cmd/proxy/main.go index 6d448cc..5abc0b5 100644 --- a/cmd/proxy/main.go +++ b/cmd/proxy/main.go @@ -2,6 +2,7 @@ package proxy import ( "XProxy/cmd/common" + "path" ) type Config struct { @@ -20,7 +21,7 @@ type Config struct { } func saveConfig(configDir string, caption string, content string, overwrite bool) { - filePath := configDir + "/" + caption + ".json" + filePath := path.Join(configDir, caption+".json") common.WriteFile(filePath, content+"\n", overwrite) } @@ -52,15 +53,15 @@ func loadInbounds(config *Config) string { } func Load(configDir string, exposeDir string, config *Config) { - common.CreateFolder(exposeDir + "/log") - common.CreateFolder(exposeDir + "/config") + common.CreateFolder(path.Join(exposeDir, "log")) + common.CreateFolder(path.Join(exposeDir, "config")) common.CreateFolder(configDir) - saveConfig(exposeDir+"/config", "dns", dnsConfig, false) - saveConfig(exposeDir+"/config", "route", routeConfig, false) - saveConfig(exposeDir+"/config", "outbounds", outboundsConfig, false) + saveConfig(path.Join(exposeDir, "config"), "dns", dnsConfig, false) + saveConfig(path.Join(exposeDir, "config"), "route", routeConfig, false) + saveConfig(path.Join(exposeDir, "config"), "outbounds", outboundsConfig, false) saveConfig(configDir, "inbounds", loadInbounds(config), true) - saveConfig(configDir, "log", loadLogConfig(config.Log, exposeDir+"/log"), true) - for _, configFile := range common.ListFiles(exposeDir+"/config", ".json") { - common.CopyFile(exposeDir+"/config/"+configFile, configDir+"/"+configFile) + saveConfig(configDir, "log", loadLogConfig(config.Log, path.Join(exposeDir, "log")), true) + for _, configFile := range common.ListFiles(path.Join(exposeDir, "config"), ".json") { + common.CopyFile(path.Join(exposeDir, "config", configFile), path.Join(configDir, configFile)) } } diff --git a/cmd/xproxy.go b/cmd/xproxy.go index bfb4fb4..16c6a2b 100644 --- a/cmd/xproxy.go +++ b/cmd/xproxy.go @@ -1,25 +1,26 @@ package main import ( + "XProxy/cmd/common" "XProxy/cmd/config" "XProxy/cmd/process" log "github.com/sirupsen/logrus" "os" + "path" "strconv" ) -var version = "0.9.0" +var version = "0.9.1" var v4RouteTable = 100 var v6RouteTable = 106 var v4TProxyPort = 7288 var v6TProxyPort = 7289 - var configDir = "/etc/xproxy" var assetFile = "/assets.tar.xz" -var assetDir, exposeDir, configFile string var subProcess []*process.Process +var assetDir, exposeDir, configFile string func xproxyInit() { log.SetFormatter(&log.TextFormatter{ @@ -53,8 +54,9 @@ func xproxyInit() { if os.Getenv("EXPOSE_DIR") != "" { exposeDir = os.Getenv("EXPOSE_DIR") } - assetDir = exposeDir + "/assets" - configFile = exposeDir + "/config.yml" + common.CreateFolder(exposeDir) + assetDir = path.Join(exposeDir, "assets") + configFile = path.Join(exposeDir, "xproxy.yml") log.Debugf("Expose folder -> %s", exposeDir) log.Debugf("Assets folder -> %s", assetDir) log.Debugf("Config file -> %s", configFile)