Advertisement
Guest User

Untitled

a guest
Apr 6th, 2017
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 9.44 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. set /P menuchoice=Enter selection and press ENTER key:
  60. if /I "%menuchoice%" EQU "1" goto :installopenvpn
  61. if /I "%menuchoice%" EQU "2" goto :changeconfig
  62. if /I "%menuchoice%" EQU "3" goto :changepass
  63. if /I "%menuchoice%" EQU "4" goto :stop_sc
  64. if /I "%menuchoice%" EQU "5" goto :start_sc
  65. if /I "%menuchoice%" EQU "6" goto :killswitch
  66. if /I "%menuchoice%" EQU "Q" exit
  67. echo Invalid selection.
  68. pause
  69. cls & goto :installmenu
  70.  
  71. :INSTALLOPENVPN
  72. cls
  73. IF EXIST "%programfiles%\openvpn\bin\openvpn.exe" echo OpenVpn already installed. & pause & goto :installmenu
  74. echo Downloading and installing OpenVpn, this can take a couple minutes...
  75. bitsadmin.exe /transfer "ovpndowload" /priority high %openvpnpath% "%tempdir%\openvpninstall.exe" >nul
  76. if %ERRORLEVEL% NEQ 0 cls & echo ERROR: Can't find file. & pause & goto :installmenu
  77. openvpninstall.exe /S /D=%programfiles%\openvpn
  78. del openvpninstall.exe
  79. del "%public%\desktop\openvpn gui.lnk"
  80. cls
  81.  
  82. :CHANGECONFIG
  83. cls
  84. if "%menuchoice%"=="2" IF NOT EXIST "%programfiles%\openvpn\bin\openvpn.exe" echo OpenVpn is not installed. & pause & goto :installmenu
  85. if "%menuchoice%"=="2" %service_stop% >nul
  86. IF EXIST "%programfiles%\openvpn\config\*.ovpn" del "%programfiles%\openvpn\config\*.ovpn" >nul
  87. echo [1] Sweden servers UDP
  88. echo [2] Sweden servers TCP
  89. echo [3] Germany servers UDP
  90. echo [4] Germany servers TCP
  91. echo [5] Netherland servers UDP
  92. echo [6] Netherland servers TCP
  93. echo [7] Canada servers UDP
  94. echo [8] Canada servers TCP
  95.  
  96. echo Select serverconfig and press ENTER key:
  97. set /P confchoice=
  98. if /I "%confchoice%" EQU "1" set addr=%swedenudp% & set addrfile=ovpn-se.ovpn >nul & goto :changeconfig_install
  99. if /I "%confchoice%" EQU "2" set addr=%swedentcp% & set addrfile=ovpn-se-tcp.ovpn >nul & goto :changeconfig_install
  100. if /I "%confchoice%" EQU "3" set addr=%germanyudp% & set addrfile=ovpn-de.ovpn >nul & goto :changeconfig_install
  101. if /I "%confchoice%" EQU "4" set addr=%germanytcp% & set addrfile=ovpn-de-tcp.ovpn >nul & goto :changeconfig_install
  102. if /I "%confchoice%" EQU "5" set addr=%netherlandudp% & set addrfile=ovpn-nl.ovpn >nul & goto :changeconfig_install
  103. if /I "%confchoice%" EQU "6" set addr=%netherlandtcp% & set addrfile=ovpn-nl-tcp.ovpn >nul & goto :changeconfig_install
  104. if /I "%confchoice%" EQU "7" set addr=%canadaudp% & set addrfile=ovpn-ca.ovpn >nul & goto :changeconfig_install
  105. if /I "%confchoice%" EQU "8" set addr=%canadatcp% & set addrfile=ovpn-ca-tcp.ovpn >nul & goto :changeconfig_install
  106. echo Invalid selection.
  107. pause
  108. cls & goto :changeconfig
  109. :changeconfig_install
  110. cls
  111. echo Installing server configuration wait...
  112. bitsadmin.exe /transfer "ovpndowload" %addr% "%tempdir%\%addrfile%" >nul
  113. if %ERRORLEVEL% NEQ 0 cls & echo ERROR: Can't find file. & pause & goto :installmenu
  114. icacls "*.ovpn" /setowner %username% /T /C >nul
  115.  
  116. setlocal enabledelayedexpansion
  117. for /f "tokens=1,* delims=¶" %%A in ( '"findstr /n ^^ %addrfile%"') do (
  118.    SET string=%%A
  119.    for /f "delims=: tokens=1,*" %%a in ("!string!") do set "string=%%b"
  120.    if  "!string!" == "" (
  121.        echo.>>config.tmp
  122.    ) else (
  123.       SET modified=!string:auth-user-pass=auth-user-pass auth.txt!
  124.       echo !modified! >> config.tmp
  125.   )
  126. )
  127. del %addrfile%
  128. rename config.tmp %addrfile%
  129. endlocal
  130.  
  131. copy %addrfile% "%programfiles%\OpenVPN\config" >nul
  132. del %addrfile% >nul
  133.  
  134. if "%menuchoice%"=="1" goto :changepass
  135. if "%menuchoice%"=="2" %service_run% >nul & cls & echo Config changed to %addr%
  136. pause
  137. goto :installmenu
  138.  
  139.  
  140. :CHANGEPASS
  141. cls
  142. if "%menuchoice%"=="3" IF NOT EXIST "%programfiles%\openvpn\bin\openvpn.exe" echo OpenVpn is not installed. & pause & goto :installmenu
  143. if "%menuchoice%"=="3" %service_stop% >nul
  144. set /p usern= Input your vpn username:
  145. set /p passw= input your vpn password:
  146. cls
  147. :changepass_yn
  148. echo Username:%usern% Password:%passw% is this correct? [Y] [N]
  149. set /P m=
  150. if /I "%m%" EQU "Y" goto :changepass_install
  151. if /I "%m%" EQU "N" goto :CHANGEPASS
  152. echo Invalid selection.
  153. pause
  154. cls & goto :changepass_yn
  155. :changepass_install
  156. cls
  157. cd %tempdir% >nul
  158. echo %usern%>auth.txt
  159. echo %passw%>>auth.txt
  160. icacls "auth.txt" /setowner %username% /T /C >nul
  161. copy auth.txt "%programfiles%\OpenVPN\config" >nul
  162. del auth.txt >nul
  163. if "%menuchoice%"=="1" %service_auto% >nul & %service_run% >nul & echo OpenVpn is installed and running.
  164. if "%menuchoice%"=="3" %service_run% >nul & echo Password changed.
  165. pause
  166. goto :installmenu
  167.  
  168. :STOP_SC
  169. cls
  170. IF NOT EXIST "%programfiles%\openvpn\bin\openvpn.exe" echo OpenVpn is not installed. & pause & goto :installmenu
  171. sc query "openvpnservice" | findstr /i "STOPPED" >nul
  172.   if %errorlevel%== 0 (
  173.   echo Service already stopped...
  174.   pause
  175.   goto :installmenu
  176. ) else (
  177.   echo not stopped >nul
  178. )
  179. %service_stop% >nul
  180. timeout 2 >nul
  181. :STOP_SC_CHECK
  182. sc query "openvpnservice" | findstr /i "STOPPED" >nul
  183.   if %errorlevel%== 1  (
  184.   timeout 5 >nul
  185.   goto :stop_sc_check
  186. ) else (
  187.   echo Service stopped...
  188.   pause
  189.   goto :installmenu
  190. )
  191.  
  192. :START_SC
  193. cls
  194. IF NOT EXIST "%programfiles%\openvpn\bin\openvpn.exe" echo OpenVpn is not installed. & pause & goto :installmenu
  195. sc query "openvpnservice" | findstr /i "RUNNING" >nul
  196.   if %errorlevel%== 0 (
  197.   echo Service already running...
  198.   pause
  199.   goto :installmenu
  200. ) else (
  201.   echo No running >nul
  202. )
  203. %service_run% >nul
  204. timeout 2 >nul
  205. :START_SC_CHECK
  206. sc query "openvpnservice" | findstr /i "RUNNING" >nul
  207.   if %errorlevel%== 1  (
  208.   timeout 5 >nul
  209.   goto :start_sc_check
  210. ) else (
  211.   echo Service started...
  212.   pause
  213.   goto :installmenu
  214. )
  215.  
  216. :KILLSWITCH
  217. cls
  218. echo This will set your network connection to private if it's public or domain.
  219. echo After that install two firewall rules.
  220. echo which prevent your torrent client to communicate as long as your vpn connection is lost.
  221. echo.
  222. echo Do you want to install these rules? [Y] [N]
  223. set /P killswitchyesno=
  224. if /I "%killswitchyesno%" EQU "Y" goto :killswitch_install
  225. if /I "%killswitchyesno%" EQU "N" goto :installmenu
  226. echo Invalid selection.
  227. pause
  228. cls & goto :killswitch
  229. :killswitch_install
  230. cls
  231. set /p torrentlocation= Input path: (example: C:\Program Files (x86)\uTorrent\uTorrent.exe)
  232. cls
  233. if not exist "%torrentlocation%" echo Can't find torrentclient. & pause & cls & goto :killswitch
  234.  
  235. for /f "delims=" %%a in ('reg query "HKEY_LOCAL_MACHINE\software\microsoft\windows nt\currentversion\networklist\Profiles" ^| FIND "{"') do set var=%%~nxa
  236.  
  237. reg query "HKEY_LOCAL_MACHINE\software\microsoft\windows nt\currentversion\networklist\Profiles\%var%" /v Category | find "0x1">nul
  238.  
  239. IF %ERRORLEVEL%== 0 (
  240.     goto :torrent_installrules
  241. ) else (
  242. for /f "delims=" %%a in ('reg query "HKEY_LOCAL_MACHINE\software\microsoft\windows nt\currentversion\networklist\Profiles" ^| FIND "{"') do set var=%%~nxa
  243. Reg Add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Profiles\%var%" /V Category /t REG_DWORD /d 1 /f >nul
  244. )
  245.  
  246. :torrent_installrules
  247. cls
  248. netsh advfirewall firewall show rule name="Private torrent (IN)" >nul
  249. if %errorlevel%== 0 (
  250.     echo Rules already installed.
  251. ) ELSE (
  252.     netsh advfirewall firewall add rule name="Private torrent (IN)" dir=in action=block program="%Torrentlocation%" profile=private,domain >nul
  253.     netsh advfirewall firewall add rule name="Private torrent (OUT)" dir=out action=block program="%Torrentlocation%" profile=private,domain >nul
  254.     echo Rules installed.
  255. )
  256. pause
  257. cls
  258. goto :installmenu
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement