58 lines
1.2 KiB
Go
58 lines
1.2 KiB
Go
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
|
|
}
|