-
Notifications
You must be signed in to change notification settings - Fork 209
/
wireguard-manager.sh
executable file
·2054 lines (1975 loc) · 108 KB
/
wireguard-manager.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
#!/bin/bash
# WireGuard-Manager Installation Script
# Purpose: This script automates the installation of WireGuard-Manager, a comprehensive tool for managing WireGuard VPN configurations.
# Author: ComplexOrganizations
# Repository: https://github.com/complexorganizations/wireguard-manager
# Usage Instructions:
# 1. System Requirements: Ensure you have 'curl' installed on your system. This script is compatible with most Linux distributions.
# 2. Downloading the Script:
# - Use the following command to download the script:
# curl https://raw.githubusercontent.com/complexorganizations/wireguard-manager/main/wireguard-manager.sh --create-dirs -o /usr/local/bin/wireguard-manager.sh
# 3. Making the Script Executable:
# - Grant execution permissions to the script:
# chmod +x /usr/local/bin/wireguard-manager.sh
# 4. Running the Script:
# - Execute the script with root privileges:
# bash /usr/local/bin/wireguard-manager.sh
# 5. Follow the on-screen instructions to complete the installation of WireGuard-Manager.
# Advanced Usage:
# - The script supports various command-line arguments for custom installations. Refer to the repository's readme.md for more details.
# - For automated deployments, environment variables can be set before running this script.
# Troubleshooting:
# - If you encounter issues, ensure your system is up-to-date and retry the installation.
# - For specific errors, refer to the 'Troubleshooting' section in the repository's documentation.
# Contributing:
# - Contributions to the script are welcome. Please follow the contributing guidelines in the repository.
# Contact Information:
# - For support, feature requests, or bug reports, please open an issue on the GitHub repository.
# License: MIT License
# Note: This script is provided 'as is', without warranty of any kind. The user is responsible for understanding the operations and risks involved.
# Check if the script is running as root
function check_root() {
if [ "$(id -u)" -ne 0 ]; then
echo "Error: This script must be run as root."
exit 1
fi
}
# Call the function to check root privileges
check_root
# Function to gather current system details
function system-information() {
# This function fetches the ID, version, and major version of the current system
if [ -f /etc/os-release ]; then
# If /etc/os-release file is present, source it to obtain system details
# shellcheck source=/dev/null
source /etc/os-release
CURRENT_DISTRO=${ID} # CURRENT_DISTRO holds the system's ID
CURRENT_DISTRO_VERSION=${VERSION_ID} # CURRENT_DISTRO_VERSION holds the system's VERSION_ID
CURRENT_DISTRO_MAJOR_VERSION=$(echo "${CURRENT_DISTRO_VERSION}" | cut --delimiter="." --fields=1) # CURRENT_DISTRO_MAJOR_VERSION holds the major version of the system (e.g., "16" for Ubuntu 16.04)
fi
}
# Invoke the system-information function
system-information
# Define a function to check system requirements
function installing-system-requirements() {
# Check if the current Linux distribution is supported
if { [ "${CURRENT_DISTRO}" == "ubuntu" ] || [ "${CURRENT_DISTRO}" == "debian" ] || [ "${CURRENT_DISTRO}" == "raspbian" ] || [ "${CURRENT_DISTRO}" == "pop" ] || [ "${CURRENT_DISTRO}" == "kali" ] || [ "${CURRENT_DISTRO}" == "linuxmint" ] || [ "${CURRENT_DISTRO}" == "neon" ] || [ "${CURRENT_DISTRO}" == "fedora" ] || [ "${CURRENT_DISTRO}" == "centos" ] || [ "${CURRENT_DISTRO}" == "rhel" ] || [ "${CURRENT_DISTRO}" == "almalinux" ] || [ "${CURRENT_DISTRO}" == "rocky" ] || [ "${CURRENT_DISTRO}" == "arch" ] || [ "${CURRENT_DISTRO}" == "archarm" ] || [ "${CURRENT_DISTRO}" == "manjaro" ] || [ "${CURRENT_DISTRO}" == "alpine" ] || [ "${CURRENT_DISTRO}" == "freebsd" ] || [ "${CURRENT_DISTRO}" == "ol" ]; }; then
# Check if required packages are already installed
if { [ ! -x "$(command -v curl)" ] || [ ! -x "$(command -v cut)" ] || [ ! -x "$(command -v jq)" ] || [ ! -x "$(command -v ip)" ] || [ ! -x "$(command -v lsof)" ] || [ ! -x "$(command -v cron)" ] || [ ! -x "$(command -v awk)" ] || [ ! -x "$(command -v ps)" ] || [ ! -x "$(command -v grep)" ] || [ ! -x "$(command -v qrencode)" ] || [ ! -x "$(command -v sed)" ] || [ ! -x "$(command -v zip)" ] || [ ! -x "$(command -v unzip)" ] || [ ! -x "$(command -v openssl)" ] || [ ! -x "$(command -v nft)" ] || [ ! -x "$(command -v ifup)" ] || [ ! -x "$(command -v chattr)" ] || [ ! -x "$(command -v gpg)" ] || [ ! -x "$(command -v systemd-detect-virt)" ]; }; then
# Install required packages depending on the Linux distribution
if { [ "${CURRENT_DISTRO}" == "ubuntu" ] || [ "${CURRENT_DISTRO}" == "debian" ] || [ "${CURRENT_DISTRO}" == "raspbian" ] || [ "${CURRENT_DISTRO}" == "pop" ] || [ "${CURRENT_DISTRO}" == "kali" ] || [ "${CURRENT_DISTRO}" == "linuxmint" ] || [ "${CURRENT_DISTRO}" == "neon" ]; }; then
apt-get update
apt-get install curl coreutils jq iproute2 lsof cron gawk procps grep qrencode sed zip unzip openssl nftables ifupdown e2fsprogs gnupg systemd -y
elif { [ "${CURRENT_DISTRO}" == "fedora" ] || [ "${CURRENT_DISTRO}" == "centos" ] || [ "${CURRENT_DISTRO}" == "rhel" ] || [ "${CURRENT_DISTRO}" == "almalinux" ] || [ "${CURRENT_DISTRO}" == "rocky" ]; }; then
yum check-update
if [ "${CURRENT_DISTRO}" == "centos" ] && [ "${CURRENT_DISTRO_MAJOR_VERSION}" -ge 7 ]; then
yum install epel-release elrepo-release -y
fi
if [ "${CURRENT_DISTRO}" == "centos" ] && [ "${CURRENT_DISTRO_MAJOR_VERSION}" == 7 ]; then
yum install yum-plugin-elrepo -y
fi
yum install curl coreutils jq iproute lsof cronie gawk procps-ng grep qrencode sed zip unzip openssl nftables NetworkManager e2fsprogs gnupg systemd -y
elif { [ "${CURRENT_DISTRO}" == "arch" ] || [ "${CURRENT_DISTRO}" == "archarm" ] || [ "${CURRENT_DISTRO}" == "manjaro" ]; }; then
pacman -Sy --noconfirm archlinux-keyring
pacman -Su --noconfirm --needed curl coreutils jq iproute2 lsof cronie gawk procps-ng grep qrencode sed zip unzip openssl nftables ifupdown e2fsprogs gnupg systemd
elif [ "${CURRENT_DISTRO}" == "alpine" ]; then
apk update
apk add curl coreutils jq iproute2 lsof cronie gawk procps grep sed zip unzip openssl nftables e2fsprogs gnupg
# apk add curl coreutils jq iproute2 lsof cronie gawk procps grep qrencode sed zip unzip openssl nftables ifupdown e2fsprogs gnupg systemd
elif [ "${CURRENT_DISTRO}" == "freebsd" ]; then
pkg update
pkg install curl coreutils jq iproute2 lsof cronie gawk procps grep qrencode sed zip unzip openssl nftables ifupdown e2fsprogs gnupg systemd
elif [ "${CURRENT_DISTRO}" == "ol" ]; then
yum check-update
yum install curl coreutils jq iproute lsof cronie gawk procps-ng grep qrencode sed zip unzip openssl nftables NetworkManager e2fsprogs gnupg systemd -y
fi
fi
else
echo "Error: Your current distribution ${CURRENT_DISTRO} version ${CURRENT_DISTRO_VERSION} is not supported by this script. Please consider updating your distribution or using a supported one."
exit
fi
}
# Call the function to check for system requirements and install necessary packages if needed
installing-system-requirements
# Checking For Virtualization
function virt-check() {
# This code checks if the system is running in a supported virtualization.
# It returns the name of the virtualization if it is supported, or "none" if
# it is not supported. This code is used to check if the system is running in
# a virtual machine, and if so, if it is running in a supported virtualization.
# systemd-detect-virt is a utility that detects the type of virtualization
# that the system is running on. It returns a string that indicates the name
# of the virtualization, such as "kvm" or "vmware".
CURRENT_SYSTEM_VIRTUALIZATION=$(systemd-detect-virt)
# This case statement checks if the virtualization that the system is running
# on is supported. If it is not supported, the script will print an error
# message and exit.
case ${CURRENT_SYSTEM_VIRTUALIZATION} in
"kvm" | "none" | "qemu" | "lxc" | "microsoft" | "vmware" | "xen" | "amazon" | "docker") ;;
*)
echo "Error: the ${CURRENT_SYSTEM_VIRTUALIZATION} virtualization is currently not supported. Please stay tuned for future updates."
exit
;;
esac
}
# Call the virt-check function to check for supported virtualization.
virt-check
# The following function checks the kernel version.
function kernel-check() {
CURRENT_KERNEL_VERSION=$(uname --kernel-release | cut --delimiter="." --fields=1-2)
# Get the current kernel version and extract the major and minor version numbers.
CURRENT_KERNEL_MAJOR_VERSION=$(echo "${CURRENT_KERNEL_VERSION}" | cut --delimiter="." --fields=1)
# Extract the major version number from the current kernel version.
CURRENT_KERNEL_MINOR_VERSION=$(echo "${CURRENT_KERNEL_VERSION}" | cut --delimiter="." --fields=2)
# Extract the minor version number from the current kernel version.
ALLOWED_KERNEL_VERSION="3.1"
# Set the minimum allowed kernel version to 3.1.0.
ALLOWED_KERNEL_MAJOR_VERSION=$(echo ${ALLOWED_KERNEL_VERSION} | cut --delimiter="." --fields=1)
# Extract the major version number from the allowed kernel version.
ALLOWED_KERNEL_MINOR_VERSION=$(echo ${ALLOWED_KERNEL_VERSION} | cut --delimiter="." --fields=2)
# Extract the minor version number from the allowed kernel version.
if [ "${CURRENT_KERNEL_MAJOR_VERSION}" -lt "${ALLOWED_KERNEL_MAJOR_VERSION}" ]; then
# If the current major version is less than the allowed major version, show an error message and exit.
echo "Error: Your current kernel version ${CURRENT_KERNEL_VERSION} is not supported. Please update to version ${ALLOWED_KERNEL_VERSION} or later."
exit
fi
if [ "${CURRENT_KERNEL_MAJOR_VERSION}" == "${ALLOWED_KERNEL_MAJOR_VERSION}" ]; then
# If the current major version is equal to the allowed major version, check the minor version.
if [ "${CURRENT_KERNEL_MINOR_VERSION}" -lt "${ALLOWED_KERNEL_MINOR_VERSION}" ]; then
# If the current minor version is less than the allowed minor version, show an error message and exit.
echo "Error: Your current kernel version ${CURRENT_KERNEL_VERSION} is not supported. Please update to version ${ALLOWED_KERNEL_VERSION} or later."
exit
fi
fi
}
# Call the kernel-check function to verify the kernel version.
kernel-check
# The following function checks if the current init system is one of the allowed options.
function check-current-init-system() {
# This function checks if the current init system is systemd or sysvinit.
# If it is neither, the script exits.
CURRENT_INIT_SYSTEM=$(ps --no-headers -o comm 1)
# This line retrieves the current init system by checking the process name of PID 1.
case ${CURRENT_INIT_SYSTEM} in
# The case statement checks if the retrieved init system is one of the allowed options.
*"systemd"* | *"init"* | *"bash"* | *"sh"*)
# If the init system is systemd or sysvinit (init), continue with the script.
;;
*)
# If the init system is not one of the allowed options, display an error message and exit.
echo "Error: The ${CURRENT_INIT_SYSTEM} initialization system is currently not supported. Please stay tuned for future updates."
exit
;;
esac
}
# The check-current-init-system function is being called.
check-current-init-system
# Calls the check-current-init-system function.
# The following function checks if there's enough disk space to proceed with the installation.
function check-disk-space() {
# This function checks if there is more than 1 GB of free space on the drive.
FREE_SPACE_ON_DRIVE_IN_MB=$(df -m / | tr --squeeze-repeats " " | tail -n1 | cut --delimiter=" " --fields=4)
# This line calculates the available free space on the root partition in MB.
if [ "${FREE_SPACE_ON_DRIVE_IN_MB}" -le 1024 ]; then
# If the available free space is less than or equal to 1024 MB (1 GB), display an error message and exit.
echo "Error: You need more than 1 GB of free space to install everything. Please free up some space and try again."
exit
fi
}
# The check-disk-space function is being called.
check-disk-space
# Calls the check-disk-space function.
# Global variables
# Assigns the path of the current script to a variable
CURRENT_FILE_PATH=$(realpath "${0}")
# Assigns the WireGuard website URL to a variable
WIREGUARD_WEBSITE_URL="https://www.wireguard.com"
# Assigns a path for WireGuard
WIREGUARD_PATH="/etc/wireguard"
# Assigns a path for WireGuard clients
WIREGUARD_CLIENT_PATH="${WIREGUARD_PATH}/clients"
# Assigns a public network interface name for WireGuard
WIREGUARD_PUB_NIC="wg0"
# Assigns a path for the WireGuard configuration file
WIREGUARD_CONFIG="${WIREGUARD_PATH}/${WIREGUARD_PUB_NIC}.conf"
# Assigns a path for the WireGuard additional peer configuration file
WIREGUARD_ADD_PEER_CONFIG="${WIREGUARD_PATH}/${WIREGUARD_PUB_NIC}-add-peer.conf"
# Assigns a path for system backups
SYSTEM_BACKUP_PATH="/var/backups"
# Assigns a path for the WireGuard configuration backup file
WIREGUARD_CONFIG_BACKUP="${SYSTEM_BACKUP_PATH}/wireguard-manager.zip"
# Assigns a path for the WireGuard backup password file
WIREGUARD_BACKUP_PASSWORD_PATH="${HOME}/.wireguard-manager"
# Assigns a path for the DNS resolver configuration file
RESOLV_CONFIG="/etc/resolv.conf"
# Assigns a path for the old DNS resolver configuration file
RESOLV_CONFIG_OLD="${RESOLV_CONFIG}.old"
# Assigns a path for Unbound DNS resolver
UNBOUND_ROOT="/etc/unbound"
# Assigns a path for the WireGuard Manager script
UNBOUND_MANAGER="${UNBOUND_ROOT}/wireguard-manager"
# Assigns a path for the Unbound configuration file
UNBOUND_CONFIG="${UNBOUND_ROOT}/unbound.conf"
# Assigns a path for the Unbound root hints file
UNBOUND_ROOT_HINTS="${UNBOUND_ROOT}/root.hints"
# Assigns a path for the Unbound anchor file
UNBOUND_ANCHOR="/var/lib/unbound/root.key"
if { [ "${CURRENT_DISTRO}" == "arch" ] || [ "${CURRENT_DISTRO}" == "archarm" ] || [ "${CURRENT_DISTRO}" == "manjaro" ]; }; then
UNBOUND_ANCHOR="${UNBOUND_ROOT}/root.key"
fi
# Assigns a path for the Unbound configuration directory
UNBOUND_CONFIG_DIRECTORY="${UNBOUND_ROOT}/unbound.conf.d"
# Assigns a path for the Unbound hosts configuration file
UNBOUND_CONFIG_HOST="${UNBOUND_CONFIG_DIRECTORY}/hosts.conf"
case $(shuf --input-range=1-5 --head-count=1) in
1)
UNBOUND_ROOT_SERVER_CONFIG_URL="https://raw.githubusercontent.com/complexorganizations/wireguard-manager/main/assets/named.cache"
;;
2)
UNBOUND_ROOT_SERVER_CONFIG_URL="https://cdn.statically.io/gh/complexorganizations/wireguard-manager/main/assets/named.cache"
;;
3)
UNBOUND_ROOT_SERVER_CONFIG_URL="https://cdn.jsdelivr.net/gh/complexorganizations/wireguard-manager/assets/named.cache"
;;
4)
UNBOUND_ROOT_SERVER_CONFIG_URL="https://www.internic.net/domain/named.cache"
;;
5)
UNBOUND_ROOT_SERVER_CONFIG_URL="https://gitlab.com/complex-organizations/wireguard-manager/-/raw/main/assets/named.cache"
;;
esac
case $(shuf --input-range=1-5 --head-count=1) in
1)
UNBOUND_CONFIG_HOST_URL="https://raw.githubusercontent.com/complexorganizations/content-blocker/main/assets/hosts"
;;
2)
UNBOUND_CONFIG_HOST_URL="https://cdn.statically.io/gh/complexorganizations/content-blocker/main/assets/hosts"
;;
3)
UNBOUND_CONFIG_HOST_URL="https://cdn.jsdelivr.net/gh/complexorganizations/content-blocker/assets/hosts"
;;
4)
UNBOUND_CONFIG_HOST_URL="https://combinatronics.io/complexorganizations/content-blocker/main/assets/hosts"
;;
5)
UNBOUND_CONFIG_HOST_URL="https://gitlab.com/complex-organizations/wireguard-manager/-/raw/main/assets/hosts"
;;
esac
case $(shuf --input-range=1-5 --head-count=1) in
1)
WIREGUARD_MANAGER_UPDATE="https://raw.githubusercontent.com/complexorganizations/wireguard-manager/main/wireguard-manager.sh"
;;
2)
WIREGUARD_MANAGER_UPDATE="https://cdn.statically.io/gh/complexorganizations/wireguard-manager/main/wireguard-manager.sh"
;;
3)
WIREGUARD_MANAGER_UPDATE="https://cdn.jsdelivr.net/gh/complexorganizations/wireguard-manager/wireguard-manager.sh"
;;
4)
WIREGUARD_MANAGER_UPDATE="https://combinatronics.io/complexorganizations/wireguard-manager/main/wireguard-manager.sh"
;;
5)
WIREGUARD_MANAGER_UPDATE="https://gitlab.com/complex-organizations/wireguard-manager/-/raw/main/wireguard-manager.sh"
;;
esac
# Check if the CURRENT_DISTRO variable matches any of the following distros:
# fedora, centos, rhel, almalinux, or rocky
if { [ "${CURRENT_DISTRO}" == "fedora" ] || [ "${CURRENT_DISTRO}" == "centos" ] || [ "${CURRENT_DISTRO}" == "rhel" ] || [ "${CURRENT_DISTRO}" == "almalinux" ] || [ "${CURRENT_DISTRO}" == "rocky" ]; }; then
# If the condition is true, set the SYSTEM_CRON_NAME variable to "crond"
SYSTEM_CRON_NAME="crond"
# If the CURRENT_DISTRO variable matches any of the following distros:
# arch, archarm, or manjaro
elif { [ "${CURRENT_DISTRO}" == "arch" ] || [ "${CURRENT_DISTRO}" == "archarm" ] || [ "${CURRENT_DISTRO}" == "manjaro" ]; }; then
# If the condition is true, set the SYSTEM_CRON_NAME variable to "cronie"
SYSTEM_CRON_NAME="cronie"
else
# If none of the above conditions are met, set the SYSTEM_CRON_NAME variable to "cron"
SYSTEM_CRON_NAME="cron"
fi
# This is a Bash function named "get-network-information" that retrieves network information.
function get-network-information() {
# This variable will store the IPv4 address of the default network interface by querying the "ipengine" API using "curl" command and extracting it using "jq" command.
DEFAULT_INTERFACE_IPV4="$(curl --ipv4 --connect-timeout 5 --tlsv1.2 --silent 'https://checkip.amazonaws.com')"
# If the IPv4 address is empty, try getting it from another API.
if [ -z "${DEFAULT_INTERFACE_IPV4}" ]; then
DEFAULT_INTERFACE_IPV4="$(curl --ipv4 --connect-timeout 5 --tlsv1.3 --silent 'https://icanhazip.com')"
fi
# This variable will store the IPv6 address of the default network interface by querying the "ipengine" API using "curl" command and extracting it using "jq" command.
DEFAULT_INTERFACE_IPV6="$(curl --ipv6 --connect-timeout 5 --tlsv1.3 --silent 'https://ifconfig.co')"
# If the IPv6 address is empty, try getting it from another API.
if [ -z "${DEFAULT_INTERFACE_IPV6}" ]; then
DEFAULT_INTERFACE_IPV6="$(curl --ipv6 --connect-timeout 5 --tlsv1.3 --silent 'https://icanhazip.com')"
fi
}
# Usage Guide of the application
function usage-guide() {
echo "Usage: ./$(basename "${0}") <command>"
echo " --install Installs the WireGuard interface on your system"
echo " --start Starts the WireGuard interface if it's not already running"
echo " --stop Stops the WireGuard interface if it's currently running"
echo " --restart Restarts the WireGuard interface"
echo " --list Lists all the peers currently connected to the WireGuard interface"
echo " --add Adds a new peer to the WireGuard interface"
echo " --remove Removes a specified peer from the WireGuard interface"
echo " --reinstall Reinstalls the WireGuard interface, keeping the current configuration"
echo " --uninstall Uninstalls the WireGuard interface from your system"
echo " --update Updates the WireGuard Manager to the latest version"
echo " --ddns Updates the IP address of the WireGuard interface using Dynamic DNS"
echo " --backup Creates a backup of your current WireGuard configuration"
echo " --restore Restores the WireGuard configuration from a previous backup"
echo " --purge Removes all peers from the WireGuard interface"
echo " --help Displays this usage guide"
}
# Define a function that takes command line arguments as input
function usage() {
# Check if there are any command line arguments left
while [ $# -ne 0 ]; do
# Use a switch-case statement to check the value of the first argument
case ${1} in
--install) # If it's "--install", set the variable HEADLESS_INSTALL to "true"
shift
HEADLESS_INSTALL=${HEADLESS_INSTALL=true}
;;
--start) # If it's "--start", set the variable WIREGUARD_OPTIONS to 2
shift
WIREGUARD_OPTIONS=${WIREGUARD_OPTIONS=2}
;;
--stop) # If it's "--stop", set the variable WIREGUARD_OPTIONS to 3
shift
WIREGUARD_OPTIONS=${WIREGUARD_OPTIONS=3}
;;
--restart) # If it's "--restart", set the variable WIREGUARD_OPTIONS to 4
shift
WIREGUARD_OPTIONS=${WIREGUARD_OPTIONS=4}
;;
--list) # If it's "--list", set the variable WIREGUARD_OPTIONS to 1
shift
WIREGUARD_OPTIONS=${WIREGUARD_OPTIONS=1}
;;
--add) # If it's "--add", set the variable WIREGUARD_OPTIONS to 5
shift
WIREGUARD_OPTIONS=${WIREGUARD_OPTIONS=5}
;;
--remove) # If it's "--remove", set the variable WIREGUARD_OPTIONS to 6
shift
WIREGUARD_OPTIONS=${WIREGUARD_OPTIONS=6}
;;
--reinstall) # If it's "--reinstall", set the variable WIREGUARD_OPTIONS to 7
shift
WIREGUARD_OPTIONS=${WIREGUARD_OPTIONS=7}
;;
--uninstall) # If it's "--uninstall", set the variable WIREGUARD_OPTIONS to 8
shift
WIREGUARD_OPTIONS=${WIREGUARD_OPTIONS=8}
;;
--update) # If it's "--update", set the variable WIREGUARD_OPTIONS to 9
shift
WIREGUARD_OPTIONS=${WIREGUARD_OPTIONS=9}
;;
--backup) # If it's "--backup", set the variable WIREGUARD_OPTIONS to 10
shift
WIREGUARD_OPTIONS=${WIREGUARD_OPTIONS=10}
;;
--restore) # If it's "--restore", set the variable WIREGUARD_OPTIONS to 11
shift
WIREGUARD_OPTIONS=${WIREGUARD_OPTIONS=11}
;;
--ddns) # If it's "--ddns", set the variable WIREGUARD_OPTIONS to 12
shift
WIREGUARD_OPTIONS=${WIREGUARD_OPTIONS=12}
;;
--purge) # If it's "--purge", set the variable WIREGUARD_OPTIONS to 14
shift
WIREGUARD_OPTIONS=${WIREGUARD_OPTIONS=14}
;;
--help) # If it's "--help", call the function usage-guide
shift
usage-guide
;;
*) # If it's anything else, print an error message and call the function usage-guide, then exit
echo "Invalid argument: ${1}"
usage-guide
exit
;;
esac
done
}
# Call the function usage with all the command line arguments
usage "$@"
# The function defines default values for configuration variables when installing WireGuard in headless mode.
# These variables include private subnet settings, server host settings, NAT choice, MTU choice, client allowed IP settings, automatic updates, automatic backup, DNS provider settings, content blocker settings, client name, and automatic config remover.
function headless-install() {
# If headless installation is specified, set default values for configuration variables.
if [ "${HEADLESS_INSTALL}" == true ]; then
PRIVATE_SUBNET_V4_SETTINGS=${PRIVATE_SUBNET_V4_SETTINGS=1} # Default to 1 if not specified
PRIVATE_SUBNET_V6_SETTINGS=${PRIVATE_SUBNET_V6_SETTINGS=1} # Default to 1 if not specified
SERVER_HOST_V4_SETTINGS=${SERVER_HOST_V4_SETTINGS=1} # Default to 1 if not specified
SERVER_HOST_V6_SETTINGS=${SERVER_HOST_V6_SETTINGS=1} # Default to 1 if not specified
SERVER_PUB_NIC_SETTINGS=${SERVER_PUB_NIC_SETTINGS=1} # Default to 1 if not specified
SERVER_PORT_SETTINGS=${SERVER_PORT_SETTINGS=1} # Default to 1 if not specified
NAT_CHOICE_SETTINGS=${NAT_CHOICE_SETTINGS=1} # Default to 1 if not specified
MTU_CHOICE_SETTINGS=${MTU_CHOICE_SETTINGS=1} # Default to 1 if not specified
SERVER_HOST_SETTINGS=${SERVER_HOST_SETTINGS=1} # Default to 1 if not specified
CLIENT_ALLOWED_IP_SETTINGS=${CLIENT_ALLOWED_IP_SETTINGS=1} # Default to 1 if not specified
AUTOMATIC_UPDATES_SETTINGS=${AUTOMATIC_UPDATES_SETTINGS=1} # Default to 1 if not specified
AUTOMATIC_BACKUP_SETTINGS=${AUTOMATIC_BACKUP_SETTINGS=1} # Default to 1 if not specified
DNS_PROVIDER_SETTINGS=${DNS_PROVIDER_SETTINGS=1} # Default to 1 if not specified
CONTENT_BLOCKER_SETTINGS=${CONTENT_BLOCKER_SETTINGS=1} # Default to 1 if not specified
CLIENT_NAME=${CLIENT_NAME=$(openssl rand -hex 25)} # Generate a random client name if not specified
AUTOMATIC_CONFIG_REMOVER=${AUTOMATIC_CONFIG_REMOVER=1} # Default to 1 if not specified
fi
}
# Call the headless-install function to set default values for configuration variables in headless mode.
headless-install
# Set up the wireguard, if config it isn't already there.
if [ ! -f "${WIREGUARD_CONFIG}" ]; then
# Define a function to set a custom IPv4 subnet
function set-ipv4-subnet() {
# Prompt the user for the desired IPv4 subnet
echo "Please specify the IPv4 subnet you want to use for the WireGuard interface. This should be a private subnet that is not in use elsewhere on your network. For example, you might choose '10.0.0.0/24' if it's not already in use."
echo " 1) 10.0.0.0/8 (Recommended)"
echo " 2) Custom (Advanced)"
# Keep prompting the user until they enter a valid subnet choice
until [[ "${PRIVATE_SUBNET_V4_SETTINGS}" =~ ^[1-2]$ ]]; do
read -rp "Subnet Choice [1-2]:" -e -i 1 PRIVATE_SUBNET_V4_SETTINGS
done
# Based on the user's choice, set the private IPv4 subnet
case ${PRIVATE_SUBNET_V4_SETTINGS} in
1)
PRIVATE_SUBNET_V4="10.0.0.0/8" # Set a default IPv4 subnet
;;
2)
read -rp "Custom IPv4 Subnet:" PRIVATE_SUBNET_V4 # Prompt user for custom subnet
if [ -z "${PRIVATE_SUBNET_V4}" ]; then # If the user did not enter a subnet, set default
PRIVATE_SUBNET_V4="10.0.0.0/8"
fi
;;
esac
}
# Call the function to set the custom IPv4 subnet
set-ipv4-subnet
# Define a function to set a custom IPv6 subnet
function set-ipv6-subnet() {
# Ask the user which IPv6 subnet they want to use
echo "Please specify the IPv6 subnet you want to use for the WireGuard interface. This should be a private subnet that is not in use elsewhere on your network. For example, you might choose 'fd00::/64' if it's not already in use."
echo " 1) fd00:00:00::0/8 (Recommended)"
echo " 2) Custom (Advanced)"
# Use a loop to ensure the user inputs a valid option
until [[ "${PRIVATE_SUBNET_V6_SETTINGS}" =~ ^[1-2]$ ]]; do
read -rp "Please choose the IPv6 subnet for your WireGuard interface [Option 1-2]: " -e -i 1 PRIVATE_SUBNET_V6_SETTINGS
done
# Use a case statement to set the IPv6 subnet based on the user's choice
case ${PRIVATE_SUBNET_V6_SETTINGS} in
1)
# Use the recommended IPv6 subnet if the user chooses option 1
PRIVATE_SUBNET_V6="fd00:00:00::0/8"
;;
2)
# Ask the user for a custom IPv6 subnet if they choose option 2
read -rp "Please enter a custom IPv6 subnet for your WireGuard interface: " PRIVATE_SUBNET_V6
# If the user does not input a subnet, use the recommended one
if [ -z "${PRIVATE_SUBNET_V6}" ]; then
PRIVATE_SUBNET_V6="fd00:00:00::0/8"
fi
;;
esac
}
# Call the set-ipv6-subnet function to set the custom IPv6 subnet
set-ipv6-subnet
# Define the private subnet mask for the IPv4 network used by the WireGuard interface
PRIVATE_SUBNET_MASK_V4=$(echo "${PRIVATE_SUBNET_V4}" | cut --delimiter="/" --fields=2) # Get the subnet mask of IPv4
# Define the IPv4 gateway for the WireGuard interface
GATEWAY_ADDRESS_V4=$(echo "${PRIVATE_SUBNET_V4}" | cut --delimiter="." --fields=1-3).1 # Get the gateway address of IPv4
# Define the private subnet mask for the IPv6 network used by the WireGuard interface
PRIVATE_SUBNET_MASK_V6=$(echo "${PRIVATE_SUBNET_V6}" | cut --delimiter="/" --fields=2) # Get the subnet mask of IPv6
# Define the IPv6 gateway for the WireGuard interface
GATEWAY_ADDRESS_V6=$(echo "${PRIVATE_SUBNET_V6}" | cut --delimiter=":" --fields=1-3)::1 # Get the gateway address of IPv6
# Retrieve the networking configuration details
get-network-information
# Call a function to get the networking data
# Define a function to retrieve the IPv4 address of the WireGuard interface
function test-connectivity-v4() {
# Prompt the user to choose the method for detecting the IPv4 address
echo "How would you like to detect IPv4?"
echo " 1) Curl (Recommended)"
echo " 2) Custom (Advanced)"
# Loop until the user provides a valid input
until [[ "${SERVER_HOST_V4_SETTINGS}" =~ ^[1-2]$ ]]; do
read -rp "IPv4 Choice [1-2]:" -e -i 1 SERVER_HOST_V4_SETTINGS
done
# Choose the method for detecting the IPv4 address based on the user's input
case ${SERVER_HOST_V4_SETTINGS} in
1)
SERVER_HOST_V4=${DEFAULT_INTERFACE_IPV4} # Use the default IPv4 address
;;
2)
# Prompt the user to enter a custom IPv4 address
read -rp "Custom IPv4:" SERVER_HOST_V4
# If the user doesn't provide an input, use the default IPv4 address
if [ -z "${SERVER_HOST_V4}" ]; then
SERVER_HOST_V4=${DEFAULT_INTERFACE_IPV4}
fi
;;
esac
}
# Call the function to retrieve the IPv4 address
test-connectivity-v4
# Invoke the function to get the IPv4 address
# Define a function to retrieve the IPv6 address of the WireGuard interface
function test-connectivity-v6() {
# Prompt the user to choose the method for detecting the IPv6 address
echo "How would you like to detect IPv6?"
echo " 1) Curl (Recommended)"
echo " 2) Custom (Advanced)"
# Loop until the user provides a valid input
until [[ "${SERVER_HOST_V6_SETTINGS}" =~ ^[1-2]$ ]]; do
read -rp "IPv6 Choice [1-2]:" -e -i 1 SERVER_HOST_V6_SETTINGS
done
# Choose the method for detecting the IPv6 address based on the user's input
case ${SERVER_HOST_V6_SETTINGS} in
1)
SERVER_HOST_V6=${DEFAULT_INTERFACE_IPV6} # Use the default IPv6 address
;;
2)
# Prompt the user to enter a custom IPv6 address
read -rp "Custom IPv6:" SERVER_HOST_V6
# If the user doesn't provide an input, use the default IPv6 address
if [ -z "${SERVER_HOST_V6}" ]; then
SERVER_HOST_V6=${DEFAULT_INTERFACE_IPV6}
fi
;;
esac
}
# Call the function to retrieve the IPv6 address
test-connectivity-v6
# Define a function to identify the public Network Interface Card (NIC).
function server-pub-nic() {
# Prompt the user to select the method for identifying the NIC.
echo "How would you like to identify the Network Interface Card (NIC)?"
echo " 1) IP Route (Recommended)"
echo " 2) Custom Input (Advanced)"
# Loop until the user provides a valid input (either 1 or 2).
until [[ "${SERVER_PUB_NIC_SETTINGS}" =~ ^[1-2]$ ]]; do
read -rp "NIC Choice [1-2]:" -e -i 1 SERVER_PUB_NIC_SETTINGS
done
# Execute a case statement based on the user's choice.
case ${SERVER_PUB_NIC_SETTINGS} in
1)
# Use the IP route command to automatically identify the NIC.
SERVER_PUB_NIC="$(ip route | grep default | head --lines=1 | cut --delimiter=" " --fields=5)"
# If no NIC is found, exit the script with an error message.
if [ -z "${SERVER_PUB_NIC}" ]; then
echo "Error: Unable to identify your server's public network interface."
exit
fi
;;
2)
# Prompt the user to manually input the NIC.
read -rp "Custom NIC:" SERVER_PUB_NIC
# If the user doesn't provide an input, use the IP route command to identify the NIC.
if [ -z "${SERVER_PUB_NIC}" ]; then
SERVER_PUB_NIC="$(ip route | grep default | head --lines=1 | cut --delimiter=" " --fields=5)"
fi
;;
esac
}
# Call the function to identify the public NIC.
server-pub-nic
# Define a function to configure the WireGuard server's listening port
function set-port() {
# Prompt the user to specify the port for the WireGuard server
echo "What port do you want WireGuard server to listen to?"
# Provide the user with options for setting the port
echo " 1) 51820 (Recommended)"
echo " 2) Custom (Advanced)"
# Continue prompting the user until a valid option (1 or 2) is selected
until [[ "${SERVER_PORT_SETTINGS}" =~ ^[1-2]$ ]]; do
# Ask the user for their port choice, with 1 as the default option
read -rp "Port Choice [1-2]:" -e -i 1 SERVER_PORT_SETTINGS
done
# Set the SERVER_PORT variable based on the user's choice
case ${SERVER_PORT_SETTINGS} in
1)
SERVER_PORT="51820"
# If the chosen port is already in use, display an error message and exit the script
if [ "$(lsof -i UDP:"${SERVER_PORT}")" ]; then
echo "Error: Please use a different port because ${SERVER_PORT} is already in use."
exit
fi
;;
2)
# Continue prompting the user until a valid custom port number (between 1 and 65535) is entered
until [[ "${SERVER_PORT}" =~ ^[0-9]+$ ]] && [ "${SERVER_PORT}" -ge 1 ] && [ "${SERVER_PORT}" -le 65535 ]; do
read -rp "Custom port [1-65535]:" SERVER_PORT
done
# If no custom port is entered, set the SERVER_PORT variable to the default of 51820
if [ -z "${SERVER_PORT}" ]; then
SERVER_PORT="51820"
fi
# If the chosen port is already in use, display an error message and exit the script
if [ "$(lsof -i UDP:"${SERVER_PORT}")" ]; then
echo "Error: The port ${SERVER_PORT} is already used by a different application, please use a different port."
exit
fi
;;
esac
}
# Invoke the set-port function to configure the WireGuard server's listening port
set-port
# Define a function to set the NAT keepalive interval.
function nat-keepalive() {
# Prompt the user to specify the NAT keepalive interval.
echo "What do you want your NAT keepalive interval to be?"
# Provide the user with options for setting the interval.
echo " 1) 25 seconds (Default)"
echo " 2) Custom (Advanced)"
# Continue prompting the user until a valid option (1 or 2) is selected.
until [[ "${NAT_CHOICE_SETTINGS}" =~ ^[1-2]$ ]]; do
# Ask the user for their interval choice, with 1 as the default option.
read -rp "Keepalive Choice [1-2]:" -e -i 1 NAT_CHOICE_SETTINGS
done
# Set the NAT_CHOICE variable based on the user's choice.
case ${NAT_CHOICE_SETTINGS} in
1)
# If the user chose the default option, set the NAT_CHOICE to 25 seconds.
NAT_CHOICE="25"
;;
2)
# If the user chose the custom option, prompt them to enter a custom interval.
until [[ "${NAT_CHOICE}" =~ ^[0-9]+$ ]] && [ "${NAT_CHOICE}" -ge 1 ] && [ "${NAT_CHOICE}" -le 65535 ]; do
read -rp "Custom NAT [1-65535]:" NAT_CHOICE
done
# If no custom interval is entered, set the NAT_CHOICE variable to the default of 25 seconds.
if [ -z "${NAT_CHOICE}" ]; then
NAT_CHOICE="25"
fi
;;
esac
}
# Invoke the nat-keepalive function to set the NAT keepalive interval.
nat-keepalive
# Define a function to configure the Maximum Transmission Unit (MTU) settings.
function mtu-set() {
# Ask the user to specify the MTU settings.
echo "What MTU do you want to use?"
# Provide the user with options for setting the MTU.
echo " 1) 1420 for Interface, 1280 for Peer (Recommended)"
echo " 2) Custom (Advanced)"
# Continue prompting the user until a valid option (1 or 2) is selected.
until [[ "${MTU_CHOICE_SETTINGS}" =~ ^[1-2]$ ]]; do
# Ask the user for their MTU choice, with 1 as the default option.
read -rp "MTU Choice [1-2]:" -e -i 1 MTU_CHOICE_SETTINGS
done
# Set the MTU variables based on the user's choice.
case ${MTU_CHOICE_SETTINGS} in
1)
# If the user chose the default option, set the Interface MTU to 1420 and Peer MTU to 1280.
INTERFACE_MTU_CHOICE="1420"
PEER_MTU_CHOICE="1280"
;;
2)
# If the user chose the custom option, prompt them to enter a custom MTU for Interface and Peer.
until [[ "${INTERFACE_MTU_CHOICE}" =~ ^[0-9]+$ ]] && [ "${INTERFACE_MTU_CHOICE}" -ge 1 ] && [ "${INTERFACE_MTU_CHOICE}" -le 65535 ]; do
read -rp "Custom Interface MTU [1-65535]:" INTERFACE_MTU_CHOICE
done
# If no custom Interface MTU is entered, set the INTERFACE_MTU_CHOICE variable to the default of 1420.
if [ -z "${INTERFACE_MTU_CHOICE}" ]; then
INTERFACE_MTU_CHOICE="1420"
fi
until [[ "${PEER_MTU_CHOICE}" =~ ^[0-9]+$ ]] && [ "${PEER_MTU_CHOICE}" -ge 1 ] && [ "${PEER_MTU_CHOICE}" -le 65535 ]; do
read -rp "Custom Peer MTU [1-65535]:" PEER_MTU_CHOICE
done
# If no custom Peer MTU is entered, set the PEER_MTU_CHOICE variable to the default of 1280.
if [ -z "${PEER_MTU_CHOICE}" ]; then
PEER_MTU_CHOICE="1280"
fi
;;
esac
}
# Invoke the mtu-set function to configure the MTU settings.
mtu-set
# Define a function to select the IP version for the WireGuard server.
function ipvx-select() {
# Ask the user to specify the IP version to use for connecting to the WireGuard server.
echo "Which IP version do you want to use for the WireGuard server?"
# Provide the user with options for setting the IP version.
echo " 1) IPv4 (Recommended)"
echo " 2) IPv6"
# Continue prompting the user until a valid option (1 or 2) is selected.
until [[ "${SERVER_HOST_SETTINGS}" =~ ^[1-2]$ ]]; do
# Ask the user for their IP version choice, with 1 as the default option.
read -rp "IP Version Choice [1-2]:" -e -i 1 SERVER_HOST_SETTINGS
done
# Set the SERVER_HOST variable based on the user's choice.
case ${SERVER_HOST_SETTINGS} in
1)
# If the user chose IPv4 and a default IPv4 interface is available, use it.
if [ -n "${DEFAULT_INTERFACE_IPV4}" ]; then
SERVER_HOST="${DEFAULT_INTERFACE_IPV4}"
else
# If no default IPv4 interface is available, use the default IPv6 interface.
SERVER_HOST="[${DEFAULT_INTERFACE_IPV6}]"
fi
;;
2)
# If the user chose IPv6 and a default IPv6 interface is available, use it.
if [ -n "${DEFAULT_INTERFACE_IPV6}" ]; then
SERVER_HOST="[${DEFAULT_INTERFACE_IPV6}]"
else
# If no default IPv6 interface is available, use the default IPv4 interface.
SERVER_HOST="${DEFAULT_INTERFACE_IPV4}"
fi
;;
esac
}
# Invoke the ipvx-select function to select the IP version for the WireGuard server.
ipvx-select
# Define a function to configure the type of traffic the client is allowed to forward through WireGuard.
function client-allowed-ip() {
# Ask the user to specify the type of traffic to be forwarded.
echo "What type of traffic do you want the client to forward through WireGuard?"
# Provide the user with options for setting the traffic type.
echo " 1) All Traffic (Recommended)"
echo " 2) Custom Traffic (Advanced)"
# Continue prompting the user until a valid option (1 or 2) is selected.
until [[ "${CLIENT_ALLOWED_IP_SETTINGS}" =~ ^[1-2]$ ]]; do
# Ask the user for their traffic type choice, with 1 as the default option.
read -rp "Traffic Type Choice [1-2]:" -e -i 1 CLIENT_ALLOWED_IP_SETTINGS
done
# Set the CLIENT_ALLOWED_IP variable based on the user's choice.
case ${CLIENT_ALLOWED_IP_SETTINGS} in
1)
# If the user chose the default option, set the CLIENT_ALLOWED_IP to allow all traffic.
CLIENT_ALLOWED_IP="0.0.0.0/0,::/0"
;;
2)
# If the user chose the custom option, prompt them to enter a custom IP range.
read -rp "Custom IP Range:" CLIENT_ALLOWED_IP
# If no custom IP range is entered, set the CLIENT_ALLOWED_IP variable to allow all traffic.
if [ -z "${CLIENT_ALLOWED_IP}" ]; then
CLIENT_ALLOWED_IP="0.0.0.0/0,::/0"
fi
;;
esac
}
# Invoke the client-allowed-ip function to configure the type of traffic the client is allowed to forward.
client-allowed-ip
# Function to configure automatic updates
function enable-automatic-updates() {
# Prompt the user to decide if they want to enable automatic updates
echo "Would you like to setup real-time updates?"
# Option 1: Enable automatic updates
echo " 1) Yes (Recommended)"
# Option 2: Disable automatic updates
echo " 2) No (Advanced)"
# Loop until a valid choice (1 or 2) is made
until [[ "${AUTOMATIC_UPDATES_SETTINGS}" =~ ^[1-2]$ ]]; do
# Read user input for automatic updates setting
read -rp "Automatic Updates [1-2]:" -e -i 1 AUTOMATIC_UPDATES_SETTINGS
done
# Evaluate user choice for automatic updates
case ${AUTOMATIC_UPDATES_SETTINGS} in
1)
# If user chose to enable automatic updates, set up a cron job
crontab -l | {
cat
# Add a cron job to run the script with --update option every day at midnight
echo "0 0 * * * ${CURRENT_FILE_PATH} --update"
} | crontab -
# Check the init system in use
if [[ "${CURRENT_INIT_SYSTEM}" == *"systemd"* ]]; then
# If systemd is in use, enable and start the cron service
systemctl enable --now ${SYSTEM_CRON_NAME}
elif [[ "${CURRENT_INIT_SYSTEM}" == *"init"* ]]; then
# If initd is in use, start the cron service
service ${SYSTEM_CRON_NAME} start
fi
;;
2)
# If user chose to disable automatic updates, display a confirmation message
echo "Real-time Updates Disabled"
;;
esac
}
# Invoke the function to configure automatic updates
enable-automatic-updates
# Function to configure automatic backup
function enable-automatic-backup() {
# Prompt the user to decide if they want to enable automatic backup
echo "Would you like to setup real-time backup?"
# Option 1: Enable automatic backup
echo " 1) Yes (Recommended)"
# Option 2: Disable automatic backup
echo " 2) No (Advanced)"
# Loop until a valid choice (1 or 2) is made
until [[ "${AUTOMATIC_BACKUP_SETTINGS}" =~ ^[1-2]$ ]]; do
# Read user input for automatic backup setting
read -rp "Automatic Backup [1-2]:" -e -i 1 AUTOMATIC_BACKUP_SETTINGS
done
# Evaluate user choice for automatic backup
case ${AUTOMATIC_BACKUP_SETTINGS} in
1)
# If user chose to enable automatic backup, set up a cron job
crontab -l | {
cat
# Add a cron job to run the script with --backup option every day at midnight
echo "0 0 * * * ${CURRENT_FILE_PATH} --backup"
} | crontab -
# Check the init system in use
if [[ "${CURRENT_INIT_SYSTEM}" == *"systemd"* ]]; then
# If systemd is in use, enable and start the cron service
systemctl enable --now ${SYSTEM_CRON_NAME}
elif [[ "${CURRENT_INIT_SYSTEM}" == *"init"* ]]; then
# If initd is in use, start the cron service
service ${SYSTEM_CRON_NAME} start
fi
;;
2)
# If user chose to disable automatic backup, display a confirmation message
echo "Real-time Backup Disabled"
;;
esac
}
# Invoke the function to configure automatic backup
enable-automatic-backup
# Function to prompt the user for their preferred DNS provider.
function ask-install-dns() {
# Display the DNS provider options to the user.
echo "Which DNS provider would you like to use?"
echo " 1) Unbound (Recommended)"
echo " 2) Custom (Advanced)"
# Continue prompting until the user enters a valid choice (1 or 2).
until [[ "${DNS_PROVIDER_SETTINGS}" =~ ^[1-2]$ ]]; do
# Read the user's DNS provider choice and store it in DNS_PROVIDER_SETTINGS.
read -rp "DNS provider [1-2]:" -e -i 1 DNS_PROVIDER_SETTINGS
done
# Set variables based on the user's DNS provider choice.
case ${DNS_PROVIDER_SETTINGS} in
1)
# If the user chose Unbound, set INSTALL_UNBOUND to true.
INSTALL_UNBOUND=true
# Ask the user if they want to install a content-blocker.
echo "Do you want to prevent advertisements, tracking, malware, and phishing using the content-blocker?"
echo " 1) Yes (Recommended)"
echo " 2) No"
# Continue prompting until the user enters a valid choice (1 or 2).
until [[ "${CONTENT_BLOCKER_SETTINGS}" =~ ^[1-2]$ ]]; do
# Read the user's content blocker choice and store it in CONTENT_BLOCKER_SETTINGS.
read -rp "Content Blocker Choice [1-2]:" -e -i 1 CONTENT_BLOCKER_SETTINGS
done
# Set INSTALL_BLOCK_LIST based on the user's content blocker choice.
case ${CONTENT_BLOCKER_SETTINGS} in
1)
# If the user chose to install the content blocker, set INSTALL_BLOCK_LIST to true.
INSTALL_BLOCK_LIST=true
;;
2)
# If the user chose not to install the content blocker, set INSTALL_BLOCK_LIST to false.
INSTALL_BLOCK_LIST=false
;;
esac
;;
2)
# If the user chose to use a custom DNS provider, set CUSTOM_DNS to true.
CUSTOM_DNS=true
;;
esac
}
# Invoke the ask-install-dns function to begin the DNS provider selection process.
ask-install-dns
# Function to allow users to select a custom DNS provider.
function custom-dns() {
# If the custom DNS option is enabled, proceed with the DNS selection.
if [ "${CUSTOM_DNS}" == true ]; then
# Present the user with a list of DNS providers to choose from.
echo "Select the DNS provider you wish to use with your WireGuard connection:"
echo " 1) Cloudflare (Recommended)"
echo " 2) AdGuard"
echo " 3) NextDNS"
echo " 4) OpenDNS"
echo " 5) Google"
echo " 6) Verisign"
echo " 7) Quad9"
echo " 8) FDN"
echo " 9) Custom (Advanced)"
# If Pi-Hole is installed, add it as an option.
if [ -x "$(command -v pihole)" ]; then
echo " 10) Pi-Hole (Advanced)"
fi
# Prompt the user to make a selection from the list of DNS providers.
until [[ "${CLIENT_DNS_SETTINGS}" =~ ^[0-9]+$ ]] && [ "${CLIENT_DNS_SETTINGS}" -ge 1 ] && [ "${CLIENT_DNS_SETTINGS}" -le 10 ]; do
read -rp "DNS [1-10]:" -e -i 1 CLIENT_DNS_SETTINGS
done
# Based on the user's selection, set the DNS addresses.
case ${CLIENT_DNS_SETTINGS} in
1)
# Set DNS addresses for Cloudflare.
CLIENT_DNS="1.1.1.1,1.0.0.1,2606:4700:4700::1111,2606:4700:4700::1001"
;;
2)
# Set DNS addresses for AdGuard.
CLIENT_DNS="94.140.14.14,94.140.15.15,2a10:50c0::ad1:ff,2a10:50c0::ad2:ff"
;;
3)
# Set DNS addresses for NextDNS.
CLIENT_DNS="45.90.28.167,45.90.30.167,2a07:a8c0::12:cf53,2a07:a8c1::12:cf53"
;;
4)
# Set DNS addresses for OpenDNS.
CLIENT_DNS="208.67.222.222,208.67.220.220,2620:119:35::35,2620:119:53::53"
;;
5)
# Set DNS addresses for Google.
CLIENT_DNS="8.8.8.8,8.8.4.4,2001:4860:4860::8888,2001:4860:4860::8844"
;;
6)
# Set DNS addresses for Verisign.
CLIENT_DNS="64.6.64.6,64.6.65.6,2620:74:1b::1:1,2620:74:1c::2:2"
;;
7)
# Set DNS addresses for Quad9.
CLIENT_DNS="9.9.9.9,149.112.112.112,2620:fe::fe,2620:fe::9"
;;
8)
# Set DNS addresses for FDN.
CLIENT_DNS="80.67.169.40,80.67.169.12,2001:910:800::40,2001:910:800::12"
;;
9)
# Prompt the user to enter a custom DNS address.
read -rp "Custom DNS:" CLIENT_DNS
# If the user doesn't provide a custom DNS, default to Google's DNS.
if [ -z "${CLIENT_DNS}" ]; then