Am I Open
+ {% endblock %} + ++
+ To root / +
+
-
+
+
diff --git a/src/middlewares/presentation.ts b/src/middlewares/presentation.ts index 49f132c..e09f58f 100644 --- a/src/middlewares/presentation.ts +++ b/src/middlewares/presentation.ts @@ -1,10 +1,12 @@ import { createMiddleware } from "hono/factory"; -export type PresentationType = "terminal" | "browser"; +export type PresentationType = "terminal" | "browser" | "cli-browser"; const TERMINAL_USER_AGENT = /^(curl|wget|python-urllib|pycurl|java|go-http-client|php)/i; +const CLI_BROWSER_USER_AGENT = /^(lynx|elinks|w3m)/i; + const presentation = createMiddleware<{ Variables: { presentation: PresentationType; @@ -14,6 +16,8 @@ const presentation = createMiddleware<{ 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"); } diff --git a/src/routes/root.ts b/src/routes/root.ts index dbb78bb..41b012e 100644 --- a/src/routes/root.ts +++ b/src/routes/root.ts @@ -6,6 +6,17 @@ import render from "../utils/renderer"; export const rootRoute = new Hono(); rootRoute.get("/", realIP, presentation, context => { + // Move request to correct route + const { port, ipAddress } = context.req.query(); + + if (port) { + if (ipAddress) { + return context.redirect(`/${ipAddress}/${port}`); + } else { + return context.redirect(`/${port}`); + } + } + return render(context, "index", { ip: context.get("ip"), }); diff --git a/src/utils/renderer.ts b/src/utils/renderer.ts index 7eb1bc4..32cf5ea 100644 --- a/src/utils/renderer.ts +++ b/src/utils/renderer.ts @@ -11,14 +11,17 @@ export default async function render( const extension = { browser: ".html", terminal: ".txt", + "cli-browser": ".cli.html", }[presentation]; - const key = templateName + extension + ".njk"; + const key = templateName + ".njk" + extension; const content = nunjucks.render(key, ctx); switch (presentation) { case "browser": return context.html(content); + case "cli-browser": + return context.html(content); case "terminal": return context.text(content.trimEnd()); } diff --git a/templates/base.njk.cli.html b/templates/base.njk.cli.html new file mode 100644 index 0000000..bc089f8 --- /dev/null +++ b/templates/base.njk.cli.html @@ -0,0 +1,40 @@ + + +
+ + + +- Your IP address: {{ip}} - -
- - Check if your port is reachable: -$ curl amiopen.now/<port>
-
-// Example
-$ curl amiopen.now/80
-> open
-
- Check if an IP address is reachable:
- $ curl amiopen.now/<ip address>/<port>
-
-// Example
-$ curl amiopen.now/1.1.1.1/53
-> open
-
- Check if your ISP is blocking a port:
- $ telnet amiopen.now <port>
-
-// Example
-$ telnet amiopen.now 80
-
-
- Hint: You can also check if you can access SSH by using:
- $ ssh hello@amiopen.now
-
-{% endblock %}
-
-{% block scripts %}
-
-{% endblock %}
diff --git a/templates/index.njk.cli.html b/templates/index.njk.cli.html
new file mode 100644
index 0000000..8f460f3
--- /dev/null
+++ b/templates/index.njk.cli.html
@@ -0,0 +1,58 @@
+{% extends "base.njk.cli.html" %} {% block main %}
+Your IP address: {{ip}}
+ +Check if your port is reachable: ++ Your IP address: {{ip}} + +
+ + Check if your port is reachable: +$ curl amiopen.now/<port>
+
+// Example
+$ curl amiopen.now/80
+> open
+
+ Check if an IP address is reachable:
+$ curl amiopen.now/<ip address>/<port>
+
+// Example
+$ curl amiopen.now/1.1.1.1/53
+> open
+
+ Check if your ISP is blocking a port:
+$ telnet amiopen.now <port>
+
+// Example
+$ telnet amiopen.now 80
+
+
+ Hint: You can also check if you can access SSH by using:
+ $ ssh hello@amiopen.now
+
+{% endblock %} {% block scripts %}
+
+{% endblock %}
diff --git a/templates/index.txt.njk b/templates/index.njk.txt
similarity index 100%
rename from templates/index.txt.njk
rename to templates/index.njk.txt
diff --git a/templates/port.html.njk b/templates/port.html.njk
deleted file mode 100644
index 81e51ea..0000000
--- a/templates/port.html.njk
+++ /dev/null
@@ -1,96 +0,0 @@
-{% extends "base.html.njk" %}
-
-{% block header %}
-
-{% endblock %}
-
-{% block main %}
- Port {{port}} is open
-Port {{port}} is closed
-Port {{port}} is open
+{% else %} +Port {{port}} is closed
+{% endif %} {% endblock %} diff --git a/templates/port.njk.html b/templates/port.njk.html new file mode 100644 index 0000000..4592b02 --- /dev/null +++ b/templates/port.njk.html @@ -0,0 +1,91 @@ +{% extends "base.njk.html" %} {% block header %} + +{% endblock %} {% block main %} +Port {{port}} is open
+Port {{port}} is closed
+