1

I was developing a simple react app and wanted to test it on my phone. For that every time I do npm start it runs a react-scripts module that launches the browser to show the project you're doing as developing. Great thing! was working like a charm.

urls

But because I was trying to test it on my phone and the on your network url wasn't working I had to find some solution and got a script on the WSL github with the following commands.

$remoteport = bash.exe -c "ifconfig eth0 | grep 'inet '"
$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,10000,3000,5000);


#[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";
}

After I read it I seemed fine I knew that the correct url was the actual IP assigned from my router to something like 192.168.1.36 and not the 172.19.118.whatever and that the script creates rules on the firewall from Windows to allow accessing the WSL2 instance.

The Question

Now I have a problem... I can't use localhost on my computer.
And I don't know what the script did I just fear the part that says 0.0.0.0

Do any of you guys know what to do in order to revert or fix the localhost alias on my laptop?

Update

Since WSL localhost resolution has nothing to do with the hosts file I'm removing that part of the post. I was confused and didn't understand this problem at all..

2
  • 1
    You will have to decide if you want to use localhost or kubernetes.docker.internal I suppose you can do both but only one is defined.
    – Ramhound
    Commented Aug 14, 2022 at 2:40
  • The 0.0.0.0 in the script means all network interfaces on your PC. The portproxy command will be effective for any current and future IP addresses assigned to your PC, firewall rules notwithstanding. It’s nothing alarming or special.
    – Daniel B
    Commented Aug 18, 2022 at 18:07

4 Answers 4

1

Change this 127.0.0.1 kubernetes.docker.internal to

127.0.0.1 kubernetes.docker.internal localhost
0

In the hosts file you bind a name/domain to an ip.

The 127.* is the localhost loopback on every machine.

With ping 127.1 or 127.0.0.1 you can check if your network device works if ping is not disabled.

You can bind more domain, names to the 127.* loopback.

You can set multiple domains/hosts to one ip:

Example hosts 1:

127.0.0.1 localhost name1 name2.domain name.name3.domain  kubernetes.docker.internal 

192.168.1.36 host.docker.internal gateway.docker.internal myreactapp

Example hosts 2:

127.0.0.1    localhost

127.0.0.1    name1

127.0.0.1    name2.domain

127.0.0.1    kubernetes.docker.internal   

192.168.1.36 host.docker.internal

192.168.1.36 gateway.docker.internal

192.168.1.36 myreactapp

Example hosts 3:

If you work with virtual host as an example in apache or an other webservice, you can bind them to different 127.* loopback addresses like

127.0.0.1 localhost

127.0.0.2 name1

127.0.1.1 kubernetes.docker.internal

192.168.1.36 host.docker.internal

192.168.1.36 gateway.docker.internal

192.168.1.36 myreactapp

On a normal way you don't need to touch your hosts file.

You create/run your react-script and you find the app on your local machine with the given port, like the 3000

http://localhost:3000

In your case the ip http://172.19.118.105:3000 is a virtual ip given by docker or your container/service environment(i think you work with docker or?) so you can http://172.19.118.105:3000 this address only from your local machine.

3 Solutions what you can do now:

  1. Change the ip of your phone to somethink like 172.19.118.106 and gateway over your router if you have not a direct connection and call the app with http://172.19.118.105:3000 maybe you need top add this IP to your Hosts File, and maybe this example will Not Work in your machine

  2. Use your WSL github script and modify your hosts to your need and call your app like you do with http://192.168.1.36:3000

  3. Run on windows ipconfig, linux ifconfig or ip a find your local ip like 192.168.1.36 and if the phone is on the same network try http://192.168.1.36:3000 if you got problems check your firewall rules

You can http your app from other devices like your phone only with the ip address if you wanna call by name/domain you need to add entrys to all hosts file on every device or you need to run a dns server

1
  • thanks for your answer but I'm looking for someone that explains what does the script and how it messed up my configuration in this case this has nothing to do with my hosts file because everything is happening on WSL. I do know that my firewall is blocking WSL access when typing the IP from my phone so I cant do a dynamic reverse proxy inside my computer because HyperV is complicated Commented Aug 21, 2022 at 2:17
0

Thank gosh someone at Microsoft did something to backup the settings!

I fixed it with this command netsh interface portproxy reset The last lines of the script touch hidden settings redirecting a proxy that is constantly changing because WSL has a dynamic IP.

So the hosts file in Windows is useless, everything has to do with WSL and port mapping from Windows with deep HyperV wiring. That stuff is way to complicated for me so I'm leaving it there. I can always deploy on a virtual machine for testing. It's a shame that WSL2 doesnt support a bridged network adapter. It uses NAT as default and only option.

Hope this helps for someone else

0

Its not an answer but a workaround.

But whenever 0.0.0.0 is selected, I SSH on a unique port, and forward whatever i need to, to my phone.

IE, I Forward X11 using XSDL. it uses 6000/+1 on default, but i change it at will. the same process works with tvnc etc...

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .