|
5 | 5 | "context" |
6 | 6 | "fmt" |
7 | 7 | "os" |
| 8 | + "os/exec" |
8 | 9 | "runtime" |
9 | 10 | "strings" |
10 | 11 | "time" |
@@ -198,32 +199,36 @@ func DisconnectFromExternalWifi(ctx context.Context) error { |
198 | 199 | case "windows": |
199 | 200 | // TODO: Implement Windows-specific logic here. |
200 | 201 | 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") |
203 | 206 | 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) |
205 | 208 | return err |
206 | 209 | } |
207 | 210 |
|
208 | | - // The output from nmcli is a newline-separated list of network names. |
209 | | - // We'll split this into a slice for easier processing. |
210 | 211 | networks := strings.Split(output, "\n") |
211 | 212 |
|
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) |
227 | 232 | } |
228 | 233 | } |
229 | 234 | } |
|
0 commit comments