-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWSL2PortMappingTool.bat
244 lines (201 loc) · 6.28 KB
/
WSL2PortMappingTool.bat
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
@echo off
setlocal enabledelayedexpansion
set "ruleNamePattern=Allowing LAN connections to port"
:: Check for administrative permissions
call :checkAdmin
:menu
cls
set "option="
echo.
echo Firewall Rules and Port Forwardings for WSL2
echo --------------------------------------------------
set "hasRules=0"
call :displayRules
echo.
echo 1) Create new rule and port forwarding
if "%hasRules%"=="1" echo 2) Delete an existing rule
echo 3) Display Firewall Rule for port
echo 4) Display all Firewall rules "%ruleNamePattern% *"
echo 5) Display all port forwardings
echo 0) Exit
echo.
set /p option="Choose an option: "
goto option-%option% 2>nul
goto menu
:option-0
exit
:option-1
call :createRule
goto menu
:option-2
call :deleteRule
goto menu
:option-3
call :displayFirewallRuleForPort
goto menu
:option-4
call :displayAllFirewallRuleNamePattern
goto menu
:option-5
call :displayPortForwardings
goto menu
:displayPortForwardings
netsh interface portproxy show all
echo.
pause
goto menu
:displayFirewallRuleForPort
set /p port="Enter the port number for which you want to see the firewall rules: "
if not defined port (
echo No port number provided.
pause
goto menu
)
echo.
echo Showing firewall rules for port %port%:
echo --------------------------------------------------
netsh advfirewall firewall show rule name="%ruleNamePattern% %port%"
echo.
pause
goto menu
:displayAllFirewallRuleNamePattern
netsh advfirewall firewall show rule name=all | find "%ruleNamePattern%"
echo.
pause
:checkAdmin
:: Check for administrative permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
if '%errorlevel%' NEQ '0' (
call :requestAdmin
) else (
call :gotAdmin
)
exit /b
:requestAdmin
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
set params = %*:"=""
echo UAC.ShellExecute "cmd.exe", "/c %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
exit
:gotAdmin
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
pushd "%CD%"
CD /D "%~dp0"
exit /b
:displayRules
:: Step 1: Create firewall rules ports list
set "portsList="
for /f "tokens=*" %%l in ('netsh advfirewall firewall show rule name^=all ^| find "%ruleNamePattern%"') do (
for %%p in (%%l) do set "portNum=%%p"
if not "!portNum!"=="" set "portsList=!portsList! !portNum!"
set "hasRules=1"
)
:: Paso 2: Find firewall rule port and matching port forwardings
for %%p in (%portsList%) do (
if not "%%p"=="" (
echo Firewall Rule: %ruleNamePattern% %%p
for /f "tokens=1,2,3,4" %%a in ('netsh interface portproxy show all ^| findstr /C:"%%p"') do (
echo %%a:%%b to %%c:%%d
)
echo.
)
)
exit /b
:ruleExists
setlocal
set "rulePort=%~1"
set "ruleExists=0"
for /f "tokens=*" %%a in ('netsh advfirewall firewall show rule name^=all ^| find "%ruleNamePattern% %rulePort%"') do (
set "ruleExists=1"
)
endlocal & set "ruleExists=%ruleExists%"
exit /b
:createRule
set defaultLocalPort=5000
set defaultWslPort=5000
set defaultListenIP=0.0.0.0
set defaultRemoteIP=any
:: Reset user input values
set "localPort="
set "wslPort="
set "listenIP="
set "remoteIP="
call :getUserInput "Enter the local port (default: %defaultLocalPort%): " localPort %defaultLocalPort%
call :getUserInput "Enter the WSL2 port (default: %defaultWslPort%): " wslPort %defaultWslPort%
call :getUserInput "Enter the listen IP (default: %defaultListenIP%): " listenIP %defaultListenIP%
call :getUserInput "Enter the remote IP or mask (default: %defaultRemoteIP%): " remoteIP %defaultRemoteIP%
:: If user pressed escape
if "%localPort%"=="ESC" goto menu
if "%wslPort%"=="ESC" goto menu
if "%listenIP%"=="ESC" goto menu
if "%remoteIP%"=="ESC" goto menu
call :ruleExists %localPort%
if "%ruleExists%"=="0" (
netsh advfirewall firewall add rule name="%ruleNamePattern% %localPort%" dir=in action=allow protocol=TCP localport=%localPort% remoteip=%remoteIP%
)
netsh interface portproxy add v4tov4 listenaddress=%listenIP% listenport=%localPort% connectaddress=localhost connectport=%wslPort%
goto menu
:deleteRule
cls
echo Existing Rules:
echo.
call :displayRules
echo.
echo 0) Return to main menu
echo.
set /p choice="Enter the port number you wish to delete or 0 to return: "
if "%choice%"=="" goto menu
if "%choice%"=="0" goto menu
:: Count how many forwardings are associated with the chosen port
set "count=0"
for /f "tokens=1,2" %%a in ('netsh interface portproxy show all ^| findstr /C:" %choice% "') do (
set /a count+=1
)
:: If there's only one forwarding, delete it
if "%count%"=="1" (
for /f "tokens=1,2" %%a in ('netsh interface portproxy show all ^| findstr /C:" %choice% "') do (
set detectedListenIP=%%a
set detectedPort=%%b
)
netsh interface portproxy delete v4tov4 listenaddress=!detectedListenIP! listenport=!detectedPort!
netsh advfirewall firewall delete rule name="%ruleNamePattern% %choice%"
goto menu
)
:: If there are multiple forwardings, ask the user which one to delete
echo.
echo Multiple forwardings detected for port %choice%. Choose one to delete:
set "index=1"
for /f "tokens=1,2" %%a in ('netsh interface portproxy show all ^| findstr /C:" %choice% "') do (
echo !index!^) %%a:%%b
set "listenIP!index!=%%a"
set "port!index!=%%b"
set /a index+=1
)
set allIndex=!index!
echo !allIndex!^) Delete all forwardings for port %choice%
echo.
set /p listenIPChoice="Enter the number of the forwarding you wish to delete or !allIndex! to delete all: "
if "%listenIPChoice%"=="!allIndex!" (
for /l %%i in (1,1,!allIndex!) do (
if defined listenIP%%i (
netsh interface portproxy delete v4tov4 listenaddress=!listenIP%%i! listenport=%choice%
)
)
netsh advfirewall firewall delete rule name="%ruleNamePattern% %choice%"
goto menu
) else (
set detectedListenIP=!listenIP%listenIPChoice%!
set detectedPort=!port%listenIPChoice%!
netsh interface portproxy delete v4tov4 listenaddress=!detectedListenIP! listenport=!detectedPort!
goto menu
)
:getUserInput
setlocal
set "promptText=%~1"
set "returnValue="
set "defaultVal=%~3"
set /p returnValue="%promptText%"
if not defined returnValue set returnValue=%defaultVal%
if "%returnValue%"=="" set returnValue=ESC
endlocal & set "%~2=%returnValue%"
exit /b