Zuletzt aktiv 1740709113

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

Änderung 10a463eea835810a205cfd4e4c6fea6f9ca0f85c

portforwarding.ps1 Orginalformat
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
4if( $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
23iex "Remove-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' ";
24
25#adding Exception Rules for inbound and outbound Rules
26iex "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Outbound -LocalPort $ports_a -Action Allow -Protocol TCP";
27iex "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Inbound -LocalPort $ports_a -Action Allow -Protocol TCP";
28
29for( $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}