Utoljára aktív 1740709113

WSL2 기반 서버 구성 시 자동 포트포워딩 스크립트입니다.

bigdee gist felülvizsgálása 1740709113. Revízióhoz ugrás

1 file changed, 33 insertions

portforwarding.ps1(fájl létrehozva)

@@ -0,0 +1,33 @@
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 + }
9 +
10 + #[Ports]
11 +
12 + #All the ports you want to forward separated by coma
13 + $ports=@(5431,8080,8113);
14 +
15 +
16 + #[Static ip]
17 + #You can change the addr to your ip config to listen to a specific address
18 + $addr='0.0.0.0';
19 + $ports_a = $ports -join ",";
20 +
21 +
22 + #Remove Firewall Exception Rules
23 + iex "Remove-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' ";
24 +
25 + #adding Exception Rules for inbound and outbound Rules
26 + iex "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Outbound -LocalPort $ports_a -Action Allow -Protocol TCP";
27 + iex "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Inbound -LocalPort $ports_a -Action Allow -Protocol TCP";
28 +
29 + for( $i = 0; $i -lt $ports.length; $i++ ){
30 + $port = $ports[$i];
31 + iex "netsh interface portproxy delete v4tov4 listenport=$port listenaddress=$addr";
32 + iex "netsh interface portproxy add v4tov4 listenport=$port listenaddress=$addr connectport=$port connectaddress=$remoteport";
33 + }
Újabb Régebbi