1
0
mirror of https://github.com/QSCTech/zjunet.git synced 2026-01-22 19:54:48 +08:00
zjunet/lib/xl2tpd.sh

192 lines
4.3 KiB
Bash
Raw Normal View History

2014-09-20 21:54:42 +08:00
#!/bin/sh
# xl2tpd.sh -- xl2tpd connect / disconnect
#
# Copyright (C) 2014 Zeno Zeng <zenoofzeng@gmail.com>
2014-09-21 15:33:26 +08:00
# Copyright (C) 2014 Zhang Hai <Dreaming.in.Code.ZH@Gmail.com>
2014-11-12 18:35:21 +08:00
# Copyright (C) 2014 Xero Essential <x@xeroe.net || xqyww123@gmail.com>
2014-09-20 21:54:42 +08:00
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see
# <http://www.gnu.org/licenses/>.
2014-10-13 21:18:58 +08:00
#
# RUN AS ROOT! >..< ~~ / by xero ~~~ ararararar ~~~~
2014-09-20 21:54:42 +08:00
2014-09-21 19:58:19 +08:00
LNS="10.5.1.9"
L2TPD_CFG_FILE=/etc/xl2tpd/xl2tpd.conf
USERNAME=$2
PASSWORD=$3
LAC_NAME=zju-l2tp-${USERNAME}
2014-10-15 16:24:20 +08:00
PPP_LOG_FILE=/tmp/zju-l2tp-log-${USERNAME}
2014-09-21 19:58:19 +08:00
PPP_OPT_FILE=/etc/ppp/peers/${LAC_NAME}
2014-09-21 17:20:38 +08:00
2014-09-21 23:50:35 +08:00
mkdir -p /var/log/zjunet/
LOG_FILE=/var/log/zjunet/${USERNAME}
XL2TPD_CONTROL_FILE=/var/run/xl2tpd/l2tp-control
2014-11-22 16:54:34 +08:00
type systemctl >/dev/null 2>&1
HAVE_SYSTEMD=$?
2014-11-22 16:54:34 +08:00
xl2tpd_stop() {
echo "[INFO] Stopping xl2tpd"
if [ $HAVE_SYSTEMD -eq 0 ]; then
2014-11-12 18:35:21 +08:00
systemctl stop xl2tpd
2014-11-22 16:54:34 +08:00
else
/etc/init.d/xl2tpd stop
fi
}
2014-09-20 21:54:42 +08:00
xl2tpd_start() {
2014-11-22 16:54:34 +08:00
echo "[INFO] Starting xl2tpd"
if [ $HAVE_SYSTEMD -eq 0 ]; then
2014-11-12 18:35:21 +08:00
systemctl start xl2tpd
2014-11-22 16:54:34 +08:00
else
/etc/init.d/xl2tpd start
fi
2014-10-13 21:26:01 +08:00
2014-09-20 21:54:42 +08:00
# wait until ready
for i in $(seq 0 10); do
2014-11-12 18:35:21 +08:00
if [ -e ${XL2TPD_CONTROL_FILE} ] || (type systemctl >/dev/null && systemctl status xl2tpd >/dev/null) ; then
echo "[INFO] xl2tpd ready."
2014-09-20 21:54:42 +08:00
return 0
fi
sleep 1
done
echo "Fail to start xl2tpd"
exit 1
2014-09-20 21:54:42 +08:00
}
2014-10-13 21:37:22 +08:00
xl2tpd_trystart() {
2014-11-22 16:54:34 +08:00
echo "[INFO] Try to start xl2tpd if not"
2014-11-12 18:35:21 +08:00
if [ -e ${XL2TPD_CONTROL_FILE} ] || (type systemctl >/dev/null && systemctl status xl2tpd >/dev/null); then
2014-10-13 21:37:22 +08:00
echo "[INFO] xl2tpd ready."
else
xl2tpd_start
fi
}
xl2tpd_restart() {
xl2tpd_stop
rm -f ${XL2TPD_CONTROL_FILE}
xl2tpd_start
}
2014-09-21 15:33:26 +08:00
xl2tpd_create_lac() {
2014-09-21 17:20:38 +08:00
2014-09-21 15:33:26 +08:00
cat > $PPP_OPT_FILE <<EOF
noauth
linkname $LAC_NAME
logfile $PPP_LOG_FILE
2014-09-21 19:58:19 +08:00
name $USERNAME
password $PASSWORD
2014-09-21 15:33:26 +08:00
EOF
chmod 600 $PPP_OPT_FILE
2014-09-21 17:20:38 +08:00
touch $L2TPD_CFG_FILE
if ! grep -q "\[lac ${LAC_NAME}\]" $L2TPD_CFG_FILE; then
cat >> $L2TPD_CFG_FILE <<EOF
2014-09-21 15:33:26 +08:00
[lac ${LAC_NAME}]
lns = $LNS
redial = no
redial timeout = 5
require chap = yes
require authentication = no
ppp debug = no
pppoptfile = ${PPP_OPT_FILE}
require pap = no
autodial = yes
2014-10-13 21:26:01 +08:00
2014-09-21 15:33:26 +08:00
EOF
fi
2014-09-20 21:54:42 +08:00
}
xl2tpd_connect() {
2014-11-22 16:54:34 +08:00
echo "[INFO] try connecting $1"
pkill xl2tpd-control > /dev/null
2014-11-22 17:21:02 +08:00
xl2tpd-control connect $1 &
2014-11-22 16:54:34 +08:00
echo "[INFO] xl2tpd-control done"
}
xl2tpd_disconnect() {
2014-11-22 16:54:34 +08:00
echo "[INFO] try disconnecting $1"
pkill xl2tpd-control > /dev/null
2014-11-22 17:21:02 +08:00
xl2tpd-control disconnect $1 &
2014-11-22 16:54:34 +08:00
echo "[INFO] xl2tpd-control done"
}
2014-10-15 16:24:20 +08:00
xl2tpd_waituser() {
2014-10-01 04:10:17 +08:00
for i in $(seq 0 10000); do
2014-09-21 17:20:38 +08:00
2014-09-21 23:50:35 +08:00
tail $PPP_LOG_FILE >> $LOG_FILE
if [ $(tail $PPP_LOG_FILE | grep 'Connection terminated' | wc -l) -ne 0 ]
then
echo "[INFO] Connection terminated."
echo -n > $PPP_LOG_FILE
2014-11-22 17:21:02 +08:00
echo "[INFO] Retrying now. "
sleep 1
xl2tpd_disconnect ${LAC_NAME}
sleep 5
xl2tpd_connect ${LAC_NAME}
2014-11-22 17:37:43 +08:00
echo "[INFO] again"
fi
2014-09-21 17:23:49 +08:00
echo -n > $PPP_LOG_FILE
2014-09-21 15:33:26 +08:00
2014-10-15 16:24:20 +08:00
pid="/var/run/ppp-${LAC_NAME}.pid"
if [ -e $pid ]; then
ppp=$(cat $pid | grep ppp)
if ip addr show | grep "inet.*${ppp}" > /dev/null; then
ip addr show | grep "inet.*${ppp}" | sed 's/^ */[VPN] /'
return
fi
2014-09-21 15:33:26 +08:00
fi
2014-09-21 17:20:38 +08:00
2014-09-21 15:33:26 +08:00
done
2014-09-21 17:28:07 +08:00
echo "Fail to bring up ppp, timeout."
2014-09-21 15:33:26 +08:00
xl2tpd_disconnect ${LAC_NAME}
2014-09-21 15:33:26 +08:00
}
2014-09-20 21:54:42 +08:00
2014-09-26 13:55:11 +08:00
disconnect() {
xl2tpd_disconnect ${LAC_NAME}
2014-09-26 13:55:11 +08:00
tail $PPP_LOG_FILE
echo -n > $PPP_LOG_FILE
}
2014-09-20 21:54:42 +08:00
case $1 in
restart)
xl2tpd_restart
;;
2014-10-13 21:37:22 +08:00
trystart)
# will start unless already started
xl2tpd_trystart
;;
adduser)
2014-09-21 19:58:19 +08:00
xl2tpd_create_lac
;;
2014-10-15 16:24:20 +08:00
waituser)
xl2tpd_waituser
2014-09-20 21:54:42 +08:00
;;
disconnect)
disconnect
;;
2014-09-20 21:54:42 +08:00
esac