after test too much I got blocked to 81.3.27.55
so I vibe script instead.
port it here may help others.
#!/bin/bash
#Ex: sudo -u bind DblDownloader.sh -q porn phishing gambling malware
#License GPL-v2
#Vibe coding by densin@linux.in.th buddy with gemini
QUIET=false
RPZPATH=“/etc/bind/zones.rpz”
#RPZPATH=“zones.rpz” #debug
BLOCKLIST=() # can add default zone herer
download_new() {
local BASE_FILE=“$1”
local URL=“$2”
if [ -z "$BASE_FILE" ] || [ -z "$URL" ]; then
$QUIET || echo "Usage: download_new <base_filename> <url>"
return 1
fi
# Automatically append .new to the designated output file
local OUTPUT_FILE="${BASE_FILE}.new"
# --- Step 0: Date Freshness Check ---
# Check the server against your existing BASE_FILE (e.g., db.example.com)
if [ -f "$BASE_FILE" ]; then
local REMOTE_LAST_MODIFIED=$(curl -sIL "$URL" | grep -i '^last-modified:' | tail -n 1 | cut -d: -f2- | tr -d '\r')
if [ -n "$REMOTE_LAST_MODIFIED" ]; then
local REMOTE_EPOCH=$(date -d "$REMOTE_LAST_MODIFIED" +%s 2>/dev/null)
local LOCAL_EPOCH=$(date -r "$BASE_FILE" +%s 2>/dev/null)
if [ -n "$REMOTE_EPOCH" ] && [ -n "$LOCAL_EPOCH" ] && [ "$LOCAL_EPOCH" -ge "$REMOTE_EPOCH" ]; then
$QUIET || echo "⏭️ SKIPPED: Local file '$BASE_FILE' is already up-to-date."
return 0
fi
fi
fi
$QUIET || echo "Downloading to $OUTPUT_FILE..."
# --- Step 1: Download ---
local HEADER_FILE=$(mktemp)
local HTTP_STATUS=$(curl -sL -w "%{http_code}" -D "$HEADER_FILE" -o "$OUTPUT_FILE" "$URL")
# --- Step 2: HTTP Status Check ---
if [ "$HTTP_STATUS" -ne 200 ]; then
$QUIET || echo "❌ FAILED: HTTP $HTTP_STATUS"
[ -f "$OUTPUT_FILE" ] && rm "$OUTPUT_FILE"
rm "$HEADER_FILE"
return 1
fi
# --- Step 3: Size Check ---
local EXPECTED_SIZE=$(grep -i '^content-length:' "$HEADER_FILE" | awk '{print $2}' | tr -d '\r' | tail -n 1)
local ACTUAL_SIZE=$(wc -c < "$OUTPUT_FILE" | tr -d ' ')
rm "$HEADER_FILE"
if [ -n "$EXPECTED_SIZE" ]; then
if [ "$ACTUAL_SIZE" -eq "$EXPECTED_SIZE" ]; then
$QUIET || echo "✅ SUCCESS: $OUTPUT_FILE ($ACTUAL_SIZE bytes)"
return 0
else
$QUIET || echo "❌ ERROR: Size mismatch. Expected $EXPECTED_SIZE, got $ACTUAL_SIZE. Removing $OUTPUT_FILE."
[ -f "$OUTPUT_FILE" ] && rm "$OUTPUT_FILE"
return 2
fi
else
$QUIET || echo "⚠️ FINISHED: $OUTPUT_FILE ($ACTUAL_SIZE bytes) - No size header to verify."
return 0
fi
}
zonefilter() {
local ZONE_NAME=“$1”
local ZONE_FILE=“$2”
# Input Validation
if [ -z "$ZONE_NAME" ] || [ -z "$ZONE_FILE" ]; then
$QUIET || echo "Usage: zonefilter <zone_name> <zone_file>"
return 1
fi
if [ ! -f "$ZONE_FILE" ]; then
$QUIET || echo "❌ ERROR: File '$ZONE_FILE' does not exist."
return 1
fi
# Run check based on global QUIET variable
if $QUIET; then
named-checkzone "$ZONE_NAME" "$ZONE_FILE" &> /dev/null
local EXIT_CODE=$?
else
echo "Filtering zone '$ZONE_NAME' via named-checkzone..."
named-checkzone "$ZONE_NAME" "$ZONE_FILE"
local EXIT_CODE=$?
fi
# Handle errors by deleting the file
if [ $EXIT_CODE -ne 0 ]; then
$QUIET || echo "❌ ERROR detected in '$ZONE_FILE'. Deleting error file."
[ -f "$ZONE_FILE" ] && rm "$ZONE_FILE"
return 3
fi
$QUIET || echo "✅ PASSED: '$ZONE_FILE' is a valid zone."
return 0
}
replacefile() {
local SRC_FILE=“$1”
local DST_FILE=“$2”
# Input Validation
if [ -z "$SRC_FILE" ] || [ -z "$DST_FILE" ]; then
$QUIET || echo "Usage: replacefile <src_file> <dst_file>"
return 1
fi
# Check if source file exists before doing anything
if [ -f "$SRC_FILE" ]; then
# Move source to destination (overwriting destination if it exists)
if mv "$SRC_FILE" "$DST_FILE"; then
return 0
else
return 1
fi
else
# If src doesn't exist, we do nothing and return a distinct exit code (e.g., 4)
return 4
fi
}
while [[ $# -gt 0 ]]; do
case $1 in
-h|–help)
echo “IpfireDblDownloader.sh [-q] <blocklist … >”
shift
exit 1
;;
-q|–quiet)
QUIET=true
shift
;;
*)
BLOCKLIST+=(“$1”)
shift
;;
esac
done
#mkdir -p ${RPZPATH}
if [ ! -d “$RPZPATH” ]; then
echo “Error: Directory ‘$RPZPATH’ does not exist.” >&2
echo "Create Directory or change RPZPATH in script "
echo " make sure about permission and bind config "
exit 1
fi
for BLOCK in “${BLOCKLIST[@]}”; do
download_new ${RPZPATH}“/”$BLOCK".rpz" https://dbl.ipfire.org/lists/${BLOCK}/rpz.zone
zonefilter ${BLOCK}“.rpz.ipfire.org” ${RPZPATH}“/”$BLOCK".rpz.new"
replacefile ${RPZPATH}“/”$BLOCK".rpz.new" ${RPZPATH}“/”$BLOCK".rpz"
done
rndc reload
######################## end ####################