Browse Source

update: auto create folder

v1.x.x
dnomd343 2 years ago
parent
commit
6631cf3faa
  1. 27
      src/load.go
  2. 4
      src/main.go

27
src/load.go

@ -53,6 +53,15 @@ func isFileExist(filePath string) bool {
return !s.IsDir() 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) { func saveConfig(configDir string, caption string, content string, overwrite bool) {
filePath := configDir + "/" + caption + ".json" filePath := configDir + "/" + caption + ".json"
if !overwrite && isFileExist(filePath) { // file exist and don't overwrite 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) { func loadProxy(configDir string, exposeDir string) {
// TODO: mkdir -p configDir and exposeDir 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, "${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 // TODO: load inbounds config
saveConfig(configDir, "log", logConfig+"\n", true) // TODO: copy exposeDir/config/*.json -> configDir (exclude log and inbounds)
saveConfig(configDir, "dns", dnsConfig+"\n", false)
saveConfig(configDir, "route", routeConfig+"\n", false)
saveConfig(configDir, "outbounds", outboundsConfig+"\n", false)
} }

4
src/main.go

@ -4,11 +4,13 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
var logLevel = "debug"
func main() { func main() {
log.SetLevel(log.DebugLevel) log.SetLevel(log.DebugLevel)
log.Warning("XProxy start") log.Warning("XProxy start")
proxyConfig("/etc/xproxy/config", "debug", "/xproxy/log") loadProxy("/etc/xproxy/config", "/xproxy")
//content, err := os.ReadFile("test.yml") //content, err := os.ReadFile("test.yml")
//if err != nil { //if err != nil {

Loading…
Cancel
Save