This commit is contained in:
Bxio 2025-06-23 15:58:11 +01:00
parent 8a6de01f48
commit b75ecef339

View File

@ -9,7 +9,7 @@ echo -e "
██║░░██║██║░╚███║██████╔╝██████╔╝╚█████╔╝██████╔╝ ██║░░██║██║░╚███║██████╔╝██████╔╝╚█████╔╝██████╔╝
╚═╝░░╚═╝╚═╝░░╚══╝╚═════╝░╚═════╝░░╚════╝░╚═════╝░" ╚═╝░░╚═╝╚═╝░░╚══╝╚═════╝░╚═════╝░░╚════╝░╚═════╝░"
COUNTRIES="af es" COUNTRIES="cn ru us in br id vn pk tr ir eg bd mx za ua kr ng ph"
ZONE_DIR="zone" ZONE_DIR="zone"
mkdir -p "$ZONE_DIR" mkdir -p "$ZONE_DIR"
@ -19,56 +19,49 @@ function add_iptables_rule() {
local set_name=$2 local set_name=$2
if iptables -t raw -C "$chain" -m set --match-set "$set_name" src -j DROP 2>/dev/null; then if iptables -t raw -C "$chain" -m set --match-set "$set_name" src -j DROP 2>/dev/null; then
echo "Regra iptables para ipset '$set_name' já existe na cadeia $chain." echo "Regra iptables já existe: $chain$set_name"
else else
echo "Inserindo regra iptables para ipset '$set_name' na cadeia $chain..." echo "Inserindo regra iptables: $chain$set_name"
iptables -t raw -I "$chain" -m set --match-set "$set_name" src -j DROP iptables -t raw -I "$chain" -m set --match-set "$set_name" src -j DROP
fi fi
} }
for country in $COUNTRIES; do for country in $COUNTRIES; do
url="https://www.ipdeny.com/ipblocks/data/countries/${country}.zone" url="https://www.ipdeny.com/ipblocks/data/countries/${country}.zone"
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Tentando baixar lista de IPs para o país: $country" echo "[$(date '+%Y-%m-%d %H:%M:%S')] Baixando lista IPs: $country"
tmp_file="${ZONE_DIR}/${country}.zone.tmp" tmp_file="${ZONE_DIR}/${country}.zone.tmp"
target_file="${ZONE_DIR}/${country}.zone" target_file="${ZONE_DIR}/${country}.zone"
if curl -sSf -o "$tmp_file" "$url"; then if curl -sSf -o "$tmp_file" "$url"; then
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Download concluído para $country."
if [ -f "$target_file" ] && cmp -s "$tmp_file" "$target_file"; then if [ -f "$target_file" ] && cmp -s "$tmp_file" "$target_file"; then
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Arquivo para $country não mudou. Mantendo versão atual." echo "[$country] Arquivo inalterado."
rm "$tmp_file" rm "$tmp_file"
else else
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Arquivo para $country mudou ou não existia. Atualizando..." echo "[$country] Atualizando IPs."
mv "$tmp_file" "$target_file" mv "$tmp_file" "$target_file"
fi fi
else else
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Falha no download para $country. Mantendo versão antiga (se existir)." echo "[$country] Falha no download. Mantendo antigo (se existir)."
[ -f "$tmp_file" ] && rm "$tmp_file" [ -f "$tmp_file" ] && rm "$tmp_file"
continue continue
fi fi
if ipset list "$country" &>/dev/null; then if ipset list "$country" &>/dev/null; then
echo "[$(date '+%Y-%m-%d %H:%M:%S')] IpSet '$country' já existe. Limpando entradas antigas..." echo "[$country] Limpando ipset antigo..."
ipset flush "$country" ipset flush "$country"
else else
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Criando IpSet '$country'." echo "[$country] Criando novo ipset..."
ipset create "$country" hash:net ipset create "$country" hash:net
fi fi
while read -r subnet; do while read -r subnet; do
if ! ipset add "$country" "$subnet" -exist; then ipset add "$country" "$subnet" -exist || echo "Falha ao adicionar: $subnet"
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Aviso: falha ao adicionar $subnet ao ipset $country"
fi
done < "$target_file" done < "$target_file"
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Configuração do IpSet concluída para o país $country."
add_iptables_rule PREROUTING "$country" add_iptables_rule PREROUTING "$country"
add_iptables_rule OUTPUT "$country"
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Regras iptables configuradas para o país $country." echo "[$country] Proteção configurada."
done done
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Script concluído." echo "[$(date '+%Y-%m-%d %H:%M:%S')] ✅ Proteção geográfica concluída!"