Browse Source

update: merge path with `path.Join`

v1.x.x
dnomd343 2 years ago
parent
commit
979a9b971c
  1. 8
      cmd/asset/asset.go
  2. 9
      cmd/asset/update.go
  3. 4
      cmd/config/decode.go
  4. 1
      cmd/config/default.go
  5. 3
      cmd/controller.go
  6. 4
      cmd/process/exit.go
  7. 5
      cmd/proxy/config.go
  8. 19
      cmd/proxy/main.go
  9. 12
      cmd/xproxy.go

8
cmd/asset/asset.go

@ -3,14 +3,16 @@ package asset
import ( import (
"XProxy/cmd/common" "XProxy/cmd/common"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"path"
) )
func extractFile(archive string, geoFile string, targetDir string) { func extractFile(archive string, geoFile string, targetDir string) { // extract `.dat` file into targetDir
if common.IsFileExist(targetDir + "/" + geoFile) { filePath := path.Join(targetDir, geoFile)
if common.IsFileExist(filePath) {
log.Debugf("Asset %s exist -> skip extract", geoFile) log.Debugf("Asset %s exist -> skip extract", geoFile)
return return
} }
log.Infof("Extract asset file -> %s", targetDir+"/"+geoFile) log.Infof("Extract asset file -> %s", filePath)
common.RunCommand("tar", "xvf", archive, "./"+geoFile, "-C", targetDir) common.RunCommand("tar", "xvf", archive, "./"+geoFile, "-C", targetDir)
} }

9
cmd/asset/update.go

@ -4,6 +4,7 @@ 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"
"path"
) )
type Config struct { type Config struct {
@ -11,18 +12,18 @@ type Config struct {
Url map[string]string `yaml:"url" json:"url"` 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 { if len(urls) != 0 {
log.Info("Start update assets") log.Info("Start update assets")
for file, url := range urls { 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 := cron.New()
_ = autoUpdate.AddFunc(update.Cron, func() { _ = autoUpdate.AddFunc(update.Cron, func() { // cron function
updateAsset(update.Url, assetDir) updateAsset(update.Url, assetDir)
}) })
autoUpdate.Start() autoUpdate.Start()

4
cmd/config/decode.go

@ -29,11 +29,11 @@ type yamlConfig struct {
func yamlDecode(raw []byte) yamlConfig { func yamlDecode(raw []byte) yamlConfig {
var config 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 if err := yaml.Unmarshal(raw, &config); err != nil { // yaml (or json) decode
log.Panicf("Decode config file error -> %v", err) log.Panicf("Decode config file error -> %v", err)
} }
log.Debugf("Decoded config -> %v", config) log.Debugf("Decoded configure -> %v", config)
return config return config
} }

1
cmd/config/default.go

@ -2,6 +2,7 @@ package config
var defaultConfig = `# default configure file for xproxy var defaultConfig = `# default configure file for xproxy
proxy: proxy:
core: xray
log: warning log: warning
network: network:

3
cmd/controller.go

@ -11,6 +11,7 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"os" "os"
"os/signal" "os/signal"
"path"
"syscall" "syscall"
) )
@ -71,6 +72,6 @@ func runProxy(settings *config.Config) {
func runRadvd(settings *config.Config) { func runRadvd(settings *config.Config) {
if settings.Radvd.Enable { 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"))
} }
} }

4
cmd/process/exit.go

@ -10,21 +10,17 @@ var exitFlag bool
func Exit(subProcess ...*Process) { func Exit(subProcess ...*Process) {
exitFlag = true // setting up exit flag -> exit daemon mode exitFlag = true // setting up exit flag -> exit daemon mode
log.Warningf("Start exit process") log.Warningf("Start exit process")
for _, sub := range subProcess { for _, sub := range subProcess {
if sub.process != nil { if sub.process != nil {
log.Infof("Send kill signal to process %s", sub.name) log.Infof("Send kill signal to process %s", sub.name)
sub.Signal(syscall.SIGTERM) sub.Signal(syscall.SIGTERM)
} }
} }
log.Info("Wait all sub process exit") log.Info("Wait all sub process exit")
for _, sub := range subProcess { for _, sub := range subProcess {
if sub.process != nil { if sub.process != nil {
_ = sub.process.Wait() _ = sub.process.Wait()
} }
} }
log.Infof("Exit complete") log.Infof("Exit complete")
} }

5
cmd/proxy/config.go

@ -3,6 +3,7 @@ package proxy
import ( import (
"XProxy/cmd/common" "XProxy/cmd/common"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"path"
) )
var dnsConfig = `{ var dnsConfig = `{
@ -72,8 +73,8 @@ func loadLogConfig(logLevel string, logDir string) string {
} }
logConfig := logObject{} logConfig := logObject{}
logConfig.Log.Loglevel = logLevel logConfig.Log.Loglevel = logLevel
logConfig.Log.Access = logDir + "/access.log" logConfig.Log.Access = path.Join(logDir, "access.log")
logConfig.Log.Error = logDir + "/error.log" logConfig.Log.Error = path.Join(logDir, "error.log")
return common.JsonEncode(logConfig) return common.JsonEncode(logConfig)
} }

19
cmd/proxy/main.go

@ -2,6 +2,7 @@ package proxy
import ( import (
"XProxy/cmd/common" "XProxy/cmd/common"
"path"
) )
type Config struct { type Config struct {
@ -20,7 +21,7 @@ type Config struct {
} }
func saveConfig(configDir string, caption string, content string, overwrite bool) { 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) common.WriteFile(filePath, content+"\n", overwrite)
} }
@ -52,15 +53,15 @@ func loadInbounds(config *Config) string {
} }
func Load(configDir string, exposeDir string, config *Config) { func Load(configDir string, exposeDir string, config *Config) {
common.CreateFolder(exposeDir + "/log") common.CreateFolder(path.Join(exposeDir, "log"))
common.CreateFolder(exposeDir + "/config") common.CreateFolder(path.Join(exposeDir, "config"))
common.CreateFolder(configDir) common.CreateFolder(configDir)
saveConfig(exposeDir+"/config", "dns", dnsConfig, false) saveConfig(path.Join(exposeDir, "config"), "dns", dnsConfig, false)
saveConfig(exposeDir+"/config", "route", routeConfig, false) saveConfig(path.Join(exposeDir, "config"), "route", routeConfig, false)
saveConfig(exposeDir+"/config", "outbounds", outboundsConfig, false) saveConfig(path.Join(exposeDir, "config"), "outbounds", outboundsConfig, false)
saveConfig(configDir, "inbounds", loadInbounds(config), true) saveConfig(configDir, "inbounds", loadInbounds(config), true)
saveConfig(configDir, "log", loadLogConfig(config.Log, exposeDir+"/log"), true) saveConfig(configDir, "log", loadLogConfig(config.Log, path.Join(exposeDir, "log")), true)
for _, configFile := range common.ListFiles(exposeDir+"/config", ".json") { for _, configFile := range common.ListFiles(path.Join(exposeDir, "config"), ".json") {
common.CopyFile(exposeDir+"/config/"+configFile, configDir+"/"+configFile) common.CopyFile(path.Join(exposeDir, "config", configFile), path.Join(configDir, configFile))
} }
} }

12
cmd/xproxy.go

@ -1,25 +1,26 @@
package main package main
import ( import (
"XProxy/cmd/common"
"XProxy/cmd/config" "XProxy/cmd/config"
"XProxy/cmd/process" "XProxy/cmd/process"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"os" "os"
"path"
"strconv" "strconv"
) )
var version = "0.9.0" var version = "0.9.1"
var v4RouteTable = 100 var v4RouteTable = 100
var v6RouteTable = 106 var v6RouteTable = 106
var v4TProxyPort = 7288 var v4TProxyPort = 7288
var v6TProxyPort = 7289 var v6TProxyPort = 7289
var configDir = "/etc/xproxy" var configDir = "/etc/xproxy"
var assetFile = "/assets.tar.xz" var assetFile = "/assets.tar.xz"
var assetDir, exposeDir, configFile string
var subProcess []*process.Process var subProcess []*process.Process
var assetDir, exposeDir, configFile string
func xproxyInit() { func xproxyInit() {
log.SetFormatter(&log.TextFormatter{ log.SetFormatter(&log.TextFormatter{
@ -53,8 +54,9 @@ func xproxyInit() {
if os.Getenv("EXPOSE_DIR") != "" { if os.Getenv("EXPOSE_DIR") != "" {
exposeDir = os.Getenv("EXPOSE_DIR") exposeDir = os.Getenv("EXPOSE_DIR")
} }
assetDir = exposeDir + "/assets" common.CreateFolder(exposeDir)
configFile = exposeDir + "/config.yml" assetDir = path.Join(exposeDir, "assets")
configFile = path.Join(exposeDir, "xproxy.yml")
log.Debugf("Expose folder -> %s", exposeDir) log.Debugf("Expose folder -> %s", exposeDir)
log.Debugf("Assets folder -> %s", assetDir) log.Debugf("Assets folder -> %s", assetDir)
log.Debugf("Config file -> %s", configFile) log.Debugf("Config file -> %s", configFile)

Loading…
Cancel
Save