fix: update IP display to real IP in proxy condition.

This commit is contained in:
2026-05-09 20:35:22 +02:00
parent a28e6d5f11
commit bbdf82441e
2 changed files with 41 additions and 19 deletions
+23 -11
View File
@@ -53,7 +53,7 @@ HTML_BASIC = """<!doctype html>
<body>
<h1>Hello,</h1>
<p>This is a test HTTP server.</p>
<p>Your request came from <strong>{ip}</strong>.</p>
<p>Your request came from {ip_html}.</p>
{proxy_headers_html}
<p>Have a nice day!</p>
</body>
@@ -79,7 +79,7 @@ HTML_NICE = """<!doctype html>
<div class="card">
<h1>Hello,</h1>
<p>This is a test HTTP server.</p>
<p>Your request came from <strong>{ip}</strong>.</p>
<p>Your request came from {ip_html}.</p>
{proxy_headers_html}
<p>Have a nice day!</p>
</div>
@@ -110,7 +110,7 @@ HTML_BOOTSTRAP = """<!doctype html>
<div class="card-body">
<h1 class="card-title">Hello,</h1>
<p class="card-text">This is a test HTTP server.</p>
<p class="card-text">Your request came from <strong>{ip}</strong>.</p>
<p class="card-text">Your request came from {ip_html}.</p>
{proxy_headers_html}
<hr>
<p class="mb-0">Have a nice day!</p>
@@ -146,7 +146,7 @@ HTML_TAILWIND = """<!doctype html>
<main class="space-y-4 px-6 py-6">
<p class="text-2xl font-bold">Hello,</p>
<p>This is a test HTTP server.</p>
<p>Your request came from <strong>{ip}</strong>.</p>
<p>Your request came from {ip_html}.</p>
{proxy_headers_html}
<p class="pt-2">Have a nice day!</p>
</main>
@@ -209,25 +209,25 @@ class OkHandler(BaseHTTPRequestHandler):
return look
return None
def _html_for_look(self, look: str, ip: str, proxy_headers_html: str) -> str:
def _html_for_look(self, look: str, ip_html: str, proxy_headers_html: str) -> str:
if look == "basic":
return HTML_BASIC.format(
ip=ip,
ip_html=ip_html,
proxy_headers_html=proxy_headers_html,
)
if look == "nice":
return HTML_NICE.format(
ip=ip,
ip_html=ip_html,
proxy_headers_html=proxy_headers_html,
)
if look == "bootstrap":
return HTML_BOOTSTRAP.format(
ip=ip,
ip_html=ip_html,
proxy_headers_html=proxy_headers_html,
)
# fallback to tailwind
return HTML_TAILWIND.format(
ip=ip,
ip_html=ip_html,
proxy_headers_html=proxy_headers_html,
)
@@ -235,9 +235,21 @@ class OkHandler(BaseHTTPRequestHandler):
x_headers = self._collect_x_headers()
proxy_headers_block, proxy_headers_html = self._proxy_markup(x_headers)
xff = self.headers.get("X-Forwarded-For", "")
if xff:
real_ip = xff.split(",")[0].strip()
ip_text = f"{real_ip} (forwarded by proxy {client_ip})"
ip_html = (
f"<strong>{escape(real_ip)}</strong>"
f" (forwarded by proxy <strong>{escape(client_ip)}</strong>)"
)
else:
ip_text = client_ip
ip_html = f"<strong>{escape(client_ip)}</strong>"
if self._is_cli_agent(user_agent):
body = PLAIN_TEMPLATE.format(
ip=client_ip,
ip=ip_text,
proxy_headers_block=proxy_headers_block,
).encode("utf-8")
ctype = "text/plain; charset=utf-8"
@@ -251,7 +263,7 @@ class OkHandler(BaseHTTPRequestHandler):
# fallback to server-level default (now: nice)
look = getattr(self.server, "look", "nice")
html = self._html_for_look(look, client_ip, proxy_headers_html)
html = self._html_for_look(look, ip_html, proxy_headers_html)
body = html.encode("utf-8")
ctype = "text/html; charset=utf-8"
return body, ctype