Skip to content

Commit

Permalink
Add wsl
Browse files Browse the repository at this point in the history
  • Loading branch information
Chi-EEE committed Oct 19, 2023
1 parent 5262a05 commit 100cd2c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion tcp/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
How to enable Port Forwarding for TCP?

[CzBiX/WSLHostPatcher](https://github.com/CzBiX/WSLHostPatcher)
Run the wsl-port-forwarding.ps1 in Powershell Administrator
23 changes: 23 additions & 0 deletions tcp/wsl-port-forwarding.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# From: https://stackoverflow.com/a/71546540

$ports=@(12345) # the ports you want to open
$addr='0.0.0.0';

$wslIP = bash.exe -c "hostname -I"
$found = $wslIP -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';

if(! $wslIP -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}') {
echo "WSL's IP cannot be found. Aborting";
exit;
}

$portsStr = $ports -join ",";
iex "Remove-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' ";
iex "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Outbound -LocalPort $portsStr -Action Allow -Protocol TCP";
iex "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Inbound -LocalPort $portsStr -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=$wslIP";
}

0 comments on commit 100cd2c

Please sign in to comment.