11@ echo  off 
2- setlocal  enabledelayedexpansion 
3- 
4- ::  Check if running as administrator
5- net session  > nul  2 >& 1
6- if  %errorlevel%  neq  0  (
7-     echo  This script must be run as administrator
8-     pause 
9-     exit  /b 1 
10- )
11- 
12- ::  Check WSL network status
13- echo  Checking WSL network status...
14- wsl  ping  -c 1  8.8.8.8 > nul  2 >& 1
15- if  %errorlevel%  neq  0  (
16-     echo  WSL network is not properly configured. Please check your WSL installation.
17-     exit  /b 1 
18- )
19- 
202::  Fetch the WSL IP dynamically using PowerShell and only pick the first IP
213for  /f " tokens=1"   %%i  in  ('powershell -command " wsl hostname -I"  ') do  set  WSL_IP = %%i 
224
@@ -31,42 +13,15 @@ set PLEX_PORT=32400
3113::  Initialize list of additional ports to process later
3214set  ADDITIONAL_PORTS = 
3315
34- ::  Ask about firewall rules
35- set  /p CONFIGURE_FIREWALL = " Do you want to allow external devices on your network to access these ports? (Y/N): " 
36- if  /i " %CONFIGURE_FIREWALL% " == " Y"   (
37-     echo  Firewall rules will be configured to allow external access.
38- ) else  (
39-     echo  Firewall rules will not be modified. Only local access will be possible.
40- )
41- 
42- ::  Function to validate port number
43- :validate_port 
44- set  PORT = %1 
45- if  %PORT%  LSS  1  (
46-     echo  Invalid port number: %PORT% 
47-     exit  /b 1 
48- )
49- if  %PORT%  GTR  65535  (
50-     echo  Invalid port number: %PORT% 
51-     exit  /b 1 
52- )
53- goto  :eof 
54- 
5516::  Step 1: Ensure port 3000 is proxied
5617echo  Ensuring port %DEFAULT_PORT%  is proxied...
57- call  :validate_port  %DEFAULT_PORT% 
58- if  %errorlevel%  equ  0  (
59-     call  :proxy_port  %DEFAULT_PORT% 
60- )
18+ call  :proxy_port  %DEFAULT_PORT% 
6119
6220::  Step 2: Ask if user wants Plex (32400) to be proxied via their machine IP
6321set  /p PROXY_PLEX = " Do you want to access Plex via %USER_IP% :%PLEX_PORT% ? (Y/N): " 
6422if  /i " %PROXY_PLEX% " == " Y"   (
6523    echo  Adding Plex port %PLEX_PORT%  to the list for proxying...
66-     call  :validate_port  %PLEX_PORT% 
67-     if  %errorlevel%  equ  0  (
68-         set  ADDITIONAL_PORTS = %PLEX_PORT% 
69-     )
24+     set  ADDITIONAL_PORTS = %PLEX_PORT% 
7025)
7126
7227::  Step 3: Ask if user wants to proxy any other ports
@@ -76,10 +31,7 @@ if /i "%ADD_PORTS%"=="Y" (
7631
7732    ::  Process the input and append to ADDITIONAL_PORTS
7833    for  %%p  in  (%ADDITIONAL_PORTS_INPUT% ) do  (
79-         call  :validate_port  %%p 
80-         if  %errorlevel%  equ  0  (
81-             set  ADDITIONAL_PORTS = !ADDITIONAL_PORTS!  %%p 
82-         )
34+         set  ADDITIONAL_PORTS = %ADDITIONAL_PORTS%  %%p 
8335    )
8436)
8537
@@ -92,7 +44,6 @@ if not "%ADDITIONAL_PORTS%"=="" (
9244)
9345
9446::  End of script
95- echo  All ports have been configured successfully!
9647goto  :eof 
9748
9849::  Function to proxy a port and configure firewall
@@ -103,31 +54,22 @@ echo Checking if port %PORT% is already proxied...
10354::  Check if the port proxy rule already exists
10455netsh  interface portproxy show v4tov4 |  findstr  " %PORT% "   > nul 
10556if  %ERRORLEVEL%  equ  0  (
106-     echo  Port forwarding already exists for port  %PORT% 
57+     echo  Port forwarding already exists for 0.0.0.0: %PORT% 
10758    goto  :configure_firewall 
10859)
10960
11061::  Add port forwarding rule to proxy the specified port for all interfaces
11162echo  Adding port forwarding for 0.0.0.0:%PORT%  to WSL2 IP %WSL_IP% :%PORT% 
11263netsh  interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=%PORT%  connectaddress=%WSL_IP%  connectport=%PORT% 
11364
65+ ::  Configure the firewall
11466:configure_firewall 
115- ::  Only configure firewall if user chose to do so
116- if  /i " %CONFIGURE_FIREWALL% " == " Y"   (
117-     ::  Configure the firewall - only add if it doesn't exist
118-     netsh  advfirewall firewall show rule name=" Allow Port %PORT% "   > nul  2 >& 1
119-     if  %ERRORLEVEL%  neq  0  (
120-         echo  Adding firewall rule for port %PORT%  to allow external access...
121-         netsh  advfirewall firewall add rule name=" Allow Port %PORT% "   dir=in action=allow protocol=TCP localport=%PORT% 
122-     ) else  (
123-         echo  Firewall rule already exists for port %PORT% 
124-     )
125- ) else  (
126-     echo  Skipping firewall configuration for port %PORT%  as per user choice.
127- )
67+ echo  Configuring firewall to allow access to port %PORT% ...
68+ netsh  advfirewall firewall add rule name=" Allow Port %PORT% "   dir=in action=allow protocol=TCP localport=%PORT% 
12869
129- ::  Show current configuration for this port 
130- echo  Current port proxy configuration for port  %PORT% :
131- netsh  interface portproxy show v4tov4  |   findstr   " %PORT% " 
70+ ::  Verify port proxy rules 
71+ echo  Current port proxy rules :
72+ netsh  interface portproxy show all 
13273
74+ echo  Firewall rule created for port %PORT% .
13375goto  :eof 
0 commit comments