l2tp.sh 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #!/bin/bash
  2. PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
  3. export PATH
  4. #
  5. # This is a Shell script for configure and start L2TP/IPSec VPN server with Docker image
  6. #
  7. # Copyright (C) 2018 Teddysun <[email protected]>
  8. #
  9. # Reference URL:
  10. # https://github.com/libreswan/libreswan
  11. # https://github.com/xelerance/xl2tpd
  12. if [ ! -f "/.dockerenv" ]; then
  13. echo "Error: This script must be run in a Docker container." >&2
  14. exit 1
  15. fi
  16. if ip link add dummy0 type dummy 2>&1 | grep -q "not permitted"; then
  17. echo "Error: This Docker image must be run in privileged mode." >&2
  18. exit 1
  19. fi
  20. ip link delete dummy0 >/dev/null 2>&1
  21. rand(){
  22. index=0
  23. str=""
  24. for i in {a..z}; do arr[index]=${i}; index=$(expr ${index} + 1); done
  25. for i in {A..Z}; do arr[index]=${i}; index=$(expr ${index} + 1); done
  26. for i in {0..9}; do arr[index]=${i}; index=$(expr ${index} + 1); done
  27. for i in {1..10}; do str="$str${arr[$RANDOM%$index]}"; done
  28. echo ${str}
  29. }
  30. is_64bit(){
  31. if [ "$(getconf WORD_BIT)" == "32" ] && [ "$(getconf LONG_BIT)" == "64" ]; then
  32. return 0
  33. else
  34. return 1
  35. fi
  36. }
  37. # Environment file name
  38. l2tp_env_file="/etc/l2tp.env"
  39. # Auto generated
  40. if [ -z "${VPN_IPSEC_PSK}" ] && [ -z "${VPN_USER}" ] && [ -z "${VPN_PASSWORD}" ]; then
  41. if [ -f "${l2tp_env_file}" ]; then
  42. echo "Loading previously generated environment variables for L2TP/IPSec VPN Server..."
  43. . "${l2tp_env_file}"
  44. else
  45. echo "L2TP/IPSec VPN Server environment variables is not set. Use default environment variables..."
  46. VPN_IPSEC_PSK="teddysun.com"
  47. VPN_USER="vpnuser"
  48. VPN_PASSWORD="$(rand)"
  49. echo "VPN_IPSEC_PSK=${VPN_IPSEC_PSK}" > ${l2tp_env_file}
  50. echo "VPN_USER=${VPN_USER}" >> ${l2tp_env_file}
  51. echo "VPN_PASSWORD=${VPN_PASSWORD}" >> ${l2tp_env_file}
  52. chmod 600 ${l2tp_env_file}
  53. fi
  54. fi
  55. # Environment variables:
  56. # VPN_IPSEC_PSK
  57. # VPN_USER
  58. # VPN_PASSWORD
  59. if [ -z "${VPN_IPSEC_PSK}" ] || [ -z "${VPN_USER}" ] || [ -z "${VPN_PASSWORD}" ]; then
  60. echo "Error: Environment variables must be specified. please edit your environment file and retry again." >&2
  61. exit 1
  62. fi
  63. if printf '%s' "${VPN_IPSEC_PSK} ${VPN_USER} ${VPN_PASSWORD}" | LC_ALL=C grep -q '[^ -~]\+'; then
  64. echo "Error: Environment variables must not contain non-ASCII characters." >&2
  65. exit 1
  66. fi
  67. case "${VPN_IPSEC_PSK} ${VPN_USER} ${VPN_PASSWORD}" in
  68. *[\\\"\']*)
  69. echo "Error: Environment variables must not contain these special characters like: \\ \" '"
  70. exit 1
  71. ;;
  72. esac
  73. # Environment variables:
  74. # VPN_PUBLIC_IP
  75. PUBLIC_IP=${VPN_PUBLIC_IP:-''}
  76. [ -z "${PUBLIC_IP}" ] && PUBLIC_IP=$( wget -qO- -t1 -T2 ipv4.icanhazip.com )
  77. [ -z "${PUBLIC_IP}" ] && PUBLIC_IP=$( wget -qO- -t1 -T2 ipinfo.io/ip )
  78. # Environment variables:
  79. # VPN_L2TP_NET
  80. # VPN_L2TP_LOCAL
  81. # VPN_L2TP_REMOTE
  82. # VPN_XAUTH_NET
  83. # VPN_XAUTH_REMOTE
  84. # VPN_DNS1
  85. # VPN_DNS2
  86. L2TP_NET=${VPN_L2TP_NET:-'192.168.18.0/24'}
  87. L2TP_LOCAL=${VPN_L2TP_LOCAL:-'192.168.18.1'}
  88. L2TP_REMOTE=${VPN_L2TP_REMOTE:-'192.168.18.10-192.168.18.250'}
  89. XAUTH_NET=${VPN_XAUTH_NET:-'192.168.20.0/24'}
  90. XAUTH_REMOTE=${VPN_XAUTH_REMOTE:-'192.168.20.10-192.168.20.250'}
  91. DNS1=${VPN_DNS1:-'8.8.8.8'}
  92. DNS2=${VPN_DNS2:-'8.8.4.4'}
  93. # Create IPSec config
  94. cat > /etc/ipsec.conf <<EOF
  95. version 2.0
  96. config setup
  97. protostack=netkey
  98. nhelpers=0
  99. uniqueids=no
  100. interfaces=%defaultroute
  101. virtual-private=%v4:10.0.0.0/8,%v4:192.168.0.0/16,%v4:172.16.0.0/12,%v4:!${L2TP_NET},%v4:!${XAUTH_NET}
  102. conn shared
  103. left=%defaultroute
  104. leftid=${PUBLIC_IP}
  105. right=%any
  106. encapsulation=yes
  107. authby=secret
  108. pfs=no
  109. rekey=no
  110. keyingtries=5
  111. dpddelay=30
  112. dpdtimeout=120
  113. dpdaction=clear
  114. ike=3des-sha1,3des-sha2,aes-sha1,aes-sha1;modp1024,aes-sha2,aes-sha2;modp1024
  115. phase2alg=3des-sha1,3des-sha2,aes-sha1,aes-sha2,aes256-sha2_512
  116. sha2-truncbug=yes
  117. conn l2tp-psk
  118. auto=add
  119. leftprotoport=17/1701
  120. rightprotoport=17/%any
  121. type=transport
  122. phase2=esp
  123. also=shared
  124. conn xauth-psk
  125. auto=add
  126. leftsubnet=0.0.0.0/0
  127. rightaddresspool=${XAUTH_REMOTE}
  128. modecfgdns=${DNS1},${DNS2}
  129. leftxauthserver=yes
  130. rightxauthclient=yes
  131. leftmodecfgserver=yes
  132. rightmodecfgclient=yes
  133. modecfgpull=yes
  134. xauthby=file
  135. ike-frag=yes
  136. ikev2=never
  137. cisco-unity=yes
  138. also=shared
  139. EOF
  140. cat > /etc/xl2tpd/xl2tpd.conf <<EOF
  141. [global]
  142. port = 1701
  143. [lns default]
  144. local ip = ${L2TP_LOCAL}
  145. ip range = ${L2TP_REMOTE}
  146. require chap = yes
  147. refuse pap = yes
  148. require authentication = yes
  149. name = l2tpd
  150. pppoptfile = /etc/ppp/options.xl2tpd
  151. length bit = yes
  152. EOF
  153. cat > /etc/ppp/options.xl2tpd <<EOF
  154. +mschap-v2
  155. ipcp-accept-local
  156. ipcp-accept-remote
  157. ms-dns ${DNS1}
  158. ms-dns ${DNS2}
  159. noccp
  160. auth
  161. mtu 1280
  162. mru 1280
  163. proxyarp
  164. lcp-echo-failure 4
  165. lcp-echo-interval 30
  166. connect-delay 5000
  167. EOF
  168. cat > /etc/ipsec.secrets <<EOF
  169. %any %any : PSK "${VPN_IPSEC_PSK}"
  170. EOF
  171. cat > /etc/ppp/chap-secrets <<EOF
  172. ${VPN_USER} l2tpd ${VPN_PASSWORD} *
  173. EOF
  174. VPN_PASSWORD_ENC=$(openssl passwd -1 "${VPN_PASSWORD}")
  175. cat > /etc/ipsec.d/passwd <<EOF
  176. ${VPN_USER}:${VPN_PASSWORD_ENC}:xauth-psk
  177. EOF
  178. chmod 600 /etc/ipsec.secrets /etc/ppp/chap-secrets /etc/ipsec.d/passwd
  179. # Update sysctl settings
  180. if is_64bit; then
  181. SHM_MAX=68719476736
  182. SHM_ALL=4294967296
  183. else
  184. SHM_MAX=4294967295
  185. SHM_ALL=268435456
  186. fi
  187. sysctl -eqw kernel.msgmnb=65536
  188. sysctl -eqw kernel.msgmax=65536
  189. sysctl -eqw kernel.shmmax=${SHM_MAX}
  190. sysctl -eqw kernel.shmall=${SHM_ALL}
  191. sysctl -eqw net.ipv4.ip_forward=1
  192. sysctl -eqw net.ipv4.conf.all.accept_source_route=0
  193. sysctl -eqw net.ipv4.conf.all.accept_redirects=0
  194. sysctl -eqw net.ipv4.conf.all.send_redirects=0
  195. sysctl -eqw net.ipv4.conf.all.rp_filter=0
  196. sysctl -eqw net.ipv4.conf.default.accept_source_route=0
  197. sysctl -eqw net.ipv4.conf.default.accept_redirects=0
  198. sysctl -eqw net.ipv4.conf.default.send_redirects=0
  199. sysctl -eqw net.ipv4.conf.default.rp_filter=0
  200. sysctl -eqw net.ipv4.conf.eth0.send_redirects=0
  201. sysctl -eqw net.ipv4.conf.eth0.rp_filter=0
  202. # Create iptables rules
  203. iptables -I INPUT 1 -p udp --dport 1701 -m policy --dir in --pol none -j DROP
  204. iptables -I INPUT 2 -m conntrack --ctstate INVALID -j DROP
  205. iptables -I INPUT 3 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
  206. iptables -I INPUT 4 -p udp -m multiport --dports 500,4500 -j ACCEPT
  207. iptables -I INPUT 5 -p udp --dport 1701 -m policy --dir in --pol ipsec -j ACCEPT
  208. iptables -I INPUT 6 -p udp --dport 1701 -j DROP
  209. iptables -I FORWARD 1 -m conntrack --ctstate INVALID -j DROP
  210. iptables -I FORWARD 2 -i eth+ -o ppp+ -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
  211. iptables -I FORWARD 3 -i ppp+ -o eth+ -j ACCEPT
  212. iptables -I FORWARD 4 -i ppp+ -o ppp+ -s "${L2TP_NET}" -d "${L2TP_NET}" -j ACCEPT
  213. iptables -I FORWARD 5 -i eth+ -d "${XAUTH_NET}" -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
  214. iptables -I FORWARD 6 -s "${XAUTH_NET}" -o eth+ -j ACCEPT
  215. iptables -A FORWARD -j DROP
  216. iptables -t nat -I POSTROUTING -s "${XAUTH_NET}" -o eth+ -m policy --dir out --pol none -j MASQUERADE
  217. iptables -t nat -I POSTROUTING -s "${L2TP_NET}" -o eth+ -j MASQUERADE
  218. cat <<EOF
  219. L2TP/IPsec VPN Server with the Username and Password is below:
  220. Server IP: ${PUBLIC_IP}
  221. IPSec PSK: ${VPN_IPSEC_PSK}
  222. Username : ${VPN_USER}
  223. Password : ${VPN_PASSWORD}
  224. EOF
  225. # Load IPsec kernel module
  226. modprobe af_key
  227. # Start services
  228. mkdir -p /run/pluto /var/run/pluto /var/run/xl2tpd
  229. rm -f /run/pluto/pluto.pid /var/run/pluto/pluto.pid /var/run/xl2tpd.pid
  230. /usr/sbin/ipsec start
  231. exec /usr/sbin/xl2tpd -D -c /etc/xl2tpd/xl2tpd.conf