Refactor code structure for improved readability and maintainability
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
package resolver
|
||||
|
||||
import "dns-helper/platform"
|
||||
|
||||
// BuildResolverPool constructs the ordered list of DNS resolver addresses for
|
||||
// the given ServerMode and discovered NetworkInfo.
|
||||
//
|
||||
// Modes:
|
||||
// - "default" → local DNS servers + bootstrap resolvers (deduplicated, local first)
|
||||
// - "local" → only the local DNS servers from NetworkInfo.DNSServers
|
||||
// - "gateway" → only NetworkInfo.Gateway
|
||||
// - "explicit" → only ServerMode.ExplicitAddr
|
||||
//
|
||||
// Returns nil for unknown modes. Callers should validate that the pool is
|
||||
// non-empty before proceeding (e.g. gateway mode with no discovered gateway).
|
||||
func BuildResolverPool(mode ServerMode, info platform.NetworkInfo) []string {
|
||||
switch mode.Mode {
|
||||
case "default":
|
||||
seen := make(map[string]bool, len(info.DNSServers)+len(BootstrapResolvers))
|
||||
pool := make([]string, 0, len(info.DNSServers)+len(BootstrapResolvers))
|
||||
|
||||
for _, r := range info.DNSServers {
|
||||
if !seen[r] {
|
||||
seen[r] = true
|
||||
pool = append(pool, r)
|
||||
}
|
||||
}
|
||||
for _, r := range BootstrapResolvers {
|
||||
if !seen[r] {
|
||||
seen[r] = true
|
||||
pool = append(pool, r)
|
||||
}
|
||||
}
|
||||
return pool
|
||||
|
||||
case "local":
|
||||
return info.DNSServers
|
||||
|
||||
case "gateway":
|
||||
return []string{info.Gateway}
|
||||
|
||||
case "explicit":
|
||||
return []string{mode.ExplicitAddr}
|
||||
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user