Last active
December 11, 2024 08:58
-
-
Save axonxorz/612865c294df5b87ced06fc2717c1ffc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# [CONFIGURATION] | |
# Ensure WSL2 container is started and SSH is running | |
wsl sudo /etc/init.d/ssh start | |
# Ports to be forwarded | |
$ports = @(22) + @(6543,6544) + @(6800..6810); | |
# Change $addr to restrict connections to a particular interface IP | |
$listen_addr = '0.0.0.0'; | |
# [RUNTIME] | |
$wsl_addr = (wsl hostname -I).trim() | |
if(!$wsl_addr){ | |
echo "Cannot determine IP of WSL2 instance"; | |
exit; | |
} | |
Write-Host "WSL IP: $wsl_addr" | |
$rule_name = "WSL2 Forwarding" | |
# Remove old firewall rules | |
Write-Host "Removing old firewall rules" | |
Remove-NetFireWallRule -DisplayName $rule_name | |
# Add new firewall rules | |
Write-Host "Add firewall rules" | |
New-NetFireWallRule -DisplayName $rule_name -Direction Outbound -LocalPort $ports_all -Action Allow -Protocol TCP | |
New-NetFireWallRule -DisplayName $rule_name -Direction Inbound -LocalPort $ports_all -Action Allow -Protocol TCP | |
for( $i = 0; $i -lt $ports.length; $i++ ){ | |
$port = $ports[$i]; | |
Write-Host "Update portproxy for $port" | |
netsh interface portproxy delete v4tov4 listenport=$port listenaddress=$listen_addr | |
netsh interface portproxy add v4tov4 listenport=$port listenaddress=$listen_addr connectport=$port connectaddress=$wsl_addr | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment