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
2025-01-15 15:39:35 +07:00
committed by Cuong Manh Le
parent 89600f6091
commit 4df470b869
3 changed files with 37 additions and 19 deletions

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.
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 {

View File

@@ -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)

View File

@@ -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()