-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvpn.sh
executable file
·164 lines (130 loc) · 4.07 KB
/
vpn.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
:
#
# usage: $0 country
#
if [ -z "$1" ] ; then
echo Usage: $0 country-you-want-to-be-teleported-to [DEBUG]
echo
echo " or, to list what's countries are currently out there -"
echo
echo Usage: $0 list
echo
exit 1
fi
country="$1"
#
# to see OpenVPN output
#
DEBUG=""
if [ ! -z "$2" -a "$2" == "DEBUG" ] ; then
DEBUG="YES"
fi
# need openvpn somewhere....
which openvpn &> /dev/null
if [ $? != 0 ]; then
echo \"openvpn\" must be installed and in your path
exit 2
fi
#
# shangri-la... but http? Really? :)
#
URL="http://www.vpngate.net/api/iphone/"
#
# we'll be stuffing the conf in a temp file
#
tmp_conf=$(mktemp)
up_script=$(mktemp)
vpn_output=$(mktemp)
# nuke tmps when done
# trap "rm -f $tmp_conf $up_script $vpn_output" EXIT
# trap "rm -f $tmp_conf $up_script $vpn_output" EXIT
# this is where the list of openvpn servers lives
VPN_SERVERS="$HOME/.vpnlist"
EXPIRE="3000" # server cache list will expire after 1 hour
CURRENT_TIME=$(date +%s)
STALE_WHEN=$(echo $(expr $CURRENT_TIME - $EXPIRE))
# assume hard -n- crusty
STALE="YES"
# if it's zero length, nuke it
if [ ! -s "$VPN_SERVERS" ] ; then
rm -f "$VPN_SERVERS"
fi
# check cache file... does it exist, and, if it does, how fresh?
if [ -f "$VPN_SERVERS" ] ; then
echo cache found, checking age...
AGE=$(/usr/bin/stat -f %m "$VPN_SERVERS")
if [ $STALE_WHEN -gt $AGE ] ; then
STALE="YES"
echo cache is old, old, old....
else
STALE="NO"
echo cache is still minty fresh
fi
fi
if [ "$STALE" = "YES" ]; then
echo 'getting fresh server list'
curl -s "$URL" > "$VPN_SERVERS"
fi
if [ $? != 0 ]; then
echo Failed getting VPN server list from $URL, bailin\'...
exit 3
fi
#
# special case
#
if [ "$country" == "list" ]; then
awk -F, 'NR > 2 {print $7, $6}' "$VPN_SERVERS" | sort -u
exit 0
fi
echo "looking for country $country in server list"
# field 7 is country, last field is config
#HostName,IP,Score,Ping,Speed,CountryLong,CountryShort,NumVpnSessions,Uptime,TotalUsers,TotalTraffic,LogType,Operator,Message,OpenVPN_ConfigData_Base64
# awk -F, '"'"$country"'" == $7 {print $NF; exit 0}' "$VPN_SERVERS" | /usr/bin/base64 -D > "$tmp_conf"
# choose a somewhat random one of the choices, if any
awk -F, 'BEGIN { n=0 } "'"$country"'" == $7 {vpns[n] = $NF; n++} END { srand(); if (vpns[0]) { print vpns[int(n*rand())] ; exit 0; } }' "$VPN_SERVERS" | /usr/bin/base64 -D > "$tmp_conf"
if [ ! -s "$tmp_conf" ]; then
echo "Couldn't find country $country"
exit 4
fi
# a script that will run after connecting to try to figure out what IP & country you're now in
echo "#!/bin/bash
# no longer works....
ip=\$(curl -m 10 -s https://ifconfig.co)
ip=\$(curl -m 10 -s ifconfig.me)
if [ -z "\$ip" ] ; then
echo
echo
echo Cannot determine my IP Address... curl/whois may be blocked, the VPN may not have worked, or....?
echo
echo Try going to 'https://www.google.com/search?client=firefox-b-1-d&q=what+is+my+ip' and see what happens?
echo
echo
exit 0
fi
echo
echo
echo
echo my current ip: \$ip
echo
echo trying to get my current location...
echo
echo Grats,you are now in... \$(echo \$(dig +short \$(echo \$ip | awk -F\\. '{printf(\"%s.%s.%s.%s\", \$4,\$3,\$2,\$1)}').origin.asn.cymru.com TXT) | awk -F\\| '{print \$3}')
echo
echo
echo
" > "$up_script"
# Country ... $(dig +short $(echo $ip | awk -F. '{print "$4.$3.$2.$1"}').origin.asn.cymru.com TXT) | awk -F| '{print $3}'
chmod 700 "$up_script"
echo Starting up VPN, you should be magically transported to $country if this all works....
sudo chown root "$up_script"
echo
#
# openvpn doesn't want to play ball... so hack time to run a script when we're really connected
#
sudo openvpn --script-security 2 --config "$tmp_conf" 2>&1 |
awk '{ if ("'"$DEBUG"'" != "") print "OpenVPN:", $0}
/Initialization Sequence Completed/ { system ("sudo bash '"$up_script"' &"); exit(0)} \
END { exit(1) }'
if [ ${PIPESTATUS[1]} -ne 0 ]; then
echo -e "\nVPN stopped...! (To see VPN output, add 'DEBUG' to the command line invocation, e.g." $0 $* " DEBUG)"
fi