cmd/cli: do not save static DNS when ctrld is already installed

If ctrld was installed, the DNS setting was changed, we could not
determine the dynamic or static settings before installing ctrld.
This commit is contained in:
Cuong Manh Le authored and Cuong Manh Le committed 2024-02-21 17:49:19 +07:00
1 parent dabbf2037b
commit 906479a15c
2 files changed
+19 -5

No files matched your search

+15 -2
View File
@@ -259,8 +259,11 @@ func initCLI() {
return return
} }
status, err := s.Status()
isCtrldInstalled := !errors.Is(err, service.ErrNotInstalled)
// If pin code was set, do not allow running start command. // If pin code was set, do not allow running start command.
if status, _ := s.Status(); status == service.StatusRunning { if status == service.StatusRunning {
if err := checkDeactivationPin(s); isCheckDeactivationPinErr(err) { if err := checkDeactivationPin(s); isCheckDeactivationPinErr(err) {
os.Exit(deactivationPinInvalidExitCode) os.Exit(deactivationPinInvalidExitCode)
} }
@@ -276,8 +279,18 @@ func initCLI() {
{func() error { return doGenerateNextDNSConfig(nextdns) }, true}, {func() error { return doGenerateNextDNSConfig(nextdns) }, true},
{func() error { return ensureUninstall(s) }, false}, {func() error { return ensureUninstall(s) }, false},
{func() error { {func() error {
// If ctrld is installed, we should not save current DNS settings, because:
//
// - The DNS settings was being set by ctrld already.
// - We could not determine the state of DNS settings before installing ctrld.
if isCtrldInstalled {
return nil
}
// Save current DNS so we can restore later. // Save current DNS so we can restore later.
withEachPhysicalInterfaces("", "save DNS settings", saveCurrentStaticDNS) withEachPhysicalInterfaces("", "save DNS settings", func(i *net.Interface) error {
return saveCurrentStaticDNS(i)
})
return nil return nil
}, false}, }, false},
{s.Install, false}, {s.Install, false},
+4 -3
View File
@@ -5,6 +5,7 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"io/fs"
"math/rand" "math/rand"
"net" "net"
"net/netip" "net/netip"
@@ -729,13 +730,13 @@ func saveCurrentStaticDNS(iface *net.Interface) error {
return nil return nil
} }
file := savedStaticDnsSettingsFilePath(iface) file := savedStaticDnsSettingsFilePath(iface)
if err := os.Remove(file); err != nil && !errors.Is(err, os.ErrNotExist) {
mainLog.Load().Warn().Err(err).Msg("could not remove old static DNS settings file")
}
ns, _ := currentStaticDNS(iface) ns, _ := currentStaticDNS(iface)
if len(ns) == 0 { if len(ns) == 0 {
return nil return nil
} }
if err := os.Remove(file); err != nil && !errors.Is(err, fs.ErrNotExist) {
mainLog.Load().Warn().Err(err).Msg("could not remove old static DNS settings file")
}
mainLog.Load().Debug().Msgf("DNS settings for %s is static, saving ...", iface.Name) mainLog.Load().Debug().Msgf("DNS settings for %s is static, saving ...", iface.Name)
if err := os.WriteFile(file, []byte(strings.Join(ns, ",")), 0600); err != nil { if err := os.WriteFile(file, []byte(strings.Join(ns, ",")), 0600); err != nil {
mainLog.Load().Err(err).Msgf("could not save DNS settings for iface: %s", iface.Name) mainLog.Load().Err(err).Msgf("could not save DNS settings for iface: %s", iface.Name)