-
Notifications
You must be signed in to change notification settings - Fork 0
/
vpn_setup_on_aliyun.sh
executable file
·130 lines (112 loc) · 2.92 KB
/
vpn_setup_on_aliyun.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
#!/bin/bash
usage()
{
echo "usage: `basename $0` VPN_CONFIG_NAME ACTION"
}
if [ $# -ne 2 ]
then
usage
exit 1
fi
VPN_CONFIG_NAME=$1
ACTION=$2
pppd_stop(){
echo "[pppd]:stop"
sudo killall pppd &>/dev/null
sudo killall pppd &>/dev/null
}
pppd_start(){
echo "[pppd]:start"
pppd_stop
sudo pppd call $1
}
pppd_check(){
is_running=`ifconfig | grep ppp0`
if [ "$is_running" == "" ];then
echo "[pppd]: not running..."
return 1
else
echo "[pppd]: already running..."
return 0
fi
}
pppd_check_and_start(){
pppd_check
ret=$?
if [ "$ret" == 1 ];then
pppd_start $VPN_CONFIG_NAME
fi
}
WHO_COUNT=`who | awk '{print $6}' | sed 's/(//' | sed 's/)//' | uniq | wc -l`
WHO_IP=`who | awk '{print $6}' | sed 's/(//' | sed 's/)//' | uniq`
route_keep_who(){
ALIYUN_GATEWAY_IP=`route | grep default | awk '{print $2}'`
# TODO
echo "[route_keep_who] who_ip: $WHO_IP"
echo "[route_keep_who] aliyun_gateway_ip: $ALIYUN_GATEWAY_IP"
sudo route add -host $WHO_IP gw $ALIYUN_GATEWAY_IP
}
route_del_default(){
ALIYUN_GATEWAY_IP=`route | grep default | awk '{print $2}'`
echo "[route_del_default] aliyun_gateway_ip: $ALIYUN_GATEWAY_IP"
sudo route del -net 0.0.0.0 gw $ALIYUN_GATEWAY_IP
}
route_to_vpn_server(){
VPN_SERVER_REMOTE_IP=`ifconfig ppp0 2>/dev/null | grep 'inet addr:' | awk '{print $3}' | cut -d: -f2`
echo "[route_to_vpn_server] vpn_server_remote_ip: $VPN_SERVER_REMOTE_IP"
sudo route add default gw $VPN_SERVER_REMOTE_IP
}
route_restore_default(){
echo "who_ip: $WHO_IP"
ALIYUN_GATEWAY_IP=`route | grep $WHO_IP | awk '{print $2}'`
echo "[route_restore_default] aliyun_gateway_ip: $ALIYUN_GATEWAY_IP"
# TODO
VPN_REMOTE_IP=`ifconfig ppp0 2>/dev/null | grep 'inet addr:' | awk '{print $3}' | cut -d: -f2`
# TODO
echo "[route_restore_default] vpn_remote_ip: $VPN_REMOTE_IP"
sudo route del default gw $VPN_REMOTE_IP
sudo route add default gw $ALIYUN_GATEWAY_IP
}
# TODO: support multi user choosen
who_check_and_keep(){
if [ $WHO_COUNT != 1 ];then
echo "[ERROR]: you are not the only user who logged in"
exit 1
fi
route_keep_who
}
case $VPN_CONFIG_NAME in
hk1)
echo "[Config]: hk1" ;;
hk2)
echo "[Config]: hk2" ;;
hk3)
echo "[Config]: hk3" ;;
*)
echo "[ERROR]: invalid vpn config name" ;
exit 1 ;;
esac
case $ACTION in
start)
echo "[action]: start" ;
who_check_and_keep ;
pppd_check_and_start ;
sleep 10 ;
route_del_default ;
sleep 1 ;
route_to_vpn_server ;
sleep 1 ;
curl ip.gs ;
echo "Done." ;;
stop)
echo "[action]: stop" ;
route_restore_default ;
sleep 1 ;
pppd_stop ;
sleep 1 ;
curl ip.gs ;
echo "Done." ;;
*)
echo "[ERROR]: invalid action" ;
exit 1 ;;
esac