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.
This commit is contained in:
Cuong Manh Le authored and Cuong Manh Le committed 2025-01-20 14:57:34 +07:00
1 parent 89600f6091
commit 4df470b869
3 files changed
+37 -19

No files matched your search

+12 -8
View File
@@ -1723,20 +1723,22 @@ func upgradeUrl(baseUrl string) string {
} }
// runningIface returns the value of the iface variable used by ctrld process which is running. // 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 sockDir, err := socketDir(); err == nil {
if cc := newSocketControlClient(context.TODO(), s, sockDir); cc != nil { if cc := newSocketControlClient(context.TODO(), s, sockDir); cc != nil {
resp, err := cc.post(ifacePath, nil) resp, err := cc.post(ifacePath, nil)
if err != nil { if err != nil {
return "" return nil
} }
defer resp.Body.Close() defer resp.Body.Close()
if buf, _ := io.ReadAll(resp.Body); len(buf) > 0 { res := &ifaceResponse{}
return string(buf) if err := json.NewDecoder(resp.Body).Decode(res); err != nil {
return nil
} }
return res
} }
} }
return "" return nil
} }
// resetDnsNoLog performs resetting DNS with logging disable. // resetDnsNoLog performs resetting DNS with logging disable.
@@ -1754,7 +1756,7 @@ func resetDnsNoLog(p *prog) {
} }
// resetDnsTask returns a task which perform reset DNS operation. // 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 { return task{func() error {
if iface == "" { if iface == "" {
return nil return nil
@@ -1764,8 +1766,10 @@ func resetDnsTask(p *prog, s service.Service, isCtrldInstalled bool, currentRunn
// process to reset what setDNS has done properly. // process to reset what setDNS has done properly.
oldIface := iface oldIface := iface
iface = "auto" iface = "auto"
if currentRunningIface != "" { p.requiredMultiNICsConfig = requiredMultiNICsConfig()
iface = currentRunningIface if ir != nil {
iface = ir.Name
p.requiredMultiNICsConfig = ir.All
} }
p.runningIface = iface p.runningIface = iface
if isCtrldInstalled { if isCtrldInstalled {
+12 -8
View File
@@ -197,7 +197,7 @@ NOTE: running "ctrld start" without any arguments will start already installed c
isCtrldInstalled := !errors.Is(err, service.ErrNotInstalled) isCtrldInstalled := !errors.Is(err, service.ErrNotInstalled)
// Get current running iface, if any. // Get current running iface, if any.
var currentIface string var currentIface *ifaceResponse
// If pin code was set, do not allow running start command. // If pin code was set, do not allow running start command.
if isCtrldRunning { if isCtrldRunning {
@@ -522,9 +522,10 @@ func initStopCmd() *cobra.Command {
mainLog.Load().Error().Msg(err.Error()) mainLog.Load().Error().Msg(err.Error())
return return
} }
p.runningIface = iface p.preRun()
if ri := runningIface(s); ri != "" { if ir := runningIface(s); ir != nil {
p.runningIface = ri p.runningIface = ir.Name
p.requiredMultiNICsConfig = ir.All
} }
initLogging() initLogging()
@@ -610,7 +611,9 @@ func initRestartCmd() *cobra.Command {
doValidateCdRemoteConfig(cdUID) doValidateCdRemoteConfig(cdUID)
} }
iface = runningIface(s) if ir := runningIface(s); ir != nil {
iface = ir.Name
}
tasks := []task{ tasks := []task{
{s.Stop, false}, {s.Stop, false},
{s.Start, true}, {s.Start, true},
@@ -777,9 +780,10 @@ NOTE: Uninstalling will set DNS to values provided by DHCP.`,
if iface == "" { if iface == "" {
iface = "auto" iface = "auto"
} }
p.runningIface = iface p.preRun()
if ri := runningIface(s); ri != "" { if ir := runningIface(s); ir != nil {
p.runningIface = ri p.runningIface = ir.Name
p.requiredMultiNICsConfig = ir.All
} }
if err := checkDeactivationPin(s, nil); isCheckDeactivationPinErr(err) { if err := checkDeactivationPin(s, nil); isCheckDeactivationPinErr(err) {
os.Exit(deactivationPinInvalidExitCode) os.Exit(deactivationPinInvalidExitCode)
+13 -3
View File
@@ -31,6 +31,11 @@ const (
sendLogsPath = "/logs/send" sendLogsPath = "/logs/send"
) )
type ifaceResponse struct {
Name string `json:"name"`
All bool `json:"all"`
}
type controlServer struct { type controlServer struct {
server *http.Server server *http.Server
mux *http.ServeMux mux *http.ServeMux
@@ -205,15 +210,20 @@ func (p *prog) registerControlServerHandler() {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
})) }))
p.cs.register(ifacePath, http.HandlerFunc(func(w http.ResponseWriter, request *http.Request) { 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 // p.setDNS is only called when running as a service
if !service.Interactive() { if !service.Interactive() {
<-p.csSetDnsDone <-p.csSetDnsDone
if p.csSetDnsOk { if p.csSetDnsOk {
w.Write([]byte(iface)) res.Name = p.runningIface
return 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) { p.cs.register(viewLogsPath, http.HandlerFunc(func(w http.ResponseWriter, request *http.Request) {
lr, err := p.logReader() lr, err := p.logReader()