Skip to content

Instantly share code, notes, and snippets.

@johncovv
Last active December 26, 2022 15:08
Show Gist options
  • Save johncovv/3fce5a1d1bcfb5ce3fad1f25d79ee17f to your computer and use it in GitHub Desktop.
Save johncovv/3fce5a1d1bcfb5ce3fad1f25d79ee17f to your computer and use it in GitHub Desktop.
portproxy wsl2 to localhost
# check if you are in administration mode, if not, ask for authorization
If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{
$arguments = "& '" + $myinvocation.mycommand.definition + "'"
Start-Process powershell -Verb runAs -ArgumentList $arguments
Break
}
# get wsl intern ip
$wsl_host = $(wsl hostname -I);
# here you specify the ports for the proxy
$ports = @(3000, 3333, 5432, 27017, 3001, 4200, 4201);
# reset all interfaces
Invoke-Expression "netsh interface portproxy reset";
for ( $i = 0; $i -lt $ports.length; $i++ ) {
$port = $ports[$i];
Invoke-Expression "netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=$port connectport=$port connectaddress=$wsl_host";
}
Invoke-Expression "netsh interface portproxy show v4tov4";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment