Skip to content

Commit

Permalink
feat(scripts): check_network_version nmap support
Browse files Browse the repository at this point in the history
  • Loading branch information
lklimek committed Aug 30, 2024
1 parent 5ba0e8f commit 15a63d6
Showing 1 changed file with 44 additions and 26 deletions.
70 changes: 44 additions & 26 deletions scripts/check_network_version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,26 @@ MASTERNODES=""
ALL=""

function help() {
if [[ -z "$1" ]]; then

echo This script connects to all masternodes and tries to detect their version.
echo ""
echo "Usage: $0 [-p | --port <port>] [-h | --help] [-m | --masternodes <masternodes.txt>] [-j | --json <masternodes.json>] [-w | --mnowatch] [-c | --csv <file>]"
echo "Options:"
echo " -p, --port <port> Port to connect to (default: $PORT)"
echo " -h, --help Show this help"
echo " -m, --masternodes <file> File with IP addresses of masternodes to check"
echo " -j, --json <file> File with masternodes.json in format generated by mnowatch"
echo " -w, --mnowatch Use mnowatch.org to get masternodes"
echo " -c, --csv <file> Save results to CSV file; default: $CSV"
echo ""
echo "Note: --masternodes, --json, and --mnowatch are mutually exclusive and the latest one takes precedence."
exit 1
fi

echo This script connects to all masternodes and tries to detect their version.
echo ""
echo "Usage: $0 [-p | --port <port>] [-h | --help] [-m | --masternodes <masternodes.txt>] [-j | --json <masternodes.json>] [-w | --mnowatch] [-c | --csv <file>]"
echo "Options:"
echo " -p, --port <port> Port to connect to (default: $PORT)"
echo " -h, --help Show this help"
echo " -m, --masternodes <file> File with IP addresses of masternodes to check"
echo " -j, --json <file> File with masternodes.json in format generated by mnowatch"
echo " -w, --mnowatch Use mnowatch.org to get masternodes"
echo " -c, --csv <file> Save results to CSV file; default: $CSV"
echo ""
echo "Note: --masternodes, --json, and --mnowatch are mutually exclusive and the latest one takes precedence."
exit 1
}

REPO="$(realpath "$(dirname "$0")/..")"

function load_masternodes_json() {
# curl https://mnowatch.org/json/?method=emn_details
# TODO: use curl above
jq 'map(select(.platformHTTPPortStatus != "CLOSED"))|map(select(.active_YNU!="N"))|map(select(.status!="P"))' "$1" |
jq 'map(select(.status!="P"))' "$1" |
jq -r .[].ip |
sort |
uniq
Expand All @@ -56,7 +52,9 @@ function grpc_platform {
-import-path "/repo/packages/dapi-grpc/protos/platform/v0" \
-proto platform.proto \
-max-time 30 \
-connect-timeout 5 \
-keepalive-time 1 \
-insecure \
"$@"
}

Expand All @@ -73,7 +71,7 @@ function grpc_platform {
function detect_platform() {
NODE="$1"

err="$(grpc_platform -insecure "$NODE:${PORT}" org.dash.platform.dapi.v0.Platform/getTotalCreditsInPlatform 2>&1)"
err="$(grpc_platform "$NODE:${PORT}" org.dash.platform.dapi.v0.Platform/getTotalCreditsInPlatform 2>&1)"

echo "============ $NODE ============" >>/tmp/detect_platform.log
echo "$err" >>/tmp/detect_platform.log
Expand All @@ -83,16 +81,19 @@ function detect_platform() {
echo 1.1
;;
*"upstream connect error or disconnect/reset before headers"*)
echo "upstream connect error"
echo "upstream connect error; misconfiguration or drive down"
;;
*"connect: connection refused"*)
echo "connection refused"
echo "connection refused; no platform or firewalled"
;;
*"Code: Unimplemented"*)
echo "unimplemented"
echo "1.0"
;;
*"context deadline exceeded"*)
echo "timeout; firewalled?"
;;
*)
echo "ERROR"
echo "$err"
;;
esac
}
Expand Down Expand Up @@ -125,6 +126,14 @@ function detect_core() {
fi
}

function run_nmap() {
nmap -n -Pn -p "443,9999,26656,80" -oG - $MASTERNODES
}

if [[ $# -eq 0 ]]; then
help
fi

# Parse arguments
while [[ "$1" == -* ]]; do
case "$1" in
Expand Down Expand Up @@ -154,6 +163,10 @@ while [[ "$1" == -* ]]; do
CSV="$2"
shift 2
;;
-h | --help)
help
shift 1
;;
*)
echo "Unknown option: $1"
exit 1
Expand Down Expand Up @@ -184,16 +197,21 @@ if [[ -z "$MASTERNODES" ]]; then
fi

# CSV header
echo "'no','masternode','platform','core'" >"$CSV"
echo "'no','masternode','platform','core','tenderdash','ports'" >"$CSV"

NMAP_RESULTS=$(run_nmap)

i=1
for MN in $MASTERNODES; do
echo "Checking status of evo node $MN ($i/$COUNT)"

PLATFORM="$(detect_platform "$MN")"
PORTS=$(echo "$NMAP_RESULTS" | grep "$MN.*Ports")
# CORE="$(detect_core "$MN")"
grep -v 26656/open <<<"$PORTS" >/dev/null
TENDERDASH=$?
# Format result as CSV
echo "$i,'$MN','$PLATFORM','$CORE'" >>"$CSV"
echo "$i,'$MN','$PLATFORM','$CORE','$TENDERDASH','$PORTS'" >>"$CSV"
i=$((i + 1))
done

Expand Down

0 comments on commit 15a63d6

Please sign in to comment.