Last active
June 30, 2025 08:40
-
-
Save sycomix/1ecb01f9676da68d0d6b6abec94d71ef to your computer and use it in GitHub Desktop.
blanket ban states or countries on linux
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # GeoBlocker: Region-based IP banning using ipset + iptables | |
| BLOCK_DIR="/etc/ipblocker" | |
| BLOCK_LOG="/var/log/geo-block.log" | |
| IPSET_CONF="/etc/ipset.conf" | |
| mkdir -p "$BLOCK_DIR" | |
| function log() { | |
| echo "$(date +'%Y-%m-%d %H:%M:%S') $1" | tee -a "$BLOCK_LOG" | |
| } | |
| function setup() { | |
| log "[+] Installing dependencies..." | |
| apt update && apt install -y ipset iptables-persistent curl | |
| touch "$BLOCK_LOG" | |
| log "[β] Setup complete." | |
| } | |
| function create_ipset() { | |
| ipset create "block-$1" hash:net -exist | |
| } | |
| function add_region() { | |
| local region="$1" | |
| local source="$2" | |
| create_ipset "$region" | |
| log "[+] Adding region: $region" | |
| if [[ -f "$source" ]]; then | |
| log " Source: Local file" | |
| cat "$source" | while read -r ip; do | |
| ipset add "block-$region" "$ip" -exist | |
| done | |
| else | |
| log " Source: Remote URL" | |
| curl -s "$source" | while read -r ip; do | |
| ipset add "block-$region" "$ip" -exist | |
| done | |
| fi | |
| iptables -C INPUT -m set --match-set "block-$region" src -j DROP 2>/dev/null || \ | |
| iptables -I INPUT -m set --match-set "block-$region" src -j DROP | |
| log "[β] Region $region blocked." | |
| } | |
| function remove_region() { | |
| local region="$1" | |
| log "[Γ] Removing region: $region" | |
| iptables -D INPUT -m set --match-set "block-$region" src -j DROP 2>/dev/null | |
| ipset destroy "block-$region" | |
| } | |
| function list_regions() { | |
| echo "π Blocked Regions:" | |
| ipset list -name | grep "^block-" | |
| } | |
| function save_state() { | |
| log "[πΎ] Saving IP block state..." | |
| ipset save > "$IPSET_CONF" | |
| netfilter-persistent save | |
| log "[β] Save complete." | |
| } | |
| function help() { | |
| cat <<EOF | |
| Usage: $0 [command] [region] [source] | |
| Commands: | |
| setup Initialize environment | |
| add <region> <source> Add IPs to block (URL or local file) | |
| remove <region> Remove a region block | |
| list Show all active region blocks | |
| save Persist current ipset/iptables state | |
| help Display this menu | |
| Examples: | |
| $0 setup | |
| $0 add china https://www.ipdeny.com/ipblocks/data/countries/cn.zone | |
| $0 add florida /opt/geo/florida_cidrs.txt | |
| $0 remove china | |
| $0 list | |
| $0 save | |
| EOF | |
| } | |
| # Entry | |
| case "$1" in | |
| setup) setup ;; | |
| add) add_region "$2" "$3" ;; | |
| remove) remove_region "$2" ;; | |
| list) list_regions ;; | |
| save) save_state ;; | |
| help|*) help ;; | |
| esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment