-
Notifications
You must be signed in to change notification settings - Fork 4
/
vpnup.sh
executable file
·182 lines (163 loc) · 5.46 KB
/
vpnup.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/bin/sh
# TT-AUTOVPN
# E-mail:[email protected]
# http://bbs.enjoydiy.com
set -x
export PATH="/bin:/sbin:/usr/sbin:/usr/bin"
LOG='/tmp/autoddvpn.log'
LOCK='/tmp/autoddvpn.lock'
PID=$$
EXROUTEDIR='/jffs/exroute.d'
INFO="[INFO#${PID}]"
DEBUG="[DEBUG#${PID}]"
ERROR="[ERROR#${PID}]"
CHINART='/jffs/up.sh'
echo "$INFO $(date "+%d/%b/%Y:%H:%M:%S") vpnup.sh started" >> $LOG
for i in 1 2 3 4 5 6
do
if [ -f $LOCK ]; then
echo "$INFO $(date "+%d/%b/%Y:%H:%M:%S") got $LOCK , sleep 10 secs. #$i/6" >> $LOG
sleep 10
else
break
fi
done
if [ -f $LOCK ]; then
echo "$ERROR $(date "+%d/%b/%Y:%H:%M:%S") still got $LOCK , I'm aborted. Fix me." >> $LOG
exit 0
fi
#else
# echo "$INFO $(date "+%d/%b/%Y:%H:%M:%S") $LOCK was released, let's continue." >> $LOG
#fi
# create the lock
echo "$INFO $(date "+%d/%b/%Y:%H:%M:%S") vpnup" >> $LOCK
OLDGW=$(nvram get wan_gateway)
case $1 in
"pptp")
case "$(nvram get router_name)" in
"tomato")
echo "$INFO $(date "+%d/%b/%Y:%H:%M:%S") router type: tomato" >> $LOG
VPNSRV=$(nvram get pptpd_client_srvip)
VPNSRVSUB=$(nvram get pptpd_client_srvsub)
PPTPDEV=$(nvram get pptp_client_iface)
VPNGW=$(nvram get pptp_client_gateway)
VPNUPCUSTOM='/jffs/pptp/vpnup_custom'
;;
*)
# assume it to be a DD-WRT
echo "$INFO $(date "+%d/%b/%Y:%H:%M:%S") router type: DD-WRT" >> $LOG
VPNSRV=$(nvram get pptpd_client_srvip)
VPNSRVSUB=$(nvram get pptpd_client_srvsub)
#PPTPDEV=$(route -n | grep ^$VPNSRVSUB | awk '{print $NF}')
PPTPDEV=$(route -n | grep ^${VPNSRVSUB%.[0-9]*} | awk '{print $NF}' | head -n 1)
VPNGW=$(ifconfig $PPTPDEV | grep -Eo "P-t-P:([0-9.]+)" | cut -d: -f2)
VPNUPCUSTOM='/jffs/pptp/vpnup_custom'
;;
esac
;;
"openvpn")
VPNSRV=$(nvram get openvpncl_remoteip)
#OPENVPNSRVSUB=$(nvram get OPENVPNd_client_srvsub)
#OPENVPNDEV=$(route | grep ^$OPENVPNSRVSUB | awk '{print $NF}')
OPENVPNDEV='tun0'
VPNGW=$(ifconfig $OPENVPNDEV | grep -Eo "P-t-P:([0-9.]+)" | cut -d: -f2)
VPNUPCUSTOM='/jffs/openvpn/vpnup_custom'
;;
*)
echo "$INFO $(date "+%d/%b/%Y:%H:%M:%S") unknown vpnup.sh parameter,quit." >> $LOCK
exit 1
esac
if [ $OLDGW == '' ]; then
echo "$ERROR OLDGW is empty, is the WAN disconnected?" >> $LOG
exit 0
else
echo "$INFO OLDGW is $OLDGW"
fi
echo "$INFO $(date "+%d/%b/%Y:%H:%M:%S") make $VPNSRV gw $OLDGW" >> $LOG
route add -host $VPNSRV gw $OLDGW
echo "$INFO $(date "+%d/%b/%Y:%H:%M:%S") delete default gw $OLDGW" >> $LOG
route del default gw $OLDGW
echo "$INFO $(date "+%d/%b/%Y:%H:%M:%S") add default gw $VPNGW" >> $LOG
route add default gw $VPNGW
echo "$INFO $(date "+%d/%b/%Y:%H:%M:%S") adding the static routes, this may take a while." >> $LOG
##### begin batch route #####
export VPNGW=$VPNGW
export OLDGW=$OLDGW
grep ^route $CHINART | /bin/sh -x
##### end batch route #####
if [ -f $VPNUPCUSTOM ]; then
grep ^route $VPNUPCUSTOM | /bin/sh -x
fi
# prepare for the exceptional routes, see http://code.google.com/p/autoddvpn/issues/detail?id=7
echo "$INFO $(date "+%d/%b/%Y:%H:%M:%S") preparing the exceptional routes" >> $LOG
if [ $(nvram get exroute_enable) -eq 1 ]; then
echo "$INFO $(date "+%d/%b/%Y:%H:%M:%S") modifying the exceptional routes" >> $LOG
if [ ! -d $EXROUTEDIR ]; then
EXROUTEDIR='/tmp/exroute.d'
mkdir $EXROUTEDIR
fi
for i in $(nvram get exroute_list)
do
echo "$INFO $(date "+%d/%b/%Y:%H:%M:%S") fetching exceptional routes for $i" >> $LOG
if [ -d $EXROUTEDIR -a ! -f $EXROUTEDIR/$i ]; then
echo "$INFO $(date "+%d/%b/%Y:%H:%M:%S") missing $EXROUTEDIR/$i, wget it now." >> $LOG
wget http://autoddvpn.googlecode.com/svn/trunk/exroute.d/$i -O $EXROUTEDIR/$i
fi
if [ ! -f $EXROUTEDIR/$i ]; then
echo "$INFO $(date "+%d/%b/%Y:%H:%M:%S") $EXROUTEDIR/$i not found, skip." >> $LOG
continue
fi
for r in $(grep -v ^# $EXROUTEDIR/$i)
do
echo "$INFO $(date "+%d/%b/%Y:%H:%M:%S") adding $r via wan_gateway" >> $LOG
# check the item is a subnet or a single ip address
echo $r | grep "/" > /dev/null
if [ $? -eq 0 ]; then
route add -net $r gw $(nvram get wan_gateway)
else
route add $r gw $(nvram get wan_gateway)
fi
done
done
#route | grep ^default | awk '{print $2}' >> $LOG
# for custom list of exceptional routes
echo "$INFO $(date "+%d/%b/%Y:%H:%M:%S") modifying custom exceptional routes if available" >> $LOG
for i in $(nvram get exroute_custom)
do
echo "$INFO $(date "+%d/%b/%Y:%H:%M:%S") adding custom host/subnet $i via wan_gateway" >> $LOG
# check the item is a subnet or a single ip address
echo $i | grep "/" > /dev/null
if [ $? -eq 0 ]; then
route add -net $i gw $(nvram get wan_gateway)
else
route add $i gw $(nvram get wan_gateway)
fi
done
else
echo "$INFO $(date "+%d/%b/%Y:%H:%M:%S") exceptional routes disabled." >> $LOG
echo "$INFO $(date "+%d/%b/%Y:%H:%M:%S") exceptional routes features detail: http://goo.gl/fYfJ" >> $LOG
fi
# final check again
echo "$INFO final check the default gw"
while true
do
GW=$(route -n | grep ^0.0.0.0 | awk '{print $2}')
echo "$DEBUG my current gw is $GW"
#route | grep ^default | awk '{print $2}'
if [ "$GW" == "$OLDGW" ]; then
echo "$DEBUG still got the OLDGW, why?"
echo "$INFO delete default gw $OLDGW"
route del default gw $OLDGW
echo "$INFO add default gw $VPNGW again"
route add default gw $VPNGW
sleep 3
else
break
fi
done
echo "$INFO static routes added"
echo "$INFO $(date "+%d/%b/%Y:%H:%M:%S") vpnup.sh ended" >> $LOG
echo "$INFO $(date "+%d/%b/%Y:%H:%M:%S") restarting DNS" >> $LOG
restart_dns
# release the lock
rm -f $LOCK