blob: 9e52d883193a88c07649f2d4b8882453a80cf0de (
plain)
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
#!/bin/bash
bold=$(tput bold)
normal=$(tput sgr0)
if [ "$EUID" -ne 0 ]
then
printf "This script must be run as root.\n"
exit 1
fi
if [ ! -d "$DIRECTORY" ]; then mkdir -p /etc/purevpn; fi
if [ ! -f /etc/purevpn/vpn-list.tsv ]; then ln -s /usr/lib/purevpn/vpn-list.tsv /etc/purevpn/; fi
usage() {
cat <<EOF
usage: ${0##*/} [OPTION]
Options:
-h Print this help message and exit
-l List the available cities files and exit
-c [city] Choose a city for which to install the OpenVPN config
This option can be used multiple times. e.g. -c Lima -c New\ York
(NOTE: just use the city name, the part that is ${bold}bold or
coloured${normal} (if supported) when you run "purevpn -l")
-t [protocol] Choose the protocol to install (either "pptp" or "ovpn")
This option can be used multiple times. e.g. -t pptp -t ovpn
-u [username] Set the username in all PureVPN files in /etc/NetworkManager
-p [password] Set the password in all PureVPN files in /etc/NetworkManager
-x Purge all the PureVPN files in /etc/NetworkManager and exit
-z Attempt to update the VPN server list from
https://support.purevpn.com/vpn-servers and exit
If 'OPTION' is unspecified, ${0##*/} will not make any changes.
EOF
}
updlist() {
printf "==> Updating the list of VPN servers from https://support.purevpn.com/vpn-servers\n"
rm -v /etc/purevpn/vpn-list.tsv
## Lots of credit to: https://stackoverflow.com/a/10189130/6047155
curl "https://support.purevpn.com/vpn-servers" 2>/dev/null | \
grep -i -e '</\?TABLE\|</\?TD\|</\?TR\|</\?TH' | \
sed 's/^[\ \t]*//g' | tr -d '\n' | \
sed 's/<\/TR[^>]*>/\n/Ig;s/<\/\?\(TABLE\|TR\)[^>]*>//Ig' | \
sed 's/^<T[DH][^>]*>\|<\/\?T[DH][^>]*>$//Ig;s/<\/T[DH][^>]*><T[DH][^>]*>/\t/Ig' | \
sed 's/<[^<>]*>//g;s/\s*\t\s*/\t/g;1d' > /etc/purevpn/vpn-list.tsv
printf "==> New server list contains $(wc -l < /etc/purevpn/vpn-list.tsv) lines.\n"
}
list() {
printf "==> The following VPN locations are available\n"
while IFS=$'\t' read region country city pptp udp tcp
do
echo "$region/$country, ${bold}\""$city\""${normal}"
done < /etc/purevpn/vpn-list.tsv
}
get_pptp_url() {
while IFS=$'\t' read region country city pptp udp tcp
do
if [ "${city}" == "${config}" ]; then
printf "$pptp"
fi
done < /etc/purevpn/vpn-list.tsv
}
get_tcp_url() {
while IFS=$'\t' read region country city pptp udp tcp
do
if [ "${city}" == "${config}" ]; then
printf "$tcp"
fi
done < /etc/purevpn/vpn-list.tsv
}
get_upd_url() {
while IFS=$'\t' read region country city pptp udp tcp
do
if [ "${city}" == "${config}" ]; then
printf "$udp"
fi
done < /etc/purevpn/vpn-list.tsv
}
process_ovpn() {
printf "Creating OpenVPN config for $config\n"
cleanconfig=$(echo $config | tr -d '[:punct:]|[:space:]')
mkdir -p /tmp/purevpn
for proto in TCP UDP
do
filename=$(printf "PureVPN-${cleanconfig}-${proto}")
cp "/usr/lib/purevpn/template-ovpn" "/tmp/purevpn/${filename}"
if [ "$proto" == "UDP" ]
then
host=$(get_upd_url)
port=53
elif [ "$proto" == "TCP" ]
then
host=$(get_tcp_url)
port=80
sed -i '/port=/a \proto-tcp=yes' "/tmp/purevpn/${filename}"
fi
if [[ $host == "" ]]; then
printf "Looks like there is no host for this city and protocol.\n"
exit 1
fi
sed -i "s/\bid=/&${filename}/" "/tmp/purevpn/${filename}" #id
sed -i "s/\buuid=/&$(uuidgen)/" "/tmp/purevpn/${filename}" #uuid
sed -i "s/\bport=/&${port}/" "/tmp/purevpn/${filename}" #port
sed -i "s/\bremote=/&$host:${port}/" "/tmp/purevpn/${filename}" #host
chmod 600 "/tmp/purevpn/${filename}" #permissions
mv "/tmp/purevpn/${filename}" /etc/NetworkManager/system-connections
done
}
process_pptp() {
printf "Creating PPTP config for $config\n"
mkdir -p /tmp/purevpn
cleanconfig=$(echo $config | tr -d '[:punct:]|[:space:]')
filename=$(printf "PureVPN-${cleanconfig}-PPTP")
cp "/usr/lib/purevpn/template-pptp" "/tmp/purevpn/${filename}"
host=$(get_pptp_url)
if [[ $host == "" ]]; then
printf "Looks like there is no host for this city and protocol.\n"
exit 1
fi
sed -i "s/\bid=/&${filename}/" "/tmp/purevpn/${filename}" #id
sed -i "s/\buuid=/&$(uuidgen)/" "/tmp/purevpn/${filename}" #uuid
sed -i "s/\bgateway=/&$host/" "/tmp/purevpn/${filename}" #host
chmod 600 "/tmp/purevpn/${filename}" #permissions
mv "/tmp/purevpn/${filename}" /etc/NetworkManager/system-connections
}
addpass() {
cd /etc/NetworkManager/system-connections
find -iname "*PureVPN*" -print0 | while read -d $'\0' file
do
sed -i "s/\bpassword=.*/password=${passw}/g" "$file"
done
}
adduser() {
cd /etc/NetworkManager/system-connections
find -iname "*PureVPN*" -print0 | while read -d $'\0' file
do
if [[ $file == *"PPTP"* ]]; then
sed -i "s/\buser=.*/user=${uname}/g" "$file"
else
sed -i "s/\busername=.*/username=${uname}/g" "$file"
fi
done
}
clean() {
printf "Purging all PureVPN files!\n"
find /etc/NetworkManager/system-connections -iname "*PureVPN*" -exec rm -v {} \;
}
while getopts "hlc:t:u:p:xz" opt
do
case $opt in
h)
usage
exit 0
;;
l)
list
exit 0
;;
c)
configs+=("${OPTARG}")
;;
t)
protocs+=("${OPTARG}")
;;
u)
uname=$OPTARG
;;
p)
passw=$OPTARG
;;
x)
clean
exit 0
;;
z)
updlist
exit 0
;;
\?)
usage
exit 1
;;
esac
done
## Main script
if [ -z "$protocs" ];
then
if [[ -v uname ]] || [[ -v passw ]];
then
SKIPCITY="true"
else
printf "No protocol was specified. No files will be installed.\n"
exit 1;
fi
fi
if [ -z "$configs" ];
then
if [[ -v uname ]] || [[ -v passw ]];
then
SKIPCITY="true"
else
printf "No city specified. No files will be installed.\n"
exit 1;
fi
fi
if [ -z "$SKIPCITY" ];
then
for config in "${configs[@]}"; do
if list | grep "${config}" > /dev/null; then
for protoc in "${protocs[@]}"; do
if [ "$protoc" == "pptp" ]; then
process_pptp
elif [ "$protoc" == "ovpn" ]; then
process_ovpn
else
printf "$protoc does not seem to be a valid protocol. Choose pptp or ovpn.\n"
warn;
fi
done
else
printf "We couldn't find a city called $config.\n"
printf "Try running purevpn -l to list available cities.\n"
exit 1;
fi
done
fi
if [[ -z "$uname" ]]; then
printf "No username was specified.\n"
else
printf "Adding the username to the installed config files.\n"
adduser
fi
if [[ -z "$passw" ]]; then
printf "No password was specified.\n"
else
printf "Adding the password to the installed config files.\n"
addpass
fi
printf "Restarting NetworkManager.\n"
systemctl restart NetworkManager
exit 0
|