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" };
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user