Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 9.45 KB | None | 0 0
  1. :: Script by Dserv
  2.  
  3. @echo off
  4.  
  5. ::check if run as admin
  6. openfiles > NUL 2>&1
  7. if %ERRORLEVEL% EQU 1 echo You need to run this script as Administrator. & pause & exit
  8.  
  9. ::check processor architecture and set variable for program folder and openvpn-location.
  10. if not "%processor_architecture%"=="AMD64" echo This is a 64-bit installer. & pause & exit
  11.  
  12. ::check internet connection
  13. echo Checking internet connection...
  14. ping -4 www.google.se >nul
  15. if %ERRORLEVEL% EQU 1 cls & ping www.google.se >nul
  16. if %ERRORLEVEL% EQU 1 cls echo No internet connection & pause & exit
  17.  
  18. ::Set tempdir
  19. set tempdir=%userprofile%\desktop
  20. cd %tempdir%
  21.  
  22. ::Set paths and filenames and service variables.
  23. set openvpnpath=https://swupdate.openvpn.org/community/releases/openvpn-install-2.3.10-I604-x86_64.exe
  24. set swedenudp=https://files.ovpn.se/windows/ovpn-se.ovpn
  25. set swedentcp=https://files.ovpn.se/windows/ovpn-se-tcp.ovpn
  26. set germanyudp=https://files.ovpn.se/windows/ovpn-de.ovpn
  27. set germanytcp=https://files.ovpn.se/windows/ovpn-de-tcp.ovpn
  28. set netherlandudp=https://files.ovpn.se/windows/ovpn-nl.ovpn
  29. set netherlandtcp=https://files.ovpn.se/windows/ovpn-nl-tcp.ovpn
  30. set canadaudp=https://files.ovpn.se/windows/ovpn-ca.ovpn
  31. set canadatcp=https://files.ovpn.se/windows/ovpn-ca-tcp.ovpn
  32. set service_run=SC Start "Openvpnservice"
  33. set service_stop=SC Stop "Openvpnservice"
  34. set service_auto=SC Config "Openvpnservice" start= AUTO
  35.  
  36. ::Choices are connected to installopenvpn (keep in mind when adding choices betweeen 1-3.)
  37. :INSTALLMENU
  38. ::Only for menu verification.
  39. set instp=if exist "%programfiles%\openvpn\bin\openvpn.exe" >nul
  40. cls
  41. echo OpenVpn version: %openvpnpath%
  42. echo.
  43. echo Note: This script relies on the availability of Openvpn version 2.3.10.
  44. echo If installation process (application install or configuration change)
  45. echo takes more then 5 minutes terminate script, and contact Dserv for a new version.
  46. echo.
  47.  
  48. :installmenu_ws
  49. echo MENU:
  50. echo [1] INSTALL OPENVPN
  51. %instp% echo [2] CHANGE CONNECTION CONFIG
  52. %instp% echo [3] CHANGE PASSWORD
  53. %instp% echo [4] STOP SERVICE
  54. %instp% echo [5] START SERVICE
  55. %instp% echo [6] KILLSWITCH
  56. echo [Q] EXIT
  57. echo ==========================
  58.  
  59. echo Enter selection and press ENTER key:
  60. set /P menuchoice=
  61. if /I "%menuchoice%" EQU "1" goto :installopenvpn
  62. if /I "%menuchoice%" EQU "2" goto :changeconfig
  63. if /I "%menuchoice%" EQU "3" goto :changepass
  64. if /I "%menuchoice%" EQU "4" goto :stop_sc
  65. if /I "%menuchoice%" EQU "5" goto :start_sc
  66. if /I "%menuchoice%" EQU "6" goto :killswitch
  67. if /I "%menuchoice%" EQU "Q" exit
  68. echo Invalid selection.
  69. pause
  70. cls & goto :installmenu
  71.  
  72. :INSTALLOPENVPN
  73. cls
  74. IF EXIST "%programfiles%\openvpn\bin\openvpn.exe" echo OpenVpn already installed. & pause & goto :installmenu
  75. echo Downloading and installing OpenVpn, this can take a couple minutes...
  76. bitsadmin.exe /transfer "ovpndowload" /priority high %openvpnpath% "%tempdir%\openvpninstall.exe" >nul
  77. if %ERRORLEVEL% NEQ 0 cls & echo ERROR: Can't find file. & pause & goto :installmenu
  78. openvpninstall.exe /S /D=%programfiles%\openvpn
  79. del openvpninstall.exe
  80. del "%public%\desktop\openvpn gui.lnk"
  81. cls
  82.  
  83. :CHANGECONFIG
  84. cls
  85. if "%menuchoice%"=="2" IF NOT EXIST "%programfiles%\openvpn\bin\openvpn.exe" echo OpenVpn is not installed. & pause & goto :installmenu
  86. if "%menuchoice%"=="2" %service_stop% >nul
  87. IF EXIST "%programfiles%\openvpn\config\*.ovpn" del "%programfiles%\openvpn\config\*.ovpn" >nul
  88. echo [1] Sweden servers UDP
  89. echo [2] Sweden servers TCP
  90. echo [3] Germany servers UDP
  91. echo [4] Germany servers TCP
  92. echo [5] Netherland servers UDP
  93. echo [6] Netherland servers TCP
  94. echo [7] Canada servers UDP
  95. echo [8] Canada servers TCP
  96.  
  97. echo Select serverconfig and press ENTER key:
  98. set /P confchoice=
  99. if /I "%confchoice%" EQU "1" set addr=%swedenudp% & set addrfile=ovpn-se.ovpn >nul & goto :changeconfig_install
  100. if /I "%confchoice%" EQU "2" set addr=%swedentcp% & set addrfile=ovpn-se-tcp.ovpn >nul & goto :changeconfig_install
  101. if /I "%confchoice%" EQU "3" set addr=%germanyudp% & set addrfile=ovpn-de.ovpn >nul & goto :changeconfig_install
  102. if /I "%confchoice%" EQU "4" set addr=%germanytcp% & set addrfile=ovpn-de-tcp.ovpn >nul & goto :changeconfig_install
  103. if /I "%confchoice%" EQU "5" set addr=%netherlandudp% & set addrfile=ovpn-nl.ovpn >nul & goto :changeconfig_install
  104. if /I "%confchoice%" EQU "6" set addr=%netherlandtcp% & set addrfile=ovpn-nl-tcp.ovpn >nul & goto :changeconfig_install
  105. if /I "%confchoice%" EQU "7" set addr=%canadaudp% & set addrfile=ovpn-ca.ovpn >nul & goto :changeconfig_install
  106. if /I "%confchoice%" EQU "8" set addr=%canadatcp% & set addrfile=ovpn-ca-tcp.ovpn >nul & goto :changeconfig_install
  107. echo Invalid selection.
  108. pause
  109. cls & goto :changeconfig
  110. :changeconfig_install
  111. cls
  112. echo Installing server configuration wait...
  113. bitsadmin.exe /transfer "ovpndowload" %addr% "%tempdir%\%addrfile%" >nul
  114. if %ERRORLEVEL% NEQ 0 cls & echo ERROR: Can't find file. & pause & goto :installmenu
  115. icacls "*.ovpn" /setowner %username% /T /C >nul
  116.  
  117. setlocal enabledelayedexpansion
  118. for /f "tokens=1,* delims=¶" %%A in ( '"findstr /n ^^ %addrfile%"') do (
  119.    SET string=%%A
  120.    for /f "delims=: tokens=1,*" %%a in ("!string!") do set "string=%%b"
  121.    if  "!string!" == "" (
  122.        echo.>>config.tmp
  123.    ) else (
  124.       SET modified=!string:auth-user-pass=auth-user-pass auth.txt!
  125.       echo !modified! >> config.tmp
  126.   )
  127. )
  128. del %addrfile%
  129. rename config.tmp %addrfile%
  130. endlocal
  131.  
  132. copy %addrfile% "%programfiles%\OpenVPN\config" >nul
  133. del %addrfile% >nul
  134.  
  135. if "%menuchoice%"=="1" goto :changepass
  136. if "%menuchoice%"=="2" %service_run% >nul & cls & echo Config changed to %addr%
  137. pause
  138. goto :installmenu
  139.  
  140.  
  141. :CHANGEPASS
  142. cls
  143. if "%menuchoice%"=="3" IF NOT EXIST "%programfiles%\openvpn\bin\openvpn.exe" echo OpenVpn is not installed. & pause & goto :installmenu
  144. if "%menuchoice%"=="3" %service_stop% >nul
  145. set /p usern= Input your vpn username:
  146. set /p passw= input your vpn password:
  147. cls
  148. :changepass_yn
  149. echo Username:%usern% Password:%passw% is this correct? [Y] [N]
  150. set /P m=
  151. if /I "%m%" EQU "Y" goto :changepass_install
  152. if /I "%m%" EQU "N" goto :CHANGEPASS
  153. echo Invalid selection.
  154. pause
  155. cls & goto :changepass_yn
  156. :changepass_install
  157. cls
  158. cd %tempdir% >nul
  159. echo %usern%>auth.txt
  160. echo %passw%>>auth.txt
  161. icacls "auth.txt" /setowner %username% /T /C >nul
  162. copy auth.txt "%programfiles%\OpenVPN\config" >nul
  163. del auth.txt >nul
  164. if "%menuchoice%"=="1" %service_auto% >nul & %service_run% >nul & echo OpenVpn is installed and running.
  165. if "%menuchoice%"=="3" %service_run% >nul & echo Password changed.
  166. pause
  167. goto :installmenu
  168.  
  169. :STOP_SC
  170. cls
  171. IF NOT EXIST "%programfiles%\openvpn\bin\openvpn.exe" echo OpenVpn is not installed. & pause & goto :installmenu
  172. sc query "openvpnservice" | findstr /i "STOPPED" >nul
  173.   if %errorlevel%== 0 (
  174.   echo Service already stopped...
  175.   pause
  176.   goto :installmenu
  177. ) else (
  178.   echo not stopped >nul
  179. )
  180. %service_stop% >nul
  181. timeout 2 >nul
  182. :STOP_SC_CHECK
  183. sc query "openvpnservice" | findstr /i "STOPPED" >nul
  184.   if %errorlevel%== 1  (
  185.   timeout 5 >nul
  186.   goto :stop_sc_check
  187. ) else (
  188.   echo Service stopped...
  189.   pause
  190.   goto :installmenu
  191. )
  192.  
  193. :START_SC
  194. cls
  195. IF NOT EXIST "%programfiles%\openvpn\bin\openvpn.exe" echo OpenVpn is not installed. & pause & goto :installmenu
  196. sc query "openvpnservice" | findstr /i "RUNNING" >nul
  197.   if %errorlevel%== 0 (
  198.   echo Service already running...
  199.   pause
  200.   goto :installmenu
  201. ) else (
  202.   echo No running >nul
  203. )
  204. %service_run% >nul
  205. timeout 2 >nul
  206. :START_SC_CHECK
  207. sc query "openvpnservice" | findstr /i "RUNNING" >nul
  208.   if %errorlevel%== 1  (
  209.   timeout 5 >nul
  210.   goto :start_sc_check
  211. ) else (
  212.   echo Service started...
  213.   pause
  214.   goto :installmenu
  215. )
  216.  
  217. :KILLSWITCH
  218. cls
  219. echo This will set your network connection to private if it's public or domain.
  220. echo After that install two firewall rules.
  221. echo which prevent your torrent client to communicate as long as your vpn connection is lost.
  222. echo.
  223. echo Do you want to install these rules? [Y] [N]
  224. set /P killswitchyesno=
  225. if /I "%killswitchyesno%" EQU "Y" goto :killswitch_install
  226. if /I "%killswitchyesno%" EQU "N" goto :installmenu
  227. echo Invalid selection.
  228. pause
  229. cls & goto :killswitch
  230. :killswitch_install
  231. cls
  232. set /p torrentlocation= Input path: (example: C:\Program Files (x86)\uTorrent\uTorrent.exe)
  233. cls
  234. if not exist "%torrentlocation%" echo Can't find torrentclient. & pause & cls & goto :killswitch
  235.  
  236. for /f "delims=" %%a in ('reg query "HKEY_LOCAL_MACHINE\software\microsoft\windows nt\currentversion\networklist\Profiles" ^| FIND "{"') do set var=%%~nxa
  237.  
  238. reg query "HKEY_LOCAL_MACHINE\software\microsoft\windows nt\currentversion\networklist\Profiles\%var%" /v Category | find "0x1">nul
  239.  
  240. IF %ERRORLEVEL%== 0 (
  241.     goto :torrent_installrules
  242. ) else (
  243. for /f "delims=" %%a in ('reg query "HKEY_LOCAL_MACHINE\software\microsoft\windows nt\currentversion\networklist\Profiles" ^| FIND "{"') do set var=%%~nxa
  244. Reg Add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Profiles\%var%" /V Category /t REG_DWORD /d 1 /f >nul
  245. )
  246.  
  247. :torrent_installrules
  248. cls
  249. netsh advfirewall firewall show rule name="Private torrent (IN)" >nul
  250. if %errorlevel%== 0 (
  251.     echo Rules already installed.
  252. ) ELSE (
  253.     netsh advfirewall firewall add rule name="Private torrent (IN)" dir=in action=block program="%Torrentlocation%" profile=private,domain >nul
  254.     netsh advfirewall firewall add rule name="Private torrent (OUT)" dir=out action=block program="%Torrentlocation%" profile=private,domain >nul
  255.     echo Rules installed.
  256. )
  257. pause
  258. cls
  259. goto :installmenu
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement