Skip to content

Instantly share code, notes, and snippets.

@tairosonloa
Last active December 15, 2023 00:15
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save tairosonloa/cd848c6f6befb6ba0a0af6924d66ea7e to your computer and use it in GitHub Desktop.
Save tairosonloa/cd848c6f6befb6ba0a0af6924d66ea7e to your computer and use it in GitHub Desktop.
Map WSL2 ports to windows host ports

What is this for?

This Powershell script lets you map ports from your WSL2 instance to the same ports on your Windows host machine. It's useful when working with development tools that must comunicate between the host and the WSL2, such an Android emulator running on Windows and a development server running on WSL2.

How does this work?

  1. Start your WSL2 instance
  2. Write into the $ports array the ports you want to map.
  3. With the WSL2 instance running, run the powersell script on Windows as administrator.

Unfortunately, as the WSL2 instance IP address is dynamic and changes everytime the instance is restarted, you must repeat the step 3. every time you relaunch WSL2.

FAQ

How can I connect an expo development server inside WSL2 with an Android emulator on Windows?

From my "response" in Stack Overflow:

  1. Install an Android Emulator and use the Android Image that contains Google Services and all related Google Stuff.
  2. Login into your Google account on the emulator. Then, go to the Google Play Store (the apps store) on the emulator and download the Expo app.
  3. On the downloaded Expo app, login in with the same Expo account you uses for development.
  4. You should be able to see the development app on Recently in development or Recently opened. If not, just open it scanning the QR from your Android Phone (not the emulator) once, to be shyncronized with your expo account. 5 Also, you could copy the url exp://<your-windows-ip>:90000 and it should be detected in the emulator from clipboard.
$remoteport = bash.exe -c "ip -4 addr show eth0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}'"
$found = $remoteport -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
if( $found ){
$remoteport = $matches[0];
} else{
echo "The Script Exited, the ip address of WSL 2 cannot be found";
exit;
}
#[Ports]
#All the ports you want to forward separated by coma
$ports=@(80,443,3000,3333,5000,8080,8081,19000,19001);
#[Static ip]
#You can change the addr to your ip config to listen to a specific address
$addr='0.0.0.0';
$ports_a = $ports -join ",";
#Remove Firewall Exception Rules
iex "Remove-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' ";
#adding Exception Rules for inbound and outbound Rules
iex "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Outbound -LocalPort $ports_a -Action Allow -Protocol TCP";
iex "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Inbound -LocalPort $ports_a -Action Allow -Protocol TCP";
for( $i = 0; $i -lt $ports.length; $i++ ){
$port = $ports[$i];
iex "netsh interface portproxy delete v4tov4 listenport=$port listenaddress=$addr";
iex "netsh interface portproxy add v4tov4 listenport=$port listenaddress=$addr connectport=$port connectaddress=$remoteport";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment