forked from matsuro-hadouken/casper-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
explorer.sh
executable file
·221 lines (144 loc) · 6.51 KB
/
explorer.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
#!/bin/bash
# We can not get all IP's anymore, explorer show only connected peers to specified host.
# Works with 'Delta-11'
# Matsuro Hadouken <[email protected]> 2020
# This file is free software; as a special exception the author gives
# unlimited permission to copy and/or distribute it, with or without
# modifications, as long as this notice is preserved.
# PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND
LOCAL_HTTP_PORT='8888' # set target if any
HostLocalNetwork='127.0.0.1' # set target if any
IPv4_STRING='(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)'
RED='\033[0;31m'
GREEN='\033[0;32m'
CYAN='\033[0;36m'
WHITE='\033[0;37m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
NC='\033[0m'
ValidatorsCount="0"
UselessHosts="0"
BlockedHTTP="0"
AvailableHosts="0"
header="${CYAN} %-10s %-16s %10s %20s %9s %7s %11s\n${NC}"
COLUMNS=$(tput cols)
divider=$(printf ${YELLOW}"%${COLUMNS}s" " "${NC} | tr " " "-")
width=101
function Report() {
NC='\033[0m'
color=$(echo "$@" | cut -d" " -f1)
msg=$(echo "$*" | cut -d" " -f2-)
[[ $color == WHITE ]] && color="$WHITE"
[[ $color == RED ]] && color="$RED"
[[ $color == GREEN ]] && color="$GREEN"
[[ $color == CYAN ]] && color="$CYAN"
echo -e "$color$msg${NC}" && echo
}
function Seeds() {
declare -a TrustedHashArray
echo && printf "$header" "STATUS" "IP ADDRESS" "TRUSTED HASH" "CHAIN NAME" "ERA" "HEIGH" "VERSION"
printf "%$width.${width}s\n" "$divider"
read -r -a trustedHosts < <(echo $(cat /etc/casper/1_0_0/config.toml | grep 'known_addresses = ' | grep -E -o "$IPv4_STRING"))
for seed_ip in "${trustedHosts[@]}"; do
GetPeerData "$seed_ip"
TrustedHashArray+=("$LastAddedBlockHash")
printf "$format" "$HostStatus" "$seed_ip" "$last_added_block_hash" "$chainspec_name" "$era_id" "$chain_height" "$build_version"
done
printf "%$width.${width}s\n" "$divider"
if ! [[ "${TrustedHashArray[0]}" =~ ${TrustedHashArray[1]} ]] && ! [[ "${TrustedHashArray[1]}" =~ ${TrustedHashArray[2]} ]]; then
Report RED "Trusted validators hash missmatch, please run script again." && exit
else
ReferenceChainspec=$chainspec_name
ReferenceBuildVersion=$build_version
ReferenceIP=$seed_ip
TrustedHash=$LastAddedBlockHash
fi
}
function Condition() {
if [[ "${#LastAddedBlockHash}" -eq 64 ]] && [[ ! "$LastAddedBlockHash" =~ 'null' ]]; then
format=" ${GREEN}%-10s${NC} %-16s %17s %19s %5s %6s %10s\n"
HostStatus='Trusted'
elif [[ "$LastAddedBlockHash" =~ 'null' ]]; then
format=" ${CYAN}%-10s${NC} %-16s %17s %19s %5s %6s %10s\n"
HostStatus='Genesis'
else
format=" ${RED}%-10s${NC} %-16s %17s %19s %5s %6s %10s\n"
HostStatus='Bogus'
fi
}
function GetPeerData() {
validator_ip="$1"
Stage="$2"
PeerDataList=$(curl -s --connect-timeout 2 --max-time 2 "http://$validator_ip:$LOCAL_HTTP_PORT/status" | jq -r '.build_version, .chainspec_name, .last_added_block_info.hash, .last_added_block_info.era_id, .last_added_block_info.height')
if ! [[ $PeerDataList ]]; then
format=" ${YELLOW}%-10s %-16s %17s %19s %5s %6s %10s${NC}\n"
HostStatus='Blocked'
build_version=''
chainspec_name=''
last_added_block_hash=''
era_id=''
chain_height=''
BlockedHTTP=$((BlockedHTTP + 1))
return
fi # NO HTTP ACCESS OR DEAD PEER
readarray -t PeerDataArray <<<"$PeerDataList"
LastAddedBlockHash=${PeerDataArray[2]}
build_version=${PeerDataArray[0]}
chainspec_name=${PeerDataArray[1]}
if [[ $LastAddedBlockHash == "null" ]]; then
last_added_block_hash="null"
else
last_added_block_hash="$(echo "$LastAddedBlockHash" | cut -c1-5) .... $(echo "$LastAddedBlockHash" | cut -c59-64)"
fi
era_id=${PeerDataArray[3]}
chain_height=${PeerDataArray[4]}
if [[ "$Stage" =~ "COUNT" ]]; then
ValidatorsCount=$((ValidatorsCount + 1))
else
Condition
fi
}
function CheckPeers() {
read -r -a peers_list < <(echo $(curl -s "http://$HostLocalNetwork:$LOCAL_HTTP_PORT/status" | jq .peers | grep -E -o "$IPv4_STRING"))
for peer_ip in "${peers_list[@]}"; do
format=" %-10s %-16s %17s %19s %5s %6s %10s\n"
HostStatus='Available'
GetPeerData "$peer_ip" "COUNT"
if ! [[ $HostStatus =~ 'Blocked' ]] && ! [[ $chainspec_name =~ $ReferenceChainspec ]]; then
format=" ${RED}%-10s %-16s %-17s %19s %5s %6s %10s${NC}\n"
HostStatus='Useless' && echo "$peer_ip" >> useless.peers
UselessHosts=$((UselessHosts + 1))
elif ! [[ $HostStatus =~ 'Blocked' ]] && [[ $chainspec_name =~ $ReferenceChainspec ]]; then
AvailableHosts=$((AvailableHosts + 1))
fi
# 0 1 2 3 4 5 6
# "STATUS" "IP ADDRESS" "TRUSTED HASH" "CHAIN NAME" "ERA" "HEIGH" "VERSION"
printf "$format" "$HostStatus" "$peer_ip" "$last_added_block_hash" "$chainspec_name" "$era_id" "$chain_height" "$build_version"
done
}
function CheckActiveHost() {
# check if run from active active validator host, so it will be +1
if [[ $(curl -s "http://$HostLocalNetwork:$LOCAL_HTTP_PORT/status" | jq -r .api_version) ]]; then
GetPeerData "$HostLocalNetwork" "COUNT"
HostStatus='HOST'
peer_ip="$HostLocalNetwork"
if [[ $chainspec_name =~ $ReferenceChainspec ]]; then
format=" ${BLUE}%-10s${NC} %-16s %-17s %19s %5s %6s %10s${NC}\n"
AvailableHosts=$((AvailableHosts + 1))
IsHostActive='true'
else
format=" ${RED}%-10s${NC} %-16s ${RED}%-17s${NC} %19s %5s %6s %10s${NC}\n"
UselessHosts=$((UselessHosts + 1)) # THIS NEED TO REWRITE WITH CHECK FOR OUTSIDE PORT ACCESS ( BLOCKED / AWAILABLE )
IsHostActive='false'
fi
fi
printf "$format" "$HostStatus" "$peer_ip" "$last_added_block_hash" "$chainspec_name" "$era_id" "$chain_height" "$build_version"
}
Seeds
CheckActiveHost
CheckPeers
echo && echo -e "${CYAN}Trusted hash:${NC} ${GREEN}$TrustedHash${NC}" && echo
echo -e "${CYAN}Peers connected: ${GREEN}$ValidatorsCount${NC}" && echo
echo -e "${CYAN}Useless peers: ${GREEN}$UselessHosts${NC}"
echo -e "${CYAN}Blocked access: ${GREEN}$BlockedHTTP${NC}"
echo -e "${CYAN}Good condition: ${GREEN}$((ValidatorsCount - UselessHosts - BlockedHTTP))${NC}" && echo