$remoteport = bash.exe -c "ifconfig eth0 | grep 'inet '" | |
$found = $remoteport -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}'; | |
if( $found ){ | |
$remoteport = $matches[0]; | |
} else{ | |
echo "The Script Exited, the ip address of WSL 2 cannot be found"; | |
exit; | |
} | |
#[Ports] | |
#All the ports you want to forward separated by coma | |
$ports=@(80,443,10000,3000,5000); | |
#[Static ip] | |
#You can change the addr to your ip config to listen to a specific address | |
$addr='0.0.0.0'; | |
$ports_a = $ports -join ","; | |
#Remove Firewall Exception Rules | |
iex "Remove-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' "; | |
#adding Exception Rules for inbound and outbound Rules | |
iex "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Outbound -LocalPort $ports_a -Action Allow -Protocol TCP"; | |
iex "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Inbound -LocalPort $ports_a -Action Allow -Protocol TCP"; | |
for( $i = 0; $i -lt $ports.length; $i++ ){ | |
$port = $ports[$i]; | |
iex "netsh interface portproxy delete v4tov4 listenport=$port listenaddress=$addr"; | |
iex "netsh interface portproxy add v4tov4 listenport=$port listenaddress=$addr connectport=$port connectaddress=$remoteport"; | |
} |
This comment has been minimized.
This comment has been minimized.
good! it works! tk! |
This comment has been minimized.
This comment has been minimized.
btw, this script comes from original comment: |
This comment has been minimized.
This comment has been minimized.
Well, the script actually doesn't work: after executing the above script (exactly as it was posted) I don't see any additional rules at Windows firewall and port forwarding doesn't work. Also I've tried to completely turn off Windows firewall - still no effect. |
This comment has been minimized.
This comment has been minimized.
Note the above only works with IPv4. I needed my host to be exposed on it's ipv6 address so I have to run the following |
This comment has been minimized.
This comment has been minimized.
I saw this script everywhere, but cant solve my problem unfortunately.. I have the same issue that @jmpp but my phone is Android help please! |
This comment has been minimized.
This comment has been minimized.
@luizwhite you're not alone :))) |
This comment has been minimized.
This comment has been minimized.
you can obtain ip with: wsl hostname -I |
This comment has been minimized.
This comment has been minimized.
$remoteport to $wsl2_ip maybe better naming? |
This comment has been minimized.
Nice, thank you!