diff --git a/ok-server.mjs b/ok-server.mjs index b86de00..cd8a22b 100755 --- a/ok-server.mjs +++ b/ok-server.mjs @@ -37,7 +37,7 @@ const HTML_BASIC = `

Hello,

This is a test HTTP server.

-

Your request came from {ip}.

+

Your request came from {ip_html}.

{proxy_headers_html}

Have a nice day!

@@ -62,7 +62,7 @@ const HTML_NICE = `

Hello,

This is a test HTTP server.

-

Your request came from {ip}.

+

Your request came from {ip_html}.

{proxy_headers_html}

Have a nice day!

@@ -92,7 +92,7 @@ const HTML_BOOTSTRAP = `

Hello,

This is a test HTTP server.

-

Your request came from {ip}.

+

Your request came from {ip_html}.

{proxy_headers_html}

Have a nice day!

@@ -127,7 +127,7 @@ const HTML_TAILWIND = `

Hello,

This is a test HTTP server.

-

Your request came from {ip}.

+

Your request came from {ip_html}.

{proxy_headers_html}

Have a nice day!

@@ -201,7 +201,7 @@ function proxyMarkup(xHeaders) { return { proxyHeadersBlock, proxyHeadersHtml }; } -function htmlForLook(look, ip, proxyHeadersHtml) { +function htmlForLook(look, ipHtml, proxyHeadersHtml) { const template = look === "basic" ? HTML_BASIC @@ -212,7 +212,7 @@ function htmlForLook(look, ip, proxyHeadersHtml) { : HTML_TAILWIND; return template - .replaceAll("{ip}", ip) + .replace("{ip_html}", ipHtml) .replace("{proxy_headers_html}", proxyHeadersHtml); } @@ -221,16 +221,26 @@ function makeBodyAndType(req, clientIp, defaultLook) { const xHeaders = collectXHeaders(req); const { proxyHeadersBlock, proxyHeadersHtml } = proxyMarkup(xHeaders); + const xff = (req.headers["x-forwarded-for"] || "").split(",")[0].trim(); + let ipText, ipHtml; + if (xff) { + ipText = `${xff} (forwarded by proxy ${clientIp})`; + ipHtml = `${escapeHtml(xff)} (forwarded by proxy ${escapeHtml(clientIp)})`; + } else { + ipText = clientIp; + ipHtml = `${escapeHtml(clientIp)}`; + } + if (isCliAgent(userAgent)) { const body = PLAIN_TEMPLATE - .replace("{ip}", clientIp) + .replace("{ip}", ipText) .replace("{proxy_headers_block}", proxyHeadersBlock); return { body: Buffer.from(body, "utf8"), contentType: "text/plain; charset=utf-8" }; } const requestLook = getRequestLook(req); const look = requestLook || defaultLook; - const html = htmlForLook(look, clientIp, proxyHeadersHtml); + const html = htmlForLook(look, ipHtml, proxyHeadersHtml); return { body: Buffer.from(html, "utf8"), contentType: "text/html; charset=utf-8" }; } diff --git a/ok-server.py b/ok-server.py index 6b70fb9..d62c32d 100755 --- a/ok-server.py +++ b/ok-server.py @@ -53,7 +53,7 @@ HTML_BASIC = """

Hello,

This is a test HTTP server.

-

Your request came from {ip}.

+

Your request came from {ip_html}.

{proxy_headers_html}

Have a nice day!

@@ -79,7 +79,7 @@ HTML_NICE = """

Hello,

This is a test HTTP server.

-

Your request came from {ip}.

+

Your request came from {ip_html}.

{proxy_headers_html}

Have a nice day!

@@ -110,7 +110,7 @@ HTML_BOOTSTRAP = """

Hello,

This is a test HTTP server.

-

Your request came from {ip}.

+

Your request came from {ip_html}.

{proxy_headers_html}

Have a nice day!

@@ -146,7 +146,7 @@ HTML_TAILWIND = """

Hello,

This is a test HTTP server.

-

Your request came from {ip}.

+

Your request came from {ip_html}.

{proxy_headers_html}

Have a nice day!

@@ -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"{escape(real_ip)}" + f" (forwarded by proxy {escape(client_ip)})" + ) + else: + ip_text = client_ip + ip_html = f"{escape(client_ip)}" + 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