-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathupdatepeers.sh
55 lines (47 loc) · 2.48 KB
/
updatepeers.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
#!/bin/bash
# @author ARAT - https://github.com/agent-rat/ada-stakepool
# @date 26/12/2019
# modified as standalone script by Straightpool - https://straightpool.github.io/about/
# @dependencies
# * sudo apt install jq
# * sudo wget http://www.vdberg.org/~richard/tcpping && chmod +x tcpping
# @notes
# * tcpping can be replaced by either nc or hping3
# * node-config can be in yaml or json format. This script only support json format
# Init vars
PATH_TCPPING='/usr/bin/tcpping'
PATH_NODE_CONFIG='/config/node-config-source.json' # File containing node config with trusted peers
PATH_NEW_NODE_CONFIG='/config/node-config-runtime.json' # New file to be generated with modified peers
PATH_TMP_PEER_RUNTIME='/config/tmp-runtime.json' # This is a temporary file generated by this script
# Extract json peer in multiline format so we are iterate them and filter them
# {"address":"/ip4/3.124.116.145/tcp/3000","id":"99cb10f53185fbef110472d45a36082905ee12df8a049b74"}
# {"address":"/ip4/52.28.91.178/tcp/3000","id":"23b3ca09c644fe8098f64c24d75d9f79c8e058642e63a28c"}
# {"address":"/ip4/3.125.75.156/tcp/3000","id":"22fb117f9f72f38b21bca5c0f069766c0d4327925d967791"}
JSON_PEER=`cat ${PATH_NODE_CONFIG} | jq -c -r '.p2p.trusted_peers[]' `
# Loop through line formatted json above and perform TCP ping
echo "Checking ${PATH_NODE_CONFIG} for trusted peers..."
VALID_JSON_PEER=''
for JPEER in $JSON_PEER; do
# Extract IP from line
IP=`echo $JPEER | jq '.address' | tr -d \" | cut -d/ -f3`
# Extract port from line
PORT=`echo $JPEER | jq '.address' | tr -d \" | cut -d/ -f5`
# Perform the test and grab the stdout result which should contain latency and state of port (open/close)
RESULT=`${PATH_TCPPING} -x 1 $IP $PORT`
echo $RESULT # Display it on the screen for debugging
# Test for open ports
if [[ $RESULT == *'[open]'* ]]; then
# Append to valid list of peers
if [[ $VALID_JSON_PEER != '' ]]; then
VALID_JSON_PEER="$VALID_JSON_PEER," # Special handling for anything other than first peer as we need to comma delimit the json entries
fi
VALID_JSON_PEER="$VALID_JSON_PEER$JPEER"
fi
done
echo ""
# Create a temp file of valid peers only
echo "[$VALID_JSON_PEER]" > ${PATH_TMP_PEER_RUNTIME}
# Replace trusted_peer list from original config, and inject the new valid ones from temp file. Produce a new runtime config file
echo "Regenerating ${PATH_NEW_NODE_CONFIG}..."
jq -n 'input | .p2p.trusted_peers = input' ${PATH_NODE_CONFIG} ${PATH_TMP_PEER_RUNTIME} > ${PATH_NEW_NODE_CONFIG}
echo ""