Skip to content

Instantly share code, notes, and snippets.

@enpitsuLin
Last active January 11, 2023 06:03
Show Gist options
  • Save enpitsuLin/9ad741a25e88d9e0dc8d1ebe1a29daf7 to your computer and use it in GitHub Desktop.
Save enpitsuLin/9ad741a25e88d9e0dc8d1ebe1a29daf7 to your computer and use it in GitHub Desktop.
$WSLADDRESS = wsl -- ip -o -4 -json addr list eth0 | ConvertFrom-Json | ForEach-Object { $_.addr_info.local } | Where-Object { $_ }
#[Ports]
$PORTS = $args;
#[Static ip]
$LISTENADDRESS = '0.0.0.0';
$PORTS_STR = $PORTS -join ",";
#Remove Firewall Exception Rules
# Invoke-Expression "Remove-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' ";
#adding Exception Rules for inbound and outbound Rules
# Invoke-Expression "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Outbound -LocalPort $PORTS_STR -Action Allow -Protocol TCP";
# Invoke-Expression "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Inbound -LocalPort $PORTS_STR -Action Allow -Protocol TCP";
for ( $i = 0; $i -lt $PORTS.length; $i++ ) {
$port = $PORTS[$i];
Invoke-Expression "netsh interface portproxy delete v4tov4 listenport=$port listenaddress=$LISTENADDRESS";
Invoke-Expression "netsh interface portproxy add v4tov4 listenport=$port listenaddress=$LISTENADDRESS connectport=$port connectaddress=$WSLADDRESS";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment