all: always reset DNS before initializing OS resolver

So ctrld could always get the correct nameservers used by system to be
used for its OS resolver.
This commit is contained in:
Cuong Manh Le
2024-05-27 15:40:25 +07:00
committed by Cuong Manh Le
parent 411f7434f4
commit 5aca118dbb
3 changed files with 22 additions and 11 deletions

View File

@@ -182,7 +182,7 @@ func initCLI() {
return return
} }
status, err := s.Status() status, _ := s.Status()
isCtrldRunning := status == service.StatusRunning isCtrldRunning := status == service.StatusRunning
// If pin code was set, do not allow running start command. // If pin code was set, do not allow running start command.
@@ -500,7 +500,6 @@ func initCLI() {
iface = runningIface(s) iface = runningIface(s)
tasks := []task{ tasks := []task{
{s.Stop, false}, {s.Stop, false},
{func() error { resetDnsNoLog(p); return nil }, false},
{s.Start, true}, {s.Start, true},
} }
if doTasks(tasks) { if doTasks(tasks) {
@@ -509,10 +508,12 @@ func initCLI() {
mainLog.Load().Warn().Err(err).Msg("Service was restarted, but could not ping the control server") mainLog.Load().Warn().Err(err).Msg("Service was restarted, but could not ping the control server")
return return
} }
if cc := newSocketControlClient(s, dir); cc == nil { cc := newSocketControlClient(s, dir)
if cc == nil {
mainLog.Load().Notice().Msg("Service was not restarted") mainLog.Load().Notice().Msg("Service was not restarted")
os.Exit(1) os.Exit(1)
} }
_, _ = cc.post(ifacePath, nil)
mainLog.Load().Notice().Msg("Service restarted") mainLog.Load().Notice().Msg("Service restarted")
} }
}, },
@@ -927,13 +928,15 @@ NOTE: Uninstalling will set DNS to values provided by DHCP.`,
return true return true
} }
tasks := []task{ tasks := []task{
resetDnsTask(p, s),
{s.Stop, false}, {s.Stop, false},
{s.Start, false}, {s.Start, false},
} }
if doTasks(tasks) { if doTasks(tasks) {
if dir, err := socketDir(); err == nil { if dir, err := socketDir(); err == nil {
return newSocketControlClient(s, dir) != nil if cc := newSocketControlClient(s, dir); cc != nil {
_, _ = cc.post(ifacePath, nil)
return true
}
} }
} }
return false return false

View File

@@ -206,8 +206,10 @@ func (p *prog) preRun() {
} }
func (p *prog) postRun() { func (p *prog) postRun() {
mainLog.Load().Debug().Msgf("initialized OS resolver with nameservers: %v", ctrld.OsNameservers)
if !service.Interactive() { if !service.Interactive() {
p.resetDNS()
ns := ctrld.InitializeOsResolver()
mainLog.Load().Debug().Msgf("initialized OS resolver with nameservers: %v", ns)
p.setDNS() p.setDNS()
} }
} }

View File

@@ -32,12 +32,8 @@ const (
const bootstrapDNS = "76.76.2.22" const bootstrapDNS = "76.76.2.22"
// OsNameservers is the list of DNS nameservers used by OS resolver.
// This reads OS settings at the time ctrld process starts.
var OsNameservers = defaultNameservers()
// or is the Resolver used for ResolverTypeOS. // or is the Resolver used for ResolverTypeOS.
var or = &osResolver{nameservers: OsNameservers} var or = &osResolver{nameservers: defaultNameservers()}
// defaultNameservers returns nameservers used by the OS. // defaultNameservers returns nameservers used by the OS.
// If no nameservers can be found, ctrld bootstrap nameserver will be used. // If no nameservers can be found, ctrld bootstrap nameserver will be used.
@@ -49,6 +45,16 @@ func defaultNameservers() []string {
return ns return ns
} }
// InitializeOsResolver initializes OS resolver using the current system DNS settings.
// It returns the nameservers that is going to be used by the OS resolver.
//
// It's the caller's responsibility to ensure the system DNS is in a clean state before
// calling this function.
func InitializeOsResolver() []string {
or.nameservers = defaultNameservers()
return or.nameservers
}
// Resolver is the interface that wraps the basic DNS operations. // Resolver is the interface that wraps the basic DNS operations.
// //
// Resolve resolves the DNS query, return the result and the corresponding error. // Resolve resolves the DNS query, return the result and the corresponding error.