iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🐧

How to Access WSL2 Web Apps from a Smartphone on the Same LAN

に公開

I want to immediately check a web app under development in WSL2 on a smartphone

It is well known that when your smartphone and PC are connected to the same Wi-Fi, you can verify your web app by accessing http://<PC_IP_Address>:<Web_App_Port> from your smartphone's browser.
I usually develop business systems and libraries for PCs, so I rarely have the chance to use this method, but I recently needed to check something on a smartphone for the first time in a while.

Since specific steps were required when the development environment is WSL2, I'm publishing this as a memo.

Conclusion for those who only want to know WSL2-specific processing

Please open PowerShell with administrator privileges.
You can enter the following line by line, or it should work if you write it in a .ps1 file and execute it.
I'm not very familiar with PowerShell, so I type them in one by one.

Start

$ports = @(3000, 8000) # Specify ports used by the web app in an array
$fwRuleName = "WSL 2 Firewall Unlock"
$wsl2Address = wsl -e hostname -I | ForEach-Object { $_.trim() }

New-NetFireWallRule -DisplayName $fwRuleName -Direction Inbound -LocalPort $ports -Action Allow -Protocol TCP

for ($i = 0; $i -lt $ports.length; $i++) {
  $port = $ports[$i]
  netsh interface portproxy add v4tov4 listenport=$port listenaddress=* connectport=$port connectaddress=$wsl2Address
}

Verify

Get-NetFireWallRule -DisplayName $fwRuleName | Get-NetFireWallPortFilter
netsh interface portproxy show v4tov4

End

Remove-NetFireWallRule -DisplayName $fwRuleName

for ($i = 0; $i -lt $ports.length; $i++) {
  $port = $ports[$i]
  netsh interface portproxy delete v4tov4 listenport=$port listenaddress=*
}

If you only use one port, you can define $port = 3000 instead of $ports = @(3000) and dismantle the for loop.

Start

$port = 3000
$fwRuleName = "WSL 2 Firewall Unlock"
$wsl2Address = wsl -e hostname -I | ForEach-Object { $_.trim() }

New-NetFireWallRule -DisplayName $fwRuleName -Direction Inbound -LocalPort $port -Action Allow -Protocol TCP
netsh interface portproxy add v4tov4 listenport=$port listenaddress=* connectport=$port connectaddress=$wsl2Address

End

Remove-NetFireWallRule -DisplayName $fwRuleName
netsh interface portproxy delete v4tov4 listenport=$port listenaddress=*

Following the official MS documentation below resulted in smartphone access being blocked by the firewall, so New-NetFireWallRule was necessary.
https://docs.microsoft.com/ja-jp/windows/wsl/networking#accessing-a-wsl-2-distribution-from-your-local-area-network-lan

When assigning the standard output to $wsl2Address, I used ForEach-Object to trim trailing whitespace; please let me know if there's a shorter way to write this.
(I avoided the % alias because it's hard to understand if you're not familiar with PowerShell.)

Reference sites

https://rcmdnk.com/blog/2021/03/01/computer-windows-network/
http://yanor.net/wiki/?Windows/WSL/ネットワーク/WSL2のホストへ外部からSSH接続する
https://aka-akatsuki.hatenablog.com/entry/2018/08/19/162819

Additional information for those who couldn't connect with the above alone

Checking the PC's IP address

Please search for "windows ipconfig" on Google.

Checking the WSL2 IP address

It is displayed by the command below, which is also included in the start commands above:

wsl -e hostname -I

https://qiita.com/neko_the_shadow/items/25b797cb436078b9e832

Wi-Fi router settings

If a feature called "Privacy Separator" is enabled, your smartphone will not be able to find your PC.
https://www.pfu.fujitsu.com/imaging/downloads/manual/cloud_cmn/ix100/jp/installationcloud/topic/ap.html

Web framework settings

Modern web frameworks sometimes block access from addresses other than "localhost" or "127.0.0.1" as a countermeasure against DNS rebinding.
https://blog.tokumaru.org/2022/05/dns-rebinding-protection.html

Please check your web framework's documentation and set it to host: '0.0.0.0'.

https://ja.vitejs.dev/config/#server-host
https://nuxtjs.org/ja/docs/features/configuration/#ホストとポート番号を編集する
https://nextjs.org/docs/api-reference/cli#development

Debugging steps

If you're not sure where it's failing, it's a good idea to check using the following steps:

  1. Install the web framework directly on the Windows side.
  2. Set host: '0.0.0.0'.
  3. Start the server.
  4. Access http://localhost:<port> in your PC's browser and confirm the display.
  5. Check the PC's IP address with ipconfig.
  6. Access http://<PC_IP_address>:<port> from your smartphone.
  7. If it's not displayed, the Wi-Fi router's privacy separator setting might be the cause, so check it.
  8. If it's displayed, stop the server and remove the web framework.
  9. Install the web framework directly on the WSL2 side.
  10. Set host: '0.0.0.0'.
  11. Start the server.
  12. Enter the start commands provided at the top of this article.
  13. Access http://<PC_IP_address>:<port> in your PC's browser.
  14. If it's not displayed, the netsh interface portproxy add ~ command is incorrect; check the settings with netsh interface portproxy show v4tov4.
  15. If it is displayed on the PC, access http://<PC_IP_address>:<port> from your smartphone.
  16. If it's still not displayed on the smartphone, the New-NetFireWallRule -DisplayName ~ command is incorrect; check the details with Get-NetFireWallRule -DisplayName $fwRuleName | Get-NetFireWallPortFilter.
  17. Success!

Have a great WSL2 life!

Discussion