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

83 lines
2.1 KiB
Bash
Raw Permalink Normal View History

2014-09-20 18:46:30 +08:00
#!/bin/sh
# vpn.sh -- xl2tpd connect / disconnect
#
# Requirements: xl2tpd
#
# 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-09-20 18:46:30 +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-09-28 21:39:15 +08:00
BASEDIR=$(dirname $0)
2014-09-20 18:46:30 +08:00
2014-10-03 21:12:37 +08:00
flush() {
# see also: https://github.com/QSCTech/zjunet/issues/39
2014-10-13 21:18:58 +08:00
"${BASEDIR}/sudo.sh" ip route flush 10.5.1.5
"${BASEDIR}/sudo.sh" ip route flush 10.5.1.7
"${BASEDIR}/sudo.sh" ip route flush 10.5.1.9
"${BASEDIR}/sudo.sh" ip route flush 10.5.6.2
2014-10-03 21:12:37 +08:00
}
2014-09-21 23:59:24 +08:00
disconnect() {
2014-09-21 20:28:40 +08:00
users=$("${BASEDIR}/user.sh" getall)
for username in $users; do
2014-10-13 21:18:58 +08:00
"${BASEDIR}/sudo.sh" "${BASEDIR}/xl2tpd.sh" disconnect $username
2014-09-21 20:28:40 +08:00
done
2014-10-13 21:18:58 +08:00
"${BASEDIR}/sudo.sh" "${BASEDIR}/route.sh"
2014-09-20 18:46:30 +08:00
}
2014-09-21 23:59:24 +08:00
connect() {
2014-09-21 23:03:24 +08:00
users=$("${BASEDIR}/user.sh" getall)
2014-10-15 16:24:20 +08:00
"${BASEDIR}/sudo.sh" "${BASEDIR}/xl2tpd.sh" restart
2014-09-21 23:03:24 +08:00
for username in $users; do
2014-09-21 23:59:24 +08:00
password=$("${BASEDIR}/user.sh" getpwd $username)
echo "[INFO] Login using ${username}"
2014-10-15 16:24:20 +08:00
"${BASEDIR}/sudo.sh" "${BASEDIR}/xl2tpd.sh" waituser $username
2014-10-03 21:12:37 +08:00
flush
2014-09-21 23:03:24 +08:00
done
2014-10-13 21:18:58 +08:00
"${BASEDIR}/sudo.sh" "${BASEDIR}/route.sh"
2014-09-21 23:03:24 +08:00
}
2014-09-21 15:33:26 +08:00
#####################################
#
# Dispatch
#
#####################################
2014-10-13 21:37:22 +08:00
# start xl2tpd if not
"${BASEDIR}/sudo.sh" "${BASEDIR}/xl2tpd.sh" trystart
2014-09-20 18:46:30 +08:00
case "$1" in
d|-d|disconnect)
2014-09-20 18:46:30 +08:00
disconnect
;;
""|c|-c|connect)
2014-09-20 18:46:30 +08:00
disconnect
sleep 3
connect
2014-09-20 18:46:30 +08:00
;;
*)
echo Invalid subcommand \"$1\" for \`zjunet vpn\`. Run \`zjunet usage\` for help.
2014-09-20 18:46:30 +08:00
;;
esac