cmd/cli: save DNS settings only once

While at it, also fixing a bug in getting saved nameservers.
This commit is contained in:
Cuong Manh Le
2024-02-08 08:59:53 +07:00
committed by Cuong Manh Le
parent d55563cac5
commit 18e8616834
2 changed files with 9 additions and 3 deletions

View File

@@ -268,6 +268,14 @@ func initCLI() {
{s.Stop, false},
{func() error { return doGenerateNextDNSConfig(nextdns) }, true},
{func() error { return ensureUninstall(s) }, false},
{func() error {
// Save current DNS so we can restore later.
withEachPhysicalInterfaces("", "save DNS settings", func(i *net.Interface) error {
saveCurrentDNS(i)
return nil
})
return nil
}, false},
{s.Install, false},
{s.Start, true},
// Note that startCmd do not actually write ControlD config, but the config file was

View File

@@ -462,7 +462,6 @@ func (p *prog) setDNS() {
if needRFC1918Listeners(lc) {
nameservers = append(nameservers, ctrld.Rfc1918Addresses()...)
}
saveCurrentDNS(netIface)
if err := setDNS(netIface, nameservers); err != nil {
logger.Error().Err(err).Msgf("could not set DNS for interface")
return
@@ -470,7 +469,6 @@ func (p *prog) setDNS() {
logger.Debug().Msg("setting DNS successfully")
if allIfaces {
withEachPhysicalInterfaces(netIface.Name, "set DNS", func(i *net.Interface) error {
saveCurrentDNS(i)
return setDNS(i, nameservers)
})
}
@@ -753,7 +751,7 @@ func savedDnsSettingsFilePath(iface *net.Interface) string {
// savedNameservers returns the static DNS nameservers of the given interface.
func savedNameservers(iface *net.Interface) []string {
file := savedDnsSettingsFilePath(iface)
if data, err := os.ReadFile(file); err != nil && len(data) > 0 {
if data, _ := os.ReadFile(file); len(data) > 0 {
return strings.Split(string(data), ",")
}
return nil