mirror of
https://github.com/Myzel394/amiopen.now.git
synced 2025-06-18 07:25:27 +02:00
feat: Add json presentation support; Add specifying format support
This commit is contained in:
parent
0e1c34d34e
commit
021b941199
@ -1,6 +1,6 @@
|
|||||||
import { createMiddleware } from "hono/factory";
|
import { createMiddleware } from "hono/factory";
|
||||||
|
|
||||||
export type PresentationType = "terminal" | "browser" | "cli-browser";
|
export type PresentationType = "terminal" | "browser" | "cli-browser" | "json";
|
||||||
|
|
||||||
const TERMINAL_USER_AGENT =
|
const TERMINAL_USER_AGENT =
|
||||||
/^(curl|wget|python-urllib|pycurl|java|go-http-client|php)/i;
|
/^(curl|wget|python-urllib|pycurl|java|go-http-client|php)/i;
|
||||||
@ -12,16 +12,53 @@ const presentation = createMiddleware<{
|
|||||||
presentation: PresentationType;
|
presentation: PresentationType;
|
||||||
};
|
};
|
||||||
}>(async (context, next) => {
|
}>(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)) {
|
switch (suggestedFormat) {
|
||||||
context.set("presentation", "terminal");
|
case "terminal":
|
||||||
} else if (CLI_BROWSER_USER_AGENT.test(userAgent)) {
|
case "cli":
|
||||||
context.set("presentation", "cli-browser");
|
case "script":
|
||||||
} else {
|
case "shell":
|
||||||
context.set("presentation", "browser");
|
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();
|
await next();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -8,6 +8,7 @@ const realIP = createMiddleware<{
|
|||||||
}>(async (context, next) => {
|
}>(async (context, next) => {
|
||||||
const ip =
|
const ip =
|
||||||
context.req.header("x-forwarded-for") ||
|
context.req.header("x-forwarded-for") ||
|
||||||
|
context.req.header("x-real-ip") ||
|
||||||
getConnInfo(context).remote.address;
|
getConnInfo(context).remote.address;
|
||||||
|
|
||||||
if (!ip) {
|
if (!ip) {
|
||||||
|
@ -12,6 +12,7 @@ export default async function render(
|
|||||||
browser: ".html",
|
browser: ".html",
|
||||||
terminal: ".txt",
|
terminal: ".txt",
|
||||||
"cli-browser": ".cli.html",
|
"cli-browser": ".cli.html",
|
||||||
|
json: ".jsonn",
|
||||||
}[presentation];
|
}[presentation];
|
||||||
const key = templateName + ".njk" + extension;
|
const key = templateName + ".njk" + extension;
|
||||||
|
|
||||||
@ -24,5 +25,7 @@ export default async function render(
|
|||||||
return context.html(content);
|
return context.html(content);
|
||||||
case "terminal":
|
case "terminal":
|
||||||
return context.text(content.trimEnd());
|
return context.text(content.trimEnd());
|
||||||
|
case "json":
|
||||||
|
return context.json(JSON.parse(content));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
1
templates/index.njk.jsonn
Normal file
1
templates/index.njk.jsonn
Normal file
@ -0,0 +1 @@
|
|||||||
|
{ "ipv4": "{{ip}}" }
|
@ -1,6 +1,4 @@
|
|||||||
{% extends "base.njk.cli.html" %} {% block main %}
|
{% extends "base.njk.cli.html" %} {% block main %}
|
||||||
<h2>Am I Open</h2>
|
|
||||||
|
|
||||||
<i>{{ ip }}</i>
|
<i>{{ ip }}</i>
|
||||||
|
|
||||||
{% if isOpen %}
|
{% if isOpen %}
|
||||||
|
@ -72,8 +72,6 @@
|
|||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
{% endblock %} {% block main %}
|
{% endblock %} {% block main %}
|
||||||
<h2>Am I Open</h2>
|
|
||||||
|
|
||||||
<i>{{ ip }}</i>
|
<i>{{ ip }}</i>
|
||||||
|
|
||||||
{% if isOpen %}
|
{% if isOpen %}
|
||||||
|
1
templates/port.njk.jsonn
Normal file
1
templates/port.njk.jsonn
Normal file
@ -0,0 +1 @@
|
|||||||
|
{"isOpen":{% if isOpen %}true{% else %}false{% endif %}}
|
Loading…
x
Reference in New Issue
Block a user