Skip to content

Instantly share code, notes, and snippets.

@LouDnl
Created August 14, 2023 12:44
Show Gist options
  • Save LouDnl/f1ff1fc6e506b07fcf0e247f35d2cf81 to your computer and use it in GitHub Desktop.
Save LouDnl/f1ff1fc6e506b07fcf0e247f35d2cf81 to your computer and use it in GitHub Desktop.
WSL Port Forwarding by John Wright - https://pastebin.com/HufmMFun
$ports = @(80, 443, 10000, 3000, 5000);
$wslAddress = bash.exe -c "ip addr show eth0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}'";
if ($wslAddress -match '^(\d{1,3}\.){3}\d{1,3}$') {
Write-Host "WSL IP address: $wslAddress" -ForegroundColor Green;
Write-Host "Ports: $ports" -ForegroundColor Green;
}
else {
Write-Host "Error: Could not find WSL IP address." -ForegroundColor Red;
exit;
}
$listenAddress = '0.0.0.0';
foreach ($port in $ports) {
netsh interface portproxy delete v4tov4 listenport=$port listenaddress=$listenAddress;
netsh interface portproxy add v4tov4 listenport=$port listenaddress=$listenAddress connectport=$port connectaddress=$wslAddress;
}
$fireWallDisplayName = 'WSL Port Forwarding';
Remove-NetFireWallRule -DisplayName $fireWallDisplayName;
New-NetFireWallRule -DisplayName $fireWallDisplayName -Direction Outbound -LocalPort $ports -Action Allow -Protocol TCP;
New-NetFireWallRule -DisplayName $fireWallDisplayName -Direction Inbound -LocalPort $ports -Action Allow -Protocol TCP;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment