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
2025-06-18 15:50:30 +07:00
committed by Cuong Manh Le
parent 64632fa640
commit f0cb810dd6
8 changed files with 25 additions and 17 deletions

View File

@@ -9,7 +9,6 @@ import (
"strings"
"github.com/Control-D-Inc/ctrld"
"github.com/Control-D-Inc/ctrld/internal/resolvconffile"
)
// allocate loopback ip
@@ -92,7 +91,7 @@ func restoreDNS(iface *net.Interface) (err error) {
}
func currentDNS(_ *net.Interface) []string {
return resolvconffile.NameServers()
return ctrld.CurrentNameserversFromResolvconf()
}
// currentStaticDNS returns the current static DNS settings of given interface.

View File

@@ -9,8 +9,8 @@ import (
"tailscale.com/health"
"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/resolvconffile"
)
// allocate loopback ip
@@ -94,7 +94,7 @@ func restoreDNS(iface *net.Interface) (err error) {
}
func currentDNS(_ *net.Interface) []string {
return resolvconffile.NameServers()
return ctrld.CurrentNameserversFromResolvconf()
}
// currentStaticDNS returns the current static DNS settings of given interface.

View File

@@ -21,9 +21,9 @@ import (
"tailscale.com/health"
"tailscale.com/util/dnsname"
"github.com/Control-D-Inc/ctrld"
"github.com/Control-D-Inc/ctrld/internal/dns"
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"
@@ -201,7 +201,7 @@ func restoreDNS(iface *net.Interface) (err error) {
}
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} {
if ns := fn(iface.Name); len(ns) > 0 {
return ns

View File

@@ -9,6 +9,7 @@ import (
const resolvconfPath = "/etc/resolv.conf"
// NameServersWithPort retrieves a list of nameservers with the default DNS port 53 appended to each address.
func NameServersWithPort() []string {
c, err := resolvconffile.ParseFile(resolvconfPath)
if err != nil {
@@ -21,11 +22,17 @@ func NameServersWithPort() []string {
return ns
}
// NameServers retrieves a list of nameservers from the /etc/resolv.conf file
// Returns an empty slice if reading fails.
func NameServers() []string {
nss, _ := NameserversFromFile(resolvconfPath)
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) {
c, err := resolvconffile.ParseFile(path)
if err != nil {

View File

@@ -1,6 +1,10 @@
package ctrld
import "context"
import (
"context"
"github.com/Control-D-Inc/ctrld/internal/resolvconffile"
)
type dnsFn func(ctx context.Context) []string
@@ -28,3 +32,8 @@ func nameservers(ctx context.Context) []string {
return dns
}
// CurrentNameserversFromResolvconf returns the current nameservers set from /etc/resolv.conf file.
func CurrentNameserversFromResolvconf() []string {
return resolvconffile.NameServers()
}

View File

@@ -9,15 +9,8 @@ import (
"time"
"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.
// A nameserver is usable if it's not one of current machine's IP addresses
// and loopback IP addresses.
@@ -35,7 +28,7 @@ func dnsFromResolvConf(_ context.Context) []string {
time.Sleep(retryInterval)
}
nss := resolvconffile.NameServers()
nss := CurrentNameserversFromResolvconf()
var localDNS []string
seen := make(map[string]bool)

View File

@@ -297,7 +297,7 @@ func getDNSServers(ctx context.Context) ([]string, error) {
return ns, nil
}
// currentNameserversFromResolvconf returns a nil slice of strings.
// CurrentNameserversFromResolvconf returns a nil slice of strings.
func currentNameserversFromResolvconf() []string {
return nil
}

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.
func NewPrivateResolver(ctx context.Context) Resolver {
nss := initDefaultOsResolver(ctx)
resolveConfNss := currentNameserversFromResolvconf()
resolveConfNss := CurrentNameserversFromResolvconf()
localRfc1918Addrs := Rfc1918Addresses()
n := 0
for _, ns := range nss {