-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpvpn.sh
executable file
·337 lines (291 loc) · 9.92 KB
/
pvpn.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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#!/usr/bin/env bash
DEFAULT_RETRY=3
PVPN_SCRIPT_NAME="$(basename ${BASH_SOURCE:-$0})"
CONNECT_COMMAND_DESCRIPTION="connect to SERVER (default to fastest server)"
EXIT_CODE_PROTONVPN_NOT_INSTALLED=1
EXIT_CODE_CONNECTION_ERROR=2
EXIT_CODE_INVALID_COMMAND=3
EXIT_CODE_INVALID_ARG=4
EXIT_CODE_INVALID_PROTOCOL=5
EXIT_CODE_INVALID_RETRY_VALUE=6
EXIT_CODE_FLAG_SET_TWICE=7
EXIT_CODE_FLAG_SET_WITH_NO_VALUE=8
EXIT_CODE_TERMINATED_BY_USER=9
EXIT_CODE_UNHANDLED_ERROR=10
function print_intro() {
echo "protonvpn-cli with retry functionality"
}
function print_usage_and_commands() {
echo -e "Usage: \e[1m$PVPN_SCRIPT_NAME\e[0m [\e[1mCOMMAND\e[0m (default to 'c' or 'connect')]"
echo
echo -e "\e[1mCOMMAND:\e[0m"
echo -e "\tc, connect [-p|--protocol|-r|--retry] [SERVER]"
echo -e "\t\t\t $CONNECT_COMMAND_DESCRIPTION."
echo -e "\t\t\t Use '$PVPN_SCRIPT_NAME connect --help' to know more specific arguments"
echo -e "\td, disconnect\t disconnect from vpn"
echo -e "\tr, reconnect\t reconnect to previously connected server"
echo -e "\ts, status\t connection status"
echo -e "\tn, select\t select server manually through ncurses-ui"
echo -e "\th, help\t\t show the help message"
}
function print_help() {
print_intro
echo
print_usage_and_commands
echo
echo -e "set \e[1mDEBUG\e[0m env variable to a non-zero value to running in debug mode."
echo
echo -e "\e[1mExit Codes Definition:\e[0m"
echo -e "\t$EXIT_CODE_PROTONVPN_NOT_INSTALLED => EXIT_CODE_PROTONVPN_NOT_INSTALLED"
echo -e "\t$EXIT_CODE_CONNECTION_ERROR => EXIT_CODE_CONNECTION_ERROR"
echo -e "\t$EXIT_CODE_INVALID_COMMAND => EXIT_CODE_INVALID_COMMAND"
echo -e "\t$EXIT_CODE_INVALID_ARG => EXIT_CODE_INVALID_ARG"
echo -e "\t$EXIT_CODE_INVALID_PROTOCOL => EXIT_CODE_INVALID_PROTOCOL"
echo -e "\t$EXIT_CODE_INVALID_RETRY_VALUE => EXIT_CODE_INVALID_RETRY_VALUE"
echo -e "\t$EXIT_CODE_FLAG_SET_TWICE => EXIT_CODE_FLAG_SET_TWICE"
echo -e "\t$EXIT_CODE_FLAG_SET_WITH_NO_VALUE => EXIT_CODE_FLAG_SET_WITH_NO_VALUE"
echo -e "\t$EXIT_CODE_TERMINATED_BY_USER => EXIT_CODE_TERMINATED_BY_USER"
echo -e "\t$EXIT_CODE_UNHANDLED_ERROR => EXIT_CODE_UNHANDLED_ERROR"
}
function print_connect_help() {
echo -e "Usage: $PVPN_SCRIPT_NAME \e[1mconnect\e[0m [ARGS] [SERVER]"
echo -e "$CONNECT_COMMAND_DESCRIPTION"
echo
echo -e "\e[1mSERVER\e[0m:\n\tconnect to specified server (ie: CH#4, CH-US-1, HK5-Tor)"
echo -e "\e[1mARGS\e[0m:"
echo -e "\t-p, --protocol PROTO\t connect via PROTO protocol (possible values: tcp|udp)"
echo -e "\t-r, --retry N\t\t retry N times before giving up to connect (defaul to $DEFAULT_RETRY)"
}
function print_install_guide() {
echo "Looks like 'ProtonVPN' is not install on your system."
echo -n "To install 'ProtonVPN', open this link on your web browser: "
echo -e "'https://protonvpn.com/download-linux'"
}
function handle_127_exit_code() {
if [ "$1" -eq "127" ]; then
print_install_guide
exit $EXIT_CODE_PROTONVPN_NOT_INSTALLED
fi
}
function status() {
protonvpn-cli s 2>/dev/null
handle_127_exit_code "$?"
}
function select_() {
protonvpn-cli c 2>/dev/null
handle_127_exit_code "$?"
}
function disconnect() {
protonvpn-cli d 2>/dev/null
handle_127_exit_code "$?"
}
function reconnect() {
protonvpn-cli r 2>/dev/null
handle_127_exit_code "$?"
}
function connect_vpn() {
local output="$(protonvpn-cli c "$1" 2>&1)"
local result="$?"
if [ "$result" -ne "0" ]; then
handle_127_exit_code "$result"
echo "$output" | egrep -i 'failed' >/dev/null 2>&1
local conn_failed="$?"
#
# check if connection timeouts so try again.
# else error is not recoverable and should break immidiatly
#
if [ "$conn_failed" -eq "0" ]; then
return "$result"
else
#
# stripe leading "Setting up ProtonVPN" and "Connecting to ProtonVPN" headers and empty lines from output error message
#
output="$(echo -e "$output" | egrep -iv "setting up protonvpn|connecting to protonvpn|^$")"
echo -e "$output"
exit $EXIT_CODE_UNHANDLED_ERROR
fi
else
#
# protonvpn-cli bug that fail to connect but exit with zero code :(
#
###################################################
if [ -n "$DEBUG" ]; then
echo "[DEBUG] protonvpn-cli output on success:"
echo -e "$output"
fi
###################################################
echo "$output" | egrep -i 'unable to connect to protonvpn|an unknown error has occured' >/dev/null 2>&1
local unhandled_error="$?"
if [ "$unhandled_error" -eq "0" ]; then
output="$(echo -e "$output" | egrep -iv "setting up protonvpn|connecting to protonvpn|^$")"
echo -e "$output"
############################################
if [ -n "$DEBUG" ]; then
echo "[DEBUG] retrying..."
return -1
fi
###########################################
echo -e "unhandled 'protonvpn-cli' backend error happend.\nfor more details run '$PVPN_SCRIPT_NAME' in debug mode."
exit $EXIT_CODE_UNHANDLED_ERROR
fi
fi
}
function connect() {
local connection_args=""
local server=""
local protocol=""
local retry=""
#
# parse argumments passed to "c" or "connect" command
#
while [ -n "$1" ]; do
case "$1" in
-p | --protocol)
if [ -n "$protocol" ]; then
echo "error: '-p|--protocol' flag set twice"
echo
print_connect_help
exit $EXIT_CODE_FLAG_SET_TWICE
fi
if [ -z "$2" ]; then
echo "error: '-p|--protocol' flag set but no protocol specified"
echo
print_connect_help
exit $EXIT_CODE_FLAG_SET_WITH_NO_VALUE
fi
shift
protocol="$1"
;;
-r | --retry)
if [ -n "$retry" ]; then
echo "error: '-r|--retry' flag set twice"
echo
print_connect_help
exit $EXIT_CODE_FLAG_SET_TWICE
fi
if [ -z "$2" ]; then
echo "error: '-r|--retry' flag set but no value specified"
echo
print_connect_help
exit $EXIT_CODE_FLAG_SET_WITH_NO_VALUE
fi
shift
retry="$1"
;;
-h | --help)
print_connect_help
exit 0
;;
*)
if [ -n "$server" ]; then
echo "error: invalid argument '$1'"
exit $EXIT_CODE_INVALID_ARG
fi
server="$1"
esac
shift
done
if [ -z "$protocol" ]; then
protocol="udp"
elif [ "$protocol" != "udp" ] && [ "$protocol" != "tcp" ]; then
echo "error: invalid protocol '$protocol'"
echo
print_connect_help
exit $EXIT_CODE_INVALID_PROTOCOL
fi
if [ -z "$retry" ]; then
retry="$DEFAULT_RETRY"
fi
if ! echo "$retry" | egrep -q '^[0-9]+$'; then
echo "error: invalid retry value '$retry'"
echo
print_connect_help
exit $EXIT_CODE_INVALID_RETRY_VALUE
fi
if [ "$retry" -lt 0 ]; then
echo -e "negative retry value '$retry'. default to '$DEFAULT_RETRY'..."
retry="$DEFAULT_RETRY"
fi
if [ -z "$server" ]; then
connection_args="-f -p $protocol"
else
connection_args="$server -p $protocol"
fi
###########################################################################################################
if [ -n "$DEBUG" ]; then
echo -e "[DEBUG] server: $server, protocol: $protocol, retry: $retry, connection args: $connection_args"
fi
###########################################################################################################
local tried_counts=0
local result=0
#
# retry facility
#
for _ in $(seq "$(expr 1 + "$retry")"); do
connect_vpn $connection_args
result=$?
if [ "$result" -ne 0 ]; then
if [ "$tried_counts" -lt "$retry" ]; then
((++tried_counts))
clear_the_line
echo -ne "retrying within 1 second ($tried_counts/$retry)...\r"
sleep 1
fi
else
break
fi
done
clear_the_line
if [ "$result" -ne 0 ]; then
script_name="$(basename ${BASH_SOURCE[0]:-$0})"
if [ "$retry" -gt 0 ]; then
echo "cannot connect to server (retried $retry times). try '$script_name n|select'"
else
echo "cannot connect to server. try '$script_name n|select'"
fi
exit $EXIT_CODE_CONNECTION_ERROR
fi
status
}
####################################################################################
function clear_the_line() {
echo -ne " \r"
}
function ctrl_c() {
clear_the_line
echo -e "** opration terminated by user..."
exit $EXIT_CODE_TERMINATED_BY_USER
}
# trap ctrl-c and call ctrl_c()
trap ctrl_c INT
if [ -z "$1" ]; then
connect -r "$DEFAULT_RETRY"
exit $?
fi
case "$1" in
c | connect)
shift
connect "$@"
;;
d | disconnect)
disconnect
;;
r | reconnect)
reconnect
;;
n | select)
select_
;;
s | status)
status
;;
h | help | -h | --help)
print_help
;;
*)
echo "invalid command '$1'"
echo
print_usage_and_commands
exit $EXIT_CODE_INVALID_COMMAND
;;
esac