portforwarding.ps1
· 1.1 KiB · PowerShell
Brut
$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";
}
#[Ports]
#All the ports you want to forward separated by coma
$ports=@(5431,8080,8113);
#[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";
}
| 1 | $remoteport = bash.exe -c "ifconfig eth0 | grep 'inet '" |
| 2 | $found = $remoteport -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}'; |
| 3 | |
| 4 | if( $found ){ |
| 5 | $remoteport = $matches[0]; |
| 6 | } else{ |
| 7 | echo "The Script Exited, the ip address of WSL 2 cannot be found"; |
| 8 | } |
| 9 | |
| 10 | #[Ports] |
| 11 | |
| 12 | #All the ports you want to forward separated by coma |
| 13 | $ports=@(5431,8080,8113); |
| 14 | |
| 15 | |
| 16 | #[Static ip] |
| 17 | #You can change the addr to your ip config to listen to a specific address |
| 18 | $addr='0.0.0.0'; |
| 19 | $ports_a = $ports -join ","; |
| 20 | |
| 21 | |
| 22 | #Remove Firewall Exception Rules |
| 23 | iex "Remove-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' "; |
| 24 | |
| 25 | #adding Exception Rules for inbound and outbound Rules |
| 26 | iex "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Outbound -LocalPort $ports_a -Action Allow -Protocol TCP"; |
| 27 | iex "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Inbound -LocalPort $ports_a -Action Allow -Protocol TCP"; |
| 28 | |
| 29 | for( $i = 0; $i -lt $ports.length; $i++ ){ |
| 30 | $port = $ports[$i]; |
| 31 | iex "netsh interface portproxy delete v4tov4 listenport=$port listenaddress=$addr"; |
| 32 | iex "netsh interface portproxy add v4tov4 listenport=$port listenaddress=$addr connectport=$port connectaddress=$remoteport"; |
| 33 | } |