-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall-node.sh
More file actions
2004 lines (1765 loc) · 66.9 KB
/
install-node.sh
File metadata and controls
2004 lines (1765 loc) · 66.9 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
# Initialize logging
# Function to display help text
print_help() {
cat <<HELP_TEXT
Erebrus Node Installation Script
Usage: $0 [OPTIONS]
This script installs the Erebrus node software or its Xray component, including
binaries, configuration files, and a management script.
Options:
--xray-only Install only the Xray binary and management script, skipping
full node installation (dependencies and node binary).
Default: Full node installation.
-h, --help Display this help message and exit.
Examples:
$0 Installs the full Erebrus node (default).
$0 --xray-only Installs only the Xray binary and management script.
$0 --help Shows this help text.
For more details, refer to:
https://docs.netsepio.com/latest/erebrus/nodes/beacon-node
HELP_TEXT
exit 0
}
init_logging() {
LOG_DIR="/tmp"
local INSTALLATION_ID=$(LC_ALL=C tr -dc A-Za-z0-9 </dev/urandom | head -c 8; echo)
LOG_FILE="${LOG_DIR}/erebrus-install-${INSTALLATION_ID}.log"
# Clear previous log and start fresh
> "$LOG_FILE"
log_info "=== Erebrus Node Installation Started ==="
log_info "Installation directory: $INSTALL_DIR"
log_info "Installation mode: ${INSTALLATION_MODE:-binary}"
log_info "Timestamp: $(date)"
}
# Centralized logging functions
log_info() {
local message="$1"
echo "[$(date '+%Y-%m-%d %H:%M:%S')] [INFO] $message" >> "$LOG_FILE"
}
log_error() {
local message="$1"
echo "[$(date '+%Y-%m-%d %H:%M:%S')] [ERROR] $message" >> "$LOG_FILE"
}
log_success() {
local message="$1"
echo "[$(date '+%Y-%m-%d %H:%M:%S')] [SUCCESS] $message" >> "$LOG_FILE"
}
log_warning() {
local message="$1"
echo "[$(date '+%Y-%m-%d %H:%M:%S')] [WARNING] $message" >> "$LOG_FILE"
}
format_status() {
local status="$1"
case "$status" in
"✔ Complete") echo "[\033[32m$status\033[0m]" ;;
"✘ Skipped") echo "[\033[33m$status\033[0m]" ;;
"✘ Failed") echo "[\033[31m$status\033[0m]" ;;
"In Progress")echo "[\033[34m$status\033[0m]" ;;
"Pending") echo "[$status]" ;;
*) echo "[$status]" ;;
esac
}
display_header() {
# clear everything including scrollback buffer
printf '\033[2J\033[3J\033[H'
local header_buffer=""
# Add the logo to buffer
header_buffer+="$(tput clear)$(tput civis)"
header_buffer+="\e[94m"
header_buffer+=$(cat << "EOF"
/$$$$$$$$ /$$
| $$_____/ | $$
| $$ /$$$$$$ /$$$$$$ | $$$$$$$ /$$$$$$ /$$ /$$ /$$$$$$$
| $$$$$ /$$__ $$ /$$__ $$| $$__ $$ /$$__ $$| $$ | $$ /$$_____/
| $$__/ | $$ \__/| $$$$$$$$| $$ \ $$| $$ \__/| $$ | $$| $$$$$$
| $$ | $$ | $$_____/| $$ | $$| $$ | $$ | $$ \____ $$
| $$$$$$$$| $$ | $$$$$$$| $$$$$$$/| $$ | $$$$$$/ /$$$$$$$/
|________/|__/ \_______/|_______/ |__/ \______/ |_______/
EOF
)
header_buffer+="\e[0m\n\n"
header_buffer+="\033[1m\033[4mErebrus Node Software Installer v1.1\033[0m\n"
# printf '─%.0s' {1..80}
# Add separator and requirements
header_buffer+=$(printf '─%.0s' {1..100})
header_buffer+="\n\e[1mRequirements:\e[0m\n"
header_buffer+="→ Erebrus node needs static public IP that is routable from internet & controlled by you.\n"
header_buffer+="→ Ports 9080, 9002, 9003, 51820, and 8088 must be open on your firewall and/or host system.\n"
header_buffer+=$(printf '─%.0s' {1..100})
header_buffer+="\n"
# Add status lines
header_buffer+="\033[1m🔧 Configure Node: \033[0m$(format_status "${STAGE_STATUS[0]}")\n"
header_buffer+="\033[1m📦 Install Packages: \033[0m$(format_status "${STAGE_STATUS[1]}")\n"
header_buffer+="\033[1m🚀 Run Node: \033[0m$(format_status "${STAGE_STATUS[2]}")\n"
# Add final separator
header_buffer+=$(printf '─%.0s' {1..100})
header_buffer+="\n"
# Print the entire buffer at once
echo -e "$header_buffer"
# Save cursor position after printing everything
tput sc
log_info "Header displayed successfully"
}
# Function to clear all subprocess output
function clear_subprocess_output() {
# Go to saved position after header (status lines)
tput rc
# Move down 4 lines (3 status lines + 1 separator line)
# tput cud
# Clear everything from current position to end of screen
tput ed
log_info "Subprocess output cleared"
}
# Function to show spinner
show_spinner() {
local pid=$1
local msg=$2
local delay=0.2
local spinstr='|/-\'
log_info "Starting subprocess: $msg"
# Disable keyboard input echoing and save terminal settings
stty -echo
local old_tty_settings=$(stty -g)
# Print the initial message with brackets and spinner placeholder
printf "\n%s [ ]" "$msg"
printf "\b\b" # Move cursor back inside the brackets
# Start the spinner
while kill -0 $pid 2>/dev/null; do
local temp=${spinstr#?}
printf "%c\b" "$spinstr" # Print spinner char and move back
local spinstr=$temp${spinstr%"$temp"}
sleep $delay
# Clear any input to prevent line breaks
read -t 0.1 -n 10000 discard 2>/dev/null || true
done
# Get the exit status of the process
wait $pid
local exit_status=$?
# Update with Done/Failed in brackets and add newline
if [ $exit_status -eq 0 ]; then
printf "\033[32mSuccess\033[0m]\n"
log_success "Subprocess completed: $msg"
else
printf "\033[31mFailed\033[0m]\n"
log_error "Subprocess failed: $msg (exit code: $exit_status)"
fi
# Restore terminal settings
stty "$old_tty_settings"
stty echo
return $exit_status
}
# Function to check and create installation directory
create_install_directory() {
local base_dir="$1"
if [[ -z "$base_dir" ]]; then
log_error "create_install_directory: base_dir parameter is required"
return 1
fi
# Set installation directory to base_dir/erebrus
INSTALL_DIR="${base_dir}/erebrus"
log_info "Setting installation directory to: $INSTALL_DIR"
# Check if directory already exists
if [ -d "$INSTALL_DIR" ]; then
log_info "Installation directory already exists: $INSTALL_DIR"
return 0
fi
log_info "Creating installation directory: $INSTALL_DIR"
# Try to create without sudo first
if mkdir -p "$INSTALL_DIR/wireguard" 2>/dev/null && chown -R $(id -u -n):$(id -g -n) "$INSTALL_DIR" 2>/dev/null; then
log_success "Installation directory created successfully: $INSTALL_DIR"
return 0
else
# Try with sudo
printf "Creating directory '%s' requires elevated permissions.\n" "$INSTALL_DIR"
if sudo mkdir -p "$INSTALL_DIR/wireguard" && sudo chown -R $(whoami):$(whoami) "$INSTALL_DIR"; then
printf "Directory '%s' created successfully.\n" "$INSTALL_DIR"
log_success "Installation directory created with sudo: $INSTALL_DIR"
return 0
else
printf "Error: Failed to create directory '%s'.\n" "$INSTALL_DIR"
log_error "Failed to create installation directory: $INSTALL_DIR"
return 1
fi
fi
}
# Function to get the public IP address
get_public_ip() {
log_info "Attempting to get public IP address"
local ip=$(curl -s ifconfig.io 2>>"$LOG_FILE")
if [[ -n "$ip" ]]; then
log_success "Public IP detected: $ip"
echo "$ip"
else
log_error "Failed to detect public IP address"
echo ""
fi
}
# Function to get region
get_region() {
log_info "Attempting to get region"
local region=$(curl -s ifconfig.io/country_code 2>>"$LOG_FILE")
if [[ -n "$region" ]]; then
log_success "Region detected: $region"
echo "$region"
else
log_error "Failed to detect region"
echo "US"
fi
}
# Function to check if Docker is installed
is_docker_installed() {
log_info "Checking if Docker is installed"
if command -v docker > /dev/null && command -v docker-compose > /dev/null; then
log_success "Docker is already installed"
return 0
else
log_info "Docker is not installed"
return 1
fi
}
# This function does the actual test and returns success/failure. It doesn't print any messages
do_ip_port_test() {
local host_ip=$1
local port=$2
local listener_pid=""
log_info "Performing IP:Port reachability test on $host_ip:$port"
if sudo lsof -i TCP:"$port" >/dev/null 2>&1; then
log_warning "Port $port is already in use, skipping reachability test"
return 0
fi
# Prefer socat
if command -v socat >/dev/null 2>&1; then
log_info "Using socat for testing IP and port"
socat TCP-LISTEN:"$port",fork,reuseaddr - >/dev/null 2>&1 &
listener_pid=$!
# Fallback to python3
elif command -v python3 >/dev/null 2>&1; then
log_info "Using python3 for testing IP and port"
python3 -c "
import socket
s = socket.socket()
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('0.0.0.0', $port))
s.listen(1)
conn, addr = s.accept()
conn.close()
s.close()
" >/dev/null 2>&1 &
listener_pid=$!
else
log_warning "No supported listener tool (socat/python3); skipping IP reachability test"
return 0
fi
( sleep 5 && kill -0 "$listener_pid" 2>/dev/null && kill "$listener_pid" ) &
sleep 2
if echo "test" | nc "$host_ip" "$port" >/dev/null 2>&1; then
kill "$listener_pid" >/dev/null 2>&1
log_success "IP reachability test passed for $host_ip:$port"
return 0
else
kill "$listener_pid" >/dev/null 2>&1
log_error "IP reachability test failed for $host_ip:$port"
return 1
fi
}
# Function to test if the IP is directly reachable from the internet
test_ip_reachability() {
local host_ip=$HOST_IP
local port=9080
do_ip_port_test "$host_ip" "$port" &
show_spinner $! "→ Verifying IP & port reachability"
return $?
}
# Docker check_node_status() has been deprecated. See bottom of script if needed.
check_node_status() {
log_info "Checking node status"
local container_running=0
local port_9080_listening=0
local port_9002_listening=0
local port_8088_listening=0
local service_responding=0
# Check if container 'erebrus' is running (more precise check)
if [ "$INSTALLATION_MODE" = "container" ]; then
if sudo docker ps --format "table {{.Names}}" | grep -q "^erebrus$"; then
container_running=1
log_info "Erebrus container is running"
else
log_info "Erebrus container is not running"
fi
fi
# Check specific ports more efficiently
if sudo lsof -i :9080 -sTCP:LISTEN >/dev/null 2>&1; then
port_9080_listening=1
log_info "Port 9080 is listening"
else
log_info "Port 9080 is not listening"
fi
if sudo lsof -i :9002 -sTCP:LISTEN >/dev/null 2>&1; then
port_9002_listening=1
log_info "Port 9002 is listening"
else
log_info "Port 9002 is not listening"
fi
if sudo lsof -i :8088 -sTCP:LISTEN >/dev/null 2>&1; then
port_8088_listening=1
log_info "Xray Port 8088 is listening"
else
log_info "Xray Port 8088 is not listening"
fi
# HTTP health check to verify service is actually responding
if [ "$port_9080_listening" -eq 1 ]; then
if curl -s --connect-timeout 5 --max-time 10 "http://localhost:9080" >/dev/null 2>&1 || \
curl -s --connect-timeout 5 --max-time 10 "http://localhost:9080/health" >/dev/null 2>&1 || \
curl -s --connect-timeout 5 --max-time 10 "http://localhost:9080/api" >/dev/null 2>&1; then
service_responding=1
log_info "Erebrus service is responding to HTTP requests"
else
log_warning "Port 9080 is listening but service is not responding to HTTP requests"
fi
fi
# Determine overall status
local status_ok=0
if [ "$INSTALLATION_MODE" = "container" ]; then
if [[ "$container_running" -eq 1 && "$port_9080_listening" -eq 1 && "$port_9002_listening" -eq 1 ]]; then
status_ok=1
fi
else
if [[ "$port_9080_listening" -eq 1 && "$port_9002_listening" -eq 1 && "$port_8088_listening" -eq 1 ]]; then
status_ok=1
fi
fi
# Check if service is also responding
if [[ "$status_ok" -eq 1 && "$service_responding" -eq 1 ]]; then
log_success "Node status check passed - Service is fully operational"
return 0
elif [[ "$status_ok" -eq 1 ]]; then
log_success "Node status check passed - Ports are listening"
return 0
else
log_error "Node status check failed"
return 1
fi
}
validate_post_install() {
echo "🔍 Preparing to validate installation..."
(sleep 5 && check_node_status) &
show_spinner $! "→ Validating installation"
return $?
}
check_mnemonic_format() {
log_info "Validating mnemonic format"
local mnemonic="$1"
# Split the mnemonic into an array of words
IFS=' ' read -r -a words <<< "$mnemonic"
# Define the required number of words in the mnemonic (12, 15, 18, 21, or 24 typically for BIP39)
local required_words=(12 15 18 21 24)
# Check if the mnemonic has the correct number of words
local num_words=${#words[@]}
if ! [[ " ${required_words[*]} " =~ " $num_words " ]]; then
log_error "Invalid mnemonic: wrong number of words ($num_words). Expected: 12, 15, 18, 21, or 24"
return 1
fi
# Check if each word in the mnemonic is valid
for word in "${words[@]}"; do
if [[ ! "$word" =~ ^[a-zA-Z]+$ ]]; then
log_error "Invalid mnemonic: word '$word' contains non-alphabetic characters"
return 1
fi
done
log_success "Mnemonic format validation passed ($num_words words)"
return 0
}
print_final_message() {
log_info "Generating final installation message"
# Check if any enabled stages failed
local has_failures=false
local enabled_stages=0
local completed_stages=0
for i in {0..2}; do
local status="${STAGE_STATUS[$i]}"
if [[ "$status" == "✘ Failed" || "$status" == "✘ Blocked" ]]; then
has_failures=true
fi
if [[ "$status" != "✘ Skipped" ]]; then
enabled_stages=$((enabled_stages + 1))
if [[ "$status" == "✔ Complete" ]]; then
completed_stages=$((completed_stages + 1))
fi
fi
done
if [[ "$has_failures" == true ]]; then
printf "\e[31mInstallation failed due to stage failures.\e[0m\n"
printf "See $LOG_FILE for details.\n"
log_error "Installation failed - One or more stages failed"
elif [[ $enabled_stages -eq 0 ]]; then
printf "\e[33mNo stages were enabled to run.\e[0m\n"
log_warning "No stages were enabled to run"
elif [[ $completed_stages -eq $enabled_stages ]]; then
printf "\e[32mErebrus node installation is finished.\e[0m\n"
printf "Erebrus Node API is accessible at http://${HOST_IP}:9080\n"
printf "Refer \e[4mhttps://github.com/NetSepio/erebrus/blob/main/docs/docs.md\e[0m for API documentation.\n"
printf "\nYou can now manage the node using the \e[1merebrus\e[0m command. Try:\n"
printf " \e[36merebrus status\e[0m\n"
printf "\n\e[32mAll stages completed successfully!\e[0m\n\n"
log_success "Installation completed successfully - Node is running"
else
printf "\e[33mSome enabled stages did not complete successfully.\e[0m\n"
printf "See $LOG_FILE for details.\n"
log_warning "Some enabled stages did not complete successfully"
fi
}
#Function to enable IP forwarding on host. Required for wireguard to forward traffic
enable_ip_forwarding() {
local os
os=$(uname)
local config_file
local setting
if [[ "$os" == "Linux" ]]; then
config_file="/etc/sysctl.d/99-erebrus.conf"
setting="net.ipv4.ip_forward=1"
log_info "Configuring IP forwarding in $config_file"
# Remove any conflicting settings from the target file if it exists
if [[ -f "$config_file" ]]; then
sudo sed -i '/^net\.ipv4\.ip_forward/d' "$config_file"
fi
# Add the correct setting
echo "$setting" | sudo tee "$config_file" > /dev/null
log_info "IP forwarding setting written to $config_file"
# Apply all sysctl settings from all config files
log_info "Applying sysctl settings using sysctl --system..."
if sudo sysctl --system >> "$LOG_FILE" 2>&1; then
# Verify the setting actually took effect
if [[ "$(sysctl -n net.ipv4.ip_forward)" == "1" ]]; then
log_success "IP forwarding is enabled and verified"
return 0
else
log_error "sysctl applied, but IP forwarding not active"
return 1
fi
else
log_error "Failed to apply sysctl settings with sysctl --system"
return 1
fi
elif [[ "$os" == "Darwin" ]]; then
config_file="/etc/sysctl.conf"
setting="net.inet.ip.forwarding=1"
# Enable immediately
if [[ $(sysctl -n net.inet.ip.forwarding) -eq 1 ]]; then
log_info "IP forwarding is already enabled on this macOS system"
else
log_info "Enabling IP forwarding immediately"
sudo sysctl -w net.inet.ip.forwarding=1
fi
# Persist setting
if [[ ! -f "$config_file" ]]; then
echo "$setting" | sudo tee "$config_file" > /dev/null
log_info "Created $config_file and enabled IP forwarding persistently"
else
if grep -qE "^${setting}$" "$config_file"; then
log_info "IP forwarding is already enabled in $config_file"
else
sudo sed -i.bak '/net\.inet\.ip\.forwarding/d' "$config_file"
echo "$setting" | sudo tee -a "$config_file" > /dev/null
log_info "IP forwarding added to $config_file"
fi
fi
log_info "Note: On macOS, a reboot may be required for persistent IP forwarding to take effect."
else
log_error "Unsupported OS: $os"
return 1
fi
return 0
}
#Test if IP forwarding is enable on host
test_ip_forwarding() {
log_info "Checking IP forwarding setting..."
enable_ip_forwarding &
show_spinner $! "→ Validating IP Forwarding"
return $?
}
# Stage #1 - Configure Node environment variables
configure_node() {
log_info "=== Starting Stage 1: Configure Node ==="
echo "📋 Configuring node..."
# Prompt for installation directory and validate input
read -p "Enter installation directory (default: current directory): " INSTALL_DIR_INPUT
# Set base directory from input or use current default
BASE_DIR=${INSTALL_DIR_INPUT:-$(pwd)}
echo "Installation directory set to "$BASE_DIR""
log_info "User input for installation directory: $INSTALL_DIR_INPUT"
# Create the installation directory
if ! create_install_directory "$BASE_DIR"; then
log_error "Failed to create installation directory"
return 1
fi
# Configure .env for xray-only installation mode
if $INSTALL_XRAY_ONLY; then
log_info "INSTALL_XRAY_ONLY=true, XRAY_ENABLED=${XRAY_ENABLED}"
if [[ -f "${INSTALL_DIR}/.env" ]]; then
# Verify file is writable
if [[ ! -w "${INSTALL_DIR}/.env" ]]; then
log_error "Cannot write to ${INSTALL_DIR}/.env: Permission denied"
return 1
fi
# Debug: Log current .env content
log_info "Current .env content before update:"
log_info "$(cat "${INSTALL_DIR}/.env")"
# Check if XRAY_ENABLED exists in .env
if grep -q "^XRAY_ENABLED=" "${INSTALL_DIR}/.env"; then
# Update existing XRAY_ENABLED (cross-platform sed)
if sed -i.bak "s/^XRAY_ENABLED=.*/XRAY_ENABLED=${XRAY_ENABLED}/" "${INSTALL_DIR}/.env" 2>/dev/null || sed -i "" "s/^XRAY_ENABLED=.*/XRAY_ENABLED=${XRAY_ENABLED}/" "${INSTALL_DIR}/.env"; then
log_info "Updated XRAY_ENABLED to ${XRAY_ENABLED} in ${INSTALL_DIR}/.env"
else
log_error "Failed to update XRAY_ENABLED in ${INSTALL_DIR}/.env"
return 1
fi
# Remove backup file if created
rm -f "${INSTALL_DIR}/.env.bak"
else
# Append XRAY_ENABLED
printf "\n# #Erebrus Xray Installation Flag\nXRAY_ENABLED=${XRAY_ENABLED}\n" >> "${INSTALL_DIR}/.env"
log_info "Appended XRAY_ENABLED=${XRAY_ENABLED} to ${INSTALL_DIR}/.env"
fi
# Debug: Log .env content after update
log_info "Current .env content after update:"
log_info "$(cat "${INSTALL_DIR}/.env")"
else
# Create new .env with XRAY_ENABLED
bash -c "cat > ${INSTALL_DIR}/.env" <<EOL
# Application Configuration
XRAY_ENABLED=${XRAY_ENABLED}
EOL
log_info "Created new ${INSTALL_DIR}/.env with XRAY_ENABLED=${XRAY_ENABLED}"
fi
#Continue with regular node configuration
else
DEFAULT_HOST_IP=$(get_public_ip)
# Prompt for Public IP
printf "\nAutomatically detected public IP: ${DEFAULT_HOST_IP}\n"
read -p "Do you want to use this public IP? (default: y) (y/n): " use_default_host_ip
log_info "User choice for public IP: $use_default_host_ip"
if [ "$use_default_host_ip" = "n" ]; then
read -p "Enter your public IP (default: ${DEFAULT_HOST_IP}): " HOST_IP
HOST_IP=${HOST_IP:-$DEFAULT_HOST_IP}
log_info "User provided custom IP: $HOST_IP"
else
HOST_IP=${DEFAULT_HOST_IP}
log_info "Using detected IP: $HOST_IP"
fi
DEFAULT_DOMAIN="http://${HOST_IP}:9080"
# Prompt for Node Details
while [[ -z "$NODE_NAME" ]]; do
read -p "Enter your node name: " NODE_NAME
if [[ -z "$NODE_NAME" ]]; then
echo "❌ Node name cannot be empty. Please try again."
fi
done
log_info "Node name set: $NODE_NAME"
printf "Select a configuration type from the list below:\n"
PS3="Select a config type (e.g. 1): "
options=("ASTRO - Coming soon" "BEACON" "TITAN - Coming soon" "NEXUS" "ZENETH")
while true; do
select choice in "${options[@]}"; do
case "$choice" in
"ASTRO - Coming soon"|"TITAN - Coming soon")
echo "This configuration will be in upcoming updates. Please choose another option."
break # Restart the select prompt
;;
"BEACON"|"NEXUS"|"ZENETH")
CONFIG="$choice"
echo "You selected: $CONFIG"
log_info "Configuration type selected: $CONFIG"
break 2 # Exit both select and while loops
;;
*)
echo "Invalid choice. Please select a valid config type."
;;
esac
done
done
read -p "Enable Xray (default: n) (y/n): " enable_xray
enable_xray=${enable_xray:-n} # default to 'n' if empty
log_info "Xray enable choice: $enable_xray"
if [[ "$enable_xray" =~ ^[yY]$ ]]; then
XRAY_ENABLED="true"
printf "\033[0;32mXray will be enabled on this node.\033[0m\n"
log_info "Xray enabled"
else
XRAY_ENABLED="false"
printf "\033[0;31mXray will be disabled on this node.\033[0m\n"
log_info "Xray disabled"
fi
# Prompt for Chain
printf "Select valid chain from list below:\n"
PS3="Select a chain (e.g. 1): "
options=("SOLANA" "PEAQ")
select CHAIN in "${options[@]}"; do
if [ -n "$CHAIN" ]; then
log_info "Chain selected: $CHAIN"
break
else
echo "Invalid choice. Please select a valid chain."
fi
done
# Set RPC_URL and CONTRACT_ADDRESS based on CHAIN_NAME
case "$CHAIN" in
"PEAQ")
RPC_URL="https://peaq-rpc.publicnode.com"
CONTRACT_ADDRESS="0x8811Ffaa9565B5be4a030f3da4c5F1B9eC1d2177"
;;
"MONADTestnet")
RPC_URL="https://testnet-rpc.monad.xyz/"
CONTRACT_ADDRESS="0x4b4Fd104fb1f33a508300C1196cd5893f016F81c"
;;
"RISETestnet")
RPC_URL="https://testnet.riselabs.xyz/"
CONTRACT_ADDRESS="0xa5c3c7207B4362431bD02D0E02af3B8a73Bb35eD"
;;
"Solana")
RPC_URL=""
CONTRACT_ADDRESS="0x291eC3328b56d5ECebdF993c3712a400Cb7569c3"
;;
*)
RPC_URL=""
CONTRACT_ADDRESS=""
esac
while true; do
read -p "Enter your wallet mnemonic: " WALLET_MNEMONIC
if check_mnemonic_format "$WALLET_MNEMONIC"; then
break
else
printf "Wrong mnemonic, try again with correct mnemonic.\n"
fi
done
# Prompt for Config Type
printf "Select an access type from list below:\n"
PS3="Select an access type (e.g. 1): "
options=("public" "private")
select ACCESS in "${options[@]}"; do
if [ -n "$ACCESS" ]; then
log_info "Access type selected: $ACCESS"
break
else
echo "Invalid choice. Please select a valid access type."
fi
done
# Write environment variables to .env file
bash -c "cat > ${INSTALL_DIR}/.env" <<EOL
# Erebrus dVPN Node Configuration
RUNTYPE=released
SERVER=0.0.0.0
HTTP_PORT=9080
GRPC_PORT=9003
LIBP2P_PORT=9002
REGION=$(get_region)
NODE_NAME=${NODE_NAME}
DOMAIN=${DEFAULT_DOMAIN}
HOST_IP=${HOST_IP}
GATEWAY_DOMAIN=https://gateway.erebrus.io
POLYGON_RPC=
SIGNED_BY=NetSepio
FOOTER=NetSepio 2024
GATEWAY_WALLET=0x0
LOAD_CONFIG_FILE=false
GATEWAY_PEERID=/ip4/178.156.141.248/tcp/9001/p2p/12D3KooWJSMKigKLzehhhmppTjX7iQprA7558uU52hqvKqyjbELf
CHAIN_NAME=${CHAIN}
NODE_TYPE=VPN
NODE_CONFIG=${CONFIG}
MNEMONIC=${WALLET_MNEMONIC}
CONTRACT_ADDRESS=${CONTRACT_ADDRESS}
RPC_URL=${RPC_URL}
NODE_ACCESS=${ACCESS}
# WireGuard Configuration
WG_CONF_DIR=/etc/wireguard
WG_CLIENTS_DIR=/etc/wireguard/clients
WG_INTERFACE_NAME=wg0.conf
# WireGuard Specifications
WG_ENDPOINT_HOST=${HOST_IP}
WG_ENDPOINT_PORT=51820
WG_IPv4_SUBNET=10.0.0.1/16
WG_IPv6_SUBNET=fd9f:0000::10:0:0:1/64
WG_DNS=1.1.1.1
WG_ALLOWED_IP_1=0.0.0.0/0
WG_ALLOWED_IP_2=::/0
WG_PRE_UP=echo WireGuard PreUp
WG_POST_UP=iptables -A FORWARD -i %i -j ACCEPT; iptables -A FORWARD -o %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
WG_PRE_DOWN=echo WireGuard PreDown
WG_POST_DOWN=iptables -D FORWARD -i %i -j ACCEPT; iptables -D FORWARD -o %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
SERVICE_CONF_DIR=./erebrus
# Authentication & Policies
PASETO_EXPIRATION_IN_HOURS=168
AUTH_EULA=I Accept the Erebrus Terms of Service https://erebrus.io/terms
# Caddy Specifications
CADDY_CONF_DIR=/etc/caddy # /etc/caddy
CADDY_INTERFACE_NAME=Caddyfile
#Erebrus Xray Installation Flag
XRAY_ENABLED=${XRAY_ENABLED}
EOL
fi
log_success "Environment file created successfully: ${INSTALL_DIR}/.env"
if [[ "$XRAY_ENABLED" == "true" ]]; then
create_xray_config
return $?
fi
return 0
}
#Stage #2 - Install Dependencies
function install_dependencies(){
log_info "=== Starting Stage 2: Install Dependencies ==="
# Ensure installation directory exists (in case Stage 1 was skipped)
if ! create_install_directory "$BASE_DIR"; then
log_error "Failed to ensure installation directory exists"
return 1
fi
if [[ "$INSTALLATION_MODE" == "container" ]]; then
install_dependencies_docker_mode &
show_spinner $! "→ Installing Docker..."
local status=$?
else
install_dependencies_binary_mode &
show_spinner $! "→ Installing dependencies"
local deps_status=$?
download_erebrus_binary &
show_spinner $! "→ Downloading Erebrus binary"
local binary_status=$?
if [[ "$XRAY_ENABLED" == "true" ]]; then
download_xray_binary &
show_spinner $! "→ Downloading Xray binary"
local xray_status=$?
fi
fi
# Return overall status (non-zero if any subprocess failed)
[[ $deps_status -eq 0 && $binary_status -eq 0 && ${xray_status:-0} -eq 0 ]]
log_info "=== Finished Stage 2: Install Dependencies ==="
return $?
}
# Check if given group name exists in system
function group_exists() {
if command -v getent >/dev/null 2>&1; then
# Use getent if available (common in Linux)
if getent group "$1" >/dev/null 2>&1; then
return 0
else
return 1
fi
# Check using dscl (might be more reliable on macOS)
elif command -v dscl >/dev/null 2>&1; then
if dscl . -list /Groups | grep "$1" >/dev/null 2>&1; then
return 0
else
return 1
fi
fi
}
function create_group() {
# Create group, takes group name as an argument $1
if command -v groupadd; then
sudo groupadd "$1"
[[ $? -eq 0 ]] && return 0 || return 1
elif command -v dscl; then
dscl . -create /Groups/"$1"
[[ $? -eq 0 ]] && return 0 || return 1
fi
}
#Create docker group and add user to the group
function add_user_to_group() {
# Add current user to docker group
if command -v usermod; then
if ! groups "$USER" | grep "$1"; then
sudo usermod -aG "$1" "$USER" # Use sudo and usermod for Linux
[[ $? -eq 0 ]] && return 0 || return 1
fi
elif command -v dscl; then
if ! dscl . -read /Groups/"$1" | grep GroupMembership | grep "$USER"; then
dscl . -append /Groups/"$1" GroupMembership "$USER" # Use dscl for macOS
[[ $? -eq 0 ]] && return 0 || return 1
fi
fi
}
install_dependencies_docker_mode() {
log_info "=== Starting install_dependencies_docker_mode ==="
printf " → Checking Docker installation...\n"
if is_docker_installed; then
printf " ✓ Docker already installed\n"
sleep 2
else
printf " → Installing Docker...\n"
if command -v apt-get > /dev/null; then
(sudo apt-get update -qq && sudo apt-get install -y containerd docker.io && sudo apt-get install socat-* -y && sudo apt-get install lsof -y >> "$LOG_FILE" 2>&1) &
elif command -v yum > /dev/null; then
(sudo yum install yum-utils -y && sudo yum install nmap-ncat.x86_64 -y && sudo yum install lsof socat -y && sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo && yum install -y docker >> "$LOG_FILE" 2>&1 && sudo systemctl start docker && sudo systemctl enable docker >> "$LOG_FILE" 2>&1) &
elif command -v pacman > /dev/null; then
(sudo pacman -Sy --noconfirm docker socat >> "$LOG_FILE" 2>&1 && sudo systemctl start docker && sudo systemctl enable docker >> "$LOG_FILE" 2>&1) &
elif command -v dnf > /dev/null; then
printf " → Installing Docker on Fedora...\n"
(sudo dnf install dnf-plugins-core && dnf config-manager --add-repo https://download.docker.com/linux/fedora/docker-ce.repo && dnf install -y docker-ce docker-ce-cli containerd.io >> "$LOG_FILE" 2>&1) &
elif [[ "$OSTYPE" == "darwin"* ]]; then
printf " → Installing Docker on macOS...\n"
if ! command -v brew > /dev/null; then
printf " ✗ Homebrew not found. Please install Homebrew first.\n"
exit 1
fi
(brew install --cask docker socat >> "$LOG_FILE" 2>&1 && open /Applications/Docker.app) &
printf " ✓ Docker installation complete\n"
else
printf " ✗ Unsupported Linux distribution.\n"
exit 1
fi
printf " ✓ Docker installation complete\n"
fi
if docker --version > /dev/null 2>&1; then
printf " → Configuring Docker group...\n"
# Created docker group if not exits
if ! group_exists "docker"; then
create_group "docker";
fi
if add_user_to_group "docker"; then
if [[ $? -ne 0 ]]; then
printf " ✗ Failed to create group, docker configuration failed.\n"
exit 1
fi
fi
printf " ✓ Docker configuration complete\n"
fi
log_info "=== Finished install_dependencies_docker_mode ==="
}
function install_dependencies_binary_mode() {
log_info "=== Starting install_dependencies_binary_mode ==="
create_erebrus_folder
CURRENT_DIR=$(pwd)
INSTALL_FAILED=false
# Detect OS and install dependencies
if command -v apk > /dev/null; then
apk update >> "$LOG_FILE" 2>&1
apk add --no-cache bash openresolv bind-tools wireguard-tools gettext inotify-tools iptables >> "$LOG_FILE" 2>&1 || INSTALL_FAILED=true
elif command -v apt-get > /dev/null; then
sudo apt-get update -qq >> "$LOG_FILE" 2>&1
sudo apt-get install -y bash resolvconf dnsutils wireguard-tools gettext inotify-tools iptables systemd socat-* lsof >> "$LOG_FILE" 2>&1 || INSTALL_FAILED=true
elif command -v yum > /dev/null; then
sudo yum install -y bash openresolv bind-utils wireguard-tools gettext inotify-tools iptables socat lsof >> "$LOG_FILE" 2>&1 || INSTALL_FAILED=true
elif command -v pacman > /dev/null; then
sudo pacman -Sy --noconfirm bash openresolv bind-tools wireguard-tools gettext inotify-tools iptables socat lsof >> "$LOG_FILE" 2>&1 || INSTALL_FAILED=true
elif command -v dnf > /dev/null; then
sudo dnf install -y bash openresolv bind-utils wireguard-tools gettext inotify-tools iptables socat lsof >> "$LOG_FILE" 2>&1 || INSTALL_FAILED=true
elif command -v brew > /dev/null; then
sudo -u "$SUDO_USER" brew install bash wireguard-tools gettext coreutils iproute2mac curl socat lsof >> "$LOG_FILE" 2>&1 || INSTALL_FAILED=true
else
echo " ✗ Unsupported Linux distribution. Exiting." | tee -a "$LOG_FILE"
exit 1
fi
if [ "$INSTALL_FAILED" = true ]; then
log_error "Some dependencies failed to install."
fi
log_info "=== Finished install_dependencies_binary_mode ==="
}
function download_xray_binary() {
log_info "=== Starting download_xray_binary ==="
XRAY_REPO="NetSepio/erebrus-xray"
DOWNLOAD_DIR="${INSTALL_DIR}"
# Detect OS and ARCH (same logic as erebrus binary)
OS=$(uname | tr '[:upper:]' '[:lower:]') # "linux" or "darwin"
ARCH=$(uname -m)
case "$ARCH" in
x86_64) ARCH="amd64" ;;
arm64 | aarch64) ARCH="arm64" ;;
*)
log_error "Unsupported architecture: $ARCH"
echo " ✗ Unsupported architecture: $ARCH"
return 1
;;
esac
XRAY_BINARY_NAME="erebrus-xray-${OS}-${ARCH}"
XRAY_PATH="$DOWNLOAD_DIR/$XRAY_BINARY_NAME"
log_info "Detected OS: $OS, Architecture: $ARCH"
log_info "Target binary: $XRAY_BINARY_NAME"
# Check if binary already exists and is executable
if [[ -f "$XRAY_PATH" && -x "$XRAY_PATH" ]]; then
log_info "Erebrus-Xray binary already exists at $XRAY_PATH"