Search code examples
windowsapinetworkinglocalwindows-subsystem-for-linux

Exposing local api inside WSL to physical devices and emulators


I'm working on a react native app from a windows 10 computer. I installed WSL, set up my backend needs on it, and I can see my api responding on localhost from windows, no problem on that end.

Now I would like to call it from my mobile app - both from an android emulator using Android Studio and from my physical android device (using Expo) connected to the same local network over wifi.

Using Ngrok it could work, exposing my localhost to a public url, but that's not what I'm looking for. I want it all to remain local if possible. My pc's local ipv4 is 10.0.0.8, but nothing comes out on that address (not from the mobile app neither from my desktop browser, which works when using localhost).

My Ubuntu inside WSL says:

~$ cat /etc/hosts

# This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf:
# [network]
# generateHosts = false
127.0.0.1       localhost
127.0.1.1       JB-PC.localdomain       JB-PC

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

My ipconfig:

Carte Ethernet Ethernet :

   Suffixe DNS propre à la connexion. . . : Home
   Adresse IPv6 de liaison locale. . . . .: fe80::f04e:ae2b:40b9:3d82%6
   Adresse IPv4. . . . . . . . . . . . . .: 10.0.0.8
   Masque de sous-réseau. . . . . . . . . : 255.255.255.0
   Passerelle par défaut. . . . . . . . . : 10.0.0.138

Carte Ethernet vEthernet (WSL) :

   Suffixe DNS propre à la connexion. . . :
   Adresse IPv6 de liaison locale. . . . .: fe80::68c3:25c9:588a:a957%17
   Adresse IPv4. . . . . . . . . . . . . .: 172.20.32.1
   Masque de sous-réseau. . . . . . . . . : 255.255.240.0
   Passerelle par défaut. . . . . . . . . :

localhost pings on ::1 :

C:\WINDOWS\system32>ping localhost

Envoi d’une requête 'ping' sur JB-PC [::1] avec 32 octets de données :
Réponse de ::1 : temps<1ms
Réponse de ::1 : temps<1ms
Réponse de ::1 : temps<1ms
Réponse de ::1 : temps<1ms

Statistiques Ping pour ::1:
    Paquets : envoyés = 4, reçus = 4, perdus = 0 (perte 0%),
Durée approximative des boucles en millisecondes :
    Minimum = 0ms, Maximum = 0ms, Moyenne = 0ms

and 127.0.0.1 pings as well, and pinging localhost with -4 option gives the same output:

C:\WINDOWS\system32>ping 127.0.0.1

Envoi d’une requête 'Ping'  127.0.0.1 avec 32 octets de données :
Réponse de 127.0.0.1 : octets=32 temps<1ms TTL=128
Réponse de 127.0.0.1 : octets=32 temps<1ms TTL=128
Réponse de 127.0.0.1 : octets=32 temps<1ms TTL=128
Réponse de 127.0.0.1 : octets=32 temps<1ms TTL=128

Statistiques Ping pour 127.0.0.1:
    Paquets : envoyés = 4, reçus = 4, perdus = 0 (perte 0%),
Durée approximative des boucles en millisecondes :
    Minimum = 0ms, Maximum = 0ms, Moyenne = 0ms

What should I do to make this work?


Solution

  • So I found a solution thanks to https://superuser.com/a/1618446/757755 I successfully used option 2 and used port forwarding, with the script found here. Remember that your PowerShell script has to be ran as administrator.