-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvpnbook
executable file
·156 lines (138 loc) · 3.34 KB
/
vpnbook
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
#!/bin/sh
# The MIT License (MIT)
# Copyright (c) 2014 Tobias Bell <[email protected]>
PROGRAM="vpnbook"
# URL to the site containing user and password
SITE="https://www.vpnbook.com/freevpn"
# URL to the site containing OpenVPN configs
OPENVPN_SITE="https://www.vpnbook.com"
# File where VPNBook credentials get stored
AUTH_FILE="./vpnbook.auth"
# Path to temporary file
AUTH_FILE_TMP="/tmp/vpnbook.$$"
# Folder where OpenVPN configs are created
CONFIG_FOLDER="./"
# Path to temporary folder for config generation
CONFIG_FOLDER_TEMP="/tmp/vpnbook-config.$$"
cleanup() {
rm -f "$AUTH_FILE_TMP"
rm -rf "$CONFIG_FOLDER_TEMP"
}
trap cleanup HUP INT TERM
usage() {
cat << EOF
Usage: $PROGRAM <command> [parameter]
Commands are
config Generate OpenVPN configs for non-interactive usage based on configs provided
on $OPENVPN_SITE
auth Extract user and password from $OPENVPN_SITE and save to auth-user-pass file
(default: $AUTH_FILE)
Parameters are
-a <file> Path to the auth_user_pass file (default: $AUTH_FILE)
-c <folder> Folder where OpenVPN configs are generated (default: $CONFIG_FOLDER)
-h Show this help
EOF
}
download_site() {
local site="$1"
curl "$site" -s
}
extract_credentials() {
awk -F '[<>]' '
BEGIN { exit_value = 1 }
/Username:/ && !user_found { print $5; user_found = 1; next }
/Password:/ && user_found { print $5; exit_value = 0; exit }
END { exit exit_value }
'
}
extract_config_urls() {
local site="$1"
awk -F '[<>]' -v site=$site '
/free-openvpn-account/ { split($4, a, /"/); print site a[2] }
'
}
generate_auth() {
local data="$1"
if (echo "$data" | extract_credentials) > "$AUTH_FILE_TMP"; then
mv "$AUTH_FILE_TMP" "$AUTH_FILE"
chmod 600 "$AUTH_FILE"
fi
}
generate_vpn_config() {
awk -v auth_file="$AUTH_FILE" '
# Remove windows newline
{ sub(/\r$/, "") }
# Add both ports in config file and let OpenVPN select them remote-random
/^remote/ {
host = $2; print
print $1, host, 25000
if (!remote_random_printed) {
print "remote-random"
remote_random_printed = 1
}
next
}
# Switch authentication from interactive to file based
/^auth-user-pass/ {
print $1, auth_file
print "auth-retry nointeract"
next
}
# Let OpenVPN check the remote certificate
/^<ca>/ {
print "ns-cert-type server"
}
# Output everything else unchanged
{ print }
'
}
generate_config() {
local data="$1"
mkdir "$CONFIG_FOLDER_TEMP" || return
for url in $(echo "$site_data" | extract_config_urls "$OPENVPN_SITE"); do
local file="$CONFIG_FOLDER_TEMP/${url##*/}"
local conf_file="vpnbook-${file##*-}"
conf_file=${conf_file%.zip}.ovpn
conf_file="$CONFIG_FOLDER/$(echo $conf_file | tr 'A-Z' 'a-z')"
curl "$url" -o "$file" -s
unzip -p "$file" '*udp53*.ovpn' | generate_vpn_config > "$conf_file"
done
generate_auth "$data"
}
main() {
case $1 in
config | auth )
generator_func="generate_$1"
;;
* )
usage
exit 1
;;
esac
shift
while getopts 'a:c:h' options; do
case $options in
a )
auth_file=$OPTARG
;;
c )
config_folder=$OPTARG
;;
h | \?)
usage
exit 1
;;
esac
done
AUTH_FILE=${auth_file:-$AUTH_FILE}
CONFIG_FOLDER=${config_folder:-$CONFIG_FOLDER}
local site_data="$(download_site "$SITE")"
$generator_func "$site_data"
retval=$?
cleanup
return $retval
}
if [ "$PROGRAM" = ${0##*/} ]; then
main "$@"
exit $?
fi