From 6631cf3faa41f5a01b07955a05e70583973aa4f4 Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Mon, 15 Aug 2022 16:50:33 +0800 Subject: [PATCH] update: auto create folder --- src/load.go | 27 ++++++++++++++++++++------- src/main.go | 4 +++- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/load.go b/src/load.go index 03b0981..47e08f6 100644 --- a/src/load.go +++ b/src/load.go @@ -53,6 +53,15 @@ func isFileExist(filePath string) bool { return !s.IsDir() } +func createFolder(folderPath string) { + log.Debugf("Loading folder -> %s", folderPath) + err := os.MkdirAll(folderPath, 0755) + if err != nil { + log.Errorf("Create folder `%s` failed", folderPath) + panic("Create folder failed") + } +} + func saveConfig(configDir string, caption string, content string, overwrite bool) { filePath := configDir + "/" + caption + ".json" if !overwrite && isFileExist(filePath) { // file exist and don't overwrite @@ -67,16 +76,20 @@ func saveConfig(configDir string, caption string, content string, overwrite bool } } -func proxyConfig(configDir string, logLevel string, logDir string) { - // TODO: mkdir -p configDir and exposeDir +func loadProxy(configDir string, exposeDir string) { + createFolder(exposeDir + "/log") + createFolder(exposeDir + "/config") + createFolder(configDir) + saveConfig(exposeDir+"/config", "dns", dnsConfig+"\n", false) + saveConfig(exposeDir+"/config", "route", routeConfig+"\n", false) + saveConfig(exposeDir+"/config", "outbounds", outboundsConfig+"\n", false) logConfig = strings.ReplaceAll(logConfig, "${LEVEL}", logLevel) - logConfig = strings.ReplaceAll(logConfig, "${DIR}", logDir) + logConfig = strings.ReplaceAll(logConfig, "${DIR}", exposeDir+"/log") + saveConfig(configDir, "log", logConfig+"\n", true) + // TODO: load inbounds config - saveConfig(configDir, "log", logConfig+"\n", true) - saveConfig(configDir, "dns", dnsConfig+"\n", false) - saveConfig(configDir, "route", routeConfig+"\n", false) - saveConfig(configDir, "outbounds", outboundsConfig+"\n", false) + // TODO: copy exposeDir/config/*.json -> configDir (exclude log and inbounds) } diff --git a/src/main.go b/src/main.go index fdfbecd..16df17b 100644 --- a/src/main.go +++ b/src/main.go @@ -4,11 +4,13 @@ import ( log "github.com/sirupsen/logrus" ) +var logLevel = "debug" + func main() { log.SetLevel(log.DebugLevel) log.Warning("XProxy start") - proxyConfig("/etc/xproxy/config", "debug", "/xproxy/log") + loadProxy("/etc/xproxy/config", "/xproxy") //content, err := os.ReadFile("test.yml") //if err != nil {