platform: modularize api/gui, add docs-tests-web foundation, and refresh root config
This commit is contained in:
69
selective-vpn-api/app/dnscfg/upstreams.go
Normal file
69
selective-vpn-api/app/dnscfg/upstreams.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package dnscfg
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func NormalizeUpstreams(cfg Upstreams, defaults Upstreams, normalizeUpstream func(raw string, defaultPort string) string) Upstreams {
|
||||
if normalizeUpstream != nil {
|
||||
cfg.Default1 = normalizeUpstream(cfg.Default1, "53")
|
||||
cfg.Default2 = normalizeUpstream(cfg.Default2, "53")
|
||||
cfg.Meta1 = normalizeUpstream(cfg.Meta1, "53")
|
||||
cfg.Meta2 = normalizeUpstream(cfg.Meta2, "53")
|
||||
}
|
||||
|
||||
if strings.TrimSpace(cfg.Default1) == "" {
|
||||
cfg.Default1 = defaults.Default1
|
||||
}
|
||||
if strings.TrimSpace(cfg.Default2) == "" {
|
||||
cfg.Default2 = defaults.Default2
|
||||
}
|
||||
if strings.TrimSpace(cfg.Meta1) == "" {
|
||||
cfg.Meta1 = defaults.Meta1
|
||||
}
|
||||
if strings.TrimSpace(cfg.Meta2) == "" {
|
||||
cfg.Meta2 = defaults.Meta2
|
||||
}
|
||||
return cfg
|
||||
}
|
||||
|
||||
func ParseUpstreamsConf(content string, defaults Upstreams, normalizeUpstream func(raw string, defaultPort string) string) Upstreams {
|
||||
cfg := defaults
|
||||
for _, ln := range strings.Split(content, "\n") {
|
||||
s := strings.TrimSpace(ln)
|
||||
if s == "" || strings.HasPrefix(s, "#") {
|
||||
continue
|
||||
}
|
||||
parts := strings.Fields(s)
|
||||
if len(parts) < 2 {
|
||||
continue
|
||||
}
|
||||
key := strings.ToLower(strings.TrimSpace(parts[0]))
|
||||
vals := parts[1:]
|
||||
switch key {
|
||||
case "default":
|
||||
if len(vals) > 0 {
|
||||
cfg.Default1 = vals[0]
|
||||
}
|
||||
if len(vals) > 1 {
|
||||
cfg.Default2 = vals[1]
|
||||
}
|
||||
case "meta":
|
||||
if len(vals) > 0 {
|
||||
cfg.Meta1 = vals[0]
|
||||
}
|
||||
if len(vals) > 1 {
|
||||
cfg.Meta2 = vals[1]
|
||||
}
|
||||
}
|
||||
}
|
||||
return NormalizeUpstreams(cfg, defaults, normalizeUpstream)
|
||||
}
|
||||
|
||||
func RenderUpstreamsConf(cfg Upstreams) string {
|
||||
return fmt.Sprintf(
|
||||
"default %s %s\nmeta %s %s\n",
|
||||
cfg.Default1, cfg.Default2, cfg.Meta1, cfg.Meta2,
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user