Files
ctrld/nameservers.go
Cuong Manh Le f0cb810dd6 all: move nameserver resolution to public API
Make nameserver resolution functions more consistent and accessible:
- Rename currentNameserversFromResolvconf to CurrentNameserversFromResolvconf
- Move function to public API for better reusability
- Update all internal references to use the new public API
- Add comprehensive godoc comments for nameserver functions
- Improve code organization by centralizing DNS resolution logic

This change makes the nameserver resolution functionality more maintainable
and easier to use across different parts of the codebase.
2025-10-09 17:49:21 +07:00

40 lines
761 B
Go

package ctrld
import (
"context"
"github.com/Control-D-Inc/ctrld/internal/resolvconffile"
)
type dnsFn func(ctx context.Context) []string
// nameservers returns DNS nameservers from system settings.
func nameservers(ctx context.Context) []string {
var dns []string
seen := make(map[string]bool)
ch := make(chan []string)
fns := dnsFns()
for _, fn := range fns {
go func(fn dnsFn) {
ch <- fn(ctx)
}(fn)
}
for range fns {
for _, ns := range <-ch {
if seen[ns] {
continue
}
seen[ns] = true
dns = append(dns, ns)
}
}
return dns
}
// CurrentNameserversFromResolvconf returns the current nameservers set from /etc/resolv.conf file.
func CurrentNameserversFromResolvconf() []string {
return resolvconffile.NameServers()
}