Browse Source

update: default config struct

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

71
cmd/config/default.go

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