This commit is contained in:
Robert Vojcik 2026-02-16 10:27:20 +00:00 committed by GitHub
commit 8e4714df15
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -0,0 +1,80 @@
#
# Author: Robert Vojcik
# Based on: cloudflare.conf by Mike Rushton
#
# This action using global cloudflare IP Lists.
# Add and remove IP addresses to IP lists instead of editing Access Rules.
# You could define rules according your needs and use IP List in it.
# This action only manage content of the IP list
#
# IMPORTANT
#
# Please set jail.local's permission to 640 because it contains your CF API token.
#
# This action depends on curl.
#
# To get your Cloudflare API token: https://developers.cloudflare.com/api/tokens/create/
[Definition]
# Option: actionstart
# Notes.: command executed on demand at the first ban (or at the start of Fail2Ban if actionstart_on_demand is set to false).
# Values: CMD
#
actionstart =
# Option: actionstop
# Notes.: command executed at the stop of jail (or at the end of Fail2Ban)
# Values: CMD
#
actionstop =
# Option: actioncheck
# Notes.: command executed once before each actionban command
# Values: CMD
#
actioncheck =
# Option: actionban
# Notes.: command executed when banning an IP. Take care that the
# command is executed with Fail2Ban user rights.
# Tags: <ip> IP address
# <failures> number of failures
# <time> unix timestamp of the ban time
# Values: CMD
actionban = curl -s -X PUT "<_cf_api_url>" \
<_cf_api_prms> \
--data '[{"ip":"<ip>", "comment":"<notes>"}]'
# Option: actionunban
# Notes.: command executed when unbanning an IP. Take care that the
# command is executed with Fail2Ban user rights.
# Tags: <ip> IP address
# <failures> number of failures
# <time> unix timestamp of the ban time
# Values: CMD
#
actionunban = id=$(curl -s -X GET <_cf_api_prms> \
"<_cf_api_url>?ip=<ip>" \
| { jq -r '.result[0].id' 2>/dev/null || awk -F"[,:}]" '{for(i=1;i<=NF;i++){if($i~/'id'\042/){print $(i+1)}}}' | tr -d ' "' | head -n 1; })
if [ -z "$id" ]; then echo "<name>: id for <ip> cannot be found using list <cflist>"; exit 0; fi;
curl -s -o /dev/null -X DELETE <_cf_api_prms> "<_cf_api_url>" --data "{\"items\": [{\"id\":\"$id\"}]}"
_cf_api_url = https://api.cloudflare.com/client/v4/accounts/<cfaccount>/rules/lists/<cflist>/items
_cf_api_prms = -H "Authorization: Bearer <cftoken>" -H "Content-Type: application/json"
[Init]
# The Cloudflare <ACCOUNT_ID> of your account
#
# cfaccount =
# Token must be created with at least Account.Account Filter Lists edit rights
# cftoken =
# After you create new IP List, get its id to cflist variable
# cflist =
# The message to include in IP comment in the IP List.
#
notes = Fail2Ban <name>