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>
|
<body>
|
||||||
<h1>Hello,</h1>
|
<h1>Hello,</h1>
|
||||||
<p>This is a test HTTP server.</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}
|
{proxy_headers_html}
|
||||||
<p>Have a nice day!</p>
|
<p>Have a nice day!</p>
|
||||||
</body>
|
</body>
|
||||||
@@ -62,7 +62,7 @@ const HTML_NICE = `<!doctype html>
|
|||||||
<div class="card">
|
<div class="card">
|
||||||
<h1>Hello,</h1>
|
<h1>Hello,</h1>
|
||||||
<p>This is a test HTTP server.</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}
|
{proxy_headers_html}
|
||||||
<p>Have a nice day!</p>
|
<p>Have a nice day!</p>
|
||||||
</div>
|
</div>
|
||||||
@@ -92,7 +92,7 @@ const HTML_BOOTSTRAP = `<!doctype html>
|
|||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
<h1 class="card-title">Hello,</h1>
|
<h1 class="card-title">Hello,</h1>
|
||||||
<p class="card-text">This is a test HTTP server.</p>
|
<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}
|
{proxy_headers_html}
|
||||||
<hr>
|
<hr>
|
||||||
<p class="mb-0">Have a nice day!</p>
|
<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">
|
<main class="space-y-4 px-6 py-6">
|
||||||
<p class="text-2xl font-bold">Hello,</p>
|
<p class="text-2xl font-bold">Hello,</p>
|
||||||
<p>This is a test HTTP server.</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}
|
{proxy_headers_html}
|
||||||
<p class="pt-2">Have a nice day!</p>
|
<p class="pt-2">Have a nice day!</p>
|
||||||
</main>
|
</main>
|
||||||
@@ -201,7 +201,7 @@ function proxyMarkup(xHeaders) {
|
|||||||
return { proxyHeadersBlock, proxyHeadersHtml };
|
return { proxyHeadersBlock, proxyHeadersHtml };
|
||||||
}
|
}
|
||||||
|
|
||||||
function htmlForLook(look, ip, proxyHeadersHtml) {
|
function htmlForLook(look, ipHtml, proxyHeadersHtml) {
|
||||||
const template =
|
const template =
|
||||||
look === "basic"
|
look === "basic"
|
||||||
? HTML_BASIC
|
? HTML_BASIC
|
||||||
@@ -212,7 +212,7 @@ function htmlForLook(look, ip, proxyHeadersHtml) {
|
|||||||
: HTML_TAILWIND;
|
: HTML_TAILWIND;
|
||||||
|
|
||||||
return template
|
return template
|
||||||
.replaceAll("{ip}", ip)
|
.replace("{ip_html}", ipHtml)
|
||||||
.replace("{proxy_headers_html}", proxyHeadersHtml);
|
.replace("{proxy_headers_html}", proxyHeadersHtml);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -221,16 +221,26 @@ function makeBodyAndType(req, clientIp, defaultLook) {
|
|||||||
const xHeaders = collectXHeaders(req);
|
const xHeaders = collectXHeaders(req);
|
||||||
const { proxyHeadersBlock, proxyHeadersHtml } = proxyMarkup(xHeaders);
|
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)) {
|
if (isCliAgent(userAgent)) {
|
||||||
const body = PLAIN_TEMPLATE
|
const body = PLAIN_TEMPLATE
|
||||||
.replace("{ip}", clientIp)
|
.replace("{ip}", ipText)
|
||||||
.replace("{proxy_headers_block}", proxyHeadersBlock);
|
.replace("{proxy_headers_block}", proxyHeadersBlock);
|
||||||
return { body: Buffer.from(body, "utf8"), contentType: "text/plain; charset=utf-8" };
|
return { body: Buffer.from(body, "utf8"), contentType: "text/plain; charset=utf-8" };
|
||||||
}
|
}
|
||||||
|
|
||||||
const requestLook = getRequestLook(req);
|
const requestLook = getRequestLook(req);
|
||||||
const look = requestLook || defaultLook;
|
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" };
|
return { body: Buffer.from(html, "utf8"), contentType: "text/html; charset=utf-8" };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+23
-11
@@ -53,7 +53,7 @@ HTML_BASIC = """<!doctype html>
|
|||||||
<body>
|
<body>
|
||||||
<h1>Hello,</h1>
|
<h1>Hello,</h1>
|
||||||
<p>This is a test HTTP server.</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}
|
{proxy_headers_html}
|
||||||
<p>Have a nice day!</p>
|
<p>Have a nice day!</p>
|
||||||
</body>
|
</body>
|
||||||
@@ -79,7 +79,7 @@ HTML_NICE = """<!doctype html>
|
|||||||
<div class="card">
|
<div class="card">
|
||||||
<h1>Hello,</h1>
|
<h1>Hello,</h1>
|
||||||
<p>This is a test HTTP server.</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}
|
{proxy_headers_html}
|
||||||
<p>Have a nice day!</p>
|
<p>Have a nice day!</p>
|
||||||
</div>
|
</div>
|
||||||
@@ -110,7 +110,7 @@ HTML_BOOTSTRAP = """<!doctype html>
|
|||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
<h1 class="card-title">Hello,</h1>
|
<h1 class="card-title">Hello,</h1>
|
||||||
<p class="card-text">This is a test HTTP server.</p>
|
<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}
|
{proxy_headers_html}
|
||||||
<hr>
|
<hr>
|
||||||
<p class="mb-0">Have a nice day!</p>
|
<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">
|
<main class="space-y-4 px-6 py-6">
|
||||||
<p class="text-2xl font-bold">Hello,</p>
|
<p class="text-2xl font-bold">Hello,</p>
|
||||||
<p>This is a test HTTP server.</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}
|
{proxy_headers_html}
|
||||||
<p class="pt-2">Have a nice day!</p>
|
<p class="pt-2">Have a nice day!</p>
|
||||||
</main>
|
</main>
|
||||||
@@ -209,25 +209,25 @@ class OkHandler(BaseHTTPRequestHandler):
|
|||||||
return look
|
return look
|
||||||
return None
|
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":
|
if look == "basic":
|
||||||
return HTML_BASIC.format(
|
return HTML_BASIC.format(
|
||||||
ip=ip,
|
ip_html=ip_html,
|
||||||
proxy_headers_html=proxy_headers_html,
|
proxy_headers_html=proxy_headers_html,
|
||||||
)
|
)
|
||||||
if look == "nice":
|
if look == "nice":
|
||||||
return HTML_NICE.format(
|
return HTML_NICE.format(
|
||||||
ip=ip,
|
ip_html=ip_html,
|
||||||
proxy_headers_html=proxy_headers_html,
|
proxy_headers_html=proxy_headers_html,
|
||||||
)
|
)
|
||||||
if look == "bootstrap":
|
if look == "bootstrap":
|
||||||
return HTML_BOOTSTRAP.format(
|
return HTML_BOOTSTRAP.format(
|
||||||
ip=ip,
|
ip_html=ip_html,
|
||||||
proxy_headers_html=proxy_headers_html,
|
proxy_headers_html=proxy_headers_html,
|
||||||
)
|
)
|
||||||
# fallback to tailwind
|
# fallback to tailwind
|
||||||
return HTML_TAILWIND.format(
|
return HTML_TAILWIND.format(
|
||||||
ip=ip,
|
ip_html=ip_html,
|
||||||
proxy_headers_html=proxy_headers_html,
|
proxy_headers_html=proxy_headers_html,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -235,9 +235,21 @@ class OkHandler(BaseHTTPRequestHandler):
|
|||||||
x_headers = self._collect_x_headers()
|
x_headers = self._collect_x_headers()
|
||||||
proxy_headers_block, proxy_headers_html = self._proxy_markup(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):
|
if self._is_cli_agent(user_agent):
|
||||||
body = PLAIN_TEMPLATE.format(
|
body = PLAIN_TEMPLATE.format(
|
||||||
ip=client_ip,
|
ip=ip_text,
|
||||||
proxy_headers_block=proxy_headers_block,
|
proxy_headers_block=proxy_headers_block,
|
||||||
).encode("utf-8")
|
).encode("utf-8")
|
||||||
ctype = "text/plain; charset=utf-8"
|
ctype = "text/plain; charset=utf-8"
|
||||||
@@ -251,7 +263,7 @@ class OkHandler(BaseHTTPRequestHandler):
|
|||||||
# fallback to server-level default (now: nice)
|
# fallback to server-level default (now: nice)
|
||||||
look = getattr(self.server, "look", "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")
|
body = html.encode("utf-8")
|
||||||
ctype = "text/html; charset=utf-8"
|
ctype = "text/html; charset=utf-8"
|
||||||
return body, ctype
|
return body, ctype
|
||||||
|
|||||||
Reference in New Issue
Block a user