-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwslbridge.ps1
26 lines (26 loc) · 1.24 KB
/
wslbridge.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# [Configuration]
# All the ports you want to forward separated by coma
$ports=@(8081, 80, 443);
# You can change the addr to your ip
# config to listen to a specific address
$addr='192.168.1.142';
# [Init]
$remoteport = bash.exe -c "ip a show dev eth0 | grep 'inet '"
$found = $remoteport -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
if( $found ){
$remoteport = $matches[0];
} else{
Write-Output "The Script Exited, the ip address of WSL 2 cannot be found";
exit;
}
$ports_a = $ports -join ",";
# Remove Firewall Exception Rules
Invoke-Expression "Remove-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' ";
# adding Exception Rules for inbound and outbound Rules
Invoke-Expression "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Outbound -LocalPort $ports_a -Action Allow -Protocol TCP";
Invoke-Expression "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];
Invoke-Expression "netsh interface portproxy delete v4tov4 listenport=$port listenaddress=$addr";
Invoke-Expression "netsh interface portproxy add v4tov4 listenport=$port listenaddress=$addr connectport=$port connectaddress=$remoteport";
}