vpnro.sh 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  1. #!/bin/bash
  2. # OpenVPN road warrior installer for Debian, Ubuntu and CentOS
  3. # This script will work on Debian, Ubuntu, CentOS and probably other distros
  4. # of the same families, although no support is offered for them. It isn't
  5. # bulletproof but it will probably work if you simply want to setup a VPN on
  6. # your Debian/Ubuntu/CentOS box. It has been designed to be as unobtrusive and
  7. # universal as possible.
  8. ##############################service openvpn_tcp restart
  9. ##############################service openvpn_udp restart
  10. ###############################################################################################################
  11. # START_VARIABLE_SECTION
  12. # This section contains setup and variables
  13. ###############################################################################################################
  14. TCP_SERVICE_AND_CONFIG_NAME="openvpn_tcp"
  15. UDP_SERVICE_AND_CONFIG_NAME="openvpn_udp"
  16. if [[ "$USER" != 'root' ]]; then
  17. echo "Sorry, you need to run this as root"
  18. rm $0 > /dev/null 2>&1
  19. exit
  20. fi
  21. if [[ ! -e /dev/net/tun ]]; then
  22. echo "TUN/TAP is not available"
  23. rm $0 > /dev/null 2>&1
  24. exit
  25. fi
  26. if grep -qs "CentOS release 5" "/etc/redhat-release"; then
  27. echo "CentOS 5 is too old and not supported"
  28. rm $0 > /dev/null 2>&1
  29. exit
  30. fi
  31. if [[ -e /etc/debian_version ]]; then
  32. OS=debian
  33. RCLOCAL='/etc/rc.local'
  34. elif [[ -e /etc/centos-release || -e /etc/redhat-release ]]; then
  35. OS=centos
  36. RCLOCAL='/etc/rc.d/rc.local'
  37. # Needed for CentOS 7
  38. chmod +x /etc/rc.d/rc.local
  39. else
  40. echo "Looks like you aren't running this installer on a Debian, Ubuntu or CentOS system"
  41. rm $0 > /dev/null 2>&1
  42. exit
  43. fi
  44. ###############################################################################################################
  45. # END_VARIABLE_SECTION
  46. ###############################################################################################################
  47. newclient () {
  48. #sed -i 's/RANDFILE/#RANDFILE/g' /etc/openvpn/easy-rsa/openssl-1.0.cnf
  49. # This function is used to create udp client .ovpn file
  50. cp /etc/openvpn/client-common.txt ~/"$1.ovpn"
  51. echo "<ca>" >> ~/"$1.ovpn"
  52. cat /etc/openvpn/easy-rsa/pki/ca.crt >> ~/"$1.ovpn"
  53. echo "</ca>" >> ~/"$1.ovpn"
  54. echo "<cert>" >> ~/"$1.ovpn"
  55. cat /etc/openvpn/easy-rsa/pki/issued/"$1.crt" >> ~/"$1.ovpn"
  56. echo "</cert>" >> ~/"$1.ovpn"
  57. echo "<key>" >> ~/"$1.ovpn"
  58. cat /etc/openvpn/easy-rsa/pki/private/"$1.key" >> ~/"$1.ovpn"
  59. echo "</key>" >> ~/"$1.ovpn"
  60. if [ "$TLS" = "1" ]; then #check if TLS is selected to add a TLS static key
  61. echo "key-direction 1" >> ~/"$1.ovpn"
  62. echo "<tls-auth>" >> ~/"$1.ovpn"
  63. cat /etc/openvpn/easy-rsa/pki/private/ta.key >> ~/"$1.ovpn"
  64. echo "</tls-auth>" >> ~/"$1.ovpn"
  65. fi
  66. if [ $TLSNEW = 1 ]; then
  67. echo "--tls-version-min 1.2" >> ~/"$1.ovpn"
  68. fi
  69. }
  70. newclienttcp () {
  71. # This function is used to create tcp client .ovpn file
  72. cp /etc/openvpn/clienttcp-common.txt ~/"$1tcp.ovpn"
  73. echo "<ca>" >> ~/"$1tcp.ovpn"
  74. cat /etc/openvpn/easy-rsa/pki/ca.crt >> ~/"$1tcp.ovpn"
  75. echo "</ca>" >> ~/"$1tcp.ovpn"
  76. echo "<cert>" >> ~/"$1tcp.ovpn"
  77. cat /etc/openvpn/easy-rsa/pki/issued/"$1.crt" >> ~/"$1tcp.ovpn"
  78. echo "</cert>" >> ~/"$1tcp.ovpn"
  79. echo "<key>" >> ~/"$1tcp.ovpn"
  80. cat /etc/openvpn/easy-rsa/pki/private/"$1.key" >> ~/"$1tcp.ovpn"
  81. echo "</key>" >> ~/"$1tcp.ovpn"
  82. if [ "$TLS" = "1" ]; then #check if TLS is selected to add a TLS static key
  83. echo "key-direction 1" >> ~/"$1tcp.ovpn"
  84. echo "<tls-auth>" >> ~/"$1tcp.ovpn"
  85. cat /etc/openvpn/easy-rsa/pki/private/ta.key >> ~/"$1tcp.ovpn"
  86. echo "</tls-auth>" >> ~/"$1tcp.ovpn"
  87. fi
  88. if [ $TLSNEW = 1 ]; then
  89. echo "--tls-version-min 1.2" >> ~/"$1.ovpn"
  90. fi
  91. }
  92. function version_gt() { test "$(echo "$@" | tr " " "\n" | sort -V | head -n 1)" != "$1"; }
  93. # This function is used to compare installed openvpn and specific version
  94. # Try to get our IP from the system and fallback to the Internet.
  95. # I do this to make the script compatible with NATed servers (lowendspirit.com)
  96. # and to avoid getting an IPv6.
  97. IP=$(ip addr | grep 'inet' | grep -v inet6 | grep -vE '127\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | grep -o -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | head -1)
  98. if [[ "$IP" = "" ]]; then
  99. IP=$(wget -qO- ipv4.icanhazip.com)
  100. fi
  101. if [ -e /etc/openvpn/$UDP_SERVICE_AND_CONFIG_NAME.conf -o -e /etc/openvpn/$TCP_SERVICE_AND_CONFIG_NAME.conf ]; then #check if udp or tcp config file is present
  102. while :
  103. do
  104. clear
  105. echo "Looks like OpenVPN is already installed"
  106. echo ""
  107. echo "What do you want to do?"
  108. echo " 1) Add a cert for a new user"
  109. echo " 2) Revoke existing user cert"
  110. echo " 3) Remove OpenVPN"
  111. echo " 4) Exit"
  112. read -p "Select an option [1-4]: " option
  113. case $option in
  114. 1)
  115. echo ""
  116. echo "Tell me a name for the client cert"
  117. echo "Please, use one word only, no special characters"
  118. read -p "Client name: " -e -i client CLIENT
  119. cd /etc/openvpn/easy-rsa/
  120. ./easyrsa build-client-full "$CLIENT" nopass
  121. # Generates the custom client.ovpn
  122. if [[ -e /etc/openvpn/$UDP_SERVICE_AND_CONFIG_NAME.conf ]]; then
  123. TLS=0
  124. TLSNEW=0
  125. if [ -n "$(cat /etc/openvpn/$UDP_SERVICE_AND_CONFIG_NAME.conf | grep tls-auth)" ]; then #check if TLS is enabled in server config file so that static TLS key can be added to new client
  126. TLS=1
  127. fi
  128. if [ -n "$(cat /etc/openvpn/$UDP_SERVICE_AND_CONFIG_NAME.conf | grep "tls-version-min 1.2")" ]; then #check if TLS 1.2 is enabled in server config file so that static TLS key can be added to new client
  129. TLSNEW=1
  130. fi
  131. newclient "$CLIENT"
  132. echo "UDP client $CLIENT added, certs available at ~/$CLIENT.ovpn"
  133. fi
  134. #everything here is the same as above just for the tcp client
  135. if [[ -e /etc/openvpn/$TCP_SERVICE_AND_CONFIG_NAME.conf ]]; then
  136. TLS=0
  137. TLSNEW=0
  138. if [ -n "$(cat /etc/openvpn/$TCP_SERVICE_AND_CONFIG_NAME.conf | grep tls-auth)" ]; then
  139. TLS=1
  140. fi
  141. if [ -n "$(cat /etc/openvpn/$UDP_SERVICE_AND_CONFIG_NAME.conf | grep "tls-version-min 1.2")" ]; then
  142. TLSNEW=1
  143. fi
  144. newclienttcp "$CLIENT"
  145. echo "TCP client $CLIENT added, certs available at ~/${CLIENT}tcp.ovpn"
  146. fi
  147. echo ""
  148. exit
  149. ;;
  150. 2)
  151. # This option could be documented a bit better and maybe even be simplimplified
  152. # ...but what can I say, I want some sleep too
  153. NUMBEROFCLIENTS=$(tail -n +2 /etc/openvpn/easy-rsa/pki/index.txt | grep -c "^V")
  154. if [[ "$NUMBEROFCLIENTS" = '0' ]]; then
  155. echo ""
  156. echo "You have no existing clients!"
  157. exit
  158. fi
  159. echo ""
  160. echo "Select the existing client certificate you want to revoke"
  161. tail -n +2 /etc/openvpn/easy-rsa/pki/index.txt | grep "^V" | cut -d '=' -f 2 | nl -s ') '
  162. if [[ "$NUMBEROFCLIENTS" = '1' ]]; then
  163. read -p "Select one client [1]: " CLIENTNUMBER
  164. else
  165. read -p "Select one client [1-$NUMBEROFCLIENTS]: " CLIENTNUMBER
  166. fi
  167. CLIENT=$(tail -n +2 /etc/openvpn/easy-rsa/pki/index.txt | grep "^V" | cut -d '=' -f 2 | sed -n "$CLIENTNUMBER"p)
  168. cd /etc/openvpn/easy-rsa/
  169. ./easyrsa --batch revoke "$CLIENT"
  170. ./easyrsa gen-crl
  171. rm -rf "pki/reqs/$CLIENT.req"
  172. rm -rf "pki/private/$CLIENT.key"
  173. rm -rf "pki/issued/$CLIENT.crt"
  174. # And restart
  175. if pgrep systemd-journal; then
  176. systemctl restart openvpn
  177. else
  178. if [[ "$OS" = 'debian' ]]; then
  179. /etc/init.d/openvpn restart
  180. else
  181. service openvpn restart
  182. fi
  183. fi
  184. echo ""
  185. echo "Certificate for client \"$CLIENT\" revoked"
  186. exit
  187. ;;
  188. ###############################################################################################################
  189. # START_OPENVPN_REMOVAL_SECTION
  190. # This section contains to remove openvpn as installed by this script
  191. ###############################################################################################################
  192. 3)
  193. echo ""
  194. read -p "Do you really want to remove OpenVPN? [y/n]: " -e -i n REMOVE
  195. if [[ "$REMOVE" = 'y' ]]; then
  196. if [[ -e /etc/openvpn/$UDP_SERVICE_AND_CONFIG_NAME.conf ]]; then #removal of udp firewall rules
  197. PORT=$(grep '^port ' /etc/openvpn/$UDP_SERVICE_AND_CONFIG_NAME.conf | cut -d " " -f 2)
  198. iptables -L | grep -q REJECT
  199. sed -i "/iptables -I INPUT -p udp --dport $PORT -j ACCEPT/d" $RCLOCAL
  200. sed -i "/iptables -I FORWARD -s 10.8.0.0\/24 -j ACCEPT/d" $RCLOCAL
  201. sed -i "/iptables -I FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT/d" $RCLOCAL
  202. sed -i '/iptables -t nat -A POSTROUTING -s 10.8.0.0\/24 -j SNAT --to /d' $RCLOCAL
  203. fi
  204. if [[ -e /etc/openvpn/$TCP_SERVICE_AND_CONFIG_NAME.conf ]]; then #removal of tcp firewall rules
  205. PORT=$(grep '^port ' /etc/openvpn/$TCP_SERVICE_AND_CONFIG_NAME.conf | cut -d " " -f 2)
  206. iptables -L | grep -q REJECT
  207. sed -i "/iptables -I INPUT -p tcp --dport $PORT -j ACCEPT/d" $RCLOCAL
  208. sed -i "/iptables -I FORWARD -s 10.9.0.0\/24 -j ACCEPT/d" $RCLOCAL
  209. sed -i "/iptables -I FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT/d" $RCLOCAL
  210. sed -i '/iptables -t nat -A POSTROUTING -s 10.9.0.0\/24 -j SNAT --to /d' $RCLOCAL
  211. fi
  212. sed -i '/iptables -t nat -A PREROUTING -p tcp -i tun+ --dport 80 -j REDIRECT --to-port 8080/d' $RCLOCAL #Remove HAVP proxy
  213. iptables -t nat -D PREROUTING -i tun+ -p tcp --dport 80 -j REDIRECT --to-port 8080
  214. apt-get remove --purge -y openvpn openvpn-blacklist unbound clamav clamav-daemon privoxy havp
  215. rm -rf /etc/openvpn
  216. rm -rf /usr/share/doc/openvpn*
  217. if pgrep systemd-journal; then
  218. sudo systemctl disable $UDP_SERVICE_AND_CONFIG_NAME.service
  219. sudo systemctl disable $TCP_SERVICE_AND_CONFIG_NAME.service
  220. fi
  221. rm -rf /etc/systemd/system/$UDP_SERVICE_AND_CONFIG_NAME.service
  222. rm -rf /etc/systemd/system/$TCP_SERVICE_AND_CONFIG_NAME.service
  223. echo ""
  224. echo "OpenVPN removed!"
  225. fi
  226. exit
  227. ;;
  228. ###############################################################################################################
  229. # END_OPENVPN_REMOVAL_SECTION
  230. ###############################################################################################################
  231. 4) exit;;
  232. esac
  233. done
  234. else
  235. clear
  236. echo 'Welcome to this quick OpenVPN "road warrior" installer'
  237. echo ""
  238. # OpenVPN setup and first user creation
  239. echo "I need to ask you a few questions before starting the setup"
  240. echo "You can leave the default options and just press enter if you are ok with them"
  241. echo ""
  242. echo "First I need to know the IPv4 address of the network interface you want OpenVPN"
  243. echo "listening to."
  244. read -p "IP address: " -e -i $IP IP
  245. echo ""
  246. while :
  247. do
  248. while :
  249. do
  250. clear
  251. read -p "Do you want to run a UDP server [y/n]: " -e -i y UDP
  252. case $UDP in
  253. y) UDP=1
  254. break ;;
  255. n) UDP=0
  256. break ;;
  257. esac
  258. done
  259. while :
  260. do
  261. clear
  262. echo "***************************************************"
  263. echo "* !!!!!NB!!!!! *"
  264. echo "* *"
  265. echo "* Here be dragons!!! If you're using this to get *"
  266. echo "* past firewalls then go ahead and choose *y*, *"
  267. echo "* but please read and understand *"
  268. echo "* *"
  269. echo "* http://sites.inka.de/bigred/devel/tcp-tcp.html *"
  270. echo "* http://tinyurl.com/34qzu5z *"
  271. echo "***************************************************"
  272. echo ""
  273. read -p "Do you want to run a TCP server [y/n]: " -e -i n TCP
  274. case $TCP in
  275. y) TCP=1
  276. break ;;
  277. n) TCP=0
  278. break ;;
  279. esac
  280. done
  281. if [ "$UDP" = 1 -o "$TCP" = 1 ]; then
  282. break
  283. fi
  284. done
  285. if [ "$UDP" = 1 ]; then
  286. clear
  287. read -p "What UDP port do you want to run OpenVPN on?: " -e -i 1194 PORT
  288. fi
  289. if [ "$TCP" = 1 ]; then
  290. clear
  291. read -p "What TCP port do you want to run OpenVPN on?: " -e -i 443 PORTTCP
  292. fi
  293. while :
  294. do
  295. clear
  296. echo "What size do you want your key to be? :"
  297. echo " 1) 2048bits"
  298. echo " 2) 4096bits"
  299. echo ""
  300. read -p "Key Size [1-2]: " -e -i 1 KEYSIZE
  301. case $KEYSIZE in
  302. 1)
  303. KEYSIZE=2048
  304. break
  305. ;;
  306. 2)
  307. KEYSIZE=4096
  308. break
  309. ;;
  310. esac
  311. done
  312. while :
  313. do
  314. clear
  315. echo "What size do you want your SHA digest to be? :"
  316. echo " 1) 256bits"
  317. echo " 2) 512bits"
  318. echo ""
  319. read -p "Digest Size [1-2]: " -e -i 1 DIGEST
  320. case $DIGEST in
  321. 1)
  322. DIGEST=SHA256
  323. break
  324. ;;
  325. 2)
  326. DIGEST=SHA512
  327. break
  328. ;;
  329. esac
  330. done
  331. AES=0
  332. grep -q aes /proc/cpuinfo #Check for AES-NI availability
  333. if [[ "$?" -eq 0 ]]; then
  334. AES=1
  335. fi
  336. while :
  337. do
  338. clear
  339. if [[ "$AES" -eq 1 ]]; then
  340. echo "Your CPU supports AES-NI instruction set."
  341. echo "It enables faster AES encryption/decryption."
  342. echo "Choosing AES will decrease CPU usage."
  343. fi
  344. echo "Which cipher do you want to use? :"
  345. echo " 1) AES-256-CBC"
  346. echo " 2) AES-128-CBC"
  347. echo " 3) BF-CBC"
  348. echo " 4) CAMELLIA-256-CBC"
  349. echo " 5) CAMELLIA-128-CBC"
  350. echo ""
  351. read -p "Cipher [1-5]: " -e -i 1 CIPHER
  352. case $CIPHER in
  353. 1) CIPHER=AES-256-CBC
  354. break ;;
  355. 2) CIPHER=AES-128-CBC
  356. break ;;
  357. 3) CIPHER=BF-CBC
  358. break ;;
  359. 4) CIPHER=CAMELLIA-256-CBC
  360. break ;;
  361. 5) CIPHER=CAMELLIA-128-CBC
  362. break ;;
  363. esac
  364. done
  365. while :
  366. do
  367. clear
  368. read -p "Do you want to use additional TLS authentication [y/n]: " -e -i y TLS
  369. case $TLS in
  370. y) TLS=1
  371. break ;;
  372. n) TLS=0
  373. break ;;
  374. esac
  375. done
  376. while :
  377. do
  378. clear
  379. echo "Do you want to enable internal networking for the VPN(iptables only)?"
  380. echo "This can allow VPN clients to communicate between them"
  381. read -p "Allow internal networking [y/n]: " -e -i y INTERNALNETWORK
  382. case $INTERNALNETWORK in
  383. y) INTERNALNETWORK=1
  384. break ;;
  385. n) INTERNALNETWORK=0
  386. break ;;
  387. esac
  388. done
  389. while :
  390. do
  391. clear
  392. echo "Do you want to create self hosted DNS resolver ?"
  393. echo "This resolver will be only accessible through VPN to prevent"
  394. echo "your server to be used for DNS amplification attack"
  395. read -p "Create DNS resolver [y/n]: " -e -i n DNSRESOLVER
  396. case $DNSRESOLVER in
  397. y) DNSRESOLVER=1
  398. break;;
  399. n) DNSRESOLVER=0
  400. break;;
  401. esac
  402. done
  403. while :
  404. do
  405. clear
  406. echo "Do you want to setup Privoxy+ClamAV+HAVP?"
  407. echo "Privoxy will be used to block ads."
  408. echo "ClamAV+HAVP will be used to scan all of your web traffic for viruses."
  409. echo "This will only work with unencrypted traffic."
  410. echo "You should have at least 1GB RAM for this option."
  411. read -p "[y/n]: " -e -i n ANTIVIR
  412. case $ANTIVIR in
  413. y) ANTIVIR=1
  414. break;;
  415. n) ANTIVIR=0
  416. break;;
  417. esac
  418. done
  419. clear
  420. if [ "$DNSRESOLVER" = 0 ]; then #If user wants to use his own DNS resolver this selection is skipped
  421. echo "What DNS do you want to use with the VPN?"
  422. echo " 1) Current system resolvers"
  423. echo " 2) OpenDNS"
  424. echo " 3) Verisign"
  425. echo " 4) NTT"
  426. echo " 5) Hurricane Electric"
  427. echo " 6) Google"
  428. echo ""
  429. read -p "DNS [1-6]: " -e -i 1 DNS
  430. fi
  431. clear
  432. echo "Tell me your name for the client cert"
  433. echo "Please, use one word only, no special characters"
  434. read -p "Client name: " -e -i client CLIENT
  435. echo ""
  436. if [[ "$OS" = 'debian' ]]; then
  437. apt-get update -qq
  438. apt-get install openvpn iptables openssl -y -qq
  439. if [ "$DNSRESOLVER" = 1 ]; then
  440. DNS=7
  441. #Installation of "Unbound" caching DNS resolver
  442. sudo apt-get install unbound -y -qq
  443. if [ "$TCP" -eq 1 ]; then
  444. echo "interface: 10.9.0.1" >> /etc/unbound/unbound.conf
  445. fi
  446. if [ "$UDP" -eq 1 ]; then
  447. echo "interface: 10.8.0.1" >> /etc/unbound/unbound.conf
  448. fi
  449. echo "access-control: 0.0.0.0/0 allow" >> /etc/unbound/unbound.conf
  450. fi
  451. if [ "$ANTIVIR" = 1 ]; then
  452. apt-get install clamav clamav-daemon -qq -y
  453. service clamav-freshclam stop
  454. freshclam
  455. service clamav-freshclam start
  456. sed -i "s/AllowSupplementaryGroups false/AllowSupplementaryGroups true/" /etc/clamav/clamd.conf
  457. service clamav-daemon restart
  458. apt-get install havp -y
  459. sed -i '/ENABLECLAMLIB true/c\ENABLECLAMLIB false' /etc/havp/havp.config
  460. sed -i '/ENABLECLAMD false/c\ENABLECLAMD true' /etc/havp/havp.config
  461. sed -i '/RANGE false/c\RANGE true' /etc/havp/havp.config
  462. sed -i '/SCANIMAGES true/c\ENABLECLAMD false' /etc/havp/havp.config
  463. sed -i 's/\# SKIPMIME/SKIPMIME/' /etc/havp/havp.config
  464. sed -i '/\LOG_OKS true/c\LOG_OKS false' /etc/havp/havp.config
  465. gpasswd -a clamav havp
  466. service clamav-daemon restart
  467. service havp restart
  468. apt-get install privoxy -y -qq
  469. sed -i '/listen-address localhost:8118/c\listen-address 127.0.0.1:8118' /etc/privoxy/config
  470. HOST=$(hostname -f)
  471. sed -i "/hostname hostname.example.org/c\hostname "$HOST"" /etc/privoxy/config
  472. service privoxy restart
  473. sed -i '/PARENTPROXY localhost/c\PARENTPROXY 127.0.0.1' /etc/havp/havp.config
  474. sed -i '/PARENTPORT 3128/c\PARENTPORT 8118' /etc/havp/havp.config
  475. sed -i '/TRANSPARENT false/c\TRANSPARENT true' /etc/havp/havp.config
  476. sed -i "3 a\iptables -t nat -A PREROUTING -p tcp -i tun+ --dport 80 -j REDIRECT --to-port 8080" $RCLOCAL #Add this firewall rule to startup(redirect traffic on port 80 to privoxy)
  477. service havp restart
  478. iptables -t nat -A PREROUTING -i tun+ -p tcp --dport 80 -j REDIRECT --to-port 8080
  479. fi
  480. else
  481. echo "Only Debian-based distros supported currently"
  482. fi
  483. ovpnversion=$(openvpn --status-version | grep -o "([0-9].*)" | sed 's/[^0-9.]//g')
  484. if version_gt $ovpnversion "2.3.3"; then
  485. while :
  486. do
  487. clear
  488. echo "Your OpenVPN version is $ovpnversion and it supports"
  489. echo "newer and more secure TLS 1.2 protocol for its control channel."
  490. echo "Do you want to force usage of TLS 1.2 ?"
  491. echo "NOTE: Your client also must use version 2.3.3 or newer"
  492. read -p "Force TLS 1.2 [y/n]: " -e -i n TLSNEW
  493. case $TLSNEW in
  494. y) TLSNEW=1
  495. break ;;
  496. n) TLSNEW=0
  497. break ;;
  498. esac
  499. done
  500. fi
  501. echo "Okay, that was all I needed. We are ready to setup your OpenVPN server now"
  502. read -n1 -r -p "Press any key to continue..."
  503. # An old version of easy-rsa was available by default in some openvpn packages
  504. if [[ -d /etc/openvpn/easy-rsa/ ]]; then
  505. rm -rf /etc/openvpn/easy-rsa/
  506. fi
  507. # Get easy-rsa
  508. wget --no-check-certificate -O ~/EasyRSA-3.0.1.tgz https://github.com/OpenVPN/easy-rsa/releases/download/3.0.1/EasyRSA-3.0.1.tgz
  509. tar xzf ~/EasyRSA-3.0.1.tgz -C ~/
  510. mkdir /etc/openvpn
  511. mv ~/EasyRSA-3.0.1/ /etc/openvpn/EasyRSA-3.0.1
  512. mv /etc/openvpn/EasyRSA-3.0.1/ /etc/openvpn/easy-rsa/
  513. chown -R root:root /etc/openvpn/easy-rsa/
  514. rm -rf ~/EasyRSA-3.0.1.tgz
  515. cd /etc/openvpn/easy-rsa/
  516. # Create the PKI, set up the CA, the DH params and the server + client certificates
  517. ./easyrsa init-pki
  518. cp vars.example vars
  519. sed -i 's/#set_var EASYRSA_KEY_SIZE 2048/set_var EASYRSA_KEY_SIZE '$KEYSIZE'/' vars #change key size to desired size
  520. ./easyrsa --batch build-ca nopass
  521. ./easyrsa gen-dh
  522. ./easyrsa build-server-full server nopass
  523. ./easyrsa build-client-full "$CLIENT" nopass
  524. ./easyrsa gen-crl
  525. openvpn --genkey --secret /etc/openvpn/easy-rsa/pki/private/ta.key #generate TLS key for additional security
  526. # Move the stuff we need
  527. cp pki/ca.crt pki/private/ca.key pki/dh.pem pki/issued/server.crt pki/private/server.key /etc/openvpn
  528. if [ "$UDP" = 1 ]; then
  529. # Generate udp.conf
  530. echo "port $PORT
  531. proto udp
  532. dev tun
  533. ca ca.crt
  534. cert server.crt
  535. key server.key
  536. dh dh.pem
  537. push \"register-dns\"
  538. topology subnet
  539. server 10.8.0.0 255.255.255.0
  540. cipher $CIPHER
  541. auth $DIGEST
  542. ifconfig-pool-persist ipp.txt" > /etc/openvpn/$UDP_SERVICE_AND_CONFIG_NAME.conf
  543. echo 'push "redirect-gateway def1 bypass-dhcp"' >> /etc/openvpn/$UDP_SERVICE_AND_CONFIG_NAME.conf
  544. if [ $TLS = 1 ]; then
  545. echo "--tls-auth /etc/openvpn/easy-rsa/pki/private/ta.key 0" >> /etc/openvpn/$UDP_SERVICE_AND_CONFIG_NAME.conf #TLS key information added to config file
  546. fi
  547. if [ $TLSNEW = 1 ]; then
  548. echo "--tls-version-min 1.2" >> /etc/openvpn/$UDP_SERVICE_AND_CONFIG_NAME.conf
  549. fi
  550. # DNS
  551. case $DNS in
  552. 1)
  553. # Obtain the resolvers from resolv.conf and use them for OpenVPN
  554. grep -v '#' /etc/resolv.conf | grep 'nameserver' | grep -E -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | while read line; do
  555. echo "push \"dhcp-option DNS $line\"" >> /etc/openvpn/$UDP_SERVICE_AND_CONFIG_NAME.conf
  556. done
  557. ;;
  558. 2)
  559. echo 'push "dhcp-option DNS 208.67.222.222"' >> /etc/openvpn/$UDP_SERVICE_AND_CONFIG_NAME.conf
  560. echo 'push "dhcp-option DNS 208.67.220.220"' >> /etc/openvpn/$UDP_SERVICE_AND_CONFIG_NAME.conf
  561. ;;
  562. 3)
  563. echo 'push "dhcp-option DNS 64.6.64.6"' >> /etc/openvpn/$UDP_SERVICE_AND_CONFIG_NAME.conf
  564. echo 'push "dhcp-option DNS 64.6.65.6"' >> /etc/openvpn/$UDP_SERVICE_AND_CONFIG_NAME.conf
  565. ;;
  566. 4)
  567. echo 'push "dhcp-option DNS 129.250.35.250"' >> /etc/openvpn/$UDP_SERVICE_AND_CONFIG_NAME.conf
  568. echo 'push "dhcp-option DNS 129.250.35.251"' >> /etc/openvpn/$UDP_SERVICE_AND_CONFIG_NAME.conf
  569. ;;
  570. 5)
  571. echo 'push "dhcp-option DNS 74.82.42.42"' >> /etc/openvpn/$UDP_SERVICE_AND_CONFIG_NAME.conf
  572. ;;
  573. 6)
  574. echo 'push "dhcp-option DNS 8.8.8.8"' >> /etc/openvpn/$UDP_SERVICE_AND_CONFIG_NAME.conf
  575. echo 'push "dhcp-option DNS 8.8.4.4"' >> /etc/openvpn/$UDP_SERVICE_AND_CONFIG_NAME.conf
  576. ;;
  577. 7)
  578. echo 'push "dhcp-option DNS 10.8.0.1"' >> /etc/openvpn/$UDP_SERVICE_AND_CONFIG_NAME.conf
  579. esac
  580. echo "keepalive 10 120
  581. comp-lzo
  582. persist-key
  583. persist-tun
  584. status openvpn-status.log
  585. verb 3
  586. crl-verify /etc/openvpn/easy-rsa/pki/crl.pem" >> /etc/openvpn/$UDP_SERVICE_AND_CONFIG_NAME.conf
  587. if [ "$INTERNALNETWORK" = 1 ]; then
  588. echo "client-to-client" >> /etc/openvpn/$UDP_SERVICE_AND_CONFIG_NAME.conf
  589. fi
  590. fi
  591. if [ "$TCP" = 1 ]; then
  592. echo "port $PORTTCP
  593. proto tcp
  594. dev tun
  595. ca ca.crt
  596. cert server.crt
  597. key server.key
  598. dh dh.pem
  599. push \"register-dns\"
  600. topology subnet
  601. server 10.9.0.0 255.255.255.0
  602. ifconfig-pool-persist ipp.txt
  603. cipher $CIPHER
  604. auth $DIGEST
  605. sndbuf 0
  606. rcvbuf 0" > /etc/openvpn/$TCP_SERVICE_AND_CONFIG_NAME.conf
  607. echo 'push "redirect-gateway def1 bypass-dhcp"' >> /etc/openvpn/$TCP_SERVICE_AND_CONFIG_NAME.conf
  608. if [ $TLS = 1 ]; then
  609. echo "--tls-auth /etc/openvpn/easy-rsa/pki/private/ta.key 0" >> /etc/openvpn/$TCP_SERVICE_AND_CONFIG_NAME.conf #TLS key information added to config file
  610. fi
  611. if [ $TLSNEW = 1 ]; then
  612. echo "--tls-version-min 1.2" >> /etc/openvpn/$TCP_SERVICE_AND_CONFIG_NAME.conf
  613. fi
  614. # DNS
  615. case $DNS in
  616. 1)
  617. # Obtain the resolvers from resolv.conf and use them for OpenVPN
  618. grep -v '#' /etc/resolv.conf | grep 'nameserver' | grep -E -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | while read line; do
  619. echo "push \"dhcp-option DNS $line\"" >> /etc/openvpn/$TCP_SERVICE_AND_CONFIG_NAME.conf
  620. done
  621. ;;
  622. 2)
  623. echo 'push "dhcp-option DNS 208.67.222.222"' >> /etc/openvpn/$TCP_SERVICE_AND_CONFIG_NAME.conf
  624. echo 'push "dhcp-option DNS 208.67.220.220"' >> /etc/openvpn/$TCP_SERVICE_AND_CONFIG_NAME.conf
  625. ;;
  626. 3)
  627. echo 'push "dhcp-option DNS 64.6.64.6"' >> /etc/openvpn/$TCP_SERVICE_AND_CONFIG_NAME.conf
  628. echo 'push "dhcp-option DNS 64.6.65.6"' >> /etc/openvpn/$TCP_SERVICE_AND_CONFIG_NAME.conf
  629. ;;
  630. 4)
  631. echo 'push "dhcp-option DNS 129.250.35.250"' >> /etc/openvpn/$TCP_SERVICE_AND_CONFIG_NAME.conf
  632. echo 'push "dhcp-option DNS 129.250.35.251"' >> /etc/openvpn/$TCP_SERVICE_AND_CONFIG_NAME.conf
  633. ;;
  634. 5)
  635. echo 'push "dhcp-option DNS 74.82.42.42"' >> /etc/openvpn/$TCP_SERVICE_AND_CONFIG_NAME.conf
  636. ;;
  637. 6)
  638. echo 'push "dhcp-option DNS 8.8.8.8"' >> /etc/openvpn/$TCP_SERVICE_AND_CONFIG_NAME.conf
  639. echo 'push "dhcp-option DNS 8.8.4.4"' >> /etc/openvpn/$TCP_SERVICE_AND_CONFIG_NAME.conf
  640. ;;
  641. 7)
  642. echo 'push "dhcp-option DNS 10.9.0.1"' >> /etc/openvpn/$TCP_SERVICE_AND_CONFIG_NAME.conf
  643. esac
  644. echo "keepalive 10 120
  645. comp-lzo
  646. persist-key
  647. persist-tun
  648. status openvpn-status.log
  649. verb 3
  650. crl-verify /etc/openvpn/easy-rsa/pki/crl.pem" >> /etc/openvpn/$TCP_SERVICE_AND_CONFIG_NAME.conf
  651. if [ "$INTERNALNETWORK" = 1 ]; then
  652. echo "client-to-client" >> /etc/openvpn/$TCP_SERVICE_AND_CONFIG_NAME.conf
  653. fi
  654. fi
  655. # Enable net.ipv4.ip_forward for the system
  656. sed -i 's|#net.ipv4.ip_forward=1|net.ipv4.ip_forward=1|' /etc/sysctl.conf
  657. sed -i " 5 a\echo 1 > /proc/sys/net/ipv4/ip_forward" $RCLOCAL # Added for servers that don't read from sysctl at startup
  658. # Avoid an unneeded reboot
  659. echo 1 > /proc/sys/net/ipv4/ip_forward
  660. # Set NAT for the VPN subnet
  661. if [ "$INTERNALNETWORK" = 1 ]; then
  662. if [ "$UDP" = 1 ]; then
  663. iptables -t nat -A POSTROUTING -s 10.8.0.0/24 ! -d 10.8.0.0/24 -j SNAT --to $IP
  664. sed -i "1 a\iptables -t nat -A POSTROUTING -s 10.8.0.0/24 ! -d 10.8.0.0/24 -j SNAT --to $IP" $RCLOCAL
  665. fi
  666. if [ "$TCP" = 1 ]; then
  667. iptables -t nat -A POSTROUTING -s 10.9.0.0/24 ! -d 10.9.0.0/24 -j SNAT --to $IP
  668. sed -i "1 a\iptables -t nat -A POSTROUTING -s 10.9.0.0/24 ! -d 10.9.0.0/24 -j SNAT --to $IP" $RCLOCAL
  669. fi
  670. else
  671. if [ "$UDP" = 1 ]; then
  672. iptables -t nat -A POSTROUTING -s 10.8.0.0/24 ! -d 10.8.0.1 -j SNAT --to $IP
  673. sed -i "1 a\iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -j SNAT --to $IP" $RCLOCAL
  674. fi
  675. if [ "$TCP" = 1 ]; then
  676. iptables -t nat -A POSTROUTING -s 10.9.0.0/24 ! -d 10.9.0.1 -j SNAT --to $IP #This line and the next one are added for tcp server instance
  677. sed -i "1 a\iptables -t nat -A POSTROUTING -s 10.9.0.0/24 -j SNAT --to $IP" $RCLOCAL
  678. fi
  679. fi
  680. if iptables -L | grep -q REJECT; then
  681. # If iptables has at least one REJECT rule, we asume this is needed.
  682. # Not the best approach but I can't think of other and this shouldn't
  683. # cause problems.
  684. if [ "$UDP" = 1 ]; then
  685. iptables -I INPUT -p udp --dport $PORT -j ACCEPT
  686. iptables -I FORWARD -s 10.8.0.0/24 -j ACCEPT
  687. iptables -I FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
  688. sed -i "1 a\iptables -I INPUT -p udp --dport $PORT -j ACCEPT" $RCLOCAL
  689. sed -i "1 a\iptables -I FORWARD -s 10.8.0.0/24 -j ACCEPT" $RCLOCAL
  690. sed -i "1 a\iptables -I FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT" $RCLOCAL
  691. fi
  692. if [ "$TCP" = 1 ]; then
  693. iptables -I INPUT -p udp --dport $PORTTCP -j ACCEPT #This line and next 5 lines have been added for tcp support
  694. iptables -I FORWARD -s 10.9.0.0/24 -j ACCEPT
  695. iptables -I FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
  696. sed -i "1 a\iptables -I INPUT -p tcp --dport $PORTTCP -j ACCEPT" $RCLOCAL
  697. sed -i "1 a\iptables -I FORWARD -s 10.9.0.0/24 -j ACCEPT" $RCLOCAL
  698. sed -i "1 a\iptables -I FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT" $RCLOCAL
  699. fi
  700. fi
  701. ###############################################################################################################
  702. # START_SERVICE_SECTION
  703. # Install and start service for both UDP and TCP
  704. ###############################################################################################################
  705. if [ "$UDP" = 1 ]; then
  706. echo "[Unit]
  707. #Created by openvpn-install-advanced (https://github.com/pl48415/openvpn-install-advanced)
  708. Description=OpenVPN Robust And Highly Flexible Tunneling Application On <server>
  709. After=syslog.target network.target
  710. [Service]
  711. Type=forking
  712. PIDFile=/var/run/openvpn/$UDP_SERVICE_AND_CONFIG_NAME.pid
  713. ExecStart=/usr/sbin/openvpn --daemon --writepid /var/run/openvpn/$UDP_SERVICE_AND_CONFIG_NAME.pid --cd /etc/openvpn/ --config $UDP_SERVICE_AND_CONFIG_NAME.conf
  714. [Install]
  715. WantedBy=multi-user.target" > /etc/systemd/system/$UDP_SERVICE_AND_CONFIG_NAME.service
  716. if pgrep systemd-journal; then
  717. sudo systemctl enable $UDP_SERVICE_AND_CONFIG_NAME.service
  718. fi
  719. fi
  720. if [ "$TCP" = 1 ]; then
  721. echo "[Unit]
  722. #Created by openvpn-install-advanced (https://github.com/pl48415/openvpn-install-advanced)
  723. Description=OpenVPN Robust And Highly Flexible Tunneling Application On <server>
  724. After=syslog.target network.target
  725. [Service]
  726. Type=forking
  727. PIDFile=/var/run/openvpn/$TCP_SERVICE_AND_CONFIG_NAME.pid
  728. ExecStart=/usr/sbin/openvpn --daemon --writepid /var/run/openvpn/$TCP_SERVICE_AND_CONFIG_NAME.pid --cd /etc/openvpn/ --config $TCP_SERVICE_AND_CONFIG_NAME.conf
  729. [Install]
  730. WantedBy=multi-user.target" > /etc/systemd/system/$TCP_SERVICE_AND_CONFIG_NAME.service
  731. if pgrep systemd-journal; then
  732. sudo systemctl enable $TCP_SERVICE_AND_CONFIG_NAME.service
  733. fi
  734. fi
  735. if pgrep systemd-journal; then
  736. sudo systemctl start openvpn.service
  737. else
  738. if [[ "$OS" = 'debian' ]]; then
  739. /etc/init.d/openvpn start
  740. else
  741. service openvpn start
  742. fi
  743. fi
  744. ###############################################################################################################
  745. # END_SERVICE_SECTION
  746. ###############################################################################################################
  747. # Try to detect a NATed connection and ask about it to potential LowEndSpirit or Scaleway users
  748. EXTERNALIP=$(wget -qO- ipv4.icanhazip.com)
  749. if [[ "$IP" != "$EXTERNALIP" ]]; then
  750. echo ""
  751. echo "Looks like your server is behind a NAT!"
  752. echo ""
  753. echo "If your server is NATed (LowEndSpirit or Scaleway), I need to know the external IP"
  754. echo "If that's not the case, just ignore this and leave the next field blank"
  755. read -p "External IP: " -e USEREXTERNALIP
  756. if [[ "$USEREXTERNALIP" != "" ]]; then
  757. IP=$USEREXTERNALIP
  758. fi
  759. fi
  760. # client-common.txt is created so we have a template to add further UDP users later
  761. if [ "$UDP" = 1 ]; then
  762. echo "client
  763. dev tun
  764. cipher $CIPHER
  765. auth $DIGEST
  766. proto udp
  767. remote $IP $PORT
  768. resolv-retry infinite
  769. nobind
  770. persist-key
  771. persist-tun
  772. remote-cert-tls server
  773. comp-lzo
  774. verb 3" > /etc/openvpn/client-common.txt
  775. newclient "$CLIENT"
  776. fi
  777. if [ "$TCP" = 1 ]; then
  778. echo "client
  779. cipher $CIPHER
  780. auth $DIGEST
  781. dev tun
  782. proto tcp
  783. remote $IP $PORTTCP
  784. resolv-retry infinite
  785. nobind
  786. persist-key
  787. persist-tun
  788. remote-cert-tls server
  789. comp-lzo
  790. verb 3
  791. sndbuf 0
  792. rcvbuf 0
  793. " > /etc/openvpn/clienttcp-common.txt # clienttcp-common.txt is created so we have a template to add further TCP users later
  794. newclienttcp "$CLIENT"
  795. fi
  796. # Generates the custom client.ovpn
  797. echo ""
  798. echo "Finished!"
  799. echo ""
  800. if [ "$UDP" = 1 ]; then
  801. echo "Your UDP client config is available at ~/$CLIENT.ovpn"
  802. fi
  803. if [ "$TCP" = 1 ]; then
  804. echo "Your TCP client config is available at ~/${CLIENT}tcp.ovpn"
  805. fi
  806. echo "If you want to add more clients, you simply need to run this script another time!"
  807. fi
  808. if [ "$DNSRESOLVER" = 1 ]; then
  809. sudo service unbound restart
  810. fi