Skip to content

Instantly share code, notes, and snippets.

@thelazier
Created October 2, 2016 06:39
Show Gist options
  • Save thelazier/1f18748866cc2c11e29412b488ff1010 to your computer and use it in GitHub Desktop.
Save thelazier/1f18748866cc2c11e29412b488ff1010 to your computer and use it in GitHub Desktop.
Check if dash has peers from same IP more than limit or not, disconnect if exceeded.
#!/bin/bash
# Require Bash version >=4
# Require jq hexdump
# Set Dash Client command
DASH_CLI=/home/dashtest/bin/dash-cli
# Set Max connection per IP
MAX_CONN_IP=5
# List of peers
peers=$($DASH_CLI getpeerinfo |jq -r '.[]|"\(.addr)"')
# whitelist ips
whitelist="127.0.0.1 127.0.0.2"
# Processing
declare -A count
for addr in $peers
do
# Skip if IP in whitelist
ip=${addr%:*}
is_whitelist=0
for wip in $whitelist
do
if [ "$ip" == "$wip" ]; then is_whitelist=1; fi
done
if [ $is_whitelist -eq 1 ]; then continue; fi
# Count the connection node
vip=$(echo ${addr%:*}|hexdump -ve '/1 "%02x"')
if [ -z "${count[$vip]}" ]
then
count[$vip]=1
else
count[$vip]=$((${count[$vip]}+1))
fi
# Check Limit
if [ ${count[$vip]} -gt $MAX_CONN_IP ]; then
$DASH_CLI disconnectnode $addr
echo Exceeded connection from $ip for ${count[$vip]}/$MAX_CONN_IP , disconnect node $addr
fi
done
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment