Browse Source

update: default config struct

v1.x.x
Dnomd343 2 years ago
parent
commit
2b9225be33
  1. 73
      cmd/config/default.go

73
cmd/config/default.go

@ -10,41 +10,45 @@ import (
"path"
)
var defaultConfig = `# default configure file for xproxy
proxy:
core: xray
log: warning
network:
bypass:
- 169.254.0.0/16
- 224.0.0.0/3
- fc00::/7
- fe80::/10
- ff00::/8
asset:
update:
cron: "0 5 6 * * *"
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
`
var defaultConfig = map[string]interface{}{
"proxy": map[string]string{
"core": "xray",
"log": "warning",
},
"network": map[string]interface{}{
"bypass": []string{
"169.254.0.0/16",
"224.0.0.0/3",
"fc00::/7",
"fe80::/10",
"ff00::/8",
},
},
"asset": map[string]interface{}{
"update": map[string]interface{}{
"cron": "0 5 6 * * *",
"url": map[string]string{
"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 toJSON(yamlConfig string) string { // YAML -> JSON
var config interface{}
if err := yaml.Unmarshal([]byte(yamlConfig), &config); err != nil {
log.Panicf("Default config error -> %v", err)
}
jsonRaw, _ := json.Marshal(config)
func toJSON(config interface{}) string { // convert to JSON string
jsonRaw, _ := json.MarshalIndent(config, "", " ")
return string(jsonRaw)
}
func toTOML(yamlConfig string) string { // YAML -> TOML
var config interface{}
if err := yaml.Unmarshal([]byte(yamlConfig), &config); err != nil {
log.Panicf("Default config error -> %v", err)
}
func toYAML(config interface{}) string { // convert to YAML string
buf := new(bytes.Buffer)
encoder := yaml.NewEncoder(buf)
encoder.SetIndent(2) // with 2 space indent
_ = encoder.Encode(config)
return buf.String()
}
func toTOML(config interface{}) string { // convert to TOML string
buf := new(bytes.Buffer)
_ = toml.NewEncoder(buf).Encode(config)
return buf.String()
@ -54,9 +58,10 @@ func loadDefaultConfig(configFile string) {
log.Infof("Load default configure -> %s", configFile)
suffix := path.Ext(configFile)
if suffix == ".json" {
defaultConfig = toJSON(defaultConfig)
common.WriteFile(configFile, toJSON(defaultConfig), false) // JSON format
} else if suffix == ".toml" {
defaultConfig = toTOML(defaultConfig)
common.WriteFile(configFile, toTOML(defaultConfig), false) // TOML format
} else {
common.WriteFile(configFile, toYAML(defaultConfig), false) // YAML format
}
common.WriteFile(configFile, defaultConfig, false)
}

Loading…
Cancel
Save