fix: update IP display to real IP in proxy condition.
This commit is contained in:
+18
-8
@@ -37,7 +37,7 @@ const 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>
|
||||
@@ -62,7 +62,7 @@ const 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>
|
||||
@@ -92,7 +92,7 @@ const 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>
|
||||
@@ -127,7 +127,7 @@ const 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>
|
||||
@@ -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 = `<strong>${escapeHtml(xff)}</strong> (forwarded by proxy <strong>${escapeHtml(clientIp)}</strong>)`;
|
||||
} else {
|
||||
ipText = clientIp;
|
||||
ipHtml = `<strong>${escapeHtml(clientIp)}</strong>`;
|
||||
}
|
||||
|
||||
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" };
|
||||
}
|
||||
|
||||
|
||||
+23
-11
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user