script feed from stdin via json
Squashed commit of the following: commitad482533fa
Author: AR2000 <ar2000@ar2000.me> Date: Thu Jan 16 15:33:16 2025 +0100 add jq to container commit79352ec046
Author: AR2000 <ar2000@ar2000.me> Date: Thu Jan 16 14:55:37 2025 +0100 stdin commit3100810052
Author: AR2000 <ar2000@ar2000.me> Date: Thu Jan 16 14:33:05 2025 +0100 test
This commit is contained in:
@@ -1,20 +1,20 @@
|
||||
ARG IPTABLES_MODE=nft
|
||||
|
||||
#install iptables
|
||||
FROM ubuntu:noble AS iptables
|
||||
FROM ubuntu:noble AS apt
|
||||
RUN --mount=type=cache,target=/var/lib/apt/lists,sharing=locked --mount=type=cache,target=/var/cache/apt,sharing=locked <<EOF
|
||||
apt update
|
||||
apt upgrade -y
|
||||
apt install iptables -y
|
||||
apt install iptables jq -y
|
||||
EOF
|
||||
|
||||
#set alternative to use iptables-legacy
|
||||
FROM iptables AS iptables-legacy
|
||||
FROM apt AS iptables-legacy
|
||||
RUN update-alternatives --set iptables /usr/sbin/iptables-legacy
|
||||
RUN update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy
|
||||
|
||||
#set alternative to use iptables-nft
|
||||
FROM iptables AS iptables-nft
|
||||
FROM apt AS iptables-nft
|
||||
RUN update-alternatives --set iptables /usr/sbin/iptables-nft
|
||||
RUN update-alternatives --set ip6tables /usr/sbin/ip6tables-nft
|
||||
|
||||
@@ -33,6 +33,7 @@ COPY --from=build-stage /app/cs-custom-bouncer/crowdsec-custom-bouncer \
|
||||
/usr/bin/crowdsec-custom-bouncer
|
||||
COPY --from=build-stage /app/cs-custom-bouncer/config/crowdsec-custom-bouncer.yaml \
|
||||
/crowdsec-custom-bouncer.yaml
|
||||
RUN sed -i "s/total_retries: .*/total_retries: 1/g" /crowdsec-custom-bouncer.yaml
|
||||
ADD --chmod=770 bouncer.sh /bouncer.sh
|
||||
ENV BINARY_PATH=/bouncer.sh
|
||||
LABEL me.ar2000.gitea.buildargs.iptablesmode="${IPTABLES_MODE}"
|
||||
|
21
bouncer.sh
21
bouncer.sh
@@ -2,16 +2,18 @@
|
||||
#
|
||||
# Script to add /remove IPs to iptables
|
||||
|
||||
[[ -n "${IPTABLES_COMMENT}" ]] && comment="-m comment --comment \"$4\"" || comment=""
|
||||
: ${IPTABLES_CHAIN:=INPUT}
|
||||
|
||||
function iptableAdd() {
|
||||
#check if the rule already exist
|
||||
if ! iptables $comment -C $IPTABLES_CHAIN -s "$1" -j DROP; then
|
||||
#do we insert at a $IPTABLES_INSERT position or append to the chain
|
||||
if [[ -z "${IPTABLES_INSERT}" ]]; then
|
||||
iptables $comment -A $IPTABLES_CHAIN -s "$1" -j DROP
|
||||
|
||||
else
|
||||
iptables $comment -I $IPTABLES_CHAIN "$IPTABLES_INSERT" -s "$1" -j DROP
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
function iptableDel() {
|
||||
@@ -19,17 +21,25 @@ function iptableDel () {
|
||||
}
|
||||
|
||||
function ip6tableAdd() {
|
||||
#check if the rule already exist
|
||||
if ! ip6tables $comment -C $IPTABLES_CHAIN -s "$1" -j DROP; then
|
||||
#do we insert at a $IPTABLES_INSERT position or append to the chain
|
||||
if [[ -z "${IPTABLES_INSERT}" ]]; then
|
||||
ip6tables $comment -A $IPTABLES_CHAIN -s "$1" -j DROP
|
||||
else
|
||||
ip6tables $comment -I $IPTABLES_CHAIN "$IPTABLES_INSERT" -s "$1" -j DROP
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
function ip6tableDel() {
|
||||
ip6tables $comment -D $IPTABLES_CHAIN -s "$1" -j DROP
|
||||
}
|
||||
|
||||
function processAction() {
|
||||
|
||||
[[ -n "${IPTABLES_COMMENT}" ]] && comment="-m comment --comment \"$4\"" || comment=""
|
||||
|
||||
#determine action
|
||||
if [ "$1" = "add" ]; then #add
|
||||
if [[ "$2" =~ .*[.].* ]]; then #ipv4
|
||||
@@ -50,3 +60,10 @@ elif [ "$1" = "del" ]; then #del
|
||||
else
|
||||
echo "unknown action"
|
||||
fi
|
||||
}
|
||||
|
||||
while read line; do
|
||||
echo processAction $(echo "$line" | jq -r .action) $(echo "$line" | jq -r .value) $(echo "$line" | jq -r .duration) $(echo "$line" | jq -r .scenario) | tee bouncer.sh.out
|
||||
processAction $(echo "$line" | jq -r .action) $(echo "$line" | jq -r .value) $(echo "$line" | jq -r .duration) $(echo "$line" | jq -r .scenario)
|
||||
done
|
||||
#{"duration":"-1h1m9s","origin":"CAPI","scenario":"crowdsecurity/ssh-bf","scope":"Ip","type":"ban","value":"122.117.32.192","id":22739513,"action":"del"}
|
||||
|
3
publish-dev.sh
Executable file
3
publish-dev.sh
Executable file
@@ -0,0 +1,3 @@
|
||||
#!/bin/bash
|
||||
docker image tag gitea.ar2000.me/ar2000/crowdsec-legacy-firewall-bouncer:legacy gitea.ar2000.me/ar2000/crowdsec-legacy-firewall-bouncer:dev
|
||||
docker push gitea.ar2000.me/ar2000/crowdsec-legacy-firewall-bouncer:dev
|
Reference in New Issue
Block a user