fix: strip port from X-Forwarded-For header to get real IP

This commit is contained in:
2026-05-09 20:38:50 +02:00
parent bbdf82441e
commit 543480b34f
2 changed files with 12 additions and 1 deletions
+5
View File
@@ -238,6 +238,11 @@ class OkHandler(BaseHTTPRequestHandler):
xff = self.headers.get("X-Forwarded-For", "")
if xff:
real_ip = xff.split(",")[0].strip()
# strip port: [::1]:port -> ::1, 1.2.3.4:port -> 1.2.3.4
if real_ip.startswith("["):
real_ip = real_ip[1:real_ip.index("]")] if "]" in real_ip else real_ip[1:]
elif real_ip.count(":") == 1:
real_ip = real_ip.split(":")[0]
ip_text = f"{real_ip} (forwarded by proxy {client_ip})"
ip_html = (
f"<strong>{escape(real_ip)}</strong>"