platform: modularize api/gui, add docs-tests-web foundation, and refresh root config
This commit is contained in:
57
selective-vpn-api/app/resolver/dns_upstreams.go
Normal file
57
selective-vpn-api/app/resolver/dns_upstreams.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package resolver
|
||||
|
||||
import "strings"
|
||||
|
||||
func BuildResolverFallbackPool(raw string, fallbackDefaults []string, normalize func(string) string) []string {
|
||||
switch strings.ToLower(strings.TrimSpace(raw)) {
|
||||
case "off", "none", "0":
|
||||
return nil
|
||||
}
|
||||
|
||||
candidates := fallbackDefaults
|
||||
if strings.TrimSpace(raw) != "" {
|
||||
candidates = nil
|
||||
fields := strings.FieldsFunc(raw, func(r rune) bool {
|
||||
return r == ',' || r == ';' || r == ' ' || r == '\n' || r == '\t'
|
||||
})
|
||||
for _, f := range fields {
|
||||
if normalize == nil {
|
||||
continue
|
||||
}
|
||||
if n := normalize(f); n != "" {
|
||||
candidates = append(candidates, n)
|
||||
}
|
||||
}
|
||||
}
|
||||
return UniqueStrings(candidates)
|
||||
}
|
||||
|
||||
func MergeDNSUpstreamPools(primary, fallback []string, maxUpstreams int, normalize func(string) string) []string {
|
||||
if maxUpstreams < 1 {
|
||||
maxUpstreams = 1
|
||||
}
|
||||
out := make([]string, 0, len(primary)+len(fallback))
|
||||
seen := map[string]struct{}{}
|
||||
add := func(items []string) {
|
||||
for _, item := range items {
|
||||
if len(out) >= maxUpstreams {
|
||||
return
|
||||
}
|
||||
if normalize == nil {
|
||||
continue
|
||||
}
|
||||
n := normalize(item)
|
||||
if n == "" {
|
||||
continue
|
||||
}
|
||||
if _, ok := seen[n]; ok {
|
||||
continue
|
||||
}
|
||||
seen[n] = struct{}{}
|
||||
out = append(out, n)
|
||||
}
|
||||
}
|
||||
add(primary)
|
||||
add(fallback)
|
||||
return out
|
||||
}
|
||||
Reference in New Issue
Block a user