MCPcopy Create free account
hub / github.com/ddev/ddev / getWSL2VirtioProxyWindowsIP

Function getWSL2VirtioProxyWindowsIP

pkg/dockerutil/host_docker_internal.go:217–236  ·  view source on GitHub ↗

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.

()

Source from the content-addressed store, hash-verified

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.
217func 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

Callers 1

GetHostDockerInternalFunction · 0.85

Calls 2

CommandMethod · 0.80
ErrorfMethod · 0.80

Tested by

no test coverage detected