This repository has been archived by the owner on Feb 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathprotonvpn-cli.sh
executable file
·1602 lines (1380 loc) · 58.8 KB
/
protonvpn-cli.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
######################################################
# ProtonVPN CLI
# ProtonVPN Command-Line Tool
#
# Made with <3 for Linux + macOS.
###
#Author: Mazin Ahmed <Mazin AT ProtonMail DOT ch>
######################################################
version=1.1.2
if [[ ("$UID" != 0) && ("$1" != "ip") && ("$1" != "-ip") && \
("$1" != "--ip") && ! (-z "$1") && ("$1" != "-h") && \
("$1" != "--status") && ("$1" != "-status") && ("$1" != "status") && \
("$1" != "--help") && ("$1" != "--h") && ("$1" != "-help") && \
("$1" != "help") && ("$1" != "--version") && ("$1" != "-version") && \
("$1" != "-v") && ("$1" != "--v")]]; then
echo "[!] Error: The program requires root access."
exit 1
fi
function check_requirements() {
if [[ -z $(which openvpn) ]]; then
echo "[!] Error: openvpn is not installed. Install \`openvpn\` package to continue."
exit 1
fi
if [[ -n $(which python) ]]; then
python=$(which python)
elif [[ -n $(which python3) ]]; then
python=$(which python3)
elif [[ -n $(which python2) ]]; then
python=$(which python2)
fi
if [[ -z "$python" ]]; then
echo "[!] Error: python is not installed. Install \`python\` package to continue."
exit 1
fi
if [[ -z $(which dialog) ]]; then
echo "[!] Error: dialog is not installed. Install \`dialog\` package to continue."
exit 1
fi
if [[ -z $(which wget) ]]; then
echo "[!] Error: wget is not installed. Install \`wget\` package to continue."
exit 1
fi
if [[ -z $(which sysctl) && ( $(detect_platform_type) != "macos" ) ]]; then
echo "[!] Error: sysctl is not installed. Install \`sysctl\` package to continue."
exit 1
fi
if [[ $(detect_platform_type) != "macos" ]]; then
if [[ ( -z $(which iptables) ) || ( -z $(which iptables-save) ) || ( -z $(which iptables-restore) ) ]]; then
echo "[!] Error: iptables is not installed. Install \`iptables\` package to continue."
exit 1
fi
fi
sha512sum_func
if [[ -z "$sha512sum_tool" ]]; then
echo "[!] Error: sha512sum is not installed. Install \`sha512sum\` package to continue."
exit 1
fi
if [[ (! -x "/etc/openvpn/update-resolv-conf") && ( $(detect_platform_type) != "macos") ]]; then
echo "[!] Error: update-resolv-conf is not installed."
read -p "Would you like protonvpn-cli to install update-resolv-conf? (y/N): " "user_confirm"
if [[ "$user_confirm" == "y" || "$user_confirm" == "Y" ]]; then
install_update_resolv_conf
else
exit 1
fi
fi
}
function get_home() {
if [[ -z "$SUDO_USER" ]]; then
CURRENT_USER="$(whoami)"
else
CURRENT_USER="$SUDO_USER"
fi
USER_HOME=$(getent passwd "$CURRENT_USER" 2> /dev/null | cut -d: -f6)
if [[ -z "$USER_HOME" ]]; then
USER_HOME="$HOME"
fi
echo "$USER_HOME"
}
function sha512sum_func() {
if [[ -n $(which sha512sum) ]]; then
sha512sum_tool="$(which sha512sum)"
elif [[ -n $(which shasum) ]]; then
sha512sum_tool="$(which shasum) -a 512 "
fi
export sha512sum_tool
}
function get_protonvpn_cli_home() {
echo "$(get_home)/.protonvpn-cli"
}
function install_update_resolv_conf() {
if [[ ("$UID" != 0) ]]; then
echo "[!] Error: Installation requires root access."
exit 1
fi
echo "[*] Installing update-resolv-conf..."
mkdir -p "/etc/openvpn/"
file_sha512sum="81cf5ed20ec2a2f47f970bb0185fffb3e719181240f2ca3187dbee1f4d102ce63ab048ffee9daa6b68c96ac59d1d86ad4de2b1cfaf77f1b1f1918d143e96a588"
wget "https://raw.githubusercontent.com/ProtonVPN/scripts/master/update-resolv-conf.sh" -O "/etc/openvpn/update-resolv-conf"
if [[ ($? == 0) && ($($sha512sum_tool "/etc/openvpn/update-resolv-conf" | cut -d " " -f1) == "$file_sha512sum") ]]; then
chmod +x "/etc/openvpn/update-resolv-conf"
echo "[*] Done."
else
echo "[!] Error installing update-resolv-conf."
rm -f "/etc/openvpn/update-resolv-conf" 2> /dev/null
exit 1
fi
}
function check_ip() {
counter=0
ip=""
while [[ "$ip" == "" ]]; do
if [[ $counter -lt 3 ]]; then
ip=$(wget --header 'x-pm-appversion: Other' \
--header 'x-pm-apiversion: 3' \
--header 'Accept: application/vnd.protonmail.v1+json' \
-o /dev/null \
--timeout 6 --tries 1 -q -O - 'https://api.protonmail.ch/vpn/location' \
| $python -c 'import json; _ = open("/dev/stdin", "r").read(); print(json.loads(_)["IP"])' 2> /dev/null)
counter=$((counter+1))
else
ip="Error."
fi
if [[ -z "$ip" ]]; then
sleep 2 # Sleep for 2 seconds before retrying.
fi
done
echo "$ip"
}
function create_vi_bindings() {
echo -en "
bindkey menubox \\j ITEM_NEXT
bindkey menubox \\k ITEM_PREV
bindkey menubox \\q ESC
bindkey menubox \\g PAGE_FIRST
bindkey menubox \\G PAGE_LAST
bindkey menubox \\l FIELD_NEXT
bindkey menubox \\h FIELD_NEXT
" > "$(get_protonvpn_cli_home)/.dialogrc"
}
function init_cli() {
if [[ -f "$(get_protonvpn_cli_home)/protonvpn_openvpn_credentials" ]]; then
echo -n "[!] User profile for protonvpn-cli has already been initialized. Would you like to start over with a fresh configuration? [Y/n]: "
read "reset_profile"
fi
if [[ ("$reset_profile" == "n" || "$reset_profile" == "N") ]]; then
echo "[*] Profile initialization canceled."
exit 0
fi
rm -rf "$(get_protonvpn_cli_home)/" # Previous profile will be removed/overwritten, if any.
mkdir -p "$(get_protonvpn_cli_home)/"
create_vi_bindings
read -p "Enter OpenVPN username: " "openvpn_username"
read -s -p "Enter OpenVPN password: " "openvpn_password"
echo -e "$openvpn_username\n$openvpn_password" > "$(get_protonvpn_cli_home)/protonvpn_openvpn_credentials"
chown "$USER:$(id -gn $USER)" "$(get_protonvpn_cli_home)/protonvpn_openvpn_credentials"
chmod 0400 "$(get_protonvpn_cli_home)/protonvpn_openvpn_credentials"
echo -e "\n[.] ProtonVPN Plans:\n1) Free\n2) Basic\n3) Plus\n4) Visionary"
protonvpn_tier=""
available_plans=(1 2 3 4)
while [[ $protonvpn_tier == "" ]]; do
read -p "Enter Your ProtonVPN plan ID: " "protonvpn_plan"
case "${available_plans[@]}" in *"$protonvpn_plan"*)
protonvpn_tier=$((protonvpn_plan-1))
;;
4)
protonvpn_tier=$((protonvpn_tier-1)) # Visionary gives access to the same VPNs as Plus.
;;
*)
echo "Invalid input."
;; esac
done
echo -e "$protonvpn_tier" > "$(get_protonvpn_cli_home)/protonvpn_tier"
chown "$USER:$(id -gn $USER)" "$(get_protonvpn_cli_home)/protonvpn_tier"
chmod 0400 "$(get_protonvpn_cli_home)/protonvpn_tier"
read -p "[.] Would you like to use a custom DNS server? (Warning: This would make your VPN connection vulnerable to DNS leaks. Only use it when you know what you're doing) [y/N]: " "use_custom_dns"
if [[ ("$use_custom_dns" == "y" || "$use_custom_dns" == "Y") ]]; then
custom_dns=""
while [[ $custom_dns == "" ]]; do
read -p "Custom DNS Server: " "custom_dns"
done
echo -e "$custom_dns" > "$(get_protonvpn_cli_home)/.custom_dns"
chown "$USER:$(id -gn $USER)" "$(get_protonvpn_cli_home)/.custom_dns"
chmod 0400 "$(get_protonvpn_cli_home)/.custom_dns"
fi
read -p "[.] [Security] Decrease OpenVPN privileges? [Y/n]: " "decrease_openvpn_privileges"
if [[ "$decrease_openvpn_privileges" == "y" || "$decrease_openvpn_privileges" == "Y" || "$decrease_openvpn_privileges" == "" ]]; then
echo "$decrease_openvpn_privileges" > "$(get_protonvpn_cli_home)/.decrease_openvpn_privileges"
fi
# Disabling killswitch prompt
#read -p "[.] Enable Killswitch? [Y/n]: " "enable_killswitch"
#if [[ "$enable_killswitch" == "y" || "$enable_killswitch" == "Y" || "$enable_killswitch" == "" ]]; then
# echo > "$(get_protonvpn_cli_home)/.enable_killswitch"
#fi
config_cache_path="$(get_protonvpn_cli_home)/openvpn_cache/"
rm -rf "$config_cache_path"
mkdir -p "$config_cache_path" # Folder for openvpn config cache.
chown -R "$USER:$(id -gn $USER)" "$(get_protonvpn_cli_home)/"
chmod -R 0400 "$(get_protonvpn_cli_home)/"
echo "[*] Done."
}
function detect_platform_type() {
unameOut="$(uname -s)"
case "${unameOut}" in
Linux*) platform=linux;;
Darwin*) platform=macos;;
CYGWIN*) platform=linux;;
MINGW*) platform=linux;;
*) platform=linux
esac
echo "$platform"
}
function manage_ipv6() {
# ProtonVPN support for IPv6 coming soon.
errors_counter=0
if [[ ("$1" == "disable") && ( $(detect_platform_type) != "macos" ) ]]; then
if [ -n "$(ip -6 a 2> /dev/null)" ]; then
# Save linklocal address and disable IPv6.
ip -6 a | awk '/^[0-9]/ {DEV=$2}/inet6 fe80/ {print substr(DEV,1,length(DEV)-1) " " $2}' > "$(get_protonvpn_cli_home)/.ipv6_address"
if [[ $? != 0 ]]; then errors_counter=$((errors_counter+1)); fi
sysctl -w net.ipv6.conf.all.disable_ipv6=1 &> /dev/null
if [[ $? != 0 ]]; then errors_counter=$((errors_counter+1)); fi
sysctl -w net.ipv6.conf.default.disable_ipv6=1 &> /dev/null
if [[ $? != 0 ]]; then errors_counter=$((errors_counter+1)); fi
fi
fi
# Disable IPv6 in macOS.
if [[ ("$1" == "disable") && ( $(detect_platform_type) == "macos" ) ]]; then
# Get list of services and remove the first line which contains a heading.
ipv6_services="$( networksetup -listallnetworkservices | sed -e '1,1d')"
# Go through the list disabling IPv6 for enabled services, and outputting lines with the names of the services.
echo %s "$ipv6_services" | \
while read ipv6_service ; do
# If first character of a line is an asterisk, the service is disabled, so we skip it.
if [[ "${ipv6_service:0:1}" != "*" ]] ; then
ipv6_status="$( networksetup -getinfo "$ipv6_service" | grep 'IPv6: ' | sed -e 's/IPv6: //')"
if [[ "$ipv6_status" = "Automatic" ]] ; then
networksetup -setv6off "$ipv6_service"
echo "$ipv6_service" >> "$(get_protonvpn_cli_home)/.ipv6_services"
fi
fi
done
fi
if [[ ("$1" == "enable") && ( ! -f "$(get_protonvpn_cli_home)/.ipv6_address" ) && ( $(detect_platform_type) != "macos" ) ]]; then
echo "[!] This is an error in enabling IPv6 on the machine. Please enable it manually."
fi
if [[ ("$1" == "enable") && ( -f "$(get_protonvpn_cli_home)/.ipv6_address" ) && ( $(detect_platform_type) != "macos" ) ]]; then
sysctl -w net.ipv6.conf.all.disable_ipv6=0 &> /dev/null
if [[ $? != 0 ]]; then errors_counter=$((errors_counter+1)); fi
sysctl -w net.ipv6.conf.default.disable_ipv6=0 &> /dev/null
if [[ $? != 0 ]]; then errors_counter=$((errors_counter+1)); fi
# Restore linklocal on default interface.
while read -r DEV ADDR; do
ip addr add "$ADDR" dev "$DEV" &> /dev/null
if [[ ($? != 0) && ($? != 2) ]]; then errors_counter=$((errors_counter+1)) ; fi
done < "$(get_protonvpn_cli_home)/.ipv6_address"
rm -f "$(get_protonvpn_cli_home)/.ipv6_address"
fi
if [[ ("$1" == "enable") && ( ! -f "$(get_protonvpn_cli_home)/.ipv6_services" ) && ( $(detect_platform_type) == "macos" ) ]]; then
echo "[!] This is an error in enabling IPv6 on the machine. Please enable it manually."
fi
# Restore IPv6 in macOS.
if [[ ("$1" == "enable") && ( -f "$(get_protonvpn_cli_home)/.ipv6_services" ) && ( $(detect_platform_type) == "macos" ) ]]; then
if [[ $(< "$(get_protonvpn_cli_home)/.ipv6_services") == "" ]] ; then
return
fi
ipv6_service=$(< "$(get_protonvpn_cli_home)/.ipv6_services")
while read ipv6_service ; do
networksetup -setv6automatic "$ipv6_service"
done < "$(get_protonvpn_cli_home)/.ipv6_services"
rm -f "$(get_protonvpn_cli_home)/.ipv6_services"
fi
if [[ $errors_counter != 0 ]]; then
echo "[!] There are issues in managing IPv6 in the system. Please test the system for the root cause."
echo "Not being able to manage IPv6 by protonvpn-cli may leak the system's IPv6 address."
fi
}
function sanitize_interface_name() {
echo "$1" | sed 's/[^a-zA-Z0-9_-]/_/g'
}
function modify_dns() {
# Backup DNS entries.
if [[ ("$1" == "backup") ]]; then
if [[ ( $(detect_platform_type) == "macos" ) ]]; then
networksetup listallnetworkservices | tail +2 | while read interface; do
networksetup -getdnsservers "$interface" > "$(get_protonvpn_cli_home)/$(sanitize_interface_name "$interface").dns_backup"
done
else # non-Mac
cp "/etc/resolv.conf" "$(get_protonvpn_cli_home)/.resolv.conf.protonvpn_backup"
fi
fi
# Apply ProtonVPN DNS.
if [[ ("$1" == "to_protonvpn_dns") ]]; then
connection_logs="$(get_protonvpn_cli_home)/connection_logs"
dns_server=$(grep 'dhcp-option DNS' "$connection_logs" | head -n 1 | awk -F 'dhcp-option DNS ' '{print $2}' | cut -d ',' -f1) # ProtonVPN internal DNS.
if [[ ( $(detect_platform_type) == "macos" ) ]]; then
networksetup listallnetworkservices | tail +2 | while read interface; do
networksetup -setdnsservers "$interface" $dns_server
done
else # non-Mac
echo -e "# ProtonVPN DNS - protonvpn-cli\nnameserver $dns_server" > "/etc/resolv.conf"
fi
fi
# Apply Custom DNS.
if [[ ("$1" == "to_custom_dns") ]]; then
custom_dns="$(get_protonvpn_cli_home)/.custom_dns"
dns_server=$(< "$custom_dns")
if [[ ( $(detect_platform_type) == "macos" ) ]]; then
networksetup listallnetworkservices | tail +2 | while read interface; do
networksetup -setdnsservers "$interface" $dns_server
done
else # non-Mac
echo -e "# ProtonVPN DNS - Custom DNS\nnameserver $dns_server" > "/etc/resolv.conf"
fi
fi
# Restore backed-up DNS entries.
if [[ "$1" == "revert_to_backup" ]]; then
if [[ ( $(detect_platform_type) == "macos" ) ]]; then
networksetup listallnetworkservices | tail +2 | while read interface; do
file="$(get_protonvpn_cli_home)/$(sanitize_interface_name "$interface").dns_backup"
if [[ -f "$file" ]]; then
if grep -q "There aren't any DNS Servers set" "$file"; then
networksetup -setdnsservers "$interface" empty
else
networksetup -setdnsservers "$interface" "$(< "$file")"
fi
fi
done
else # non-Mac
cp "$(get_protonvpn_cli_home)/.resolv.conf.protonvpn_backup" "/etc/resolv.conf"
fi
fi
}
function is_internet_working_normally() {
if [[ "$(check_ip)" != "Error." ]]; then
echo true
else
echo false
fi
}
function check_if_internet_is_working_normally() {
if [[ "$(is_internet_working_normally)" == false ]]; then
echo "[!] Error: There is an internet connection issue."
exit 1
fi
}
function is_openvpn_currently_running() {
if [[ $(pgrep openvpn) == "" ]]; then
echo false
else
echo true
fi
}
function check_if_openvpn_is_currently_running() {
if [[ $(is_openvpn_currently_running) == true ]]; then
echo "[!] Error: OpenVPN is already running on this machine."
exit 1
fi
}
function check_if_profile_initialized() {
_=$(cat "$(get_protonvpn_cli_home)/protonvpn_openvpn_credentials" "$(get_protonvpn_cli_home)/protonvpn_tier" &> /dev/null)
if [[ $? != 0 ]]; then
echo "[!] Profile is not initialized."
echo -e "Initialize your profile using: \n $(basename $0) --init"
exit 1
fi
}
function openvpn_disconnect() {
max_checks=3
counter=0
disconnected=false
if [[ "$1" != "quiet" ]]; then
echo "Disconnecting..."
fi
if [[ $(is_openvpn_currently_running) == true ]]; then
manage_ipv6 enable # Enabling IPv6 on machine.
fi
while [[ $counter -lt $max_checks ]]; do
pkill -f openvpn
sleep 0.50
if [[ $(is_openvpn_currently_running) == false ]]; then
modify_dns revert_to_backup # Reverting to original DNS entries
disconnected=true
# killswitch disable # Disabling killswitch
cp "$(get_protonvpn_cli_home)/.connection_config_id" "$(get_protonvpn_cli_home)/.previous_connection_config_id" 2> /dev/null
cp "$(get_protonvpn_cli_home)/.connection_selected_protocol" "$(get_protonvpn_cli_home)/.previous_connection_selected_protocol" 2> /dev/null
rm -f "$(get_protonvpn_cli_home)/.connection_config_id" "$(get_protonvpn_cli_home)/.connection_selected_protocol" 2> /dev/null
if [[ "$1" != "quiet" ]]; then
echo "[#] Disconnected."
echo "[#] Current IP: $(check_ip)"
fi
if [[ "$2" != "dont_exit" ]]; then
exit 0
else
break
fi
fi
counter=$((counter+1))
done
if [[ "$disconnected" == false ]]; then
if [[ "$1" != "quiet" ]]; then
echo "[!] Error disconnecting OpenVPN."
if [[ "$2" != "dont_exit" ]]; then
exit 1
fi
fi
fi
}
function openvpn_connect() {
check_if_openvpn_is_currently_running
modify_dns backup # Backing-up current DNS entries.
manage_ipv6 disable # Disabling IPv6 on machine.
# killswitch backup_rules # Backing-up firewall rules.
config_id=$1
selected_protocol=$2
if [[ -z "$selected_protocol" ]]; then
selected_protocol="udp" # Default protocol
fi
current_ip="$(check_ip)"
connection_logs="$(get_protonvpn_cli_home)/connection_logs"
openvpn_config="$(get_protonvpn_cli_home)/protonvpn_openvpn_config.conf"
rm -f "$connection_logs" # Remove previous connection logs.
rm -f "$openvpn_config" # Remove previous openvpn config.
config_cache_path="$(get_protonvpn_cli_home)/openvpn_cache/"
mkdir -p "$config_cache_path" # Folder for openvpn config cache.
if [[ "$PROTONVPN_CLI_LOG" == "true" ]]; then # PROTONVPN_CLI_LOG is retrieved from env.
# This option only prints the path of connection_logs to end-user.
echo "[*] CLI logging mode enabled."
echo -e "[*] Logs path: $connection_logs"
fi
# Set PROTONVPN_CLI_DAEMON=false to disable daemonization of openvpn.
PROTONVPN_CLI_DAEMON=${PROTONVPN_CLI_DAEMON:=true}
wget \
--header 'x-pm-appversion: Other' \
--header 'x-pm-apiversion: 3' \
--header 'Accept: application/vnd.protonmail.v1+json' \
-o /dev/null \
--timeout 10 --tries 1 -q -O "$openvpn_config" \
"https://api.protonmail.ch/vpn/config?Platform=$(detect_platform_type)&LogicalID=$config_id&Protocol=$selected_protocol"
config_cache_name="$config_cache_path/$(detect_platform_type)-$config_id-$selected_protocol"
if [[ -f "$config_cache_name" ]]; then
if [[ $(diff "$config_cache_name" "$openvpn_config") ]]; then
echo "Configuration changed (of $(detect_platform_type)-$selected_protocol-$config_id)"
fi
fi
cp "$openvpn_config" "$config_cache_name"
echo "Connecting..."
{
max_checks=3
counter=0
while [[ $counter -lt $max_checks ]]; do
sleep 6
new_ip="$(check_ip)"
if [[ ("$current_ip" != "$new_ip") && ("$new_ip" != "Error.") ]]; then
echo "[$] Connected!"
echo "[#] New IP: $new_ip"
# DNS management
if [[ -f "$(get_protonvpn_cli_home)/.custom_dns" ]]; then
modify_dns to_custom_dns # Use Custom DNS.
echo "[Warning] You have chosen to use a custom DNS server. This may make you vulnerable to DNS leaks. Re-initialize your profile to disable the use of custom DNS."
else
modify_dns to_protonvpn_dns # Use ProtonVPN DNS server.
fi
# killswitch enable # Enable killswitch
echo "$config_id" > "$(get_protonvpn_cli_home)/.connection_config_id"
echo "$selected_protocol" > "$(get_protonvpn_cli_home)/.connection_selected_protocol"
exit 0
fi
counter=$((counter+1))
done
echo "[!] Error connecting to VPN."
if grep -q "AUTH_FAILED" "$connection_logs"; then
echo "[!] Reason: Authentication failed. Please check your ProtonVPN OpenVPN credentials."
fi
openvpn_disconnect quiet dont_exit
exit 1
} &
status_check_pid=$!
OPENVPN_OPTS=(
--config "$openvpn_config"
--auth-user-pass "$(get_protonvpn_cli_home)/protonvpn_openvpn_credentials"
--auth-retry nointeract
--verb 4
--log "$connection_logs"
)
if [[ -f "$(get_protonvpn_cli_home)/.decrease_openvpn_privileges" ]]; then
OPENVPN_OPTS+=(--user nobody
--group "$(id -gn nobody)"
)
fi
if [[ $PROTONVPN_CLI_DAEMON == true ]]; then
openvpn --daemon "${OPENVPN_OPTS[@]}"
trap 'openvpn_disconnect "" dont_exit' INT TERM
else
trap 'openvpn_disconnect "" dont_exit' INT TERM
openvpn "${OPENVPN_OPTS[@]}"
openvpn_exit=$?
fi
wait $status_check_pid
status_exit=$?
if [[ $PROTONVPN_CLI_DAEMON != true ]] && (( status_exit == 0 )); then
status_exit=$openvpn_exit
fi
exit $status_exit
}
function update_cli() {
check_if_internet_is_working_normally
cli_path="/usr/local/bin/protonvpn-cli"
if [[ ! -f "$cli_path" ]]; then
echo "[!] Error: protonvpn-cli does not seem to be installed."
exit 1
fi
echo "[#] Checking for update..."
current_local_hashsum=$($sha512sum_tool "$cli_path" | cut -d " " -f1)
remote_=$(wget --timeout 6 -o /dev/null -q -O - 'https://raw.githubusercontent.com/ProtonVPN/protonvpn-cli/master/protonvpn-cli.sh')
if [[ $? != 0 ]]; then
echo "[!] Error: There is an error updating protonvpn-cli."
exit 1
fi
remote_hashsum=$(echo "$remote_" | $sha512sum_tool | cut -d ' ' -f1)
if [[ "$current_local_hashsum" == "$remote_hashsum" ]]; then
echo "[*] protonvpn-cli is up-to-date!"
exit 0
else
echo "[#] A new update is available."
echo "[#] Updating..."
wget -q --timeout 20 -O "$cli_path" 'https://raw.githubusercontent.com/ProtonVPN/protonvpn-cli/master/protonvpn-cli.sh'
if [[ $? == 0 ]]; then
echo "[#] protonvpn-cli has been updated successfully."
exit 0
else
echo "[!] Error: There is an error updating protonvpn-cli."
exit 1
fi
fi
}
function install_cli() {
mkdir -p "/usr/bin/" "/usr/local/bin/"
cli="$(cd "$(dirname "$0")" && pwd -P)/$(basename "$0")"
errors_counter=0
cp "$cli" "/usr/local/bin/protonvpn-cli" &> /dev/null
if [[ $? != 0 ]]; then errors_counter=$((errors_counter+1)); fi
ln -s -f "/usr/local/bin/protonvpn-cli" "/usr/local/bin/pvpn" &> /dev/null
if [[ $? != 0 ]]; then errors_counter=$((errors_counter+1)); fi
ln -s -f "/usr/local/bin/protonvpn-cli" "/usr/bin/protonvpn-cli" &> /dev/null
if [[ $? != 0 ]]; then errors_counter=$((errors_counter+1)); fi
ln -s -f "/usr/local/bin/protonvpn-cli" "/usr/bin/pvpn" &> /dev/null
if [[ $? != 0 ]]; then errors_counter=$((errors_counter+1)); fi
chown "$USER:$(id -gn $USER)" "/usr/local/bin/protonvpn-cli" "/usr/local/bin/pvpn" "/usr/bin/protonvpn-cli" "/usr/bin/pvpn" &> /dev/null
if [[ $? != 0 ]]; then errors_counter=$((errors_counter+1)); fi
chmod 0755 "/usr/local/bin/protonvpn-cli" "/usr/local/bin/pvpn" "/usr/bin/protonvpn-cli" "/usr/bin/pvpn" &> /dev/null
if [[ $? != 0 ]]; then errors_counter=$((errors_counter+1)); fi
if [[ ($errors_counter == 0) || ( -n $(which protonvpn-cli) ) ]]; then
echo "[*] Done."
exit 0
else
echo "[!] Error: There was an error in installing protonvpn-cli."
exit 1
fi
}
function uninstall_cli() {
if [[ $(is_openvpn_currently_running) == true ]]; then
echo "[!] OpenVPN is currently running."
echo "[!] Session will be disconnected."
openvpn_disconnect quiet dont_exit
if [[ $(is_openvpn_currently_running) == true ]]; then # Checking if OpenVPN is still active.
echo "[!] Error disconnecting OpenVPN."
echo "[!] Please disconnect manually and try the uninstallation again."
exit 1
else
echo "[#] Disconnected."
fi
fi
errors_counter=0
rm -f "/usr/local/bin/protonvpn-cli" "/usr/local/bin/pvpn" "/usr/bin/protonvpn-cli" "/usr/bin/pvpn" &> /dev/null
if [[ $? != 0 ]]; then errors_counter=$((errors_counter+1)); fi
rm -rf "$(get_protonvpn_cli_home)/" &> /dev/null
if [[ $? != 0 ]]; then errors_counter=$((errors_counter+1)); fi
if [[ ($errors_counter == 0) || ( $(which protonvpn-cli) == "" ) ]]; then
echo "[*] Done."
exit 0
else
echo "[!] Error: There was an error in uninstalling protonvpn-cli."
exit 1
fi
}
function print_console_status() {
current_ip="$(check_ip)"
if [[ $(is_openvpn_currently_running) == true ]]; then
echo "[OpenVPN Status]: Running"
else
echo "[OpenVPN Status]: Not Running"
fi
if [[ -f "$(get_protonvpn_cli_home)/.connection_config_id" ]]; then
echo "[ProtonVPN Status]: Running"
else
echo "[ProtonVPN Status]: Not Running"
fi
if [[ "$current_ip" == "Error." ]]; then
echo "[Internet Status]: Offline"
exit 0
else
echo "[Internet Status]: Online"
echo "[Public IP Address]: $current_ip"
fi
if [[ -f "$(get_protonvpn_cli_home)/.connection_config_id" ]]; then
config_id=$(< "$(get_protonvpn_cli_home)/.connection_config_id")
vpn_server_details=$(get_vpn_server_details "$config_id")
server_name=$(echo "$vpn_server_details" | cut -d '@' -f1)
server_exit_country=$(echo "$vpn_server_details" | cut -d '@' -f2)
server_tier=$(echo "$vpn_server_details" | cut -d '@' -f3)
server_features=$(echo "$vpn_server_details" | cut -d '@' -f4)
server_load=$(echo "$vpn_server_details" | cut -d '@' -f5)
selected_protocol=$(tr '[:lower:]' '[:upper:]' < "$(get_protonvpn_cli_home)/.connection_selected_protocol")
echo "[ProtonVPN] [Server Name]: $server_name"
echo "[ProtonVPN] [OpenVPN Protocol]: $selected_protocol"
echo "[ProtonVPN] [Exit Country]: $server_exit_country"
echo "[ProtonVPN] [Tier]: $server_tier"
echo "[ProtonVPN] [Server Features]: $server_features"
echo "[ProtonVPN] [Server Load]: $server_load"
fi
exit 0
}
function get_openvpn_config_info() {
vpn_ip=$(awk '$1 == "remote" {print $2}' "$(get_protonvpn_cli_home)/protonvpn_openvpn_config.conf" | head -n 1)
vpn_port=$(awk '$1 == "remote" {print $3}' "$(get_protonvpn_cli_home)/protonvpn_openvpn_config.conf" | head -n 1)
vpn_type=$(awk '$1 == "proto" {print $2}' "$(get_protonvpn_cli_home)/protonvpn_openvpn_config.conf" | head -n 1)
vpn_device_name=$(grep -P "TUN/TAP device (.)+ opened" "$(get_protonvpn_cli_home)/connection_logs" | awk '{print $9}')
echo "$vpn_ip@$vpn_port@$vpn_type@$vpn_device_name"
}
function killswitch() {
if [[ ! -f "$(get_protonvpn_cli_home)/.enable_killswitch" ]]; then
return
fi
if [[ $1 == "backup_rules" ]]; then
if [[ $(detect_platform_type) == "linux" ]]; then
iptables-save > "$(get_protonvpn_cli_home)/.iptables.save"
elif [[ $(detect_platform_type) == "macos" ]]; then
# Todo: logic
false
fi
fi
if [[ $1 == "enable" ]]; then
if [[ $(detect_platform_type) == "linux" ]]; then
vpn_port=$(get_openvpn_config_info | cut -d "@" -f2)
vpn_type=$(get_openvpn_config_info | cut -d "@" -f3)
vpn_device_name=$(get_openvpn_config_info | cut -d "@" -f4)
iptables -F
iptables -P INPUT DROP
iptables -P OUTPUT DROP
iptables -P FORWARD DROP
iptables -A OUTPUT -o "$vpn_device_name" -j ACCEPT
iptables -A INPUT -i "$vpn_device_name" -j ACCEPT
iptables -A INPUT -i "$vpn_device_name" -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -o "$vpn_device_name" -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -p "$vpn_type" -m "$vpn_type" --dport "$vpn_port" -j ACCEPT
iptables -A INPUT -p "$vpn_type" -m "$vpn_type" --sport "$vpn_port" -j ACCEPT
elif [[ $(detect_platform_type) == "macos" ]]; then
# Todo: logic
false
fi
fi
if [[ $1 == "disable" ]]; then
if [[ $(detect_platform_type) == "linux" ]]; then
iptables -F
iptables-restore < "$(get_protonvpn_cli_home)/.iptables.save"
elif [[ $(detect_platform_type) == "macos" ]]; then
# Todo: logic
false
fi
fi
}
function connect_to_fastest_vpn() {
check_if_profile_initialized
check_if_openvpn_is_currently_running
check_if_internet_is_working_normally
echo "Fetching ProtonVPN servers..."
config_id=$(get_fastest_vpn_connection_id)
selected_protocol="udp"
openvpn_connect "$config_id" "$selected_protocol"
}
function connect_to_fastest_p2p_vpn() {
check_if_profile_initialized
check_if_openvpn_is_currently_running
check_if_internet_is_working_normally
echo "Fetching ProtonVPN servers..."
config_id=$(get_fastest_vpn_connection_id "P2P")
selected_protocol="udp"
openvpn_connect "$config_id" "$selected_protocol"
}
function connect_to_fastest_tor_vpn() {
check_if_profile_initialized
check_if_openvpn_is_currently_running
check_if_internet_is_working_normally
echo "Fetching ProtonVPN servers..."
config_id=$(get_fastest_vpn_connection_id "TOR")
selected_protocol="udp"
openvpn_connect "$config_id" "$selected_protocol"
}
function connect_to_fastest_secure_core_vpn() {
check_if_profile_initialized
check_if_openvpn_is_currently_running
check_if_internet_is_working_normally
echo "Fetching ProtonVPN servers..."
config_id=$(get_fastest_vpn_connection_id "SECURE_CORE")
selected_protocol="udp"
openvpn_connect "$config_id" "$selected_protocol"
}
function connect_to_random_vpn() {
check_if_profile_initialized
check_if_openvpn_is_currently_running
check_if_internet_is_working_normally
echo "Fetching ProtonVPN servers..."
config_id=$(get_random_vpn_connection_id)
available_protocols=("tcp" "udp")
selected_protocol=${available_protocols[$RANDOM % ${#available_protocols[@]}]}
openvpn_connect "$config_id" "$selected_protocol"
}
function connect_to_previous_vpn() {
check_if_profile_initialized
check_if_openvpn_is_currently_running
check_if_internet_is_working_normally
if ! [[ -f "$(get_protonvpn_cli_home)/.previous_connection_config_id" && \
-f "$(get_protonvpn_cli_home)/.previous_connection_selected_protocol" ]]; then
echo "[!] No previous VPN server were found."
exit 1
fi
config_id=$(< "$(get_protonvpn_cli_home)/.previous_connection_config_id")
selected_protocol=$(< "$(get_protonvpn_cli_home)/.previous_connection_selected_protocol")
openvpn_connect "$config_id" "$selected_protocol"
}
function reconnect_to_current_vpn() {
check_if_profile_initialized
if [[ ! -f "$(get_protonvpn_cli_home)/.connection_config_id" ]] ; then
echo "[!] Error: ProtonVPN is not currently running."
exit 1
fi
openvpn_disconnect "quiet" "dont_exit"
if [[ $(is_openvpn_currently_running) == true ]]; then # Checking if OpenVPN is still active.
echo "[!] Error disconnecting OpenVPN."
exit 1
else
echo "[#] Disconnected."
fi
echo "Reconnecting..."
connect_to_previous_vpn
}
function connect_to_specific_server() {
check_if_profile_initialized
check_if_openvpn_is_currently_running
check_if_internet_is_working_normally
echo "Fetching ProtonVPN servers..."
if [[ "$3" == "server" ]]; then
server_list=$(get_vpn_config_details | tr ' ' '@')
fi
if [[ "$3" == "country" ]]; then
server_list=$(get_country_vpn_servers_details | tr ' ' '@')
fi
if [[ "$(echo "$2" | tr '[:upper:]' '[:lower:]')" == "tcp" ]]; then
protocol="tcp"
else
protocol="udp"
fi
if [[ "$3" == "server" ]]; then
for i in $server_list; do
id=$(echo "$i" | cut -d"@" -f1)
name=$(echo "$i" | cut -d"@" -f2)
if [[ "$(echo "$1" | tr '[:upper:]' '[:lower:]')" == "$(echo "$name" | tr '[:upper:]' '[:lower:]')" ]]; then
openvpn_connect "$id" "$protocol"
fi
done
fi
if [[ "$3" == "country" ]]; then
for i in $server_list; do
id=$(echo "$i" | cut -d"@" -f1)
name=$(echo "$i" | cut -d"@" -f2)
country=$(echo "$i" | cut -d"@" -f3)
if [[ "$(echo "$1" | tr '[:upper:]' '[:lower:]')" == "$(echo "$country" | tr '[:upper:]' '[:lower:]')" ]]; then
openvpn_connect "$id" "$protocol"
fi
done
fi
# If not found in $server_list.
if [[ "$3" == "server" ]]; then
echo "[!] Error: Invalid server name, or server not accessible with your plan."
fi
if [[ "$3" == "country" ]]; then
echo "[!] Error: Invalid country name, or country not accessible with your plan."
fi
exit 1
}
function connection_to_vpn_via_dialog_menu() {
check_if_profile_initialized
check_if_openvpn_is_currently_running
check_if_internet_is_working_normally
available_protocols=("udp" " " "tcp" " ")
IFS=$'\n'
ARRAY=()
echo "Fetching ProtonVPN servers..."
if [[ "$1" == "servers" ]]; then
c2=$(get_vpn_config_details)
fi
if [[ "$1" == "countries" ]]; then
c2=$(get_country_vpn_servers_details)
fi
counter=0
for i in $c2; do
ID=$(echo "$i" | cut -d " " -f1)
data=$(echo "$i" | tr '@' ' ' | awk '{$1=""; print $0}' | tr ' ' '@')
counter=$((counter+1))
ARRAY+=("$counter")
ARRAY+=("$data")
done
# Set DIALOGRC to a custom file including VI key binding.
if [[ -f "$(get_protonvpn_cli_home)/.dialogrc" ]]; then
DIALOGRC="$(get_protonvpn_cli_home)/.dialogrc"
export DIALOGRC
fi
config_id=$(dialog --clear --ascii-lines --output-fd 1 --title "ProtonVPN-CLI" --column-separator "@" \
--menu "ID - Name - Country - Load - EntryIP - ExitIP - Features" 35 300 "$((${#ARRAY[@]}/2))" "${ARRAY[@]}" )
clear
if [[ -z "$config_id" ]]; then
exit 1
fi
c=1
for i in $c2; do
ID=$(echo "$i" | cut -d " " -f1)
if [[ $c -eq $config_id ]]; then
ID=$(echo "$i" | cut -d " " -f1)
config_id=$ID
break
fi
c=$((c+1))
done
selected_protocol=$(dialog --clear --ascii-lines --output-fd 1 --title "ProtonVPN-CLI" \
--menu "Select Network Protocol" 35 80 2 "${available_protocols[@]}")
clear
if [[ -z "$selected_protocol" ]]; then
exit 1
fi
openvpn_connect "$config_id" "$selected_protocol"
}
function connection_to_vpn_via_general_dialog_menu() {
echo "[$] Loading..."
check_if_profile_initialized