freedombone-app-vpn 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # VPN functions
  12. # https://www.digitalocean.com/community/tutorials/how-to-set-up-an-openvpn-server-on-debian-8
  13. # https://jamielinux.com/blog/force-all-network-traffic-through-openvpn-using-iptables/
  14. # http://www.farrellf.com/projects/software/2016-05-04_Running_a_VPN_Server_with_OpenVPN_and_Stunnel/index_.php
  15. #
  16. # License
  17. # =======
  18. #
  19. # Copyright (C) 2014-2017 Bob Mottram <bob@freedombone.net>
  20. #
  21. # This program is free software: you can redistribute it and/or modify
  22. # it under the terms of the GNU Affero General Public License as published by
  23. # the Free Software Foundation, either version 3 of the License, or
  24. # (at your option) any later version.
  25. #
  26. # This program is distributed in the hope that it will be useful,
  27. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. # GNU Affero General Public License for more details.
  30. #
  31. # You should have received a copy of the GNU Affero General Public License
  32. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  33. VARIANTS='full full-vim'
  34. IN_DEFAULT_INSTALL=0
  35. SHOW_ON_ABOUT=0
  36. OPENVPN_SERVER_NAME="server"
  37. OPENVPN_KEY_FILENAME='client.ovpn'
  38. VPN_COUNTRY_CODE="US"
  39. VPN_AREA="Apparent Free Speech Zone"
  40. VPN_LOCATION="Freedomville"
  41. VPN_ORGANISATION="Freedombone"
  42. VPN_UNIT="Freedombone Unit"
  43. STUNNEL_PORT=3439
  44. VPN_TLS_PORT=553
  45. vpn_variables=(MY_EMAIL_ADDRESS
  46. DEFAULT_DOMAIN_NAME
  47. MY_USERNAME
  48. VPN_COUNTRY_CODE
  49. VPN_AREA
  50. VPN_LOCATION
  51. VPN_ORGANISATION
  52. VPN_UNIT
  53. VPN_TLS_PORT)
  54. function logging_on_vpn {
  55. echo -n ''
  56. }
  57. function logging_off_vpn {
  58. echo -n ''
  59. }
  60. function install_interactive_vpn {
  61. VPN_DETAILS_COMPLETE=
  62. while [ ! $VPN_DETAILS_COMPLETE ]
  63. do
  64. data=$(tempfile 2>/dev/null)
  65. trap "rm -f $data" 0 1 2 5 15
  66. dialog --backtitle $"Freedombone Configuration" \
  67. --title $"VPN Configuration" \
  68. --form $"\nPlease enter your VPN details. Changing the port to 443 will help defend against censorship but will prevent other web apps from running." 12 65 1 \
  69. $"TLS port:" 1 1 "$(grep 'VPN_TLS_PORT' temp.cfg | awk -F '=' '{print $2}')" 1 12 4 4 \
  70. 2> $data
  71. sel=$?
  72. case $sel in
  73. 1) exit 1;;
  74. 255) exit 1;;
  75. esac
  76. tlsport=$(cat $data | sed -n 1p)
  77. if [ ${#tlsport} -gt 1 ]; then
  78. if [[ "$tlsport" != *' '* && "$tlsport" != *'.'* ]]; then
  79. VPN_TLS_PORT="$tlsport"
  80. VPN_DETAILS_COMPLETE="yes"
  81. write_config_param "VPN_TLS_PORT" "$VPN_TLS_PORT"
  82. fi
  83. fi
  84. done
  85. APP_INSTALLED=1
  86. }
  87. function vpn_change_tls_port {
  88. EXISTING_VPN_TLS_PORT=$VPN_TLS_PORT
  89. data=$(tempfile 2>/dev/null)
  90. trap "rm -f $data" 0 1 2 5 15
  91. dialog --title $"VPN Configuration" \
  92. --backtitle $"Freedombone Control Panel" \
  93. --inputbox $'Change TLS port' 10 50 $VPN_TLS_PORT 2>$data
  94. sel=$?
  95. case $sel in
  96. 0)
  97. tlsport=$(<$data)
  98. if [ ${#tlsport} -gt 0 ]; then
  99. if [[ "$tlsport" != "$EXISTING_VPN_TLS_PORT" ]]; then
  100. clear
  101. VPN_TLS_PORT=$tlsport
  102. write_config_param "VPN_TLS_PORT" "$VPN_TLS_PORT"
  103. sed -i "s|accept =.*|accept = $VPN_TLS_PORT|g" /etc/stunnel/stunnel.conf
  104. sed -i "s|accept =.*|accept = $VPN_TLS_PORT|g" /etc/stunnel/stunnel-client.conf
  105. for d in /home/*/ ; do
  106. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  107. if [ -f /home/$USERNAME/stunnel-client.conf ]; then
  108. cp /etc/stunnel/stunnel-client.conf /home/$USERNAME/stunnel-client.conf
  109. chown $USERNAME:$USERNAME /home/$USERNAME/stunnel-client.conf
  110. fi
  111. done
  112. if [ $VPN_TLS_PORT -eq 443 ]; then
  113. systemctl stop nginx
  114. systemctl disable nginx
  115. else
  116. systemctl enable nginx
  117. systemctl restart nginx
  118. fi
  119. systemctl restart stunnel
  120. dialog --title $"VPN Configuration" \
  121. --msgbox $"TLS port changed to $VPN_TLS_PORT" 6 60
  122. fi
  123. fi
  124. ;;
  125. esac
  126. }
  127. function vpn_regenerate_client_keys {
  128. data=$(tempfile 2>/dev/null)
  129. trap "rm -f $data" 0 1 2 5 15
  130. dialog --title $"Regenerate VPN keys for a user" \
  131. --backtitle $"Freedombone Control Panel" \
  132. --inputbox $'username' 10 50 2>$data
  133. sel=$?
  134. case $sel in
  135. 0)
  136. USERNAME=$(<$data)
  137. if [ ${#USERNAME} -gt 0 ]; then
  138. if [ -d /home/$USERNAME ]; then
  139. clear
  140. create_user_vpn_key $USERNAME
  141. dialog --title $"Regenerate VPN keys for a user" \
  142. --msgbox $"VPN keys were regenerated for $USERNAME" 6 60
  143. fi
  144. fi
  145. ;;
  146. esac
  147. }
  148. function configure_interactive_vpn {
  149. read_config_param VPN_TLS_PORT
  150. while true
  151. do
  152. data=$(tempfile 2>/dev/null)
  153. trap "rm -f $data" 0 1 2 5 15
  154. dialog --backtitle $"Freedombone Control Panel" \
  155. --title $"VPN Configuration" \
  156. --radiolist $"Choose an operation:" 13 70 3 \
  157. 1 $"Change TLS port (currently $VPN_TLS_PORT)" off \
  158. 2 $"Regenerate keys for a user" off \
  159. 3 $"Exit" on 2> $data
  160. sel=$?
  161. case $sel in
  162. 1) return;;
  163. 255) return;;
  164. esac
  165. case $(cat $data) in
  166. 1) vpn_change_tls_port;;
  167. 2) vpn_regenerate_client_keys;;
  168. 3) break;;
  169. esac
  170. done
  171. }
  172. function reconfigure_vpn {
  173. echo -n ''
  174. }
  175. function upgrade_vpn {
  176. echo -n ''
  177. }
  178. function backup_local_vpn {
  179. for d in /home/*/ ; do
  180. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  181. if [ -f /home/$USERNAME/$OPENVPN_KEY_FILENAME ]; then
  182. cp /home/$USERNAME/$OPENVPN_KEY_FILENAME /etc/openvpn/easy-rsa/keys/${USERNAME}_${OPENVPN_KEY_FILENAME}
  183. fi
  184. done
  185. function_check backup_directory_to_usb
  186. backup_directory_to_usb /etc/openvpn/easy-rsa/keys vpn
  187. }
  188. function restore_local_vpn {
  189. temp_restore_dir=/root/tempvpn
  190. restore_directory_from_usb $temp_restore_dir vpn
  191. if [ -d ${temp_restore_dir} ]; then
  192. cp -r ${temp_restore_dir}/* /etc/openvpn/easy-rsa/keys
  193. rm -rf ${temp_restore_dir}
  194. for d in /home/*/ ; do
  195. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  196. if [ -f /etc/openvpn/easy-rsa/keys/${USERNAME}_${OPENVPN_KEY_FILENAME} ]; then
  197. cp /etc/openvpn/easy-rsa/keys/${USERNAME}_${OPENVPN_KEY_FILENAME} /home/$USERNAME/$OPENVPN_KEY_FILENAME
  198. chown $USERNAME:$USERNAME /home/$USERNAME/$OPENVPN_KEY_FILENAME
  199. fi
  200. done
  201. fi
  202. }
  203. function backup_remote_vpn {
  204. for d in /home/*/ ; do
  205. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  206. if [ -f /home/$USERNAME/$OPENVPN_KEY_FILENAME ]; then
  207. cp /home/$USERNAME/$OPENVPN_KEY_FILENAME /etc/openvpn/easy-rsa/keys/${USERNAME}_${OPENVPN_KEY_FILENAME}
  208. fi
  209. done
  210. function_check backup_directory_to_friend
  211. backup_directory_to_friend /etc/openvpn/easy-rsa/keys vpn
  212. }
  213. function restore_remote_vpn {
  214. temp_restore_dir=/root/tempvpn
  215. restore_directory_from_friend $temp_restore_dir vpn
  216. if [ -d ${temp_restore_dir} ]; then
  217. cp -r ${temp_restore_dir}/* /etc/openvpn/easy-rsa/keys
  218. rm -rf ${temp_restore_dir}
  219. for d in /home/*/ ; do
  220. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  221. if [ -f /etc/openvpn/easy-rsa/keys/${USERNAME}_${OPENVPN_KEY_FILENAME} ]; then
  222. cp /etc/openvpn/easy-rsa/keys/${USERNAME}_${OPENVPN_KEY_FILENAME} /home/$USERNAME/$OPENVPN_KEY_FILENAME
  223. chown $USERNAME:$USERNAME /home/$USERNAME/$OPENVPN_KEY_FILENAME
  224. fi
  225. done
  226. fi
  227. }
  228. function remove_vpn {
  229. systemctl stop stunnel
  230. systemctl disable stunnel
  231. rm /etc/systemd/system/stunnel.service
  232. systemctl stop openvpn
  233. if [ $VPN_TLS_PORT -ne 443 ]; then
  234. firewall_remove VPN-TLS $VPN_TLS_PORT
  235. fi
  236. apt-get -yq remove --purge fastd openvpn easy-rsa
  237. apt-get -yq remove stunnel4
  238. if [ -d /etc/openvpn ]; then
  239. rm -rf /etc/openvpn
  240. fi
  241. firewall_disable_vpn
  242. echo 0 > /proc/sys/net/ipv4/ip_forward
  243. sed -i 's|net.ipv4.ip_forward=.*|net.ipv4.ip_forward=0|g' /etc/sysctl.conf
  244. remove_completion_param install_vpn
  245. # remove any client keys
  246. for d in /home/*/ ; do
  247. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  248. if [ -f /home/$USERNAME/$OPENVPN_KEY_FILENAME ]; then
  249. shred -zu /home/$USERNAME/$OPENVPN_KEY_FILENAME
  250. fi
  251. rm /home/$USERNAME/stunnel*
  252. done
  253. userdel -f vpn
  254. groupdel -f vpn
  255. if [ -d /etc/stunnel ]; then
  256. rm -rf /etc/stunnel
  257. fi
  258. }
  259. function create_user_vpn_key {
  260. username=$1
  261. if [ ! -d /home/$username ]; then
  262. return
  263. fi
  264. echo $"Creating VPN key for $username"
  265. cd /etc/openvpn/easy-rsa
  266. if [ -f /etc/openvpn/easy-rsa/keys/$username.crt ]; then
  267. rm /etc/openvpn/easy-rsa/keys/$username.crt
  268. fi
  269. if [ -f /etc/openvpn/easy-rsa/keys/$username.key ]; then
  270. rm /etc/openvpn/easy-rsa/keys/$username.key
  271. fi
  272. if [ -f /etc/openvpn/easy-rsa/keys/$username.csr ]; then
  273. rm /etc/openvpn/easy-rsa/keys/$username.csr
  274. fi
  275. sed -i 's| --interact||g' build-key
  276. ./build-key "$username"
  277. if [ ! -f /etc/openvpn/easy-rsa/keys/$username.crt ]; then
  278. echo $'VPN user cert not generated'
  279. exit 783528
  280. fi
  281. user_cert=$(cat /etc/openvpn/easy-rsa/keys/$username.crt)
  282. if [ ${#user_cert} -lt 10 ]; then
  283. cat /etc/openvpn/easy-rsa/keys/$username.crt
  284. echo $'User cert generation failed'
  285. exit 634659
  286. fi
  287. if [ ! -f /etc/openvpn/easy-rsa/keys/$username.key ]; then
  288. echo $'VPN user key not generated'
  289. exit 682523
  290. fi
  291. user_key=$(cat /etc/openvpn/easy-rsa/keys/$username.key)
  292. if [ ${#user_key} -lt 10 ]; then
  293. cat /etc/openvpn/easy-rsa/keys/$username.key
  294. echo $'User key generation failed'
  295. exit 285838
  296. fi
  297. user_vpn_cert_file=/home/$username/$OPENVPN_KEY_FILENAME
  298. if [ ! -f /usr/share/doc/openvpn/examples/sample-config-files/client.conf ]; then
  299. echo $'No VPN client template found'
  300. exit 429823
  301. fi
  302. cp /usr/share/doc/openvpn/examples/sample-config-files/client.conf $user_vpn_cert_file
  303. sed -i "s|remote .*|remote $DEFAULT_DOMAIN_NAME $STUNNEL_PORT|g" $user_vpn_cert_file
  304. sed -i 's|;user .*|user nobody|g' $user_vpn_cert_file
  305. sed -i 's|;group .*|group nobody|g' $user_vpn_cert_file
  306. sed -i 's|ca ca.crt|;ca ca.crt|g' $user_vpn_cert_file
  307. sed -i 's|cert client.crt|;cert client.crt|g' $user_vpn_cert_file
  308. sed -i 's|key client.key|;key client.key|g' $user_vpn_cert_file
  309. sed -i 's|tls-auth ta.key|;tls-auth ta.key|g' $user_vpn_cert_file
  310. sed -i 's|;proto tcp|proto tcp|g' $user_vpn_cert_file
  311. sed -i 's|proto udp|;proto udp|g' $user_vpn_cert_file
  312. echo '<ca>' >> $user_vpn_cert_file
  313. cat /etc/openvpn/ca.crt >> $user_vpn_cert_file
  314. echo '</ca>' >> $user_vpn_cert_file
  315. echo '<cert>' >> $user_vpn_cert_file
  316. cat /etc/openvpn/easy-rsa/keys/$username.crt >> $user_vpn_cert_file
  317. echo '</cert>' >> $user_vpn_cert_file
  318. echo '<key>' >> $user_vpn_cert_file
  319. cat /etc/openvpn/easy-rsa/keys/$username.key >> $user_vpn_cert_file
  320. echo '</key>' >> $user_vpn_cert_file
  321. chown $username:$username $user_vpn_cert_file
  322. # keep a backup
  323. cp $user_vpn_cert_file /etc/openvpn/easy-rsa/keys/$username.ovpn
  324. #rm /etc/openvpn/easy-rsa/keys/$username.crt
  325. #rm /etc/openvpn/easy-rsa/keys/$username.csr
  326. shred -zu /etc/openvpn/easy-rsa/keys/$username.key
  327. echo $"VPN key created at $user_vpn_cert_file"
  328. }
  329. function add_user_vpn {
  330. new_username="$1"
  331. new_user_password="$2"
  332. create_user_vpn_key $new_username
  333. if [ -f /etc/stunnel/stunnel.pem ]; then
  334. cp /etc/stunnel/stunnel.pem /home/$new_username/stunnel.pem
  335. chown $new_username:$new_username /home/$new_username/stunnel.pem
  336. fi
  337. if [ -f /etc/stunnel/stunnel.p12 ]; then
  338. cp /etc/stunnel/stunnel.p12 /home/$new_username/stunnel.p12
  339. chown $new_username:$new_username /home/$new_username/stunnel.p12
  340. fi
  341. cp /etc/stunnel/stunnel-client.conf /home/$new_username/stunnel-client.conf
  342. chown $new_username:$new_username /home/$new_username/stunnel-client.conf
  343. }
  344. function remove_user_vpn {
  345. new_username="$1"
  346. }
  347. function install_stunnel {
  348. apt-get -yq install stunnel4
  349. cd /etc/stunnel
  350. openssl req -x509 -nodes -days 3650 -sha256 \
  351. -subj "/O=$VPN_ORGANISATION/OU=$VPN_UNIT/C=$VPN_COUNTRY_CODE/ST=$VPN_AREA/L=$VPN_LOCATION/CN=$HOSTNAME" \
  352. -newkey rsa:2048 -keyout key.pem \
  353. -out cert.pem
  354. if [ ! -f key.pem ]; then
  355. echo $'stunnel key not created'
  356. exit 793530
  357. fi
  358. if [ ! -f cert.pem ]; then
  359. echo $'stunnel cert not created'
  360. exit 204587
  361. fi
  362. chmod 400 key.pem
  363. chmod 640 cert.pem
  364. cat key.pem cert.pem >> stunnel.pem
  365. chmod 640 stunnel.pem
  366. openssl pkcs12 -export -out stunnel.p12 -inkey key.pem -in cert.pem -passout pass:
  367. if [ ! -f stunnel.p12 ]; then
  368. echo $'stunnel pkcs12 not created'
  369. exit 639353
  370. fi
  371. chmod 640 stunnel.p12
  372. echo 'chroot = /var/lib/stunnel4' > stunnel.conf
  373. echo 'pid = /stunnel4.pid' >> stunnel.conf
  374. echo 'setuid = stunnel4' >> stunnel.conf
  375. echo 'setgid = stunnel4' >> stunnel.conf
  376. echo 'socket = l:TCP_NODELAY=1' >> stunnel.conf
  377. echo 'socket = r:TCP_NODELAY=1' >> stunnel.conf
  378. echo 'cert = /etc/stunnel/stunnel.pem' >> stunnel.conf
  379. echo '[openvpn]' >> stunnel.conf
  380. echo "accept = $VPN_TLS_PORT" >> stunnel.conf
  381. echo 'connect = localhost:1194' >> stunnel.conf
  382. echo 'cert = /etc/stunnel/stunnel.pem' >> stunnel.conf
  383. sed -i 's|ENABLED=.*|ENABLED=1|g' /etc/default/stunnel4
  384. echo '[openvpn]' > stunnel-client.conf
  385. echo 'client = yes' >> stunnel-client.conf
  386. echo "accept = $STUNNEL_PORT" >> stunnel-client.conf
  387. echo "connect = $DEFAULT_DOMAIN_NAME:$VPN_TLS_PORT" >> stunnel-client.conf
  388. echo 'cert = /etc/stunnel/stunnel.pem' >> stunnel-client.conf
  389. echo '[Unit]' > /etc/systemd/system/stunnel.service
  390. echo 'Description=SSL tunnel for network daemons' >> /etc/systemd/system/stunnel.service
  391. echo 'Documentation=man:stunnel https://www.stunnel.org/docs.html' >> /etc/systemd/system/stunnel.service
  392. echo 'DefaultDependencies=no' >> /etc/systemd/system/stunnel.service
  393. echo 'After=network.target' >> /etc/systemd/system/stunnel.service
  394. echo 'After=syslog.target' >> /etc/systemd/system/stunnel.service
  395. echo '' >> /etc/systemd/system/stunnel.service
  396. echo '[Install]' >> /etc/systemd/system/stunnel.service
  397. echo 'WantedBy=multi-user.target' >> /etc/systemd/system/stunnel.service
  398. echo 'Alias=stunnel.target' >> /etc/systemd/system/stunnel.service
  399. echo '' >> /etc/systemd/system/stunnel.service
  400. echo '[Service]' >> /etc/systemd/system/stunnel.service
  401. echo 'Type=forking' >> /etc/systemd/system/stunnel.service
  402. echo 'RuntimeDirectory=stunnel' >> /etc/systemd/system/stunnel.service
  403. echo 'EnvironmentFile=-/etc/stunnel/stunnel.conf' >> /etc/systemd/system/stunnel.service
  404. echo 'ExecStart=/usr/bin/stunnel /etc/stunnel/stunnel.conf' >> /etc/systemd/system/stunnel.service
  405. echo 'ExecStop=/usr/bin/killall -9 stunnel' >> /etc/systemd/system/stunnel.service
  406. echo 'RemainAfterExit=yes' >> /etc/systemd/system/stunnel.service
  407. if [ $VPN_TLS_PORT -eq 443 ]; then
  408. systemctl stop nginx
  409. systemctl disable nginx
  410. else
  411. systemctl enable nginx
  412. systemctl restart nginx
  413. fi
  414. systemctl enable stunnel
  415. systemctl daemon-reload
  416. systemctl start stunnel
  417. cp /etc/stunnel/stunnel.pem /home/$MY_USERNAME/stunnel.pem
  418. cp /etc/stunnel/stunnel.p12 /home/$MY_USERNAME/stunnel.p12
  419. cp /etc/stunnel/stunnel-client.conf /home/$MY_USERNAME/stunnel-client.conf
  420. chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/stunnel*
  421. }
  422. function install_vpn {
  423. apt-get -yq install fastd openvpn easy-rsa
  424. if [ ! -f /usr/share/doc/openvpn/examples/sample-config-files/server.conf.gz ]; then
  425. echo $'Example openvpn server config not found'
  426. exit 783953
  427. fi
  428. groupadd vpn
  429. useradd -r -s /bin/false -g vpn vpn
  430. # server configuration
  431. gunzip -c /usr/share/doc/openvpn/examples/sample-config-files/server.conf.gz > /etc/openvpn/server.conf
  432. sed -i "s|;push \"redirect-gateway|push \"redirect-gateway|g" /etc/openvpn/server.conf
  433. sed -i 's|;push "dhcp-option|push "dhcp-option|g' /etc/openvpn/server.conf
  434. sed -i 's|;user no.*|user vpn|g' /etc/openvpn/server.conf
  435. sed -i 's|;group no.*|group vpn|g' /etc/openvpn/server.conf
  436. sed -i 's|;max-clients.*|max-clients 2|g' /etc/openvpn/server.conf
  437. sed -i 's|;proto tcp|proto tcp|g' /etc/openvpn/server.conf
  438. sed -i 's|proto udp|;proto udp|g' /etc/openvpn/server.conf
  439. sed -i 's|explicit-exit-notify.*|explicit-exit-notify 0|g' /etc/openvpn/server.conf
  440. sed -i 's|tls-auth|;tls-auth|g' /etc/openvpn/server.conf
  441. echo 1 > /proc/sys/net/ipv4/ip_forward
  442. sed -i 's|# net.ipv4.ip_forward|net.ipv4.ip_forward|g' /etc/sysctl.conf
  443. sed -i 's|#net.ipv4.ip_forward|net.ipv4.ip_forward|g' /etc/sysctl.conf
  444. sed -i 's|net.ipv4.ip_forward.*|net.ipv4.ip_forward=1|g' /etc/sysctl.conf
  445. cp -r /usr/share/easy-rsa/ /etc/openvpn
  446. if [ ! -d /etc/openvpn/easy-rsa/keys ]; then
  447. mkdir /etc/openvpn/easy-rsa/keys
  448. fi
  449. # keys configuration
  450. sed -i "s|export KEY_COUNTRY.*|export KEY_COUNTRY=\"US\"|g" /etc/openvpn/easy-rsa/vars
  451. sed -i "s|export KEY_PROVINCE.*|export KEY_PROVINCE=\"TX\"|g" /etc/openvpn/easy-rsa/vars
  452. sed -i "s|export KEY_CITY.*|export KEY_CITY=\"Dallas\"|g" /etc/openvpn/easy-rsa/vars
  453. sed -i "s|export KEY_ORG.*|export KEY_ORG=\"$PROJECT_NAME\"|g" /etc/openvpn/easy-rsa/vars
  454. sed -i "s|export KEY_EMAIL.*|export KEY_EMAIL=\"$MY_EMAIL_ADDRESS\"|g" /etc/openvpn/easy-rsa/vars
  455. sed -i "s|export KEY_OU=.*|export KEY_OU=\"MoonUnit\"|g" /etc/openvpn/easy-rsa/vars
  456. sed -i "s|export KEY_NAME.*|export KEY_NAME=\"$OPENVPN_SERVER_NAME\"|g" /etc/openvpn/easy-rsa/vars
  457. # generate host keys
  458. if [ ! -f /etc/openvpn/dh2048.pem ]; then
  459. openssl dhparam -out /etc/openvpn/dh2048.pem 2048
  460. fi
  461. cd /etc/openvpn/easy-rsa
  462. . ./vars
  463. ./clean-all
  464. vpn_openssl_version='1.0.0'
  465. if [ ! -f openssl-${vpn_openssl_version}.cnf ]; then
  466. echo $"openssl-${vpn_openssl_version}.cnf was not found"
  467. exit 7392353
  468. fi
  469. cp openssl-${vpn_openssl_version}.cnf openssl.cnf
  470. if [ -f /etc/openvpn/easy-rsa/keys/${OPENVPN_SERVER_NAME}.crt ]; then
  471. rm /etc/openvpn/easy-rsa/keys/${OPENVPN_SERVER_NAME}.crt
  472. fi
  473. if [ -f /etc/openvpn/easy-rsa/keys/${OPENVPN_SERVER_NAME}.key ]; then
  474. rm /etc/openvpn/easy-rsa/keys/${OPENVPN_SERVER_NAME}.key
  475. fi
  476. if [ -f /etc/openvpn/easy-rsa/keys/${OPENVPN_SERVER_NAME}.csr ]; then
  477. rm /etc/openvpn/easy-rsa/keys/${OPENVPN_SERVER_NAME}.csr
  478. fi
  479. sed -i 's| --interact||g' build-key-server
  480. sed -i 's| --interact||g' build-ca
  481. ./build-ca
  482. ./build-key-server $OPENVPN_SERVER_NAME
  483. if [ ! -f /etc/openvpn/easy-rsa/keys/${OPENVPN_SERVER_NAME}.crt ]; then
  484. echo $'OpenVPN crt not found'
  485. exit 7823352
  486. fi
  487. server_cert=$(cat /etc/openvpn/easy-rsa/keys/${OPENVPN_SERVER_NAME}.crt)
  488. if [ ${#server_cert} -lt 10 ]; then
  489. cat /etc/openvpn/easy-rsa/keys/${OPENVPN_SERVER_NAME}.crt
  490. echo $'Server cert generation failed'
  491. exit 3284682
  492. fi
  493. if [ ! -f /etc/openvpn/easy-rsa/keys/${OPENVPN_SERVER_NAME}.key ]; then
  494. echo $'OpenVPN key not found'
  495. exit 6839436
  496. fi
  497. if [ ! -f /etc/openvpn/easy-rsa/keys/ca.key ]; then
  498. echo $'OpenVPN ca not found'
  499. exit 7935203
  500. fi
  501. cp /etc/openvpn/easy-rsa/keys/{$OPENVPN_SERVER_NAME.crt,$OPENVPN_SERVER_NAME.key,ca.crt} /etc/openvpn
  502. create_user_vpn_key $MY_USERNAME
  503. firewall_enable_vpn
  504. if [ $VPN_TLS_PORT -ne 443 ]; then
  505. firewall_add VPN-TLS $VPN_TLS_PORT tcp
  506. fi
  507. systemctl start openvpn
  508. install_stunnel
  509. APP_INSTALLED=1
  510. }
  511. # NOTE: deliberately there is no "exit 0"