Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: accessing the server on WSL2 from your LAN #13393

Merged
merged 1 commit into from
May 31, 2023

Conversation

sapphi-red
Copy link
Member

Description

close #13028

Additional context


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the PR Title Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@sapphi-red sapphi-red added the documentation Improvements or additions to documentation label May 31, 2023
@stackblitz
Copy link

stackblitz bot commented May 31, 2023

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@patak-dev patak-dev merged commit 2872d55 into vitejs:main May 31, 2023
@sapphi-red sapphi-red deleted the docs/wsl2-expose-lan branch June 1, 2023 06:21
@Esensats
Copy link

The issue with the description of the problem was closed, so I figured that I would leave this here, for whoever finds manually doing all of this Firewall stuff very unproductive.


Here's a ps1 script that automatically forwards required ports from WSL and adds Windows Firewall rules:

try {
	$remoteport = bash.exe -c "ip addr show eth0 | grep 'inet '"
	$found = $remoteport -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';

	if( $found ){
	  $remoteport = $matches[0];
	} else{
	  throw "The Script Exited, the ip address of WSL 2 cannot be found";
	}

	#[Ports]

	#All the ports you want to forward separated by a comma (no spaces). 5174 was added for demonstration
	$ports=@(5173,5174);


	#[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
	Remove-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -ErrorAction SilentlyContinue;

	#adding Exception Rules for inbound and outbound Rules
	New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Outbound -LocalPort $ports -Action Allow -Protocol TCP -ErrorAction stop;

	New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Inbound -LocalPort $ports -Action Allow -Protocol TCP -ErrorAction stop;


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

	echo "For WSL IP $remoteport added Firewall rules and port forwards for ports: $ports_a";
} catch {
    Write-Error $_.Exception.ToString();
    Read-Host -Prompt "The above error occurred. Press Enter to exit.";
}

Change ports as you need.

Save this script in a .ps1 file. You can then either run this script with admin rights manually every time you start your PC or:

  • Create a task in the task scheduler for the script with increased rights on starting PC
  • Or create a shortcut to the script that has admin rights, and in the "object" field of the properties of the object you should have this:
"C:\Program Files\PowerShell\7\pwsh.exe" -f C:\path\to\your\script.ps1

This launches the script with powershell (change powershell and script paths to your own).

If you had custom firewall rules and/or netsh portproxies for this, then delete them. To list all portproxies that you have right now use this:

netsh interface portproxy show all

If you want to change ports, change them in the script and re-run the script.


If maintainers (understandably) find this offtop, then please provide alternatives to users, or at least give them the script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Expose host to network when running in WSL2
3 participants