diff --git a/bun.lockb b/bun.lockb index 75248b3..1bdb2af 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/src/middlewares/presentation.ts b/src/middlewares/presentation.ts index e09f58f..7cf5ec7 100644 --- a/src/middlewares/presentation.ts +++ b/src/middlewares/presentation.ts @@ -1,6 +1,6 @@ import { createMiddleware } from "hono/factory"; -export type PresentationType = "terminal" | "browser" | "cli-browser"; +export type PresentationType = "terminal" | "browser" | "cli-browser" | "json"; const TERMINAL_USER_AGENT = /^(curl|wget|python-urllib|pycurl|java|go-http-client|php)/i; @@ -12,16 +12,53 @@ const presentation = createMiddleware<{ presentation: PresentationType; }; }>(async (context, next) => { - const userAgent = context.req.header("User-Agent") || ""; + let presentation: PresentationType = "browser"; + const suggestedFormat = + context.req.query("format") || context.req.query("f"); - if (TERMINAL_USER_AGENT.test(userAgent)) { - context.set("presentation", "terminal"); - } else if (CLI_BROWSER_USER_AGENT.test(userAgent)) { - context.set("presentation", "cli-browser"); - } else { - context.set("presentation", "browser"); + switch (suggestedFormat) { + case "terminal": + case "cli": + case "script": + case "shell": + case "sh": + case "bash": + presentation = "terminal"; + break; + + case "browser": + case "default": + case "html": + case "web": + presentation = "browser"; + break; + + case "cli-browser": + case "lynx": + case "elinks": + case "w3m": + presentation = "cli-browser"; + break; + + case "json": + presentation = "json"; + break; + + default: + // Detect using User-Agent + const userAgent = context.req.header("User-Agent") || ""; + + if (TERMINAL_USER_AGENT.test(userAgent)) { + presentation = "terminal"; + } else if (CLI_BROWSER_USER_AGENT.test(userAgent)) { + presentation = "cli-browser"; + } else { + presentation = "browser"; + } } + context.set("presentation", presentation); + await next(); }); diff --git a/src/middlewares/real-ip.ts b/src/middlewares/real-ip.ts index a0e7bf9..9255a22 100644 --- a/src/middlewares/real-ip.ts +++ b/src/middlewares/real-ip.ts @@ -8,6 +8,7 @@ const realIP = createMiddleware<{ }>(async (context, next) => { const ip = context.req.header("x-forwarded-for") || + context.req.header("x-real-ip") || getConnInfo(context).remote.address; if (!ip) { diff --git a/src/utils/renderer.ts b/src/utils/renderer.ts index 32cf5ea..afabd07 100644 --- a/src/utils/renderer.ts +++ b/src/utils/renderer.ts @@ -12,6 +12,7 @@ export default async function render( browser: ".html", terminal: ".txt", "cli-browser": ".cli.html", + json: ".jsonn", }[presentation]; const key = templateName + ".njk" + extension; @@ -24,5 +25,7 @@ export default async function render( return context.html(content); case "terminal": return context.text(content.trimEnd()); + case "json": + return context.json(JSON.parse(content)); } } diff --git a/templates/index.njk.jsonn b/templates/index.njk.jsonn new file mode 100644 index 0000000..60252d4 --- /dev/null +++ b/templates/index.njk.jsonn @@ -0,0 +1 @@ +{ "ipv4": "{{ip}}" } diff --git a/templates/port.njk.cli.html b/templates/port.njk.cli.html index 7c27e84..1a2a4c1 100644 --- a/templates/port.njk.cli.html +++ b/templates/port.njk.cli.html @@ -1,6 +1,4 @@ {% extends "base.njk.cli.html" %} {% block main %} -

Am I Open

- {{ ip }} {% if isOpen %} diff --git a/templates/port.njk.html b/templates/port.njk.html index 4592b02..65bf215 100644 --- a/templates/port.njk.html +++ b/templates/port.njk.html @@ -72,8 +72,6 @@ } {% endblock %} {% block main %} -

Am I Open

- {{ ip }} {% if isOpen %} diff --git a/templates/port.njk.jsonn b/templates/port.njk.jsonn new file mode 100644 index 0000000..b682a15 --- /dev/null +++ b/templates/port.njk.jsonn @@ -0,0 +1 @@ +{"isOpen":{% if isOpen %}true{% else %}false{% endif %}}