Merge pull request 'Whitelist-Einträge "name/NN" für dynamische IPv6-Präfixe' (#1) from ipv6-prefix into main
This commit was merged in pull request #1.
This commit is contained in:
commit
6c7bef7bdd
3 files changed
+65
-5
No files matched your search
@@ -52,10 +52,13 @@ vpn.example.com
|
||||
203.0.113.42
|
||||
198.51.100.0/24
|
||||
2001:db8:1234::/48
|
||||
home.example.com/56
|
||||
```
|
||||
|
||||
DNS-Einträge werden in A- und AAAA-Records aufgelöst. Einzelne IPv4-Adressen werden zu `/32`, einzelne IPv6-Adressen zu `/128`.
|
||||
|
||||
**DNS-Name mit IPv6-Präfix (`name/NN`):** Die AAAA-Records werden auf das Netz `/NN` gekürzt, A-Records bleiben `/32`. Das ist für Anschlüsse mit wechselndem IPv6-Präfix (DHCPv6-PD) gedacht, deren Clients Privacy-Adressen in mehreren /64-Netzen nutzen. Der Name muss per DynDNS auf eine Adresse innerhalb des Netzes zeigen. Beispiel: `home.example.com/56` mit AAAA `2001:db8:abcd:1234::1` ergibt `2001:db8:abcd:1200::/56`. Präfixe kürzer als `IPV6_PREFIX_MIN` (Standard `/48`) werden mit Warnung ignoriert.
|
||||
|
||||
## Einrichtung
|
||||
|
||||
1. Kopiere diesen Ordner in dein Traefik-Projekt:
|
||||
@@ -215,6 +218,7 @@ curl -i -u "team:zweites-langes-zufaelliges-passwort" -H "X-Forwarded-For: 203.0
|
||||
- `BASIC_AUTH_PASSWORD_3`: optionales Passwort des dritten BasicAuth-Benutzers.
|
||||
- `BASIC_AUTH_PASSWORD_SHA256_3`: optionale Alternative zu `BASIC_AUTH_PASSWORD_3`; Hex-SHA-256-Digest des dritten Passworts.
|
||||
- `WHITELIST_EXTRA_CIDRS`: optionale, kommagetrennte CIDR/IP-Liste, die zur externen Whitelist hinzugefügt wird.
|
||||
- `IPV6_PREFIX_MIN`: kleinste erlaubte Präfixlänge für Einträge `name/NN`, Standardwert `48`.
|
||||
- `CLIENT_IP_STRATEGY`: standardmäßig `rightmost`, alternativ `leftmost`.
|
||||
- `CLIENT_IP_HEADER`: optionaler benutzerdefinierter Client-IP-Header; standardmäßig `X-Forwarded-For`.
|
||||
|
||||
|
||||
@@ -30,6 +30,9 @@ LISTEN_PORT = int(os.getenv("LISTEN_PORT", "8080"))
|
||||
BASIC_AUTH_REALM = os.getenv("BASIC_AUTH_REALM", "Protected Area")
|
||||
CLIENT_IP_STRATEGY = os.getenv("CLIENT_IP_STRATEGY", "rightmost").lower()
|
||||
EXTRA_CIDRS = os.getenv("WHITELIST_EXTRA_CIDRS", "")
|
||||
# Kleinste erlaubte IPv6-Präfixlänge für Einträge der Form "domain/NN"
|
||||
# (Schutz vor zu großen Freigaben wie /32 = ganzer Provider)
|
||||
IPV6_PREFIX_MIN = int(os.getenv("IPV6_PREFIX_MIN", "48"))
|
||||
MAX_BASIC_AUTH_USERS = 3
|
||||
|
||||
|
||||
@@ -144,26 +147,51 @@ def parse_extra_cidrs(value):
|
||||
return networks
|
||||
|
||||
|
||||
def query_networks(query):
|
||||
networks = []
|
||||
for raw_value in query.get("allowlist", []):
|
||||
networks.extend(parse_extra_cidrs(raw_value))
|
||||
return networks
|
||||
|
||||
|
||||
def resolve_entry(entry):
|
||||
try:
|
||||
return [ipaddress.ip_network(entry, strict=False)]
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
# "domain/NN": AAAA-Records auf das Netz /NN kürzen (z. B. DHCPv6-PD-Anschluss),
|
||||
# A-Records bleiben Einzeladressen.
|
||||
host = entry
|
||||
v6_prefix = 128
|
||||
if "/" in entry:
|
||||
host, _, prefix_text = entry.rpartition("/")
|
||||
if not prefix_text.isdigit():
|
||||
logging.warning("invalid entry %s: prefix length is not a number", entry)
|
||||
return []
|
||||
v6_prefix = int(prefix_text)
|
||||
if not IPV6_PREFIX_MIN <= v6_prefix <= 128:
|
||||
logging.warning(
|
||||
"invalid entry %s: IPv6 prefix length must be between /%s and /128",
|
||||
entry,
|
||||
IPV6_PREFIX_MIN,
|
||||
)
|
||||
return []
|
||||
|
||||
networks = []
|
||||
try:
|
||||
answers = socket.getaddrinfo(entry, None, type=socket.SOCK_STREAM)
|
||||
answers = socket.getaddrinfo(host, None, type=socket.SOCK_STREAM)
|
||||
except socket.gaierror as exc:
|
||||
logging.warning("DNS lookup failed for %s: %s", entry, exc)
|
||||
logging.warning("DNS lookup failed for %s: %s", host, exc)
|
||||
return networks
|
||||
|
||||
for family, _, _, _, sockaddr in answers:
|
||||
ip_text = sockaddr[0]
|
||||
ip_text = sockaddr[0].split("%", 1)[0]
|
||||
ip = ipaddress.ip_address(ip_text)
|
||||
if family == socket.AF_INET:
|
||||
networks.append(ipaddress.ip_network(f"{ip}/32", strict=False))
|
||||
elif family == socket.AF_INET6:
|
||||
networks.append(ipaddress.ip_network(f"{ip}/128", strict=False))
|
||||
networks.append(ipaddress.ip_network(f"{ip}/{v6_prefix}", strict=False))
|
||||
|
||||
return list(set(networks))
|
||||
|
||||
@@ -330,9 +358,16 @@ class AccessGateHandler(BaseHTTPRequestHandler):
|
||||
self.respond(404, {"error": "not found"}, include_body)
|
||||
return
|
||||
|
||||
mode = query.get("mode", [""])[0].strip().lower()
|
||||
query_allowlist = query_networks(query)
|
||||
client_ip = request_client_ip(self)
|
||||
log_context = request_log_context(self)
|
||||
if STORE.contains(client_ip):
|
||||
if mode == "replace":
|
||||
allowlisted = any(client_ip in network for network in query_allowlist)
|
||||
else:
|
||||
allowlisted = STORE.contains(client_ip) or any(client_ip in network for network in query_allowlist)
|
||||
|
||||
if allowlisted:
|
||||
logging.info(
|
||||
"allowlisted ip=%s host=%s uri=%s xff=%s xreal=%s remote=%s auth_scheme=%s",
|
||||
client_ip,
|
||||
@@ -348,6 +383,25 @@ class AccessGateHandler(BaseHTTPRequestHandler):
|
||||
self.end_headers()
|
||||
return
|
||||
|
||||
if mode in ("deny", "forbid", "block"):
|
||||
logging.info(
|
||||
"access denied ip=%s host=%s uri=%s xff=%s xreal=%s remote=%s auth_scheme=%s",
|
||||
client_ip,
|
||||
log_context["host"],
|
||||
log_context["uri"],
|
||||
log_context["xff"],
|
||||
log_context["xreal"],
|
||||
log_context["remote"],
|
||||
log_context["auth_scheme"],
|
||||
)
|
||||
self.send_response(403)
|
||||
self.send_header("Cache-Control", "no-store")
|
||||
self.send_header("Content-Type", "text/plain; charset=utf-8")
|
||||
self.end_headers()
|
||||
if include_body:
|
||||
self.wfile.write(b"Forbidden\n")
|
||||
return
|
||||
|
||||
authenticated_user = basic_auth_valid(self.headers.get("Authorization", ""))
|
||||
if authenticated_user:
|
||||
logging.info(
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
# One entry per line.
|
||||
# Entries can be DNS names, single IP addresses, or CIDR ranges.
|
||||
# DNS name with IPv6 prefix: name/NN -> AAAA records are widened to /NN.
|
||||
|
||||
office.example.com
|
||||
vpn.example.com
|
||||
203.0.113.42
|
||||
198.51.100.0/24
|
||||
2001:db8:1234::/48
|
||||
home.example.com/56
|
||||
Reference in new issue
Block a user