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.
This commit is contained in:
Cuong Manh Le
2026-04-30 19:19:18 +07:00
committed by Cuong Manh Le
parent 95699fa4a1
commit 29b8b4277c
8 changed files with 25 additions and 17 deletions
+1 -2
View File
@@ -9,7 +9,6 @@ import (
"strings" "strings"
"github.com/Control-D-Inc/ctrld" "github.com/Control-D-Inc/ctrld"
"github.com/Control-D-Inc/ctrld/internal/resolvconffile"
) )
// allocate loopback ip // allocate loopback ip
@@ -92,7 +91,7 @@ func restoreDNS(iface *net.Interface) (err error) {
} }
func currentDNS(_ *net.Interface) []string { func currentDNS(_ *net.Interface) []string {
return resolvconffile.NameServers() return ctrld.CurrentNameserversFromResolvconf()
} }
// currentStaticDNS returns the current static DNS settings of given interface. // currentStaticDNS returns the current static DNS settings of given interface.
+2 -2
View File
@@ -9,8 +9,8 @@ import (
"tailscale.com/health" "tailscale.com/health"
"tailscale.com/util/dnsname" "tailscale.com/util/dnsname"
"github.com/Control-D-Inc/ctrld"
"github.com/Control-D-Inc/ctrld/internal/dns" "github.com/Control-D-Inc/ctrld/internal/dns"
"github.com/Control-D-Inc/ctrld/internal/resolvconffile"
) )
// allocate loopback ip // allocate loopback ip
@@ -94,7 +94,7 @@ func restoreDNS(iface *net.Interface) (err error) {
} }
func currentDNS(_ *net.Interface) []string { func currentDNS(_ *net.Interface) []string {
return resolvconffile.NameServers() return ctrld.CurrentNameserversFromResolvconf()
} }
// currentStaticDNS returns the current static DNS settings of given interface. // currentStaticDNS returns the current static DNS settings of given interface.
+2 -2
View File
@@ -21,9 +21,9 @@ import (
"tailscale.com/health" "tailscale.com/health"
"tailscale.com/util/dnsname" "tailscale.com/util/dnsname"
"github.com/Control-D-Inc/ctrld"
"github.com/Control-D-Inc/ctrld/internal/dns" "github.com/Control-D-Inc/ctrld/internal/dns"
ctrldnet "github.com/Control-D-Inc/ctrld/internal/net" ctrldnet "github.com/Control-D-Inc/ctrld/internal/net"
"github.com/Control-D-Inc/ctrld/internal/resolvconffile"
) )
const resolvConfBackupFailedMsg = "open /etc/resolv.pre-ctrld-backup.conf: read-only file system" const resolvConfBackupFailedMsg = "open /etc/resolv.pre-ctrld-backup.conf: read-only file system"
@@ -201,7 +201,7 @@ func restoreDNS(iface *net.Interface) (err error) {
} }
func currentDNS(iface *net.Interface) []string { func currentDNS(iface *net.Interface) []string {
resolvconfFunc := func(_ string) []string { return resolvconffile.NameServers() } resolvconfFunc := func(_ string) []string { return ctrld.CurrentNameserversFromResolvconf() }
for _, fn := range []getDNS{getDNSByResolvectl, getDNSBySystemdResolved, getDNSByNmcli, resolvconfFunc} { for _, fn := range []getDNS{getDNSByResolvectl, getDNSBySystemdResolved, getDNSByNmcli, resolvconfFunc} {
if ns := fn(iface.Name); len(ns) > 0 { if ns := fn(iface.Name); len(ns) > 0 {
return ns return ns
+7
View File
@@ -9,6 +9,7 @@ import (
const resolvconfPath = "/etc/resolv.conf" const resolvconfPath = "/etc/resolv.conf"
// NameServersWithPort retrieves a list of nameservers with the default DNS port 53 appended to each address.
func NameServersWithPort() []string { func NameServersWithPort() []string {
c, err := resolvconffile.ParseFile(resolvconfPath) c, err := resolvconffile.ParseFile(resolvconfPath)
if err != nil { if err != nil {
@@ -21,11 +22,17 @@ func NameServersWithPort() []string {
return ns return ns
} }
// NameServers retrieves a list of nameservers from the /etc/resolv.conf file
// Returns an empty slice if reading fails.
func NameServers() []string { func NameServers() []string {
nss, _ := NameserversFromFile(resolvconfPath) nss, _ := NameserversFromFile(resolvconfPath)
return nss return nss
} }
// NameserversFromFile reads nameserver addresses from the specified resolv.conf file
// and returns them as a slice of strings.
//
// Returns an error if the file cannot be parsed.
func NameserversFromFile(path string) ([]string, error) { func NameserversFromFile(path string) ([]string, error) {
c, err := resolvconffile.ParseFile(path) c, err := resolvconffile.ParseFile(path)
if err != nil { if err != nil {
+10 -1
View File
@@ -1,6 +1,10 @@
package ctrld package ctrld
import "context" import (
"context"
"github.com/Control-D-Inc/ctrld/internal/resolvconffile"
)
type dnsFn func(ctx context.Context) []string type dnsFn func(ctx context.Context) []string
@@ -28,3 +32,8 @@ func nameservers(ctx context.Context) []string {
return dns return dns
} }
// CurrentNameserversFromResolvconf returns the current nameservers set from /etc/resolv.conf file.
func CurrentNameserversFromResolvconf() []string {
return resolvconffile.NameServers()
}
+1 -8
View File
@@ -9,15 +9,8 @@ import (
"time" "time"
"tailscale.com/net/netmon" "tailscale.com/net/netmon"
"github.com/Control-D-Inc/ctrld/internal/resolvconffile"
) )
// currentNameserversFromResolvconf returns the current nameservers set from /etc/resolv.conf file.
func currentNameserversFromResolvconf() []string {
return resolvconffile.NameServers()
}
// dnsFromResolvConf reads usable nameservers from /etc/resolv.conf file. // dnsFromResolvConf reads usable nameservers from /etc/resolv.conf file.
// A nameserver is usable if it's not one of current machine's IP addresses // A nameserver is usable if it's not one of current machine's IP addresses
// and loopback IP addresses. // and loopback IP addresses.
@@ -35,7 +28,7 @@ func dnsFromResolvConf(_ context.Context) []string {
time.Sleep(retryInterval) time.Sleep(retryInterval)
} }
nss := resolvconffile.NameServers() nss := CurrentNameserversFromResolvconf()
var localDNS []string var localDNS []string
seen := make(map[string]bool) seen := make(map[string]bool)
+1 -1
View File
@@ -297,7 +297,7 @@ func getDNSServers(ctx context.Context) ([]string, error) {
return ns, nil return ns, nil
} }
// currentNameserversFromResolvconf returns a nil slice of strings. // CurrentNameserversFromResolvconf returns a nil slice of strings.
func currentNameserversFromResolvconf() []string { func currentNameserversFromResolvconf() []string {
return nil return nil
} }
+1 -1
View File
@@ -647,7 +647,7 @@ func lookupIP(ctx context.Context, domain string, timeout int, bootstrapDNS []st
// This is useful for doing PTR lookup in LAN network. // This is useful for doing PTR lookup in LAN network.
func NewPrivateResolver(ctx context.Context) Resolver { func NewPrivateResolver(ctx context.Context) Resolver {
nss := initDefaultOsResolver(ctx) nss := initDefaultOsResolver(ctx)
resolveConfNss := currentNameserversFromResolvconf() resolveConfNss := CurrentNameserversFromResolvconf()
localRfc1918Addrs := Rfc1918Addresses() localRfc1918Addrs := Rfc1918Addresses()
n := 0 n := 0
for _, ns := range nss { for _, ns := range nss {