From 628c4302aa4a140737265098c1f76ea24793c176 Mon Sep 17 00:00:00 2001 From: Cuong Manh Le Date: Wed, 4 Jun 2025 17:40:27 +0700 Subject: [PATCH] cmd/cli: preserve search domains when reverting resolv.conf Fixes search domains not being preserved when the resolv.conf file is reverted to its previous state. This ensures that important domain search configuration is maintained during DNS configuration changes. The search domains handling was missing in setResolvConf function, which is responsible for restoring DNS settings. --- cmd/cli/prog.go | 7 +++++++ cmd/cli/prog_linux.go | 5 +---- cmd/cli/resolvconf_not_darwin_unix.go | 16 +++++++++++++--- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/cmd/cli/prog.go b/cmd/cli/prog.go index 089bfd0..dd8de9f 100644 --- a/cmd/cli/prog.go +++ b/cmd/cli/prog.go @@ -70,10 +70,17 @@ func ControlSocketName() string { } } +// logf is a function variable used for logging formatted debug messages with optional arguments. +// This is used only when creating a new DNS OS configurator. var logf = func(format string, args ...any) { mainLog.Load().Debug().Msgf(format, args...) } +// noopLogf is like logf but discards formatted log messages and arguments without any processing. +// +//lint:ignore U1000 use in newLoopbackOSConfigurator +var noopLogf = func(format string, args ...any) {} + var svcConfig = &service.Config{ Name: ctrldServiceName, DisplayName: "Control-D Helper Service", diff --git a/cmd/cli/prog_linux.go b/cmd/cli/prog_linux.go index cc0046b..2e5c7c7 100644 --- a/cmd/cli/prog_linux.go +++ b/cmd/cli/prog_linux.go @@ -9,15 +9,12 @@ import ( "strings" "github.com/kardianos/service" - "tailscale.com/control/controlknobs" - "tailscale.com/health" - "github.com/Control-D-Inc/ctrld/internal/dns" "github.com/Control-D-Inc/ctrld/internal/router" ) func init() { - if r, err := dns.NewOSConfigurator(func(format string, args ...any) {}, &health.Tracker{}, &controlknobs.Knobs{}, "lo"); err == nil { + if r, err := newLoopbackOSConfigurator(); err == nil { useSystemdResolved = r.Mode() == "systemd-resolved" } // Disable quic-go's ECN support by default, see https://github.com/quic-go/quic-go/issues/3911 diff --git a/cmd/cli/resolvconf_not_darwin_unix.go b/cmd/cli/resolvconf_not_darwin_unix.go index 7181e95..af33572 100644 --- a/cmd/cli/resolvconf_not_darwin_unix.go +++ b/cmd/cli/resolvconf_not_darwin_unix.go @@ -13,9 +13,9 @@ import ( "github.com/Control-D-Inc/ctrld/internal/dns" ) -// setResolvConf sets the content of resolv.conf file using the given nameservers list. +// setResolvConf sets the content of the resolv.conf file using the given nameservers list. func setResolvConf(iface *net.Interface, ns []netip.Addr) error { - r, err := dns.NewOSConfigurator(func(format string, args ...any) {}, &health.Tracker{}, &controlknobs.Knobs{}, "lo") // interface name does not matter. + r, err := newLoopbackOSConfigurator() if err != nil { return err } @@ -24,12 +24,17 @@ func setResolvConf(iface *net.Interface, ns []netip.Addr) error { Nameservers: ns, SearchDomains: []dnsname.FQDN{}, } + if sds, err := searchDomains(); err == nil { + oc.SearchDomains = sds + } else { + mainLog.Load().Debug().Err(err).Msg("failed to get search domains list when reverting resolv.conf file") + } return r.SetDNS(oc) } // shouldWatchResolvconf reports whether ctrld should watch changes to resolv.conf file with given OS configurator. func shouldWatchResolvconf() bool { - r, err := dns.NewOSConfigurator(func(format string, args ...any) {}, &health.Tracker{}, &controlknobs.Knobs{}, "lo") // interface name does not matter. + r, err := newLoopbackOSConfigurator() if err != nil { return false } @@ -40,3 +45,8 @@ func shouldWatchResolvconf() bool { return false } } + +// newLoopbackOSConfigurator creates an OSConfigurator for DNS management using the "lo" interface. +func newLoopbackOSConfigurator() (dns.OSConfigurator, error) { + return dns.NewOSConfigurator(noopLogf, &health.Tracker{}, &controlknobs.Knobs{}, "lo") +}