14ag

firewall.bat

Oct 30th, 2025
1,075
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 8.59 KB | Source Code | 0 0
  1. ::---------------------------------------------------------------------------------------------------
  2. :: commandline usage:  fire_wall.bat "path\to\program.exe" ^[allow^|block^] ^[in^|out^|all^]
  3. ::
  4. ::---------------------------------------------------------------------------------------------------
  5. @echo off
  6.  
  7. :: user variables
  8. setlocal
  9. set "extensions=.exe"
  10. :: callback script
  11. set "OTHER_SCRIPT="
  12.  
  13.  
  14.  
  15. :: functional variables
  16. set "loop=0"
  17. set "currentDirectory=%~dp0"
  18. set "_path=%~1"
  19. set "allow_block=%~2"
  20. set "in_out_all=%~3"
  21. set "empty_var="
  22.  
  23. :: validate callback script exists and is executable function goes here
  24.  
  25. :: Validate allow_block parameter
  26. call :validate "allow block" %allow_block%
  27. if "%validate%"=="false" set "allow_block="
  28.  
  29. :: Validate in_out_all parameter
  30. call :validate "in out all" %in_out_all%
  31. if "%validate%"=="false" set "in_out_all="
  32.  
  33. ::check if theres need to  show usage in single instance mode
  34. call :validate "'%in_out_all%' '%allow_block%' '%_path%'" '%empty_var%'
  35. if "%validate%"=="true" call :usage
  36.  
  37. ::file validation
  38. if "%_path%"=="" (
  39.     set "loop=1"
  40.     goto :getFile
  41. )
  42.  
  43. goto :file_or_folder0
  44.  
  45.  
  46. :getVars
  47. cls
  48. :: sets loop to happen if drag and drop is not happening
  49. set "loop=1"
  50. set "allow_block="
  51. set "in_out_all="
  52.  
  53.  
  54. :getFile
  55. call :info enter the file or folder to be processed here or
  56. call :info Press Enter to process all files with the extensions "%extensions%" in the current directory
  57. set "_path="
  58. set /p "_path=::"
  59. if not defined _path (
  60.     set "_path=%currentDirectory:"=%"
  61. ) else if defined _path (
  62.         set "_path=%_path:"=%"
  63.     )
  64.  
  65.  
  66. :file_or_folder0
  67. set "workingDirectory="
  68. set "file="
  69. call :file_or_folder "%_path%"
  70. if "%file_or_folder%"=="folder" (
  71.     set "workingDirectory="%_path%""
  72.     goto :directory_processing
  73. ) else if "%file_or_folder%"=="file" (
  74.     set "file="%_path%""
  75.     goto :fileProcessing
  76. ) else if "%file_or_folder%"=="" (
  77.     call :error "...%_path:~-10%" not found
  78.     goto :getFile
  79. ) else goto :getfile
  80.  
  81.  
  82. :directory_processing
  83. cls
  84. call :info "%workingDirectory%" in use
  85. set "workingDirectory=%workingDirectory:"=%"
  86. if "%workingDirectory:~-1%"=="\" (
  87.     set "workingDirectory=%workingDirectory:~0,-1%"
  88. )
  89.  
  90. :: check for compatible files
  91. cd %workingDirectory%
  92. set "found_files=0"
  93. for %%j in (%extensions%) do (
  94.     dir /b *%%j 2>nul | find "." >nul && set /a "found_files+=1"
  95. )
  96. if %found_files% equ 0 (
  97.     call :error No compatible files found in "...%workingDirectory:~-10%"
  98.     pause
  99.     cls
  100.     goto :getFile
  101. )
  102.  
  103. if not defined in_out_all call :in_out_all
  104. if not defined allow_block call :allow_block
  105. cls
  106. call :info the following files will be %allow_block%ed:
  107. for %%j in (%extensions%) do (
  108.     dir /b *%%j
  109. )
  110.  
  111. setlocal enabledelayedexpansion
  112. :: confirm install all files in the current directory
  113. echo.
  114. call :reset_choice
  115. CHOICE /C yn /N /M "\\\\\\\\ continue? [Y]es, [N]o ///////////"
  116. if %errorlevel% equ 2 (
  117.     cls
  118.     goto :getVars
  119. ) else if %errorlevel% equ 1 (
  120.     cls
  121.     set "ok_count=0"
  122.     set "all_count=0"
  123.     :: install each file in the current directory
  124.     for %%j in (%extensions%) do (
  125.         for /r "%workingDirectory%" %%i in (*%%j) do (
  126.             call :subRoutine "%%~i"
  127.             set /a "all_count+=1"
  128.             if errorlevel 0 set /a "ok_count+=1"
  129.         )   )
  130.        
  131.     :: show number of files installed successfully
  132.     call :info done. !ok_count!/!all_count! files processed.
  133.     endlocal
  134.     goto :end
  135. ) else exit /b 1
  136.  
  137.  
  138. :fileProcessing
  139. if not defined in_out_all call :in_out_all
  140. if not defined allow_block call :allow_block
  141. call :check %file% "%extensions%"
  142. if "%check%"=="fail" goto :getFile
  143. call :subRoutine %file%
  144. if errorlevel 0 (
  145.     call :info \\\\\\\ done ///////
  146.     ) else if not errorlevel 0 (
  147.         call :error /////// failed \\\\\\\
  148.         )
  149. goto :end
  150.  
  151.  
  152. ::---------------------------------------------------------------------------------------------------
  153. :subRoutine
  154. ::---------------------------------------------------------------------------------------------------
  155. set "x=%*"
  156. if "%in_out_all%" neq "all" (
  157.     call :main %x%
  158. ) else if "%in_out_all%" equ "all" (
  159.         set "in_out_all=in"
  160.         call :main %x%
  161.         set "in_out_all=out"
  162.         call :main %x%
  163.         set "in_out_all=all"
  164.     )
  165. exit /b %errorlevel%
  166.  
  167.  
  168. :main
  169. set "program_full_path=%*"
  170. for %%i in ("%program_full_path:"=%") do set "rule_name=%%~ni"
  171. netsh advfirewall firewall add rule name="%rule_name%" dir=%in_out_all% program="%program_full_path:"=%" profile=any action=%allow_block% enable=yes
  172. exit /b %errorlevel%
  173. ::---------------------------------------------------------------------------------------------------
  174.  
  175.  
  176. :: Display usage information and instructions
  177. :usage
  178. call :info "Usage: %~nx0 'path\to\program.exe' [allow|block] [in|out|all]"
  179. pause
  180. exit /b 0
  181.  
  182.  
  183. :in_out_all
  184. echo.
  185. call :selector "echo in & echo out & echo all"
  186. set "in_out_all=%selector%"
  187. exit /b 0
  188.  
  189.  
  190. :allow_block
  191. echo.
  192. call :selector "echo allow & echo block"
  193. set "allow_block=%selector%"
  194. exit /b 0
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208. :::::::::::::::::::::::::::::::::::::::::::helper functions::::::::::::::::::::::::::::::::::::::::::::::
  209.  
  210. :: reset errorlevel for correct choice
  211. :reset_choice
  212. exit /b 0
  213.  
  214.  
  215. :: error handling
  216. :error
  217. Echo 1n| CHOICE /N >nul 2>&1 & rem BEL
  218. echo error: %*
  219. pause
  220. exit /b 1
  221.  
  222.  
  223. :: info handling
  224. :info
  225. echo.
  226. echo info: %*
  227. exit /b 0
  228.  
  229.  
  230. :: call :truncate_str file.name extension
  231. :: returns extension of file.name in variable [truncate_str]
  232. :: file.name is the name of the file with extension
  233. :: extension is the extension to be truncated
  234. :truncate_str
  235. set "truncate_str="
  236. setlocal enabledelayedexpansion
  237. set "control_extension=%1"
  238. set "filename=%2"
  239. for /L %%a in (1,1,10) do (
  240.     if "!control_extension:~%%a!"=="" (
  241.         for /f "tokens=1" %%b in ("-%%a") do (
  242.             for /f "tokens=1" %%c in ("!filename:~%%b!") do (
  243.                 endlocal & set "truncate_str=%%c"
  244.             )   )   )   ) >nul 2>&1
  245. exit /b 0
  246.  
  247.  
  248. ::call :file_or_folder file_or_folder
  249. :: returns "file" or "folder" in variable [file_or_folder]
  250. :: file_or_folder is the path to the file or folder
  251. :file_or_folder
  252. set "file_or_folder="
  253. setlocal enabledelayedexpansion
  254. set "b=%1"
  255. set "b=%b:"=%"
  256. if exist "%b%" (
  257.     for %%I in ("%b%") do (
  258.         set "attrs=%%~aI"
  259.         REM Check if the first attribute is 'd' (directory)
  260.         if "!attrs:~0,1!" == "d" (
  261.             endlocal & set "file_or_folder=folder"
  262.         ) else (
  263.             endlocal & set "file_or_folder=file"
  264.         )   )
  265. )
  266. exit /b 0
  267.  
  268.  
  269. :: call :validate "control" %items_to_test%
  270. :: returns true or false in variable [validate]
  271. :: control is a string of items separated by spaces
  272. :validate
  273. set "validate="
  274. set "control=%1"
  275. set "items_to_test=%2"
  276. set "items=0"
  277. set "count=0"
  278. for %%i in (%control:"=%) do (
  279.     if not "%items_to_test%"=="%%i" set /a count+=1
  280.     set /a items+=1
  281. )
  282. if "%count%" geq "%items%" (
  283.     set "validate=false"
  284. ) else (
  285.     set "validate=true"
  286. )
  287. exit /b 0
  288.  
  289.  
  290. :: call :selector "[command that outputs list eg echo a & echo b & echo c]"
  291. :: & is just a command separator, while && is a conditional operator
  292. :selector
  293. echo.
  294. set "selector="
  295. setlocal enabledelayedexpansion
  296. set command=%* >nul
  297. set "i=0"
  298. set "choicelist="
  299. :: Loop through a list, act on each line
  300. for /f "eol=L tokens=1" %%a in ('!command!') do (
  301.     if errorlevel 1 (
  302.         echo Error: Failed to execute command: !command!
  303.         endlocal & exit /b 1
  304.     )
  305.     set /a i+=1
  306.     :: Create dynamic variable names (_1, _2, etc.)
  307.     for %%b in (_!i!) do (
  308.         set "%%b=%%a"
  309.         set "choicelist=!choicelist!!i!"
  310.         echo !i!. %%a
  311.     )   )
  312.  
  313. call :reset_choice
  314. choice /c %choicelist% /n /m "pick option btn %choicelist:~0,1% and %choicelist:~-1,1% ::"
  315. for /L %%c in (%choicelist:~-1%,-1,%choicelist:~0,1%) do (
  316.     if errorlevel %%c (
  317.     for %%d in (!_%%c!) do (
  318.             endlocal & set "selector=%%d"
  319.             goto :break
  320.     )   )   )
  321. :break
  322. exit /b 0
  323.  
  324.  
  325. ::tests to find out if filename [%1] has any of these extensions [%2]
  326. :check
  327. set "check="
  328. set "filename=%1"
  329. set "extensions=%2"
  330. set "filename=%filename:"=%"
  331. set "extensions=%extensions:"=%"
  332. setlocal enabledelayedexpansion
  333. :: verify file existence & validate its type
  334. if exist "%filename%" (
  335.     ::this loops thru each extension
  336.     for %%k in ("%filename%") do (
  337.         for %%j in (%extensions%) do (
  338.             call :truncate_str %%j %%~nxk
  339.             if /i not "%%j"=="!truncate_str!" (
  340.                 call :error not a supported file.
  341.                 endlocal & set "check=fail"
  342.                 ) else (
  343.                     endlocal & set "check=pass"
  344.                 )   )   )
  345. ) else if not exist "%filename%" (
  346.     call :error "%filename%" not found
  347.     endlocal & set "check=fail"
  348. )
  349. exit /b 0
  350.  
  351.  
  352. :: loops if drag and drop is not happening
  353. :end
  354. if "%loop%"=="1" (
  355.     pause
  356.     cls
  357.     goto getVars
  358. ) else (
  359.     endlocal & exit /b %errorlevel%
  360. )
  361.  
Tags: bat cmd
Advertisement
Add Comment
Please, Sign In to add comment