getWSL2VirtioProxyWindowsIP uses PowerShell to get the Windows vEthernet (WSL) interface IP. In virtioproxy mode, the default gateway is the network router (not Windows), so we need the Hyper-V virtual switch IP that connects Windows to WSL2.
()
| 215 | // In virtioproxy mode, the default gateway is the network router (not Windows), |
| 216 | // so we need the Hyper-V virtual switch IP that connects Windows to WSL2. |
| 217 | func getWSL2VirtioProxyWindowsIP() (string, error) { |
| 218 | psScript := `Get-NetIPAddress -AddressFamily IPv4 -ErrorAction SilentlyContinue | Where-Object { $_.InterfaceAlias -like 'vEthernet (WSL*' } | Select-Object -First 1 -ExpandProperty IPAddress` |
| 219 | |
| 220 | cmd := exec.Command("powershell.exe", "-NoProfile", "-NonInteractive", "-Command", psScript) |
| 221 | out, err := cmd.CombinedOutput() |
| 222 | if err != nil { |
| 223 | return "", fmt.Errorf("powershell failed in getWSL2VirtioProxyWindowsIP: %w (output: %s)", err, string(out)) |
| 224 | } |
| 225 | |
| 226 | ip := strings.TrimSpace(string(out)) |
| 227 | if ip == "" { |
| 228 | return "", fmt.Errorf("no vEthernet (WSL) IPv4 address found via PowerShell") |
| 229 | } |
| 230 | |
| 231 | if parsedIP := net.ParseIP(ip); parsedIP == nil { |
| 232 | return "", fmt.Errorf("unable to parse IP address '%s' from vEthernet (WSL) interface", ip) |
| 233 | } |
| 234 | |
| 235 | return ip, nil |
| 236 | } |
| 237 | |
| 238 | // getWSL2PhysicalIP returns the WSL2 VM's own physical IP address by finding |
| 239 | // the source IP used for traffic to an external destination. This is used as a |
no test coverage detected