CountryBlock/start.sh
2025-06-23 15:58:11 +01:00

68 lines
2.6 KiB
Bash

#!/bin/bash
set -euo pipefail
echo -e "
░█████╗░███╗░░██╗██████╗░██████╗░░█████╗░░██████╗
██╔══██╗████╗░██║██╔══██╗██╔══██╗██╔══██╗██╔════╝
███████║██╔██╗██║██║░░██║██║░░██║██║░░██║╚█████╗░
██╔══██║██║╚████║██║░░██║██║░░██║██║░░██║░╚═══██╗
██║░░██║██║░╚███║██████╔╝██████╔╝╚█████╔╝██████╔╝
╚═╝░░╚═╝╚═╝░░╚══╝╚═════╝░╚═════╝░░╚════╝░╚═════╝░"
COUNTRIES="cn ru us in br id vn pk tr ir eg bd mx za ua kr ng ph"
ZONE_DIR="zone"
mkdir -p "$ZONE_DIR"
function add_iptables_rule() {
local chain=$1
local set_name=$2
if iptables -t raw -C "$chain" -m set --match-set "$set_name" src -j DROP 2>/dev/null; then
echo "Regra iptables já existe: $chain$set_name"
else
echo "Inserindo regra iptables: $chain$set_name"
iptables -t raw -I "$chain" -m set --match-set "$set_name" src -j DROP
fi
}
for country in $COUNTRIES; do
url="https://www.ipdeny.com/ipblocks/data/countries/${country}.zone"
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Baixando lista IPs: $country"
tmp_file="${ZONE_DIR}/${country}.zone.tmp"
target_file="${ZONE_DIR}/${country}.zone"
if curl -sSf -o "$tmp_file" "$url"; then
if [ -f "$target_file" ] && cmp -s "$tmp_file" "$target_file"; then
echo "[$country] Arquivo inalterado."
rm "$tmp_file"
else
echo "[$country] Atualizando IPs."
mv "$tmp_file" "$target_file"
fi
else
echo "[$country] Falha no download. Mantendo antigo (se existir)."
[ -f "$tmp_file" ] && rm "$tmp_file"
continue
fi
if ipset list "$country" &>/dev/null; then
echo "[$country] Limpando ipset antigo..."
ipset flush "$country"
else
echo "[$country] Criando novo ipset..."
ipset create "$country" hash:net
fi
while read -r subnet; do
ipset add "$country" "$subnet" -exist || echo "Falha ao adicionar: $subnet"
done < "$target_file"
add_iptables_rule PREROUTING "$country"
echo "[$country] Proteção configurada."
done
echo "[$(date '+%Y-%m-%d %H:%M:%S')] ✅ Proteção geográfica concluída!"