Advertisement
athaariq

Fix WSL 2 network problem

Jun 19th, 2020
1,301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.19 KB | None | 0 0
  1. $remoteport = bash.exe -c "ifconfig eth0 | grep 'inet '"
  2. $found = $remoteport -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
  3.  
  4. if( $found ){
  5.   $remoteport = $matches[0];
  6. } else{
  7.   echo "The Script Exited, the ip address of WSL 2 cannot be found";
  8.   exit;
  9. }
  10.  
  11. #[Ports]
  12.  
  13. #All the ports you want to forward separated by coma
  14. $ports=@(80,443,8000,8080);
  15.  
  16.  
  17. #[Static ip]
  18. #You can change the addr to your ip config to listen to a specific address
  19. $addr='0.0.0.0';
  20. $ports_a = $ports -join ",";
  21.  
  22.  
  23. #Remove Firewall Exception Rules
  24. iex "Remove-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' ";
  25.  
  26. #adding Exception Rules for inbound and outbound Rules
  27. iex "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Outbound -LocalPort $ports_a -Action Allow -Protocol TCP";
  28. iex "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Inbound -LocalPort $ports_a -Action Allow -Protocol TCP";
  29.  
  30. for( $i = 0; $i -lt $ports.length; $i++ ){
  31.   $port = $ports[$i];
  32.   iex "netsh interface portproxy delete v4tov4 listenport=$port listenaddress=$addr";
  33.   iex "netsh interface portproxy add v4tov4 listenport=$port listenaddress=$addr connectport=$port connectaddress=$remoteport";
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement