openvpn-install.sh 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353
  1. #!/bin/bash
  2. # shellcheck disable=SC1091,SC2164,SC2034,SC1072,SC1073,SC1009
  3. # Secure OpenVPN server installer for Debian, Ubuntu, CentOS, Amazon Linux 2, Fedora, Oracle Linux 8, Arch Linux, Rocky Linux and AlmaLinux.
  4. # https://github.com/angristan/openvpn-install
  5. function isRoot() {
  6. if [ "$EUID" -ne 0 ]; then
  7. return 1
  8. fi
  9. }
  10. function tunAvailable() {
  11. if [ ! -e /dev/net/tun ]; then
  12. return 1
  13. fi
  14. }
  15. function checkOS() {
  16. if [[ -e /etc/debian_version ]]; then
  17. OS="debian"
  18. source /etc/os-release
  19. if [[ $ID == "debian" || $ID == "raspbian" ]]; then
  20. if [[ $VERSION_ID -lt 9 ]]; then
  21. echo "⚠️ Your version of Debian is not supported."
  22. echo ""
  23. echo "However, if you're using Debian >= 9 or unstable/testing then you can continue, at your own risk."
  24. echo ""
  25. until [[ $CONTINUE =~ (y|n) ]]; do
  26. read -rp "Continue? [y/n]: " -e CONTINUE
  27. done
  28. if [[ $CONTINUE == "n" ]]; then
  29. exit 1
  30. fi
  31. fi
  32. elif [[ $ID == "ubuntu" ]]; then
  33. OS="ubuntu"
  34. MAJOR_UBUNTU_VERSION=$(echo "$VERSION_ID" | cut -d '.' -f1)
  35. if [[ $MAJOR_UBUNTU_VERSION -lt 16 ]]; then
  36. echo "⚠️ Your version of Ubuntu is not supported."
  37. echo ""
  38. echo "However, if you're using Ubuntu >= 16.04 or beta, then you can continue, at your own risk."
  39. echo ""
  40. until [[ $CONTINUE =~ (y|n) ]]; do
  41. read -rp "Continue? [y/n]: " -e CONTINUE
  42. done
  43. if [[ $CONTINUE == "n" ]]; then
  44. exit 1
  45. fi
  46. fi
  47. fi
  48. elif [[ -e /etc/system-release ]]; then
  49. source /etc/os-release
  50. if [[ $ID == "fedora" || $ID_LIKE == "fedora" ]]; then
  51. OS="fedora"
  52. fi
  53. if [[ $ID == "centos" || $ID == "rocky" || $ID == "almalinux" ]]; then
  54. OS="centos"
  55. if [[ ! $VERSION_ID =~ (7|8) ]]; then
  56. echo "⚠️ Your version of CentOS is not supported."
  57. echo ""
  58. echo "The script only support CentOS 7 and CentOS 8."
  59. echo ""
  60. exit 1
  61. fi
  62. fi
  63. if [[ $ID == "ol" ]]; then
  64. OS="oracle"
  65. if [[ ! $VERSION_ID =~ (8) ]]; then
  66. echo "Your version of Oracle Linux is not supported."
  67. echo ""
  68. echo "The script only support Oracle Linux 8."
  69. exit 1
  70. fi
  71. fi
  72. if [[ $ID == "amzn" ]]; then
  73. OS="amzn"
  74. if [[ $VERSION_ID != "2" ]]; then
  75. echo "⚠️ Your version of Amazon Linux is not supported."
  76. echo ""
  77. echo "The script only support Amazon Linux 2."
  78. echo ""
  79. exit 1
  80. fi
  81. fi
  82. elif [[ -e /etc/arch-release ]]; then
  83. OS=arch
  84. else
  85. echo "Looks like you aren't running this installer on a Debian, Ubuntu, Fedora, CentOS, Amazon Linux 2, Oracle Linux 8 or Arch Linux system"
  86. exit 1
  87. fi
  88. }
  89. function initialCheck() {
  90. if ! isRoot; then
  91. echo "Sorry, you need to run this as root"
  92. exit 1
  93. fi
  94. if ! tunAvailable; then
  95. echo "TUN is not available"
  96. exit 1
  97. fi
  98. checkOS
  99. }
  100. function installUnbound() {
  101. # If Unbound isn't installed, install it
  102. if [[ ! -e /etc/unbound/unbound.conf ]]; then
  103. if [[ $OS =~ (debian|ubuntu) ]]; then
  104. apt-get install -y unbound
  105. # Configuration
  106. echo 'interface: 10.8.0.1
  107. access-control: 10.8.0.1/24 allow
  108. hide-identity: yes
  109. hide-version: yes
  110. use-caps-for-id: yes
  111. prefetch: yes' >>/etc/unbound/unbound.conf
  112. elif [[ $OS =~ (centos|amzn|oracle) ]]; then
  113. yum install -y unbound
  114. # Configuration
  115. sed -i 's|# interface: 0.0.0.0$|interface: 10.8.0.1|' /etc/unbound/unbound.conf
  116. sed -i 's|# access-control: 127.0.0.0/8 allow|access-control: 10.8.0.1/24 allow|' /etc/unbound/unbound.conf
  117. sed -i 's|# hide-identity: no|hide-identity: yes|' /etc/unbound/unbound.conf
  118. sed -i 's|# hide-version: no|hide-version: yes|' /etc/unbound/unbound.conf
  119. sed -i 's|use-caps-for-id: no|use-caps-for-id: yes|' /etc/unbound/unbound.conf
  120. elif [[ $OS == "fedora" ]]; then
  121. dnf install -y unbound
  122. # Configuration
  123. sed -i 's|# interface: 0.0.0.0$|interface: 10.8.0.1|' /etc/unbound/unbound.conf
  124. sed -i 's|# access-control: 127.0.0.0/8 allow|access-control: 10.8.0.1/24 allow|' /etc/unbound/unbound.conf
  125. sed -i 's|# hide-identity: no|hide-identity: yes|' /etc/unbound/unbound.conf
  126. sed -i 's|# hide-version: no|hide-version: yes|' /etc/unbound/unbound.conf
  127. sed -i 's|# use-caps-for-id: no|use-caps-for-id: yes|' /etc/unbound/unbound.conf
  128. elif [[ $OS == "arch" ]]; then
  129. pacman -Syu --noconfirm unbound
  130. # Get root servers list
  131. curl -o /etc/unbound/root.hints https://www.internic.net/domain/named.cache
  132. if [[ ! -f /etc/unbound/unbound.conf.old ]]; then
  133. mv /etc/unbound/unbound.conf /etc/unbound/unbound.conf.old
  134. fi
  135. echo 'server:
  136. use-syslog: yes
  137. do-daemonize: no
  138. username: "unbound"
  139. directory: "/etc/unbound"
  140. trust-anchor-file: trusted-key.key
  141. root-hints: root.hints
  142. interface: 10.8.0.1
  143. access-control: 10.8.0.1/24 allow
  144. port: 53
  145. num-threads: 2
  146. use-caps-for-id: yes
  147. harden-glue: yes
  148. hide-identity: yes
  149. hide-version: yes
  150. qname-minimisation: yes
  151. prefetch: yes' >/etc/unbound/unbound.conf
  152. fi
  153. # IPv6 DNS for all OS
  154. if [[ $IPV6_SUPPORT == 'y' ]]; then
  155. echo 'interface: fd42:42:42:42::1
  156. access-control: fd42:42:42:42::/112 allow' >>/etc/unbound/unbound.conf
  157. fi
  158. if [[ ! $OS =~ (fedora|centos|amzn|oracle) ]]; then
  159. # DNS Rebinding fix
  160. echo "private-address: 10.0.0.0/8
  161. private-address: fd42:42:42:42::/112
  162. private-address: 172.16.0.0/12
  163. private-address: 192.168.0.0/16
  164. private-address: 169.254.0.0/16
  165. private-address: fd00::/8
  166. private-address: fe80::/10
  167. private-address: 127.0.0.0/8
  168. private-address: ::ffff:0:0/96" >>/etc/unbound/unbound.conf
  169. fi
  170. else # Unbound is already installed
  171. echo 'include: /etc/unbound/openvpn.conf' >>/etc/unbound/unbound.conf
  172. # Add Unbound 'server' for the OpenVPN subnet
  173. echo 'server:
  174. interface: 10.8.0.1
  175. access-control: 10.8.0.1/24 allow
  176. hide-identity: yes
  177. hide-version: yes
  178. use-caps-for-id: yes
  179. prefetch: yes
  180. private-address: 10.0.0.0/8
  181. private-address: fd42:42:42:42::/112
  182. private-address: 172.16.0.0/12
  183. private-address: 192.168.0.0/16
  184. private-address: 169.254.0.0/16
  185. private-address: fd00::/8
  186. private-address: fe80::/10
  187. private-address: 127.0.0.0/8
  188. private-address: ::ffff:0:0/96' >/etc/unbound/openvpn.conf
  189. if [[ $IPV6_SUPPORT == 'y' ]]; then
  190. echo 'interface: fd42:42:42:42::1
  191. access-control: fd42:42:42:42::/112 allow' >>/etc/unbound/openvpn.conf
  192. fi
  193. fi
  194. systemctl enable unbound
  195. systemctl restart unbound
  196. }
  197. function installQuestions() {
  198. echo "Welcome to the OpenVPN installer!"
  199. echo "The git repository is available at: https://github.com/angristan/openvpn-install"
  200. echo ""
  201. echo "I need to ask you a few questions before starting the setup."
  202. echo "You can leave the default options and just press enter if you are ok with them."
  203. echo ""
  204. echo "I need to know the IPv4 address of the network interface you want OpenVPN listening to."
  205. echo "Unless your server is behind NAT, it should be your public IPv4 address."
  206. # Detect public IPv4 address and pre-fill for the user
  207. IP=$(ip -4 addr | sed -ne 's|^.* inet \([^/]*\)/.* scope global.*$|\1|p' | head -1)
  208. if [[ -z $IP ]]; then
  209. # Detect public IPv6 address
  210. IP=$(ip -6 addr | sed -ne 's|^.* inet6 \([^/]*\)/.* scope global.*$|\1|p' | head -1)
  211. fi
  212. APPROVE_IP=${APPROVE_IP:-n}
  213. if [[ $APPROVE_IP =~ n ]]; then
  214. read -rp "IP address: " -e -i "$IP" IP
  215. fi
  216. # If $IP is a private IP address, the server must be behind NAT
  217. if echo "$IP" | grep -qE '^(10\.|172\.1[6789]\.|172\.2[0-9]\.|172\.3[01]\.|192\.168)'; then
  218. echo ""
  219. echo "It seems this server is behind NAT. What is its public IPv4 address or hostname?"
  220. echo "We need it for the clients to connect to the server."
  221. PUBLICIP=$(curl -s https://api.ipify.org)
  222. until [[ $ENDPOINT != "" ]]; do
  223. read -rp "Public IPv4 address or hostname: " -e -i "$PUBLICIP" ENDPOINT
  224. done
  225. fi
  226. echo ""
  227. echo "Checking for IPv6 connectivity..."
  228. echo ""
  229. # "ping6" and "ping -6" availability varies depending on the distribution
  230. if type ping6 >/dev/null 2>&1; then
  231. PING6="ping6 -c3 ipv6.google.com > /dev/null 2>&1"
  232. else
  233. PING6="ping -6 -c3 ipv6.google.com > /dev/null 2>&1"
  234. fi
  235. if eval "$PING6"; then
  236. echo "Your host appears to have IPv6 connectivity."
  237. SUGGESTION="y"
  238. else
  239. echo "Your host does not appear to have IPv6 connectivity."
  240. SUGGESTION="n"
  241. fi
  242. echo ""
  243. # Ask the user if they want to enable IPv6 regardless its availability.
  244. until [[ $IPV6_SUPPORT =~ (y|n) ]]; do
  245. read -rp "Do you want to enable IPv6 support (NAT)? [y/n]: " -e -i $SUGGESTION IPV6_SUPPORT
  246. done
  247. echo ""
  248. echo "What port do you want OpenVPN to listen to?"
  249. echo " 1) Default: 1194"
  250. echo " 2) Custom"
  251. echo " 3) Random [49152-65535]"
  252. until [[ $PORT_CHOICE =~ ^[1-3]$ ]]; do
  253. read -rp "Port choice [1-3]: " -e -i 1 PORT_CHOICE
  254. done
  255. case $PORT_CHOICE in
  256. 1)
  257. PORT="1194"
  258. ;;
  259. 2)
  260. until [[ $PORT =~ ^[0-9]+$ ]] && [ "$PORT" -ge 1 ] && [ "$PORT" -le 65535 ]; do
  261. read -rp "Custom port [1-65535]: " -e -i 1194 PORT
  262. done
  263. ;;
  264. 3)
  265. # Generate random number within private ports range
  266. PORT=$(shuf -i49152-65535 -n1)
  267. echo "Random Port: $PORT"
  268. ;;
  269. esac
  270. echo ""
  271. echo "What protocol do you want OpenVPN to use?"
  272. echo "UDP is faster. Unless it is not available, you shouldn't use TCP."
  273. echo " 1) UDP"
  274. echo " 2) TCP"
  275. until [[ $PROTOCOL_CHOICE =~ ^[1-2]$ ]]; do
  276. read -rp "Protocol [1-2]: " -e -i 1 PROTOCOL_CHOICE
  277. done
  278. case $PROTOCOL_CHOICE in
  279. 1)
  280. PROTOCOL="udp"
  281. ;;
  282. 2)
  283. PROTOCOL="tcp"
  284. ;;
  285. esac
  286. echo ""
  287. echo "What DNS resolvers do you want to use with the VPN?"
  288. echo " 1) Current system resolvers (from /etc/resolv.conf)"
  289. echo " 2) Self-hosted DNS Resolver (Unbound)"
  290. echo " 3) Cloudflare (Anycast: worldwide)"
  291. echo " 4) Quad9 (Anycast: worldwide)"
  292. echo " 5) Quad9 uncensored (Anycast: worldwide)"
  293. echo " 6) FDN (France)"
  294. echo " 7) DNS.WATCH (Germany)"
  295. echo " 8) OpenDNS (Anycast: worldwide)"
  296. echo " 9) Google (Anycast: worldwide)"
  297. echo " 10) Yandex Basic (Russia)"
  298. echo " 11) AdGuard DNS (Anycast: worldwide)"
  299. echo " 12) NextDNS (Anycast: worldwide)"
  300. echo " 13) Custom"
  301. until [[ $DNS =~ ^[0-9]+$ ]] && [ "$DNS" -ge 1 ] && [ "$DNS" -le 13 ]; do
  302. read -rp "DNS [1-12]: " -e -i 11 DNS
  303. if [[ $DNS == 2 ]] && [[ -e /etc/unbound/unbound.conf ]]; then
  304. echo ""
  305. echo "Unbound is already installed."
  306. echo "You can allow the script to configure it in order to use it from your OpenVPN clients"
  307. echo "We will simply add a second server to /etc/unbound/unbound.conf for the OpenVPN subnet."
  308. echo "No changes are made to the current configuration."
  309. echo ""
  310. until [[ $CONTINUE =~ (y|n) ]]; do
  311. read -rp "Apply configuration changes to Unbound? [y/n]: " -e CONTINUE
  312. done
  313. if [[ $CONTINUE == "n" ]]; then
  314. # Break the loop and cleanup
  315. unset DNS
  316. unset CONTINUE
  317. fi
  318. elif [[ $DNS == "13" ]]; then
  319. until [[ $DNS1 =~ ^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$ ]]; do
  320. read -rp "Primary DNS: " -e DNS1
  321. done
  322. until [[ $DNS2 =~ ^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$ ]]; do
  323. read -rp "Secondary DNS (optional): " -e DNS2
  324. if [[ $DNS2 == "" ]]; then
  325. break
  326. fi
  327. done
  328. fi
  329. done
  330. echo ""
  331. echo "Do you want to use compression? It is not recommended since the VORACLE attack makes use of it."
  332. until [[ $COMPRESSION_ENABLED =~ (y|n) ]]; do
  333. read -rp"Enable compression? [y/n]: " -e -i n COMPRESSION_ENABLED
  334. done
  335. if [[ $COMPRESSION_ENABLED == "y" ]]; then
  336. echo "Choose which compression algorithm you want to use: (they are ordered by efficiency)"
  337. echo " 1) LZ4-v2"
  338. echo " 2) LZ4"
  339. echo " 3) LZ0"
  340. until [[ $COMPRESSION_CHOICE =~ ^[1-3]$ ]]; do
  341. read -rp"Compression algorithm [1-3]: " -e -i 1 COMPRESSION_CHOICE
  342. done
  343. case $COMPRESSION_CHOICE in
  344. 1)
  345. COMPRESSION_ALG="lz4-v2"
  346. ;;
  347. 2)
  348. COMPRESSION_ALG="lz4"
  349. ;;
  350. 3)
  351. COMPRESSION_ALG="lzo"
  352. ;;
  353. esac
  354. fi
  355. echo ""
  356. echo "Do you want to customize encryption settings?"
  357. echo "Unless you know what you're doing, you should stick with the default parameters provided by the script."
  358. echo "Note that whatever you choose, all the choices presented in the script are safe. (Unlike OpenVPN's defaults)"
  359. echo "See https://github.com/angristan/openvpn-install#security-and-encryption to learn more."
  360. echo ""
  361. until [[ $CUSTOMIZE_ENC =~ (y|n) ]]; do
  362. read -rp "Customize encryption settings? [y/n]: " -e -i n CUSTOMIZE_ENC
  363. done
  364. if [[ $CUSTOMIZE_ENC == "n" ]]; then
  365. # Use default, sane and fast parameters
  366. CIPHER="AES-128-GCM"
  367. CERT_TYPE="1" # ECDSA
  368. CERT_CURVE="prime256v1"
  369. CC_CIPHER="TLS-ECDHE-ECDSA-WITH-AES-128-GCM-SHA256"
  370. DH_TYPE="1" # ECDH
  371. DH_CURVE="prime256v1"
  372. HMAC_ALG="SHA256"
  373. TLS_SIG="1" # tls-crypt
  374. else
  375. echo ""
  376. echo "Choose which cipher you want to use for the data channel:"
  377. echo " 1) AES-128-GCM (recommended)"
  378. echo " 2) AES-192-GCM"
  379. echo " 3) AES-256-GCM"
  380. echo " 4) AES-128-CBC"
  381. echo " 5) AES-192-CBC"
  382. echo " 6) AES-256-CBC"
  383. until [[ $CIPHER_CHOICE =~ ^[1-6]$ ]]; do
  384. read -rp "Cipher [1-6]: " -e -i 1 CIPHER_CHOICE
  385. done
  386. case $CIPHER_CHOICE in
  387. 1)
  388. CIPHER="AES-128-GCM"
  389. ;;
  390. 2)
  391. CIPHER="AES-192-GCM"
  392. ;;
  393. 3)
  394. CIPHER="AES-256-GCM"
  395. ;;
  396. 4)
  397. CIPHER="AES-128-CBC"
  398. ;;
  399. 5)
  400. CIPHER="AES-192-CBC"
  401. ;;
  402. 6)
  403. CIPHER="AES-256-CBC"
  404. ;;
  405. esac
  406. echo ""
  407. echo "Choose what kind of certificate you want to use:"
  408. echo " 1) ECDSA (recommended)"
  409. echo " 2) RSA"
  410. until [[ $CERT_TYPE =~ ^[1-2]$ ]]; do
  411. read -rp"Certificate key type [1-2]: " -e -i 1 CERT_TYPE
  412. done
  413. case $CERT_TYPE in
  414. 1)
  415. echo ""
  416. echo "Choose which curve you want to use for the certificate's key:"
  417. echo " 1) prime256v1 (recommended)"
  418. echo " 2) secp384r1"
  419. echo " 3) secp521r1"
  420. until [[ $CERT_CURVE_CHOICE =~ ^[1-3]$ ]]; do
  421. read -rp"Curve [1-3]: " -e -i 1 CERT_CURVE_CHOICE
  422. done
  423. case $CERT_CURVE_CHOICE in
  424. 1)
  425. CERT_CURVE="prime256v1"
  426. ;;
  427. 2)
  428. CERT_CURVE="secp384r1"
  429. ;;
  430. 3)
  431. CERT_CURVE="secp521r1"
  432. ;;
  433. esac
  434. ;;
  435. 2)
  436. echo ""
  437. echo "Choose which size you want to use for the certificate's RSA key:"
  438. echo " 1) 2048 bits (recommended)"
  439. echo " 2) 3072 bits"
  440. echo " 3) 4096 bits"
  441. until [[ $RSA_KEY_SIZE_CHOICE =~ ^[1-3]$ ]]; do
  442. read -rp "RSA key size [1-3]: " -e -i 1 RSA_KEY_SIZE_CHOICE
  443. done
  444. case $RSA_KEY_SIZE_CHOICE in
  445. 1)
  446. RSA_KEY_SIZE="2048"
  447. ;;
  448. 2)
  449. RSA_KEY_SIZE="3072"
  450. ;;
  451. 3)
  452. RSA_KEY_SIZE="4096"
  453. ;;
  454. esac
  455. ;;
  456. esac
  457. echo ""
  458. echo "Choose which cipher you want to use for the control channel:"
  459. case $CERT_TYPE in
  460. 1)
  461. echo " 1) ECDHE-ECDSA-AES-128-GCM-SHA256 (recommended)"
  462. echo " 2) ECDHE-ECDSA-AES-256-GCM-SHA384"
  463. until [[ $CC_CIPHER_CHOICE =~ ^[1-2]$ ]]; do
  464. read -rp"Control channel cipher [1-2]: " -e -i 1 CC_CIPHER_CHOICE
  465. done
  466. case $CC_CIPHER_CHOICE in
  467. 1)
  468. CC_CIPHER="TLS-ECDHE-ECDSA-WITH-AES-128-GCM-SHA256"
  469. ;;
  470. 2)
  471. CC_CIPHER="TLS-ECDHE-ECDSA-WITH-AES-256-GCM-SHA384"
  472. ;;
  473. esac
  474. ;;
  475. 2)
  476. echo " 1) ECDHE-RSA-AES-128-GCM-SHA256 (recommended)"
  477. echo " 2) ECDHE-RSA-AES-256-GCM-SHA384"
  478. until [[ $CC_CIPHER_CHOICE =~ ^[1-2]$ ]]; do
  479. read -rp"Control channel cipher [1-2]: " -e -i 1 CC_CIPHER_CHOICE
  480. done
  481. case $CC_CIPHER_CHOICE in
  482. 1)
  483. CC_CIPHER="TLS-ECDHE-RSA-WITH-AES-128-GCM-SHA256"
  484. ;;
  485. 2)
  486. CC_CIPHER="TLS-ECDHE-RSA-WITH-AES-256-GCM-SHA384"
  487. ;;
  488. esac
  489. ;;
  490. esac
  491. echo ""
  492. echo "Choose what kind of Diffie-Hellman key you want to use:"
  493. echo " 1) ECDH (recommended)"
  494. echo " 2) DH"
  495. until [[ $DH_TYPE =~ [1-2] ]]; do
  496. read -rp"DH key type [1-2]: " -e -i 1 DH_TYPE
  497. done
  498. case $DH_TYPE in
  499. 1)
  500. echo ""
  501. echo "Choose which curve you want to use for the ECDH key:"
  502. echo " 1) prime256v1 (recommended)"
  503. echo " 2) secp384r1"
  504. echo " 3) secp521r1"
  505. while [[ $DH_CURVE_CHOICE != "1" && $DH_CURVE_CHOICE != "2" && $DH_CURVE_CHOICE != "3" ]]; do
  506. read -rp"Curve [1-3]: " -e -i 1 DH_CURVE_CHOICE
  507. done
  508. case $DH_CURVE_CHOICE in
  509. 1)
  510. DH_CURVE="prime256v1"
  511. ;;
  512. 2)
  513. DH_CURVE="secp384r1"
  514. ;;
  515. 3)
  516. DH_CURVE="secp521r1"
  517. ;;
  518. esac
  519. ;;
  520. 2)
  521. echo ""
  522. echo "Choose what size of Diffie-Hellman key you want to use:"
  523. echo " 1) 2048 bits (recommended)"
  524. echo " 2) 3072 bits"
  525. echo " 3) 4096 bits"
  526. until [[ $DH_KEY_SIZE_CHOICE =~ ^[1-3]$ ]]; do
  527. read -rp "DH key size [1-3]: " -e -i 1 DH_KEY_SIZE_CHOICE
  528. done
  529. case $DH_KEY_SIZE_CHOICE in
  530. 1)
  531. DH_KEY_SIZE="2048"
  532. ;;
  533. 2)
  534. DH_KEY_SIZE="3072"
  535. ;;
  536. 3)
  537. DH_KEY_SIZE="4096"
  538. ;;
  539. esac
  540. ;;
  541. esac
  542. echo ""
  543. # The "auth" options behaves differently with AEAD ciphers
  544. if [[ $CIPHER =~ CBC$ ]]; then
  545. echo "The digest algorithm authenticates data channel packets and tls-auth packets from the control channel."
  546. elif [[ $CIPHER =~ GCM$ ]]; then
  547. echo "The digest algorithm authenticates tls-auth packets from the control channel."
  548. fi
  549. echo "Which digest algorithm do you want to use for HMAC?"
  550. echo " 1) SHA-256 (recommended)"
  551. echo " 2) SHA-384"
  552. echo " 3) SHA-512"
  553. until [[ $HMAC_ALG_CHOICE =~ ^[1-3]$ ]]; do
  554. read -rp "Digest algorithm [1-3]: " -e -i 1 HMAC_ALG_CHOICE
  555. done
  556. case $HMAC_ALG_CHOICE in
  557. 1)
  558. HMAC_ALG="SHA256"
  559. ;;
  560. 2)
  561. HMAC_ALG="SHA384"
  562. ;;
  563. 3)
  564. HMAC_ALG="SHA512"
  565. ;;
  566. esac
  567. echo ""
  568. echo "You can add an additional layer of security to the control channel with tls-auth and tls-crypt"
  569. echo "tls-auth authenticates the packets, while tls-crypt authenticate and encrypt them."
  570. echo " 1) tls-crypt (recommended)"
  571. echo " 2) tls-auth"
  572. until [[ $TLS_SIG =~ [1-2] ]]; do
  573. read -rp "Control channel additional security mechanism [1-2]: " -e -i 1 TLS_SIG
  574. done
  575. fi
  576. echo ""
  577. echo "Okay, that was all I needed. We are ready to setup your OpenVPN server now."
  578. echo "You will be able to generate a client at the end of the installation."
  579. APPROVE_INSTALL=${APPROVE_INSTALL:-n}
  580. if [[ $APPROVE_INSTALL =~ n ]]; then
  581. read -n1 -r -p "Press any key to continue..."
  582. fi
  583. }
  584. function installOpenVPN() {
  585. if [[ $AUTO_INSTALL == "y" ]]; then
  586. # Set default choices so that no questions will be asked.
  587. APPROVE_INSTALL=${APPROVE_INSTALL:-y}
  588. APPROVE_IP=${APPROVE_IP:-y}
  589. IPV6_SUPPORT=${IPV6_SUPPORT:-n}
  590. PORT_CHOICE=${PORT_CHOICE:-1}
  591. PROTOCOL_CHOICE=${PROTOCOL_CHOICE:-1}
  592. DNS=${DNS:-1}
  593. COMPRESSION_ENABLED=${COMPRESSION_ENABLED:-n}
  594. CUSTOMIZE_ENC=${CUSTOMIZE_ENC:-n}
  595. CLIENT=${CLIENT:-client}
  596. PASS=${PASS:-1}
  597. CONTINUE=${CONTINUE:-y}
  598. # Behind NAT, we'll default to the publicly reachable IPv4/IPv6.
  599. if [[ $IPV6_SUPPORT == "y" ]]; then
  600. PUBLIC_IP=$(curl --retry 5 --retry-connrefused https://ifconfig.co)
  601. else
  602. PUBLIC_IP=$(curl --retry 5 --retry-connrefused -4 https://ifconfig.co)
  603. fi
  604. ENDPOINT=${ENDPOINT:-$PUBLIC_IP}
  605. fi
  606. # Run setup questions first, and set other variables if auto-install
  607. installQuestions
  608. # Get the "public" interface from the default route
  609. NIC=$(ip -4 route ls | grep default | grep -Po '(?<=dev )(\S+)' | head -1)
  610. if [[ -z $NIC ]] && [[ $IPV6_SUPPORT == 'y' ]]; then
  611. NIC=$(ip -6 route show default | sed -ne 's/^default .* dev \([^ ]*\) .*$/\1/p')
  612. fi
  613. # $NIC can not be empty for script rm-openvpn-rules.sh
  614. if [[ -z $NIC ]]; then
  615. echo
  616. echo "Can not detect public interface."
  617. echo "This needs for setup MASQUERADE."
  618. until [[ $CONTINUE =~ (y|n) ]]; do
  619. read -rp "Continue? [y/n]: " -e CONTINUE
  620. done
  621. if [[ $CONTINUE == "n" ]]; then
  622. exit 1
  623. fi
  624. fi
  625. # If OpenVPN isn't installed yet, install it. This script is more-or-less
  626. # idempotent on multiple runs, but will only install OpenVPN from upstream
  627. # the first time.
  628. if [[ ! -e /etc/openvpn/server.conf ]]; then
  629. if [[ $OS =~ (debian|ubuntu) ]]; then
  630. apt-get update
  631. apt-get -y install ca-certificates gnupg
  632. # We add the OpenVPN repo to get the latest version.
  633. if [[ $VERSION_ID == "16.04" ]]; then
  634. echo "deb http://build.openvpn.net/debian/openvpn/stable xenial main" >/etc/apt/sources.list.d/openvpn.list
  635. wget -O - https://swupdate.openvpn.net/repos/repo-public.gpg | apt-key add -
  636. apt-get update
  637. fi
  638. # Ubuntu > 16.04 and Debian > 8 have OpenVPN >= 2.4 without the need of a third party repository.
  639. apt-get install -y openvpn iptables openssl wget ca-certificates curl
  640. elif [[ $OS == 'centos' ]]; then
  641. yum install -y epel-release
  642. yum install -y openvpn iptables openssl wget ca-certificates curl tar 'policycoreutils-python*'
  643. elif [[ $OS == 'oracle' ]]; then
  644. yum install -y oracle-epel-release-el8
  645. yum-config-manager --enable ol8_developer_EPEL
  646. yum install -y openvpn iptables openssl wget ca-certificates curl tar policycoreutils-python-utils
  647. elif [[ $OS == 'amzn' ]]; then
  648. amazon-linux-extras install -y epel
  649. yum install -y openvpn iptables openssl wget ca-certificates curl
  650. elif [[ $OS == 'fedora' ]]; then
  651. dnf install -y openvpn iptables openssl wget ca-certificates curl policycoreutils-python-utils
  652. elif [[ $OS == 'arch' ]]; then
  653. # Install required dependencies and upgrade the system
  654. pacman --needed --noconfirm -Syu openvpn iptables openssl wget ca-certificates curl
  655. fi
  656. # An old version of easy-rsa was available by default in some openvpn packages
  657. if [[ -d /etc/openvpn/easy-rsa/ ]]; then
  658. rm -rf /etc/openvpn/easy-rsa/
  659. fi
  660. fi
  661. # Find out if the machine uses nogroup or nobody for the permissionless group
  662. if grep -qs "^nogroup:" /etc/group; then
  663. NOGROUP=nogroup
  664. else
  665. NOGROUP=nobody
  666. fi
  667. # Install the latest version of easy-rsa from source, if not already installed.
  668. if [[ ! -d /etc/openvpn/easy-rsa/ ]]; then
  669. local version="3.0.7"
  670. wget -O ~/easy-rsa.tgz https://github.com/OpenVPN/easy-rsa/releases/download/v${version}/EasyRSA-${version}.tgz
  671. mkdir -p /etc/openvpn/easy-rsa
  672. tar xzf ~/easy-rsa.tgz --strip-components=1 --directory /etc/openvpn/easy-rsa
  673. rm -f ~/easy-rsa.tgz
  674. cd /etc/openvpn/easy-rsa/ || return
  675. cp /etc/openvpn/easy-rsa/vars.example /etc/openvpn/easy-rsa/vars
  676. sed -i 's/#set_var EASYRSA_REQ_COUNTRY\t"US"/set_var EASYRSA_REQ_COUNTRY\t"MY"/g' /etc/openvpn/easy-rsa/vars
  677. sed -i 's/#set_var EASYRSA_REQ_PROVINCE\t"California"/set_var EASYRSA_REQ_PROVINCE\t"Selangor"/g' /etc/openvpn/easy-rsa/vars
  678. sed -i 's/#set_var EASYRSA_REQ_CITY\t"San Francisco"/set_var EASYRSA_REQ_CITY\t"Gombak"/g' /etc/openvpn/easy-rsa/vars
  679. sed -i 's/#set_var EASYRSA_REQ_ORG\t"Copyleft Certificate Co"/set_var EASYRSA_REQ_ORG\t\t"Aidan Technology"/g' /etc/openvpn/easy-rsa/vars
  680. sed -i 's/#set_var EASYRSA_REQ_EMAIL\t"me@example.net"/set_var EASYRSA_REQ_EMAIL\t"irwan@aidan.my"/g' /etc/openvpn/easy-rsa/vars
  681. sed -i 's/#set_var EASYRSA_REQ_OU\t\t"My Organizational Unit"/set_var EASYRSA_REQ_OU\t\t"Aidan Staff Only"/g' /etc/openvpn/easy-rsa/vars
  682. sed -i 's/#set_var EASYRSA_CA_EXPIRE\t3650/set_var EASYRSA_CA_EXPIRE\t3650/g' /etc/openvpn/easy-rsa/vars
  683. sed -i 's/#set_var EASYRSA_CERT_EXPIRE\t825/set_var EASYRSA_CERT_EXPIRE\t3650/g' /etc/openvpn/easy-rsa/vars
  684. sed -i 's/#set_var EASYRSA_REQ_CN\t\t"ChangeMe"/set_var EASYRSA_REQ_CN\t\t"Aidan VPN"/g' /etc/openvpn/easy-rsa/vars
  685. case $CERT_TYPE in
  686. 1)
  687. echo "set_var EASYRSA_ALGO ec" >vars
  688. echo "set_var EASYRSA_CURVE $CERT_CURVE" >>vars
  689. ;;
  690. 2)
  691. echo "set_var EASYRSA_KEY_SIZE $RSA_KEY_SIZE" >vars
  692. ;;
  693. esac
  694. # Generate a random, alphanumeric identifier of 16 characters for CN and one for server name
  695. SERVER_CN="cn_$(head /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 16 | head -n 1)"
  696. echo "$SERVER_CN" >SERVER_CN_GENERATED
  697. SERVER_NAME="server_$(head /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 16 | head -n 1)"
  698. echo "$SERVER_NAME" >SERVER_NAME_GENERATED
  699. echo "set_var EASYRSA_REQ_CN $SERVER_CN" >>vars
  700. # Create the PKI, set up the CA, the DH params and the server certificate
  701. ./easyrsa init-pki
  702. ./easyrsa --batch build-ca nopass
  703. if [[ $DH_TYPE == "2" ]]; then
  704. # ECDH keys are generated on-the-fly so we don't need to generate them beforehand
  705. openssl dhparam -out dh.pem $DH_KEY_SIZE
  706. fi
  707. ./easyrsa build-server-full "$SERVER_NAME" nopass
  708. EASYRSA_CRL_DAYS=3650 ./easyrsa gen-crl
  709. case $TLS_SIG in
  710. 1)
  711. # Generate tls-crypt key
  712. openvpn --genkey --secret /etc/openvpn/tls-crypt.key
  713. ;;
  714. 2)
  715. # Generate tls-auth key
  716. openvpn --genkey --secret /etc/openvpn/tls-auth.key
  717. ;;
  718. esac
  719. else
  720. # If easy-rsa is already installed, grab the generated SERVER_NAME
  721. # for client configs
  722. cd /etc/openvpn/easy-rsa/ || return
  723. SERVER_NAME=$(cat SERVER_NAME_GENERATED)
  724. fi
  725. # Move all the generated files
  726. cp pki/ca.crt pki/private/ca.key "pki/issued/$SERVER_NAME.crt" "pki/private/$SERVER_NAME.key" /etc/openvpn/easy-rsa/pki/crl.pem /etc/openvpn
  727. if [[ $DH_TYPE == "2" ]]; then
  728. cp dh.pem /etc/openvpn
  729. fi
  730. # Make cert revocation list readable for non-root
  731. chmod 644 /etc/openvpn/crl.pem
  732. # Generate server.conf
  733. echo "port $PORT" >/etc/openvpn/server.conf
  734. if [[ $IPV6_SUPPORT == 'n' ]]; then
  735. echo "proto $PROTOCOL" >>/etc/openvpn/server.conf
  736. elif [[ $IPV6_SUPPORT == 'y' ]]; then
  737. echo "proto ${PROTOCOL}6" >>/etc/openvpn/server.conf
  738. fi
  739. echo "dev tun
  740. user nobody
  741. group $NOGROUP
  742. persist-key
  743. persist-tun
  744. keepalive 10 120
  745. topology subnet
  746. server 10.8.0.0 255.255.255.0
  747. ifconfig-pool-persist ipp.txt" >>/etc/openvpn/server.conf
  748. # DNS resolvers
  749. case $DNS in
  750. 1) # Current system resolvers
  751. # Locate the proper resolv.conf
  752. # Needed for systems running systemd-resolved
  753. if grep -q "127.0.0.53" "/etc/resolv.conf"; then
  754. RESOLVCONF='/run/systemd/resolve/resolv.conf'
  755. else
  756. RESOLVCONF='/etc/resolv.conf'
  757. fi
  758. # Obtain the resolvers from resolv.conf and use them for OpenVPN
  759. sed -ne 's/^nameserver[[:space:]]\+\([^[:space:]]\+\).*$/\1/p' $RESOLVCONF | while read -r line; do
  760. # Copy, if it's a IPv4 |or| if IPv6 is enabled, IPv4/IPv6 does not matter
  761. if [[ $line =~ ^[0-9.]*$ ]] || [[ $IPV6_SUPPORT == 'y' ]]; then
  762. echo "push \"dhcp-option DNS $line\"" >>/etc/openvpn/server.conf
  763. fi
  764. done
  765. ;;
  766. 2) # Self-hosted DNS resolver (Unbound)
  767. echo 'push "dhcp-option DNS 10.8.0.1"' >>/etc/openvpn/server.conf
  768. if [[ $IPV6_SUPPORT == 'y' ]]; then
  769. echo 'push "dhcp-option DNS fd42:42:42:42::1"' >>/etc/openvpn/server.conf
  770. fi
  771. ;;
  772. 3) # Cloudflare
  773. echo 'push "dhcp-option DNS 1.0.0.1"' >>/etc/openvpn/server.conf
  774. echo 'push "dhcp-option DNS 1.1.1.1"' >>/etc/openvpn/server.conf
  775. ;;
  776. 4) # Quad9
  777. echo 'push "dhcp-option DNS 9.9.9.9"' >>/etc/openvpn/server.conf
  778. echo 'push "dhcp-option DNS 149.112.112.112"' >>/etc/openvpn/server.conf
  779. ;;
  780. 5) # Quad9 uncensored
  781. echo 'push "dhcp-option DNS 9.9.9.10"' >>/etc/openvpn/server.conf
  782. echo 'push "dhcp-option DNS 149.112.112.10"' >>/etc/openvpn/server.conf
  783. ;;
  784. 6) # FDN
  785. echo 'push "dhcp-option DNS 80.67.169.40"' >>/etc/openvpn/server.conf
  786. echo 'push "dhcp-option DNS 80.67.169.12"' >>/etc/openvpn/server.conf
  787. ;;
  788. 7) # DNS.WATCH
  789. echo 'push "dhcp-option DNS 84.200.69.80"' >>/etc/openvpn/server.conf
  790. echo 'push "dhcp-option DNS 84.200.70.40"' >>/etc/openvpn/server.conf
  791. ;;
  792. 8) # OpenDNS
  793. echo 'push "dhcp-option DNS 208.67.222.222"' >>/etc/openvpn/server.conf
  794. echo 'push "dhcp-option DNS 208.67.220.220"' >>/etc/openvpn/server.conf
  795. ;;
  796. 9) # Google
  797. echo 'push "dhcp-option DNS 8.8.8.8"' >>/etc/openvpn/server.conf
  798. echo 'push "dhcp-option DNS 8.8.4.4"' >>/etc/openvpn/server.conf
  799. ;;
  800. 10) # Yandex Basic
  801. echo 'push "dhcp-option DNS 77.88.8.8"' >>/etc/openvpn/server.conf
  802. echo 'push "dhcp-option DNS 77.88.8.1"' >>/etc/openvpn/server.conf
  803. ;;
  804. 11) # AdGuard DNS
  805. echo 'push "dhcp-option DNS 94.140.14.14"' >>/etc/openvpn/server.conf
  806. echo 'push "dhcp-option DNS 94.140.15.15"' >>/etc/openvpn/server.conf
  807. ;;
  808. 12) # NextDNS
  809. echo 'push "dhcp-option DNS 45.90.28.167"' >>/etc/openvpn/server.conf
  810. echo 'push "dhcp-option DNS 45.90.30.167"' >>/etc/openvpn/server.conf
  811. ;;
  812. 13) # Custom DNS
  813. echo "push \"dhcp-option DNS $DNS1\"" >>/etc/openvpn/server.conf
  814. if [[ $DNS2 != "" ]]; then
  815. echo "push \"dhcp-option DNS $DNS2\"" >>/etc/openvpn/server.conf
  816. fi
  817. ;;
  818. esac
  819. echo 'push "redirect-gateway def1 bypass-dhcp"' >>/etc/openvpn/server.conf
  820. # IPv6 network settings if needed
  821. if [[ $IPV6_SUPPORT == 'y' ]]; then
  822. echo 'server-ipv6 fd42:42:42:42::/112
  823. tun-ipv6
  824. push tun-ipv6
  825. push "route-ipv6 2000::/3"
  826. push "redirect-gateway ipv6"' >>/etc/openvpn/server.conf
  827. fi
  828. if [[ $COMPRESSION_ENABLED == "y" ]]; then
  829. echo "compress $COMPRESSION_ALG" >>/etc/openvpn/server.conf
  830. fi
  831. if [[ $DH_TYPE == "1" ]]; then
  832. echo "dh none" >>/etc/openvpn/server.conf
  833. echo "ecdh-curve $DH_CURVE" >>/etc/openvpn/server.conf
  834. elif [[ $DH_TYPE == "2" ]]; then
  835. echo "dh dh.pem" >>/etc/openvpn/server.conf
  836. fi
  837. case $TLS_SIG in
  838. 1)
  839. echo "tls-crypt tls-crypt.key" >>/etc/openvpn/server.conf
  840. ;;
  841. 2)
  842. echo "tls-auth tls-auth.key 0" >>/etc/openvpn/server.conf
  843. ;;
  844. esac
  845. echo "crl-verify crl.pem
  846. ca ca.crt
  847. cert $SERVER_NAME.crt
  848. key $SERVER_NAME.key
  849. auth $HMAC_ALG
  850. cipher $CIPHER
  851. ncp-ciphers $CIPHER
  852. tls-server
  853. tls-version-min 1.2
  854. tls-cipher $CC_CIPHER
  855. client-config-dir /etc/openvpn/ccd
  856. status /var/log/openvpn/status.log
  857. verb 3" >>/etc/openvpn/server.conf
  858. # Create client-config-dir dir
  859. mkdir -p /etc/openvpn/ccd
  860. # Create log dir
  861. mkdir -p /var/log/openvpn
  862. # Enable routing
  863. echo 'net.ipv4.ip_forward=1' >/etc/sysctl.d/99-openvpn.conf
  864. if [[ $IPV6_SUPPORT == 'y' ]]; then
  865. echo 'net.ipv6.conf.all.forwarding=1' >>/etc/sysctl.d/99-openvpn.conf
  866. fi
  867. # Apply sysctl rules
  868. sysctl --system
  869. # If SELinux is enabled and a custom port was selected, we need this
  870. if hash sestatus 2>/dev/null; then
  871. if sestatus | grep "Current mode" | grep -qs "enforcing"; then
  872. if [[ $PORT != '1194' ]]; then
  873. semanage port -a -t openvpn_port_t -p "$PROTOCOL" "$PORT"
  874. fi
  875. fi
  876. fi
  877. # Finally, restart and enable OpenVPN
  878. if [[ $OS == 'arch' || $OS == 'fedora' || $OS == 'centos' || $OS == 'oracle' ]]; then
  879. # Don't modify package-provided service
  880. cp /usr/lib/systemd/system/openvpn-server@.service /etc/systemd/system/openvpn-server@.service
  881. # Workaround to fix OpenVPN service on OpenVZ
  882. sed -i 's|LimitNPROC|#LimitNPROC|' /etc/systemd/system/openvpn-server@.service
  883. # Another workaround to keep using /etc/openvpn/
  884. sed -i 's|/etc/openvpn/server|/etc/openvpn|' /etc/systemd/system/openvpn-server@.service
  885. systemctl daemon-reload
  886. systemctl enable openvpn-server@server
  887. systemctl restart openvpn-server@server
  888. elif [[ $OS == "ubuntu" ]] && [[ $VERSION_ID == "16.04" ]]; then
  889. # On Ubuntu 16.04, we use the package from the OpenVPN repo
  890. # This package uses a sysvinit service
  891. systemctl enable openvpn
  892. systemctl start openvpn
  893. else
  894. # Don't modify package-provided service
  895. cp /lib/systemd/system/openvpn\@.service /etc/systemd/system/openvpn\@.service
  896. # Workaround to fix OpenVPN service on OpenVZ
  897. sed -i 's|LimitNPROC|#LimitNPROC|' /etc/systemd/system/openvpn\@.service
  898. # Another workaround to keep using /etc/openvpn/
  899. sed -i 's|/etc/openvpn/server|/etc/openvpn|' /etc/systemd/system/openvpn\@.service
  900. systemctl daemon-reload
  901. systemctl enable openvpn@server
  902. systemctl restart openvpn@server
  903. fi
  904. if [[ $DNS == 2 ]]; then
  905. installUnbound
  906. fi
  907. # Add iptables rules in two scripts
  908. mkdir -p /etc/iptables
  909. # Script to add rules
  910. echo "#!/bin/sh
  911. iptables -t nat -I POSTROUTING 1 -s 10.8.0.0/24 -o $NIC -j MASQUERADE
  912. iptables -I INPUT 1 -i tun0 -j ACCEPT
  913. iptables -I FORWARD 1 -i $NIC -o tun0 -j ACCEPT
  914. iptables -I FORWARD 1 -i tun0 -o $NIC -j ACCEPT
  915. iptables -I INPUT 1 -i $NIC -p $PROTOCOL --dport $PORT -j ACCEPT" >/etc/iptables/add-openvpn-rules.sh
  916. if [[ $IPV6_SUPPORT == 'y' ]]; then
  917. echo "ip6tables -t nat -I POSTROUTING 1 -s fd42:42:42:42::/112 -o $NIC -j MASQUERADE
  918. ip6tables -I INPUT 1 -i tun0 -j ACCEPT
  919. ip6tables -I FORWARD 1 -i $NIC -o tun0 -j ACCEPT
  920. ip6tables -I FORWARD 1 -i tun0 -o $NIC -j ACCEPT
  921. ip6tables -I INPUT 1 -i $NIC -p $PROTOCOL --dport $PORT -j ACCEPT" >>/etc/iptables/add-openvpn-rules.sh
  922. fi
  923. # Script to remove rules
  924. echo "#!/bin/sh
  925. iptables -t nat -D POSTROUTING -s 10.8.0.0/24 -o $NIC -j MASQUERADE
  926. iptables -D INPUT -i tun0 -j ACCEPT
  927. iptables -D FORWARD -i $NIC -o tun0 -j ACCEPT
  928. iptables -D FORWARD -i tun0 -o $NIC -j ACCEPT
  929. iptables -D INPUT -i $NIC -p $PROTOCOL --dport $PORT -j ACCEPT" >/etc/iptables/rm-openvpn-rules.sh
  930. if [[ $IPV6_SUPPORT == 'y' ]]; then
  931. echo "ip6tables -t nat -D POSTROUTING -s fd42:42:42:42::/112 -o $NIC -j MASQUERADE
  932. ip6tables -D INPUT -i tun0 -j ACCEPT
  933. ip6tables -D FORWARD -i $NIC -o tun0 -j ACCEPT
  934. ip6tables -D FORWARD -i tun0 -o $NIC -j ACCEPT
  935. ip6tables -D INPUT -i $NIC -p $PROTOCOL --dport $PORT -j ACCEPT" >>/etc/iptables/rm-openvpn-rules.sh
  936. fi
  937. chmod +x /etc/iptables/add-openvpn-rules.sh
  938. chmod +x /etc/iptables/rm-openvpn-rules.sh
  939. # Handle the rules via a systemd script
  940. echo "[Unit]
  941. Description=iptables rules for OpenVPN
  942. Before=network-online.target
  943. Wants=network-online.target
  944. [Service]
  945. Type=oneshot
  946. ExecStart=/etc/iptables/add-openvpn-rules.sh
  947. ExecStop=/etc/iptables/rm-openvpn-rules.sh
  948. RemainAfterExit=yes
  949. [Install]
  950. WantedBy=multi-user.target" >/etc/systemd/system/iptables-openvpn.service
  951. # Enable service and apply rules
  952. systemctl daemon-reload
  953. systemctl enable iptables-openvpn
  954. systemctl start iptables-openvpn
  955. # If the server is behind a NAT, use the correct IP address for the clients to connect to
  956. if [[ $ENDPOINT != "" ]]; then
  957. IP=$ENDPOINT
  958. fi
  959. # client-template.txt is created so we have a template to add further users later
  960. echo "client" >/etc/openvpn/client-template.txt
  961. if [[ $PROTOCOL == 'udp' ]]; then
  962. echo "proto udp" >>/etc/openvpn/client-template.txt
  963. echo "explicit-exit-notify" >>/etc/openvpn/client-template.txt
  964. elif [[ $PROTOCOL == 'tcp' ]]; then
  965. echo "proto tcp-client" >>/etc/openvpn/client-template.txt
  966. fi
  967. echo "remote $IP $PORT
  968. dev tun
  969. resolv-retry infinite
  970. nobind
  971. persist-key
  972. persist-tun
  973. remote-cert-tls server
  974. verify-x509-name $SERVER_NAME name
  975. auth $HMAC_ALG
  976. auth-nocache
  977. cipher $CIPHER
  978. tls-client
  979. tls-version-min 1.2
  980. tls-cipher $CC_CIPHER
  981. ignore-unknown-option block-outside-dns
  982. setenv opt block-outside-dns # Prevent Windows 10 DNS leak
  983. verb 3" >>/etc/openvpn/client-template.txt
  984. if [[ $COMPRESSION_ENABLED == "y" ]]; then
  985. echo "compress $COMPRESSION_ALG" >>/etc/openvpn/client-template.txt
  986. fi
  987. # Generate the custom client.ovpn
  988. newClient
  989. echo "If you want to add more clients, you simply need to run this script another time!"
  990. }
  991. function newClient() {
  992. echo ""
  993. echo "Tell me a name for the client."
  994. echo "The name must consist of alphanumeric character. It may also include an underscore or a dash."
  995. until [[ $CLIENT =~ ^[a-zA-Z0-9_-]+$ ]]; do
  996. read -rp "Client name: " -e CLIENT
  997. done
  998. echo ""
  999. echo "Do you want to protect the configuration file with a password?"
  1000. echo "(e.g. encrypt the private key with a password)"
  1001. echo " 1) Add a passwordless client"
  1002. echo " 2) Use a password for the client"
  1003. until [[ $PASS =~ ^[1-2]$ ]]; do
  1004. read -rp "Select an option [1-2]: " -e -i 1 PASS
  1005. done
  1006. CLIENTEXISTS=$(tail -n +2 /etc/openvpn/easy-rsa/pki/index.txt | grep -c -E "/CN=$CLIENT\$")
  1007. if [[ $CLIENTEXISTS == '1' ]]; then
  1008. echo ""
  1009. echo "The specified client CN was already found in easy-rsa, please choose another name."
  1010. exit
  1011. else
  1012. cd /etc/openvpn/easy-rsa/ || return
  1013. case $PASS in
  1014. 1)
  1015. ./easyrsa build-client-full "$CLIENT" nopass
  1016. ;;
  1017. 2)
  1018. echo "⚠️ You will be asked for the client password below ⚠️"
  1019. ./easyrsa build-client-full "$CLIENT"
  1020. ;;
  1021. esac
  1022. echo "Client $CLIENT added."
  1023. fi
  1024. # Home directory of the user, where the client configuration will be written
  1025. if [ -e "/home/${CLIENT}" ]; then
  1026. # if $1 is a user name
  1027. homeDir="/home/${CLIENT}"
  1028. elif [ "${SUDO_USER}" ]; then
  1029. # if not, use SUDO_USER
  1030. if [ "${SUDO_USER}" == "root" ]; then
  1031. # If running sudo as root
  1032. homeDir="/root"
  1033. else
  1034. homeDir="/home/${SUDO_USER}"
  1035. fi
  1036. else
  1037. # if not SUDO_USER, use /root
  1038. homeDir="/root"
  1039. fi
  1040. # Determine if we use tls-auth or tls-crypt
  1041. if grep -qs "^tls-crypt" /etc/openvpn/server.conf; then
  1042. TLS_SIG="1"
  1043. elif grep -qs "^tls-auth" /etc/openvpn/server.conf; then
  1044. TLS_SIG="2"
  1045. fi
  1046. # Generates the custom client.ovpn
  1047. cp /etc/openvpn/client-template.txt "$homeDir/$CLIENT.ovpn"
  1048. {
  1049. echo "<ca>"
  1050. cat "/etc/openvpn/easy-rsa/pki/ca.crt"
  1051. echo "</ca>"
  1052. echo "<cert>"
  1053. awk '/BEGIN/,/END/' "/etc/openvpn/easy-rsa/pki/issued/$CLIENT.crt"
  1054. echo "</cert>"
  1055. echo "<key>"
  1056. cat "/etc/openvpn/easy-rsa/pki/private/$CLIENT.key"
  1057. echo "</key>"
  1058. case $TLS_SIG in
  1059. 1)
  1060. echo "<tls-crypt>"
  1061. cat /etc/openvpn/tls-crypt.key
  1062. echo "</tls-crypt>"
  1063. ;;
  1064. 2)
  1065. echo "key-direction 1"
  1066. echo "<tls-auth>"
  1067. cat /etc/openvpn/tls-auth.key
  1068. echo "</tls-auth>"
  1069. ;;
  1070. esac
  1071. } >>"$homeDir/$CLIENT.ovpn"
  1072. echo ""
  1073. echo "The configuration file has been written to $homeDir/$CLIENT.ovpn."
  1074. echo "Download the .ovpn file and import it in your OpenVPN client."
  1075. exit 0
  1076. }
  1077. function revokeClient() {
  1078. NUMBEROFCLIENTS=$(tail -n +2 /etc/openvpn/easy-rsa/pki/index.txt | grep -c "^V")
  1079. if [[ $NUMBEROFCLIENTS == '0' ]]; then
  1080. echo ""
  1081. echo "You have no existing clients!"
  1082. exit 1
  1083. fi
  1084. echo ""
  1085. echo "Select the existing client certificate you want to revoke"
  1086. tail -n +2 /etc/openvpn/easy-rsa/pki/index.txt | grep "^V" | cut -d '=' -f 2 | nl -s ') '
  1087. until [[ $CLIENTNUMBER -ge 1 && $CLIENTNUMBER -le $NUMBEROFCLIENTS ]]; do
  1088. if [[ $CLIENTNUMBER == '1' ]]; then
  1089. read -rp "Select one client [1]: " CLIENTNUMBER
  1090. else
  1091. read -rp "Select one client [1-$NUMBEROFCLIENTS]: " CLIENTNUMBER
  1092. fi
  1093. done
  1094. CLIENT=$(tail -n +2 /etc/openvpn/easy-rsa/pki/index.txt | grep "^V" | cut -d '=' -f 2 | sed -n "$CLIENTNUMBER"p)
  1095. cd /etc/openvpn/easy-rsa/ || return
  1096. ./easyrsa --batch revoke "$CLIENT"
  1097. EASYRSA_CRL_DAYS=3650 ./easyrsa gen-crl
  1098. rm -f /etc/openvpn/crl.pem
  1099. cp /etc/openvpn/easy-rsa/pki/crl.pem /etc/openvpn/crl.pem
  1100. chmod 644 /etc/openvpn/crl.pem
  1101. find /home/ -maxdepth 2 -name "$CLIENT.ovpn" -delete
  1102. rm -f "/root/$CLIENT.ovpn"
  1103. sed -i "/^$CLIENT,.*/d" /etc/openvpn/ipp.txt
  1104. cp /etc/openvpn/easy-rsa/pki/index.txt{,.bk}
  1105. echo ""
  1106. echo "Certificate for client $CLIENT revoked."
  1107. }
  1108. function removeUnbound() {
  1109. # Remove OpenVPN-related config
  1110. sed -i '/include: \/etc\/unbound\/openvpn.conf/d' /etc/unbound/unbound.conf
  1111. rm /etc/unbound/openvpn.conf
  1112. until [[ $REMOVE_UNBOUND =~ (y|n) ]]; do
  1113. echo ""
  1114. echo "If you were already using Unbound before installing OpenVPN, I removed the configuration related to OpenVPN."
  1115. read -rp "Do you want to completely remove Unbound? [y/n]: " -e REMOVE_UNBOUND
  1116. done
  1117. if [[ $REMOVE_UNBOUND == 'y' ]]; then
  1118. # Stop Unbound
  1119. systemctl stop unbound
  1120. if [[ $OS =~ (debian|ubuntu) ]]; then
  1121. apt-get remove --purge -y unbound
  1122. elif [[ $OS == 'arch' ]]; then
  1123. pacman --noconfirm -R unbound
  1124. elif [[ $OS =~ (centos|amzn|oracle) ]]; then
  1125. yum remove -y unbound
  1126. elif [[ $OS == 'fedora' ]]; then
  1127. dnf remove -y unbound
  1128. fi
  1129. rm -rf /etc/unbound/
  1130. echo ""
  1131. echo "Unbound removed!"
  1132. else
  1133. systemctl restart unbound
  1134. echo ""
  1135. echo "Unbound wasn't removed."
  1136. fi
  1137. }
  1138. function removeOpenVPN() {
  1139. echo ""
  1140. read -rp "Do you really want to remove OpenVPN? [y/n]: " -e -i n REMOVE
  1141. if [[ $REMOVE == 'y' ]]; then
  1142. # Get OpenVPN port from the configuration
  1143. PORT=$(grep '^port ' /etc/openvpn/server.conf | cut -d " " -f 2)
  1144. PROTOCOL=$(grep '^proto ' /etc/openvpn/server.conf | cut -d " " -f 2)
  1145. # Stop OpenVPN
  1146. if [[ $OS =~ (fedora|arch|centos|oracle) ]]; then
  1147. systemctl disable openvpn-server@server
  1148. systemctl stop openvpn-server@server
  1149. # Remove customised service
  1150. rm /etc/systemd/system/openvpn-server@.service
  1151. elif [[ $OS == "ubuntu" ]] && [[ $VERSION_ID == "16.04" ]]; then
  1152. systemctl disable openvpn
  1153. systemctl stop openvpn
  1154. else
  1155. systemctl disable openvpn@server
  1156. systemctl stop openvpn@server
  1157. # Remove customised service
  1158. rm /etc/systemd/system/openvpn\@.service
  1159. fi
  1160. # Remove the iptables rules related to the script
  1161. systemctl stop iptables-openvpn
  1162. # Cleanup
  1163. systemctl disable iptables-openvpn
  1164. rm /etc/systemd/system/iptables-openvpn.service
  1165. systemctl daemon-reload
  1166. rm /etc/iptables/add-openvpn-rules.sh
  1167. rm /etc/iptables/rm-openvpn-rules.sh
  1168. # SELinux
  1169. if hash sestatus 2>/dev/null; then
  1170. if sestatus | grep "Current mode" | grep -qs "enforcing"; then
  1171. if [[ $PORT != '1194' ]]; then
  1172. semanage port -d -t openvpn_port_t -p "$PROTOCOL" "$PORT"
  1173. fi
  1174. fi
  1175. fi
  1176. if [[ $OS =~ (debian|ubuntu) ]]; then
  1177. apt-get remove --purge -y openvpn
  1178. if [[ -e /etc/apt/sources.list.d/openvpn.list ]]; then
  1179. rm /etc/apt/sources.list.d/openvpn.list
  1180. apt-get update
  1181. fi
  1182. elif [[ $OS == 'arch' ]]; then
  1183. pacman --noconfirm -R openvpn
  1184. elif [[ $OS =~ (centos|amzn|oracle) ]]; then
  1185. yum remove -y openvpn
  1186. elif [[ $OS == 'fedora' ]]; then
  1187. dnf remove -y openvpn
  1188. fi
  1189. # Cleanup
  1190. find /home/ -maxdepth 2 -name "*.ovpn" -delete
  1191. find /root/ -maxdepth 1 -name "*.ovpn" -delete
  1192. rm -rf /etc/openvpn
  1193. rm -rf /usr/share/doc/openvpn*
  1194. rm -f /etc/sysctl.d/99-openvpn.conf
  1195. rm -rf /var/log/openvpn
  1196. # Unbound
  1197. if [[ -e /etc/unbound/openvpn.conf ]]; then
  1198. removeUnbound
  1199. fi
  1200. echo ""
  1201. echo "OpenVPN removed!"
  1202. else
  1203. echo ""
  1204. echo "Removal aborted!"
  1205. fi
  1206. }
  1207. function manageMenu() {
  1208. echo "Welcome to OpenVPN-install!"
  1209. echo "The git repository is available at: https://github.com/angristan/openvpn-install"
  1210. echo ""
  1211. echo "It looks like OpenVPN is already installed."
  1212. echo ""
  1213. echo "What do you want to do?"
  1214. echo " 1) Add a new user"
  1215. echo " 2) Revoke existing user"
  1216. echo " 3) Remove OpenVPN"
  1217. echo " 4) Exit"
  1218. until [[ $MENU_OPTION =~ ^[1-4]$ ]]; do
  1219. read -rp "Select an option [1-4]: " MENU_OPTION
  1220. done
  1221. case $MENU_OPTION in
  1222. 1)
  1223. newClient
  1224. ;;
  1225. 2)
  1226. revokeClient
  1227. ;;
  1228. 3)
  1229. removeOpenVPN
  1230. ;;
  1231. 4)
  1232. exit 0
  1233. ;;
  1234. esac
  1235. }
  1236. # Check for root, TUN, OS...
  1237. initialCheck
  1238. # Check if OpenVPN is already installed
  1239. if [[ -e /etc/openvpn/server.conf && $AUTO_INSTALL != "y" ]]; then
  1240. manageMenu
  1241. else
  1242. installOpenVPN
  1243. fi