From 3d75d9edc687e226e6b3d440bb2fa42dfe96a9ab Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Mon, 15 Aug 2022 16:25:44 +0800 Subject: [PATCH] feat: add default config for xray --- src/load.go | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.go | 14 +++++---- 2 files changed, 90 insertions(+), 6 deletions(-) create mode 100644 src/load.go diff --git a/src/load.go b/src/load.go new file mode 100644 index 0000000..03b0981 --- /dev/null +++ b/src/load.go @@ -0,0 +1,82 @@ +package main + +import ( + log "github.com/sirupsen/logrus" + "os" + "strings" +) + +var logConfig = `{ + "log": { + "loglevel": "${LEVEL}", + "access": "${DIR}/access.log", + "error": "${DIR}/error.log" + } +}` + +var dnsConfig = `{ + "dns": { + "servers": [ + "localhost" + ] + } +}` + +var routeConfig = `{ + "routing": { + "domainStrategy": "AsIs", + "rules": [ + { + "type": "field", + "network": "tcp,udp", + "outboundTag": "node" + } + ] + } +}` + +var outboundsConfig = `{ + "outbounds": [ + { + "tag": "node", + "protocol": "freedom", + "settings": {} + } + ] +}` + +func isFileExist(filePath string) bool { + s, err := os.Stat(filePath) + if err != nil { // file or folder not exist + return false + } + return !s.IsDir() +} + +func saveConfig(configDir string, caption string, content string, overwrite bool) { + filePath := configDir + "/" + caption + ".json" + if !overwrite && isFileExist(filePath) { // file exist and don't overwrite + log.Debugf("Skip loading config -> %s", filePath) + return + } + log.Debugf("Loading %s -> \n%s", filePath, content) + err := os.WriteFile(filePath, []byte(content), 0644) + if err != nil { + log.Errorf("File %s -> %v", caption, err) + panic("File save error") + } +} + +func proxyConfig(configDir string, logLevel string, logDir string) { + // TODO: mkdir -p configDir and exposeDir + + logConfig = strings.ReplaceAll(logConfig, "${LEVEL}", logLevel) + logConfig = strings.ReplaceAll(logConfig, "${DIR}", logDir) + // 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) + +} diff --git a/src/main.go b/src/main.go index fd539c8..fdfbecd 100644 --- a/src/main.go +++ b/src/main.go @@ -2,15 +2,17 @@ package main import ( log "github.com/sirupsen/logrus" - "os" ) func main() { log.SetLevel(log.DebugLevel) log.Warning("XProxy start") - content, err := os.ReadFile("test.yml") - if err != nil { - panic(err) - } - loadConfig(content) + + proxyConfig("/etc/xproxy/config", "debug", "/xproxy/log") + + //content, err := os.ReadFile("test.yml") + //if err != nil { + // panic(err) + //} + //loadConfig(content) }