forked from wolasss/l2tp-ipsec-vpn-client
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstartup.sh
executable file
·70 lines (60 loc) · 1.97 KB
/
startup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/bin/bash
set -eo pipefail
# template out all the config files using env vars
sed -i "s/right=.*/right=$VPN_SERVER_IPV4/" /etc/ipsec.conf
echo ": PSK $VPN_PSK" >/etc/ipsec.secrets
sed -i "s/lns = .*/lns = $VPN_SERVER_IPV4/" /etc/xl2tpd/xl2tpd.conf
sed -i "/^name/cname $VPN_USERNAME" /etc/ppp/options.l2tpd.client
sed -i "/^password/cpassword $VPN_PASSWORD" /etc/ppp/options.l2tpd.client
# startup ipsec tunnel
if [ -n "$VPN_PSK" ]; then
ipsec initnss
sleep 1
ipsec pluto --stderrlog --config /etc/ipsec.conf
sleep 5
ipsec auto --up L2TP-PSK
if ! (ipsec status | grep 'ISAKMP SA established' && ipsec status | grep 'IPsec SA established'); then
echo "IPSEC connection couldn't be established. "
exit 1
fi
fi
# if USERPEERDNS not setted, disable usepeerdns
if [ -z "$USEPEERDNS" ]; then
sed -i "/usepeerdns/d" /etc/ppp/options.l2tpd.client
fi
# if COUSTOM_ROUTE setted, disable default route in ppp options
# COUSTOM_ROUTE='192.168.42.0/24,192.168.43.0/24'
if [ -n "$CUSTOM_ROUTE" ]; then
sed -i "/defaultroute/d" /etc/ppp/options.l2tpd.client
(
# background task for checking custom route
while true; do
sleep 3
# determine ppp device
ppp_dev=$(ls /var/run | grep ppp[0-9]\.pid | awk -F . '{print $1}')
# if dev ppp ready add route
if [[ -n $ppp_dev ]]; then
CIDRs=${CUSTOM_ROUTE//,/ }
gateway=$(ip a | grep "$ppp_dev"$ | awk '{print $4}' | awk -F / '{print $1}')
# check if route exits otherwise add route
for CIDR in ${CIDRs[@]}; do
if ! (ip route | grep "$CIDR" >/dev/null); then
ip route add "$CIDR" via "$gateway"
fi
done
break
fi
done
) &
fi
# if DEBUG not setted, disable DEBUG
if [ -z "$DEBUG" ]; then
sed -i "/debug/d" /etc/xl2tpd/xl2tpd.conf
sed -i "/debug/d" /etc/ppp/options.l2tpd.client
fi
if [ "${HEALTH_CHECK:-false}" == "true" ]; then
# health-check
/health-check.sh &
fi
# startup xl2tpd ppp daemon
exec /usr/sbin/xl2tpd -p /var/run/xl2tpd.pid -c /etc/xl2tpd/xl2tpd.conf -C /var/run/xl2tpd/l2tp-control -D