-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathpasarguard.sh
More file actions
executable file
·1988 lines (1729 loc) · 64.7 KB
/
pasarguard.sh
File metadata and controls
executable file
·1988 lines (1729 loc) · 64.7 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
#!/usr/bin/env bash
set -e
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
SHARED_LIB_DIR="${SCRIPT_DIR}/lib"
REQUIRED_SHARED_LIBS="common.sh system.sh docker.sh github.sh env.sh pasarguard-backup.sh pasarguard-restore.sh"
if [ ! -f "$SHARED_LIB_DIR/common.sh" ]; then
SHARED_LIB_DIR="/usr/local/lib/pasarguard-scripts/lib"
fi
bootstrap_pasarguard_shared_libs() {
local fetch_repo="PasarGuard/scripts"
local bootstrap_dir="/usr/local/lib/pasarguard-scripts/lib"
local tmp_dir=""
local shared_lib=""
tmp_dir=$(mktemp -d) || return 1
mkdir -p "$bootstrap_dir" || {
rm -rf "$tmp_dir"
return 1
}
for shared_lib in $REQUIRED_SHARED_LIBS; do
if ! curl -fsSL "https://github.com/${fetch_repo}/raw/main/lib/${shared_lib}" -o "$tmp_dir/$shared_lib"; then
rm -rf "$tmp_dir"
return 1
fi
if ! install -m 644 "$tmp_dir/$shared_lib" "$bootstrap_dir/$shared_lib"; then
rm -rf "$tmp_dir"
return 1
fi
done
rm -rf "$tmp_dir"
SHARED_LIB_DIR="$bootstrap_dir"
return 0
}
missing_shared_lib=false
for shared_lib in $REQUIRED_SHARED_LIBS; do
if [ ! -f "$SHARED_LIB_DIR/$shared_lib" ]; then
missing_shared_lib=true
break
fi
done
if [ "$missing_shared_lib" = true ]; then
bootstrap_pasarguard_shared_libs
fi
for shared_lib in $REQUIRED_SHARED_LIBS; do
if [ ! -f "$SHARED_LIB_DIR/$shared_lib" ]; then
printf 'Missing shared library: %s\n' "$SHARED_LIB_DIR/$shared_lib" >&2
exit 1
fi
done
# shellcheck source=lib/common.sh
source "$SHARED_LIB_DIR/common.sh"
# shellcheck source=lib/system.sh
source "$SHARED_LIB_DIR/system.sh"
# shellcheck source=lib/docker.sh
source "$SHARED_LIB_DIR/docker.sh"
# shellcheck source=lib/github.sh
source "$SHARED_LIB_DIR/github.sh"
# shellcheck source=lib/env.sh
source "$SHARED_LIB_DIR/env.sh"
# shellcheck source=lib/pasarguard-backup.sh
source "$SHARED_LIB_DIR/pasarguard-backup.sh"
# shellcheck source=lib/pasarguard-restore.sh
source "$SHARED_LIB_DIR/pasarguard-restore.sh"
# Handle @ symbol if used in installation (skip it)
if [ "${1:-}" = "@" ]; then
shift
fi
INSTALL_DIR="/opt"
if [ -z "${APP_NAME:-}" ]; then
APP_NAME="pasarguard"
fi
APP_DIR="${APP_DIR:-$INSTALL_DIR/$APP_NAME}"
DATA_DIR="${DATA_DIR:-/var/lib/$APP_NAME}"
THEMES_DIR="$APP_DIR/themes"
COMPOSE_FILE="$APP_DIR/docker-compose.yml"
ENV_FILE="$APP_DIR/.env"
LAST_XRAY_CORES=10
is_valid_proxy_url() {
local proxy_url="$1"
[[ -z "$proxy_url" ]] && return 1
if [[ "$proxy_url" =~ ^(http|https|socks|socks4|socks4a|socks5|socks5h):// ]]; then
return 0
fi
return 1
}
get_backup_proxy_url() {
local proxy_value="${BACKUP_PROXY_URL:-${BACKUP_PROXY:-}}"
local proxy_enabled="${BACKUP_PROXY_ENABLED:-}"
if [ -z "$proxy_value" ]; then
return 1
fi
if [ -n "$proxy_enabled" ] && [[ ! "$proxy_enabled" =~ ^([Tt]rue|[Yy]es|1)$ ]]; then
return 1
fi
printf '%s\n' "$proxy_value"
return 0
}
is_domain() {
[[ "$1" =~ ^([A-Za-z0-9](-*[A-Za-z0-9])*\.)+(xn--[a-z0-9]{2,}|[A-Za-z]{2,})$ ]] && return 0 || return 1
}
is_ipv4() {
local ip="$1"
local IFS='.'
local octets=()
[[ "$ip" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]] || return 1
read -r -a octets <<<"$ip"
[ "${#octets[@]}" -eq 4 ] || return 1
for octet in "${octets[@]}"; do
if [ "$octet" -lt 0 ] || [ "$octet" -gt 255 ]; then
return 1
fi
done
return 0
}
is_ipv6() {
[[ "$1" =~ : ]] && return 0 || return 1
}
get_public_ipv4() {
local urls=(
"https://api4.ipify.org"
"https://ipv4.icanhazip.com"
"https://v4.ident.me"
"https://ipv4.myexternalip.com/raw"
"https://ifconfig.me/ip"
)
local server_ip=""
local url=""
for url in "${urls[@]}"; do
server_ip=$(curl -4 -fsS --max-time 5 "$url" 2>/dev/null | tr -d '[:space:]' || true)
if is_ipv4 "$server_ip"; then
echo "$server_ip"
return 0
fi
done
return 1
}
is_port_in_use() {
local port="$1"
if command -v ss >/dev/null 2>&1; then
ss -ltn 2>/dev/null | awk -v p=":${port}$" '$4 ~ p {found=1; exit} END {exit found ? 0 : 1}'
return
fi
if command -v netstat >/dev/null 2>&1; then
netstat -lnt 2>/dev/null | awk -v p=":${port} " '$4 ~ p {found=1; exit} END {exit found ? 0 : 1}'
return
fi
if command -v lsof >/dev/null 2>&1; then
lsof -nP -iTCP:"${port}" -sTCP:LISTEN >/dev/null 2>&1 && return 0
fi
return 1
}
ensure_acme_dependencies() {
command -v socat >/dev/null 2>&1 || install_package socat
command -v openssl >/dev/null 2>&1 || install_package openssl
}
install_acme() {
colorized_echo blue "Installing acme.sh for SSL certificate management..."
if curl -s https://get.acme.sh | sh >/dev/null 2>&1; then
colorized_echo green "acme.sh installed successfully"
return 0
fi
colorized_echo red "Failed to install acme.sh"
return 1
}
get_acme_sh_binary() {
if [ -x "${HOME}/.acme.sh/acme.sh" ]; then
echo "${HOME}/.acme.sh/acme.sh"
return 0
fi
if [ -x "/root/.acme.sh/acme.sh" ]; then
echo "/root/.acme.sh/acme.sh"
return 0
fi
if command -v acme.sh >/dev/null 2>&1; then
command -v acme.sh
return 0
fi
return 1
}
ensure_acme_auto_renew() {
local acme_bin="$1"
[ -n "$acme_bin" ] || return 0
"$acme_bin" --upgrade --auto-upgrade >/dev/null 2>&1 || true
"$acme_bin" --install-cronjob >/dev/null 2>&1 || true
}
build_pasarguard_ssl_reload_command() {
local backend_service=""
backend_service=$(detect_pasarguard_backend_service 2>/dev/null || true)
if [ -n "$backend_service" ]; then
echo "docker compose -f ${COMPOSE_FILE} -p ${APP_NAME} restart ${backend_service} >/dev/null 2>&1 || docker-compose -f ${COMPOSE_FILE} -p ${APP_NAME} restart ${backend_service} >/dev/null 2>&1 || docker compose -f ${COMPOSE_FILE} -p ${APP_NAME} restart >/dev/null 2>&1 || docker-compose -f ${COMPOSE_FILE} -p ${APP_NAME} restart >/dev/null 2>&1 || true"
else
echo "docker compose -f ${COMPOSE_FILE} -p ${APP_NAME} restart >/dev/null 2>&1 || docker-compose -f ${COMPOSE_FILE} -p ${APP_NAME} restart >/dev/null 2>&1 || true"
fi
}
has_nonempty_ssl_pair() {
local cert_file="$1"
local key_file="$2"
[ -s "$cert_file" ] && [ -s "$key_file" ]
}
copy_acme_cert_pair_from_store() {
local identifier="$1"
local cert_file="$2"
local key_file="$3"
local acme_home=""
local candidate_dir=""
local candidate_cert=""
local candidate_key=""
for acme_home in "${HOME}/.acme.sh" "/root/.acme.sh"; do
[ -d "$acme_home" ] || continue
for candidate_dir in "${acme_home}/${identifier}" "${acme_home}/${identifier}_ecc"; do
[ -d "$candidate_dir" ] || continue
candidate_cert="${candidate_dir}/fullchain.cer"
[ -s "$candidate_cert" ] || candidate_cert="${candidate_dir}/${identifier}.cer"
candidate_key="${candidate_dir}/${identifier}.key"
if [ -s "$candidate_cert" ] && [ -s "$candidate_key" ] &&
cp "$candidate_cert" "$cert_file" &&
cp "$candidate_key" "$key_file"; then
return 0
fi
done
done
return 1
}
install_acme_cert_pair() {
local acme_bin="$1"
local identifier="$2"
local cert_dir="$3"
local reload_cmd="$4"
local cert_file="${cert_dir}/fullchain.pem"
local key_file="${cert_dir}/privkey.pem"
"$acme_bin" --installcert -d "$identifier" \
--key-file "$key_file" \
--fullchain-file "$cert_file" \
--reloadcmd "$reload_cmd" >/dev/null 2>&1 || true
if has_nonempty_ssl_pair "$cert_file" "$key_file"; then
return 0
fi
"$acme_bin" --installcert -d "$identifier" --ecc \
--key-file "$key_file" \
--fullchain-file "$cert_file" \
--reloadcmd "$reload_cmd" >/dev/null 2>&1 || true
if has_nonempty_ssl_pair "$cert_file" "$key_file"; then
return 0
fi
if copy_acme_cert_pair_from_store "$identifier" "$cert_file" "$key_file" &&
has_nonempty_ssl_pair "$cert_file" "$key_file"; then
return 0
fi
rm -f "$cert_file" "$key_file" 2>/dev/null || true
return 1
}
setup_ssl_certificate() {
local domain="$1"
local http_port="${2:-80}"
local acme_bin=""
local cert_dir="$DATA_DIR/certs/${domain}"
local reload_cmd=""
if [ -z "$domain" ]; then
colorized_echo red "Domain is required for SSL certificate issuance."
return 1
fi
if ! is_domain "$domain"; then
colorized_echo red "Invalid domain format: ${domain}"
return 1
fi
if ! [[ "$http_port" =~ ^[0-9]+$ ]] || [ "$http_port" -lt 1 ] || [ "$http_port" -gt 65535 ]; then
colorized_echo red "Invalid HTTP challenge port: ${http_port}"
return 1
fi
ensure_acme_dependencies
if ! acme_bin=$(get_acme_sh_binary); then
install_acme || return 1
acme_bin=$(get_acme_sh_binary) || {
colorized_echo red "acme.sh binary not found after installation."
return 1
}
fi
if is_port_in_use "$http_port"; then
colorized_echo yellow "Port ${http_port} is already in use. SSL issuance may fail unless that service is stopped."
fi
mkdir -p "$cert_dir"
reload_cmd=$(build_pasarguard_ssl_reload_command)
colorized_echo blue "Issuing Let's Encrypt certificate for ${domain}..."
"$acme_bin" --set-default-ca --server letsencrypt --force >/dev/null 2>&1 || true
if ! "$acme_bin" --issue -d "$domain" --standalone --httpport "$http_port" --force; then
colorized_echo red "Failed to issue certificate for ${domain}."
rm -rf "${HOME}/.acme.sh/${domain}" "$cert_dir" 2>/dev/null || true
return 1
fi
if ! install_acme_cert_pair "$acme_bin" "$domain" "$cert_dir" "$reload_cmd"; then
colorized_echo red "Failed to install certificate for ${domain}."
return 1
fi
ensure_acme_auto_renew "$acme_bin"
chmod 600 "${cert_dir}/privkey.pem" 2>/dev/null || true
chmod 644 "${cert_dir}/fullchain.pem" 2>/dev/null || true
colorized_echo green "SSL certificate installed successfully."
colorized_echo green "Certificate: ${cert_dir}/fullchain.pem"
colorized_echo green "Private key: ${cert_dir}/privkey.pem"
return 0
}
setup_ip_ssl_certificate() {
local ipv4="$1"
local ipv6="$2"
local http_port="${3:-80}"
local acme_bin=""
local cert_dir="$DATA_DIR/certs/ip"
local reload_cmd=""
local domain_args=()
if ! is_ipv4 "$ipv4"; then
colorized_echo red "Invalid IPv4 address: ${ipv4}"
return 1
fi
if [ -n "$ipv6" ] && ! is_ipv6 "$ipv6"; then
colorized_echo red "Invalid IPv6 address: ${ipv6}"
return 1
fi
if ! [[ "$http_port" =~ ^[0-9]+$ ]] || [ "$http_port" -lt 1 ] || [ "$http_port" -gt 65535 ]; then
colorized_echo red "Invalid HTTP challenge port: ${http_port}"
return 1
fi
ensure_acme_dependencies
if ! acme_bin=$(get_acme_sh_binary); then
install_acme || return 1
acme_bin=$(get_acme_sh_binary) || {
colorized_echo red "acme.sh binary not found after installation."
return 1
}
fi
if is_port_in_use "$http_port"; then
colorized_echo yellow "Port ${http_port} is already in use. SSL issuance may fail unless that service is stopped."
fi
mkdir -p "$cert_dir"
reload_cmd=$(build_pasarguard_ssl_reload_command)
domain_args=(-d "$ipv4")
if [ -n "$ipv6" ]; then
domain_args+=(-d "$ipv6")
fi
colorized_echo blue "Issuing Let's Encrypt IP certificate for ${ipv4}..."
"$acme_bin" --set-default-ca --server letsencrypt --force >/dev/null 2>&1 || true
if ! "$acme_bin" --issue \
"${domain_args[@]}" \
--standalone \
--server letsencrypt \
--certificate-profile shortlived \
--days 6 \
--httpport "$http_port" \
--force; then
colorized_echo red "Failed to issue IP certificate."
rm -rf "${HOME}/.acme.sh/${ipv4}" "$cert_dir" 2>/dev/null || true
[ -n "$ipv6" ] && rm -rf "${HOME}/.acme.sh/${ipv6}" 2>/dev/null || true
return 1
fi
if ! install_acme_cert_pair "$acme_bin" "$ipv4" "$cert_dir" "$reload_cmd"; then
colorized_echo red "Failed to install IP certificate files."
rm -rf "$cert_dir" 2>/dev/null || true
return 1
fi
ensure_acme_auto_renew "$acme_bin"
chmod 600 "${cert_dir}/privkey.pem" 2>/dev/null || true
chmod 644 "${cert_dir}/fullchain.pem" 2>/dev/null || true
colorized_echo green "IP certificate installed successfully."
colorized_echo green "Certificate: ${cert_dir}/fullchain.pem"
colorized_echo green "Private key: ${cert_dir}/privkey.pem"
return 0
}
configure_custom_ssl_certificate() {
local cert_source="$1"
local key_source="$2"
local ca_type="${3:-public}"
local cert_dir="$DATA_DIR/certs/custom"
local target_cert="${cert_dir}/fullchain.pem"
local target_key="${cert_dir}/privkey.pem"
if [ ! -f "$cert_source" ] || [ ! -r "$cert_source" ] || [ ! -s "$cert_source" ]; then
colorized_echo red "Certificate file is missing or unreadable: $cert_source"
return 1
fi
if [ ! -f "$key_source" ] || [ ! -r "$key_source" ] || [ ! -s "$key_source" ]; then
colorized_echo red "Key file is missing or unreadable: $key_source"
return 1
fi
mkdir -p "$cert_dir"
cp "$cert_source" "$target_cert"
cp "$key_source" "$target_key"
chmod 644 "$target_cert" 2>/dev/null || true
chmod 600 "$target_key" 2>/dev/null || true
enable_pasarguard_ssl_env "$target_cert" "$target_key" "$ca_type"
colorized_echo green "Custom SSL certificate configured successfully."
return 0
}
enable_pasarguard_ssl_env() {
local cert_file="$1"
local key_file="$2"
local ca_type="${3:-public}"
set_or_uncomment_env_var "UVICORN_SSL_CERTFILE" "$cert_file" true "$ENV_FILE"
set_or_uncomment_env_var "UVICORN_SSL_KEYFILE" "$key_file" true "$ENV_FILE"
set_or_uncomment_env_var "UVICORN_SSL_CA_TYPE" "$ca_type" true "$ENV_FILE"
}
disable_pasarguard_ssl_env() {
comment_out_env_var "UVICORN_SSL_CERTFILE" "$ENV_FILE"
comment_out_env_var "UVICORN_SSL_KEYFILE" "$ENV_FILE"
comment_out_env_var "UVICORN_SSL_CA_TYPE" "$ENV_FILE"
}
setup_pasarguard_ssl_during_install() {
local ssl_mode="$1"
local ssl_domain="$2"
local ssl_http_port="$3"
local ssl_choice=""
local detected_ipv4=""
local input_ipv4=""
local input_ipv6=""
local custom_cert=""
local custom_key=""
local custom_ca_choice=""
local custom_ca_type="public"
if [ "$ssl_mode" = "disabled" ]; then
disable_pasarguard_ssl_env
colorized_echo yellow "Skipping SSL setup (--no-ssl). PasarGuard will bind to localhost only."
return 0
fi
if ! [[ "$ssl_http_port" =~ ^[0-9]+$ ]] || [ "$ssl_http_port" -lt 1 ] || [ "$ssl_http_port" -gt 65535 ]; then
colorized_echo red "Invalid SSL HTTP challenge port: ${ssl_http_port}"
return 1
fi
if [ "$ssl_mode" = "domain" ] && [ -n "$ssl_domain" ]; then
ssl_choice="1"
else
colorized_echo cyan "Choose SSL setup method for panel installation:"
colorized_echo green " 1) Let's Encrypt Domain certificate"
colorized_echo green " 2) Let's Encrypt IP certificate (short-lived)"
colorized_echo green " 3) Custom certificate + key paths"
colorized_echo yellow " 4) No SSL"
colorized_echo yellow "Port 80 (or configured --ssl-http-port) must be reachable for Let's Encrypt."
read -p "Select SSL option [1-4] (default: 1): " ssl_choice
ssl_choice="${ssl_choice// /}"
[ -z "$ssl_choice" ] && ssl_choice="1"
fi
case "$ssl_choice" in
1)
while [ -z "$ssl_domain" ]; do
read -p "Enter domain for SSL certificate (example: panel.example.com): " ssl_domain
ssl_domain="${ssl_domain// /}"
if [ -z "$ssl_domain" ]; then
colorized_echo red "Domain cannot be empty."
continue
fi
if ! is_domain "$ssl_domain"; then
colorized_echo red "Invalid domain format: ${ssl_domain}"
ssl_domain=""
fi
done
if setup_ssl_certificate "$ssl_domain" "$ssl_http_port"; then
enable_pasarguard_ssl_env "${DATA_DIR}/certs/${ssl_domain}/fullchain.pem" "${DATA_DIR}/certs/${ssl_domain}/privkey.pem" "public"
colorized_echo green "SSL enabled for https://${ssl_domain}:8000/dashboard/"
return 0
fi
;;
2)
detected_ipv4=$(get_public_ipv4 || true)
if [ -n "$detected_ipv4" ]; then
read -p "Enter IPv4 for SSL certificate (default: ${detected_ipv4}): " input_ipv4
input_ipv4="${input_ipv4// /}"
[ -z "$input_ipv4" ] && input_ipv4="$detected_ipv4"
else
read -p "Enter IPv4 for SSL certificate: " input_ipv4
input_ipv4="${input_ipv4// /}"
fi
if ! is_ipv4 "$input_ipv4"; then
colorized_echo red "Invalid IPv4 address: ${input_ipv4}"
disable_pasarguard_ssl_env
colorized_echo yellow "Continuing without SSL."
return 0
fi
read -p "Enter IPv6 for SSL certificate (optional, press Enter to skip): " input_ipv6
input_ipv6="${input_ipv6// /}"
if [ -n "$input_ipv6" ] && ! is_ipv6 "$input_ipv6"; then
colorized_echo red "Invalid IPv6 address: ${input_ipv6}"
disable_pasarguard_ssl_env
colorized_echo yellow "Continuing without SSL."
return 0
fi
if setup_ip_ssl_certificate "$input_ipv4" "$input_ipv6" "$ssl_http_port"; then
enable_pasarguard_ssl_env "${DATA_DIR}/certs/ip/fullchain.pem" "${DATA_DIR}/certs/ip/privkey.pem" "public"
colorized_echo green "SSL enabled for https://${input_ipv4}:8000/dashboard/"
return 0
fi
;;
3)
while true; do
read -p "Enter full path to certificate file (crt/pem/fullchain): " custom_cert
custom_cert=$(echo "$custom_cert" | tr -d '"' | tr -d "'" | xargs)
if [ -f "$custom_cert" ] && [ -r "$custom_cert" ] && [ -s "$custom_cert" ]; then
break
fi
colorized_echo red "Certificate file not found/readable: $custom_cert"
done
while true; do
read -p "Enter full path to private key file (key/pem): " custom_key
custom_key=$(echo "$custom_key" | tr -d '"' | tr -d "'" | xargs)
if [ -f "$custom_key" ] && [ -r "$custom_key" ] && [ -s "$custom_key" ]; then
break
fi
colorized_echo red "Private key file not found/readable: $custom_key"
done
read -p "Is this certificate from a public CA? [Y/n]: " custom_ca_choice
if [[ -n "$custom_ca_choice" && ! "$custom_ca_choice" =~ ^[Yy]$ ]]; then
custom_ca_type="private"
fi
if configure_custom_ssl_certificate "$custom_cert" "$custom_key" "$custom_ca_type"; then
colorized_echo green "SSL enabled from custom certificate files."
return 0
fi
;;
4)
disable_pasarguard_ssl_env
colorized_echo yellow "Continuing without SSL."
return 0
;;
*)
disable_pasarguard_ssl_env
colorized_echo yellow "Invalid SSL option. Continuing without SSL."
return 0
;;
esac
disable_pasarguard_ssl_env
colorized_echo yellow "SSL setup failed. Continuing without SSL. You can configure SSL later in ${ENV_FILE}."
return 0
}
compose_service_exists() {
local service_name="$1"
[ -z "$service_name" ] && return 1
$COMPOSE -f "$COMPOSE_FILE" -p "$APP_NAME" config --services 2>/dev/null | grep -Fxq "$service_name"
}
list_pasarguard_app_services() {
local detected_services=""
detected_services=$($COMPOSE -f "$COMPOSE_FILE" -p "$APP_NAME" config 2>/dev/null | awk '
BEGIN { in_services = 0; service = ""; is_app = 0 }
function flush_service() {
if (service != "" && is_app) {
print service
}
}
/^services:[[:space:]]*$/ {
in_services = 1
next
}
in_services && /^[^[:space:]]/ {
flush_service()
in_services = 0
service = ""
is_app = 0
next
}
!in_services {
next
}
/^ [A-Za-z0-9_.-]+:[[:space:]]*$/ {
flush_service()
service = $0
sub(/^ /, "", service)
sub(/:[[:space:]]*$/, "", service)
is_app = 0
next
}
/^[[:space:]]+image:[[:space:]]*pasarguard\/panel([:@].*)?$/ {
is_app = 1
next
}
/^[[:space:]]+ROLE:[[:space:]]*(backend|node|scheduler)([[:space:]]|$)/ {
is_app = 1
next
}
/^[[:space:]]+-[[:space:]]*ROLE=(backend|node|scheduler)([[:space:]]|$)/ {
is_app = 1
next
}
END {
flush_service()
}
' 2>/dev/null || true)
if [ -n "$detected_services" ]; then
echo "$detected_services"
return 0
fi
for candidate in panel pasarguard node-worker scheduler; do
if compose_service_exists "$candidate"; then
echo "$candidate"
fi
done
}
detect_pasarguard_backend_service() {
local service_name=""
for candidate in panel pasarguard; do
if compose_service_exists "$candidate"; then
echo "$candidate"
return 0
fi
done
service_name=$($COMPOSE -f "$COMPOSE_FILE" -p "$APP_NAME" config 2>/dev/null | awk '
BEGIN { in_services = 0; service = ""; is_backend = 0 }
function flush_service() {
if (service != "" && is_backend) {
print service
exit
}
}
/^services:[[:space:]]*$/ {
in_services = 1
next
}
in_services && /^[^[:space:]]/ {
flush_service()
in_services = 0
next
}
!in_services {
next
}
/^ [A-Za-z0-9_.-]+:[[:space:]]*$/ {
flush_service()
service = $0
sub(/^ /, "", service)
sub(/:[[:space:]]*$/, "", service)
is_backend = 0
next
}
/^[[:space:]]+ROLE:[[:space:]]*backend([[:space:]]|$)/ {
is_backend = 1
next
}
/^[[:space:]]+-[[:space:]]*ROLE=backend([[:space:]]|$)/ {
is_backend = 1
next
}
END {
if (service != "" && is_backend) {
print service
}
}
' | head -n 1)
if [ -n "$service_name" ]; then
echo "$service_name"
return 0
fi
service_name=$(list_pasarguard_app_services | head -n 1)
if [ -n "$service_name" ]; then
echo "$service_name"
return 0
fi
return 1
}
stop_pasarguard_app_services() {
local services
services=$(list_pasarguard_app_services | xargs)
[ -z "$services" ] && services="pasarguard"
$COMPOSE -f "$COMPOSE_FILE" -p "$APP_NAME" stop $services 2>/dev/null || true
}
start_pasarguard_app_services() {
local services
services=$(list_pasarguard_app_services | xargs)
[ -z "$services" ] && services="pasarguard"
$COMPOSE -f "$COMPOSE_FILE" -p "$APP_NAME" start $services 2>/dev/null || true
}
find_container() {
local db_type=$1
local container_name=""
detect_compose
case $db_type in
mariadb)
container_name=$($COMPOSE -f "$COMPOSE_FILE" -p "$APP_NAME" ps -q mariadb 2>/dev/null || true)
[ -z "$container_name" ] && container_name=$($COMPOSE -f "$COMPOSE_FILE" -p "$APP_NAME" ps --format json mariadb 2>/dev/null | jq -r '.Name' 2>/dev/null | head -n 1 || true)
[ -z "$container_name" ] && container_name=$(docker ps --filter "name=${APP_NAME}" --filter "name=mariadb" --format '{{.ID}}' 2>/dev/null | head -n 1 || true)
[ -z "$container_name" ] && container_name="mariadb"
;;
mysql)
container_name=$($COMPOSE -f "$COMPOSE_FILE" -p "$APP_NAME" ps -q mysql 2>/dev/null || true)
[ -z "$container_name" ] && container_name=$($COMPOSE -f "$COMPOSE_FILE" -p "$APP_NAME" ps -q mariadb 2>/dev/null || true)
[ -z "$container_name" ] && container_name=$($COMPOSE -f "$COMPOSE_FILE" -p "$APP_NAME" ps --format json mysql mariadb 2>/dev/null | jq -r 'if type == "array" then .[] else . end | .Name' 2>/dev/null | head -n 1 || true)
[ -z "$container_name" ] && container_name=$(docker ps --filter "name=${APP_NAME}" --filter "name=mysql" --format '{{.ID}}' 2>/dev/null | head -n 1 || true)
[ -z "$container_name" ] && container_name=$(docker ps --filter "name=${APP_NAME}" --filter "name=mariadb" --format '{{.ID}}' 2>/dev/null | head -n 1 || true)
[ -z "$container_name" ] && container_name="mysql"
;;
postgresql|timescaledb)
container_name=$($COMPOSE -f "$COMPOSE_FILE" -p "$APP_NAME" ps -q timescaledb 2>/dev/null || true)
[ -z "$container_name" ] && container_name=$($COMPOSE -f "$COMPOSE_FILE" -p "$APP_NAME" ps -q postgresql 2>/dev/null || true)
[ -z "$container_name" ] && container_name=$($COMPOSE -f "$COMPOSE_FILE" -p "$APP_NAME" ps --format json timescaledb postgresql 2>/dev/null | jq -r 'if type == "array" then .[] else . end | .Name' 2>/dev/null | head -n 1 || true)
[ -z "$container_name" ] && container_name="${APP_NAME}-timescaledb-1"
;;
esac
echo "$container_name"
}
check_container() {
local container_name=$1
local db_type=$2
local actual_container=""
if docker inspect "$container_name" >/dev/null 2>&1; then
actual_container="$container_name"
else
case $db_type in
mariadb)
actual_container=$($COMPOSE -f "$COMPOSE_FILE" -p "$APP_NAME" ps -q mariadb 2>/dev/null || true)
[ -z "$actual_container" ] && [ -f "$COMPOSE_FILE" ] && actual_container="${APP_NAME}-mariadb-1"
;;
mysql)
actual_container=$($COMPOSE -f "$COMPOSE_FILE" -p "$APP_NAME" ps -q mysql 2>/dev/null || true)
[ -z "$actual_container" ] && actual_container=$($COMPOSE -f "$COMPOSE_FILE" -p "$APP_NAME" ps -q mariadb 2>/dev/null || true)
[ -z "$actual_container" ] && [ -f "$COMPOSE_FILE" ] && actual_container="${APP_NAME}-mysql-1"
;;
postgresql|timescaledb)
actual_container=$($COMPOSE -f "$COMPOSE_FILE" -p "$APP_NAME" ps -q postgresql 2>/dev/null || true)
[ -z "$actual_container" ] && actual_container=$($COMPOSE -f "$COMPOSE_FILE" -p "$APP_NAME" ps -q timescaledb 2>/dev/null || true)
[ -z "$actual_container" ] && [ -f "$COMPOSE_FILE" ] && actual_container="${APP_NAME}-postgresql-1"
;;
esac
fi
[ -z "$actual_container" ] && { echo ""; return 1; }
container_name="$actual_container"
docker ps --filter "id=${container_name}" --format '{{.ID}}' 2>/dev/null | grep -q . || \
docker ps --filter "name=${container_name}" --format '{{.Names}}' 2>/dev/null | grep -q . || \
docker ps --format '{{.Names}}' 2>/dev/null | grep -qE "^${container_name}$|/${container_name}$" || \
docker ps --format '{{.ID}}' 2>/dev/null | grep -q "^${container_name}" || { echo ""; return 1; }
echo "$container_name"
return 0
}
verify_and_start_container() {
local container_name=$1
local db_type=$2
local actual_container=""
if docker inspect "$container_name" >/dev/null 2>&1; then
actual_container="$container_name"
else
case $db_type in
mariadb)
actual_container=$($COMPOSE -f "$COMPOSE_FILE" -p "$APP_NAME" ps -q mariadb 2>/dev/null || true)
[ -z "$actual_container" ] && [ -f "$COMPOSE_FILE" ] && actual_container="${APP_NAME}-mariadb-1"
;;
mysql)
actual_container=$($COMPOSE -f "$COMPOSE_FILE" -p "$APP_NAME" ps -q mysql 2>/dev/null || true)
[ -z "$actual_container" ] && actual_container=$($COMPOSE -f "$COMPOSE_FILE" -p "$APP_NAME" ps -q mariadb 2>/dev/null || true)
[ -z "$actual_container" ] && [ -f "$COMPOSE_FILE" ] && actual_container="${APP_NAME}-mysql-1"
;;
postgresql|timescaledb)
actual_container=$($COMPOSE -f "$COMPOSE_FILE" -p "$APP_NAME" ps -q postgresql 2>/dev/null || true)
[ -z "$actual_container" ] && actual_container=$($COMPOSE -f "$COMPOSE_FILE" -p "$APP_NAME" ps -q timescaledb 2>/dev/null || true)
[ -z "$actual_container" ] && [ -f "$COMPOSE_FILE" ] && actual_container="${APP_NAME}-postgresql-1"
;;
esac
fi
[ -z "$actual_container" ] && { echo ""; return 1; }
container_name="$actual_container"
local container_running=false
docker ps --filter "id=${container_name}" --format '{{.ID}}' 2>/dev/null | grep -q . && container_running=true || \
docker ps --filter "name=${container_name}" --format '{{.Names}}' 2>/dev/null | grep -q . && container_running=true || \
docker ps --format '{{.Names}}' 2>/dev/null | grep -qE "^${container_name}$|/${container_name}$" && container_running=true || \
docker ps --format '{{.ID}}' 2>/dev/null | grep -q "^${container_name}" && container_running=true
if [ "$container_running" = false ]; then
colorized_echo yellow "Database container '$container_name' is not running. Attempting to start it..."
docker start "$container_name" >/dev/null 2>&1 || \
$COMPOSE -f "$COMPOSE_FILE" -p "$APP_NAME" start "${db_type%%|*}" 2>/dev/null || true
sleep 2
docker ps --filter "id=${container_name}" --format '{{.ID}}' 2>/dev/null | grep -q . && container_running=true || \
docker ps --filter "name=${container_name}" --format '{{.Names}}' 2>/dev/null | grep -q . && container_running=true
fi
[ "$container_running" = true ] && { echo "$container_name"; return 0; } || { echo ""; return 1; }
}
install_pasarguard_script() {
FETCH_REPO="PasarGuard/scripts"
colorized_echo blue "Installing pasarguard script"
install_shared_libs_from_repo "$FETCH_REPO" common.sh system.sh docker.sh github.sh env.sh pasarguard-backup.sh pasarguard-restore.sh
github_install_script_from_repo "$FETCH_REPO" "pasarguard.sh" "pasarguard"
colorized_echo green "pasarguard script installed successfully"
}
is_pasarguard_installed() {
if [ -d $APP_DIR ]; then
return 0
else
return 1
fi
}
set_pasarguard_panel_image() {
local target_image="$1"
local service_name=""
local image_name=""
local updated_any=false
while IFS= read -r service_name; do
[ -z "$service_name" ] && continue
image_name=$(yq eval -r ".services.\"${service_name}\".image // \"\"" "$COMPOSE_FILE" 2>/dev/null)
if [[ "$image_name" =~ ^pasarguard/panel([:@].*)?$ ]]; then
yq -i ".services.\"${service_name}\".image = \"${target_image}\"" "$COMPOSE_FILE"
updated_any=true
fi
done < <(yq eval -r '.services | keys | .[]' "$COMPOSE_FILE" 2>/dev/null || true)
if [ "$updated_any" = false ]; then
for service_name in panel pasarguard node-worker scheduler; do
if yq eval -e ".services.\"${service_name}\"" "$COMPOSE_FILE" >/dev/null 2>&1; then
yq -i ".services.\"${service_name}\".image = \"${target_image}\"" "$COMPOSE_FILE"
updated_any=true
fi
done
fi
if [ "$updated_any" = false ]; then
yq -i ".services.pasarguard.image = \"${target_image}\"" "$COMPOSE_FILE"
fi
}
install_pasarguard() {
local pasarguard_version=$1
local major_version=$2
local database_type=$3
FILES_URL_PREFIX="https://raw.githubusercontent.com/pasarguard/panel"
COMPOSE_FILES_URL_PREFIX="https://raw.githubusercontent.com/pasarguard/scripts/main/docker-compose"
mkdir -p "$DATA_DIR"
mkdir -p "$APP_DIR"
colorized_echo blue "Fetching .env file"
curl -sL "$FILES_URL_PREFIX/main/.env.example" -o "$APP_DIR/.env"
colorized_echo green "File saved in $APP_DIR/.env"
if [[ "$database_type" =~ ^(mysql|mariadb|postgresql|timescaledb)$ ]]; then
case "$database_type" in
mysql) db_name="MySQL" ;;
mariadb) db_name="MariaDB" ;;
timescaledb) db_name="TimeScaleDB" ;;
*) db_name="PostgreSQL" ;;
esac
echo "----------------------------"
colorized_echo red "Using $db_name as database"
echo "----------------------------"
colorized_echo blue "Fetching compose file for pasarguard+$db_name"
curl -sL "$COMPOSE_FILES_URL_PREFIX/pasarguard-$database_type.yml" -o "$COMPOSE_FILE"
# Comment out the SQLite line
sed -i 's~^SQLALCHEMY_DATABASE_URL = "sqlite~#&~' "$APP_DIR/.env"
DB_NAME="pasarguard"
DB_USER="pasarguard"
prompt_for_db_password
echo "" >>"$ENV_FILE"
echo "# Database configuration" >>"$ENV_FILE"
echo "DB_NAME=\"${DB_NAME}\"" >>"$ENV_FILE"
echo "DB_USER=\"${DB_USER}\"" >>"$ENV_FILE"
echo "DB_PASSWORD=\"${DB_PASSWORD}\"" >>"$ENV_FILE"
if [[ "$database_type" == "postgresql" || "$database_type" == "timescaledb" ]]; then
DB_PORT="6432"
prompt_for_pgadmin_password
echo "" >>"$ENV_FILE"
echo "# PGAdmin configuration" >>"$ENV_FILE"
echo "PGADMIN_EMAIL=\"pg@github.io\"" >>"$ENV_FILE"
echo "PGADMIN_PASSWORD=\"${PGADMIN_PASSWORD}\"" >>"$ENV_FILE"
else
colorized_echo green "phpMyAdmin address: 0.0.0.0:8010"
DB_PORT="3306"
MYSQL_ROOT_PASSWORD=$(LC_ALL=C tr -dc 'A-Za-z0-9' </dev/urandom | head -c 20 || true)
echo "MYSQL_ROOT_PASSWORD=\"$MYSQL_ROOT_PASSWORD\"" >>"$ENV_FILE"
fi
if [[ "$database_type" =~ ^(postgresql|timescaledb)$ ]]; then
if [ "$major_version" -lt 1 ]; then
colorized_echo red "Error: --database $database_type is only supported in v1.0.0 and later."
colorized_echo yellow "Use --pre-release or --version v1.x.y, or choose mysql/mariadb/sqlite for v0.x."
exit 1
fi
db_driver_scheme="postgresql+asyncpg"
else
db_driver_scheme="mysql+asyncmy"
fi
SQLALCHEMY_DATABASE_URL="${db_driver_scheme}://${DB_USER}:${DB_PASSWORD}@127.0.0.1:${DB_PORT}/${DB_NAME}"