mirror of https://github.com/dnomd343/XProxy.git
dnomd343
2 years ago
2 changed files with 90 additions and 6 deletions
@ -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) |
||||
|
|
||||
|
} |
Loading…
Reference in new issue