Files
crowdsec-legacy-firewall-bo…/bouncer.sh
2024-11-10 14:08:38 +01:00

56 lines
1.6 KiB
Bash
Executable File

#!/bin/bash
#
# Script to add /remove IPs to iptables
[[ -z "${IPTABLES_COMMENT}" ]] && commment="-m comment --comment \"$4\"" || comment=""
function iptableAdd () {
if [[ -z "${IPTABLES_INSERT}" ]]; then
iptables $comment -A INPUT -s $1 -j DROP
iptables $comment -A DOCKER-USER -s $1 -j DROP
else
iptables $comment -I INPUT $IPTABLES_INSERT -s $1 -j DROP
iptables $comment -I DOCKER-USER $IPTABLES_INSERT -s $1 -j DROP
fi
}
function iptableDel () {
iptables $comment -D INPUT -s $1 -j DROP
iptables $comment -D DOCKER-USER -s $1 -j DROP
}
function ip6tableAdd () {
if [[ -z "${IPTABLES_INSERT}" ]]; then
ip6tables $comment -A INPUT -s $1 -j DROP
ip6tables $comment -A DOCKER-USER -s $1 -j DROP
else
ip6tables $comment -I INPUT $IPTABLES_INSERT -s $1 -j DROP
ip6tables $comment -I DOCKER-USER $IPTABLES_INSERT -s $1 -j DROP
fi
}
function ip6tableDel () {
ip6tables $comment -D INPUT -s $1 -j DROP
ip6tables $comment -D DOCKER-USER -s $1 -j DROP
}
#determine action
if [ "$1" = "add" ]; then #add
if [[ "$2" =~ .*[.].* ]]; then #ipv4
echo "add $2 for $3 with $4"
iptableAdd $2
elif [[ "$2" =~ .*[:].* ]]; then #ipv6
echo "IPV6 : add $2 for $3 with $4"
ip6tableAdd $2
fi
elif [ "$1" = "del" ]; then #del
if [[ "$2" =~ .*[.].* ]]; then #ipv4
echo "del $2 for $3 with $4"
iptableDel $2
elif [[ "$2" =~ .*[:].* ]]; then #ipv6
echo "IPV6 : add $2 for $3 with $4"
ip6tableDel $2
fi
else
echo "unknon action"
fi