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.
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..
localhost
orkubernetes.docker.internal
I suppose you can do both but only one is defined.