-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathmarzban-node.sh
executable file
·1084 lines (931 loc) · 30.7 KB
/
marzban-node.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
set -e
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
install|update|uninstall|up|down|restart|status|logs|core-update|install-script|uninstall-script|edit)
COMMAND="$1"
shift # past argument
;;
--name)
if [[ "$COMMAND" == "install" || "$COMMAND" == "install-script" ]]; then
APP_NAME="$2"
shift # past argument
else
echo "Error: --name parameter is only allowed with 'install' or 'install-script' commands."
exit 1
fi
shift # past value
;;
*)
shift # past unknown argument
;;
esac
done
# Fetch IP address from ipinfo.io API
NODE_IP=$(curl -s -4 ifconfig.io)
# If the IPv4 retrieval is empty, attempt to retrieve the IPv6 address
if [ -z "$NODE_IP" ]; then
NODE_IP=$(curl -s -6 ifconfig.io)
fi
if [[ "$COMMAND" == "install" || "$COMMAND" == "install-script" ]] && [ -z "$APP_NAME" ]; then
APP_NAME="marzban-node"
fi
# Set script name if APP_NAME is not set
if [ -z "$APP_NAME" ]; then
SCRIPT_NAME=$(basename "$0")
APP_NAME="${SCRIPT_NAME%.*}"
fi
INSTALL_DIR="/opt"
if [ -d "$INSTALL_DIR/$APP_NAME" ]; then
APP_DIR="$INSTALL_DIR/$APP_NAME"
elif [ -d "$INSTALL_DIR/Marzban-node" ]; then
APP_DIR="$INSTALL_DIR/Marzban-node"
else
APP_DIR="$INSTALL_DIR/$APP_NAME"
fi
DATA_DIR="/var/lib/$APP_NAME"
DATA_MAIN_DIR="/var/lib/$APP_NAME"
COMPOSE_FILE="$APP_DIR/docker-compose.yml"
LAST_XRAY_CORES=5
CERT_FILE="$DATA_DIR/cert.pem"
FETCH_REPO="Gozargah/Marzban-scripts"
SCRIPT_URL="https://github.com/$FETCH_REPO/raw/master/marzban-node.sh"
colorized_echo() {
local color=$1
local text=$2
local style=${3:-0} # Default style is normal
case $color in
"red")
printf "\e[${style};91m${text}\e[0m\n"
;;
"green")
printf "\e[${style};92m${text}\e[0m\n"
;;
"yellow")
printf "\e[${style};93m${text}\e[0m\n"
;;
"blue")
printf "\e[${style};94m${text}\e[0m\n"
;;
"magenta")
printf "\e[${style};95m${text}\e[0m\n"
;;
"cyan")
printf "\e[${style};96m${text}\e[0m\n"
;;
*)
echo "${text}"
;;
esac
}
check_running_as_root() {
if [ "$(id -u)" != "0" ]; then
colorized_echo red "This command must be run as root."
exit 1
fi
}
detect_os() {
# Detect the operating system
if [ -f /etc/lsb-release ]; then
OS=$(lsb_release -si)
elif [ -f /etc/os-release ]; then
OS=$(awk -F= '/^NAME/{print $2}' /etc/os-release | tr -d '"')
elif [ -f /etc/redhat-release ]; then
OS=$(cat /etc/redhat-release | awk '{print $1}')
elif [ -f /etc/arch-release ]; then
OS="Arch"
else
colorized_echo red "Unsupported operating system"
exit 1
fi
}
detect_and_update_package_manager() {
colorized_echo blue "Updating package manager"
if [[ "$OS" == "Ubuntu"* ]] || [[ "$OS" == "Debian"* ]]; then
PKG_MANAGER="apt-get"
$PKG_MANAGER update -qq >/dev/null 2>&1
elif [[ "$OS" == "CentOS"* ]] || [[ "$OS" == "AlmaLinux"* ]]; then
PKG_MANAGER="yum"
$PKG_MANAGER update -y -q >/dev/null 2>&1
$PKG_MANAGER install -y -q epel-release >/dev/null 2>&1
elif [[ "$OS" == "Fedora"* ]]; then
PKG_MANAGER="dnf"
$PKG_MANAGER update -q -y >/dev/null 2>&1
elif [[ "$OS" == "Arch"* ]]; then
PKG_MANAGER="pacman"
$PKG_MANAGER -Sy --noconfirm --quiet >/dev/null 2>&1
elif [[ "$OS" == "openSUSE"* ]]; then
PKG_MANAGER="zypper"
$PKG_MANAGER refresh --quiet >/dev/null 2>&1
else
colorized_echo red "Unsupported operating system"
exit 1
fi
}
detect_compose() {
# Check if docker compose command exists
if docker compose >/dev/null 2>&1; then
COMPOSE='docker compose'
elif docker-compose >/dev/null 2>&1; then
COMPOSE='docker-compose'
else
colorized_echo red "docker compose not found"
exit 1
fi
}
install_package () {
if [ -z "$PKG_MANAGER" ]; then
detect_and_update_package_manager
fi
PACKAGE=$1
colorized_echo blue "Installing $PACKAGE"
if [[ "$OS" == "Ubuntu"* ]] || [[ "$OS" == "Debian"* ]]; then
$PKG_MANAGER -y -qq install "$PACKAGE" >/dev/null 2>&1
elif [[ "$OS" == "CentOS"* ]] || [[ "$OS" == "AlmaLinux"* ]]; then
$PKG_MANAGER install -y -q "$PACKAGE" >/dev/null 2>&1
elif [[ "$OS" == "Fedora"* ]]; then
$PKG_MANAGER install -y -q "$PACKAGE" >/dev/null 2>&1
elif [[ "$OS" == "Arch"* ]]; then
$PKG_MANAGER -S --noconfirm --quiet "$PACKAGE" >/dev/null 2>&1
elif [[ "$OS" == "openSUSE"* ]]; then
PKG_MANAGER="zypper"
$PKG_MANAGER --quiet install -y "$PACKAGE" >/dev/null 2>&1
else
colorized_echo red "Unsupported operating system"
exit 1
fi
}
install_docker() {
# Install Docker and Docker Compose using the official installation script
colorized_echo blue "Installing Docker"
curl -fsSL https://get.docker.com | sh
colorized_echo green "Docker installed successfully"
}
install_marzban_node_script() {
colorized_echo blue "Installing marzban script"
TARGET_PATH="/usr/local/bin/$APP_NAME"
curl -sSL $SCRIPT_URL -o $TARGET_PATH
sed -i "s/^APP_NAME=.*/APP_NAME=\"$APP_NAME\"/" $TARGET_PATH
chmod 755 $TARGET_PATH
colorized_echo green "Marzban-node script installed successfully at $TARGET_PATH"
}
# Get a list of occupied ports
get_occupied_ports() {
if command -v ss &>/dev/null; then
OCCUPIED_PORTS=$(ss -tuln | awk '{print $5}' | grep -Eo '[0-9]+$' | sort | uniq)
elif command -v netstat &>/dev/null; then
OCCUPIED_PORTS=$(netstat -tuln | awk '{print $4}' | grep -Eo '[0-9]+$' | sort | uniq)
else
colorized_echo yellow "Neither ss nor netstat found. Attempting to install net-tools."
detect_os
install_package net-tools
if command -v netstat &>/dev/null; then
OCCUPIED_PORTS=$(netstat -tuln | awk '{print $4}' | grep -Eo '[0-9]+$' | sort | uniq)
else
colorized_echo red "Failed to install net-tools. Please install it manually."
exit 1
fi
fi
}
# Function to check if a port is occupied
is_port_occupied() {
if echo "$OCCUPIED_PORTS" | grep -q -w "$1"; then
return 0
else
return 1
fi
}
install_marzban_node() {
# Fetch releases
mkdir -p "$DATA_DIR"
mkdir -p "$APP_DIR"
mkdir -p "$DATA_MAIN_DIR"
# Проверка на существование файла перед его очисткой
if [ -f "$CERT_FILE" ]; then
>"$CERT_FILE"
fi
# Function to print information to the user
print_info() {
echo -e "\033[1;34m$1\033[0m"
}
# Prompt the user to input the certificate
echo -e "Please paste the content of the Client Certificate, press ENTER on a new line when finished: "
while IFS= read -r line; do
if [[ -z $line ]]; then
break
fi
echo "$line" >>"$CERT_FILE"
done
print_info "Certificate saved to $CERT_FILE"
# Prompt the user to choose REST or another protocol
read -p "Do you want to use REST protocol? (Y/n): " -r use_rest
# Default to "Y" if the user just presses ENTER
if [[ -z "$use_rest" || "$use_rest" =~ ^[Yy]$ ]]; then
USE_REST=true
else
USE_REST=false
fi
get_occupied_ports
# Prompt the user to enter ports with occupation check
while true; do
read -p "Enter the SERVICE_PORT (default 62050): " -r SERVICE_PORT
if [[ -z "$SERVICE_PORT" ]]; then
SERVICE_PORT=62050
fi
if [[ "$SERVICE_PORT" -ge 1 && "$SERVICE_PORT" -le 65535 ]]; then
if is_port_occupied "$SERVICE_PORT"; then
colorized_echo red "Port $SERVICE_PORT is already in use. Please enter another port."
else
break
fi
else
colorized_echo red "Invalid port. Please enter a port between 1 and 65535."
fi
done
while true; do
read -p "Enter the XRAY_API_PORT (default 62051): " -r XRAY_API_PORT
if [[ -z "$XRAY_API_PORT" ]]; then
XRAY_API_PORT=62051
fi
if [[ "$XRAY_API_PORT" -ge 1 && "$XRAY_API_PORT" -le 65535 ]]; then
if is_port_occupied "$XRAY_API_PORT"; then
colorized_echo red "Port $XRAY_API_PORT is already in use. Please enter another port."
elif [[ "$XRAY_API_PORT" -eq "$SERVICE_PORT" ]]; then
colorized_echo red "Port $XRAY_API_PORT cannot be the same as SERVICE_PORT. Please enter another port."
else
break
fi
else
colorized_echo red "Invalid port. Please enter a port between 1 and 65535."
fi
done
colorized_echo blue "Generating compose file"
# Write content to the file
cat > "$COMPOSE_FILE" <<EOL
services:
marzban-node:
container_name: $APP_NAME
image: gozargah/marzban-node:latest
restart: always
network_mode: host
environment:
SSL_CLIENT_CERT_FILE: "/var/lib/marzban-node/cert.pem"
SERVICE_PORT: "$SERVICE_PORT"
XRAY_API_PORT: "$XRAY_API_PORT"
EOL
# Add SERVICE_PROTOCOL line only if REST is selected
if [[ "$USE_REST" = true ]]; then
cat >> "$COMPOSE_FILE" <<EOL
SERVICE_PROTOCOL: "rest"
EOL
fi
cat >> "$COMPOSE_FILE" <<EOL
volumes:
- $DATA_MAIN_DIR:/var/lib/marzban
- $DATA_DIR:/var/lib/marzban-node
EOL
colorized_echo green "File saved in $APP_DIR/docker-compose.yml"
}
uninstall_marzban_node_script() {
if [ -f "/usr/local/bin/$APP_NAME" ]; then
colorized_echo yellow "Removing marzban-node script"
rm "/usr/local/bin/$APP_NAME"
fi
}
uninstall_marzban_node() {
if [ -d "$APP_DIR" ]; then
colorized_echo yellow "Removing directory: $APP_DIR"
rm -r "$APP_DIR"
fi
}
uninstall_marzban_node_docker_images() {
images=$(docker images | grep marzban-node | awk '{print $3}')
if [ -n "$images" ]; then
colorized_echo yellow "Removing Docker images of Marzban-node"
for image in $images; do
if docker rmi "$image" >/dev/null 2>&1; then
colorized_echo yellow "Image $image removed"
fi
done
fi
}
uninstall_marzban_node_data_files() {
if [ -d "$DATA_DIR" ]; then
colorized_echo yellow "Removing directory: $DATA_DIR"
rm -r "$DATA_DIR"
fi
}
up_marzban_node() {
$COMPOSE -f $COMPOSE_FILE -p "$APP_NAME" up -d --remove-orphans
}
down_marzban_node() {
$COMPOSE -f $COMPOSE_FILE -p "$APP_NAME" down
}
show_marzban_node_logs() {
$COMPOSE -f $COMPOSE_FILE -p "$APP_NAME" logs
}
follow_marzban_node_logs() {
$COMPOSE -f $COMPOSE_FILE -p "$APP_NAME" logs -f
}
update_marzban_node_script() {
colorized_echo blue "Updating marzban-node script"
curl -sSL $SCRIPT_URL | install -m 755 /dev/stdin /usr/local/bin/$APP_NAME
colorized_echo green "marzban-node script updated successfully"
}
update_marzban_node() {
$COMPOSE -f $COMPOSE_FILE -p "$APP_NAME" pull
}
is_marzban_node_installed() {
if [ -d $APP_DIR ]; then
return 0
else
return 1
fi
}
is_marzban_node_up() {
if [ -z "$($COMPOSE -f $COMPOSE_FILE ps -q -a)" ]; then
return 1
else
return 0
fi
}
install_command() {
check_running_as_root
# Check if marzban is already installed
if is_marzban_node_installed; then
colorized_echo red "Marzban-node is already installed at $APP_DIR"
read -p "Do you want to override the previous installation? (y/n) "
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
colorized_echo red "Aborted installation"
exit 1
fi
fi
detect_os
if ! command -v jq >/dev/null 2>&1; then
install_package jq
fi
if ! command -v curl >/dev/null 2>&1; then
install_package curl
fi
if ! command -v docker >/dev/null 2>&1; then
install_docker
fi
detect_compose
install_marzban_node_script
install_marzban_node
up_marzban_node
follow_marzban_node_logs
echo "Use your IP: $NODE_IP and defaults ports: $SERVICE_PORT and $XRAY_API_PORT to setup your Marzban Main Panel"
}
uninstall_command() {
check_running_as_root
# Check if marzban is installed
if ! is_marzban_node_installed; then
colorized_echo red "Marzban-node not installed!"
exit 1
fi
read -p "Do you really want to uninstall Marzban-node? (y/n) "
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
colorized_echo red "Aborted"
exit 1
fi
detect_compose
if is_marzban_node_up; then
down_marzban_node
fi
uninstall_marzban_node_script
uninstall_marzban_node
uninstall_marzban_node_docker_images
read -p "Do you want to remove Marzban-node data files too ($DATA_DIR)? (y/n) "
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
colorized_echo green "Marzban-node uninstalled successfully"
else
uninstall_marzban_node_data_files
colorized_echo green "Marzban-node uninstalled successfully"
fi
}
up_command() {
help() {
colorized_echo red "Usage: marzban-node up [options]"
echo ""
echo "OPTIONS:"
echo " -h, --help display this help message"
echo " -n, --no-logs do not follow logs after starting"
}
local no_logs=false
while [[ "$#" -gt 0 ]]; do
case "$1" in
-n|--no-logs)
no_logs=true
;;
-h|--help)
help
exit 0
;;
*)
echo "Error: Invalid option: $1" >&2
help
exit 0
;;
esac
shift
done
# Check if marzban-node is installed
if ! is_marzban_node_installed; then
colorized_echo red "Marzban-node's not installed!"
exit 1
fi
detect_compose
if is_marzban_node_up; then
colorized_echo red "Marzban-node's already up"
exit 1
fi
up_marzban_node
if [ "$no_logs" = false ]; then
follow_marzban_node_logs
fi
}
down_command() {
# Check if marzban-node is installed
if ! is_marzban_node_installed; then
colorized_echo red "Marzban-node not installed!"
exit 1
fi
detect_compose
if ! is_marzban_node_up; then
colorized_echo red "Marzban-node already down"
exit 1
fi
down_marzban_node
}
restart_command() {
help() {
colorized_echo red "Usage: marzban-node restart [options]"
echo
echo "OPTIONS:"
echo " -h, --help display this help message"
echo " -n, --no-logs do not follow logs after starting"
}
local no_logs=false
while [[ "$#" -gt 0 ]]; do
case "$1" in
-n|--no-logs)
no_logs=true
;;
-h|--help)
help
exit 0
;;
*)
echo "Error: Invalid option: $1" >&2
help
exit 0
;;
esac
shift
done
# Check if marzban-node is installed
if ! is_marzban_node_installed; then
colorized_echo red "Marzban-node not installed!"
exit 1
fi
detect_compose
down_marzban_node
up_marzban_node
}
status_command() {
# Check if marzban-node is installed
if ! is_marzban_node_installed; then
echo -n "Status: "
colorized_echo red "Not Installed"
exit 1
fi
detect_compose
if ! is_marzban_node_up; then
echo -n "Status: "
colorized_echo blue "Down"
exit 1
fi
echo -n "Status: "
colorized_echo green "Up"
json=$($COMPOSE -f $COMPOSE_FILE ps -a --format=json)
services=$(echo "$json" | jq -r 'if type == "array" then .[] else . end | .Service')
states=$(echo "$json" | jq -r 'if type == "array" then .[] else . end | .State')
# Print out the service names and statuses
for i in $(seq 0 $(expr $(echo $services | wc -w) - 1)); do
service=$(echo $services | cut -d' ' -f $(expr $i + 1))
state=$(echo $states | cut -d' ' -f $(expr $i + 1))
echo -n "- $service: "
if [ "$state" == "running" ]; then
colorized_echo green $state
else
colorized_echo red $state
fi
done
}
logs_command() {
help() {
colorized_echo red "Usage: marzban-node logs [options]"
echo ""
echo "OPTIONS:"
echo " -h, --help display this help message"
echo " -n, --no-follow do not show follow logs"
}
local no_follow=false
while [[ "$#" -gt 0 ]]; do
case "$1" in
-n|--no-follow)
no_follow=true
;;
-h|--help)
help
exit 0
;;
*)
echo "Error: Invalid option: $1" >&2
help
exit 0
;;
esac
shift
done
# Check if marzban is installed
if ! is_marzban_node_installed; then
colorized_echo red "Marzban-node's not installed!"
exit 1
fi
detect_compose
if ! is_marzban_node_up; then
colorized_echo red "Marzban-node is not up."
exit 1
fi
if [ "$no_follow" = true ]; then
show_marzban_node_logs
else
follow_marzban_node_logs
fi
}
update_command() {
check_running_as_root
# Check if marzban is installed
if ! is_marzban_node_installed; then
colorized_echo red "Marzban-node not installed!"
exit 1
fi
detect_compose
update_marzban_node_script
colorized_echo blue "Pulling latest version"
update_marzban_node
colorized_echo blue "Restarting Marzban-node services"
down_marzban_node
up_marzban_node
colorized_echo blue "Marzban-node updated successfully"
}
identify_the_operating_system_and_architecture() {
if [[ "$(uname)" == 'Linux' ]]; then
case "$(uname -m)" in
'i386' | 'i686')
ARCH='32'
;;
'amd64' | 'x86_64')
ARCH='64'
;;
'armv5tel')
ARCH='arm32-v5'
;;
'armv6l')
ARCH='arm32-v6'
grep Features /proc/cpuinfo | grep -qw 'vfp' || ARCH='arm32-v5'
;;
'armv7' | 'armv7l')
ARCH='arm32-v7a'
grep Features /proc/cpuinfo | grep -qw 'vfp' || ARCH='arm32-v5'
;;
'armv8' | 'aarch64')
ARCH='arm64-v8a'
;;
'mips')
ARCH='mips32'
;;
'mipsle')
ARCH='mips32le'
;;
'mips64')
ARCH='mips64'
lscpu | grep -q "Little Endian" && ARCH='mips64le'
;;
'mips64le')
ARCH='mips64le'
;;
'ppc64')
ARCH='ppc64'
;;
'ppc64le')
ARCH='ppc64le'
;;
'riscv64')
ARCH='riscv64'
;;
's390x')
ARCH='s390x'
;;
*)
echo "error: The architecture is not supported."
exit 1
;;
esac
else
echo "error: This operating system is not supported."
exit 1
fi
}
# Function to update the Xray core
get_xray_core() {
identify_the_operating_system_and_architecture
clear
validate_version() {
local version="$1"
local response=$(curl -s "https://api.github.com/repos/XTLS/Xray-core/releases/tags/$version")
if echo "$response" | grep -q '"message": "Not Found"'; then
echo "invalid"
else
echo "valid"
fi
}
print_menu() {
clear
echo -e "\033[1;32m==============================\033[0m"
echo -e "\033[1;32m Xray-core Installer \033[0m"
echo -e "\033[1;32m==============================\033[0m"
current_version=$(get_current_xray_core_version)
echo -e "\033[1;33m>>>> Current Xray-core version: \033[1;1m$current_version\033[0m"
echo -e "\033[1;32m==============================\033[0m"
echo -e "\033[1;33mAvailable Xray-core versions:\033[0m"
for ((i=0; i<${#versions[@]}; i++)); do
echo -e "\033[1;34m$((i + 1)):\033[0m ${versions[i]}"
done
echo -e "\033[1;32m==============================\033[0m"
echo -e "\033[1;35mM:\033[0m Enter a version manually"
echo -e "\033[1;31mQ:\033[0m Quit"
echo -e "\033[1;32m==============================\033[0m"
}
latest_releases=$(curl -s "https://api.github.com/repos/XTLS/Xray-core/releases?per_page=$LAST_XRAY_CORES")
versions=($(echo "$latest_releases" | grep -oP '"tag_name": "\K(.*?)(?=")'))
while true; do
print_menu
read -p "Choose a version to install (1-${#versions[@]}), or press M to enter manually, Q to quit: " choice
if [[ "$choice" =~ ^[1-9][0-9]*$ ]] && [ "$choice" -le "${#versions[@]}" ]; then
choice=$((choice - 1))
selected_version=${versions[choice]}
break
elif [ "$choice" == "M" ] || [ "$choice" == "m" ]; then
while true; do
read -p "Enter the version manually (e.g., v1.2.3): " custom_version
if [ "$(validate_version "$custom_version")" == "valid" ]; then
selected_version="$custom_version"
break 2
else
echo -e "\033[1;31mInvalid version or version does not exist. Please try again.\033[0m"
fi
done
elif [ "$choice" == "Q" ] || [ "$choice" == "q" ]; then
echo -e "\033[1;31mExiting.\033[0m"
exit 0
else
echo -e "\033[1;31mInvalid choice. Please try again.\033[0m"
sleep 2
fi
done
echo -e "\033[1;32mSelected version $selected_version for installation.\033[0m"
if ! dpkg -s unzip >/dev/null 2>&1; then
echo -e "\033[1;33mInstalling required packages...\033[0m"
detect_os
install_package unzip
fi
mkdir -p $DATA_MAIN_DIR/xray-core
cd $DATA_MAIN_DIR/xray-core
xray_filename="Xray-linux-$ARCH.zip"
xray_download_url="https://github.com/XTLS/Xray-core/releases/download/${selected_version}/${xray_filename}"
echo -e "\033[1;33mDownloading Xray-core version ${selected_version} in the background...\033[0m"
wget "${xray_download_url}" -q &
wait
echo -e "\033[1;33mExtracting Xray-core in the background...\033[0m"
unzip -o "${xray_filename}" >/dev/null 2>&1 &
wait
rm "${xray_filename}"
}
get_current_xray_core_version() {
XRAY_BINARY="$DATA_MAIN_DIR/xray-core/xray"
if [ -f "$XRAY_BINARY" ]; then
version_output=$("$XRAY_BINARY" -version 2>/dev/null)
if [ $? -eq 0 ]; then
version=$(echo "$version_output" | head -n1 | awk '{print $2}')
echo "$version"
return
fi
fi
# If local binary is not found or failed, check in the Docker container
CONTAINER_NAME="$APP_NAME"
if docker ps --format '{{.Names}}' | grep -q "^$CONTAINER_NAME$"; then
version_output=$(docker exec "$CONTAINER_NAME" xray -version 2>/dev/null)
if [ $? -eq 0 ]; then
# Extract the version number from the first line
version=$(echo "$version_output" | head -n1 | awk '{print $2}')
echo "$version (in container)"
return
fi
fi
echo "Not installed"
}
install_yq() {
if command -v yq &>/dev/null; then
colorized_echo green "yq is already installed."
return
fi
identify_the_operating_system_and_architecture
local base_url="https://github.com/mikefarah/yq/releases/latest/download"
local yq_binary=""
case "$ARCH" in
'64' | 'x86_64')
yq_binary="yq_linux_amd64"
;;
'arm32-v7a' | 'arm32-v6' | 'arm32-v5' | 'armv7l')
yq_binary="yq_linux_arm"
;;
'arm64-v8a' | 'aarch64')
yq_binary="yq_linux_arm64"
;;
'32' | 'i386' | 'i686')
yq_binary="yq_linux_386"
;;
*)
colorized_echo red "Unsupported architecture: $ARCH"
exit 1
;;
esac
local yq_url="${base_url}/${yq_binary}"
colorized_echo blue "Downloading yq from ${yq_url}..."
if ! command -v curl &>/dev/null && ! command -v wget &>/dev/null; then
colorized_echo yellow "Neither curl nor wget is installed. Attempting to install curl."
install_package curl || {
colorized_echo red "Failed to install curl. Please install curl or wget manually."
exit 1
}
fi
if command -v curl &>/dev/null; then
if curl -L "$yq_url" -o /usr/local/bin/yq; then
chmod +x /usr/local/bin/yq
colorized_echo green "yq installed successfully!"
else
colorized_echo red "Failed to download yq using curl. Please check your internet connection."
exit 1
fi
elif command -v wget &>/dev/null; then
if wget -O /usr/local/bin/yq "$yq_url"; then
chmod +x /usr/local/bin/yq
colorized_echo green "yq installed successfully!"
else
colorized_echo red "Failed to download yq using wget. Please check your internet connection."
exit 1
fi
fi
if ! echo "$PATH" | grep -q "/usr/local/bin"; then
export PATH="/usr/local/bin:$PATH"
fi
hash -r
if command -v yq &>/dev/null; then
colorized_echo green "yq is ready to use."
elif [ -x "/usr/local/bin/yq" ]; then
colorized_echo yellow "yq is installed at /usr/local/bin/yq but not found in PATH."
colorized_echo yellow "You can add /usr/local/bin to your PATH environment variable."
else
colorized_echo red "yq installation failed. Please try again or install manually."
exit 1
fi
}
update_core_command() {
check_running_as_root
get_xray_core
if ! command -v yq &>/dev/null; then
echo "yq is not installed. Installing yq..."
install_yq
fi
if ! grep -q 'XRAY_EXECUTABLE_PATH: "/var/lib/marzban-node/xray-core/xray"' "$COMPOSE_FILE"; then
yq eval '.services."marzban-node".environment.XRAY_EXECUTABLE_PATH = "/var/lib/marzban-node/xray-core/xray"' -i "$COMPOSE_FILE"
fi
if ! yq eval ".services.\"marzban-node\".volumes[] | select(. == \"${DATA_MAIN_DIR}:/var/lib/marzban-node\")" "$COMPOSE_FILE" &>/dev/null; then
yq eval ".services.\"marzban-node\".volumes += \"${DATA_MAIN_DIR}:/var/lib/marzban-node\"" -i "$COMPOSE_FILE"
fi
# Restart Marzban-node
colorized_echo red "Restarting Marzban-node..."
$APP_NAME restart -n
colorized_echo blue "Installation of XRAY-CORE version $selected_version completed."
}
check_editor() {
if [ -z "$EDITOR" ]; then
if command -v nano >/dev/null 2>&1; then
EDITOR="nano"
elif command -v vi >/dev/null 2>&1; then
EDITOR="vi"
else
detect_os
install_package nano
EDITOR="nano"
fi
fi
}
edit_command() {
detect_os
check_editor
if [ -f "$COMPOSE_FILE" ]; then
$EDITOR "$COMPOSE_FILE"
else
colorized_echo red "Compose file not found at $COMPOSE_FILE"
exit 1
fi
}
usage() {
colorized_echo blue "================================"
colorized_echo magenta " $APP_NAME Node CLI Help"
colorized_echo blue "================================"
colorized_echo cyan "Usage:"
echo " $APP_NAME [command]"