-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathamneziawg-install.sh
More file actions
3565 lines (3189 loc) · 140 KB
/
amneziawg-install.sh
File metadata and controls
3565 lines (3189 loc) · 140 KB
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
# AmneziaWG server installer
# https://github.com/wiresock/amneziawg-install
RED='\033[0;31m'
ORANGE='\033[0;33m'
GREEN='\033[0;32m'
NC='\033[0m'
AMNEZIAWG_DIR="/etc/amnezia/amneziawg"
WEB_PANEL_CONFIG_DIR="${AMNEZIAWG_DIR}/clients"
# Ensure sbin directories are in PATH for depmod, modprobe, sysctl, etc.
# Some minimal or non-login root shells may not include these by default.
# Only adjust PATH when the script is executed directly, not when sourced.
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
if [ -n "${PATH:-}" ]; then
export PATH="/sbin:/usr/sbin:${PATH:-}"
else
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
fi
fi
# Work around broken IPv6 on cloud VPS providers. Some providers resolve
# Launchpad / Ubuntu keyserver / COPR hostnames to both A and AAAA records,
# but outbound IPv6 connectivity is broken, causing apt, dnf,
# add-apt-repository, and COPR API calls to hang.
#
# Two complementary mitigations are applied:
#
# 1. APT ForceIPv4 (Debian/Ubuntu only) — a config file in apt.conf.d/
# forces all apt-based tools (including add-apt-repository, which uses
# python-apt internally) to use IPv4. This file is only written when
# apt-get or apt is present to avoid creating /etc/apt on RPM distros.
#
# 2. gai.conf IPv4-preference rule (all distros) — an IPv4-preference rule
# is injected into /etc/gai.conf so that glibc's getaddrinfo (and thus
# Python's socket.getaddrinfo and libcurl used by dnf) *prefers* IPv4.
# On Ubuntu 24.04 this also fixes a Python traceback from httplib2
# used by add-apt-repository, which does NOT honour Acquire::ForceIPv4.
APT_FORCE_IPV4_CONF="/etc/apt/apt.conf.d/99amneziawg-force-ipv4"
APT_FORCE_IPV4_SENTINEL="# Managed by amneziawg-install - safe to remove"
GAI_CONF="/etc/gai.conf"
GAI_CONF_SENTINEL="# Added by amneziawg-install - safe to remove"
GAI_CONF_IPV4_RULE="precedence ::ffff:0:0/96 100"
GAI_CONF_IPV4_RULE_REGEX='^[[:space:]]*precedence[[:space:]]+::ffff:0:0/96[[:space:]]+100([[:space:]]*(#.*)?)?$'
_APT_IPV4_PREV_TRAP_EXIT=""
_APT_IPV4_PREV_TRAP_INT=""
_APT_IPV4_PREV_TRAP_TERM=""
# Return success when gai.conf has an active (uncommented) IPv4 precedence
# rule for ::ffff:0:0/96 with value 100; commented defaults must not match.
gai_conf_has_active_ipv4_rule() {
grep -Eq "${GAI_CONF_IPV4_RULE_REGEX}" "${GAI_CONF}" 2>/dev/null
}
enable_apt_ipv4() {
# Only write the APT ForceIPv4 config on distros that actually use APT,
# to avoid creating /etc/apt on RPM-based systems.
if command -v apt-get >/dev/null 2>&1 || command -v apt >/dev/null 2>&1 || [[ -d /etc/apt ]]; then
mkdir -p /etc/apt/apt.conf.d
printf '%s\n%s\n' "${APT_FORCE_IPV4_SENTINEL}" 'Acquire::ForceIPv4 "true";' \
> "${APT_FORCE_IPV4_CONF}"
fi
# Prefer IPv4 in the system resolver so all glibc consumers (Python,
# libcurl/dnf, etc.) connect over IPv4.
if ! gai_conf_has_active_ipv4_rule; then
local _gai_existed=0
[[ -f "${GAI_CONF}" ]] && _gai_existed=1
printf '\n%s\n%s\n' "${GAI_CONF_SENTINEL}" "${GAI_CONF_IPV4_RULE}" \
>> "${GAI_CONF}"
# Only set permissions when we created the file; leave existing
# ownership/mode untouched so the cleanup path can preserve them.
if [[ "${_gai_existed}" -eq 0 ]]; then
chmod 0644 "${GAI_CONF}"
fi
fi
# Save existing trap commands so we can chain them (not just restore).
# trap -p output is eval-safe by design (bash always emits: trap -- 'body' SIG).
_APT_IPV4_PREV_TRAP_EXIT="$(trap -p EXIT || true)"
_APT_IPV4_PREV_TRAP_INT="$(trap -p INT || true)"
_APT_IPV4_PREV_TRAP_TERM="$(trap -p TERM || true)"
# Install traps that clean up *and* invoke any prior handler, so
# pre-existing cleanup logic still runs even if the script exits
# while IPv4 forcing is active.
trap '_cleanup_apt_ipv4_and_chain EXIT' EXIT
trap '_cleanup_apt_ipv4_and_chain INT' INT
trap '_cleanup_apt_ipv4_and_chain TERM' TERM
}
# Internal: remove the APT ForceIPv4 config and revert gai.conf changes.
_remove_ipv4_overrides() {
# Only remove the file if it carries our sentinel.
if [[ -f "${APT_FORCE_IPV4_CONF}" ]] && grep -qFm1 "${APT_FORCE_IPV4_SENTINEL}" "${APT_FORCE_IPV4_CONF}"; then
rm -f "${APT_FORCE_IPV4_CONF}"
fi
# Remove gai.conf lines we added (if any). Only act when our sentinel
# is present so pre-existing admin rules are never touched. This must
# be idempotent so interrupted previous runs are also cleaned up.
if [[ -f "${GAI_CONF}" ]] && grep -qF "${GAI_CONF_SENTINEL}" "${GAI_CONF}"; then
awk -v sent="${GAI_CONF_SENTINEL}" -v regex="${GAI_CONF_IPV4_RULE_REGEX}" '
# State machine:
# 1) skip our sentinel line
# 2) skip the immediately following active IPv4 rule if present
# 3) print all other lines unchanged
$0 == sent { prev_sent=1; next }
prev_sent == 1 && $0 ~ regex { prev_sent=0; next }
{ prev_sent=0; print }
' "${GAI_CONF}" > "${GAI_CONF}.tmp"
if ! chmod --reference="${GAI_CONF}" "${GAI_CONF}.tmp" || ! chown --reference="${GAI_CONF}" "${GAI_CONF}.tmp"; then
rm -f "${GAI_CONF}.tmp"
return 1
fi
mv "${GAI_CONF}.tmp" "${GAI_CONF}"
fi
}
# Internal: remove the config file, restore the previous trap for the
# given signal, then immediately invoke the restored handler so it runs
# during the same exit / signal delivery.
_cleanup_apt_ipv4_and_chain() {
local sig="$1"
# Preserve the original exit status so chained handlers see the real value.
local _saved_status=$?
_remove_ipv4_overrides
# Restore + chain: re-install the previous trap (if any), then
# re-deliver the signal / exit so bash invokes the restored handler.
# This avoids parsing trap -p output entirely (no sed/eval of bodies).
local prev_var="_APT_IPV4_PREV_TRAP_${sig}"
local prev_trap="${!prev_var}"
if [[ -n "${prev_trap}" ]]; then
# Re-install the previous trap (e.g. trap -- 'handler' EXIT).
eval "${prev_trap}"
# Re-deliver the signal so bash invokes the just-restored handler.
if [[ "${sig}" == "EXIT" ]]; then
# For EXIT: exiting re-fires the EXIT trap with the original status.
exit "${_saved_status}"
else
# For INT/TERM: re-raise the signal to invoke the restored handler.
kill -s "${sig}" "$$" 2>/dev/null || {
case "${sig}" in
INT) exit 130 ;; # 128 + SIGINT(2)
TERM) exit 143 ;; # 128 + SIGTERM(15)
esac
}
fi
else
trap - "${sig}"
if [[ "${sig}" == "EXIT" ]]; then
# Preserve the original exit status when no prior handler exists.
exit "${_saved_status}"
elif [[ "${sig}" == INT || "${sig}" == TERM ]]; then
# Re-raise the signal so default termination semantics are preserved.
kill -s "${sig}" "$$" 2>/dev/null || {
case "${sig}" in
INT) exit 130 ;; # 128 + SIGINT(2)
TERM) exit 143 ;; # 128 + SIGTERM(15)
esac
}
fi
fi
}
disable_apt_ipv4() {
_remove_ipv4_overrides
# Restore any previously installed traps.
if [[ -n "${_APT_IPV4_PREV_TRAP_EXIT}" ]]; then
eval "${_APT_IPV4_PREV_TRAP_EXIT}"
else
trap - EXIT
fi
if [[ -n "${_APT_IPV4_PREV_TRAP_INT}" ]]; then
eval "${_APT_IPV4_PREV_TRAP_INT}"
else
trap - INT
fi
if [[ -n "${_APT_IPV4_PREV_TRAP_TERM}" ]]; then
eval "${_APT_IPV4_PREV_TRAP_TERM}"
else
trap - TERM
fi
}
# For sensitive files (private keys, params, configs), a restrictive umask (077)
# is applied locally around their creation to avoid them being briefly world-readable.
# This avoids affecting subprocesses (apt/dnf, dkms, etc.) that expect the default umask.
# Safely quote a value for inclusion in a sourced params file
# Escapes single quotes and wraps in single quotes to prevent shell injection
function safeQuoteParam() {
local VALUE="$1"
# Replace each single quote with '"'"' (end quote, literal quote, start quote)
local ESCAPED
ESCAPED="$(printf '%s' "${VALUE}" | sed "s/'/'\"'\"'/g")"
printf "'%s'\n" "${ESCAPED}"
}
# Optional self-test for safeQuoteParam; run by setting SAFE_QUOTE_PARAM_SELFTEST=1
if [[ "${SAFE_QUOTE_PARAM_SELFTEST:-0}" == "1" ]]; then
TEST_VALUE="O'Reilly"
QUOTED="$(safeQuoteParam "${TEST_VALUE}")"
# Verify the quoted form matches the known-good shell-safe literal; no eval needed
EXPECTED="'O'\"'\"'Reilly'"
if [[ "${QUOTED}" != "${EXPECTED}" ]]; then
echo "ERROR: safeQuoteParam self-test failed: expected '${EXPECTED}', got '${QUOTED}'" >&2
exit 1
fi
fi
# Copy a client config file to the web panel config directory so the panel
# can discover and display it. This is a best-effort operation: if the web
# panel is not installed (directory absent), the copy is silently skipped.
function copyToWebPanelDir() {
local src_file="$1"
if [[ -d "${WEB_PANEL_CONFIG_DIR}" && ! -L "${WEB_PANEL_CONFIG_DIR}" && -f "${src_file}" && ! -L "${src_file}" ]]; then
local dest
dest="${WEB_PANEL_CONFIG_DIR}/$(basename "${src_file}")"
# Avoid following or overwriting a pre-existing symlink at the destination.
if [[ -L "${dest}" ]]; then
# Best-effort: warn and skip rather than risk clobbering the symlink target.
echo "Warning: refusing to copy '${src_file}' to '${dest}' because destination is a symlink" >&2
return 0
fi
cp -f "${src_file}" "${WEB_PANEL_CONFIG_DIR}/" 2>/dev/null || true
# Only adjust ownership and permissions on a regular non-symlink file we just copied.
if [[ -f "${dest}" && ! -L "${dest}" ]]; then
# Determine the directory's group; use it if available, otherwise fall back to root.
local dir_group dest_group
dir_group="$(stat -c '%G' "${WEB_PANEL_CONFIG_DIR}" 2>/dev/null || true)"
if [[ -n "${dir_group}" ]]; then
dest_group="${dir_group}"
else
dest_group="root"
fi
# Enforce root ownership and the chosen group before tightening permissions.
chown "root:${dest_group}" "${dest}" 2>/dev/null || true
chmod 640 "${dest}" 2>/dev/null || true
fi
fi
}
# Remove a client config file from the web panel config directory.
function removeFromWebPanelDir() {
local filename="$1"
if [[ -d "${WEB_PANEL_CONFIG_DIR}" && ! -L "${WEB_PANEL_CONFIG_DIR}" ]]; then
rm -f -- "${WEB_PANEL_CONFIG_DIR}/${filename}" 2>/dev/null || true
fi
}
# Serialize all server parameters to a params file
# Uses safe quoting for string values to prevent shell injection when sourced
# Arguments:
# $1 - Output file path to write the serialized params to
function serializeParams() {
local OUTPUT_FILE="$1"
if [[ -z "${OUTPUT_FILE}" ]]; then
echo "ERROR: serializeParams() requires an output file path" >&2
return 1
fi
# Apply a restrictive umask only while writing the params file to disk,
# so that subprocesses (apt/dnf, dkms, etc.) are not affected.
local OLD_UMASK
OLD_UMASK="$(umask)"
umask 077
cat >"${OUTPUT_FILE}" <<EOF
SERVER_PUB_IP=$(safeQuoteParam "${SERVER_PUB_IP}")
SERVER_PUB_NIC=$(safeQuoteParam "${SERVER_PUB_NIC}")
SERVER_AWG_NIC=$(safeQuoteParam "${SERVER_AWG_NIC}")
SERVER_AWG_IPV4=$(safeQuoteParam "${SERVER_AWG_IPV4}")
SERVER_AWG_IPV6=$(safeQuoteParam "${SERVER_AWG_IPV6}")
SERVER_PORT=$(safeQuoteParam "${SERVER_PORT}")
SERVER_PRIV_KEY=$(safeQuoteParam "${SERVER_PRIV_KEY}")
SERVER_PUB_KEY=$(safeQuoteParam "${SERVER_PUB_KEY}")
CLIENT_DNS_1=$(safeQuoteParam "${CLIENT_DNS_1}")
CLIENT_DNS_2=$(safeQuoteParam "${CLIENT_DNS_2}")
ALLOWED_IPS=$(safeQuoteParam "${ALLOWED_IPS}")
SERVER_AWG_JC=$(safeQuoteParam "${SERVER_AWG_JC}")
SERVER_AWG_JMIN=$(safeQuoteParam "${SERVER_AWG_JMIN}")
SERVER_AWG_JMAX=$(safeQuoteParam "${SERVER_AWG_JMAX}")
SERVER_AWG_S1=$(safeQuoteParam "${SERVER_AWG_S1}")
SERVER_AWG_S2=$(safeQuoteParam "${SERVER_AWG_S2}")
SERVER_AWG_S3=$(safeQuoteParam "${SERVER_AWG_S3}")
SERVER_AWG_S4=$(safeQuoteParam "${SERVER_AWG_S4}")
SERVER_AWG_H1=$(safeQuoteParam "${SERVER_AWG_H1}")
SERVER_AWG_H2=$(safeQuoteParam "${SERVER_AWG_H2}")
SERVER_AWG_H3=$(safeQuoteParam "${SERVER_AWG_H3}")
SERVER_AWG_H4=$(safeQuoteParam "${SERVER_AWG_H4}")
EOF
umask "${OLD_UMASK}"
}
# Validate an IPv6 address string
# Handles full form (8 hextets), compressed form (with ::), and mixed forms
# Returns 0 if valid, 1 if invalid
# Note: Does not support IPv4-mapped addresses (e.g., ::ffff:192.0.2.1)
function isValidIPv6() {
local ADDR="$1"
if [[ -z "${ADDR}" ]]; then
return 1
fi
# Must only contain hex digits and colons
if ! [[ "${ADDR}" =~ ^[a-fA-F0-9:]+$ ]]; then
return 1
fi
# Must not start or end with a single colon (:: at boundaries is OK)
if [[ "${ADDR}" =~ ^:[^:] ]] || [[ "${ADDR}" =~ [^:]:$ ]]; then
return 1
fi
# Count :: occurrences (at most one allowed)
local WITHOUT_DC="${ADDR//::}"
local DC_COUNT=$(( (${#ADDR} - ${#WITHOUT_DC}) / 2 ))
if (( DC_COUNT > 1 )); then
return 1
fi
local -a PARTS=() LEFT_PARTS=() RIGHT_PARTS=()
local PART LEFT RIGHT LEFT_COUNT RIGHT_COUNT
if (( DC_COUNT == 1 )); then
LEFT="${ADDR%%::*}"
RIGHT="${ADDR#*::}"
LEFT_COUNT=0
RIGHT_COUNT=0
if [[ -n "${LEFT}" ]]; then
IFS=':' read -ra LEFT_PARTS <<< "${LEFT}"
LEFT_COUNT=${#LEFT_PARTS[@]}
for PART in "${LEFT_PARTS[@]}"; do
if [[ -z "${PART}" ]] || (( ${#PART} > 4 )); then
return 1
fi
done
fi
if [[ -n "${RIGHT}" ]]; then
IFS=':' read -ra RIGHT_PARTS <<< "${RIGHT}"
RIGHT_COUNT=${#RIGHT_PARTS[@]}
for PART in "${RIGHT_PARTS[@]}"; do
if [[ -z "${PART}" ]] || (( ${#PART} > 4 )); then
return 1
fi
done
fi
# With :: present, total groups must be fewer than 8
if (( LEFT_COUNT + RIGHT_COUNT >= 8 )); then
return 1
fi
else
# No :: compression - must have exactly 8 colon-separated groups
IFS=':' read -ra PARTS <<< "${ADDR}"
if (( ${#PARTS[@]} != 8 )); then
return 1
fi
for PART in "${PARTS[@]}"; do
if [[ -z "${PART}" ]] || (( ${#PART} > 4 )); then
return 1
fi
done
fi
return 0
}
# Expand an IPv6 address to its full 8-group form without :: compression
# Each group is lowercase with leading zeros stripped
# e.g., fd42:42:42::1 -> fd42:42:42:0:0:0:0:1
# Used for semantic comparison and reliable prefix extraction
function normalizeIPv6() {
local ADDR="$1"
local -a HEXTETS=() LEFT_PARTS=() RIGHT_PARTS=()
local LEFT RIGHT FILL_COUNT i RESULT NORMALIZED
if [[ "${ADDR}" == *"::"* ]]; then
LEFT="${ADDR%%::*}"
RIGHT="${ADDR#*::}"
if [[ -n "${LEFT}" ]]; then
IFS=':' read -ra LEFT_PARTS <<< "${LEFT}"
fi
if [[ -n "${RIGHT}" ]]; then
IFS=':' read -ra RIGHT_PARTS <<< "${RIGHT}"
fi
FILL_COUNT=$((8 - ${#LEFT_PARTS[@]} - ${#RIGHT_PARTS[@]}))
HEXTETS=("${LEFT_PARTS[@]}")
for (( i = 0; i < FILL_COUNT; i++ )); do
HEXTETS+=("0")
done
HEXTETS+=("${RIGHT_PARTS[@]}")
else
IFS=':' read -ra HEXTETS <<< "${ADDR}"
fi
RESULT=""
for (( i = 0; i < 8; i++ )); do
if (( i > 0 )); then
RESULT+=":"
fi
printf -v NORMALIZED '%x' "0x${HEXTETS[$i]:-0}"
RESULT+="${NORMALIZED}"
done
echo "${RESULT}"
}
# Compress a fully expanded IPv6 address to its canonical compressed form (RFC 5952)
# Replaces the longest run of consecutive zero groups (>= 2) with ::
# Input should be the output of normalizeIPv6() (8 lowercase groups, no leading zeros)
# e.g., fd42:42:42:0:0:0:0:2 -> fd42:42:42::2
function compressIPv6() {
local ADDR="$1"
local -a IPV6_PARTS
IFS=':' read -ra IPV6_PARTS <<< "${ADDR}"
# Find the longest consecutive run of '0' groups (leftmost if tied)
local BEST_START=-1
local BEST_LEN=0
local CUR_START=-1
local CUR_LEN=0
local i
for (( i = 0; i < 8; i++ )); do
if [[ "${IPV6_PARTS[$i]}" == "0" ]]; then
if (( CUR_START == -1 )); then
CUR_START=$i
CUR_LEN=1
else
(( CUR_LEN++ ))
fi
if (( CUR_LEN > BEST_LEN )); then
BEST_START=$CUR_START
BEST_LEN=$CUR_LEN
fi
else
CUR_START=-1
CUR_LEN=0
fi
done
# Per RFC 5952, only compress runs of 2 or more consecutive zero groups
if (( BEST_LEN < 2 )); then
local IFS=':'
echo "${IPV6_PARTS[*]}"
return
fi
# Build the compressed address
local IFS=':'
local LEFT_PARTS=("${IPV6_PARTS[@]:0:$BEST_START}")
local RIGHT_PARTS=("${IPV6_PARTS[@]:$((BEST_START + BEST_LEN))}")
local LEFT="${LEFT_PARTS[*]}"
local RIGHT="${RIGHT_PARTS[*]}"
echo "${LEFT}::${RIGHT}"
}
# Optional self-tests for compressIPv6. These are only run when the installer
# is executed directly with AMNEZIAWG_RUN_IPV6_TESTS=1 in the environment.
# They are intended to guard against regressions in the RFC 5952 logic.
function __compressIPv6_expect() {
local EXPECTED="$1"
local INPUT="$2"
local ACTUAL
ACTUAL="$(compressIPv6 "${INPUT}")"
if [[ "${ACTUAL}" != "${EXPECTED}" ]]; then
echo "compressIPv6 test failed: input='${INPUT}' expected='${EXPECTED}' got='${ACTUAL}'" >&2
return 1
fi
return 0
}
function run_compressIPv6_tests() {
local FAIL=0
# Addresses that should NOT compress (no run of >= 2 zero groups)
__compressIPv6_expect "2001:db8:0:1:2:3:4:5" "2001:db8:0:1:2:3:4:5" || FAIL=1
__compressIPv6_expect "2001:db8:0:1:2:3:4:0" "2001:db8:0:1:2:3:4:0" || FAIL=1
# Simple middle run
__compressIPv6_expect "2001:db8::1:0:0:1" "2001:db8:0:0:1:0:0:1" || FAIL=1
# Leading zero run
__compressIPv6_expect "::1:2:3:4:5" "0:0:0:1:2:3:4:5" || FAIL=1
# Trailing zero run
__compressIPv6_expect "2001:db8:1:2:3:4::" "2001:db8:1:2:3:4:0:0" || FAIL=1
# All zeros
__compressIPv6_expect "::" "0:0:0:0:0:0:0:0" || FAIL=1
# Longest run chosen over shorter one
__compressIPv6_expect "2001::1:0:0:1" "2001:0:0:0:1:0:0:1" || FAIL=1
# Tie case: leftmost longest run wins
# Two runs of length 2: positions 1–2 and 4–5
# Input: 2001:0:0:1:0:0:1:1 -> expected: 2001::1:0:0:1:1
__compressIPv6_expect "2001::1:0:0:1:1" "2001:0:0:1:0:0:1:1" || FAIL=1
if (( FAIL != 0 )); then
echo "compressIPv6 self-tests: FAILED" >&2
return 1
fi
echo "compressIPv6 self-tests: OK"
return 0
}
# Only run self-tests when this script is executed directly and explicitly requested.
if [[ "${BASH_SOURCE[0]}" == "${0}" && "${AMNEZIAWG_RUN_IPV6_TESTS:-0}" == "1" ]]; then
if run_compressIPv6_tests; then
exit 0
else
exit 1
fi
fi
function isRoot() {
if [[ "${EUID}" -ne 0 ]]; then
echo "You need to run this script as root" >&2
exit 1
fi
}
function checkVirt() {
if ! command -v systemd-detect-virt &>/dev/null; then
return
fi
if [[ "$(systemd-detect-virt)" == "openvz" ]]; then
echo "OpenVZ is not supported" >&2
exit 1
fi
if [[ "$(systemd-detect-virt)" == "lxc" ]]; then
echo "LXC is not supported (yet)." >&2
echo "WireGuard can technically run in an LXC container," >&2
echo "but the kernel module has to be installed on the host," >&2
echo "the container has to be run with some specific parameters" >&2
echo "and only the tools need to be installed in the container." >&2
exit 1
fi
}
function checkOS() {
if [[ ! -f /etc/os-release ]] || [[ ! -r /etc/os-release ]]; then
echo "Cannot detect OS: /etc/os-release is missing or not readable" >&2
exit 1
fi
# shellcheck source=/etc/os-release
source /etc/os-release
OS="${ID}"
if [[ -z "${OS}" ]]; then
echo "Cannot detect OS: /etc/os-release is missing the ID field" >&2
exit 1
fi
if [[ ${OS} == "debian" || ${OS} == "raspbian" ]]; then
if [[ -z "${VERSION_ID}" ]]; then
echo "Cannot detect Debian version: VERSION_ID is missing from /etc/os-release" >&2
exit 1
fi
# Extract major version to handle point-release formats (e.g., "11.7")
local DEBIAN_MAJOR
DEBIAN_MAJOR=$(echo "${VERSION_ID}" | cut -d'.' -f1)
if ! [[ ${DEBIAN_MAJOR} =~ ^[0-9]+$ ]] || [[ ${DEBIAN_MAJOR} -lt 11 ]]; then
echo "Your version of Debian (${VERSION_ID}) is not supported. Please use Debian 11 Bullseye or later" >&2
exit 1
fi
OS=debian # overwrite if raspbian
elif [[ ${OS} == "ubuntu" ]]; then
if [[ -z "${VERSION_ID}" ]]; then
echo "Cannot detect Ubuntu version: VERSION_ID is missing from /etc/os-release" >&2
exit 1
fi
local RELEASE_YEAR
RELEASE_YEAR=$(echo "${VERSION_ID}" | cut -d'.' -f1)
if ! [[ ${RELEASE_YEAR} =~ ^[0-9]+$ ]] || [[ ${RELEASE_YEAR} -lt 22 ]]; then
echo "Your version of Ubuntu (${VERSION_ID}) is not supported. Please use Ubuntu 22.04 or later" >&2
exit 1
fi
elif [[ ${OS} == "linuxmint" ]]; then
if [[ -z "${VERSION_ID}" ]]; then
echo "Cannot detect Linux Mint version: VERSION_ID is missing from /etc/os-release" >&2
exit 1
fi
# Linux Mint 21.x is based on Ubuntu 22.04; require major version >= 21
local MINT_MAJOR
MINT_MAJOR=$(echo "${VERSION_ID}" | cut -d'.' -f1)
if ! [[ ${MINT_MAJOR} =~ ^[0-9]+$ ]] || [[ ${MINT_MAJOR} -lt 21 ]]; then
echo "Your version of Linux Mint (${VERSION_ID}) is not supported. Please use Linux Mint 21 or later" >&2
exit 1
fi
OS=ubuntu # treat Linux Mint as Ubuntu for package management
elif [[ ${OS} == "fedora" ]]; then
if [[ -z "${VERSION_ID}" ]]; then
echo "Cannot detect Fedora version: VERSION_ID is missing from /etc/os-release" >&2
exit 1
fi
# Extract major version to handle potential future format changes
local FEDORA_MAJOR
FEDORA_MAJOR=$(echo "${VERSION_ID}" | cut -d'.' -f1)
if ! [[ ${FEDORA_MAJOR} =~ ^[0-9]+$ ]] || [[ ${FEDORA_MAJOR} -lt 39 ]]; then
echo "Your version of Fedora (${VERSION_ID}) is not supported. Please use Fedora 39 or later" >&2
exit 1
fi
elif [[ ${OS} == 'centos' ]] || [[ ${OS} == 'almalinux' ]] || [[ ${OS} == 'rocky' ]]; then
if [[ -z "${VERSION_ID}" ]]; then
echo "Cannot detect CentOS/AlmaLinux/Rocky version: VERSION_ID is missing from /etc/os-release" >&2
exit 1
fi
if [[ ${VERSION_ID} == 7* ]] || [[ ${VERSION_ID} == 8* ]]; then
echo "Your version of CentOS (${VERSION_ID}) is not supported. Please use CentOS 9 or later" >&2
exit 1
fi
else
echo "Looks like you aren't running this installer on a supported system (Debian, Ubuntu, Linux Mint, or CentOS)." >&2
exit 1
fi
}
function getTemporarilyDisabledRPMFamilyMessage() {
echo "Fedora, AlmaLinux, and Rocky Linux support is temporarily disabled because verified AmneziaWG 2.0 packages are not currently available for these RPM-based distributions. Please watch this repository's releases and README for support status updates."
}
function ensureSupportedInstallDistro() {
# Temporary install block for RPM-family rebuild verification status.
# Keep OS detection intact so existing installs on these distros can still
# run non-install operations until AWG 2.0 packages are verified.
if [[ ${OS} == 'fedora' ]] || [[ ${OS} == 'almalinux' ]] || [[ ${OS} == 'rocky' ]]; then
echo "$(getTemporarilyDisabledRPMFamilyMessage)" >&2
exit 1
fi
}
function getHomeDirForClient() {
local CLIENT_NAME=$1
if [[ -z "${CLIENT_NAME}" ]]; then
echo "Error: getHomeDirForClient() requires a client name as argument"
exit 1
fi
# Home directory of the user, where the client configuration will be written.
# Use getent passwd for reliable lookup (supports LDAP, custom home paths, etc.),
# but gracefully handle systems where getent is unavailable or misconfigured.
local PASSWD_HOME=""
local RESULT_DIR
local HAVE_GETENT=false
if command -v getent &>/dev/null; then
HAVE_GETENT=true
fi
if [[ "${HAVE_GETENT}" == true ]]; then
PASSWD_HOME=$(getent passwd "${CLIENT_NAME}" 2>/dev/null | cut -d: -f6)
fi
if [[ -n "${PASSWD_HOME}" ]] && [[ -d "${PASSWD_HOME}" ]]; then
RESULT_DIR="${PASSWD_HOME}"
elif [[ -d "/home/${CLIENT_NAME}" ]]; then
# Fallback to traditional /home path for the client when getent is unavailable or misconfigured
RESULT_DIR="/home/${CLIENT_NAME}"
elif [[ "${CLIENT_NAME}" == "root" ]]; then
# Explicitly handle root client
RESULT_DIR="/root"
elif [[ "${SUDO_USER:-}" ]]; then
# if not a system user, use SUDO_USER
local SUDO_HOME=""
if [[ "${HAVE_GETENT}" == true ]]; then
SUDO_HOME=$(getent passwd "${SUDO_USER}" 2>/dev/null | cut -d: -f6)
fi
if [[ -n "${SUDO_HOME}" ]] && [[ -d "${SUDO_HOME}" ]]; then
RESULT_DIR="${SUDO_HOME}"
elif [[ -d "/home/${SUDO_USER}" ]]; then
# Fallback to traditional /home path when getent is unavailable or misconfigured
RESULT_DIR="/home/${SUDO_USER}"
else
RESULT_DIR="/root"
fi
else
# if not SUDO_USER, use /root
RESULT_DIR="/root"
fi
echo "${RESULT_DIR}"
}
function initialCheck() {
isRoot
checkVirt
checkOS
}
# Strip the deprecated REMAKE_INITRD directive from the amneziawg DKMS config
# (newer DKMS versions print noisy warnings for it).
function sanitizeAwgDkmsConf() {
local AWG_DKMS_CONF
for AWG_DKMS_CONF in /var/lib/dkms/amneziawg/*/source/dkms.conf; do
[[ -f "${AWG_DKMS_CONF}" ]] && sed -i '/^REMAKE_INITRD=/d' "${AWG_DKMS_CONF}"
done
}
# Install kernel headers for the running kernel so DKMS can compile the module.
# $1 – kernel version string; defaults to the running kernel (uname -r).
# For APT-based systems the caller must have already activated enable_apt_ipv4.
function installKernelHeaders() {
local KERNEL_VER="${1:-$(uname -r)}"
if [[ "${OS}" == 'ubuntu' ]]; then
local HEADER_INSTALLED=0
local HEADER_CANDIDATES=("linux-headers-${KERNEL_VER}" "raspberrypi-kernel-headers" "linux-headers-generic")
local HDR_PKG
for HDR_PKG in "${HEADER_CANDIDATES[@]}"; do
if apt-get install -y "${HDR_PKG}"; then
HEADER_INSTALLED=1
break
else
echo -e "${ORANGE}WARNING: Failed to install kernel headers package '${HDR_PKG}'. Trying next candidate...${NC}"
fi
done
if [[ "${HEADER_INSTALLED}" -ne 1 ]]; then
echo -e "${ORANGE}WARNING: Failed to install any suitable kernel headers package. DKMS module build may fail; continuing, but the amneziawg kernel module might not be available until headers are installed and the module is rebuilt.${NC}"
fi
elif [[ "${OS}" == 'debian' ]]; then
local HEADER_INSTALLED=0
local HEADER_CANDIDATES=("linux-headers-${KERNEL_VER}" "raspberrypi-kernel-headers")
local DEB_ARCH
DEB_ARCH=$(dpkg --print-architecture 2>/dev/null) && HEADER_CANDIDATES+=("linux-headers-${DEB_ARCH}")
local HDR_PKG
for HDR_PKG in "${HEADER_CANDIDATES[@]}"; do
if apt-get install -y "${HDR_PKG}"; then
HEADER_INSTALLED=1
break
else
echo -e "${ORANGE}WARNING: Failed to install kernel headers package '${HDR_PKG}'. Trying next candidate...${NC}"
fi
done
if [[ "${HEADER_INSTALLED}" -ne 1 ]]; then
echo -e "${ORANGE}WARNING: Failed to install any suitable kernel headers package. DKMS module build may fail; continuing, but the amneziawg kernel module might not be available until headers are installed and the module is rebuilt.${NC}"
fi
elif [[ "${OS}" == 'fedora' ]] || [[ "${OS}" == 'centos' ]] || [[ "${OS}" == 'almalinux' ]] || [[ "${OS}" == 'rocky' ]]; then
if ! dnf install -y "kernel-devel-${KERNEL_VER}"; then
echo -e "${ORANGE}WARNING: Failed to install kernel-devel for the running kernel (${KERNEL_VER}). Attempting to install the latest kernel-devel instead.${NC}"
if ! dnf install -y kernel-devel; then
echo -e "${ORANGE}WARNING: Failed to install any kernel-devel package. Continuing without kernel headers; DKMS module builds may fail until headers are installed and the system is rebooted.${NC}"
fi
fi
fi
}
# Start awg-quick@${SERVER_AWG_NIC} when the service is inactive.
# Called after any successful module-load path so the interface is available
# for subsequent awg syncconf calls. Exits with code 1 on failure.
function ensureAwgQuickRunning() {
if [[ -n "${SERVER_AWG_NIC:-}" ]] && ! systemctl is-active --quiet "awg-quick@${SERVER_AWG_NIC}"; then
echo -e "${ORANGE}Starting awg-quick@${SERVER_AWG_NIC} (was not running)...${NC}"
if ! systemctl start "awg-quick@${SERVER_AWG_NIC}"; then
echo -e "${RED}ERROR: Failed to start awg-quick@${SERVER_AWG_NIC}.${NC}"
echo -e "${ORANGE}Check service status with: systemctl status awg-quick@${SERVER_AWG_NIC}${NC}"
exit 1
fi
echo -e "${GREEN}awg-quick@${SERVER_AWG_NIC} started successfully.${NC}"
fi
}
# Ensure the amneziawg kernel module is built and loaded for the running kernel.
#
# After a kernel upgrade the DKMS module may still be built only for the old
# kernel. This function detects that situation and automatically:
# 1. Installs the matching kernel headers (if missing)
# 2. Runs dkms autoinstall for the current kernel
# 3. Rebuilds the module dependency cache (depmod -a)
# 4. Loads the module with modprobe
# 5. Starts the awg-quick service if it was not already running
#
# If everything is already fine the function returns immediately (idempotent).
# If repair fails, it prints diagnostic information and exits with code 1.
function ensureAmneziawgKernelModule() {
local KERNEL_VER
KERNEL_VER="$(uname -r)"
# Fast-path: if the module is already loaded, ensure the VPN service is also
# running before returning.
if lsmod 2>/dev/null | grep -q '^amneziawg '; then
ensureAwgQuickRunning
return 0
fi
# If the module is already built for this kernel, try loading it before
# falling back to the full repair path.
if [ -n "$(find "/lib/modules/${KERNEL_VER}" -name 'amneziawg.ko*' -print -quit 2>/dev/null)" ]; then
if modprobe amneziawg 2>/dev/null && lsmod 2>/dev/null | grep -q '^amneziawg '; then
# Module loaded successfully; start the VPN service if it was not running.
ensureAwgQuickRunning
return 0
fi
fi
echo -e "${ORANGE}amneziawg kernel module is not built or loaded for kernel ${KERNEL_VER}.${NC}"
echo -e "${ORANGE}Attempting automatic repair...${NC}"
# Install missing kernel headers so DKMS can compile the module.
# installKernelHeaders() tries candidates in order and warns on failure.
if [[ "${OS}" == 'ubuntu' ]] || [[ "${OS}" == 'debian' ]]; then
local HEADERS_PKG="linux-headers-${KERNEL_VER}"
if ! dpkg-query -W -f='${Status}' "${HEADERS_PKG}" 2>/dev/null | grep -q 'install ok installed'; then
echo -e "${ORANGE}Kernel headers (${HEADERS_PKG}) are not installed. Installing...${NC}"
enable_apt_ipv4
installKernelHeaders "${KERNEL_VER}"
disable_apt_ipv4
fi
elif [[ "${OS}" == 'fedora' ]] || [[ "${OS}" == 'centos' ]] || [[ "${OS}" == 'almalinux' ]] || [[ "${OS}" == 'rocky' ]]; then
local HEADERS_PKG="kernel-devel-${KERNEL_VER}"
if ! rpm -q "${HEADERS_PKG}" &>/dev/null; then
echo -e "${ORANGE}Kernel headers (${HEADERS_PKG}) are not installed. Installing...${NC}"
enable_apt_ipv4
installKernelHeaders "${KERNEL_VER}"
disable_apt_ipv4
fi
fi
# Strip the deprecated REMAKE_INITRD directive to silence newer DKMS warnings
sanitizeAwgDkmsConf
# Build the module for the current kernel with DKMS.
# Even if this step reports failure we still attempt modprobe below: the
# actual success criterion is whether the .ko ends up loadable, and an
# earlier partial build can sometimes satisfy that. modprobe is the
# definitive check and will produce a clear error if the build truly failed.
if command -v dkms &>/dev/null; then
echo -e "${ORANGE}Running: dkms autoinstall -k ${KERNEL_VER}${NC}"
if ! dkms autoinstall -k "${KERNEL_VER}"; then
echo -e "${ORANGE}WARNING: dkms autoinstall failed for kernel ${KERNEL_VER}.${NC}"
local DKMS_LOG
DKMS_LOG=$(find /var/lib/dkms/amneziawg -name 'make.log' -path "*${KERNEL_VER}*" 2>/dev/null | head -n 1)
if [[ -n "${DKMS_LOG}" ]]; then
echo -e "${ORANGE}Last 20 lines of DKMS build log (${DKMS_LOG}):${NC}"
tail -20 "${DKMS_LOG}"
else
echo -e "${ORANGE}Build log not found. Check /var/lib/dkms/amneziawg/ for details.${NC}"
fi
fi
else
echo -e "${ORANGE}WARNING: dkms is not installed. Cannot rebuild the kernel module.${NC}"
fi
# Rebuild the module dependency cache (required for DKMS + compressed modules)
if command -v depmod &>/dev/null; then
depmod -a
fi
# Attempt to load the module
if ! modprobe amneziawg; then
echo -e "${RED}ERROR: amneziawg kernel module could not be loaded for kernel ${KERNEL_VER}.${NC}"
echo -e "${ORANGE}The module is still not available in /lib/modules/${KERNEL_VER}/${NC}"
if [[ "${OS}" == 'ubuntu' ]] || [[ "${OS}" == 'debian' ]]; then
echo -e "${ORANGE}Manual recovery:${NC}"
echo -e "${ORANGE} 1. apt install -y \"linux-headers-${KERNEL_VER}\"${NC}"
echo -e "${ORANGE} 2. dkms autoinstall -k \"${KERNEL_VER}\" && depmod -a${NC}"
echo -e "${ORANGE} 3. modprobe amneziawg${NC}"
echo -e "${ORANGE} 4. systemctl start \"awg-quick@${SERVER_AWG_NIC:-awg0}\"${NC}"
elif [[ "${OS}" == 'fedora' ]] || [[ "${OS}" == 'centos' ]] || [[ "${OS}" == 'almalinux' ]] || [[ "${OS}" == 'rocky' ]]; then
echo -e "${ORANGE}Manual recovery:${NC}"
echo -e "${ORANGE} 1. dnf install -y \"kernel-devel-${KERNEL_VER}\"${NC}"
echo -e "${ORANGE} 2. dkms autoinstall -k \"${KERNEL_VER}\" && depmod -a${NC}"
echo -e "${ORANGE} 3. modprobe amneziawg${NC}"
echo -e "${ORANGE} 4. systemctl start \"awg-quick@${SERVER_AWG_NIC:-awg0}\"${NC}"
fi
exit 1
fi
echo -e "${GREEN}amneziawg module loaded successfully for kernel ${KERNEL_VER}.${NC}"
# The module was just loaded — start the VPN service if it was not running.
# After a kernel upgrade the service fails at boot because ExecStartPre
# (modprobe amneziawg) returns an error; now that the module is available
# we restart it so the awg interface exists for subsequent awg syncconf calls.
ensureAwgQuickRunning
}
function readJminAndJmax() {
SERVER_AWG_JMIN=0
SERVER_AWG_JMAX=0
until [[ ${SERVER_AWG_JMIN} =~ ^[0-9]+$ ]] && (( ${SERVER_AWG_JMIN} >= 1 )) && (( ${SERVER_AWG_JMIN} <= 1280 )); do
read -rp "Server AmneziaWG Jmin [1-1280]: " -e -i 50 SERVER_AWG_JMIN
done
until [[ ${SERVER_AWG_JMAX} =~ ^[0-9]+$ ]] && (( ${SERVER_AWG_JMAX} >= 1 )) && (( ${SERVER_AWG_JMAX} <= 1280 )); do
read -rp "Server AmneziaWG Jmax [1-1280]: " -e -i 1000 SERVER_AWG_JMAX
done
}
function generateS1AndS2() {
RANDOM_AWG_S1=$(shuf -i15-150 -n1)
RANDOM_AWG_S2=$(shuf -i15-150 -n1)
}
function readS1AndS2() {
SERVER_AWG_S1=0
SERVER_AWG_S2=0
until [[ ${SERVER_AWG_S1} =~ ^[0-9]+$ ]] && (( ${SERVER_AWG_S1} >= 15 )) && (( ${SERVER_AWG_S1} <= 150 )); do
read -rp "Server AmneziaWG S1 [15-150]: " -e -i "${RANDOM_AWG_S1}" SERVER_AWG_S1
done
until [[ ${SERVER_AWG_S2} =~ ^[0-9]+$ ]] && (( ${SERVER_AWG_S2} >= 15 )) && (( ${SERVER_AWG_S2} <= 150 )); do
read -rp "Server AmneziaWG S2 [15-150]: " -e -i "${RANDOM_AWG_S2}" SERVER_AWG_S2
done
}
function generateS3AndS4() {
RANDOM_AWG_S3=$(shuf -i15-150 -n1)
RANDOM_AWG_S4=$(shuf -i15-150 -n1)
}
function readS3AndS4() {
SERVER_AWG_S3=0
SERVER_AWG_S4=0
until [[ ${SERVER_AWG_S3} =~ ^[0-9]+$ ]] && (( ${SERVER_AWG_S3} >= 15 )) && (( ${SERVER_AWG_S3} <= 150 )); do
read -rp "Server AmneziaWG S3 [15-150]: " -e -i "${RANDOM_AWG_S3}" SERVER_AWG_S3
done
until [[ ${SERVER_AWG_S4} =~ ^[0-9]+$ ]] && (( ${SERVER_AWG_S4} >= 15 )) && (( ${SERVER_AWG_S4} <= 150 )); do
read -rp "Server AmneziaWG S4 [15-150]: " -e -i "${RANDOM_AWG_S4}" SERVER_AWG_S4
done
}
# Parse a range string "min-max" or single value into MIN and MAX variables
# Uses indirect variable assignment via printf -v to set caller's variables by name
#
# NOTE: This function only validates format and that min <= max. It does NOT
# validate bounds - callers must use validateRange() to check domain-specific
# bounds (e.g., [5-2147483647] for H parameters, [15-150] for S parameters).
function parseRange() {
local INPUT="$1" # SECURITY: Must quote to prevent shell injection
local MIN_VAR_NAME="$2" # Name of variable to store min value (indirect assignment)
local MAX_VAR_NAME="$3" # Name of variable to store max value (indirect assignment)
# Validate input is not empty
if [[ -z "${INPUT}" ]]; then
return 1
fi
if [[ ${INPUT} =~ ^([0-9]+)-([0-9]+)$ ]]; then
# Force base-10 interpretation to avoid octal issues with leading zeros
# e.g., "010" would be interpreted as 8 (octal) without 10# prefix
local MIN=$((10#${BASH_REMATCH[1]}))
local MAX=$((10#${BASH_REMATCH[2]}))
# Validate that min <= max
if (( MIN > MAX )); then
return 1
fi
# Indirect assignment: sets the variable named by $MIN_VAR_NAME to $MIN
printf -v "$MIN_VAR_NAME" '%s' "${MIN}"
printf -v "$MAX_VAR_NAME" '%s' "${MAX}"
elif [[ ${INPUT} =~ ^[0-9]+$ ]]; then
# Single value: use as both min and max
# Force base-10 interpretation here as well
local VAL=$((10#${INPUT}))
printf -v "$MIN_VAR_NAME" '%s' "${VAL}"
printf -v "$MAX_VAR_NAME" '%s' "${VAL}"
else
return 1
fi
return 0
}
# Check if two ranges overlap
# Returns 0 (true) if ranges overlap, 1 (false) if they don't
#
# Note: This uses STRICT non-overlap detection where ranges must be fully separated.
# Ranges that share a boundary point (e.g., [5-100] and [100-200]) ARE considered
# overlapping because the value 100 could be selected from either range.
# For AmneziaWG header randomization, this ensures each H parameter produces
# values from completely distinct ranges, maximizing entropy and preventing
# any single value from appearing in multiple parameters.
#
# To create non-overlapping ranges, ensure: range1_max < range2_min
# Example: [5-99] and [100-200] do NOT overlap (99 < 100)
function rangesOverlap() {
local MIN1=$1
local MAX1=$2
local MIN2=$3
local MAX2=$4
# Ranges do NOT overlap if: max1 < min2 OR max2 < min1 (strict inequality)
# This means [5-100] and [100-200] DO overlap (100 is not < 100)
if (( MAX1 < MIN2 )) || (( MAX2 < MIN1 )); then
return 1 # No overlap
fi
return 0 # Overlap exists
}
# Validate that a range is valid (min <= max) and within bounds
function validateRange() {
local MIN=$1
local MAX=$2
local LOWER_BOUND=$3
local UPPER_BOUND=$4