Created
March 3, 2023 20:08
-
-
Save joemaller/5108da8640c1d707a834ea81584dd6a5 to your computer and use it in GitHub Desktop.
script and config file for configuring WSL 2 portproxy rules
This file contains 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
{ | |
"ports": [3000, 5173, 8000] | |
} |
This file contains 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
$path = Split-Path($MyInvocation.MyCommand.Path) | |
$logfile = "wsl-portproxy.log" | |
$jsonfile = "wsl-portproxy.json" | |
Start-Transcript -Path "$path\$logfile" -Append | |
Write-Output "" | |
# Default ports: | |
# 3000 - create-react-app | |
# 5173 - Vite/Vue | |
$config = @{ | |
ports = 3000, 5173 | |
} | |
# Attempt to load ports from a $path\wsl-portproxy.json file | |
if ([System.IO.File]::Exists($jsonfile)) { | |
try { | |
$config = Get-Content $path\$jsonfile | ConvertFrom-Json | |
Write-Output "Reading ports from $path\$jsonfile" | |
} | |
catch { | |
Write-Warning "Unable to read $jsonfile" | |
Write-Error $_ | |
Write-Output "Opening default ports: $($config.ports)" | |
} | |
} | |
else { | |
Write-Output "No file found at $jsonfile" | |
Write-Output "Opening default ports: $($config.ports)" | |
} | |
Write-Output "Listing current PortProxy ports" | |
Invoke-Expression "netsh interface portproxy show all" | |
# Reset the entire interface: | |
# Trying this since I was still seeing hanging errors with the more specific option | |
Write-Output "Resetting PortProxy Interface" | |
Invoke-Expression "netsh interface portproxy reset" | |
Start-Sleep -Seconds 2 | |
foreach ($port in $config.ports) { | |
# If this doesn't work, try the general reset above | |
# Write-Output "Removing any existing mappings for port $port" | |
# Invoke-Expression "netsh interface portproxy delete v4tov4 listenaddress=0.0.0.0 listenport=$port" | |
# # Probably not necessary? | |
# Start-Sleep -Seconds 2 | |
Write-Output "Opening port $port" | |
Invoke-Expression "netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=$port connectaddress=localhost" | |
} | |
Invoke-Expression "netsh interface portproxy show all" | |
Stop-Transcript |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment