This commit is contained in:
Bxio 2025-06-23 15:54:15 +01:00
parent 7550db9f57
commit 8a6de01f48

View File

@ -1,4 +1,5 @@
#!/bin/bash #!/bin/bash
set -euo pipefail
echo -e " echo -e "
░█████╗░███╗░░██╗██████╗░██████╗░░█████╗░░██████╗ ░█████╗░███╗░░██╗██████╗░██████╗░░█████╗░░██████╗
@ -8,71 +9,66 @@ echo -e "
██║░░██║██║░╚███║██████╔╝██████╔╝╚█████╔╝██████╔╝ ██║░░██║██║░╚███║██████╔╝██████╔╝╚█████╔╝██████╔╝
╚═╝░░╚═╝╚═╝░░╚══╝╚═════╝░╚═════╝░░╚════╝░╚═════╝░" ╚═╝░░╚═╝╚═╝░░╚══╝╚═════╝░╚═════╝░░╚════╝░╚═════╝░"
COUNTRIES="af es" COUNTRIES="af es"
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 para ipset '$set_name' já existe na cadeia $chain."
else
echo "Inserindo regra iptables para ipset '$set_name' na cadeia $chain..."
iptables -t raw -I "$chain" -m set --match-set "$set_name" src -j DROP
fi
}
for country in $COUNTRIES; do for country in $COUNTRIES; do
if [ ! -d "zone" ]; then
mkdir -p zone
fi
url="https://www.ipdeny.com/ipblocks/data/countries/${country}.zone" url="https://www.ipdeny.com/ipblocks/data/countries/${country}.zone"
echo "Tentando baixar lista de IPs para o país: $country" echo "[$(date '+%Y-%m-%d %H:%M:%S')] Tentando baixar lista de IPs para o país: $country"
tmp_file="zone/${country}.zone.tmp" tmp_file="${ZONE_DIR}/${country}.zone.tmp"
target_file="zone/${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 "Download concluído para $country." echo "[$(date '+%Y-%m-%d %H:%M:%S')] Download concluído para $country."
if [ -f "$target_file" ]; then if [ -f "$target_file" ] && cmp -s "$tmp_file" "$target_file"; then
if 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 "Arquivo para $country não mudou. Mantendo versão atual."
rm "$tmp_file" rm "$tmp_file"
else else
echo "Arquivo para $country mudou. Atualizando..." echo "[$(date '+%Y-%m-%d %H:%M:%S')] Arquivo para $country mudou ou não existia. Atualizando..."
mv "$tmp_file" "$target_file" mv "$tmp_file" "$target_file"
fi fi
else else
echo "Arquivo para $country não existia. Salvando novo arquivo." echo "[$(date '+%Y-%m-%d %H:%M:%S')] Falha no download para $country. Mantendo versão antiga (se existir)."
mv "$tmp_file" "$target_file"
fi
else
echo "Falha no download do arquivo para $country. Mantendo versão antiga (se existir)."
[ -f "$tmp_file" ] && rm "$tmp_file" [ -f "$tmp_file" ] && rm "$tmp_file"
continue continue
fi fi
# Verifica se ipset existe if ipset list "$country" &>/dev/null; then
if ipset list "$country" > /dev/null 2>&1; then echo "[$(date '+%Y-%m-%d %H:%M:%S')] IpSet '$country' já existe. Limpando entradas antigas..."
echo "IpSet '$country' já existe. Limpando entradas antigas..."
ipset flush "$country" ipset flush "$country"
else else
echo "Criando IpSet '$country'." echo "[$(date '+%Y-%m-%d %H:%M:%S')] Criando IpSet '$country'."
ipset create "$country" hash:net ipset create "$country" hash:net
fi fi
# Adicionar cada rede ao ipset
while read -r subnet; do while read -r subnet; do
ipset add "$country" "$subnet" -exist if ! ipset add "$country" "$subnet" -exist; then
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Aviso: falha ao adicionar $subnet ao ipset $country"
fi
done < "$target_file" done < "$target_file"
echo "Concluído para o país $country." echo "[$(date '+%Y-%m-%d %H:%M:%S')] Configuração do IpSet concluída para o país $country."
# Verifica se a regra iptables já existe add_iptables_rule PREROUTING "$country"
if iptables -t raw -C PREROUTING -m set --match-set "$country" src -j DROP 2>/dev/null; then add_iptables_rule OUTPUT "$country"
echo "Regra iptables para ipset '$country' já existe."
else
echo "Inserindo regra iptables para bloquear IPs do ipset '$country'..."
iptables -t raw -I PREROUTING -m set --match-set "$country" src -j DROP
fi
if iptables -t raw -C OUTPUT -m set --match-set "$country" src -j DROP 2>/dev/null; then echo "[$(date '+%Y-%m-%d %H:%M:%S')] Regras iptables configuradas para o país $country."
echo "Regra iptables para ipset '$country' já existe."
else
echo "Inserindo regra iptables para bloquear IPs do ipset '$country'..."
iptables -t raw -I OUTPUT -m set --match-set "$country" src -j DROP
fi
echo "Regra iptables configurada para o país $country."
done done
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Script concluído."