-
-
Save kendallroth/1f4871febffa0577338214f58673cc1a to your computer and use it in GitHub Desktop.
# Forward WSL2 ports to host machine/platform (handles Windows Firewall) | |
# | |
# NOTE: 'iex' is a shortform for 'Invoke-Expression' | |
# Ports that should be forwarded to WSL2 and allowed through firewall (comma-separated) | |
$ports = @(8081); | |
# WSL IP address changes whenever WSL restarts | |
$wsl_ip = $(wsl hostname -I).Trim(); | |
# Incoming requests from any IP should be matched | |
$listen_all_ips = '0.0.0.0'; | |
if ( -Not $wsl_ip ) { | |
Write-Output "IP address for WSL 2 cannot be found"; | |
exit; | |
} | |
Write-Output "WSL IP: '$wsl_ip'"; | |
### Windows Firewall ##### | |
$firewall_rule = "WSL2 Forwarded Ports"; | |
$firewall_ports = $ports -join ","; | |
# Remove existing firewal rule (will error if not already present; can ignore) | |
iex "Remove-NetFireWallRule -DisplayName '$firewall_rule' "; | |
# Allow Expo ports through Windows Firewall | |
iex "New-NetFireWallRule -DisplayName '$firewall_rule' -Direction Inbound -LocalPort $firewall_ports -Action Allow -Protocol TCP;" | |
iex "New-NetFireWallRule -DisplayName '$firewall_rule' -Direction Outbound -LocalPort $firewall_ports -Action Allow -Protocol TCP;" | |
### WSL Port Proxy ##### | |
# Show all previously proxied ports | |
iex "netsh interface portproxy show v4tov4" | |
# Configure port forwarding (via remove/add) | |
for ( $i = 0; $i -lt $ports.length; $i++ ) { | |
$port = $ports[$i]; | |
# Remove previously proxied port | |
iex "netsh interface portproxy delete v4tov4 listenport=$port listenaddress=$listen_all_ips" | |
iex "netsh interface portproxy add v4tov4 listenport=$port listenaddress=$listen_all_ips connectport=$port connectaddress=$wsl_ip" | |
} | |
# Show all newly proxied ports | |
iex "netsh interface portproxy show v4tov4" | |
cmd /c pause |
An more thorough explanation can be found at GitHub - kendallroth/payme
. One thing to note is that this is a powershell script intended to be run with administrator/elevated permissions 🤷. I'm not sure if the $windows_ip
variable might need to be changed, but 0.0.0.0
allows all IP addresses instead of locking to a specific one 🤷. This script also works solely for Expo ports (would need to be changed otherwise), and should be complemented with a run script in the package.json
.
Script has been updated to handle forwarding any WSL2 ports to the host/local (such as Expo, etc), with a bit more documentation and less hardcoded-variables.
Posting this to save someone, somewhere a little time....
If you're using nativewind 4.0.X you'll want to update the ports to something like$ports = @(8081,8089);
. Basically anything else that has its own build server and needs fast-refresh you'll add those ports as well. Some tools are like this.
@kendallroth great powershell script btw. Thank you.
@the-simian Can you please post your edited script I am using nativewind 4.0.X too.
can we make the $post any port?
thanks in anticipation
@chrisgate just change line 6 in the original script to include more ports. You can include as many as you'd like, any port number. In the default case I changed
$ports = @(8081);
to
$ports = @(8081,8089);
Hello!
I'm trying to get this up and running on my machine but for some reason it is not working. Is there anything I have to change in this script such as the value of $windows_ip variable?
thank you in advance.