all: preserve DNS settings when running "ctrld restart"

By attempting to reset DNS before starting new ctrld process. This way,
ctrld will read the correct system DNS settings before changing itself.

While at it, some optimizations are made:

 - "ctrld start" won't set DNS anymore, since "ctrld run" has already did
   this, start command could just query socket control server and emittin
   proper message to users.

 - The gateway won't be included as nameservers on Windows anymore,
   since the GetAdaptersAddresses Windows API always returns the correct
   DNS servers of the interfaces.

 - The nameservers list that OS resolver is using will be shown during
   ctrld startup, making it easier for debugging.
This commit is contained in:
Cuong Manh Le
2024-05-21 17:08:18 +07:00
committed by Cuong Manh Le
parent f3dd344026
commit 96085147ff
5 changed files with 64 additions and 8 deletions

View File

@@ -377,7 +377,15 @@ func initCLI() {
uninstall(p, s)
os.Exit(1)
}
p.setDNS()
if cc := newSocketControlClient(s, sockDir); cc != nil {
if resp, _ := cc.post(ifacePath, nil); resp != nil && resp.StatusCode == http.StatusOK {
if iface == "auto" {
iface = defaultIfaceName()
}
logger := mainLog.Load().With().Str("iface", iface).Logger()
logger.Debug().Msg("setting DNS successfully")
}
}
}
},
}
@@ -482,7 +490,10 @@ func initCLI() {
Short: "Restart the ctrld service",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
s, err := newService(&prog{}, svcConfig)
readConfig(false)
v.Unmarshal(&cfg)
p := &prog{router: router.New(&cfg, runInCdMode())}
s, err := newService(p, svcConfig)
if err != nil {
mainLog.Load().Error().Msg(err.Error())
return
@@ -493,8 +504,10 @@ func initCLI() {
}
initLogging()
iface = runningIface(s)
tasks := []task{
{s.Stop, false},
{func() error { p.resetDNS(); return nil }, false},
{s.Start, true},
}
if doTasks(tasks) {
@@ -2511,3 +2524,20 @@ func upgradeUrl(baseUrl string) string {
}
return dlUrl
}
// runningIface returns the value of the iface variable used by ctrld process which is running.
func runningIface(s service.Service) string {
if sockDir, err := socketDir(); err == nil {
if cc := newSocketControlClient(s, sockDir); cc != nil {
resp, err := cc.post(ifacePath, nil)
if err != nil {
return ""
}
defer resp.Body.Close()
if buf, _ := io.ReadAll(resp.Body); len(buf) > 0 {
return string(buf)
}
}
}
return ""
}