Skip to content

Commit 5856725

Browse files
committed
Update hotspot.go
1 parent 812027e commit 5856725

1 file changed

Lines changed: 25 additions & 20 deletions

File tree

wap/pkg/wifi/hotspot.go

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"fmt"
77
"os"
8+
"os/exec"
89
"runtime"
910
"strings"
1011
"time"
@@ -198,32 +199,36 @@ func DisconnectFromExternalWifi(ctx context.Context) error {
198199
case "windows":
199200
// TODO: Implement Windows-specific logic here.
200201
default:
201-
// Use nmcli to get a list of all connected networks.
202-
output, _, err := runCommand(ctx, "nmcli --terse --fields NAME connection show --active")
202+
// Query NAME and TYPE together to avoid a per-connection type lookup,
203+
// which breaks when connection names contain spaces (e.g. "Wired connection 1")
204+
// because runCommand splits on whitespace.
205+
output, stderr, err := runCommand(ctx, "nmcli --terse --fields NAME,TYPE connection show --active")
203206
if err != nil {
204-
log.Errorw("failed to get a list of connected networks", "err", err)
207+
log.Errorw("failed to get a list of connected networks", "err", err, "stderr", stderr)
205208
return err
206209
}
207210

208-
// The output from nmcli is a newline-separated list of network names.
209-
// We'll split this into a slice for easier processing.
210211
networks := strings.Split(output, "\n")
211212

212-
// Now we'll iterate over the list of networks, and disconnect from each wifi one
213-
// that isn't named "FxBlox".
214-
for _, network := range networks {
215-
if strings.TrimSpace(network) != "" && network != "FxBlox" {
216-
connectionTypeOutput, _, err := runCommand(ctx, fmt.Sprintf("nmcli -t -f connection.type con show %s", network))
217-
if err != nil {
218-
log.Errorw("failed to get the type of network", "network", network, "err", err)
219-
} else {
220-
connectionType := strings.Split(strings.TrimSpace(connectionTypeOutput), ":")[1]
221-
if connectionType == "802-11-wireless" {
222-
_, _, err = runCommand(ctx, fmt.Sprintf("nmcli connection down %s", network))
223-
if err != nil {
224-
log.Errorw("failed to disconnect from network", "network", network, "err", err)
225-
}
226-
}
213+
for _, line := range networks {
214+
line = strings.TrimSpace(line)
215+
if line == "" {
216+
continue
217+
}
218+
// Format is NAME:TYPE — TYPE (e.g. 802-11-wireless) never contains colons,
219+
// so split on the last colon to handle connection names that may contain colons.
220+
lastColon := strings.LastIndex(line, ":")
221+
if lastColon < 0 {
222+
continue
223+
}
224+
network := line[:lastColon]
225+
connType := line[lastColon+1:]
226+
227+
if network != "FxBlox" && connType == "802-11-wireless" {
228+
// Use exec.CommandContext to properly handle connection names with spaces
229+
cmd := exec.CommandContext(ctx, "nmcli", "connection", "down", network)
230+
if cmdErr := cmd.Run(); cmdErr != nil {
231+
log.Errorw("failed to disconnect from network", "network", network, "err", cmdErr)
227232
}
228233
}
229234
}

0 commit comments

Comments
 (0)