Skip to content

Instantly share code, notes, and snippets.

@younes0
Last active September 19, 2024 12:57
Show Gist options
  • Save younes0/de8ff1386a5868e77e9907728f93645e to your computer and use it in GitHub Desktop.
Save younes0/de8ff1386a5868e77e9907728f93645e to your computer and use it in GitHub Desktop.
LAN Forwarding to Metro (Expo / React Native) dev server under WSL
{
"private": true,
"scripts": {
"wsl:lan:start": "export env REACT_NATIVE_PACKAGER_HOSTNAME=$(powershell.exe -executionpolicy bypass -File wsl-get-lan-ip.ps1) && yarn wsl:port-forward && yarn start",
"wsl:port-forward": "powershell.exe -executionpolicy bypass -File wsl-port-forward.ps1",
}
}
(get-WmiObject Win32_NetworkAdapterConfiguration|Where {$_.Ipaddress.length -gt 1}).ipaddress[0]
$ports = @(
# expo / metro
8081,
19000,
19002,
19006,
# api
5000,
);
# open ports in Windows Firewall
# --------------------------------
$wslAddress = (wsl -- ip -o -4 -json addr list eth0 `
| ConvertFrom-Json `
| %{ $_.addr_info.local } `
| ?{ $_ })
$listenAddress = '0.0.0.0';
foreach ($port in $ports) {
Invoke-Expression "netsh interface portproxy delete v4tov4 listenport=$port listenaddress=$listenAddress" 2>&1 | Out-Null;
Invoke-Expression "netsh interface portproxy add v4tov4 listenport=$port listenaddress=$listenAddress connectport=$port connectaddress=$wslAddress" 2>&1 | Out-Null;
}
$fireWallDisplayName = 'WSL Port Forwarding';
$portsStr = $ports -join ",";
Invoke-Expression "Remove-NetFireWallRule -DisplayName '$fireWallDisplayName'" 2>&1 | Out-Null;
Invoke-Expression "New-NetFireWallRule -DisplayName '$fireWallDisplayName' -Direction Outbound -LocalPort $portsStr -Action Allow -Protocol TCP" 2>&1 | Out-Null;
Invoke-Expression "New-NetFireWallRule -DisplayName '$fireWallDisplayName' -Direction Inbound -LocalPort $portsStr -Action Allow -Protocol TCP" 2>&1 | Out-Null;
@younes0
Copy link
Author

younes0 commented Aug 31, 2023

Usage:
yarn wsl:lan:start

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment