platform: modularize api/gui, add docs-tests-web foundation, and refresh root config
This commit is contained in:
100
selective-vpn-api/app/dnscfg/pool.go
Normal file
100
selective-vpn-api/app/dnscfg/pool.go
Normal file
@@ -0,0 +1,100 @@
|
||||
package dnscfg
|
||||
|
||||
import "strings"
|
||||
|
||||
type Upstreams struct {
|
||||
Default1 string
|
||||
Default2 string
|
||||
Meta1 string
|
||||
Meta2 string
|
||||
}
|
||||
|
||||
type UpstreamPoolItem struct {
|
||||
Addr string
|
||||
Enabled bool
|
||||
}
|
||||
|
||||
func NormalizeUpstreamPoolItems(items []UpstreamPoolItem, normalizeUpstream func(raw string, defaultPort string) string) []UpstreamPoolItem {
|
||||
if len(items) == 0 {
|
||||
return nil
|
||||
}
|
||||
seen := map[string]struct{}{}
|
||||
out := make([]UpstreamPoolItem, 0, len(items))
|
||||
for _, item := range items {
|
||||
addr := strings.TrimSpace(item.Addr)
|
||||
if normalizeUpstream != nil {
|
||||
addr = normalizeUpstream(addr, "53")
|
||||
}
|
||||
if addr == "" {
|
||||
continue
|
||||
}
|
||||
if _, ok := seen[addr]; ok {
|
||||
continue
|
||||
}
|
||||
seen[addr] = struct{}{}
|
||||
out = append(out, UpstreamPoolItem{
|
||||
Addr: addr,
|
||||
Enabled: item.Enabled,
|
||||
})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func UpstreamPoolFromLegacy(cfg Upstreams, normalizeUpstream func(raw string, defaultPort string) string) []UpstreamPoolItem {
|
||||
out := []UpstreamPoolItem{
|
||||
{Addr: cfg.Default1, Enabled: true},
|
||||
{Addr: cfg.Default2, Enabled: true},
|
||||
{Addr: cfg.Meta1, Enabled: true},
|
||||
{Addr: cfg.Meta2, Enabled: true},
|
||||
}
|
||||
return NormalizeUpstreamPoolItems(out, normalizeUpstream)
|
||||
}
|
||||
|
||||
func UpstreamPoolToLegacy(items []UpstreamPoolItem, defaults Upstreams, normalizeUpstream func(raw string, defaultPort string) string) Upstreams {
|
||||
items = NormalizeUpstreamPoolItems(items, normalizeUpstream)
|
||||
out := defaults
|
||||
enabled := make([]string, 0, len(items))
|
||||
for _, item := range items {
|
||||
if !item.Enabled {
|
||||
continue
|
||||
}
|
||||
addr := strings.TrimSpace(item.Addr)
|
||||
if normalizeUpstream != nil {
|
||||
addr = normalizeUpstream(addr, "53")
|
||||
}
|
||||
if addr != "" {
|
||||
enabled = append(enabled, addr)
|
||||
}
|
||||
}
|
||||
if len(enabled) > 0 {
|
||||
out.Default1 = enabled[0]
|
||||
}
|
||||
if len(enabled) > 1 {
|
||||
out.Default2 = enabled[1]
|
||||
}
|
||||
if len(enabled) > 2 {
|
||||
out.Meta1 = enabled[2]
|
||||
}
|
||||
if len(enabled) > 3 {
|
||||
out.Meta2 = enabled[3]
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func EnabledPool(items []UpstreamPoolItem, normalizeUpstream func(raw string, defaultPort string) string) []string {
|
||||
items = NormalizeUpstreamPoolItems(items, normalizeUpstream)
|
||||
out := make([]string, 0, len(items))
|
||||
for _, item := range items {
|
||||
if !item.Enabled {
|
||||
continue
|
||||
}
|
||||
addr := strings.TrimSpace(item.Addr)
|
||||
if normalizeUpstream != nil {
|
||||
addr = normalizeUpstream(addr, "53")
|
||||
}
|
||||
if addr != "" {
|
||||
out = append(out, addr)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
Reference in New Issue
Block a user