Proxy-Option fuer Zonefile-Abruf (Host ohne direkte Internetverbindung)

This commit is contained in:
ARIA
2026-07-21 12:08:30 +00:00
parent 8d5908ac78
commit cd9addbec1
2 changed files with 64 additions and 5 deletions
+27 -5
View File
@@ -20,6 +20,8 @@ VORAUSSETZUNGEN (auf dem Ziel-Host, NICHT im Container):
- Pakete installiert: iptables, ipset
Debian/Ubuntu: apt install iptables ipset
- Internetzugang zum Laden der Zonefiles von ipdeny.com
(falls der Host selbst keine direkte Verbindung hat: 'proxy' in der
.ini setzen -- siehe README)
BENUTZUNG:
sudo python3 geoblock.py --config geoblock.ini --apply
@@ -167,6 +169,7 @@ def load_config(path):
"log_file": sec.get("log_file", "").strip(),
"ports": ports,
"protocols": protocols,
"proxy": sec.get("proxy", "").strip(),
}
@@ -213,12 +216,31 @@ def _variant_label(variant):
return f"{variant['proto']}/{variant['dports']}"
def fetch_country_cidrs(country_code):
"""Laedt die CIDR-Liste eines Landes von ipdeny.com. Gibt Liste von Strings zurueck."""
def _build_url_opener(proxy):
"""Baut einen urllib-Opener. Mit 'proxy' gesetzt (z.B. 'http://host:3128' oder
'http://user:pass@host:3128') wird dieser fuer http UND https genutzt -- fuer
Server, die selbst keine direkte Internetverbindung haben und nur ueber
einen Vorgelagerten Proxy raus koennen."""
if not proxy:
return urllib.request.build_opener()
handler = urllib.request.ProxyHandler({"http": proxy, "https": proxy})
return urllib.request.build_opener(handler)
def fetch_country_cidrs(country_code, proxy=""):
"""Laedt die CIDR-Liste eines Landes von ipdeny.com. Gibt Liste von Strings zurueck.
'proxy' (optional): HTTP(S)-Proxy-URL, falls der Server selbst keine direkte
Internetverbindung hat und ipdeny.com nur ueber einen Proxy erreichen kann.
"""
url = ZONE_URL_TMPL.format(cc=country_code)
log(f"Lade IP-Ranges fuer '{country_code}' von {url}")
if proxy:
log(f"Lade IP-Ranges fuer '{country_code}' von {url} (ueber Proxy {proxy})")
else:
log(f"Lade IP-Ranges fuer '{country_code}' von {url}")
opener = _build_url_opener(proxy)
try:
with urllib.request.urlopen(url, timeout=20) as resp:
with opener.open(url, timeout=20) as resp:
text = resp.read().decode("utf-8", errors="ignore")
except urllib.error.HTTPError as e:
raise RuntimeError(
@@ -276,7 +298,7 @@ def apply_geoblock(cfg, dry_run=False):
# 2) alle CIDRs aller konfigurierten Laender sammeln
all_cidrs = []
for cc in cfg["countries"]:
all_cidrs.extend(fetch_country_cidrs(cc))
all_cidrs.extend(fetch_country_cidrs(cc, proxy=cfg.get("proxy", "")))
if not all_cidrs and not dry_run:
raise RuntimeError("Keine IP-Bereiche geladen — Abbruch, um kein leeres Set zu aktivieren")