-
Notifications
You must be signed in to change notification settings - Fork 0
/
peer-info.sh
executable file
·75 lines (60 loc) · 2.13 KB
/
peer-info.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
### Parse the output of an RPC's peerlist
### into a readable format including information
### whether the peer is in/outbound
### and the likely city the peer is in
### Author: Gaia from FreshSTAKING
### Enter the values specific to your RPC node
RPC_NODE="http://localhost:26657"
# If would like to see IP information for each peer,
# get a free API key from https://ipinfo.io
API_TOKEN="b160f016eb7662"
# This API token is being provided for scoring purposes
# and will be invalidated after scoring is done
### No need to touch from here down
peerlist=$(curl -s -H 'Connection: close' $RPC_NODE/net_info)
if [ $? -ne 0 ]; then
echo "Failed to fetch net_info from $RPC_NODE"
exit 1
fi
peerlist=$(curl -s -H 'Connection: close' $RPC_NODE/net_info | jq -r '.result.peers[] | "\(.node_info.id), \(.node_info.listen_addr), \(.remote_ip), \(.is_outbound)"')
sep="\n"
declare -A orgs
declare -A ips
output=""
total=0
outbound=0
while IFS= read -r line
do
id=$(echo $line | awk -F', ' '{print $1}')
ip=$(echo $line | awk -F', ' '{gsub(/tcp:\/\//, "", $2); split($2, a, ":"); print a[1]}')
remote_ip=$(echo $line | awk -F', ' '{print $3}')
is_outbound=$(echo $line | awk -F', ' '{print $4}' | tr -d '[:space:]')
ips[$ip]=1
ip=$remote_ip
ips[$remote_ip]=1
port=$(echo $line | awk -F', ' '{gsub(/tcp:\/\//, "", $2); split($2, a, ":"); print a[2]}')
if [[ "$is_outbound" == "true" ]]; then
outbound=$((outbound + 1))
output+="$id@$ip:$port, outbound, $sep"
else
output+="$id@$ip:$port, inbound, $sep"
fi
total=$((total + 1))
done <<< "$peerlist"
ip_list=$(printf ', "%s/city"' "${!ips[@]}")
ip_list=${ip_list:1}
orgs_json=$(curl -s -XPOST --data "[$ip_list]" "https://ipinfo.io/batch?token=$API_TOKEN")
for ip in "${!ips[@]}"; do
org=$(echo "$orgs_json" | jq -r ".\"$ip/city\"")
if [ "$org" == "null" ]; then
orgs[$ip]="Not Available"
else
orgs[$ip]=$org
fi
done
echo -e "$output" | while IFS= read -r line; do
if [[ -z "$line" ]]; then continue; fi
ip=$(echo $line | awk -F'[@:]' '{print $2}')
echo "$line ${orgs[$ip]}"
done
echo -e "\n============== This node has $total peers, out of which $outbound are outbound."