-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathmatrix.sh
1242 lines (1078 loc) · 38.6 KB
/
matrix.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
#!/bin/bash
# Check if the script is run as root
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
sleep 1
exit 1
fi
# Function to display ASCII logo
display_logo() {
cat << "EOF"
┏━┓┏━┓━━━━┓━━━━┓━━━┓━━┓━┓┏━┓
┃┃┗┛┃┃┏━┓┃┏┓┏┓┃┏━┓┃┫┣┛┓┗┛┏┛
┃┏┓┏┓┃┃┃┃┃┛┃┃┗┛┗━┛┃┃┃┃┗┓┏┛┃
┃┃┃┃┃┃┗━┛┃┃┃┃┃┃┏┓┏┛┃┃┃┏┛┗┓┃
┃┃┃┃┃┃┏━┓┃┏┛┗┓┃┃┃┗┓┫┣┓┛┏┓┗┓
┗┛┗┛┗┛┛┃┗┛┗━━┛┃┛┗━┛━━┛━┛┗━┛
EOF
}
# My statics
CONFIG_DIRECTORY='/etc/tinc/matrix'
CONFIG_PATH='/etc/tinc/matrix/tinc.conf'
HOST_DIRECTORY='/etc/tinc/matrix/hosts'
SSH_KEY='/root/.ssh/id_rsa'
# just press key to continue
press_key(){
read -p " Press Enter to continue..."
}
# Define a function to colorize text
colorize() {
local color="$1"
local text="$2"
local style="${3:-normal}"
# Define ANSI color codes
local black="\033[30m"
local red="\033[31m"
local green="\033[32m"
local yellow="\033[33m"
local blue="\033[34m"
local magenta="\033[35m"
local cyan="\033[36m"
local white="\033[37m"
local reset="\033[0m"
# Define ANSI style codes
local normal="\033[0m"
local bold="\033[1m"
local underline="\033[4m"
# Select color code
local color_code
case $color in
black) color_code=$black ;;
red) color_code=$red ;;
green) color_code=$green ;;
yellow) color_code=$yellow ;;
blue) color_code=$blue ;;
magenta) color_code=$magenta ;;
cyan) color_code=$cyan ;;
white) color_code=$white ;;
*) color_code=$reset ;; # Default case, no color
esac
# Select style code
local style_code
case $style in
bold) style_code=$bold ;;
underline) style_code=$underline ;;
normal | *) style_code=$normal ;; # Default case, normal text
esac
# Print the colored and styled text
echo -e "${style_code}${color_code}${text}${reset}"
}
function show_progress {
local bar_size=40
local bar_char_done="#"
local bar_char_todo="-"
local bar_percentage_scale=2
current="$1"
total="$2"
# calculate the progress in percentage
percent=$(bc <<< "scale=$bar_percentage_scale; 100 * $current / $total" )
# The number of done and todo characters
done=$(bc <<< "scale=0; $bar_size * $percent / 100" )
todo=$(bc <<< "scale=0; $bar_size - $done" )
# build the done and todo sub-bars
done_sub_bar=$(printf "%${done}s" | tr " " "${bar_char_done}")
todo_sub_bar=$(printf "%${todo}s" | tr " " "${bar_char_todo}")
# output the bar
echo -ne "\rProgress : [${done_sub_bar}${todo_sub_bar}] ${percent}%"
if [ $total -eq $current ]; then
echo -e ""
fi
}
# Function to install unzip if not already installed
install_core() {
if ! command -v tincd &> /dev/null; then
# Check if the system is using apt package manager
if command -v apt-get &> /dev/null; then
colorize yellow "Matrix-Core is not installed. Installing...\n"
apt-get update &> /dev/null
apt-get install -y tinc &> /dev/null
colorize green "Matrix-Core installed successfully.\n"
sleep 1
else
colorize red "Error: Unsupported package manager."
press_key
exit 1
fi
fi
}
install_boxes() {
if ! command -v boxes &> /dev/null; then
# Check if the system is using apt package manager
if command -v apt-get &> /dev/null; then
colorize yellow "Boxes is not installed. Installing...\n"
#apt-get update &> /dev/null
apt-get install -y boxes &> /dev/null
colorize green "Boxes installed successfully.\n"
sleep 1
else
colorize red "Error: Unsupported package manager."
press_key
exit 1
fi
fi
}
install_lolcat() {
if ! command -v lolcat &> /dev/null; then
# Check if the system is using apt package manager
if command -v apt-get &> /dev/null; then
colorize yellow "Lolcat is not installed. Installing...\n"
#apt-get update &> /dev/null
apt-get install -y lolcat &> /dev/null
colorize green "Lolcat installed successfully.\n"
sleep 1
else
colorize red "Error: Unsupported package manager."
press_key
exit 1
fi
fi
}
install_sshpass() {
if ! command -v sshpass &> /dev/null; then
# Check if the system is using apt package manager
if command -v apt-get &> /dev/null; then
colorize yellow "sshpass is not installed. Installing...\n"
#apt-get update &> /dev/null
apt-get install -y sshpass &> /dev/null
colorize green "sshpass installed successfully.\n"
sleep 1
else
colorize red "Error: Unsupported package manager."
press_key
exit 1
fi
fi
}
install_bc() {
if ! command -v bc &> /dev/null; then
# Check if the system is using apt package manager
if command -v apt-get &> /dev/null; then
colorize yellow "bc is not installed. Installing...\n"
#apt-get update &> /dev/null
apt-get install -y bc &> /dev/null
colorize green "bc installed successfully.\n"
sleep 1
else
colorize red "Error: Unsupported package manager."
press_key
exit 1
fi
fi
}
#Install required packages
install_core
install_lolcat
install_boxes
install_sshpass
install_bc
#_________________________________________Configurating Main Node ___________________________________
configure_main_node(){
clear
colorize reset " \033[1mMain Node Configuration\033[0m" bold | boxes -d ada-box | lolcat
echo ''
expert=false
colorize green " What do you prefer?\n" bold
colorize yellow " [1] Easy Mode?"
colorize yellow " [2] Expert Mode?"
echo -n " [-] Enter your choice [1 or 2]: "
read -p '' choice
echo ''
if [[ $choice == "2" ]]; then
expert=true
fi
if [[ $choice != "2" && $choice != "1" ]]; then
colorize yellow " Easy Mode by default...\n" bold
fi
# Host Generator ______________________
if [[ ! -d $HOST_DIRECTORY ]]; then
mkdir -p $HOST_DIRECTORY
fi
# Node Name / Consider it as a static value ______________________
node_name="main"
colorize magenta " [*] Node name: $node_name"
echo ''
# Device Name, Static value ______________________
device='/dev/net/tun' #No need to change it
# Server IP / Get it from hostname______________________
SERVER_IP=$(hostname -I | awk '{print $1}') #Easy !
colorize magenta " [*] Main Node IP Address: ${SERVER_IP}"
echo ''
# Subnet / Simple value ______________________
read -p " [*] Subnet (172.16.1.1): " subnet
if [ -z "$subnet" ]; then
colorize red " Please enter a valid subnet.\n"
press_key
return 1
else
subnet="${subnet}/32"
fi
echo ''
# Address Family / EXPERT value______________________
if $expert; then
echo -ne " [-] Address family (${GREEN}ipv4${NC}|ipv6|any): "
read -r add_family
if [ -z "$add_family" ]; then
add_family='ipv4'
fi
echo ''
else
add_family='ipv4'
fi
# Direct Only / EXPERT value______________________
if $expert; then
echo -ne " [-] Direct only (${GREEN}yes${NC}/no): "
read -r direct_only
if [ -z "$direct_only" ]; then
direct_only='yes'
fi
echo ''
else
direct_only='yes'
fi
# Port / Simple value ______________________
echo -ne " [-] Port Number (${GREEN}2097${NC}): "
read -r port
if [ -z "$port" ]; then
port="2097"
fi
# Validate if the input is a valid port number
if ! [[ $port =~ ^[0-9]+$ && $port -ge 22 && $port -le 65535 ]]; then
colorize red " Error: Please enter a valid port number between 22 and 65535.\n"
press_key
return 1
fi
echo ''
# Ping Interval / EXPERT value______________________
if $expert; then
echo -ne " [-] Ping Interval (in seconds)(${GREEN}10${NC}): "
read -r ping_interval
if [ -z "$ping_interval" ]; then
ping_interval='10'
fi
echo ''
else
ping_interval='10'
fi
# ProcessPriority / EXPERT value______________________
if $expert; then
echo -ne " [-] ProcessPriority (low, normal, ${GREEN}high${NC}): "
read -r process_priority
if [ -z "$process_priority" ]; then
process_priority='high'
fi
echo ''
else
process_priority='high'
fi
# Cipher / EXPERT value______________________
if $expert; then
echo -ne " [-] Cipher (${GREEN}aes-128-cbc${NC}, aes-256-cbc, none): "
read -r cipher
if [ -z "$cipher" ]; then
cipher='aes-128-cbc'
fi
echo ''
else
cipher='aes-128-cbc'
fi
# Digest / EXPERT value______________________
if $expert; then
echo -ne " [-] Digest (${GREEN}md5${NC}, sha256, none): "
read -r digest
if [ -z "$digest" ]; then
digest='md5'
fi
echo ''
else
digest='md5'
fi
# PTMU / EXPERT value______________________
if $expert; then
echo -ne " [-] PTMU (${GREEN}1514${NC}): "
read -r ptmu
if [ -z "$ptmu" ]; then
ptmu='1514'
fi
echo ''
else
ptmu='1514'
fi
# Configs file creator
show_progress 1 9
echo "Name = $node_name" > $CONFIG_PATH
echo "Device = $device" >> $CONFIG_PATH
echo "AddressFamily = $add_family" >> $CONFIG_PATH
echo "DirectOnly = $direct_only" >> $CONFIG_PATH
echo "AutoConnect = yes" >> $CONFIG_PATH
echo "PingInterval = $ping_interval" >> $CONFIG_PATH
echo "ProcessPriority = $process_priority" >> $CONFIG_PATH
# Host file creator
show_progress 2 9
HOST_FILE="${HOST_DIRECTORY}/${node_name}"
echo "Address = $SERVER_IP" > $HOST_FILE
echo "Subnet = $subnet" >> $HOST_FILE
echo "Port = $port" >> $HOST_FILE
echo "Cipher = $cipher" >> $HOST_FILE
echo "Digest = $digest" >> $HOST_FILE
echo "PTMU = $ptmu" >> $HOST_FILE
#Generating ssh key
show_progress 3 9
if [[ ! -f $SSH_KEY ]]; then
ssh-keygen -t rsa -f /root/.ssh/id_rsa -N "" -q &> /dev/null
fi
# Change sshd config
show_progress 4 9
sed -i 's/#GatewayPorts no/GatewayPorts yes/' /etc/ssh/sshd_config &> /dev/null
# Build RSA Keys
show_progress 4 9
tincd -n matrix -K4096 &> /dev/null
# Create route subnet
show_progress 5 9
local route_subnet=$(echo $subnet | cut -d'.' -f1-3)
local route_subnet="${route_subnet}.0/24"
# Build tinc-up
show_progress 6 9
cat <<EOF > "/etc/tinc/matrix/tinc-up"
#!/bin/bash
/sbin/ip link set \$INTERFACE up
/sbin/ip addr add $subnet dev \$INTERFACE
/sbin/ip route add $route_subnet dev \$INTERFACE
EOF
# Build tinc-up
show_progress 7 9
cat <<EOF > "/etc/tinc/matrix/tinc-down"
#!/bin/bash
/sbin/ip route del $route_subnet dev \$INTERFACE
/sbin/ip addr del $subnet dev \$INTERFACE
/sbin/ip link set \$INTERFACE down
EOF
# Executable +x
chmod +x /etc/tinc/matrix/tinc-{up,down} &> /dev/null
# Start Servies
show_progress 8 9
systemctl enable tinc@matrix &> /dev/null
systemctl start tinc@matrix &> /dev/null
# Finsish it
show_progress 9 9
echo ''
colorize green " The main node configuration completed successfully \n" bold
press_key
}
#_________________________________________Registring New Node ___________________________________
add_new_node(){
clear
# _____ Get node info and ssh to it_____
colorize reset " \033[1mPrimary Node Server Setup\033[0m" bold | boxes -d ada-box | lolcat
echo ''
# Get user/password of node server
colorize green " Enter your node username/passowrd:\n" bold
read -p " [*] Enter your node IP Address: " host_ip
# Check IPV4 is correct
local valid_ip_regex="^([0-9]{1,3}\.){3}[0-9]{1,3}$"
if [[ $host_ip =~ $valid_ip_regex ]]; then
# Check each octet to ensure it's between 0 and 255
IFS='.' read -r -a octets <<< "$host_ip"
for octet in "${octets[@]}"; do
if (( octet < 0 || octet > 255 )); then
colorize red " The IPv4 is not valid. aborting... \n" bold
press_key
return 1
fi
done
else
colorize red " The IPv4 is not valid. aborting... \n" bold
press_key
return 1
fi
read -p " [-] Enter your username (root): " username
if [ -z "$username" ]; then
username="root"
fi
read -s -p " [*] Enter your password [Hidden]: " password
echo ''
if [ -z $password ]; then
colorize red " You didn't enter your password, aborting...\n" bold
press_key
return 1
fi
# Check public ssh key
if [[ ! -f $SSH_KEY ]]; then
colorize red " No public key to trasnfer. configure main node first...\n"
press_key
return 1
fi
echo ''
# Check SSH connectivity
colorize yellow " Connecting to remote node on port 22...\n" bold
sshpass -p $password ssh -o ConnectTimeout=3 -o StrictHostKeyChecking=no $username@$host_ip "whoami" &> /dev/null
# Check the exit status of the SSH command
if [[ $? -eq 0 ]]; then
colorize green " Connected to remote node successfully\n" bold
else
colorize red " ✖︎Error, wrong username/password\n" bold
press_key
return 1
fi
#Transfer public key
sshpass -p $password ssh-copy-id -i ~/.ssh/id_rsa.pub $username@$host_ip >/dev/null 2>&1
colorize green " The public key added to remote node successfully" bold
sleep 2
#_______________________Start Configuration__________________
clear
colorize reset " \033[1mNew Node Registration\033[0m" bold | boxes -d ada-box | lolcat
echo ''
expert=false
colorize green " What do you prefer?\n" bold
colorize yellow " [1] Simple Mode?"
colorize yellow " [2] Expert Mode?"
echo -n " [-] Enter your choice [1 or 2]: "
read -p '' choice
echo ''
if [[ $choice == "2" ]]; then
expert=true
fi
if [[ $choice != "2" && $choice != "1" ]]; then
colorize yellow " Easy Mode by default...\n" bold
fi
# Node Name / simple value __________________________________
read -p " [*] Node name (must be unique): " node_name
if [ -z "$node_name" ]; then
colorize red " The node name is mandatory.\n"
press_key
return 1
fi
if [[ -f "${HOST_DIRECTORY}/${node_name}" ]]; then
colorize red " This node name is duplicate. you can't add it.\n"
press_key
return 1
fi
pattern='^[a-zA-Z0-9_]+$'
if [[ ! $node_name =~ $pattern ]]; then
colorize red " The name must consist only of a-z, A-Z, 0-9 and underscore.\n"
press_key
return 1
fi
echo ''
# Node IP / We get it before _______________________________
colorize normal " [*] Node IP Address: ${host_ip}"
echo ''
# Subnet IP / Simple value _______________________________
read -p " [*] Subnet (172.16.1.x): " subnet
if [ -z "$subnet" ]; then
colorize red " The subnet value is mandatory.\n"
press_key
return 1
else
subnet="${subnet}/32"
fi
echo ''
# Device Name / Static value by now _________________________
device='/dev/net/tun'
# Address Family / Expert value _______________________________
if $expert; then
echo -ne " [-] Address family (${GREEN}ipv4${NC}|ipv6|any): "
read -r add_family
if [ -z "$add_family" ]; then
add_family='ipv4'
fi
echo ''
else
add_family='ipv4'
fi
# Direct Only / Expert value _______________________________
if $expert; then
echo -ne " [-] Direct only (${GREEN}yes${NC}/no): "
read -r direct_only
if [ -z "$direct_only" ]; then
direct_only='yes'
fi
echo ''
else
direct_only='yes'
fi
# Port Number / Simple value _______________________________
echo -ne " [-] Port Number (${GREEN}2097${NC}): "
read -r port
if [ -z "$port" ]; then
port="2097"
fi
# Validate if the input is a valid port number
if ! [[ $port =~ ^[0-9]+$ && $port -ge 22 && $port -le 65535 ]]; then
colorize red " Error: Please enter a valid port number between 22 and 65535.\n"
press_key
return 1
fi
echo ''
# Ping Interval / EXPERT value______________________
if $expert; then
echo -ne " [-] Ping Interval (in seconds)(${GREEN}10${NC}): "
read -r ping_interval
if [ -z "$ping_interval" ]; then
ping_interval='10'
fi
echo ''
else
ping_interval='10'
fi
# ProcessPriority / EXPERT value______________________
if $expert; then
echo -ne " [-] ProcessPriority (low, normal, ${GREEN}high${NC}): "
read -r process_priority
if [ -z "$process_priority" ]; then
process_priority='high'
fi
echo ''
else
process_priority='high'
fi
# Cipher / EXPERT value______________________
if $expert; then
echo -ne " [-] Cipher (${GREEN}aes-128-cbc${NC}, aes-256-cbc, none): "
read -r cipher
if [ -z "$cipher" ]; then
cipher='aes-128-cbc'
fi
echo ''
else
cipher='aes-128-cbc'
fi
# Digest / EXPERT value______________________
if $expert; then
echo -ne " [-] Digest (${GREEN}md5${NC}, sha256, none): "
read -r digest
if [ -z "$digest" ]; then
digest='md5'
fi
echo ''
else
digest='md5'
fi
# PTMU / EXPERT value______________________
if $expert; then
echo -ne " [-] PTMU (${GREEN}1514${NC}): "
read -r ptmu
if [ -z "$ptmu" ]; then
ptmu='1514'
fi
echo ''
else
ptmu='1514'
fi
# Create route subnet. may be due a silly code by me
local route_subnet=$(echo $subnet | cut -d'.' -f1-3)
local route_subnet="${route_subnet}.0/24"
# SSH into the remote server and use cat to create files
colorize yellow " Connecting to remote server...\n" bold
sleep 1
clear
#______________________ SSH COMMANDS ____________________
ssh -T $username@$host_ip << EOF
TERM=xterm clear
echo -e "1. Installing Packages..."
apt update &> /dev/null
apt install tinc -y &> /dev/null
echo -e "2. Generating conf file..."
mkdir -p /etc/tinc/matrix/hosts/ &> /dev/null
sudo tee /etc/tinc/matrix/tinc.conf &> /dev/null <<CONFIG
Name = $node_name
Device = $device
AddressFamily = $add_family
DirectOnly = $direct_only
AutoConnect = yes
PingInterval = $ping_interval"
ProcessPriority = $process_priority
CONFIG
echo -e "3. Generating host file..."
sudo tee /etc/tinc/matrix/hosts/$node_name &> /dev/null <<NODE
Address = $host_ip
Subnet = $subnet
Port = $port
Cipher = $cipher
Digest = $digest
PTMU = $ptmu
NODE
echo -e "4. Generating RSA key file..."
yes '' | tincd -n matrix -K4096 &> /dev/null
echo -e "5. Configurating service files..."
sudo tee /etc/tinc/matrix/tinc-up &> /dev/null <<UP
#!/bin/bash
/sbin/ip link set \\\$INTERFACE up
/sbin/ip addr add $subnet dev \\\$INTERFACE
/sbin/ip route add $route_subnet dev \\\$INTERFACE
UP
sudo tee /etc/tinc/matrix/tinc-down &> /dev/null <<DOWN
#!/bin/bash
/sbin/ip route del $route_subnet dev \\\$INTERFACE
/sbin/ip addr del $subnet dev \\\$INTERFACE
/sbin/ip link set \\\$INTERFACE down
DOWN
echo -e "6. Create Matrix service..."
chmod +x /etc/tinc/matrix/tinc-{up,down} &> /dev/null
exit
EOF
#______________________ SSH CLOSED ____________________
#Copy main config to node
echo -e "7. Exchanging main key to node server ..."
ssh -T $username@$host_ip sudo mkdir /tmp/ &> /dev/null
scp ${HOST_DIRECTORY}/main $username@$host_ip:/tmp/ &> /dev/null
ssh -T $username@$host_ip sudo mv -v /tmp/main $HOST_DIRECTORY &> /dev/null
# SSH into the remote server and read the contents of a file into a variable
echo -e "8. Exchanging node key to main server ..."
node_with_key=$(ssh -T $username@$host_ip "cat ${HOST_DIRECTORY}/$node_name")
echo "$node_with_key" > ${HOST_DIRECTORY}/$node_name
#start matrix service on node
echo -e "9. Start Matrix serivce on the node server..."
ssh -T $username@$host_ip "systemctl enable --now tinc@matrix" &> /dev/null
# Maybe service exists before so restart it
ssh -T $username@$host_ip "systemctl restart tinc@matrix" &> /dev/null
# Check if the configuration exists in the file
echo -e "10. Append ConnectTo values to main node..."
if ! grep -q "ConnectTo = $node_name" $CONFIG_PATH; then
echo "ConnectTo = $node_name" >> $CONFIG_PATH
fi
echo -e "11. Restart Matrix service on main server..."
systemctl restart tinc@matrix &> /dev/null
echo ''
colorize green "The remote node configuration completed successfully \n" bold
press_key
}
#_________________________________________Funcs Related to Status Checker ___________________________________
print_table_header() {
printf "╔════════════╦══════════════╦════════════════╦════════════════╦════════════════╦════════════╗\033[0m\n" | lolcat -S 10
printf "\e[36m\033[1m║ %-10s ║ %-12s ║ %-14s ║ %-14s ║ %-14s ║ %-10s ║\033[0m\n" "Node Name" "Local IP" "Public IP" "Local Network" "Public Network" "SSH Status" | lolcat -S 11
printf "╠════════════╬══════════════╬════════════════╬════════════════╬════════════════╬════════════╬\033[0m\n" | lolcat -S 12
}
# Function to print table row
print_table_row() {
printf "║ %-10s ║ %-12s ║ %-14s ║ %-32s ║ %-32s ║ %-28s ║\n" "$1" "$2" "$3" "$4" "$5" "$6"
printf "╠════════════╬══════════════╬════════════════╬════════════════╬════════════════╬════════════╬\033[0m\n"
}
perform_ping() {
if ping -c 2 -i 0.2 "$1" &> /dev/null; then
colorize green "✔︎Online"
else
colorize red "✖︎Offline"
fi
}
perform_ssh_check() {
if ssh -o BatchMode=yes -o ConnectTimeout=3 -T "$1" "exit" &> /dev/null; then
colorize green "✔︎OK"
return 0
else
colorize red "✖︎Error"
return 1
fi
}
#_________________________________________Node Health Monitor ___________________________________
check_node_status() {
clear
colorize normal " \033[1mNode Health Monitor\033[0m " bold | boxes -d ada-box | lolcat
echo ''
if [ -z "$(ls -A $HOST_DIRECTORY 2> /dev/null)" ]; then
colorize red " There are no main server or nodes to display.\n" bold
press_key
return 1
fi
print_table_header
for node in "${HOST_DIRECTORY}"/*; do
node_name=$(basename "$node")
public_ip=$(grep -oP '(?<=Address = )\S+' "$node")
local_ip=$(grep -oP '(?<=Subnet = )[^/]*' "$node")
public_ping=$(perform_ping "$public_ip")
local_ping=$(perform_ping "$local_ip")
ssh_status=$(perform_ssh_check "$public_ip")
print_table_row "$node_name" "$local_ip" "$public_ip" "$local_ping" "$public_ping" "$ssh_status"
done
echo ''
press_key
}
#_________________________________________NODE MANAGEMENT CENTER ___________________________________
node_mangment_center(){
clear
colorize normal " \033[1mNode Administration Center\033[0m " bold | boxes -d ada-box | lolcat
echo ''
colorize green " List of your nodes:\n" bold
deactive_nodes=()
for node in "${HOST_DIRECTORY}"/*; do
node_name=$(basename "$node")
public_ip=$(grep -oP '(?<=Address = )\S+' "$node")
if perform_ssh_check "$public_ip" > /dev/null 2>&1 || [[ "$node_name" == "main" ]]; then
colorize cyan " [✓] $node_name" bold
else
colorize red " [𐄂] $node_name" bold
deactive_nodes+=("$node_name")
fi
done
echo ''
echo -n " Enter your node name: "
read -p '' todo_node
echo ''
if [[ -z "$todo_node" ]]; then
colorize red " Null value...\n" bold
press_key
return 1
fi
if echo "${deactive_nodes[@]}" | grep -qw "$todo_node"; then
colorize red " Access to $todo_node node is not possible at this time\n" bold
echo -ne " \033[1m\033[33mDo you want to remove this node from database? (yes/no)\033[33m\033[1m "
read confirm
if [[ "$confirm" == "yes" ]]; then
remove_garbage_node "$todo_node"
return 3
else
colorize red " Operarion cancelled by user\n" bold
press_key
return 2
fi
fi
if [ ! -f ${HOST_DIRECTORY}/$todo_node ]; then
colorize red " '$todo_node' not found in your registered nodes\n" bold
press_key
return
fi
clear
colorize normal " \033[1m$todo_node node Control Panel\033[0m " bold | boxes -d ada-box | lolcat
echo ''
colorize green " List of available commands:\n" bold
colorize reset " [1] Change Port Number" | lolcat -S 20
colorize reset " [2] Restart Matrix Service" | lolcat -S 30
colorize reset " [3] Reboot Node Server" | lolcat -S 40
colorize reset " [4] Remove Node completely" | lolcat -S 25
colorize reset " [5] Back to main menu" | lolcat -S 50
echo ''
echo -n " Enter your choice [1-5]: "
read -p '' choice
case $choice in
1) change_port_number $todo_node ;;
2) restart_matrix_service $todo_node;;
3) reboot_node_server $todo_node;;
4) remove_node $todo_node ;;
5) return 1 ;;
*) colorized red " Invalid option!" && sleep 1 && return 1 ;;
esac
echo ''
}
#________________Change port func#_____________
change_port_number(){
echo ''
CONFIG_FILE=${HOST_DIRECTORY}/${1}
port=$(grep -oP 'Port = \K\d+' "$CONFIG_FILE")
colorize cyan " Current port number: $port" bold
read -p $' \e[1;33mEnter new port number:\e[0m ' new_port
# Validate if the input is a valid port number
if ! [[ $new_port =~ ^[0-9]+$ && $new_port -ge 22 && $new_port -le 65535 ]]; then
colorize red " Error: Please enter a valid port number between 22 and 65535.\n"
press_key
return 1
fi
sed -i "s/Port = [0-9]*/Port = $new_port/" "$CONFIG_FILE"
if [[ $1 == "main" ]];then
echo ''
file_count=$(ls -d "$HOST_DIRECTORY"/* | wc -l)
counter=1
show_progress "$counter" "$file_count"
for node in "${HOST_DIRECTORY}"/*; do
node_name=$(basename "$node")
if [[ $node_name != "main" ]]; then
ip_address=$(grep -oP '(?<=Address = )\S+' "$node")
ssh -o BatchMode=yes -T "$ip_address" "sed -i 's/Port = [0-9]*/Port = $new_port/' \"$CONFIG_FILE\"" &> /dev/null
ssh -o BatchMode=yes -T "$ip_address" "systemctl restart tinc@matrix" &> /dev/null
((counter++)) # Increment the counter
show_progress "$counter" "$file_count"
fi
done
else
ip_address=$(grep -oP '(?<=Address = )\S+' $CONFIG_FILE)
ssh -o BatchMode=yes -T "$ip_address" "sed -i 's/Port = [0-9]*/Port = $new_port/' \"$CONFIG_FILE\"" &> /dev/null
ssh -o BatchMode=yes -T "$ip_address" "systemctl restart tinc@matrix" &> /dev/null
fi
echo ''
colorize green " Port number updated to: $new_port\n" bold
colorize green " Matrix service restarted successfully" bold
systemctl restart tinc@matrix &> /dev/null
echo ''
press_key
}
remove_garbage_node (){
echo ''
# not possible but just for safety, funny!
if [[ "$1" == "main" ]]; then
echo -e "\033[1mOMG, you did impossible possible!\033[0m" | boxes -d nuke | lolcat
echo ''
press_key
return 1
fi
local CONFIG_FILE=${HOST_DIRECTORY}/${1}
sed -i "/ConnectTo = ${1}/d" $CONFIG_PATH &> /dev/null
rm -rf "$CONFIG_FILE" &> /dev/null
echo -e " \033[1mGarbage fils of $1 node deleted from database successfully\033[0m" | boxes -d nuke | lolcat
echo ''
press_key
return 0
}
#______________Remove node func_______________
remove_node(){
echo ''
echo -ne " \033[0;33m\033[1mAre you to delete Matrix on $1 node? (yes/no)\033[0m\033[0m: "
read answer
answer=$(echo "$answer" | tr '[:upper:]' '[:lower:]')
echo ''
if [[ "$answer" != "yes" ]]; then
colorize red " The action cancelled by user \n"
press_key
return 1
fi
if [[ "$1" == "main" ]]; then
systemctl stop tinc@matrix &> /dev/null
systemctl disable tinc@matrix &> /dev/null
rm -rf "$CONFIG_DIRECTORY" &> /dev/null
clear
echo -e " \033[1mMatrix on $1 node deleted successfully\033[0m" | boxes -d nuke | lolcat
echo ''
press_key
return 0
else
local CONFIG_FILE=${HOST_DIRECTORY}/${1}
local public_ip=$(grep -oP '(?<=Address = )\S+' "$CONFIG_FILE")
ssh -T -o BatchMode=yes -o ConnectTimeout=3 "$public_ip" << EOF
TERM=xterm clear
systemctl stop tinc@matrix &> /dev/null
systemctl disable tinc@matrix &> /dev/null
rm -rf "$CONFIG_DIRECTORY" &> /dev/null
EOF
sed -i "/ConnectTo = ${1}/d" $CONFIG_PATH &> /dev/null
rm -rf "$CONFIG_FILE" &> /dev/null
echo -e " \033[1mMatrix on $1 node deleted successfully\033[0m" | boxes -d nuke | lolcat
echo ''
press_key
return 0
fi
}
#______________Restart node func_______________
restart_matrix_service(){
echo ''
if [[ "$1" == "main" ]]; then
systemctl restart tinc@matrix &> /dev/null
colorize green " Matrix service in $1 node restarted successfully \n" bold
press_key
return 0
else