forked from team-exor/exor-mn-installer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exor-mn-installer.sh
executable file
·2001 lines (1805 loc) · 76.7 KB
/
exor-mn-installer.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
#
# Version: v2.0.0
# Date: March 3, 2021
#
# Run this script with the desired parameters or leave blank to install using defaults. Use -h for help.
#
# Tested to be working on Ubuntu 16.04+ and Debian 8.x+ x64
# Please report other working instances to:
# Telegram: @joeuhren on https://t.me/ExorOfficialSupport
# or
# Discord: @Joe [Team Exor]#5573 on https://discord.gg/dSuGm3y
# A special thank you to @marsmensch for releasing the NODEMASTER script which helped immensely for integrating IPv6 support
# Global Variables
readonly SCRIPT_VERSION="2.0.0"
readonly WALLET_URL_TEMPLATE="https://github.com/team-exor/exor/releases/download/\${WALLET_VERSION}/"
readonly SOURCE_URL="https://github.com/team-exor/exor.git"
readonly SOURCE_DIR="exor"
readonly TITLE_STRING="Exor Masternode Installer"
readonly ARCHIVE_DIR_TEMPLATE="\${WALLET_PREFIX}-\${WALLET_VERSION}"
readonly DEFAULT_PORT_NUMBER=51572
readonly DEFAULT_RPC_PORT=51573
readonly DEFAULT_WALLET_DIR="Exor"
readonly DEFAULT_DATA_DIR=".exor"
readonly WALLET_CONFIG_NAME="exor.conf"
readonly IP4_CONFIG_NAME=".ip4.conf"
readonly IP6_CONFIG_NAME=".ip6.conf"
readonly NET_INTERFACE_CONFIG_NAME=".net.conf"
readonly REBOOT_SCRIPT_NAME=".reboot.sh"
readonly SHUTDOWN_SCRIPT_NAME=".shutdown.sh"
readonly WALLET_FILE_TEMPLATE="\${WALLET_PREFIX}-\${WALLET_VERSION}-x86_64-linux-gnu.tar.gz"
readonly WALLET_PREFIX="exor"
readonly PEER_DATA_CMD="getconnectioncount"
readonly BLOCKCOUNT_URL="https://explorer.exor.io/api/getblockcount"
readonly RELEASES_URL="https://api.github.com/repos/team-exor/exor/releases"
readonly NONE="\033[00m"
readonly ORANGE="\033[00;33m"
readonly RED="\033[01;31m"
readonly GREEN="\033[01;32m"
readonly CYAN="\033[01;36m"
readonly GREY="\033[01;30m"
readonly PURPLE="\033[01;35m"
readonly ULINE="\033[4m"
readonly RC_LOCAL="/etc/rc.local" # TODO: Remove this line in the near future
readonly NETWORK_BASE_TAG="5123"
readonly HOME_DIR="/usr/local/bin"
readonly SERVICE_DIR="/etc/systemd/system"
readonly TEMP_UPDATE_CONFIG_PATH="/tmp/exor-mn-update.conf"
readonly DAEMON_SCRIPT_PREFIX="d"
readonly CLI_SCRIPT_PREFIX="c"
readonly VERSION_URL="https://raw.githubusercontent.com/team-exor/exor-mn-installer/master/VERSION"
readonly SCRIPT_URL="https://raw.githubusercontent.com/team-exor/exor-mn-installer/master/exor-mn-installer.sh"
readonly NEW_CHANGES_URL="https://raw.githubusercontent.com/team-exor/exor-mn-installer/master/NEW_CHANGES"
readonly WELCOME_MSG=" ${ORANGE}.:+ssssssssss+:.\n :oys+${GREY}oosyhhhhys++${ORANGE}/oyo:\n /hs+${GREY}odNMMMMMMMMMMMMNh+${ORANGE}/oh/\n :hs/${GREY}hMMMMMMMMMMMMMMMMMMMMy${ORANGE}:oh:\n oh+${GREY}sMMMMMmmNNNNNNNNNNNMMMMMMo${ORANGE}/ho\n oh/${GREY}hMMMMMMh${ORANGE}\\-\`\`-ossss+o${GREY}MMMMMMMh${ORANGE}/do\n -d+${GREY}sMMMMMMMMM${ORANGE}y\`\`+${GREY}MMMMMN${ORANGE}s${GREY}MMMMMMMMy${ORANGE}od-\n yy:${GREY}MMMMMMMMMM${ORANGE}y\`\`+${GREY}MMMM${ORANGE}h${GREY}mMMMMMMMMMM${ORANGE}/hy\n dy+${GREY}MMMMMMMMMM${ORANGE}y\`\`.+o+:o${GREY}dMMMMMMMMMM${ORANGE}ohd\n dy+${GREY}MMMMMMMMMM${ORANGE}y.\`/${GREY}MMMm${ORANGE}y${GREY}hMMMMMMMMMM${ORANGE}ohd\n yh:${GREY}NMMMMMMMMM${ORANGE}y..+${GREY}MMMMNNMMMMMMMMMM${ORANGE}/hy\n -do${GREY}sMMMMMMMMM${ORANGE}y..+${GREY}MMMMMN${ORANGE}s:${GREY}NMMMMMMs${ORANGE}od-\n oh/${GREY}yMMMMMMMh${ORANGE}/-.-+++/-./${GREY}mMMMMMMy${ORANGE}/do\n oh+${GREY}oNMMMMMMMMMMMMMNNMMMMMMMN+${ORANGE}/do\n :hs${GREY}+yNMMMMMMMMMMMMMMMMMMNs:${ORANGE}sh:\n +hs${GREY}+ohNMMMMMMMMMMMMNy+/${ORANGE}sh+\n :ohy${GREY}++oosyyyyso++/${ORANGE}sho:\n ./+syyyyyyyys+/.${NONE}"
WALLET_VERSION="1.0.0"
# Default variables
NET_TYPE=6
INSTALL_TYPE="i"
WALLET_TYPE="d"
PORT_NUMBER=`expr $DEFAULT_PORT_NUMBER - 1`
INSTALL_NUM=1
SWAP=1
FIREWALL=1
FAIL2BAN=1
SYNCCHAIN=1
OSUPGRADE=1
WAIT_TIMEOUT=5
DATA_INSTALL_DIR=""
WALLET_INSTALL_DIR=""
NET_INTERFACE=""
UPDATE_INDEX=0
WRITE_IP4_CONF=0
WRITE_IP6_CONF=0
ARCHIVE_DIR=""
CURRENT_USER="${SUDO_USER}"
USER_HOME_DIR="$(awk -F: -v v="${CURRENT_USER}" '{if ($1==v) print $6}' /etc/passwd)"
# Functions
error_message() {
echo "${RED}Error:${NONE} $1" && echo && exit
}
welcome_screen() {
# Check if running an updateall install
if [ -z "${UPDATE_INDEX}" ] || [ ${UPDATE_INDEX} -eq 0 ] || [ ${UPDATE_INDEX} -eq 1 ]; then
# Not an updateall install or it is the first updateall install
clear
echo "${WELCOME_MSG}"
fi
}
help_menu() {
clear
echo "${0##*/}, v$SCRIPT_VERSION usage example:"
echo "sudo sh ${0##*/} [OPTION]"
echo && echo "Options:" && echo
echo " -h, --help"
echo " display this help information screen and exit"
echo " -t, --type [string]"
echo " install type"
echo " 2 valid options: i = install (default), u = uninstall"
echo " -w, --wallet [string]"
echo " wallet type"
echo " 2 valid options: d = download (default), b = build from source"
echo " -g, --genkey [string]"
echo " masternode genkey value"
echo " if left blank the genkey value will be autogenerated"
echo " -N, --net [integer]"
echo " ip address type"
echo " 2 valid options: 6 = ipv6 (default), 4 = ipv4"
echo " -i, --ip [string]"
echo " bind node to a specific ipv4 or ipv6 ip address"
echo " if left blank and -N = 4 then the main wan ipv4 address will be used"
echo " if left blank and -N = 6 then a new ipv6 address will be registered"
echo " -p, --port [integer]"
echo " node will listen on specific port #"
echo " if left blank the port will default to ${PORT_NUMBER} + value of -n"
echo " -n, --number [integer]"
echo " node install #"
echo " default install # is 1"
echo " increment this value to set up 2+ nodes"
echo " only a single wallet will be installed each time the script is run"
echo " valid inputs are 1-99"
echo " -a, --adapter [string]"
echo " bind node to a specific network adapter by name"
echo " if left blank the first available and enabled adapter will be chosen"
echo " -s, --noswap"
echo " skip creating the disk swap file"
echo " default is to install the disk swap file"
echo " the swap file only needs to be created once per computer"
echo " it is strongly recommended that you do not skip this install"
echo " unless you are sure your VPS has enough memory"
echo " -f, --nofirewall"
echo " skip the firewall setup"
echo " default is to install and configure the firewall"
echo " it is strongly recommended that you do not skip this install"
echo " unless you plan to do the firewall setup manually"
echo " -b, --nobruteprotect"
echo " skip the brute-force protection setup"
echo " default is to install and configure brute-force protection"
echo " brute-force protection only needs to be installed once per computer"
echo " -c, --nochainsync"
echo " skip waiting for the blockchain to sync after installation"
echo " default is to wait for the blockchain to fully sync before exiting"
echo " -u, --noosupgrade"
echo " skip applying operating system updates/upgrades before installation"
echo " default is to run the following before doing an install:"
echo " apt-get update"
echo " apt-get upgrade"
echo " apt-get dist-upgrade"
echo " -R, --rpcall"
echo " send an RPC command to all wallets controlled by this script and"
echo " display the results"
echo " -S, --stopall"
echo " shutdown all wallets controlled by this script and wait for all to"
echo " finish shutting down before continuing"
echo " -U, --updateall"
echo " update all wallets controlled by this script one at a time until all"
echo " are complete"
}
begins_with() { case $2 in "$1"*) true;; *) false;; esac; }
contains() { case $2 in *"$1"*) true;; *) false;; esac; }
str_replace() { echo `echo $1 | sed 's/'"${2}"'/'"${3}"'/g'`; }
count_occurances() { echo `echo "$1" | grep -o "$2" | wc -l`; }
strindex() {
x="${1%%$2*}"
[ "$x" = "$1" ] && echo -1 || echo "${#x}"
}
execute_command() {
# Ensure that the command is run as the user who initiated the script
if [ "$USER" != "${CURRENT_USER}" ]; then
su ${CURRENT_USER} -c "${1}"
else
eval "${1}"
fi
}
validate_genkey() {
if [ $(printf "%s" "$1" | wc -m) -ne 51 ]; then
echo "err"
fi
}
validate_ip4address() {
IFS=. read a b c d << EOF
$1
EOF
if ! ( for i in a b c d; do
eval test \$$i -gt 0 && eval test \$$i -le 255 || exit 1
done 2> /dev/null )
then
echo "err"
fi
}
validate_ip6address() {
if ! (echo $1 | grep -Eq '^([0-9a-fA-F]{0,4}:){1,7}[0-9a-fA-F]{0,4}$'); then
echo "err"
else
# Format is valid so now check if the address is already available
ip -6 addr | grep -qi "$1"
if [ $? -ne 0 ]; then
# IP address is not already availble so now check if the this address can be created
if ! begins_with "${IPV6_INT_BASE}" "$1"; then
echo "err"
fi
fi
fi
}
validate_port() {
if [ -z "$1" ] || ([ -n "$1" ] && ((echo $1 | egrep -q '^[0-9]+$' && ([ $1 -lt 1 ] || [ $1 -gt 65535 ])) || ! test "$1" -gt 0 2> /dev/null)); then
echo "err"
fi
}
validate_install_num() {
if [ -z "$1" ] || ([ -n "$1" ] && ((echo $1 | egrep -q '^[0-9]+$' && ([ $1 -lt 1 ] || [ $1 -gt 99 ])) || ! test "$1" -gt 0 2> /dev/null)); then
echo "err"
fi
}
check_stop_wallet() {
# Check if the wallet is currently running
if [ -f "${HOME_DIR}/${1}/${WALLET_PREFIX}d" ] && [ -n "$(lsof "${HOME_DIR}/${1}/${WALLET_PREFIX}d" 2> /dev/null)" ]; then
# Wallet is running. Issue stop command
${HOME_DIR}/${1}/${WALLET_PREFIX}-cli -datadir=${USER_HOME_DIR}/${2} stop >/dev/null 2>&1
# Wait for wallet to close
PERIOD=". "
while [ -f "${HOME_DIR}/${1}/${WALLET_PREFIX}d" ] && [ -n "$(lsof "${HOME_DIR}/${1}/${WALLET_PREFIX}d" 2> /dev/null)" ]
do
sleep 1 &
printf "\rWaiting for wallet to close%s" "${PERIOD}"
case $PERIOD in
". ") PERIOD=".. "
;;
".. ") PERIOD="..."
;;
*) PERIOD=". " ;;
esac
wait
done
printf "\rWallet closed successfully " && echo
fi
}
init_network() {
# Check if the network interface was already set
if [ -z "${NET_INTERFACE}" ]; then
# Loop through all known network interfaces
for file in /sys/class/net/*; do
# Remove the path from the network interface name
NET_INTERFACE="$(basename "$file")"
# Lookup more details about this network interface
INTERFACE_DETAIL="$(ip address show $(basename "$file"))"
# Interface is invalid until proven to be valid
VALID_INTERFACE=0
# Check if this is an IPv4 or IPv6 install
if [ "$NET_TYPE" -eq 6 ]; then
# This is an IPv6 install
# Check if this is a valid IPv6 network interface
if contains " inet6 " "${INTERFACE_DETAIL}" && ! contains " inet6 ::1" "${INTERFACE_DETAIL}"; then
# The interface is valid
VALID_INTERFACE=1
fi
else
# This is an IPv4 install
# Check if this is a valid IPv4 network interface
if contains " inet " "${INTERFACE_DETAIL}" && ! contains " inet 127.0.0.1" "${INTERFACE_DETAIL}"; then
# The interface is valid
VALID_INTERFACE=1
fi
fi
# Check if the interface is valid
if [ "$VALID_INTERFACE" -eq 1 ]; then
# Ensure that the interface is enabled
if [ "$(check_network_interface_enabled ${NET_INTERFACE})" = "1" ]; then
# This interface is enabled and meets all requirements
VALID_INTERFACE=2
# Stop checking for a valid network interfaces
break
fi
fi
done
else
# Network interface already set via command line or "remembered" via update install
# Ensure that the interface is enabled
if [ "$(check_network_interface_enabled ${NET_INTERFACE})" = "1" ]; then
# This interface is enabled and meets all requirements
VALID_INTERFACE=2
else
# Interface is invalid
VALID_INTERFACE=0
fi
fi
# Check if a valid network interface was found
if [ "$VALID_INTERFACE" -ne 2 ]; then
# No valid network interface found
echo && error_message "Cannot find the default network interface or it is disabled"
fi
}
init_ipv6() {
# Initialize network variables
init_network
# Get the base IPv6 address
IPV6_INT_BASE="$(ip -6 addr show dev ${NET_INTERFACE} | grep inet6 | awk -F '[ \t]+|/' '{print $3}' | grep -v ^fe80 | grep -v ^::1 | cut -f1-4 -d':' | head -1)"
if [ -z "${IPV6_INT_BASE}" ]; then
echo && error_message "No IPv6 support found. Did you forget to enable it during installation?"
fi
}
check_network_interface_enabled() {
# Check if the operstate file exists for this interface
if [ -f /sys/class/net/${1}/operstate ]; then
# Interface operstate file exists
# Check the current status
if [ "$(cat /sys/class/net/${1}/operstate)" = "up" ]; then
# This interface is enabled and meets all requirements
echo "1"
else
# This interface is disabled
echo "0"
fi
else
# This interface doesn't have a state file and is therefore invalid
echo "0"
fi
}
install_package() {
# Install the package
echo "${CYAN}#####${NONE} Install ${2} ${CYAN}#####${NONE}" && echo
sleep 2
apt-get install ${1} -y && echo
# Check to ensure the package was installed
if [ -z "$({ dpkg -l | grep -E '^ii' | grep ${1}; })" ]; then
echo && error_message "Failed to install ${2}"
fi
}
extract_wallet_files() {
echo "${CYAN}#####${NONE} Extract wallet files ${CYAN}#####${NONE}" && echo
tar -zxvf "${WALLET_BASE_DIR}/${WALLET_FILE}" -C "${USER_HOME_DIR}" && echo
}
write_config() {
{
echo "rpcuser=${WALLET_PREFIX}rpc${INSTALL_NUM}"
echo "rpcpassword=$(pwgen -s 32 1)"
echo "rpcallowip=127.0.0.1"
echo "rpcport=$(( $DEFAULT_RPC_PORT - 1 + $INSTALL_NUM ))"
echo "port=${PORT_NUMBER}"
echo "listen=1"
echo "server=1"
echo "daemon=1"
echo "externalip=${CONFIG_ADDRESS}"
# Check if the ip address can be bound to the wallet
if [ -n "$({ ip -${NET_TYPE} addr | grep -i "${WAN_IP}"; })" ]; then
# Bind this address to the wallet
echo "bind=${CONFIG_ADDRESS}"
fi
# Check if there is a genkey value yet
if [ -n "$NULLGENKEY" ]; then
# Write the masternode config section only after a genkey value is present
echo "masternode=1"
echo "masternodeaddr=${CONFIG_ADDRESS}"
echo "masternodeprivkey=$NULLGENKEY"
fi
} > ${USER_HOME_DIR}/${DATA_INSTALL_DIR}/${WALLET_CONFIG_NAME}
}
wait_wallet_loaded() {
CURRENT_BLOCKS=""
PERIOD=". "
# Wait for the wallet to fully load (getblockcount will return an error instead of a numeric block value until loaded)
while ! (echo ${CURRENT_BLOCKS} | egrep -q '^[0-9]+$'); do
sleep 1 &
printf "\rWaiting for wallet to load%s" "${PERIOD}"
CURRENT_BLOCKS=$("${HOME_DIR}/${WALLET_INSTALL_DIR}/${WALLET_PREFIX}-cli" -datadir="${USER_HOME_DIR}/${DATA_INSTALL_DIR}" getblockcount) >/dev/null 2>&1
case $PERIOD in
". ") PERIOD=".. "
;;
".. ") PERIOD="..."
;;
*) PERIOD=". " ;;
esac && wait
done
printf "\rWallet loaded successfully "
}
online_wallet_check() {
echo && echo "${CYAN}#####${NONE} Check for wallet update ${CYAN}#####${NONE}" && echo
# Get releases from github into a variable
WALLET_RELEASES=$({ curl -sL "${RELEASES_URL}?$(date +%s)" | awk -F"," -v k="tag_name" '{
gsub(/{|}/,"")
for(i=1;i<=NF;i++){
if ( $i ~ k ){
print $i;
}
}
}'; });
# Count the number of releases
s=$WALLET_RELEASES COUNTER=0
until
t=${s#*"tag_name"}
[ "$t" = "$s" ]
do
COUNTER=$((COUNTER + 1))
s=$t
done
# Check if there is more than one release
if [ ${COUNTER} -gt 1 ]; then
# Get the github releases json source
NEW_WALLET_VERSION=$({ curl -sL "${RELEASES_URL}?$(date +%s)" | awk -F"," -v k="tag_name" '{
gsub(/{|}/,"")
for(i=1;i<=NF;i++){
if ( $i ~ k ){
print $i;
exit;
}
}
}'; });
# Remove field header
NEW_WALLET_VERSION=$({ str_replace "${NEW_WALLET_VERSION}" '"tag_name":' ""; });
# Remove surrounding quotes
NEW_WALLET_VERSION=$({ str_replace "${NEW_WALLET_VERSION}" '"' ""; });
# Check if the wallet version # is blank or a very long string as that would usually indicate a problem
if [ "${#NEW_WALLET_VERSION}" -gt 0 ] && [ "${#NEW_WALLET_VERSION}" -lt 16 ]; then
if begins_with "v" "${NEW_WALLET_VERSION}"; then
# Remove the 'v' from the beginning of the version string
NEW_WALLET_VERSION=$(echo "${NEW_WALLET_VERSION}" | cut -c2-${#NEW_WALLET_VERSION})
fi
if [ "${WALLET_VERSION}" != "${NEW_WALLET_VERSION}" ]; then
# A new version of the wallet is available
echo "${CYAN}Current wallet:${NONE} v${WALLET_VERSION}"
echo "${CYAN}New wallet:${NONE} v${NEW_WALLET_VERSION}"
else
echo "No new update found"
fi
# Update the wallet version with the version # found online
WALLET_VERSION="${NEW_WALLET_VERSION}"
else
echo "No new update found"
fi
else
# Only one release so there cannot be an updated version
echo "No new update found"
fi
}
unregisterIPAddress() {
# Dynamically populate the config name based on the argument passed to the function
IP_CONFIG_NAME="$(eval echo \$IP${1}_CONFIG_NAME)"
# Check if the config file exists
if [ -f ${HOME_DIR}/${WALLET_INSTALL_DIR}/${IP_CONFIG_NAME} ]; then
# Check if the node was previously bound to a specific network interface
if [ -f ${HOME_DIR}/${WALLET_INSTALL_DIR}/${NET_INTERFACE_CONFIG_NAME} ]; then
# Remember the old network interface name
OLD_NET_INTERFACE="$(cat "${HOME_DIR}/${WALLET_INSTALL_DIR}/${NET_INTERFACE_CONFIG_NAME}")"
else
# Use the current network interface
OLD_NET_INTERFACE="${NET_INTERFACE}"
fi
# Unregister previous IP address
eval "unregisterIP${1}Address $(cat "${HOME_DIR}/${WALLET_INSTALL_DIR}/${IP_CONFIG_NAME}") "${OLD_NET_INTERFACE}""
# Remove the config file
rm -f "${HOME_DIR}/${WALLET_INSTALL_DIR}/${IP_CONFIG_NAME}"
fi
# Destroy the IP_CONFIG_NAME variable as it is no longer needed
unset -v IP_CONFIG_NAME
}
unregisterIP4Address() {
ip -4 addr del "${1}/23" dev ${2} >/dev/null 2>&1
}
unregisterIP6Address() {
ip -6 addr del "${1}/64" dev ${2} >/dev/null 2>&1
}
removeWalletLinks() {
rm -f "${HOME_DIR}/${WALLET_PREFIX}d${INSTALL_SUFFIX}"
rm -f "${HOME_DIR}/${WALLET_PREFIX}-cli${INSTALL_SUFFIX}"
}
rpc_command() {
WALLET_CLOSED=0
# Check for installed wallets
i=1; while [ $i -le 99 ]; do
case $i in
1) WALLET_DIR_TEST="${DEFAULT_WALLET_DIR}"
DATA_DIR_TEST="${DEFAULT_DATA_DIR}" ;;
*) WALLET_DIR_TEST="${DEFAULT_WALLET_DIR}${i}"
DATA_DIR_TEST="${DEFAULT_DATA_DIR}${i}" ;;
esac
if [ -d "${HOME_DIR}/${WALLET_DIR_TEST}" ]; then
# Found an installed wallet
# Check if the wallet is currently running
if [ -f "${HOME_DIR}/${WALLET_DIR_TEST}/${WALLET_PREFIX}d" ] && [ -n "$(lsof "${HOME_DIR}/${WALLET_DIR_TEST}/${WALLET_PREFIX}d" 2> /dev/null)" ]; then
# Wallet is running
# Add space if a wallet was already closed
if [ $WALLET_CLOSED -eq 0 ]; then
echo
fi
# Display info msg
echo "${CYAN}#####${NONE} Wallet #${i} Response: ${CYAN}#####${NONE}" && echo
# Issue rpc command
${HOME_DIR}/${WALLET_DIR_TEST}/${WALLET_PREFIX}-cli -datadir=${USER_HOME_DIR}/${DATA_DIR_TEST} ${1}
# Keep track of wallet being closed
echo && WALLET_CLOSED=1
fi
fi
i=$(( i + 1 ))
done
# Check if any wallets were closed
if [ $WALLET_CLOSED -eq 0 ]; then
echo && echo "${GREEN}#####${NONE} No wallets are currently running ${GREEN}#####${NONE}" && echo
fi
}
stop_all() {
WALLET_CLOSED=0
# Check for installed wallets
i=1; while [ $i -le 99 ]; do
case $i in
1) WALLET_DIR_TEST="${DEFAULT_WALLET_DIR}"
DATA_DIR_TEST="${DEFAULT_DATA_DIR}" ;;
*) WALLET_DIR_TEST="${DEFAULT_WALLET_DIR}${i}"
DATA_DIR_TEST="${DEFAULT_DATA_DIR}${i}" ;;
esac
if [ -d "${HOME_DIR}/${WALLET_DIR_TEST}" ]; then
# Found an installed wallet
# Check if the wallet is currently running and stop it if running
if [ -f "${HOME_DIR}/${WALLET_DIR_TEST}/${WALLET_PREFIX}d" ] && [ -n "$(lsof "${HOME_DIR}/${WALLET_DIR_TEST}/${WALLET_PREFIX}d" 2> /dev/null)" ]; then
# Wallet is running
# Add space if a wallet was already closed
if [ $WALLET_CLOSED -eq 0 ]; then
echo
fi
# Issue stop command
echo "${CYAN}#####${NONE} Closing wallet #${i} ${CYAN}#####${NONE}"
echo && check_stop_wallet "${WALLET_DIR_TEST}" "${DATA_DIR_TEST}" && echo
# Keep track of wallet being closed
WALLET_CLOSED=1
fi
fi
i=$(( i + 1 ))
done
# Check if any wallets were closed
if [ $WALLET_CLOSED -eq 1 ]; then
echo "${GREEN}#####${NONE} All wallets have been shut down ${GREEN}#####${NONE}"
else
echo && echo "${GREEN}#####${NONE} No wallets are currently running ${GREEN}#####${NONE}"
fi
echo
}
# TODO: Remove this function in the near future
remove_rc_local() {
# Check if the rc.local file exists
if [ -f ${RC_LOCAL} ]; then
# Remove the reboot script line for the current wallet from the rc.local file
grep -v "${HOME_DIR}/${WALLET_INSTALL_DIR}/${REBOOT_SCRIPT_NAME}" ${RC_LOCAL} > ${RC_LOCAL}.new; mv ${RC_LOCAL}.new ${RC_LOCAL}
fi
}
add_cron_job() {
crontab -l | { cat; echo "@reboot sleep 30; ${HOME_DIR}/${WALLET_INSTALL_DIR}/${REBOOT_SCRIPT_NAME} "\""${CURRENT_USER}"\"" & # AUTOMATICALLY ADDED VIA exor-mn-installer.sh DO NOT REMOVE OR CHANGE MANUALLY"; } | crontab -
}
get_shutdown_service_filename() {
echo "${WALLET_PREFIX}${INSTALL_SUFFIX}_shutdown"
}
# Check linux distribution
LINUX_VERSION=$(cat /etc/issue.net)
if ! contains "Ubuntu" "$LINUX_VERSION" && ! contains "Debian" "$LINUX_VERSION"; then
echo && echo "Your linux distribution: ${ORANGE}$LINUX_VERSION${NONE}"
echo "This script has been designed to run on ${GREEN}Ubuntu 16.04+${NONE} and ${GREEN}Debian 8.x+${NONE}."
echo "Would you like to continue installing anyway? [y/n]: ";
read -p "" INSTALL_ANSWER
case "$INSTALL_ANSWER" in
y|Y|yes|Yes|YES) ;;
*) exit ;;
esac
fi
# Fix the current user and home directory variables in the event that $SUDO_USER is blank (installing as root)
if [ -z "${CURRENT_USER}" ]; then
CURRENT_USER="$(whoami)"
USER_HOME_DIR="$(awk -F: -v v="${CURRENT_USER}" '{if ($1==v) print $6}' /etc/passwd)"
fi
# Read command line arguments
if ! ARGS=$(getopt -o "ht:w:g:N:i:p:n:a:R:sfbcuSU" -l "help,type:,wallet:,genkey:,net:,ip:,port:,number:,adapter:,rpcall:,noswap,nofirewall,nobruteprotect,nochainsync,noosupgrade,stopall,updateall" -n "${0##*/}" -- "$@"); then
# invalid command line arguments so show help menu
help_menu
exit;
fi
eval set -- "$ARGS";
while true; do
case "$1" in
-h|--help)
shift;
help_menu;
exit
;;
-t|--type)
shift;
if [ -n "$1" ]; then
INSTALL_TYPE="$1";
shift;
fi
;;
-w|--wallet)
shift;
if [ -n "$1" ]; then
WALLET_TYPE="$1";
shift;
fi
;;
-g|--genkey)
shift;
if [ -n "$1" ]; then
NULLGENKEY="$1";
shift;
fi
;;
-N|--net)
shift;
if [ -n "$1" ]; then
INITIAL_NET_TYPE="$1";
shift;
fi
;;
-i|--ip)
shift;
if [ -n "$1" ]; then
WAN_IP="$1";
shift;
fi
;;
-p|--port)
shift;
if [ -n "$1" ]; then
PORT_NUMBER_ARG="$1";
shift;
fi
;;
-n|--number)
shift;
if [ -n "$1" ]; then
INSTALL_NUM="$1";
shift;
fi
;;
-a|--adapter)
shift;
if [ -n "$1" ]; then
NET_INTERFACE="$1";
shift;
fi
;;
-s|--noswap)
shift;
SWAP="0";
;;
-f|--nofirewall)
shift;
FIREWALL="0";
;;
-b|--nobruteprotect)
shift;
FAIL2BAN="0";
;;
-c|--nochainsync)
shift;
SYNCCHAIN="0";
;;
-u|--noosupgrade)
shift;
OSUPGRADE="0";
;;
-R|--rpcall)
shift;
if [ -n "$1" ]; then
rpc_command "$1"
exit
fi
;;
-S|--stopall)
shift;
stop_all;
exit
;;
-U|--updateall)
shift;
# Check if the temp update config file exists
if [ -f ${TEMP_UPDATE_CONFIG_PATH} ]; then
# Read the contents of the config file
UPDATE_INDEX=$(cat "${TEMP_UPDATE_CONFIG_PATH}")
# Delete the temp update config file
rm -f "${TEMP_UPDATE_CONFIG_PATH}"
else
# Config file not found, so start with index 1
UPDATE_INDEX=1
fi
;;
--)
shift;
break;
;;
esac
done
# Verify that user has root
if [ "$(whoami)" != "root" ]; then
echo && error_message "${ORANGE}Root${NONE} privileges not detected. This script must be run using the keyword '${CYAN}sudo${NONE}' to enable ${ORANGE}root${NONE} user"
fi
# Ensure commands are executed from the users home directory
eval "cd ${USER_HOME_DIR}"
# Check and fix updateall install argument if necessary
if [ -n "$UPDATE_INDEX" ] && [ ${UPDATE_INDEX} -ne 0 ] && [ -n "$(validate_install_num $UPDATE_INDEX)" ]; then
# Invalid update index specified, but do not stop. Instead, continue with install #1
UPDATE_INDEX=1
fi
# Check if running an updateall install
if [ -n "$UPDATE_INDEX" ] && [ ${UPDATE_INDEX} -ne 0 ]; then
# Find the next installed wallet, starting from the update index
i=${UPDATE_INDEX}; while [ $i -le 99 ]; do
case $i in
1) WALLET_DIR_TEST="${DEFAULT_WALLET_DIR}"
DATA_DIR_TEST="${DEFAULT_DATA_DIR}" ;;
*) WALLET_DIR_TEST="${DEFAULT_WALLET_DIR}${i}"
DATA_DIR_TEST="${DEFAULT_DATA_DIR}${i}" ;;
esac
if [ -d "${HOME_DIR}/${WALLET_DIR_TEST}" ] && [ -f "${USER_HOME_DIR}/${DATA_DIR_TEST}/${WALLET_CONFIG_NAME}" ]; then
# Set the update index
UPDATE_INDEX=${i}
# Return from loop
break
fi
i=$(( i + 1 ))
done
# Check if a wallet install was found
if [ ${i} -gt 99 ]; then
# No wallets found
echo && error_message "No installed wallets found"
else
# Update the install number based on the update index
INSTALL_NUM="${UPDATE_INDEX}"
# Check if this is the 1st/default wallet
if [ ${UPDATE_INDEX} -eq 1 ]; then
# Always do an os upgrade on the 1st/default wallet update
OSUPGRADE=1
else
# Skip o/s upgrade for all other wallet updates
OSUPGRADE=0
fi
# Prevent from re-creating the swap file
SWAP=0
# Prevent from re-installing the firewall
FIREWALL=0
# Prevent from re-installing brute-force protection
FAIL2BAN=0
# Prevent from waiting for the chain to fully sync
SYNCCHAIN=0
fi
fi
# Set install suffix
if [ "$INSTALL_NUM" -eq 1 ]; then
# 1st/default wallet has no suffix
INSTALL_SUFFIX=""
else
# All other wallets have a suffix of the Install #
INSTALL_SUFFIX="${INSTALL_NUM}"
fi
# Set install directories
DATA_INSTALL_DIR="${DEFAULT_DATA_DIR}${INSTALL_SUFFIX}"
WALLET_INSTALL_DIR="${DEFAULT_WALLET_DIR}${INSTALL_SUFFIX}"
# Validate command line arguments
if [ -n "${INITIAL_NET_TYPE}" ]; then
NET_TYPE="${INITIAL_NET_TYPE}"
fi
case $NET_TYPE in
4) ;;
6) ;;
*) echo && error_message "Invalid ip address type" ;;
esac
case $INSTALL_TYPE in
[iI]) INSTALL_TYPE="Install" ;;
[uU]) INSTALL_TYPE="Uninstall" ;;
*) echo && error_message "Invalid install type" ;;
esac
if [ "$INSTALL_TYPE" = "Install" ]; then
if [ -f ${USER_HOME_DIR}/${DATA_INSTALL_DIR}/${WALLET_CONFIG_NAME} ]; then
# Update install
if [ -z "$NULLGENKEY" ]; then
# Read the genkey value from the config file
NULLGENKEY=$(grep "masternodeprivkey" ${USER_HOME_DIR}/${DATA_INSTALL_DIR}/${WALLET_CONFIG_NAME} | sed -e "s/masternodeprivkey=//g")
fi
# Read network interface information
if [ -z "$NET_INTERFACE" ] && [ -f ${HOME_DIR}/${WALLET_INSTALL_DIR}/${NET_INTERFACE_CONFIG_NAME} ]; then
# Remember network interface from last install
NET_INTERFACE=$(cat "${HOME_DIR}/${WALLET_INSTALL_DIR}/${NET_INTERFACE_CONFIG_NAME}")
fi
# Read ip address information
if [ -f ${HOME_DIR}/${WALLET_INSTALL_DIR}/${IP4_CONFIG_NAME} ]; then
# Previous install was IPv4, check to see if it should still be IPv4
if [ -z "${WAN_IP}" ] && ([ -z "${INITIAL_NET_TYPE}" ] || [ "$INITIAL_NET_TYPE" -eq 4 ]); then
# Ensure that the IPv4 config file is re-created
WRITE_IP4_CONF=1
# Set install to IPv4
NET_TYPE=4
# Remember IPv4 address from last install
WAN_IP=$(cat "${HOME_DIR}/${WALLET_INSTALL_DIR}/${IP4_CONFIG_NAME}")
fi
# Initialize network variables
init_network
fi
if [ -f ${HOME_DIR}/${WALLET_INSTALL_DIR}/${IP6_CONFIG_NAME} ]; then
# Previous install was IPv6, check to see if it should still be IPv6
if [ -z "${WAN_IP}" ] && ([ -z "${INITIAL_NET_TYPE}" ] || [ "$INITIAL_NET_TYPE" -eq 6 ]); then
# Ensure that the IPv6 config file is re-created
WRITE_IP6_CONF=1
# Set install to IPv6
NET_TYPE=6
# Remember IPv6 address from last install
TEMP_WAN_IP=$(cat "${HOME_DIR}/${WALLET_INSTALL_DIR}/${IP6_CONFIG_NAME}")
# Ensure that the IPv6 address is valid
if [ -z "$(validate_ip6address ${TEMP_WAN_IP})" ]; then
WAN_IP="${TEMP_WAN_IP}"
fi
fi
# Initialize network variables
init_network
fi
if [ -z "${WAN_IP}" ] && [ -z "${INITIAL_NET_TYPE}" ]; then
# Read the ip address value from the config file
WAN_IP=$(grep "masternodeaddr" ${USER_HOME_DIR}/${DATA_INSTALL_DIR}/${WALLET_CONFIG_NAME} | sed -e "s/masternodeaddr=//g")
# Check if the WAN IP is blank
if [ -n "$WAN_IP" ]; then
# Check if this is an IPv6 or IPv4 address
if begins_with "[" "${WAN_IP}"; then
# Strip the []'s and port from this IPv6 address
WAN_IP=$(echo "${WAN_IP}" | cut -c2-$({ echo "${#WAN_IP} - $(printf "%s" "$(echo "${WAN_IP}" | rev | cut -d "]" -f1 | rev)" | wc -c)" | awk '{print $1 - $3 - 1}'; }))
NET_TYPE=6
else
# Strip the port from this IPv4 address
WAN_IP=$(echo "${WAN_IP}" | cut -c1-$({ echo "${#WAN_IP} - $(printf "%s" "$(echo "${WAN_IP}" | rev | cut -d ":" -f1 | rev)" | wc -c)" | awk '{print $1 - $3 - 1}'; }))
NET_TYPE=4
fi
fi
fi
if [ -z "$PORT_NUMBER_ARG" ]; then
# Read the port value from the config file
PORT_NUMBER=$(grep "^port=" ${USER_HOME_DIR}/${DATA_INSTALL_DIR}/${WALLET_CONFIG_NAME} | sed -e "s/port=//g")
# Check if port number was read correctly
if [ -z "$PORT_NUMBER" ]; then
# Port cannot be read. Revert back to default port
PORT_NUMBER=`expr $DEFAULT_PORT_NUMBER - 1`
fi
fi
fi
case $WALLET_TYPE in
[dD]) WALLET_TYPE="d" ;;
[bB]) WALLET_TYPE="b" ;;
*) echo && error_message "Invalid wallet type" ;;
esac
if [ -n "$NULLGENKEY" ] && [ -n "$(validate_genkey $NULLGENKEY)" ]; then
echo && error_message "Invalid masternode genkey value"
fi
if [ "$NET_TYPE" -eq 6 ]; then
# Setup IPv6 support before validating ip address
init_ipv6
else
# Initialize network variables
init_network
fi
if [ -n "$WAN_IP" ] && (([ "$NET_TYPE" -eq 4 ] && [ -n "$(validate_ip4address $WAN_IP)" ]) || ([ "$NET_TYPE" -eq 6 ] && [ -n "$(validate_ip6address $WAN_IP)" ])); then
echo && error_message "Invalid ip address"
fi
if [ -n "$PORT_NUMBER_ARG" ]; then
if [ -n "$(validate_port $PORT_NUMBER_ARG)" ]; then
echo && error_message "Invalid port #"
else
PORT_NUMBER=$PORT_NUMBER_ARG
fi
else
PORT_NUMBER=$(( $PORT_NUMBER + $INSTALL_NUM ))
fi
fi
if [ -n "$INSTALL_NUM" ] && [ -n "$(validate_install_num $INSTALL_NUM)" ]; then
echo && error_message "Invalid install #"
fi
# Get the current user id for later
readonly CURRENT_USER_ID=$(execute_command "id -ur")
# Welcome
welcome_screen
# Check if running an updateall install
if [ -z "${UPDATE_INDEX}" ] || [ ${UPDATE_INDEX} -eq 0 ]; then
# Not an updateall install
# Show default install title
echo && echo "${GREY}///////////////////////////////////////////////////////${NONE}"
# Center-align the title
i=1; while [ "$i" -le "$(((55-$(printf "%s" "${TITLE_STRING}" | wc -m))/2))" ]; do
echo -n " "
i=$(($i+1))
done
echo -n "${TITLE_STRING}"
echo && echo "${GREY}///////////////////////////////////////////////////////${NONE}"
else
# This is an updateall install
# Check if this is the first/default wallet
if [ ${UPDATE_INDEX} -eq 1 ]; then
# Add an extra blank line
echo
fi
# Show updateall install title
NEW_TITLE_STRING="Update wallet #${UPDATE_INDEX}"
echo "${GREY}///////////////////////////////////////////////////////${NONE}"
# Center-align the title
i=1; while [ "$i" -le "$(((55-$(printf "%s" "${NEW_TITLE_STRING}" | wc -m))/2))" ]; do
echo -n " "
i=$(($i+1))
done
echo -n "${NEW_TITLE_STRING}"
echo && echo "${GREY}///////////////////////////////////////////////////////${NONE}"
fi
# Wait 2 seconds with the splash screen up before starting
sleep 2 && echo
# Check if curl is installed
if [ "$({ dpkg-query --show --showformat='${db:Status-Status}\n' 'curl'; })" = "not-installed" ]; then
# Install curl
install_package "curl" "curl (required for script usage)"
fi
# Check if running an updateall install
if [ -z "${UPDATE_INDEX}" ] || [ ${UPDATE_INDEX} -eq 0 ] || [ ${UPDATE_INDEX} -eq 1 ]; then
# Check for an updated version of the script
echo "${CYAN}#####${NONE} Check for script update ${CYAN}#####${NONE}" && echo