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

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

Loading…
Cancel
Save