Skip to content

Commit

Permalink
Reduce API hits and reduce complexity of iptables/nft client bash scr…
Browse files Browse the repository at this point in the history
…ipts.
  • Loading branch information
qwell committed Oct 21, 2020
1 parent bc13c72 commit 7da0749
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 25 deletions.
17 changes: 5 additions & 12 deletions clients/bash/apiban-nft.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ if [ ! -e "${CONFIG}" ] ; then
fi

# APIKEY and last known ID are stored in apibanconfig.sys
APIKEY=$(grep "APIKEY" $CONFIG | cut -d '=' -f 2)
LKID=$(grep "LKID" $CONFIG | cut -d '=' -f 2)
source $CONFIG

# Exit if no APIKEY
if [ -v "$APIKEY" ] ; then
Expand All @@ -54,8 +53,9 @@ if [ -z "$CURRIPS" ] ; then
nft insert rule ip filter FORWARD counter jump APIBAN
fi

IPADDRESS=$(curl -s https://apiban.org/api/$APIKEY/banned/$LKID | jq -r ".ipaddress?")
CURRID=$(curl -s https://apiban.org/api/$APIKEY/banned/$LKID | jq -r ".ID?")
BANLIST=$(curl -s https://apiban.org/api/$APIKEY/banned/$LKID)
IPADDRESS=$(echo $BANLIST | jq -r ".ipaddress? | .[]")
CURRID=$(echo $BANLIST | jq -r ".ID?")

# No new bans
if [ "$CURRID" = "none" ] ; then
Expand All @@ -74,14 +74,7 @@ fi
sed -i "s/^\(LKID=\).*$/\1${CURRID}/" $CONFIG

# parse through IPs
IPADDRESS=${IPADDRESS//$'\n'/}
IPADDRESS=${IPADDRESS//$'\"'/}
IPADDRESS=${IPADDRESS//$'['/}
IPADDRESS=${IPADDRESS//$']'/}
IPADDRESS=${IPADDRESS//$', '/}

IPADDRESSARR=($IPADDRESS)

IPADDRESSARR=(${IPADDRESS//$'\"'/})
for i in "${IPADDRESSARR[@]}"
do
NOW=$(date +"%Y-%m-%d %H:%M:%S")
Expand Down
19 changes: 6 additions & 13 deletions clients/bash/apiban.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ if [ ! -e "${CONFIG}" ] ; then
fi

# APIKEY and last known ID are stored in apibanconfig.sys
APIKEY=$(grep "APIKEY" $CONFIG | cut -d '=' -f 2)
LKID=$(grep "LKID" $CONFIG | cut -d '=' -f 2)
source $CONFIG

# Exit if no APIKEY
if [ -v "$APIKEY" ] ; then
Expand All @@ -54,8 +53,9 @@ if [ -z "$CURRIPS" ] ; then
iptables -I FORWARD -j APIBAN
fi

IPADDRESS=$(curl -s https://apiban.org/api/$APIKEY/banned/$LKID | jq -r ".ipaddress?")
CURRID=$(curl -s https://apiban.org/api/$APIKEY/banned/$LKID | jq -r ".ID?")
BANLIST=$(curl -s https://apiban.org/api/$APIKEY/banned/$LKID)
IPADDRESS=$(echo $BANLIST | jq -r ".ipaddress? | .[]")
CURRID=$(echo $BANLIST | jq -r ".ID?")

# No new bans
if [ "$CURRID" = "none" ] ; then
Expand All @@ -74,14 +74,7 @@ fi
sed -i "s/^\(LKID=\).*$/\1${CURRID}/" $CONFIG

# parse through IPs
IPADDRESS=${IPADDRESS//$'\n'/}
IPADDRESS=${IPADDRESS//$'\"'/}
IPADDRESS=${IPADDRESS//$'['/}
IPADDRESS=${IPADDRESS//$']'/}
IPADDRESS=${IPADDRESS//$', '/}

IPADDRESSARR=($IPADDRESS)

IPADDRESSARR=(${IPADDRESS//$'\"'/})
for i in "${IPADDRESSARR[@]}"
do
NOW=$(date +"%Y-%m-%d %H:%M:%S")
Expand All @@ -93,4 +86,4 @@ do
fi
done

echo "$NOW - All done. Exiting." >> $LOG
echo "$NOW - All done. Exiting." >> $LOG

0 comments on commit 7da0749

Please sign in to comment.