Browse Source

feat: load default configure

v1.x.x
dnomd343 2 years ago
parent
commit
ffb84276b2
  1. 43
      src/config.go
  2. 6
      src/main.go
  3. 39
      test.yml

43
src/config.go

@ -4,6 +4,7 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"net" "net"
"os"
"strings" "strings"
) )
@ -41,6 +42,32 @@ type Config struct {
} `yaml:"network"` } `yaml:"network"`
} }
var defaultConfig = `# default configure file for xproxy
proxy:
sniff: true
redirect: true
http: null
socks: null
addon: null
network:
dns: null
ipv4: null
ipv6: null
bypass:
- 169.254.0.0/16
- 224.0.0.0/3
- fc00::/7
- fe80::/10
- ff00::/8
update:
cron: "0 0 4 * * *"
url:
geoip.dat: "https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geoip.dat"
geosite.dat: "https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geosite.dat"
`
func isIP(ipAddr string, isCidr bool) bool { func isIP(ipAddr string, isCidr bool) bool {
if !isCidr { if !isCidr {
return net.ParseIP(ipAddr) != nil return net.ParseIP(ipAddr) != nil
@ -57,10 +84,22 @@ func isIPv6(ipAddr string, isCidr bool) bool {
return isIP(ipAddr, isCidr) && strings.Contains(ipAddr, ":") return isIP(ipAddr, isCidr) && strings.Contains(ipAddr, ":")
} }
func loadConfig(rawConfig []byte) { func loadConfig(configFile string) {
if !isFileExist(configFile) { // load default configure
log.Infof("Load default configure -> %s", configFile)
err := os.WriteFile(configFile, []byte(defaultConfig), 0644)
if err != nil {
log.Panicf("File %s save error -> %v", configFile, err)
}
}
config := Config{} config := Config{}
rawConfig, err := os.ReadFile(configFile)
if err != nil {
log.Panicf("Failed to open %s -> %v", configFile, err)
}
log.Debugf("Decode yaml content -> \n%s", string(rawConfig)) log.Debugf("Decode yaml content -> \n%s", string(rawConfig))
err := yaml.Unmarshal(rawConfig, &config) // yaml (or json) decode err = yaml.Unmarshal(rawConfig, &config) // yaml (or json) decode
if err != nil { if err != nil {
log.Panicf("Decode config file error -> %v", err) log.Panicf("Decode config file error -> %v", err)
} }

6
src/main.go

@ -36,11 +36,7 @@ func main() {
log.SetLevel(log.DebugLevel) log.SetLevel(log.DebugLevel)
log.Warning("XProxy start") log.Warning("XProxy start")
content, err := os.ReadFile("test.yml") loadConfig("/xproxy/config.yml")
if err != nil {
panic(err)
}
loadConfig(content)
loadProxy("/etc/xproxy/config", "/xproxy") loadProxy("/etc/xproxy/config", "/xproxy")
loadGeoSite("/xproxy/assets") loadGeoSite("/xproxy/assets")

39
test.yml

@ -1,39 +0,0 @@
proxy:
sniff: true
redirect: true
http:
ipv4: 1084
ipv6: 1086
socks:
nodeA: 1681
nodeB: 1682
nodeC: 1683
addon:
- tag: metrics
port: 9090
protocol: dokodemo-door
settings:
address: 127.0.0.1
network:
dns:
- 223.5.5.5
- 119.29.29.29
ipv4:
gateway: 192.168.2.1
address: 192.168.2.2/24
ipv6:
gateway: null
address: fc00::2/64
bypass:
- 169.254.0.0/16
- 224.0.0.0/3
- fc00::/7
- fe80::/10
- ff00::/8
update:
cron: "0 */1 * * * *"
url:
geoip.dat: "https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geoip.dat"
geosite.dat: "https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geosite.dat"
Loading…
Cancel
Save