From 4df470b869ac71c51b3cc53b7d71215e1604a0f0 Mon Sep 17 00:00:00 2001 From: Cuong Manh Le Date: Wed, 15 Jan 2025 15:39:35 +0700 Subject: [PATCH] cmd/cli: ensure all ifaces operation is set correctly Since ctrld process does not rely on the global variable iface anymore during runtime, ctrld client's operations must be updated to reflect this change, too. --- cmd/cli/cli.go | 20 ++++++++++++-------- cmd/cli/commands.go | 20 ++++++++++++-------- cmd/cli/control_server.go | 16 +++++++++++++--- 3 files changed, 37 insertions(+), 19 deletions(-) diff --git a/cmd/cli/cli.go b/cmd/cli/cli.go index 6b7ac8f..4934c5a 100644 --- a/cmd/cli/cli.go +++ b/cmd/cli/cli.go @@ -1723,20 +1723,22 @@ func upgradeUrl(baseUrl string) string { } // runningIface returns the value of the iface variable used by ctrld process which is running. -func runningIface(s service.Service) string { +func runningIface(s service.Service) *ifaceResponse { if sockDir, err := socketDir(); err == nil { if cc := newSocketControlClient(context.TODO(), s, sockDir); cc != nil { resp, err := cc.post(ifacePath, nil) if err != nil { - return "" + return nil } defer resp.Body.Close() - if buf, _ := io.ReadAll(resp.Body); len(buf) > 0 { - return string(buf) + res := &ifaceResponse{} + if err := json.NewDecoder(resp.Body).Decode(res); err != nil { + return nil } + return res } } - return "" + return nil } // resetDnsNoLog performs resetting DNS with logging disable. @@ -1754,7 +1756,7 @@ func resetDnsNoLog(p *prog) { } // resetDnsTask returns a task which perform reset DNS operation. -func resetDnsTask(p *prog, s service.Service, isCtrldInstalled bool, currentRunningIface string) task { +func resetDnsTask(p *prog, s service.Service, isCtrldInstalled bool, ir *ifaceResponse) task { return task{func() error { if iface == "" { return nil @@ -1764,8 +1766,10 @@ func resetDnsTask(p *prog, s service.Service, isCtrldInstalled bool, currentRunn // process to reset what setDNS has done properly. oldIface := iface iface = "auto" - if currentRunningIface != "" { - iface = currentRunningIface + p.requiredMultiNICsConfig = requiredMultiNICsConfig() + if ir != nil { + iface = ir.Name + p.requiredMultiNICsConfig = ir.All } p.runningIface = iface if isCtrldInstalled { diff --git a/cmd/cli/commands.go b/cmd/cli/commands.go index ebf3dec..0982647 100644 --- a/cmd/cli/commands.go +++ b/cmd/cli/commands.go @@ -197,7 +197,7 @@ NOTE: running "ctrld start" without any arguments will start already installed c isCtrldInstalled := !errors.Is(err, service.ErrNotInstalled) // Get current running iface, if any. - var currentIface string + var currentIface *ifaceResponse // If pin code was set, do not allow running start command. if isCtrldRunning { @@ -522,9 +522,10 @@ func initStopCmd() *cobra.Command { mainLog.Load().Error().Msg(err.Error()) return } - p.runningIface = iface - if ri := runningIface(s); ri != "" { - p.runningIface = ri + p.preRun() + if ir := runningIface(s); ir != nil { + p.runningIface = ir.Name + p.requiredMultiNICsConfig = ir.All } initLogging() @@ -610,7 +611,9 @@ func initRestartCmd() *cobra.Command { doValidateCdRemoteConfig(cdUID) } - iface = runningIface(s) + if ir := runningIface(s); ir != nil { + iface = ir.Name + } tasks := []task{ {s.Stop, false}, {s.Start, true}, @@ -777,9 +780,10 @@ NOTE: Uninstalling will set DNS to values provided by DHCP.`, if iface == "" { iface = "auto" } - p.runningIface = iface - if ri := runningIface(s); ri != "" { - p.runningIface = ri + p.preRun() + if ir := runningIface(s); ir != nil { + p.runningIface = ir.Name + p.requiredMultiNICsConfig = ir.All } if err := checkDeactivationPin(s, nil); isCheckDeactivationPinErr(err) { os.Exit(deactivationPinInvalidExitCode) diff --git a/cmd/cli/control_server.go b/cmd/cli/control_server.go index 52406b1..d1daea3 100644 --- a/cmd/cli/control_server.go +++ b/cmd/cli/control_server.go @@ -31,6 +31,11 @@ const ( sendLogsPath = "/logs/send" ) +type ifaceResponse struct { + Name string `json:"name"` + All bool `json:"all"` +} + type controlServer struct { server *http.Server mux *http.ServeMux @@ -205,15 +210,20 @@ func (p *prog) registerControlServerHandler() { w.WriteHeader(http.StatusBadRequest) })) p.cs.register(ifacePath, http.HandlerFunc(func(w http.ResponseWriter, request *http.Request) { + res := &ifaceResponse{Name: iface} // p.setDNS is only called when running as a service if !service.Interactive() { <-p.csSetDnsDone if p.csSetDnsOk { - w.Write([]byte(iface)) - return + res.Name = p.runningIface + res.All = p.requiredMultiNICsConfig } } - w.WriteHeader(http.StatusBadRequest) + if err := json.NewEncoder(w).Encode(res); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + http.Error(w, fmt.Sprintf("could not marshal iface data: %v", err), http.StatusInternalServerError) + return + } })) p.cs.register(viewLogsPath, http.HandlerFunc(func(w http.ResponseWriter, request *http.Request) { lr, err := p.logReader()