l2tp.sh 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. #!/usr/bin/env bash
  2. PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
  3. export PATH
  4. #=======================================================================#
  5. # System Supported: CentOS 6+ / Debian 7+ / Ubuntu 12+ #
  6. # Description: L2TP VPN Auto Installer #
  7. # Author: Teddysun <[email protected]> #
  8. # Intro: https://teddysun.com/448.html #
  9. #=======================================================================#
  10. cur_dir=`pwd`
  11. libreswan_filename="libreswan-3.27"
  12. download_root_url="https://dl.lamp.sh/files"
  13. rootness(){
  14. if [[ $EUID -ne 0 ]]; then
  15. echo "Error:This script must be run as root!" 1>&2
  16. exit 1
  17. fi
  18. }
  19. tunavailable(){
  20. if [[ ! -e /dev/net/tun ]]; then
  21. echo "Error:TUN/TAP is not available!" 1>&2
  22. exit 1
  23. fi
  24. }
  25. disable_selinux(){
  26. if [ -s /etc/selinux/config ] && grep 'SELINUX=enforcing' /etc/selinux/config; then
  27. sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
  28. setenforce 0
  29. fi
  30. }
  31. get_opsy(){
  32. [ -f /etc/redhat-release ] && awk '{print ($1,$3~/^[0-9]/?$3:$4)}' /etc/redhat-release && return
  33. [ -f /etc/os-release ] && awk -F'[= "]' '/PRETTY_NAME/{print $3,$4,$5}' /etc/os-release && return
  34. [ -f /etc/lsb-release ] && awk -F'[="]+' '/DESCRIPTION/{print $2}' /etc/lsb-release && return
  35. }
  36. get_os_info(){
  37. IP=$( ip addr | egrep -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | egrep -v "^192\.168|^172\.1[6-9]\.|^172\.2[0-9]\.|^172\.3[0-2]\.|^10\.|^127\.|^255\.|^0\." | head -n 1 )
  38. [ -z ${IP} ] && IP=$( wget -qO- -t1 -T2 ipv4.icanhazip.com )
  39. local cname=$( awk -F: '/model name/ {name=$2} END {print name}' /proc/cpuinfo | sed 's/^[ \t]*//;s/[ \t]*$//' )
  40. local cores=$( awk -F: '/model name/ {core++} END {print core}' /proc/cpuinfo )
  41. local freq=$( awk -F: '/cpu MHz/ {freq=$2} END {print freq}' /proc/cpuinfo | sed 's/^[ \t]*//;s/[ \t]*$//' )
  42. local tram=$( free -m | awk '/Mem/ {print $2}' )
  43. local swap=$( free -m | awk '/Swap/ {print $2}' )
  44. local up=$( awk '{a=$1/86400;b=($1%86400)/3600;c=($1%3600)/60;d=$1%60} {printf("%ddays, %d:%d:%d\n",a,b,c,d)}' /proc/uptime )
  45. local load=$( w | head -1 | awk -F'load average:' '{print $2}' | sed 's/^[ \t]*//;s/[ \t]*$//' )
  46. local opsy=$( get_opsy )
  47. local arch=$( uname -m )
  48. local lbit=$( getconf LONG_BIT )
  49. local host=$( hostname )
  50. local kern=$( uname -r )
  51. echo "########## System Information ##########"
  52. echo
  53. echo "CPU model : ${cname}"
  54. echo "Number of cores : ${cores}"
  55. echo "CPU frequency : ${freq} MHz"
  56. echo "Total amount of ram : ${tram} MB"
  57. echo "Total amount of swap : ${swap} MB"
  58. echo "System uptime : ${up}"
  59. echo "Load average : ${load}"
  60. echo "OS : ${opsy}"
  61. echo "Arch : ${arch} (${lbit} Bit)"
  62. echo "Kernel : ${kern}"
  63. echo "Hostname : ${host}"
  64. echo "IPv4 address : ${IP}"
  65. echo
  66. echo "########################################"
  67. }
  68. check_sys(){
  69. local checkType=$1
  70. local value=$2
  71. local release=''
  72. local systemPackage=''
  73. if [[ -f /etc/redhat-release ]]; then
  74. release="centos"
  75. systemPackage="yum"
  76. elif cat /etc/issue | grep -Eqi "debian"; then
  77. release="debian"
  78. systemPackage="apt"
  79. elif cat /etc/issue | grep -Eqi "ubuntu"; then
  80. release="ubuntu"
  81. systemPackage="apt"
  82. elif cat /etc/issue | grep -Eqi "centos|red hat|redhat"; then
  83. release="centos"
  84. systemPackage="yum"
  85. elif cat /proc/version | grep -Eqi "debian"; then
  86. release="debian"
  87. systemPackage="apt"
  88. elif cat /proc/version | grep -Eqi "ubuntu"; then
  89. release="ubuntu"
  90. systemPackage="apt"
  91. elif cat /proc/version | grep -Eqi "centos|red hat|redhat"; then
  92. release="centos"
  93. systemPackage="yum"
  94. fi
  95. if [[ ${checkType} == "sysRelease" ]]; then
  96. if [ "$value" == "$release" ];then
  97. return 0
  98. else
  99. return 1
  100. fi
  101. elif [[ ${checkType} == "packageManager" ]]; then
  102. if [ "$value" == "$systemPackage" ];then
  103. return 0
  104. else
  105. return 1
  106. fi
  107. fi
  108. }
  109. rand(){
  110. index=0
  111. str=""
  112. for i in {a..z}; do arr[index]=${i}; index=`expr ${index} + 1`; done
  113. for i in {A..Z}; do arr[index]=${i}; index=`expr ${index} + 1`; done
  114. for i in {0..9}; do arr[index]=${i}; index=`expr ${index} + 1`; done
  115. for i in {1..10}; do str="$str${arr[$RANDOM%$index]}"; done
  116. echo ${str}
  117. }
  118. is_64bit(){
  119. if [ `getconf WORD_BIT` = '32' ] && [ `getconf LONG_BIT` = '64' ] ; then
  120. return 0
  121. else
  122. return 1
  123. fi
  124. }
  125. download_file(){
  126. if [ -s ${1} ]; then
  127. echo "$1 [found]"
  128. else
  129. echo "$1 not found!!!download now..."
  130. if ! wget -c -t3 -T60 ${download_root_url}/${1}; then
  131. echo "Failed to download $1, please download it to ${cur_dir} directory manually and try again."
  132. exit 1
  133. fi
  134. fi
  135. }
  136. versionget(){
  137. if [[ -s /etc/redhat-release ]];then
  138. grep -oE "[0-9.]+" /etc/redhat-release
  139. else
  140. grep -oE "[0-9.]+" /etc/issue
  141. fi
  142. }
  143. centosversion(){
  144. if check_sys sysRelease centos;then
  145. local code=${1}
  146. local version="`versionget`"
  147. local main_ver=${version%%.*}
  148. if [ "${main_ver}" == "${code}" ];then
  149. return 0
  150. else
  151. return 1
  152. fi
  153. else
  154. return 1
  155. fi
  156. }
  157. debianversion(){
  158. if check_sys sysRelease debian;then
  159. local version=$( get_opsy )
  160. local code=${1}
  161. local main_ver=$( echo ${version} | sed 's/[^0-9]//g')
  162. if [ "${main_ver}" == "${code}" ];then
  163. return 0
  164. else
  165. return 1
  166. fi
  167. else
  168. return 1
  169. fi
  170. }
  171. version_check(){
  172. if check_sys packageManager yum; then
  173. if centosversion 5; then
  174. echo "Error: CentOS 5 is not supported, Please re-install OS and try again."
  175. exit 1
  176. fi
  177. fi
  178. }
  179. get_char(){
  180. SAVEDSTTY=`stty -g`
  181. stty -echo
  182. stty cbreak
  183. dd if=/dev/tty bs=1 count=1 2> /dev/null
  184. stty -raw
  185. stty echo
  186. stty $SAVEDSTTY
  187. }
  188. preinstall_l2tp(){
  189. echo
  190. if [ -d "/proc/vz" ]; then
  191. echo -e "\033[41;37m WARNING: \033[0m Your VPS is based on OpenVZ, and IPSec might not be supported by the kernel."
  192. echo "Continue installation? (y/n)"
  193. read -p "(Default: n)" agree
  194. [ -z ${agree} ] && agree="n"
  195. if [ "${agree}" == "n" ]; then
  196. echo
  197. echo "L2TP installation cancelled."
  198. echo
  199. exit 0
  200. fi
  201. fi
  202. echo
  203. echo "Please enter IP-Range:"
  204. read -p "(Default Range: 192.168.18):" iprange
  205. [ -z ${iprange} ] && iprange="192.168.18"
  206. echo "Please enter PSK:"
  207. read -p "(Default PSK: teddysun.com):" mypsk
  208. [ -z ${mypsk} ] && mypsk="teddysun.com"
  209. echo "Please enter Username:"
  210. read -p "(Default Username: teddysun):" username
  211. [ -z ${username} ] && username="teddysun"
  212. password=`rand`
  213. echo "Please enter ${username}'s password:"
  214. read -p "(Default Password: ${password}):" tmppassword
  215. [ ! -z ${tmppassword} ] && password=${tmppassword}
  216. echo
  217. echo "ServerIP:${IP}"
  218. echo "Server Local IP:${iprange}.1"
  219. echo "Client Remote IP Range:${iprange}.2-${iprange}.254"
  220. echo "PSK:${mypsk}"
  221. echo
  222. echo "Press any key to start... or press Ctrl + C to cancel."
  223. char=`get_char`
  224. }
  225. install_l2tp(){
  226. mknod /dev/random c 1 9
  227. if check_sys packageManager apt; then
  228. apt-get -y update
  229. if debianversion 7; then
  230. if is_64bit; then
  231. local libnspr4_filename1="libnspr4_4.10.7-1_amd64.deb"
  232. local libnspr4_filename2="libnspr4-0d_4.10.7-1_amd64.deb"
  233. local libnspr4_filename3="libnspr4-dev_4.10.7-1_amd64.deb"
  234. local libnspr4_filename4="libnspr4-dbg_4.10.7-1_amd64.deb"
  235. local libnss3_filename1="libnss3_3.17.2-1.1_amd64.deb"
  236. local libnss3_filename2="libnss3-1d_3.17.2-1.1_amd64.deb"
  237. local libnss3_filename3="libnss3-tools_3.17.2-1.1_amd64.deb"
  238. local libnss3_filename4="libnss3-dev_3.17.2-1.1_amd64.deb"
  239. local libnss3_filename5="libnss3-dbg_3.17.2-1.1_amd64.deb"
  240. else
  241. local libnspr4_filename1="libnspr4_4.10.7-1_i386.deb"
  242. local libnspr4_filename2="libnspr4-0d_4.10.7-1_i386.deb"
  243. local libnspr4_filename3="libnspr4-dev_4.10.7-1_i386.deb"
  244. local libnspr4_filename4="libnspr4-dbg_4.10.7-1_i386.deb"
  245. local libnss3_filename1="libnss3_3.17.2-1.1_i386.deb"
  246. local libnss3_filename2="libnss3-1d_3.17.2-1.1_i386.deb"
  247. local libnss3_filename3="libnss3-tools_3.17.2-1.1_i386.deb"
  248. local libnss3_filename4="libnss3-dev_3.17.2-1.1_i386.deb"
  249. local libnss3_filename5="libnss3-dbg_3.17.2-1.1_i386.deb"
  250. fi
  251. rm -rf ${cur_dir}/l2tp
  252. mkdir -p ${cur_dir}/l2tp
  253. cd ${cur_dir}/l2tp
  254. download_file "${libnspr4_filename1}"
  255. download_file "${libnspr4_filename2}"
  256. download_file "${libnspr4_filename3}"
  257. download_file "${libnspr4_filename4}"
  258. download_file "${libnss3_filename1}"
  259. download_file "${libnss3_filename2}"
  260. download_file "${libnss3_filename3}"
  261. download_file "${libnss3_filename4}"
  262. download_file "${libnss3_filename5}"
  263. dpkg -i ${libnspr4_filename1} ${libnspr4_filename2} ${libnspr4_filename3} ${libnspr4_filename4}
  264. dpkg -i ${libnss3_filename1} ${libnss3_filename2} ${libnss3_filename3} ${libnss3_filename4} ${libnss3_filename5}
  265. apt-get -y install wget gcc ppp flex bison make pkg-config libpam0g-dev libcap-ng-dev iptables \
  266. libcap-ng-utils libunbound-dev libevent-dev libcurl4-nss-dev libsystemd-daemon-dev
  267. else
  268. apt-get -y install wget gcc ppp flex bison make python libnss3-dev libnss3-tools libselinux-dev iptables \
  269. libnspr4-dev pkg-config libpam0g-dev libcap-ng-dev libcap-ng-utils libunbound-dev \
  270. libevent-dev libcurl4-nss-dev libsystemd-dev
  271. fi
  272. apt-get -y --no-install-recommends install xmlto
  273. apt-get -y install xl2tpd
  274. compile_install
  275. elif check_sys packageManager yum; then
  276. echo "Adding the EPEL repository..."
  277. yum -y install epel-release yum-utils
  278. [ ! -f /etc/yum.repos.d/epel.repo ] && echo "Install EPEL repository failed, please check it." && exit 1
  279. yum-config-manager --enable epel
  280. echo "Adding the EPEL repository complete..."
  281. if centosversion 7; then
  282. yum -y install ppp libreswan xl2tpd firewalld
  283. yum_install
  284. elif centosversion 6; then
  285. yum -y remove libevent-devel
  286. yum -y install libevent2-devel
  287. yum -y install nss-devel nspr-devel pkgconfig pam-devel \
  288. libcap-ng-devel libselinux-devel lsof \
  289. curl-devel flex bison gcc ppp make iptables gmp-devel \
  290. fipscheck-devel unbound-devel xmlto libpcap-devel xl2tpd
  291. compile_install
  292. fi
  293. fi
  294. }
  295. config_install(){
  296. cat > /etc/ipsec.conf<<EOF
  297. version 2.0
  298. config setup
  299. protostack=netkey
  300. nhelpers=0
  301. uniqueids=no
  302. interfaces=%defaultroute
  303. virtual_private=%v4:10.0.0.0/8,%v4:192.168.0.0/16,%v4:172.16.0.0/12,%v4:!${iprange}.0/24
  304. conn l2tp-psk
  305. rightsubnet=vhost:%priv
  306. also=l2tp-psk-nonat
  307. conn l2tp-psk-nonat
  308. authby=secret
  309. pfs=no
  310. auto=add
  311. keyingtries=3
  312. rekey=no
  313. ikelifetime=8h
  314. keylife=1h
  315. type=transport
  316. left=%defaultroute
  317. leftid=${IP}
  318. leftprotoport=17/1701
  319. right=%any
  320. rightprotoport=17/%any
  321. dpddelay=40
  322. dpdtimeout=130
  323. dpdaction=clear
  324. sha2-truncbug=yes
  325. EOF
  326. cat > /etc/ipsec.secrets<<EOF
  327. %any %any : PSK "${mypsk}"
  328. EOF
  329. cat > /etc/xl2tpd/xl2tpd.conf<<EOF
  330. [global]
  331. port = 1701
  332. [lns default]
  333. ip range = ${iprange}.2-${iprange}.254
  334. local ip = ${iprange}.1
  335. require chap = yes
  336. refuse pap = yes
  337. require authentication = yes
  338. name = l2tpd
  339. ppp debug = yes
  340. pppoptfile = /etc/ppp/options.xl2tpd
  341. length bit = yes
  342. EOF
  343. cat > /etc/ppp/options.xl2tpd<<EOF
  344. ipcp-accept-local
  345. ipcp-accept-remote
  346. require-mschap-v2
  347. ms-dns 8.8.8.8
  348. ms-dns 8.8.4.4
  349. noccp
  350. auth
  351. hide-password
  352. idle 1800
  353. mtu 1410
  354. mru 1410
  355. nodefaultroute
  356. debug
  357. proxyarp
  358. connect-delay 5000
  359. EOF
  360. rm -f /etc/ppp/chap-secrets
  361. cat > /etc/ppp/chap-secrets<<EOF
  362. # Secrets for authentication using CHAP
  363. # client server secret IP addresses
  364. ${username} l2tpd ${password} *
  365. EOF
  366. }
  367. compile_install(){
  368. rm -rf ${cur_dir}/l2tp
  369. mkdir -p ${cur_dir}/l2tp
  370. cd ${cur_dir}/l2tp
  371. download_file "${libreswan_filename}.tar.gz"
  372. tar -zxf ${libreswan_filename}.tar.gz
  373. cd ${cur_dir}/l2tp/${libreswan_filename}
  374. cat > Makefile.inc.local <<'EOF'
  375. WERROR_CFLAGS =
  376. USE_DNSSEC = false
  377. USE_DH31 = false
  378. USE_GLIBC_KERN_FLIP_HEADERS = true
  379. EOF
  380. make programs && make install
  381. /usr/local/sbin/ipsec --version >/dev/null 2>&1
  382. if [ $? -ne 0 ]; then
  383. echo "${libreswan_filename} install failed."
  384. exit 1
  385. fi
  386. config_install
  387. cp -pf /etc/sysctl.conf /etc/sysctl.conf.bak
  388. sed -i 's/net.ipv4.ip_forward = 0/net.ipv4.ip_forward = 1/g' /etc/sysctl.conf
  389. for each in `ls /proc/sys/net/ipv4/conf/`; do
  390. echo "net.ipv4.conf.${each}.accept_source_route=0" >> /etc/sysctl.conf
  391. echo "net.ipv4.conf.${each}.accept_redirects=0" >> /etc/sysctl.conf
  392. echo "net.ipv4.conf.${each}.send_redirects=0" >> /etc/sysctl.conf
  393. echo "net.ipv4.conf.${each}.rp_filter=0" >> /etc/sysctl.conf
  394. done
  395. sysctl -p
  396. if centosversion 6; then
  397. [ -f /etc/sysconfig/iptables ] && cp -pf /etc/sysconfig/iptables /etc/sysconfig/iptables.old.`date +%Y%m%d`
  398. if [ "`iptables -L -n | grep -c '\-\-'`" == "0" ]; then
  399. cat > /etc/sysconfig/iptables <<EOF
  400. # Added by L2TP VPN script
  401. *filter
  402. :INPUT ACCEPT [0:0]
  403. :FORWARD ACCEPT [0:0]
  404. :OUTPUT ACCEPT [0:0]
  405. -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
  406. -A INPUT -p icmp -j ACCEPT
  407. -A INPUT -i lo -j ACCEPT
  408. -A INPUT -p tcp --dport 22 -j ACCEPT
  409. -A INPUT -p udp -m multiport --dports 500,4500,1701 -j ACCEPT
  410. -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
  411. -A FORWARD -s ${iprange}.0/24 -j ACCEPT
  412. COMMIT
  413. *nat
  414. :PREROUTING ACCEPT [0:0]
  415. :OUTPUT ACCEPT [0:0]
  416. :POSTROUTING ACCEPT [0:0]
  417. -A POSTROUTING -s ${iprange}.0/24 -j SNAT --to-source ${IP}
  418. COMMIT
  419. EOF
  420. else
  421. iptables -I INPUT -p udp -m multiport --dports 500,4500,1701 -j ACCEPT
  422. iptables -I FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
  423. iptables -I FORWARD -s ${iprange}.0/24 -j ACCEPT
  424. iptables -t nat -A POSTROUTING -s ${iprange}.0/24 -j SNAT --to-source ${IP}
  425. /etc/init.d/iptables save
  426. fi
  427. if [ ! -f /etc/ipsec.d/cert9.db ]; then
  428. echo > /var/tmp/libreswan-nss-pwd
  429. certutil -N -f /var/tmp/libreswan-nss-pwd -d /etc/ipsec.d
  430. rm -f /var/tmp/libreswan-nss-pwd
  431. fi
  432. chkconfig --add iptables
  433. chkconfig iptables on
  434. chkconfig --add ipsec
  435. chkconfig ipsec on
  436. chkconfig --add xl2tpd
  437. chkconfig xl2tpd on
  438. /etc/init.d/iptables restart
  439. /etc/init.d/ipsec start
  440. /etc/init.d/xl2tpd start
  441. else
  442. [ -f /etc/iptables.rules ] && cp -pf /etc/iptables.rules /etc/iptables.rules.old.`date +%Y%m%d`
  443. if [ "`iptables -L -n | grep -c '\-\-'`" == "0" ]; then
  444. cat > /etc/iptables.rules <<EOF
  445. # Added by L2TP VPN script
  446. *filter
  447. :INPUT ACCEPT [0:0]
  448. :FORWARD ACCEPT [0:0]
  449. :OUTPUT ACCEPT [0:0]
  450. -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
  451. -A INPUT -p icmp -j ACCEPT
  452. -A INPUT -i lo -j ACCEPT
  453. -A INPUT -p tcp --dport 22 -j ACCEPT
  454. -A INPUT -p udp -m multiport --dports 500,4500,1701 -j ACCEPT
  455. -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
  456. -A FORWARD -s ${iprange}.0/24 -j ACCEPT
  457. COMMIT
  458. *nat
  459. :PREROUTING ACCEPT [0:0]
  460. :OUTPUT ACCEPT [0:0]
  461. :POSTROUTING ACCEPT [0:0]
  462. -A POSTROUTING -s ${iprange}.0/24 -j SNAT --to-source ${IP}
  463. COMMIT
  464. EOF
  465. else
  466. iptables -I INPUT -p udp -m multiport --dports 500,4500,1701 -j ACCEPT
  467. iptables -I FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
  468. iptables -I FORWARD -s ${iprange}.0/24 -j ACCEPT
  469. iptables -t nat -A POSTROUTING -s ${iprange}.0/24 -j SNAT --to-source ${IP}
  470. /sbin/iptables-save > /etc/iptables.rules
  471. fi
  472. cat > /etc/network/if-up.d/iptables <<EOF
  473. #!/bin/sh
  474. /sbin/iptables-restore < /etc/iptables.rules
  475. EOF
  476. chmod +x /etc/network/if-up.d/iptables
  477. if [ ! -f /etc/ipsec.d/cert9.db ]; then
  478. echo > /var/tmp/libreswan-nss-pwd
  479. certutil -N -f /var/tmp/libreswan-nss-pwd -d /etc/ipsec.d
  480. rm -f /var/tmp/libreswan-nss-pwd
  481. fi
  482. update-rc.d -f xl2tpd defaults
  483. cp -f /etc/rc.local /etc/rc.local.old.`date +%Y%m%d`
  484. sed --follow-symlinks -i -e '/^exit 0/d' /etc/rc.local
  485. cat >> /etc/rc.local <<EOF
  486. # Added by L2TP VPN script
  487. echo 1 > /proc/sys/net/ipv4/ip_forward
  488. /usr/sbin/service ipsec start
  489. exit 0
  490. EOF
  491. chmod +x /etc/rc.local
  492. echo 1 > /proc/sys/net/ipv4/ip_forward
  493. /sbin/iptables-restore < /etc/iptables.rules
  494. /usr/sbin/service ipsec start
  495. /usr/sbin/service xl2tpd restart
  496. fi
  497. }
  498. yum_install(){
  499. config_install
  500. cp -pf /etc/sysctl.conf /etc/sysctl.conf.bak
  501. echo "# Added by L2TP VPN" >> /etc/sysctl.conf
  502. echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
  503. echo "net.ipv4.tcp_syncookies=1" >> /etc/sysctl.conf
  504. echo "net.ipv4.icmp_echo_ignore_broadcasts=1" >> /etc/sysctl.conf
  505. echo "net.ipv4.icmp_ignore_bogus_error_responses=1" >> /etc/sysctl.conf
  506. for each in `ls /proc/sys/net/ipv4/conf/`; do
  507. echo "net.ipv4.conf.${each}.accept_source_route=0" >> /etc/sysctl.conf
  508. echo "net.ipv4.conf.${each}.accept_redirects=0" >> /etc/sysctl.conf
  509. echo "net.ipv4.conf.${each}.send_redirects=0" >> /etc/sysctl.conf
  510. echo "net.ipv4.conf.${each}.rp_filter=0" >> /etc/sysctl.conf
  511. done
  512. sysctl -p
  513. cat > /etc/firewalld/services/xl2tpd.xml<<EOF
  514. <?xml version="1.0" encoding="utf-8"?>
  515. <service>
  516. <short>xl2tpd</short>
  517. <description>L2TP IPSec</description>
  518. <port protocol="udp" port="4500"/>
  519. <port protocol="udp" port="1701"/>
  520. </service>
  521. EOF
  522. chmod 640 /etc/firewalld/services/xl2tpd.xml
  523. systemctl enable ipsec
  524. systemctl enable xl2tpd
  525. systemctl enable firewalld
  526. systemctl status firewalld > /dev/null 2>&1
  527. if [ $? -eq 0 ]; then
  528. firewall-cmd --reload
  529. echo "Checking firewalld status..."
  530. firewall-cmd --list-all
  531. echo "add firewalld rules..."
  532. firewall-cmd --permanent --add-service=ipsec
  533. firewall-cmd --permanent --add-service=xl2tpd
  534. firewall-cmd --permanent --add-masquerade
  535. firewall-cmd --reload
  536. else
  537. echo "Firewalld looks like not running, trying to start..."
  538. systemctl start firewalld
  539. if [ $? -eq 0 ]; then
  540. echo "Firewalld start successfully..."
  541. firewall-cmd --reload
  542. echo "Checking firewalld status..."
  543. firewall-cmd --list-all
  544. echo "adding firewalld rules..."
  545. firewall-cmd --permanent --add-service=ipsec
  546. firewall-cmd --permanent --add-service=xl2tpd
  547. firewall-cmd --permanent --add-masquerade
  548. firewall-cmd --reload
  549. else
  550. echo "Failed to start firewalld. please enable udp port 500 4500 1701 manually if necessary."
  551. fi
  552. fi
  553. systemctl restart ipsec
  554. systemctl restart xl2tpd
  555. echo "Checking ipsec status..."
  556. systemctl -a | grep ipsec
  557. echo "Checking xl2tpd status..."
  558. systemctl -a | grep xl2tpd
  559. echo "Checking firewalld status..."
  560. firewall-cmd --list-all
  561. }
  562. finally(){
  563. cd ${cur_dir}
  564. rm -fr ${cur_dir}/l2tp
  565. # create l2tp command
  566. cp -f ${cur_dir}/`basename $0` /usr/bin/l2tp
  567. echo "Please wait a moment..."
  568. sleep 5
  569. ipsec verify
  570. echo
  571. echo "###############################################################"
  572. echo "# L2TP VPN Auto Installer #"
  573. echo "# System Supported: CentOS 6+ / Debian 7+ / Ubuntu 12+ #"
  574. echo "# Intro: https://teddysun.com/448.html #"
  575. echo "# Author: Teddysun <[email protected]> #"
  576. echo "###############################################################"
  577. echo "If there is no [FAILED] above, you can connect to your L2TP "
  578. echo "VPN Server with the default Username/Password is below:"
  579. echo
  580. echo "Server IP: ${IP}"
  581. echo "PSK : ${mypsk}"
  582. echo "Username : ${username}"
  583. echo "Password : ${password}"
  584. echo
  585. echo "If you want to modify user settings, please use below command(s):"
  586. echo "l2tp -a (Add a user)"
  587. echo "l2tp -d (Delete a user)"
  588. echo "l2tp -l (List all users)"
  589. echo "l2tp -m (Modify a user password)"
  590. echo
  591. echo "Welcome to visit our website: https://teddysun.com/448.html"
  592. echo "Enjoy it!"
  593. echo
  594. }
  595. l2tp(){
  596. clear
  597. echo
  598. echo "###############################################################"
  599. echo "# L2TP VPN Auto Installer #"
  600. echo "# System Supported: CentOS 6+ / Debian 7+ / Ubuntu 12+ #"
  601. echo "# Intro: https://teddysun.com/448.html #"
  602. echo "# Author: Teddysun <[email protected]> #"
  603. echo "###############################################################"
  604. echo
  605. rootness
  606. tunavailable
  607. disable_selinux
  608. version_check
  609. get_os_info
  610. preinstall_l2tp
  611. install_l2tp
  612. finally
  613. }
  614. list_users(){
  615. if [ ! -f /etc/ppp/chap-secrets ];then
  616. echo "Error: /etc/ppp/chap-secrets file not found."
  617. exit 1
  618. fi
  619. local line="+-------------------------------------------+\n"
  620. local string=%20s
  621. printf "${line}|${string} |${string} |\n${line}" Username Password
  622. grep -v "^#" /etc/ppp/chap-secrets | awk '{printf "|'${string}' |'${string}' |\n", $1,$3}'
  623. printf ${line}
  624. }
  625. add_user(){
  626. while :
  627. do
  628. read -p "Please input your Username:" user
  629. if [ -z ${user} ]; then
  630. echo "Username can not be empty"
  631. else
  632. grep -w "${user}" /etc/ppp/chap-secrets > /dev/null 2>&1
  633. if [ $? -eq 0 ];then
  634. echo "Username (${user}) already exists. Please re-enter your username."
  635. else
  636. break
  637. fi
  638. fi
  639. done
  640. pass=`rand`
  641. echo "Please input ${user}'s password:"
  642. read -p "(Default Password: ${pass}):" tmppass
  643. [ ! -z ${tmppass} ] && pass=${tmppass}
  644. echo "${user} l2tpd ${pass} *" >> /etc/ppp/chap-secrets
  645. echo "Username (${user}) add completed."
  646. }
  647. del_user(){
  648. while :
  649. do
  650. read -p "Please input Username you want to delete it:" user
  651. if [ -z ${user} ]; then
  652. echo "Username can not be empty"
  653. else
  654. grep -w "${user}" /etc/ppp/chap-secrets >/dev/null 2>&1
  655. if [ $? -eq 0 ];then
  656. break
  657. else
  658. echo "Username (${user}) is not exists. Please re-enter your username."
  659. fi
  660. fi
  661. done
  662. sed -i "/^\<${user}\>/d" /etc/ppp/chap-secrets
  663. echo "Username (${user}) delete completed."
  664. }
  665. mod_user(){
  666. while :
  667. do
  668. read -p "Please input Username you want to change password:" user
  669. if [ -z ${user} ]; then
  670. echo "Username can not be empty"
  671. else
  672. grep -w "${user}" /etc/ppp/chap-secrets >/dev/null 2>&1
  673. if [ $? -eq 0 ];then
  674. break
  675. else
  676. echo "Username (${user}) is not exists. Please re-enter your username."
  677. fi
  678. fi
  679. done
  680. pass=`rand`
  681. echo "Please input ${user}'s new password:"
  682. read -p "(Default Password: ${pass}):" tmppass
  683. [ ! -z ${tmppass} ] && pass=${tmppass}
  684. sed -i "/^\<${user}\>/d" /etc/ppp/chap-secrets
  685. echo "${user} l2tpd ${pass} *" >> /etc/ppp/chap-secrets
  686. echo "Username ${user}'s password has been changed."
  687. }
  688. # Main process
  689. action=$1
  690. if [ -z ${action} ] && [ "`basename $0`" != "l2tp" ]; then
  691. action=install
  692. fi
  693. case ${action} in
  694. install)
  695. l2tp 2>&1 | tee ${cur_dir}/l2tp.log
  696. ;;
  697. -l|--list)
  698. list_users
  699. ;;
  700. -a|--add)
  701. add_user
  702. ;;
  703. -d|--del)
  704. del_user
  705. ;;
  706. -m|--mod)
  707. mod_user
  708. ;;
  709. -h|--help)
  710. echo "Usage: `basename $0` -l,--list List all users"
  711. echo " `basename $0` -a,--add Add a user"
  712. echo " `basename $0` -d,--del Delete a user"
  713. echo " `basename $0` -m,--mod Modify a user password"
  714. echo " `basename $0` -h,--help Print this help information"
  715. ;;
  716. *)
  717. echo "Usage: `basename $0` [-l,--list|-a,--add|-d,--del|-m,--mod|-h,--help]" && exit
  718. ;;
  719. esac