geoblock-update.sh.j2 726 B

123456789101112131415161718192021222324252627
  1. #!/bin/bash
  2. set -euo pipefail
  3. ZONE_URL="{{ geoblock_zone_url }}"
  4. ZONE_FILE="{{ geoblock_zone_path }}"
  5. IPSET_NAME="{{ geoblock_ipset_name }}"
  6. IPSET_TMP="${IPSET_NAME}-tmp"
  7. mkdir -p "$(dirname "$ZONE_FILE")"
  8. curl -fsSL -o "$ZONE_FILE" "$ZONE_URL"
  9. ipset create "$IPSET_TMP" hash:net -exist
  10. ipset flush "$IPSET_TMP"
  11. while IFS= read -r cidr; do
  12. [[ -z "$cidr" || "$cidr" == \#* ]] && continue
  13. ipset add "$IPSET_TMP" "$cidr" -exist
  14. done < "$ZONE_FILE"
  15. ipset create "$IPSET_NAME" hash:net -exist
  16. ipset swap "$IPSET_TMP" "$IPSET_NAME"
  17. ipset destroy "$IPSET_TMP"
  18. if ! iptables -C OUTPUT -m set --match-set "$IPSET_NAME" dst -j DROP 2>/dev/null; then
  19. iptables -A OUTPUT -m set --match-set "$IPSET_NAME" dst -j DROP
  20. fi