-
Notifications
You must be signed in to change notification settings - Fork 1
/
wifivpn.sh
executable file
·1011 lines (838 loc) · 28.3 KB
/
wifivpn.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
#-----------------------------------------------------------------------------------
# _ __ _
# __ __ _(_)/ _(_)_ ___ __ _ _
# \ V V / | _| \ V / '_ \ ' \
# \_/\_/|_|_| |_|\_/| .__/_||_|
# |_|
#
#-----------------------------------------------------------------------------------
VERSION="1.3.9"
#-----------------------------------------------------------------------------------
#
# Enables Wifi and Nord VPN connectivity using Network Manager Command Line Interface.
#
# For VPN connect it benchmarks the Nord VPN servers and connects to the fastest one.
#
#-----------------------------------------------------------------------------------
# Author: Rick Ellis
# URL: https://github.com/rickellis/WifiVPN
# License: MIT
#-----------------------------------------------------------------------------------
# Use only servers from a particular country.
# Use 2 letter country code: https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
# To select the fastest server regardless of country leave blank.
COUNTRY_CODE="US"
# Return servers with a load of less than X percent.
# DO NOT include percentage sign.
SERVER_LOAD="10"
# ADDITIONAL CONFIG VARIABLES. UNLIKELY THEY WILL NEED TO BE CHANGED
# Name of the credentials file containing the Nord VPN username/password.
# See README for mor information on creating this.
CREDENTIALS="credentials.sh"
# Nord API server dtata
NORD_SERVER_DATA="https://nordvpn.com/api/server"
# Nord VPN connection files
NORD_CONNECTION_FILES="https://downloads.nordcdn.com/configs/archives/servers/ovpn.zip"
# Geolocation helper URLs
GEOLOOKUP_URL="freegeoip.net/json/"
IPLOOKUP_URL="ipinfo.io/ip"
# Path to the Network Manager Connections folder. This is the path on Arch Linux.
# It's possible that the path might be different on other flavors of Linux.
PROFILE_PATH="/etc/NetworkManager/system-connections"
# Basepath to the directory containing the various assets.
# This allows the basepath to be correct if this script gets aliased in .bashrc
BASEPATH=$(dirname "$0")
# Path to folder containing NordVPN server config files
VPN_SERVERS="${BASEPATH}/vpn-servers"
# Suffix for vpn server config files
# NOTE: We will likely need a more robust solution. There is more than one
# version of the Nord files available at nord.com, and the naming scheme
# is slightly different. This works for now but it might break.
VPN_SERVERS_SFX=".tcp.ovpn"
# The name we're calling the active VPN profile. Every time a new Nord server is
# selected and used, the profile is named the same. This allows us to connect,
# disconnect, and delete the profile without needing a storage mechanism for the name.
PROFILE_NAME="NordVPN"
# Debug mode to show error messages. 1=on, 0=off
DEBUG=0
# ------------------------------------------------------------------------------
# Load colors script to display pretty headings and colored text
# This is an optional (but recommended) dependency
if [ -f $BASEPATH/colors.sh ]; then
. $BASEPATH/colors.sh
else
heading() {
echo " ----------------------------------------------------------------------"
echo " $2"
echo " ----------------------------------------------------------------------"
echo
}
fi
# ------------------------------------------------------------------------------
declare ACTIVECONS
declare BASECON
declare LISTCONS
declare PROFILES
declare CITY
declare STATE
declare TZ
declare CTRY
declare IP
function _reset_connections() {
ACTIVECONS=""
BASECON=""
LISTCONS=""
PROFILES=""
}
function _reset_geolocation() {
IP=""
CITY=""
STATE=""
TZ=""
CTRY=""
}
function _load_connections() {
# Get the name of the active wifi connection
ACTIVECONS=$(nmcli -t -f name con show --active)
# If a VPN connection is active, $ACTIVECONS will contain
# multiple connections separated by newlines.
# This lets us gather the base wifi connection.
BASECON="$ACTIVECONS"
IFS='\n' read -r -a BASECON <<< "$BASECON"
# This removes linebreaks from $ACTIVECONS
# so we can show all the connections on one line.
LISTCONS=${ACTIVECONS//$'\n'/\ -\ }
# Get the names of all existing connection profiles
PROFILES=$(nmcli con show)
}
# ------------------------------------------------------------------------------
# Table with various network and device statuses
function _show_status_table() {
# Get the general status of the network
STATUS=$(nmcli general status)
# This hack allows us to add a left margin to the entire status table.
# It also lets us colorize the output with better colors than the default.
STATUS="${STATUS//$'\n'/$'\012'\ \ }"
STATUS="${STATUS//disconnected/foobar}" # Prevents "connected" from getting replaced
STATUS="${STATUS//connected/${green}Connected${reset}}"
STATUS="${STATUS//foobar/${red}Disconnected${reset}}"
STATUS="${STATUS//full/${green}Full${reset}}"
STATUS="${STATUS//enabled/${green}Enabled${reset}}"
STATUS="${STATUS//disabled/${red}Disabled${reset}}"
STATUS="${STATUS//none/${red}None${reset}}"
STATUS="${STATUS//limited/${yellow}Limited${reset}}"
STATUS="${STATUS//asleep/${yellow}Asleep${reset}}"
STATUS="${STATUS//(site only)/${yellow}(Wifi Only)${reset}}"
STATUS="${STATUS//unknown/${magenta}Unknown${reset}}"
echo -e " ${STATUS}"
echo
}
# ------------------------------------------------------------------------------
# Generate the home screen
function _home_menu() {
clear
unset SELECTION
_load_connections
heading purple "WifiVPN VERSION ${VERSION}"
echo
_show_status_table
if [ -z "${ACTIVECONS}" ]; then
echo -e " You are not connected to a network"
else
echo -e " You are connected to: ${green}${LISTCONS}${reset}"
fi
_geolocation
heading green "MENU"
echo -e " 1) ${green}^${reset} Wifi Connect"
echo -e " 2) ${red}v${reset} Wifi Disconnect"
echo
echo -e " 3) ${green}^${reset} VPN Connect"
echo -e " 4) ${red}v${reset} VPN Disconnect"
echo
echo -e " 5) ${green}>${reset} Utilities"
echo
echo -e " X) ${yellow}<${reset} EXIT"
echo
read -p " ENTER SELECTION: " SELECTION
# If they hit enter we exit
if [ -z "$SELECTION" ]; then
clear
exit 1
fi
# If they hit anything but a valid number we exit
if ! echo "$SELECTION" | egrep -q '^[1-9]+$'; then
clear
exit 1
fi
# Show the selected subpage
case "$SELECTION" in
1)
clear
_wifi_connect
;;
2)
clear
_wifi_disconnect
;;
3)
clear
_vpn_connect
;;
4)
clear
_vpn_disconnect
;;
5)
clear
_utilities
;;
*)
exit 1
;;
esac
}
# ------------------------------------------------------------------------------
# Show avaialble Wifi hotspots and connect to the selected one
function _wifi_connect() {
unset NETWORK
heading purple "WIFI CONNECT"
echo -e " ${green}Scanning networks${reset}"
echo
echo -e " ${yellow}Press \"q\" to show SELECTION prompt if not shown after network list${reset}"
echo
# Rescan the network for a current list of hotspots
if [ $DEBUG -eq 1 ]; then
nmcli device wifi rescan
else
nmcli device wifi rescan >/dev/null 2>&1
fi
# Generate a list of all available hotspots
#nmcli dev wifi
nmcli dev wifi list
echo
echo -e " ENTER THE NAME OF A NETWORK TO CONNECT TO, OR"
echo
echo -e " M) ${yellow}^${reset} MAIN MENU"
echo -e " X) ${yellow}<${reset} EXIT"
echo
read -p " ENTER SELECTION: " NETWORK
# If they hit enter we exit
if [ -z "$NETWORK" ]; then
clear
exit 1
fi
# If they hit "m" we show the home page
if [ "$NETWORK" == 'm' ] || [ "$NETWORK" == 'M' ]; then
clear
_home_menu
exit 1
fi
# If they hit "x" we exit
if [ "$NETWORK" == 'x' ] || [ "$NETWORK" == 'X' ]; then
clear
exit 1
fi
# Before connecting we need to see if a profile
# exists for the supplied network. If it exists
# we use it. If it doesn't, we create it.
if echo "$PROFILES" | egrep -q "(^|\s)${NETWORK}($|\s)"; then
echo
echo -e " ${green}Establishing a connection${reset}"
echo
# Connect, but supress output so we can show our own messages
if [ $DEBUG -eq 1 ]; then
nmcli -t con up id "$NETWORK"
else
nmcli -t con up id "$NETWORK" >/dev/null 2>&1
fi
sleep 2
# Verify that we're connected to the new network
NEWCONN=$(nmcli -t -f name con show --active)
if [ -z "$NEWCONN" ]; then
echo -e " ${red}ERROR: UNABLE TO CONNECT TO: ${reset}${yellow}${NETWORK}${reset}"
else
echo -e " ${green}SUCCESS!${reset} CONNECTED TO: ${yellow}${NETWORK}${reset}"
fi
else
echo
read -p " ENTER PASSWORD (OR HIT ENTER TO LEAVE BLANK): " PASSWD
echo
echo -e " ${green}Establishing a connection${reset}"
echo
# Create a new profile
nmcli -t dev wifi con "${NETWORK}" password "${PASSWD}" name "${NETWORK}"
sleep 3
# Reset the connection variables
_reset_connections
# Verify connection
if echo "$PROFILES" | egrep -q "(^|\s)${NETWORK}($|\s)"; then
echo -e " ${green}SUCCESS!${reset} CONNECTED TO: ${yellow}${NETWORK}${reset}"
else
echo -e " ${red}ERROR:${reset} UNABLE TO CONNECT TO: ${yellow}${NETWORK}${reset}"
fi
fi
_reset_geolocation
_geolocation
_submenu
}
# ------------------------------------------------------------------------------
# Disconnect from the active wifi connection
function _wifi_disconnect() {
heading red "WIFI DISCONNECT"
echo
if [ -z "${ACTIVECONS}" ]; then
echo -e " ${yellow}You are not connected to a wifi network${reset}"
else
echo -e " ${yellow}You have been disconnected from ${BASECON}${reset}"
_wifi_quiet_disconnect
fi
_submenu
}
# ------------------------------------------------------------------------------
# Disconnects fom wifi without showing a message
function _wifi_quiet_disconnect() {
if [ ! -z "${ACTIVECONS}" ]; then
if [ $DEBUG -eq 1 ]; then
nmcli -t con down id "$BASECON"
else
nmcli -t con down id "$BASECON" >/dev/null 2>&1
fi
_reset_geolocation
_reset_connections
fi
}
# ------------------------------------------------------------------------------
# Benchmark the Nord servers and connect to the fastest one
function _vpn_connect() {
unset SELECTION
unset VPN_PROFILE
heading blue "VPN CONNECT"
# Does the credentials file exist? We can't continue without it
if [ ! -f ${BASEPATH}/${CREDENTIALS} ]; then
echo -e "${red}ERROR: No credentials file found${reset}"
echo
echo "Please see README for instructions on setting it up"
echo
_submenu
elif [ -z "${ACTIVECONS}" ]; then
echo
echo -e " ${yellow}You are not connected to a wifi network.${reset}"
echo
echo -e " ${yellow}Before connecting to Nord VPN you must first be connected to wifi.${reset}"
echo
_submenu
else
# Source the credentials file
. ${BASEPATH}/${CREDENTIALS}
# Is there an existing Nord profile?
VPN_PROFILE="n"
if echo "$PROFILES" | egrep -q "(^|\s)${PROFILE_NAME}($|\s)"; then
VPN_PROFILE="y"
fi
echo -e " MENU OPTIONS"
echo
echo -e " N) ${green}^${reset} CONNECT TO THE FASTEST SERVER"
if [ "${VPN_PROFILE}" == "y" ]; then
echo -e " L) ${green}^${reset} CONNECT TO LAST USED PROFILE"
fi
echo
echo -e " M) ${yellow}^${reset} MAIN MENU"
echo -e " X) ${yellow}<${reset} EXIT"
echo
read -p " ENTER SELECTION: " SELECTION
# If they hit enter we exit
if [ -z "$SELECTION" ]; then
clear
exit 1
fi
# If they hit "m" we show the home page
if [ "$SELECTION" == 'm' ] || [ "$SELECTION" == 'M' ]; then
clear
_home_menu
exit 1
fi
# If they hit "x" we exit
if [ "$SELECTION" == 'x' ] || [ "$SELECTION" == 'X' ]; then
clear
exit 1
fi
# If they hit "L" we use the last profile
if [ "$SELECTION" == 'l' ] || [ "$SELECTION" == 'L' ]; then
if [ "${VPN_PROFILE}" == "n" ]; then
echo -e " ${red}INVALID OPTION: ${reset} there are no saved profiles. Aborting..."
clear
exit 1
else
echo
echo -e " ${green}Establishing a connection${reset}"
echo
# Connect
if [ $DEBUG -eq 1 ]; then
nmcli -t con up id "$PROFILE_NAME"
else
nmcli -t con up id "$PROFILE_NAME" >/dev/null 2>&1
fi
sleep 2
# Reload the connection variables
_load_connections
if echo "$PROFILES" | egrep -q "(^|\s)${PROFILE_NAME}($|\s)"; then
echo -e " ${green}SUCCESS! CONNECTED TO: ${reset}${yellow}${PROFILE_NAME}${reset}"
else
echo -e " ${red}ERROR: UNABLE TO CONNECT TO: ${reset}${yellow}${PROFILE_NAME}${reset}"
fi
_reset_geolocation
_geolocation
_submenu
exit 1
fi
fi
if [ ! -d "${VPN_SERVERS}" ]; then
echo
echo -e " ${red}ERROR: VPN connection file directory doesn't exist"
echo
echo -e " ${yellow}Before attempting to connect go to the UTILITIES page${reset}"
echo -e " ${yellow}and download the VPN connection files.${reset}"
_submenu
fi
if [ -z "$(ls ${VPN_SERVERS})" ]; then
echo
echo -e " ${red}ERROR: No VPN connection files exist"
echo
echo -e " ${yellow}Before attempting to connect go to the UTILITIES page${reset}"
echo -e " ${yellow}and download the VPN connection files.${reset}"
_submenu
fi
# Disconnect from the old profile if it exists
if [ ! -z "${ACTIVECONS}" ] && echo "$ACTIVECONS" | egrep -q "(^|\s)${PROFILE_NAME}($|\s)"; then
echo
echo -e " ${red}Disconnecting active VPN${reset}"
if [ $DEBUG -eq 1 ]; then
nmcli -t con down id "${PROFILE_NAME}"
else
nmcli -t con down id "${PROFILE_NAME}" >/dev/null 2>&1
fi
sleep 2
fi
echo
echo -e " ${green}Downloading Nord VPN server data${reset}"
echo
# Fetch the server data from Nord. JSON format.
# This curl/json query by Sean Ewing
# Project: https://github.com/strobilomyces/nordvpn-nm
if [ -z "$COUNTRY_CODE" ]; then
fastest=$(curl -s ${NORD_SERVER_DATA} | jq -r 'sort_by(.load) | .[] | select(.load < '${SERVER_LOAD}' and .features.openvpn_tcp == true ) | .domain')
else
COUNTRY_CODE=${COUNTRY_CODE^^}
fastest=$(curl -s ${NORD_SERVER_DATA} | jq -r 'sort_by(.load) | .[] | select(.load < '${SERVER_LOAD}' and .flag == '\"${COUNTRY_CODE}\"' and .features.openvpn_tcp == true ) | .domain')
fi
server=""
for filename in $fastest; do
server="$filename"
break
done
# No server returned?
if [ "$server" == "" ]; then
echo
echo -e " ${red}ERROR: Server query returned no results.${reset}"
echo
echo -e " ${yellow}Tip: Set a higher load percentage in the script variables.${reset}"
_submenu
exit 1
fi
# Does the local version Nord VPN file exist?
if [ ! -f "${VPN_SERVERS}/${server}${VPN_SERVERS_SFX}" ]; then
echo
echo -e " ${red}ERROR:Unable to find the OVPN file:${reset}"
echo -e " ${yellow}${VPN_SERVERS}/${server}${VPN_SERVERS_SFX}${reset}"
_submenu
exit 1
fi
# A bit of housekeeping.
echo -e " ${red}Deleting old VPN profile${reset}"
echo
if [ $DEBUG -eq 1 ]; then
nmcli con delete id "${PROFILE_NAME}"
else
nmcli con delete id "${PROFILE_NAME}" >/dev/null 2>&1
fi
sleep 2
# Make a copy of the VPN file. We do this becuasse NetworkManager
# names profiles with the filename, so giving the profile a fixed name
# allows us to delete the old profile everytime we run this script.
# There are over 1000 servers to choose from so we would need a
# tracking mechanism if we didn't use the same name.
cp "${VPN_SERVERS}/${server}${VPN_SERVERS_SFX}" "${VPN_SERVERS}/${PROFILE_NAME}.ovpn"
# Import the new profile
echo -e " ${green}Importing new VPN profile${reset}"
echo
if [ $DEBUG -eq 1 ]; then
nmcli con import type openvpn file "${VPN_SERVERS}/${PROFILE_NAME}.ovpn"
else
nmcli con import type openvpn file "${VPN_SERVERS}/${PROFILE_NAME}.ovpn" >/dev/null 2>&1
fi
sleep 2
echo -e " ${green}Configuring profile${reset}"
echo
# Insert username into config file
if [ $DEBUG -eq 1 ]; then
sudo nmcli connection modify "${PROFILE_NAME}" +vpn.data username="${USERNAME}"
else
sudo nmcli connection modify "${PROFILE_NAME}" +vpn.data username="${USERNAME}" >/dev/null 2>&1
fi
# Set the password flag
if [ $DEBUG -eq 1 ]; then
sudo nmcli connection modify "${PROFILE_NAME}" +vpn.data password-flags=0
else
sudo nmcli connection modify "${PROFILE_NAME}" +vpn.data password-flags=0 >/dev/null 2>&1
fi
# Write password into the profile file.
# Note: since the profiles are stored in /root we use sudo tee
if [ $DEBUG -eq 1 ]; then
echo -e "\n\n[vpn-secrets]\npassword=${PASSWORD}" | sudo tee -a "${PROFILE_PATH}/${PROFILE_NAME}"
else
echo -e "\n\n[vpn-secrets]\npassword=${PASSWORD}" | sudo tee -a "${PROFILE_PATH}/${PROFILE_NAME}" >/dev/null 2>&1
fi
sleep 2
# Reload the config file
echo
echo -e " ${green}Reloading config file${reset}"
echo
if [ $DEBUG -eq 1 ]; then
sudo nmcli connection reload "${PROFILE_NAME}"
else
sudo nmcli connection reload "${PROFILE_NAME}" >/dev/null 2>&1
fi
# Delete the temp file
rm "${VPN_SERVERS}/${PROFILE_NAME}.ovpn"
echo -e " ${green}Connecting to ${server}${reset}"
echo
if [ $DEBUG -eq 1 ]; then
nmcli con up id "${PROFILE_NAME}"
else
nmcli con up id "${PROFILE_NAME}" >/dev/null 2>&1
fi
_reset_geolocation
_geolocation
_submenu
fi
}
# ------------------------------------------------------------------------------
# Disconnect from the active VPN connection
function _vpn_disconnect() {
heading red "VPN DISCONNECT"
echo
# If there are no active or VPN connections there is nothing to disconnect
if [ -z "${ACTIVECONS}" ] || ! echo "$ACTIVECONS" | egrep -q "(^|\s)${PROFILE_NAME}($|\s)"; then
echo -e " ${yellow}You are not connected to a VPN${reset}"
else
if [ $DEBUG -eq 1 ]; then
nmcli -t con down id "${PROFILE_NAME}"
else
nmcli -t con down id "${PROFILE_NAME}" >/dev/null 2>&1
fi
_reset_geolocation
echo -e " ${yellow}You have been disconnected from ${PROFILE_NAME}${reset}"
fi
_submenu
}
# ------------------------------------------------------------------------------
# Display city, state, IP
function _geolocation() {
_load_connections
heading blue "GEOLOCATION"
if [ -z "${ACTIVECONS}" ]; then
echo -e " ${yellow}Geolocation data not available${reset}"
else
if [ -z "$IP" ]; then
IP=$(curl -slent ${IPLOOKUP_URL})
IPDATA=$(curl -slent ${GEOLOOKUP_URL}${IP})
CITY=$(echo $IPDATA | jq -r .city) >/dev/null 2>&1
STATE=$(echo $IPDATA | jq -r .region_name) >/dev/null 2>&1
TZ=$(echo $IPDATA | jq -r .time_zone) >/dev/null 2>&1
CTRY=$(echo $IPDATA | jq -r .country_name) >/dev/null 2>&1
if [ -z "$TZ" ]; then
TZ="n/a"
fi
if [ -z "$CTRY" ]; then
CTRY="n/a"
fi
fi
echo -e " IP address: ${cyan}${IP}${reset}"
echo
if [ -z "$CITY" ]; then
echo -e " Location: ${yellow}${CTRY}${reset}"
else
echo -e " Location: ${yellow}${CITY} ${STATE} ${CTRY}${reset}"
fi
echo
echo -e " Timezone: ${blue}${TZ}${reset}"
fi
}
# ------------------------------------------------------------------------------
function _utilities() {
unset SELECTION
heading olive "UTILITIES"
echo -e " 1) ${green}>${reset} Show Active Connections"
echo -e " 2) ${green}>${reset} Show Network Interface Status"
echo
echo -e " 3) ${green}^${reset} Turn Wifi Interface On"
echo -e " 4) ${red}v${reset} Turn Wifi Interface Off"
echo
echo -e " 5) ${green}^${reset} Turn Network Interface On"
echo -e " 6) ${red}v${reset} Turn Network Interface Off"
echo
echo -e " 7) ${green}>${reset} Show Saved Profiles"
echo -e " 8) ${red}v${reset} Delete a Saved Profile"
echo
echo -e " 9) ${cyan}v${reset} Download Nord VPN Connection Files"
echo
echo -e " M) ${yellow}^${reset} MAIN MENU"
echo -e " X) ${yellow}<${reset} EXIT"
echo
read -p " ENTER SELECTION: " SELECTION
# If they hit ENTER we exit
if [ -z "$SELECTION" ]; then
clear
exit 1
fi
# If they hit "m" we show the home page
if [ "$SELECTION" == 'm' ] || [ "$SELECTION" == 'M' ]; then
clear
_home_menu
exit 1
fi
# If they hit "x" we exit
if [ "$SELECTION" == 'x' ] || [ "$SELECTION" == 'X' ]; then
clear
exit 1
fi
# If they hit anything but a valid number we exit
if ! echo "$SELECTION" | egrep -q '^[1-9]+$'; then
clear
exit 1
fi
# Show the selected subpage
case "$SELECTION" in
1)
clear
_show_active_cons
;;
2)
clear
_show_interface_status
;;
3)
clear
_turn_wifi_on
;;
4)
clear
_turn_wifi_off
;;
5)
clear
_turn_network_on
;;
6)
clear
_turn_network_off
;;
7)
clear
_show_profiles
;;
8)
clear
_delete_profile
;;
9)
clear
_download_vpn_files
;;
*)
exit 1
;;
esac
}
# ------------------------------------------------------------------------------
function _show_active_cons() {
heading purple "ACTIVE CONNECTIONS"
nmcli con show --active
_util_submenu
}
# ------------------------------------------------------------------------------
function _show_interface_status() {
heading purple "NETWORK INTERFACE STATUS"
nmcli device status
_util_submenu
}
# ------------------------------------------------------------------------------
function _turn_wifi_on() {
heading purple "WIFI INTERFACE ON"
nmcli radio wifi on
_reset_geolocation
echo
echo -e " ${green}Wifi Interface has been turned on${reset}"
_util_submenu
}
# ------------------------------------------------------------------------------
function _turn_wifi_off() {
heading purple "WIFI INTERFACE OFF"
nmcli radio wifi off
_reset_geolocation
echo
echo -e " ${red}Wifi Interface has been turned off${reset}"
_util_submenu
}
# ------------------------------------------------------------------------------
function _turn_network_on() {
heading purple "NETWORK INTERFACE OFF"
nmcli networking on
_reset_geolocation
echo
echo -e " ${green}Network Interface has been turned on${reset}"
_util_submenu
}
# ------------------------------------------------------------------------------
function _turn_network_off() {
heading purple "NETWORK INTERFACE ON"
nmcli networking off
_reset_geolocation
echo
echo -e " ${red}Network Interface has been turned off${reset}"
_util_submenu
}
# ------------------------------------------------------------------------------
function _show_profiles() {
heading purple "SAVED PROFILES"
nmcli con show
_util_submenu
}
# ------------------------------------------------------------------------------
function _delete_profile() {
unset SELECTION
heading purple "DELETE PROFILE"
nmcli con show
echo
echo -e " ENTER NAME OF THE PROFILE TO DELETE, OR"
echo
echo -e " M) ${yellow}^${reset} MAIN MENU"
echo -e " X) ${yellow}<${reset} EXIT"
echo
read -p " ENTER SELECTION: " SELECTION
# If they hit "m" we show the home page
if [ "$SELECTION" == 'm' ] || [ "$SELECTION" == 'M' ]; then
clear
_home_menu
exit 1
fi
# If they hit "x" we exit
if [ "$SELECTION" == 'x' ] || [ "$SELECTION" == 'X' ]; then
clear
exit 1
fi
# If they only hit enter
if [ -z "$SELECTION" ]; then
clear
exit 1
fi
echo
nmcli con delete id "$SELECTION"
_reset_geolocation
_util_submenu
}
# ------------------------------------------------------------------------------
# Download the VPN connection files from Nord.com
function _download_vpn_files() {
if [ ! -d "${VPN_SERVERS}" ]; then
echo
echo -e " ${green}Creating destination folder${reset}"
mkdir ${VPN_SERVERS}
else
echo
echo -e " ${red}Deleting old VPN connection files${reset}"
rm ${VPN_SERVERS}/* >/dev/null 2>&1
sleep 3
fi
echo
echo -e " ${green}Downloading Nord VPN connection files${reset}"
# wget is a little easier to work with so we use it
if command -v wget &>/dev/null; then
wget -q "${NORD_CONNECTION_FILES}" -P /tmp
else
curl -o -s /tmp/ovpn.zip "${NORD_CONNECTION_FILES}"
fi
echo
echo -e " ${green}Extracting archive${reset}"
unzip -q /tmp/ovpn.zip -d /tmp/
echo
echo -e " ${green}Copying files to: ${VPN_SERVERS}/${reset}"
cp -a "/tmp/ovpn_tcp/." "${VPN_SERVERS}"
sleep 3
rm /tmp/ovpn.zip
rm -r /tmp/ovpn_tcp
rm -r /tmp/ovpn_udp
echo
echo -e " ${green}Done!${reset}"
_util_submenu
}
# ------------------------------------------------------------------------------
# This gets inserted at the bottom of inerior pages
function _submenu(){
unset SELECTION
echo
heading green "MENU"
echo -e " M) ${yellow}^${reset} MAIN MENU"
echo -e " X) ${yellow}<${reset} EXIT"
echo
read -p " ENTER SELECTION: " SELECTION
# If they hit ENTER we exit
if [ -z "$SELECTION" ]; then
clear
exit 1
fi
# If they hit "m" we show the home page
if [ "$SELECTION" == 'm' ] || [ "$SELECTION" == 'M' ]; then
clear
_home_menu
exit 1
fi
# Anything else triggers an exit
clear
exit 1
}
# ------------------------------------------------------------------------------
# This gets inserted at the bottom of inerior utilites pages
function _util_submenu(){
unset SELECTION
echo
heading green "MENU"
echo -e " M) ${yellow}^${reset} MAIN MENU"
echo -e " U) ${yellow}^${reset} UTILITIES"
echo -e " X) ${yellow}<${reset} EXIT"
echo
read -p " ENTER SELECTION: " SELECTION
# If they hit ENTER we exit
if [ -z "$SELECTION" ]; then
clear
exit 1
fi
# If they hit "m" we show the home page
if [ "$SELECTION" == 'm' ] || [ "$SELECTION" == 'M' ]; then
clear
_home_menu
exit 1
fi
# If they hit "u" we show the utilties page
if [ "$SELECTION" == 'u' ] || [ "$SELECTION" == 'U' ]; then
clear
_utilities