Browse Source

feat: add default config for xray

v1.x.x
dnomd343 2 years ago
parent
commit
3d75d9edc6
  1. 82
      src/load.go
  2. 14
      src/main.go

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

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

Loading…
Cancel
Save