forked from complexorganizations/wireguard-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wireguard-manager.sh
executable file
·1500 lines (1435 loc) · 61.8 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
#!/usr/bin/env bash
# https://github.com/complexorganizations/wireguard-manager
# Require script to be run as root
function super-user-check() {
if [ "${EUID}" -ne 0 ]; then
echo "Error: You need to run this script as administrator."
exit
fi
}
# Check for root
super-user-check
# Get the current system information
function system-information() {
if [ -f /etc/os-release ]; then
# shellcheck disable=SC1091
source /etc/os-release
CURRENT_DISTRO=${ID}
CURRENT_DISTRO_VERSION=${VERSION_ID}
fi
}
# Get the current system information
system-information
# Pre-Checks system requirements
function installing-system-requirements() {
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" ]; }; then
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 iptables)" ] || [ ! -x "$(command -v ifup)" ] || [ ! -x "$(command -v chattr)" ] || [ ! -x "$(command -v gpg)" ] || [ ! -x "$(command -v systemd-detect-virt)" ]; }; then
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 iptables 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
yum install epel-release elrepo-release -y
yum install curl coreutils jq iproute lsof cronie gawk procps-ng grep qrencode sed zip unzip openssl iptables NetworkManager e2fsprogs gnupg systemd -y
elif { [ "${CURRENT_DISTRO}" == "arch" ] || [ "${CURRENT_DISTRO}" == "archarm" ] || [ "${CURRENT_DISTRO}" == "manjaro" ]; }; then
pacman -Sy
pacman -S --noconfirm --needed curl coreutils jq iproute2 lsof cronie gawk procps-ng grep qrencode sed zip unzip openssl iptables ifupdown e2fsprogs gnupg systemd
elif [ "${CURRENT_DISTRO}" == "alpine" ]; then
apk update
apk add curl coreutils jq iproute2 lsof cronie gawk procps grep qrencode sed zip unzip openssl iptables 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 iptables ifupdown e2fsprogs gnupg systemd
fi
fi
else
echo "Error: ${CURRENT_DISTRO} ${CURRENT_DISTRO_VERSION} is not supported."
exit
fi
}
# check for requirements
installing-system-requirements
# Checking For Virtualization
function virt-check() {
# Deny certain virtualization
case $(systemd-detect-virt) in
"kvm" | "none" | "qemu" | "lxc" | "microsoft" | "vmware") ;;
*)
echo "$(systemd-detect-virt) virtualization is not supported (yet)."
exit
;;
esac
}
# Virtualization Check
virt-check
# Lets check the kernel version
function kernel-check() {
CURRENT_KERNEL_VERSION=$(uname -r | cut -d'.' -f1-2)
CURRENT_KERNEL_MAJOR_VERSION=$(echo "${CURRENT_KERNEL_VERSION}" | cut -d'.' -f1)
CURRENT_KERNEL_MINOR_VERSION=$(echo "${CURRENT_KERNEL_VERSION}" | cut -d'.' -f2)
ALLOWED_KERNEL_VERSION="3.1"
ALLOWED_KERNEL_MAJOR_VERSION=$(echo ${ALLOWED_KERNEL_VERSION} | cut -d'.' -f1)
ALLOWED_KERNEL_MINOR_VERSION=$(echo ${ALLOWED_KERNEL_VERSION} | cut -d'.' -f2)
if [ "${CURRENT_KERNEL_MAJOR_VERSION}" -lt "${ALLOWED_KERNEL_MAJOR_VERSION}" ]; then
echo "Error: Kernel ${CURRENT_KERNEL_VERSION} not supported, please update to ${ALLOWED_KERNEL_VERSION}."
exit
fi
if [ "${CURRENT_KERNEL_MAJOR_VERSION}" == "${ALLOWED_KERNEL_MAJOR_VERSION}" ]; then
if [ "${CURRENT_KERNEL_MINOR_VERSION}" -lt "${ALLOWED_KERNEL_MINOR_VERSION}" ]; then
echo "Error: Kernel ${CURRENT_KERNEL_VERSION} not supported, please update to ${ALLOWED_KERNEL_VERSION}."
exit
fi
fi
}
kernel-check
# Global variables
CURRENT_FILE_PATH=$(realpath "${0}")
CURRENT_INIT_SYSTEM=$(ps --no-headers -o comm 1)
WIREGUARD_WEBSITE_URL="https://www.wireguard.com"
WIREGUARD_PATH="/etc/wireguard"
WIREGUARD_CLIENT_PATH="${WIREGUARD_PATH}/clients"
WIREGUARD_PUB_NIC="wg0"
WIREGUARD_CONFIG="${WIREGUARD_PATH}/${WIREGUARD_PUB_NIC}.conf"
WIREGUARD_ADD_PEER_CONFIG="${WIREGUARD_PATH}/${WIREGUARD_PUB_NIC}-add-peer.conf"
SYSTEM_BACKUP_PATH="/var/backups"
WIREGUARD_CONFIG_BACKUP="${SYSTEM_BACKUP_PATH}/wireguard-manager.zip"
WIREGUARD_BACKUP_PASSWORD_PATH="${HOME}/.wireguard-manager"
WIREGUARD_IP_FORWARDING_CONFIG="/etc/sysctl.d/wireguard.conf"
RESOLV_CONFIG="/etc/resolv.conf"
RESOLV_CONFIG_OLD="${RESOLV_CONFIG}.old"
UNBOUND_ROOT="/etc/unbound"
UNBOUND_MANAGER="${UNBOUND_ROOT}/wireguard-manager"
UNBOUND_CONFIG="${UNBOUND_ROOT}/unbound.conf"
UNBOUND_ROOT_HINTS="${UNBOUND_ROOT}/root.hints"
UNBOUND_ANCHOR="/var/lib/unbound/root.key"
UNBOUND_CONFIG_HOST="${UNBOUND_ROOT}/unbound.conf.d/hosts.conf"
case $(shuf -i1-5 -n1) 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/prajwal-koirala/wireguard-manager/-/raw/main/assets/named.cache"
;;
esac
case $(shuf -i1-5 -n1) 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/prajwal-koirala/content-blocker/-/raw/main/assets/hosts"
;;
esac
case $(shuf -i1-5 -n1) 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/prajwal-koirala/wireguard-manager/-/raw/main/wireguard-manager.sh"
;;
esac
# Usage Guide of the application
function usage-guide() {
echo "usage: ./$(basename "${0}") <command>"
echo " --install Install WireGuard Interface"
echo " --start Start WireGuard Interface"
echo " --stop Stop WireGuard Interface"
echo " --restart Restart WireGuard Interface"
echo " --list Show WireGuard Peer(s)"
echo " --add Add WireGuard Peer"
echo " --remove Remove WireGuard Peer"
echo " --reinstall Reinstall WireGuard Interface"
echo " --uninstall Uninstall WireGuard Interface"
echo " --update Update WireGuard Manager"
echo " --ddns Update WireGuard IP Address"
echo " --backup Backup WireGuard"
echo " --restore Restore WireGuard"
echo " --purge Purge WireGuard Peer(s)"
echo " --help Show Usage Guide"
}
# The usage of the script
function usage() {
while [ $# -ne 0 ]; do
case ${1} in
--install)
shift
HEADLESS_INSTALL=${HEADLESS_INSTALL:-true}
;;
--start)
shift
WIREGUARD_OPTIONS=${WIREGUARD_OPTIONS:-2}
;;
--stop)
shift
WIREGUARD_OPTIONS=${WIREGUARD_OPTIONS:-3}
;;
--restart)
shift
WIREGUARD_OPTIONS=${WIREGUARD_OPTIONS:-4}
;;
--list)
shift
WIREGUARD_OPTIONS=${WIREGUARD_OPTIONS:-1}
;;
--add)
shift
WIREGUARD_OPTIONS=${WIREGUARD_OPTIONS:-5}
;;
--remove)
shift
WIREGUARD_OPTIONS=${WIREGUARD_OPTIONS:-6}
;;
--reinstall)
shift
WIREGUARD_OPTIONS=${WIREGUARD_OPTIONS:-7}
;;
--uninstall)
shift
WIREGUARD_OPTIONS=${WIREGUARD_OPTIONS:-8}
;;
--update)
shift
WIREGUARD_OPTIONS=${WIREGUARD_OPTIONS:-9}
;;
--backup)
shift
WIREGUARD_OPTIONS=${WIREGUARD_OPTIONS:-10}
;;
--restore)
shift
WIREGUARD_OPTIONS=${WIREGUARD_OPTIONS:-11}
;;
--ddns)
shift
WIREGUARD_OPTIONS=${WIREGUARD_OPTIONS:-12}
;;
--purge)
shift
WIREGUARD_OPTIONS=${WIREGUARD_OPTIONS:-14}
;;
--help)
shift
usage-guide
;;
*)
echo "Invalid argument: ${1}"
usage-guide
exit
;;
esac
done
}
usage "$@"
# All questions are skipped, and wireguard is installed and a configuration is generated.
function headless-install() {
if [ "${HEADLESS_INSTALL}" == true ]; then
INTERFACE_OR_PEER=${INTERFACE_OR_PEER:-1}
IPV4_SUBNET_SETTINGS=${IPV4_SUBNET_SETTINGS:-1}
IPV6_SUBNET_SETTINGS=${IPV6_SUBNET_SETTINGS:-1}
SERVER_HOST_V4_SETTINGS=${SERVER_HOST_V4_SETTINGS:-1}
SERVER_HOST_V6_SETTINGS=${SERVER_HOST_V6_SETTINGS:-1}
SERVER_PUB_NIC_SETTINGS=${SERVER_PUB_NIC_SETTINGS:-1}
SERVER_PORT_SETTINGS=${SERVER_PORT_SETTINGS:-1}
NAT_CHOICE_SETTINGS=${NAT_CHOICE_SETTINGS:-1}
MTU_CHOICE_SETTINGS=${MTU_CHOICE_SETTINGS:-1}
SERVER_HOST_SETTINGS=${SERVER_HOST_SETTINGS:-1}
DISABLE_HOST_SETTINGS=${DISABLE_HOST_SETTINGS:-1}
CLIENT_ALLOWED_IP_SETTINGS=${CLIENT_ALLOWED_IP_SETTINGS:-1}
AUTOMATIC_UPDATES_SETTINGS=${AUTOMATIC_UPDATES_SETTINGS:-1}
AUTOMATIC_BACKUP_SETTINGS=${AUTOMATIC_BACKUP_SETTINGS:-1}
DNS_PROVIDER_SETTINGS=${DNS_PROVIDER_SETTINGS:-1}
CONTENT_BLOCKER_SETTINGS=${CONTENT_BLOCKER_SETTINGS:-1}
CLIENT_NAME=${CLIENT_NAME:-$(openssl rand -hex 50)}
AUTOMATIC_CONFIG_REMOVER=${AUTOMATIC_CONFIG_REMOVER:-1}
fi
}
# No GUI
headless-install
# Set up the wireguard, if config it isn't already there.
if [ ! -f "${WIREGUARD_CONFIG}" ]; then
# Custom IPv4 subnet
function set-ipv4-subnet() {
echo "What IPv4 subnet do you want to use?"
echo " 1) 10.0.0.0/8 (Recommended)"
echo " 2) Custom (Advanced)"
until [[ "${IPV4_SUBNET_SETTINGS}" =~ ^[1-2]$ ]]; do
read -rp "Subnet Choice [1-2]:" -e -i 1 IPV4_SUBNET_SETTINGS
done
case ${IPV4_SUBNET_SETTINGS} in
1)
IPV4_SUBNET="10.0.0.0/8"
;;
2)
read -rp "Custom IPv4 Subnet:" IPV4_SUBNET
if [ -z "${IPV4_SUBNET}" ]; then
IPV4_SUBNET="10.0.0.0/8"
fi
;;
esac
}
# Custom IPv4 Subnet
set-ipv4-subnet
# Custom IPv6 subnet
function set-ipv6-subnet() {
echo "What IPv6 subnet do you want to use?"
echo " 1) fd00:00:00::0/8 (Recommended)"
echo " 2) Custom (Advanced)"
until [[ "${IPV6_SUBNET_SETTINGS}" =~ ^[1-2]$ ]]; do
read -rp "Subnet Choice [1-2]:" -e -i 1 IPV6_SUBNET_SETTINGS
done
case ${IPV6_SUBNET_SETTINGS} in
1)
IPV6_SUBNET="fd00:00:00::0/8"
;;
2)
read -rp "Custom IPv6 Subnet:" IPV6_SUBNET
if [ -z "${IPV6_SUBNET}" ]; then
IPV6_SUBNET="fd00:00:00::0/8"
fi
;;
esac
}
# Custom IPv6 Subnet
set-ipv6-subnet
# Private Subnet Ipv4
PRIVATE_SUBNET_V4=${PRIVATE_SUBNET_V4:-"${IPV4_SUBNET}"}
# Private Subnet Mask IPv4
PRIVATE_SUBNET_MASK_V4=$(echo "${PRIVATE_SUBNET_V4}" | cut -d "/" -f 2)
# IPv4 Getaway
GATEWAY_ADDRESS_V4=$(echo "${PRIVATE_SUBNET_V4}" | cut -d'.' -f1-3).1
# Private Subnet Ipv6
PRIVATE_SUBNET_V6=${PRIVATE_SUBNET_V6:-"${IPV6_SUBNET}"}
# Private Subnet Mask IPv6
PRIVATE_SUBNET_MASK_V6=$(echo "${PRIVATE_SUBNET_V6}" | cut -d "/" -f 2)
# IPv6 Getaway
GATEWAY_ADDRESS_V6="${PRIVATE_SUBNET_V6::-3}1"
# Get the IPv4
function test-connectivity-v4() {
echo "How would you like to detect IPv4?"
echo " 1) Curl (Recommended)"
echo " 2) Custom (Advanced)"
until [[ "${SERVER_HOST_V4_SETTINGS}" =~ ^[1-2]$ ]]; do
read -rp "IPv4 Choice [1-2]:" -e -i 1 SERVER_HOST_V4_SETTINGS
done
case ${SERVER_HOST_V4_SETTINGS} in
1)
SERVER_HOST_V4="$(curl --ipv4 --connect-timeout 5 --tlsv1.3 --silent 'https://api.ipengine.dev' | jq -r '.network.ip')"
if [ -z "${SERVER_HOST_V4}" ]; then
SERVER_HOST_V4="$(curl --ipv4 --connect-timeout 5 --tlsv1.3 --silent 'https://checkip.amazonaws.com')"
fi
;;
2)
read -rp "Custom IPv4:" SERVER_HOST_V4
if [ -z "${SERVER_HOST_V4}" ]; then
SERVER_HOST_V4="$(curl --ipv4 --connect-timeout 5 --tlsv1.3 --silent 'https://api.ipengine.dev' | jq -r '.network.ip')"
fi
if [ -z "${SERVER_HOST_V4}" ]; then
SERVER_HOST_V4="$(curl --ipv4 --connect-timeout 5 --tlsv1.3 --silent 'https://checkip.amazonaws.com')"
fi
;;
esac
}
# Get the IPv4
test-connectivity-v4
# Determine IPv6
function test-connectivity-v6() {
echo "How would you like to detect IPv6?"
echo " 1) Curl (Recommended)"
echo " 2) Custom (Advanced)"
until [[ "${SERVER_HOST_V6_SETTINGS}" =~ ^[1-2]$ ]]; do
read -rp "IPv6 Choice [1-2]:" -e -i 1 SERVER_HOST_V6_SETTINGS
done
case ${SERVER_HOST_V6_SETTINGS} in
1)
SERVER_HOST_V6="$(curl --ipv6 --connect-timeout 5 --tlsv1.3 --silent 'https://api.ipengine.dev' | jq -r '.network.ip')"
if [ -z "${SERVER_HOST_V6}" ]; then
SERVER_HOST_V6="$(curl --ipv6 --connect-timeout 5 --tlsv1.3 --silent 'https://checkip.amazonaws.com')"
fi
;;
2)
read -rp "Custom IPv6:" SERVER_HOST_V6
if [ -z "${SERVER_HOST_V6}" ]; then
SERVER_HOST_V6="$(curl --ipv6 --connect-timeout 5 --tlsv1.3 --silent 'https://api.ipengine.dev' | jq -r '.network.ip')"
fi
if [ -z "${SERVER_HOST_V6}" ]; then
SERVER_HOST_V6="$(curl --ipv6 --connect-timeout 5 --tlsv1.3 --silent 'https://checkip.amazonaws.com')"
fi
;;
esac
}
# Get the IPv6
test-connectivity-v6
# Determine public NIC
function server-pub-nic() {
echo "How would you like to detect NIC?"
echo " 1) IP (Recommended)"
echo " 2) Custom (Advanced)"
until [[ "${SERVER_PUB_NIC_SETTINGS}" =~ ^[1-2]$ ]]; do
read -rp "Nic Choice [1-2]:" -e -i 1 SERVER_PUB_NIC_SETTINGS
done
case ${SERVER_PUB_NIC_SETTINGS} in
1)
SERVER_PUB_NIC="$(ip -4 route ls | grep default | grep -Po '(?<=dev )(\S+)' | head -1)"
if [ -z "${SERVER_PUB_NIC}" ]; then
echo "Error: Your server's public network interface could not be found."
fi
;;
2)
read -rp "Custom NAT:" SERVER_PUB_NIC
if [ -z "${SERVER_PUB_NIC}" ]; then
SERVER_PUB_NIC="$(ip -4 route ls | grep default | grep -Po '(?<=dev )(\S+)' | head -1)"
fi
;;
esac
}
# Determine public NIC
server-pub-nic
# Determine host port
function set-port() {
echo "What port do you want WireGuard server to listen to?"
echo " 1) 51820 (Recommended)"
echo " 2) Custom (Advanced)"
until [[ "${SERVER_PORT_SETTINGS}" =~ ^[1-2]$ ]]; do
read -rp "Port Choice [1-2]:" -e -i 1 SERVER_PORT_SETTINGS
done
case ${SERVER_PORT_SETTINGS} in
1)
SERVER_PORT="51820"
if [ "$(lsof -i UDP:"${SERVER_PORT}")" ]; then
echo "Error: Please use a different port because ${SERVER_PORT} is already in use."
fi
;;
2)
until [[ "${SERVER_PORT}" =~ ^[0-9]+$ ]] && [ "${SERVER_PORT}" -ge 1 ] && [ "${SERVER_PORT}" -le 65535 ]; do
read -rp "Custom port [1-65535]:" SERVER_PORT
done
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."
fi
;;
esac
}
# Set port
set-port
# Determine Keepalive interval.
function nat-keepalive() {
echo "What do you want your keepalive interval to be?"
echo " 1) 25 (Default)"
echo " 2) Custom (Advanced)"
until [[ "${NAT_CHOICE_SETTINGS}" =~ ^[1-2]$ ]]; do
read -rp "Nat Choice [1-2]:" -e -i 1 NAT_CHOICE_SETTINGS
done
case ${NAT_CHOICE_SETTINGS} in
1)
NAT_CHOICE="25"
;;
2)
until [[ "${NAT_CHOICE}" =~ ^[0-9]+$ ]] && [ "${NAT_CHOICE}" -ge 1 ] && [ "${NAT_CHOICE}" -le 65535 ]; do
read -rp "Custom NAT [1-65535]:" NAT_CHOICE
done
;;
esac
}
# Keepalive interval
nat-keepalive
# Custom MTU or default settings
function mtu-set() {
echo "What MTU do you want to use?"
echo " 1) 1280 (Recommended)"
echo " 2) Custom (Advanced)"
until [[ "${MTU_CHOICE_SETTINGS}" =~ ^[1-2]$ ]]; do
read -rp "MTU Choice [1-2]:" -e -i 1 MTU_CHOICE_SETTINGS
done
case ${MTU_CHOICE_SETTINGS} in
1)
MTU_CHOICE="1280"
;;
2)
until [[ "${MTU_CHOICE}" =~ ^[0-9]+$ ]] && [ "${MTU_CHOICE}" -ge 1 ] && [ "${MTU_CHOICE}" -le 65535 ]; do
read -rp "Custom MTU [1-65535]:" MTU_CHOICE
done
;;
esac
}
# Set MTU
mtu-set
# What IP version would you like to be available on this WireGuard server?
function ipvx-select() {
echo "What IPv do you want to use to connect to the WireGuard server?"
echo " 1) IPv4 (Recommended)"
echo " 2) IPv6"
until [[ "${SERVER_HOST_SETTINGS}" =~ ^[1-2]$ ]]; do
read -rp "IP Choice [1-2]:" -e -i 1 SERVER_HOST_SETTINGS
done
case ${SERVER_HOST_SETTINGS} in
1)
if [ -n "${SERVER_HOST_V4}" ]; then
SERVER_HOST="${SERVER_HOST_V4}"
else
SERVER_HOST="[${SERVER_HOST_V6}]"
fi
;;
2)
if [ -n "${SERVER_HOST_V6}" ]; then
SERVER_HOST="[${SERVER_HOST_V6}]"
else
SERVER_HOST="${SERVER_HOST_V4}"
fi
;;
esac
}
# IPv4 or IPv6 Selector
ipvx-select
# Do you want to disable IPv4 or IPv6 or leave them both enabled?
function disable-ipvx() {
echo "Do you want to disable IPv4 or IPv6 on the server?"
echo " 1) No (Recommended)"
echo " 2) Disable IPv4"
echo " 3) Disable IPv6"
until [[ "${DISABLE_HOST_SETTINGS}" =~ ^[1-3]$ ]]; do
read -rp "Disable Host Choice [1-3]:" -e -i 1 DISABLE_HOST_SETTINGS
done
case ${DISABLE_HOST_SETTINGS} in
1)
echo "net.ipv4.ip_forward=1" >${WIREGUARD_IP_FORWARDING_CONFIG}
echo "net.ipv6.conf.all.forwarding=1" >>${WIREGUARD_IP_FORWARDING_CONFIG}
;;
2)
echo "net.ipv6.conf.all.forwarding=1" >${WIREGUARD_IP_FORWARDING_CONFIG}
;;
3)
echo "net.ipv4.ip_forward=1" >${WIREGUARD_IP_FORWARDING_CONFIG}
;;
esac
sysctl -p ${WIREGUARD_IP_FORWARDING_CONFIG}
}
# Disable IPv4 or IPv6
disable-ipvx
# Would you like to allow connections to your LAN neighbors?
function client-allowed-ip() {
echo "What traffic do you want the client to forward through WireGuard?"
echo " 1) Everything (Recommended)"
echo " 2) Custom (Advanced)"
until [[ "${CLIENT_ALLOWED_IP_SETTINGS}" =~ ^[1-2]$ ]]; do
read -rp "Client Allowed IP Choice [1-2]:" -e -i 1 CLIENT_ALLOWED_IP_SETTINGS
done
case ${CLIENT_ALLOWED_IP_SETTINGS} in
1)
CLIENT_ALLOWED_IP="0.0.0.0/0,::/0"
;;
2)
read -rp "Custom IPs:" CLIENT_ALLOWED_IP
if [ -z "${CLIENT_ALLOWED_IP}" ]; then
CLIENT_ALLOWED_IP="0.0.0.0/0,::/0"
fi
;;
esac
}
# Traffic Forwarding
client-allowed-ip
# real-time updates
function enable-automatic-updates() {
echo "Would you like to setup real-time updates?"
echo " 1) Yes (Recommended)"
echo " 2) No (Advanced)"
until [[ "${AUTOMATIC_UPDATES_SETTINGS}" =~ ^[1-2]$ ]]; do
read -rp "Automatic Updates [1-2]:" -e -i 1 AUTOMATIC_UPDATES_SETTINGS
done
case ${AUTOMATIC_UPDATES_SETTINGS} in
1)
crontab -l | {
cat
echo "0 0 * * * ${CURRENT_FILE_PATH} --update"
} | crontab -
if [[ "${CURRENT_INIT_SYSTEM}" == *"systemd"* ]]; then
if { [ "${CURRENT_DISTRO}" == "fedora" ] || [ "${CURRENT_DISTRO}" == "centos" ] || [ "${CURRENT_DISTRO}" == "rhel" ] || [ "${CURRENT_DISTRO}" == "almalinux" ] || [ "${CURRENT_DISTRO}" == "rocky" ]; }; then
systemctl enable crond
systemctl start crond
else
systemctl enable cron
systemctl start cron
fi
elif [[ "${CURRENT_INIT_SYSTEM}" == *"init"* ]]; then
service cron enable
service cron start
fi
;;
2)
echo "Real-time Updates Disabled"
;;
esac
}
# real-time updates
enable-automatic-updates
# real-time backup
function enable-automatic-backup() {
echo "Would you like to setup real-time backup?"
echo " 1) Yes (Recommended)"
echo " 2) No (Advanced)"
until [[ "${AUTOMATIC_BACKUP_SETTINGS}" =~ ^[1-2]$ ]]; do
read -rp "Automatic Backup [1-2]:" -e -i 1 AUTOMATIC_BACKUP_SETTINGS
done
case ${AUTOMATIC_BACKUP_SETTINGS} in
1)
crontab -l | {
cat
echo "0 0 * * * ${CURRENT_FILE_PATH} --backup"
} | crontab -
if [[ "${CURRENT_INIT_SYSTEM}" == *"systemd"* ]]; then
if { [ "${CURRENT_DISTRO}" == "fedora" ] || [ "${CURRENT_DISTRO}" == "centos" ] || [ "${CURRENT_DISTRO}" == "rhel" ] || [ "${CURRENT_DISTRO}" == "almalinux" ] || [ "${CURRENT_DISTRO}" == "rocky" ]; }; then
systemctl enable crond
systemctl start crond
else
systemctl enable cron
systemctl start cron
fi
elif [[ "${CURRENT_INIT_SYSTEM}" == *"init"* ]]; then
service cron enable
service cron start
fi
;;
2)
echo "Real-time Backup Disabled"
;;
esac
}
# real-time backup
enable-automatic-backup
# Would you like to install unbound.
function ask-install-dns() {
echo "Which DNS provider would you like to use?"
echo " 1) Unbound (Recommended)"
echo " 2) Custom (Advanced)"
until [[ "${DNS_PROVIDER_SETTINGS}" =~ ^[1-2]$ ]]; do
read -rp "DNS provider [1-2]:" -e -i 1 DNS_PROVIDER_SETTINGS
done
case ${DNS_PROVIDER_SETTINGS} in
1)
INSTALL_UNBOUND=true
echo "Do you want to prevent advertisements, tracking, malware, and phishing using the content-blocker?"
echo " 1) Yes (Recommended)"
echo " 2) No"
until [[ "${CONTENT_BLOCKER_SETTINGS}" =~ ^[1-2]$ ]]; do
read -rp "Content Blocker Choice [1-2]:" -e -i 1 CONTENT_BLOCKER_SETTINGS
done
case ${CONTENT_BLOCKER_SETTINGS} in
1)
INSTALL_BLOCK_LIST=true
;;
2)
INSTALL_BLOCK_LIST=false
;;
esac
;;
2)
CUSTOM_DNS=true
;;
esac
}
# Ask To Install DNS
ask-install-dns
# Let the users choose their custom dns provider.
function custom-dns() {
if [ "${CUSTOM_DNS}" == true ]; then
echo "Which DNS do you want to use with the 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 [ -x "$(command -v pihole)" ]; then
echo " 10) Pi-Hole (Advanced)"
fi
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
case ${CLIENT_DNS_SETTINGS} in
1)
CLIENT_DNS="1.1.1.1,1.0.0.1,2606:4700:4700::1111,2606:4700:4700::1001"
;;
2)
CLIENT_DNS="94.140.14.14,94.140.15.15,2a10:50c0::ad1:ff,2a10:50c0::ad2:ff"
;;
3)
CLIENT_DNS="45.90.28.167,45.90.30.167,2a07:a8c0::12:cf53,2a07:a8c1::12:cf53"
;;
4)
CLIENT_DNS="208.67.222.222,208.67.220.220,2620:119:35::35,2620:119:53::53"
;;
5)
CLIENT_DNS="8.8.8.8,8.8.4.4,2001:4860:4860::8888,2001:4860:4860::8844"
;;
6)
CLIENT_DNS="64.6.64.6,64.6.65.6,2620:74:1b::1:1,2620:74:1c::2:2"
;;
7)
CLIENT_DNS="9.9.9.9,149.112.112.112,2620:fe::fe,2620:fe::9"
;;
8)
CLIENT_DNS="80.67.169.40,80.67.169.12,2001:910:800::40,2001:910:800::12"
;;
9)
read -rp "Custom DNS:" CLIENT_DNS
if [ -z "${CLIENT_DNS}" ]; then
CLIENT_DNS="8.8.8.8,8.8.4.4,2001:4860:4860::8888,2001:4860:4860::8844"
fi
;;
10)
if [ -x "$(command -v pihole)" ]; then
CLIENT_DNS="${GATEWAY_ADDRESS_V4},${GATEWAY_ADDRESS_V6}"
else
INSTALL_UNBOUND=true
INSTALL_BLOCK_LIST=true
fi
;;
esac
fi
}
# use custom dns
custom-dns
# What would you like to name your first WireGuard peer?
function client-name() {
if [ -z "${CLIENT_NAME}" ]; then
echo "Let's name the WireGuard Peer. Use one word only, no special characters, no spaces."
read -rp "Client name:" -e -i "$(openssl rand -hex 50)" CLIENT_NAME
fi
if [ -z "${CLIENT_NAME}" ]; then
CLIENT_NAME="$(openssl rand -hex 50)"
fi
}
# Client Name
client-name
# Automatically remove wireguard peers after a period of time.
function auto-remove-confg() {
echo "Would you like to expire the peer after a certain period of time?"
echo " 1) Every Year (Recommended)"
echo " 2) No"
until [[ "${AUTOMATIC_CONFIG_REMOVER}" =~ ^[1-2]$ ]]; do
read -rp "Automatic config expire [1-2]:" -e -i 1 AUTOMATIC_CONFIG_REMOVER
done
case ${AUTOMATIC_CONFIG_REMOVER} in
1)
AUTOMATIC_WIREGUARD_EXPIRATION=true
;;
2)
AUTOMATIC_WIREGUARD_EXPIRATION=false
;;
esac
}
# Automatic Remove Config
auto-remove-confg
# Lets check the kernel version and check if headers are required
function install-kernel-headers() {
ALLOWED_KERNEL_VERSION="5.6"
ALLOWED_KERNEL_MAJOR_VERSION=$(echo ${ALLOWED_KERNEL_VERSION} | cut -d'.' -f1)
ALLOWED_KERNEL_MINOR_VERSION=$(echo ${ALLOWED_KERNEL_VERSION} | cut -d'.' -f2)
if [ "${CURRENT_KERNEL_MAJOR_VERSION}" -le "${ALLOWED_KERNEL_MAJOR_VERSION}" ]; then
INSTALL_LINUX_HEADERS=true
fi
if [ "${CURRENT_KERNEL_MAJOR_VERSION}" == "${ALLOWED_KERNEL_MAJOR_VERSION}" ]; then
if [ "${CURRENT_KERNEL_MINOR_VERSION}" -lt "${ALLOWED_KERNEL_MINOR_VERSION}" ]; then
INSTALL_LINUX_HEADERS=true
fi
if [ "${CURRENT_KERNEL_MINOR_VERSION}" -ge "${ALLOWED_KERNEL_MINOR_VERSION}" ]; then
INSTALL_LINUX_HEADERS=false
fi
fi
if [ "${INSTALL_LINUX_HEADERS}" == true ]; then
if { [ "${CURRENT_DISTRO}" == "ubuntu" ] || [ "${CURRENT_DISTRO}" == "debian" ] || [ "${CURRENT_DISTRO}" == "pop" ] || [ "${CURRENT_DISTRO}" == "kali" ] || [ "${CURRENT_DISTRO}" == "linuxmint" ] || [ "${CURRENT_DISTRO}" == "neon" ]; }; then
apt-get update
apt-get install linux-headers-"$(uname -r)" -y
elif [ "${CURRENT_DISTRO}" == "raspbian" ]; then
apt-get update
apt-get install raspberrypi-kernel-headers -y
elif { [ "${CURRENT_DISTRO}" == "arch" ] || [ "${CURRENT_DISTRO}" == "archarm" ] || [ "${CURRENT_DISTRO}" == "manjaro" ]; }; then
pacman -Sy
pacman -S --noconfirm --needed linux-headers
elif [ "${CURRENT_DISTRO}" == "fedora" ]; then
dnf check-update
dnf install kernel-headers-"$(uname -r)" kernel-devel-"$(uname -r)" -y
elif { [ "${CURRENT_DISTRO}" == "centos" ] || [ "${CURRENT_DISTRO}" == "rhel" ] || [ "${CURRENT_DISTRO}" == "almalinux" ] || [ "${CURRENT_DISTRO}" == "rocky" ]; }; then
yum check-update
yum install kernel-headers-"$(uname -r)" kernel-devel-"$(uname -r)" -y
fi
fi
}
# Kernel Version
install-kernel-headers
# Install WireGuard Server
function install-wireguard-server() {
if [ ! -x "$(command -v wg)" ]; then
if [ "${CURRENT_DISTRO}" == "ubuntu" ] && [ "${CURRENT_DISTRO_VERSION%.*}" -ge 21 ]; then
apt-get update
apt-get install wireguard -y
elif [ "${CURRENT_DISTRO}" == "ubuntu" ] && [ "${CURRENT_DISTRO_VERSION%.*}" -le 20 ]; then
apt-get update
apt-get install software-properties-common -y
add-apt-repository ppa:wireguard/wireguard -y
apt-get update
apt-get install wireguard -y
elif { [ "${CURRENT_DISTRO}" == "pop" ] || [ "${CURRENT_DISTRO}" == "linuxmint" ] || [ "${CURRENT_DISTRO}" == "neon" ]; }; then
apt-get update
apt-get install wireguard -y
elif { [ "${CURRENT_DISTRO}" == "debian" ] || [ "${CURRENT_DISTRO}" == "kali" ]; }; then
apt-get update
if { [ "${CURRENT_DISTRO}" == "debian" ] && [ "${CURRENT_DISTRO_VERSION%.*}" -le 11 ]; }; then
if [ ! -f "/etc/apt/sources.list.d/backports.list" ]; then
echo "deb http://deb.debian.org/debian buster-backports main" >>/etc/apt/sources.list.d/backports.list
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 648ACFD622F3D138
apt-get update
fi
fi
apt-get install wireguard -y
elif [ "${CURRENT_DISTRO}" == "raspbian" ]; then
apt-get update
if [ ! -f "/etc/apt/sources.list.d/backports.list" ]; then
echo "deb http://deb.debian.org/debian buster-backports main" >>/etc/apt/sources.list.d/backports.list
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 648ACFD622F3D138
apt-get update
fi
apt-get install wireguard -y
elif { [ "${CURRENT_DISTRO}" == "arch" ] || [ "${CURRENT_DISTRO}" == "archarm" ] || [ "${CURRENT_DISTRO}" == "manjaro" ]; }; then
pacman -Sy
pacman -S --noconfirm --needed wireguard-tools
elif [ "${CURRENT_DISTRO}" = "fedora" ] && [ "${CURRENT_DISTRO_VERSION%.*}" -ge 32 ]; then
dnf check-update
dnf install wireguard-tools -y
elif [ "${CURRENT_DISTRO}" = "fedora" ] && [ "${CURRENT_DISTRO_VERSION%.*}" -le 31 ]; then
dnf check-update
dnf copr enable jdoss/wireguard -y
dnf install wireguard-dkms wireguard-tools -y
elif [ "${CURRENT_DISTRO}" == "centos" ] && [ "${CURRENT_DISTRO_VERSION%.*}" -ge 8 ]; then
yum check-update
yum install kmod-wireguard wireguard-tools -y
elif [ "${CURRENT_DISTRO}" == "centos" ] && [ "${CURRENT_DISTRO_VERSION%.*}" -le 7 ]; then
yum check-update
yum install yum-plugin-elrepo -y
yum install kmod-wireguard wireguard-tools -y
elif [ "${CURRENT_DISTRO}" == "rhel" ] && [ "${CURRENT_DISTRO_VERSION%.*}" == 8 ]; then
yum check-update
yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm https://www.elrepo.org/elrepo-release-8.el8.elrepo.noarch.rpm
yum check-update
yum install kmod-wireguard wireguard-tools -y
elif [ "${CURRENT_DISTRO}" == "rhel" ] && [ "${CURRENT_DISTRO_VERSION%.*}" == 7 ]; then
yum check-update
yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm https://www.elrepo.org/elrepo-release-7.el7.elrepo.noarch.rpm
yum install kmod-wireguard wireguard-tools -y
elif [ "${CURRENT_DISTRO}" == "alpine" ]; then
apk update
apk add wireguard-tools
elif [ "${CURRENT_DISTRO}" == "freebsd" ]; then
pkg update
pkg install wireguard
elif { [ "${CURRENT_DISTRO}" == "almalinux" ] || [ "${CURRENT_DISTRO}" == "rocky" ]; }; then
yum check-update
yum install kmod-wireguard wireguard-tools -y
fi
fi
}
# Install WireGuard Server
install-wireguard-server
# Function to install Unbound
function install-unbound() {
if [ "${INSTALL_UNBOUND}" == true ]; then
if { [ ! -x "$(command -v unbound)" ] || [ ! -x "$(command -v resolvconf)" ]; }; then
if { [ "${CURRENT_DISTRO}" == "debian" ] || [ "${CURRENT_DISTRO}" == "ubuntu" ] || [ "${CURRENT_DISTRO}" == "raspbian" ] || [ "${CURRENT_DISTRO}" == "pop" ] || [ "${CURRENT_DISTRO}" == "kali" ] || [ "${CURRENT_DISTRO}" == "linuxmint" ] || [ "${CURRENT_DISTRO}" == "neon" ]; }; then
apt-get install unbound resolvconf -y
if [ "${CURRENT_DISTRO}" == "ubuntu" ]; then
if [[ "${CURRENT_INIT_SYSTEM}" == *"systemd"* ]]; then
systemctl stop systemd-resolved
systemctl disable systemd-resolved
elif [[ "${CURRENT_INIT_SYSTEM}" == *"init"* ]]; then
service systemd-resolved stop
service systemd-resolved disable
fi
fi
elif { [ "${CURRENT_DISTRO}" == "centos" ] || [ "${CURRENT_DISTRO}" == "rhel" ] || [ "${CURRENT_DISTRO}" == "almalinux" ] || [ "${CURRENT_DISTRO}" == "rocky" ]; }; then
yum install unbound -y
elif [ "${CURRENT_DISTRO}" == "fedora" ]; then
dnf install unbound resolvconf -y
elif { [ "${CURRENT_DISTRO}" == "arch" ] || [ "${CURRENT_DISTRO}" == "archarm" ] || [ "${CURRENT_DISTRO}" == "manjaro" ]; }; then
pacman -S --noconfirm unbound resolvconf
elif [ "${CURRENT_DISTRO}" == "alpine" ]; then
apk add unbound resolvconf
elif [ "${CURRENT_DISTRO}" == "freebsd" ]; then
pkg install unbound resolvconf
fi
fi
unbound-anchor -a ${UNBOUND_ANCHOR}
curl "${UNBOUND_ROOT_SERVER_CONFIG_URL}" --create-dirs -o ${UNBOUND_ROOT_HINTS}
NPROC=$(nproc)
echo "server:
num-threads: ${NPROC}
verbosity: 1
root-hints: ${UNBOUND_ROOT_HINTS}
auto-trust-anchor-file: ${UNBOUND_ANCHOR}
interface: 0.0.0.0
interface: ::0
max-udp-size: 3072
access-control: 0.0.0.0/0 refuse
access-control: ::0 refuse
access-control: ${PRIVATE_SUBNET_V4} allow
access-control: ${PRIVATE_SUBNET_V6} allow
access-control: 127.0.0.1 allow
access-control: ::1 allow
private-address: ${PRIVATE_SUBNET_V4}
private-address: ${PRIVATE_SUBNET_V6}
do-tcp: no
hide-identity: yes
hide-version: yes
harden-glue: yes
harden-dnssec-stripped: yes
harden-referral-path: yes
unwanted-reply-threshold: 10000000
cache-min-ttl: 1800
cache-max-ttl: 14400
prefetch: yes
qname-minimisation: yes
prefetch-key: yes" >${UNBOUND_CONFIG}
if [ -f "${RESOLV_CONFIG_OLD}" ]; then
rm -f ${RESOLV_CONFIG_OLD}
fi
if [ -f "${RESOLV_CONFIG}" ]; then
chattr -i ${RESOLV_CONFIG}
mv ${RESOLV_CONFIG} ${RESOLV_CONFIG_OLD}
fi
echo "nameserver 127.0.0.1" >>${RESOLV_CONFIG}
echo "nameserver ::1" >>${RESOLV_CONFIG}
chattr +i ${RESOLV_CONFIG}
echo "Unbound: true" >>${UNBOUND_MANAGER}
if [ "${INSTALL_BLOCK_LIST}" == true ]; then
echo "include: ${UNBOUND_CONFIG_HOST}" >>${UNBOUND_CONFIG}
curl "${UNBOUND_CONFIG_HOST_URL}" | awk '$1' | awk '{print "local-zone: \""$1"\" always_refuse"}' >${UNBOUND_CONFIG_HOST}
fi
# restart unbound
if [[ "${CURRENT_INIT_SYSTEM}" == *"systemd"* ]]; then
systemctl reenable unbound
systemctl restart unbound
elif [[ "${CURRENT_INIT_SYSTEM}" == *"init"* ]]; then
service unbound enable
service unbound restart
fi
CLIENT_DNS="${GATEWAY_ADDRESS_V4},${GATEWAY_ADDRESS_V6}"
fi