Skip to content

Instantly share code, notes, and snippets.

@a2902793
Forked from daehahn/wsl2-network.ps1
Created October 16, 2020 10:48
Show Gist options
  • Save a2902793/12232d45a8c8729b11e2a4defd8f7caa to your computer and use it in GitHub Desktop.
Save a2902793/12232d45a8c8729b11e2a4defd8f7caa to your computer and use it in GitHub Desktop.

Revisions

  1. @daehahn daehahn revised this gist Aug 2, 2020. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion wsl2-network.ps1
    Original file line number Diff line number Diff line change
    @@ -9,16 +9,18 @@ If ($Args[0] -eq "list") {
    exit;
    }

    # If elevation needed, start new process
    If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))
    {
    # Relaunch as an elevated process:
    Start-Process powershell.exe "-File",('"{0}"' -f $MyInvocation.MyCommand.Path),"$Args runas" -Verb RunAs
    exit
    }

    $Addr = "0.0.0.0"
    # You should modify '$Ports' for your applications
    $Ports = (22,80,443,8080)

    # Check WSL ip address
    wsl hostname -I | Set-Variable -Name "WSL"
    $found = $WSL -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
    if (-not $found) {
    @@ -34,6 +36,7 @@ if ($Args[0] -ne "delete") {
    }

    # Add each port into portproxy
    $Addr = "0.0.0.0"
    Foreach ($Port in $Ports) {
    iex "netsh interface portproxy delete v4tov4 listenaddress=$Addr listenport=$Port | Out-Null";
    if ($Args[0] -ne "delete") {
  2. @daehahn daehahn revised this gist Aug 2, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion wsl2-network.ps1
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # WSL2 network port forwarding script v1
    # for enable script, 'Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope CurrentUser' in Powershell,
    # for delete exist rules and ports, use 'delete' as parameter.
    # for delete exist rules and ports use 'delete' as parameter, for show ports use 'list' as parameter.
    # written by Daehyuk Ahn, Aug-1-2020

    # Display all portproxy information
  3. @daehahn daehahn revised this gist Aug 2, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion wsl2-network.ps1
    Original file line number Diff line number Diff line change
    @@ -45,7 +45,7 @@ Foreach ($Port in $Ports) {
    netsh interface portproxy show v4tov4;

    # Give user to chance to see above list when relaunched start
    if ($Args[0] -eq "runas" -Or $Args[1] -eq "runas") {
    If ($Args[0] -eq "runas" -Or $Args[1] -eq "runas") {
    Write-Host -NoNewLine 'Press any key to close! ';
    $null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');
    }
  4. @daehahn daehahn revised this gist Aug 2, 2020. 1 changed file with 8 additions and 2 deletions.
    10 changes: 8 additions & 2 deletions wsl2-network.ps1
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,12 @@
    # for delete exist rules and ports, use 'delete' as parameter.
    # written by Daehyuk Ahn, Aug-1-2020

    # Display all portproxy information
    If ($Args[0] -eq "list") {
    netsh interface portproxy show v4tov4;
    exit;
    }

    If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))
    {
    # Relaunch as an elevated process:
    @@ -29,9 +35,9 @@ if ($Args[0] -ne "delete") {

    # Add each port into portproxy
    Foreach ($Port in $Ports) {
    netsh interface portproxy delete v4tov4 listenaddress=$Addr listenport=$Port | Out-Null;
    iex "netsh interface portproxy delete v4tov4 listenaddress=$Addr listenport=$Port | Out-Null";
    if ($Args[0] -ne "delete") {
    netsh interface portproxy add v4tov4 listenaddress=$Addr listenport=$Port connectaddress=$WSL connectport=$Port | Out-Null;
    iex "netsh interface portproxy add v4tov4 listenaddress=$Addr listenport=$Port connectaddress=$WSL connectport=$Port | Out-Null";
    }
    }

  5. @daehahn daehahn revised this gist Aug 2, 2020. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions wsl2-network.ps1
    Original file line number Diff line number Diff line change
    @@ -29,9 +29,9 @@ if ($Args[0] -ne "delete") {

    # Add each port into portproxy
    Foreach ($Port in $Ports) {
    netsh interface portproxy delete v4tov4 listenaddress=$Addr listenport=$Port > null;
    netsh interface portproxy delete v4tov4 listenaddress=$Addr listenport=$Port | Out-Null;
    if ($Args[0] -ne "delete") {
    netsh interface portproxy add v4tov4 listenaddress=$Addr listenport=$Port connectaddress=$WSL connectport=$Port > null;
    netsh interface portproxy add v4tov4 listenaddress=$Addr listenport=$Port connectaddress=$WSL connectport=$Port | Out-Null;
    }
    }

  6. @daehahn daehahn revised this gist Aug 2, 2020. 1 changed file with 3 additions and 4 deletions.
    7 changes: 3 additions & 4 deletions wsl2-network.ps1
    Original file line number Diff line number Diff line change
    @@ -12,9 +12,8 @@ If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdent

    $Addr = "0.0.0.0"
    $Ports = (22,80,443,8080)
    $Param = $Args[0].ToLower()
    wsl hostname -I | Set-Variable -Name "WSL"

    wsl hostname -I | Set-Variable -Name "WSL"
    $found = $WSL -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
    if (-not $found) {
    echo "WSL2 cannot be found. Terminate script.";
    @@ -23,15 +22,15 @@ if (-not $found) {

    # Remove and Create NetFireWallRule
    Remove-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock';
    if ($Param -ne "delete") {
    if ($Args[0] -ne "delete") {
    New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Outbound -LocalPort $Ports -Action Allow -Protocol TCP;
    New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Inbound -LocalPort $Ports -Action Allow -Protocol TCP;
    }

    # Add each port into portproxy
    Foreach ($Port in $Ports) {
    netsh interface portproxy delete v4tov4 listenaddress=$Addr listenport=$Port > null;
    if ($Param -ne "delete") {
    if ($Args[0] -ne "delete") {
    netsh interface portproxy add v4tov4 listenaddress=$Addr listenport=$Port connectaddress=$WSL connectport=$Port > null;
    }
    }
  7. @daehahn daehahn revised this gist Aug 2, 2020. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions wsl2-network.ps1
    Original file line number Diff line number Diff line change
    @@ -22,10 +22,10 @@ if (-not $found) {
    }

    # Remove and Create NetFireWallRule
    Remove-NetFireWallRule -DisplayName 'WSL2 Firewall Unlock';
    Remove-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock';
    if ($Param -ne "delete") {
    New-NetFireWallRule -DisplayName 'WSL2 Firewall Unlock' -Direction Outbound -LocalPort $Ports -Action Allow -Protocol TCP;
    New-NetFireWallRule -DisplayName 'WSL2 Firewall Unlock' -Direction Inbound -LocalPort $Ports -Action Allow -Protocol TCP;
    New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Outbound -LocalPort $Ports -Action Allow -Protocol TCP;
    New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Inbound -LocalPort $Ports -Action Allow -Protocol TCP;
    }

    # Add each port into portproxy
  8. @daehahn daehahn revised this gist Aug 2, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion wsl2-network.ps1
    Original file line number Diff line number Diff line change
    @@ -32,7 +32,7 @@ if ($Param -ne "delete") {
    Foreach ($Port in $Ports) {
    netsh interface portproxy delete v4tov4 listenaddress=$Addr listenport=$Port > null;
    if ($Param -ne "delete") {
    netsh interface portproxy add v4tov4 listenaddress=$Addr listenport=$Port connectaddress=$WSL connectport=$Port > null;
    netsh interface portproxy add v4tov4 listenaddress=$Addr listenport=$Port connectaddress=$WSL connectport=$Port > null;
    }
    }

  9. @daehahn daehahn revised this gist Aug 2, 2020. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions wsl2-network.ps1
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # WSL2 network port forwarding script v1
    # for enable script, 'set-executionpolicy remotesigned' in Powershell
    # for delete exist rules and ports, use 'delete' as parameter
    # for enable script, 'Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope CurrentUser' in Powershell,
    # for delete exist rules and ports, use 'delete' as parameter.
    # written by Daehyuk Ahn, Aug-1-2020

    If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))
  10. @daehahn daehahn created this gist Aug 2, 2020.
    46 changes: 46 additions & 0 deletions wsl2-network.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    # WSL2 network port forwarding script v1
    # for enable script, 'set-executionpolicy remotesigned' in Powershell
    # for delete exist rules and ports, use 'delete' as parameter
    # written by Daehyuk Ahn, Aug-1-2020

    If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))
    {
    # Relaunch as an elevated process:
    Start-Process powershell.exe "-File",('"{0}"' -f $MyInvocation.MyCommand.Path),"$Args runas" -Verb RunAs
    exit
    }

    $Addr = "0.0.0.0"
    $Ports = (22,80,443,8080)
    $Param = $Args[0].ToLower()
    wsl hostname -I | Set-Variable -Name "WSL"

    $found = $WSL -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
    if (-not $found) {
    echo "WSL2 cannot be found. Terminate script.";
    exit;
    }

    # Remove and Create NetFireWallRule
    Remove-NetFireWallRule -DisplayName 'WSL2 Firewall Unlock';
    if ($Param -ne "delete") {
    New-NetFireWallRule -DisplayName 'WSL2 Firewall Unlock' -Direction Outbound -LocalPort $Ports -Action Allow -Protocol TCP;
    New-NetFireWallRule -DisplayName 'WSL2 Firewall Unlock' -Direction Inbound -LocalPort $Ports -Action Allow -Protocol TCP;
    }

    # Add each port into portproxy
    Foreach ($Port in $Ports) {
    netsh interface portproxy delete v4tov4 listenaddress=$Addr listenport=$Port > null;
    if ($Param -ne "delete") {
    netsh interface portproxy add v4tov4 listenaddress=$Addr listenport=$Port connectaddress=$WSL connectport=$Port > null;
    }
    }

    # Display all portproxy information
    netsh interface portproxy show v4tov4;

    # Give user to chance to see above list when relaunched start
    if ($Args[0] -eq "runas" -Or $Args[1] -eq "runas") {
    Write-Host -NoNewLine 'Press any key to close! ';
    $null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');
    }