|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Managed by Ansible - Do not edit manually |
| 3 | +# Updates the {{ ufw_blocklist_ipset_name }} ipset from a remote IPv4 blocklist |
| 4 | +set -euo pipefail |
| 5 | + |
| 6 | +IPSET_NAME="{{ ufw_blocklist_ipset_name }}" |
| 7 | +IPSET_TMP="${IPSET_NAME}-tmp" |
| 8 | +BLOCKLIST_URL="{{ ufw_blocklist_url }}" |
| 9 | +TMP_DIR="{{ ufw_blocklist_tmp_dir }}" |
| 10 | +TMP_FILE="${TMP_DIR}/blocklist_new.txt" |
| 11 | +CURRENT_FILE="${TMP_DIR}/blocklist_current.txt" |
| 12 | +IPSET_SAVE_FILE="{{ ufw_blocklist_ipset_save_file }}" |
| 13 | +LOG_TAG="ufw-blocklist" |
| 14 | +LOCK_FILE="/var/run/ufw-blocklist-update.lock" |
| 15 | +DRY_RUN=false |
| 16 | +VERBOSE=false |
| 17 | + |
| 18 | +for arg in "$@"; do |
| 19 | + case "$arg" in |
| 20 | + --dry-run|-n) DRY_RUN=true ;; |
| 21 | + -v|--verbose) VERBOSE=true ;; |
| 22 | + esac |
| 23 | +done |
| 24 | + |
| 25 | +log() { |
| 26 | + if $DRY_RUN; then |
| 27 | + echo "[DRY-RUN] $1" |
| 28 | + elif $VERBOSE; then |
| 29 | + echo "$1" |
| 30 | + fi |
| 31 | + logger -t "$LOG_TAG" "$1" |
| 32 | +} |
| 33 | + |
| 34 | +cleanup() { |
| 35 | + rm -f "$LOCK_FILE" "$TMP_FILE" "${TMP_FILE}.clean" |
| 36 | + # Destroy tmp set if it exists |
| 37 | + ipset destroy "$IPSET_TMP" 2>/dev/null || true |
| 38 | +} |
| 39 | + |
| 40 | +trap cleanup EXIT |
| 41 | + |
| 42 | +# Prevent concurrent runs |
| 43 | +if [ -f "$LOCK_FILE" ]; then |
| 44 | + LOCK_PID=$(cat "$LOCK_FILE" 2>/dev/null || true) |
| 45 | + if [ -n "$LOCK_PID" ] && kill -0 "$LOCK_PID" 2>/dev/null; then |
| 46 | + log "Another instance is already running (PID $LOCK_PID). Exiting." |
| 47 | + exit 0 |
| 48 | + fi |
| 49 | + log "Stale lock file found. Removing." |
| 50 | + rm -f "$LOCK_FILE" |
| 51 | +fi |
| 52 | +echo $$ > "$LOCK_FILE" |
| 53 | + |
| 54 | +# Ensure temp directory exists |
| 55 | +mkdir -p "$TMP_DIR" |
| 56 | + |
| 57 | +# Ensure the main ipset exists |
| 58 | +if ! ipset list "$IPSET_NAME" >/dev/null 2>&1; then |
| 59 | + if $DRY_RUN; then |
| 60 | + log "Would create ipset: ipset create $IPSET_NAME hash:ip maxelem 200000" |
| 61 | + else |
| 62 | + log "ipset $IPSET_NAME does not exist. Creating." |
| 63 | + ipset create "$IPSET_NAME" hash:ip maxelem 200000 |
| 64 | + fi |
| 65 | +fi |
| 66 | + |
| 67 | +# Download the blocklist |
| 68 | +log "Downloading blocklist from $BLOCKLIST_URL" |
| 69 | +if ! curl -fsSL --retry 3 --retry-delay 10 --max-time {{ ufw_blocklist_download_timeout }} \ |
| 70 | + -o "$TMP_FILE" "$BLOCKLIST_URL"; then |
| 71 | + log "ERROR: Failed to download blocklist. Aborting update." |
| 72 | + exit 1 |
| 73 | +fi |
| 74 | + |
| 75 | +# Validate and clean the downloaded file: keep only valid IPv4 addresses |
| 76 | +grep -Eo '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+(/[0-9]+)?$' "$TMP_FILE" | sort -u > "${TMP_FILE}.clean" |
| 77 | +mv "${TMP_FILE}.clean" "$TMP_FILE" |
| 78 | + |
| 79 | +NEW_COUNT=$(wc -l < "$TMP_FILE") |
| 80 | +if [ "$NEW_COUNT" -eq 0 ]; then |
| 81 | + log "ERROR: Downloaded blocklist is empty. Aborting update." |
| 82 | + exit 1 |
| 83 | +fi |
| 84 | + |
| 85 | +# Sanity check: reject suspiciously small lists (possible corruption) |
| 86 | +if [ -f "$CURRENT_FILE" ]; then |
| 87 | + CURRENT_COUNT=$(wc -l < "$CURRENT_FILE") |
| 88 | + if [ "$CURRENT_COUNT" -gt 0 ]; then |
| 89 | + MIN_EXPECTED=$(( CURRENT_COUNT * {{ ufw_blocklist_min_ratio }} / 100 )) |
| 90 | + if [ "$NEW_COUNT" -lt "$MIN_EXPECTED" ]; then |
| 91 | + log "WARNING: New list ($NEW_COUNT IPs) is less than {{ ufw_blocklist_min_ratio }}% of current ($CURRENT_COUNT IPs). Aborting." |
| 92 | + exit 1 |
| 93 | + fi |
| 94 | + fi |
| 95 | +fi |
| 96 | + |
| 97 | +log "Downloaded $NEW_COUNT IPs." |
| 98 | + |
| 99 | +if $DRY_RUN; then |
| 100 | + # In dry-run, show what would change |
| 101 | + if [ -f "$CURRENT_FILE" ]; then |
| 102 | + ADD_COUNT=$(comm -23 "$TMP_FILE" "$CURRENT_FILE" | wc -l) |
| 103 | + REMOVE_COUNT=$(comm -13 "$TMP_FILE" "$CURRENT_FILE" | wc -l) |
| 104 | + else |
| 105 | + ADD_COUNT="$NEW_COUNT" |
| 106 | + REMOVE_COUNT=0 |
| 107 | + fi |
| 108 | + log "Would add: $ADD_COUNT IPs, Would remove: $REMOVE_COUNT IPs, Total: $NEW_COUNT IPs" |
| 109 | + log "Would swap ipset $IPSET_TMP -> $IPSET_NAME" |
| 110 | + log "Would save ipset to $IPSET_SAVE_FILE" |
| 111 | + exit 0 |
| 112 | +fi |
| 113 | + |
| 114 | +# Create temporary ipset and populate it |
| 115 | +ipset create "$IPSET_TMP" hash:ip maxelem 200000 |
| 116 | + |
| 117 | +log "Populating temporary ipset $IPSET_TMP with $NEW_COUNT IPs..." |
| 118 | +while IFS= read -r ip; do |
| 119 | + [ -z "$ip" ] && continue |
| 120 | + ipset add "$IPSET_TMP" "$ip" 2>/dev/null || true |
| 121 | +done < "$TMP_FILE" |
| 122 | + |
| 123 | +# Atomic swap |
| 124 | +ipset swap "$IPSET_TMP" "$IPSET_NAME" |
| 125 | +log "Swapped $IPSET_TMP -> $IPSET_NAME" |
| 126 | + |
| 127 | +# Destroy the old set (now under the tmp name) |
| 128 | +ipset destroy "$IPSET_TMP" 2>/dev/null || true |
| 129 | + |
| 130 | +# Save for persistence across reboots |
| 131 | +ipset save "$IPSET_NAME" > "$IPSET_SAVE_FILE" |
| 132 | +log "Saved ipset to $IPSET_SAVE_FILE" |
| 133 | + |
| 134 | +# Save the current list for next run's sanity check |
| 135 | +cp "$TMP_FILE" "$CURRENT_FILE" |
| 136 | + |
| 137 | +TOTAL=$(ipset list "$IPSET_NAME" 2>/dev/null | grep -c "^[0-9]" || true) |
| 138 | +log "Update complete. Total blocked: $TOTAL IPs" |
0 commit comments