From ce52341ad8aa984acb20a985f7bbb0dd5a64305e Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Mon, 11 Nov 2024 22:33:46 +0100 Subject: [PATCH 1/6] refactor: Move server into its own folder --- Dockerfile => server/Dockerfile | 0 bun.lockb => server/bun.lockb | Bin package.json => server/package.json | 0 {src => server/src}/index.ts | 0 {src => server/src}/middlewares/real-ip.ts | 0 {src => server/src}/routes/port.ts | 0 {src => server/src}/utils/connect-to-address.ts | 0 {src => server/src}/utils/timeout.ts | 0 tsconfig.json => server/tsconfig.json | 0 upload_to_server.sh | 10 +++++----- 10 files changed, 5 insertions(+), 5 deletions(-) rename Dockerfile => server/Dockerfile (100%) rename bun.lockb => server/bun.lockb (100%) rename package.json => server/package.json (100%) rename {src => server/src}/index.ts (100%) rename {src => server/src}/middlewares/real-ip.ts (100%) rename {src => server/src}/routes/port.ts (100%) rename {src => server/src}/utils/connect-to-address.ts (100%) rename {src => server/src}/utils/timeout.ts (100%) rename tsconfig.json => server/tsconfig.json (100%) diff --git a/Dockerfile b/server/Dockerfile similarity index 100% rename from Dockerfile rename to server/Dockerfile diff --git a/bun.lockb b/server/bun.lockb similarity index 100% rename from bun.lockb rename to server/bun.lockb diff --git a/package.json b/server/package.json similarity index 100% rename from package.json rename to server/package.json diff --git a/src/index.ts b/server/src/index.ts similarity index 100% rename from src/index.ts rename to server/src/index.ts diff --git a/src/middlewares/real-ip.ts b/server/src/middlewares/real-ip.ts similarity index 100% rename from src/middlewares/real-ip.ts rename to server/src/middlewares/real-ip.ts diff --git a/src/routes/port.ts b/server/src/routes/port.ts similarity index 100% rename from src/routes/port.ts rename to server/src/routes/port.ts diff --git a/src/utils/connect-to-address.ts b/server/src/utils/connect-to-address.ts similarity index 100% rename from src/utils/connect-to-address.ts rename to server/src/utils/connect-to-address.ts diff --git a/src/utils/timeout.ts b/server/src/utils/timeout.ts similarity index 100% rename from src/utils/timeout.ts rename to server/src/utils/timeout.ts diff --git a/tsconfig.json b/server/tsconfig.json similarity index 100% rename from tsconfig.json rename to server/tsconfig.json diff --git a/upload_to_server.sh b/upload_to_server.sh index 1662c1a..2537274 100755 --- a/upload_to_server.sh +++ b/upload_to_server.sh @@ -1,9 +1,9 @@ ip=$1 user=$2 -docker buildx build --platform linux/amd64 . --tag amiopen:latest -docker save -o amiopen.tar amiopen:latest - -scp amiopen.tar $user@$ip:~/amiopen/amiopen.tar -ssh $user@$ip "cd ~/amiopen && docker container stop amiopen && docker container rm amiopen && docker load -i amiopen.tar && docker compose up -d" +cd server && + docker buildx build --platform linux/amd64 . --tag amiopen:latest && + docker save -o amiopen.tar amiopen:latest && + scp amiopen.tar $user@$ip:~/amiopen/amiopen.tar && + ssh $user@$ip "cd ~/amiopen && docker container stop amiopen && docker container rm amiopen && docker load -i amiopen.tar && docker compose up -d" From 82774d656d516e5b2f38298347ae79e52560b3a9 Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Wed, 20 Nov 2024 16:21:41 +0100 Subject: [PATCH 2/6] fix: Improvements --- .github/workflows/release.yaml | 29 ++-- Dockerfile | 15 ++ server/bun.lockb => bun.lockb | Bin 4426 -> 4426 bytes flake.lock | 61 ++++++++ flake.nix | 23 +++ server/package.json => package.json | 0 server/Dockerfile | 14 -- server/src/index.ts | 12 -- src/index.ts | 14 ++ src/middlewares/presentation.ts | 23 +++ {server/src => src}/middlewares/real-ip.ts | 0 {server/src => src}/routes/port.ts | 4 +- src/routes/root.ts | 12 ++ .../src => src}/utils/connect-to-address.ts | 0 src/utils/renderer.ts | 46 ++++++ {server/src => src}/utils/timeout.ts | 0 static/copy.svg | 1 + templates/index.html | 140 ++++++++++++++++++ templates/index.txt | 1 + server/tsconfig.json => tsconfig.json | 0 20 files changed, 352 insertions(+), 43 deletions(-) create mode 100644 Dockerfile rename server/bun.lockb => bun.lockb (98%) create mode 100644 flake.lock create mode 100644 flake.nix rename server/package.json => package.json (100%) delete mode 100644 server/Dockerfile delete mode 100644 server/src/index.ts create mode 100644 src/index.ts create mode 100644 src/middlewares/presentation.ts rename {server/src => src}/middlewares/real-ip.ts (100%) rename {server/src => src}/routes/port.ts (93%) create mode 100644 src/routes/root.ts rename {server/src => src}/utils/connect-to-address.ts (100%) create mode 100644 src/utils/renderer.ts rename {server/src => src}/utils/timeout.ts (100%) create mode 100644 static/copy.svg create mode 100644 templates/index.html create mode 100644 templates/index.txt rename server/tsconfig.json => tsconfig.json (100%) diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 26bdbaa..221efb6 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -1,22 +1,21 @@ name: Build and release on: - release: - types: [published] + release: + types: [published] jobs: - build: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 - - name: Add SSH identity key - run: | - mkdir -p ~/.ssh - echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa - chmod 600 ~/.ssh/id_rsa - ssh-keyscan -H ${{ secrets.SERVER_IP }} >> ~/.ssh/known_hosts - - - name: Build & upload image - run: ./upload_to_server.sh ${{ secrets.SERVER_IP }} ${{ secrets.SSH_USER }} + - name: Add SSH identity key + run: | + mkdir -p ~/.ssh + echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa + chmod 600 ~/.ssh/id_rsa + ssh-keyscan -H ${{ secrets.SERVER_IP }} >> ~/.ssh/known_hosts + - name: Build & upload image + run: ./upload_to_server.sh ${{ secrets.SERVER_IP }} ${{ secrets.SSH_USER }} diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..d0a9adb --- /dev/null +++ b/Dockerfile @@ -0,0 +1,15 @@ +FROM oven/bun + +WORKDIR /app + +COPY . /app + +# Absolutely no idea why, but piping the output directly to the file makes it empty. +RUN mkdir -p /tmp/templates && mv /app/templates/*.html /tmp/templates/ +RUN bunx html-minifier --collapse-whitespace --remove-comments --remove-optional-tags --remove-redundant-attributes --remove-script-type-attributes --remove-tag-whitespace --use-short-doctype --minify-css true --minify-js true /tmp/templates/index.html > /app/templates/index.html +RUN bun install --production + +EXPOSE 3000 + +ENTRYPOINT ["bun", "run", "start"] + diff --git a/server/bun.lockb b/bun.lockb similarity index 98% rename from server/bun.lockb rename to bun.lockb index 79d48b7900ce89323e3cd526fade89178ce769e3..5b268d4cfdeeb9165ef7f0bf1b9ffb610d23c95e 100755 GIT binary patch delta 20 acmX@5bV_N%QyzB4IAc8^-28?ojs*Zv7Y5=0 delta 20 YcmX@5bV_N%Qyz9E1~AzCh9`~%07P*GkN^Mx diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..73fe5a1 --- /dev/null +++ b/flake.lock @@ -0,0 +1,61 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1731676054, + "narHash": "sha256-OZiZ3m8SCMfh3B6bfGC/Bm4x3qc1m2SVEAlkV6iY7Yg=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "5e4fbfb6b3de1aa2872b76d49fafc942626e2add", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs", + "utils": "utils" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + }, + "utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1731533236, + "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..08ade3a --- /dev/null +++ b/flake.nix @@ -0,0 +1,23 @@ +{ + description = "A very basic flake"; + + inputs = { + nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable"; + utils.url = "github:numtide/flake-utils"; + }; + + outputs = { self, nixpkgs, utils }: utils.lib.eachDefaultSystem (system: + let + pkgs = nixpkgs.legacyPackages.${system}; + in + { + devShells.default = pkgs.mkShell { + buildInputs = with pkgs; [ + html-minifier + bun + vscode-langservers-extracted + ]; + }; + } + ); +} diff --git a/server/package.json b/package.json similarity index 100% rename from server/package.json rename to package.json diff --git a/server/Dockerfile b/server/Dockerfile deleted file mode 100644 index 012f915..0000000 --- a/server/Dockerfile +++ /dev/null @@ -1,14 +0,0 @@ -FROM oven/bun:alpine - -WORKDIR /app - -COPY package.json ./ -COPY bun.lockb ./ -COPY src ./src - -RUN bun install - -EXPOSE 3000 - -ENTRYPOINT ["bun", "run", "start"] - diff --git a/server/src/index.ts b/server/src/index.ts deleted file mode 100644 index fd389f5..0000000 --- a/server/src/index.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { Hono } from "hono"; -import { portRoute } from "./routes/port"; -import realIP from "./middlewares/real-ip"; - -const app = new Hono(); - -app.route("/", portRoute); - -Bun.serve({ - ...app, - idleTimeout: 90, -}); diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..1556924 --- /dev/null +++ b/src/index.ts @@ -0,0 +1,14 @@ +import { Hono } from "hono"; +import { portRoute } from "./routes/port"; +import { rootRoute } from "./routes/root"; +import { serveStatic } from "hono/bun"; + +const app = new Hono() + .use("/static/*", serveStatic({ root: "./" })) + .route("/", rootRoute) + .route("/", portRoute); + +Bun.serve({ + ...app, + idleTimeout: 90, +}); diff --git a/src/middlewares/presentation.ts b/src/middlewares/presentation.ts new file mode 100644 index 0000000..3792a6f --- /dev/null +++ b/src/middlewares/presentation.ts @@ -0,0 +1,23 @@ +import { createMiddleware } from "hono/factory"; + +export type PresentationType = "terminal" | "browser"; + +const TERMINAL_USER_AGENT = /^(curl|wget|python-urllib|pycurl|java)/i; + +const presentation = createMiddleware<{ + Variables: { + presentation: PresentationType; + }; +}>(async (context, next) => { + const userAgent = context.req.header("User-Agent") || ""; + + if (TERMINAL_USER_AGENT.test(userAgent)) { + context.set("presentation", "terminal"); + } else { + context.set("presentation", "browser"); + } + + await next(); +}); + +export default presentation; diff --git a/server/src/middlewares/real-ip.ts b/src/middlewares/real-ip.ts similarity index 100% rename from server/src/middlewares/real-ip.ts rename to src/middlewares/real-ip.ts diff --git a/server/src/routes/port.ts b/src/routes/port.ts similarity index 93% rename from server/src/routes/port.ts rename to src/routes/port.ts index 5b15585..942ba1b 100644 --- a/server/src/routes/port.ts +++ b/src/routes/port.ts @@ -1,9 +1,9 @@ import { Hono } from "hono"; -import { getConnInfo } from "hono/bun"; import connectToAddress from "../utils/connect-to-address"; import { z } from "zod"; import * as IP from "ip"; import realIP from "../middlewares/real-ip"; +import presentation from "../middlewares/presentation"; export const portRoute = new Hono(); @@ -34,7 +34,7 @@ const schema = z.object({ .pipe(z.number().min(100).max(60_000)), }); -portRoute.get("/:port", realIP, async context => { +portRoute.get("/:port", realIP, presentation, async context => { const rawData = { ip: context.get("ip"), port: context.req.param("port"), diff --git a/src/routes/root.ts b/src/routes/root.ts new file mode 100644 index 0000000..dbb78bb --- /dev/null +++ b/src/routes/root.ts @@ -0,0 +1,12 @@ +import { Hono } from "hono"; +import presentation from "../middlewares/presentation"; +import realIP from "../middlewares/real-ip"; +import render from "../utils/renderer"; + +export const rootRoute = new Hono(); + +rootRoute.get("/", realIP, presentation, context => { + return render(context, "index", { + ip: context.get("ip"), + }); +}); diff --git a/server/src/utils/connect-to-address.ts b/src/utils/connect-to-address.ts similarity index 100% rename from server/src/utils/connect-to-address.ts rename to src/utils/connect-to-address.ts diff --git a/src/utils/renderer.ts b/src/utils/renderer.ts new file mode 100644 index 0000000..ff75bb3 --- /dev/null +++ b/src/utils/renderer.ts @@ -0,0 +1,46 @@ +import { Context } from "hono"; +import { PresentationType } from "../middlewares/presentation"; + +export default async function render( + request: Context, + templateName: "index", + ctx: Record, +) { + const presentation = request.get("presentation"); + let content = await getTemplate(templateName, presentation); + + for (const [key, value] of Object.entries(ctx)) { + content = content.replaceAll(`{{${key}}}`, value); + } + + switch (presentation) { + case "browser": { + return request.html(content); + } + case "terminal": { + return request.text(content); + } + } +} + +const _templateCache: Record = {}; + +async function getTemplate( + templateName: "index", + presentation: PresentationType, +): Promise { + const extension = { + browser: ".html", + terminal: ".txt", + }[presentation]; + const key = templateName + extension; + + if (_templateCache[key]) { + return _templateCache[key]; + } + + const currentPath = await Bun.resolve(`./templates/${key}`, process.cwd()); + _templateCache[key] = await Bun.file(currentPath).text(); + + return _templateCache[key]; +} diff --git a/server/src/utils/timeout.ts b/src/utils/timeout.ts similarity index 100% rename from server/src/utils/timeout.ts rename to src/utils/timeout.ts diff --git a/static/copy.svg b/static/copy.svg new file mode 100644 index 0000000..b4e4725 --- /dev/null +++ b/static/copy.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..a7bbe4a --- /dev/null +++ b/templates/index.html @@ -0,0 +1,140 @@ + + + + + + + amiopen.now + + + + +
+

Am I Open

+

+ 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
+
+
+ + + diff --git a/templates/index.txt b/templates/index.txt new file mode 100644 index 0000000..9315426 --- /dev/null +++ b/templates/index.txt @@ -0,0 +1 @@ +{{ip}} diff --git a/server/tsconfig.json b/tsconfig.json similarity index 100% rename from server/tsconfig.json rename to tsconfig.json From 2a55890764a9227fd89bcdc7d6b8e9bb435c7665 Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Wed, 20 Nov 2024 16:49:02 +0100 Subject: [PATCH 3/6] feat: Use nunjucks for rendering --- Dockerfile | 3 -- bun.lockb | Bin 4426 -> 6252 bytes package.json | 2 + src/index.ts | 5 +++ src/utils/renderer.ts | 45 +++++++---------------- templates/{index.html => index.html.njk} | 1 + templates/{index.txt => index.txt.njk} | 0 upload_to_server.sh | 3 +- 8 files changed, 22 insertions(+), 37 deletions(-) rename templates/{index.html => index.html.njk} (98%) rename templates/{index.txt => index.txt.njk} (100%) diff --git a/Dockerfile b/Dockerfile index d0a9adb..a81df0f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,9 +4,6 @@ WORKDIR /app COPY . /app -# Absolutely no idea why, but piping the output directly to the file makes it empty. -RUN mkdir -p /tmp/templates && mv /app/templates/*.html /tmp/templates/ -RUN bunx html-minifier --collapse-whitespace --remove-comments --remove-optional-tags --remove-redundant-attributes --remove-script-type-attributes --remove-tag-whitespace --use-short-doctype --minify-css true --minify-js true /tmp/templates/index.html > /app/templates/index.html RUN bun install --production EXPOSE 3000 diff --git a/bun.lockb b/bun.lockb index 5b268d4cfdeeb9165ef7f0bf1b9ffb610d23c95e..75248b3870b2fd1b3b65328d34337939df903f11 100755 GIT binary patch delta 1960 zcmc&!drXs86#u@jEp4GKRA9D(@|fsQzrH{OHYpm2Ak$f2^)<-{6e!Y%JciUqYsMCa zf|R?d7+rL6TjnDfI1Hlk5fa@nlgE(F%)~GsY@->@xtH;=b4ovE{B!^8B=`H>bIv{I z-0$3be?9R&U9%=}eg6aBbhDPbVv;#>)jpE7*BLudJ=Z>HH{~b3{7K}^$&_Ehyylqn zWntqAMR7K(!)dWoE=Q5yWUscDRTplm>;g?nQ68?6bE4xI3<&agn^o>`n1{kV7!cR& zVkjyEun$lM2xba=o)v;||GogemtQ&Beq#PoHs$u{qAd5N_CAkS)b~O6kRr`KDKam2 zYIka_BhJyDyWdlCf2`o@v@PtqqZ#a1HMWEci6PjL#LC2xitW#KmMVdl}LNaFZdA0Hz8^x*&tu zARsP)!vfMJNOw!1C=L`vAq?o7U;Ycit|>VTM1?=gIHC^cAwZ7_gc>wUv1i46U&8~ zPmrdOS*Mc^JWMRAH#Ne)(A3k=r+|3oya;YKS?l1)^^$FW z%DJS)9}Z_dw>uHPH9%o@-L>eN)eYX2d#7jXrg!ES&%EK5{np`5fB)EeLqlq5D31;yXM)VMj3~usT28F+Rgg>Y4JB$x4Ll`b zm4z>LWssfHbL30SRWY7pBhVGO4M<52GZ^hCob7K6;FOV2T1}nLIz~ZR6Kj~ z%*N0glQafRGKrN(mmPy-*bkoGz^T{hG&&&R0Lk(M2p$7?k|>I&VV)wvGgkvq6jfO2 z*kmo@DoQRkycuQGc12*RB=qcJ$VIs-g=b@_C5Z{uB~J5h_^-y~tvqt%{|@pEOVlwt@}x zq6HywK*WR8gP>G`w0IFccoAC^6nf}QOF@e1MGvAE>&#|zD%64Do7ws1+nM*?MtsRA zn+Nw_?JMWi_t}TRxq+ToZhG^4=SV_Yw!V~qO@FE`$6ke)V@k!|6CG0O31b(o2MnUD-0995AY;laS!|m z*s$)`;H+eEFZ_{gco5KFK(@Fap2;?xku=a0i%W1$vEesnZfY9C1#+R@82h_fHyVxZYTd1eRM_2Yz)C0*rHyGT+L(q;Xehux)LiI+-uH(FPw!!INH{DB$ck*-Hw;ZlY$>YGT9&pRgbha zUdzpmk&m~-HAC=(nJ|wWWvpF5T92f*D%|Rhn~84o#MVkf+X&YGVFcm6o|gOFbUp-M m^}Rc(mcBkw93JbsI9_n0d=VbE#vs|!0^_ak7AAN6, ) { - const presentation = request.get("presentation"); - let content = await getTemplate(templateName, presentation); - - for (const [key, value] of Object.entries(ctx)) { - content = content.replaceAll(`{{${key}}}`, value); - } - - switch (presentation) { - case "browser": { - return request.html(content); - } - case "terminal": { - return request.text(content); - } - } -} - -const _templateCache: Record = {}; - -async function getTemplate( - templateName: "index", - presentation: PresentationType, -): Promise { + const presentation = context.get("presentation") as PresentationType; const extension = { browser: ".html", terminal: ".txt", }[presentation]; - const key = templateName + extension; + const key = templateName + extension + ".njk"; - if (_templateCache[key]) { - return _templateCache[key]; + const content = nunjucks.render(key, ctx); + + switch (presentation) { + case "browser": + return context.html(content); + break; + case "terminal": + return context.text(content); + break; } - - const currentPath = await Bun.resolve(`./templates/${key}`, process.cwd()); - _templateCache[key] = await Bun.file(currentPath).text(); - - return _templateCache[key]; } diff --git a/templates/index.html b/templates/index.html.njk similarity index 98% rename from templates/index.html rename to templates/index.html.njk index a7bbe4a..847db48 100644 --- a/templates/index.html +++ b/templates/index.html.njk @@ -4,6 +4,7 @@ + amiopen.now + + {% block header %}{% endblock %} + + +
+ {% block main %} +

Am I Open

+ {% endblock %} +
+ {% block scripts %} + {% endblock %} + + diff --git a/templates/index.html.njk b/templates/index.html.njk index 847db48..dfd7416 100644 --- a/templates/index.html.njk +++ b/templates/index.html.njk @@ -1,141 +1,110 @@ - - - - - - - - amiopen.now +{% extends "base.html.njk" %} - +{% endblock %} - vertical-align: middle; +{% block main %} +

Am I Open

+

+ Your IP address: {{ip}} + +

- transition: background-color 0.2s; - } - - .copy:hover { - background: #ffffff40; - } - - .copy img { - width: 1rem; - color: #fff; - filter: invert(1); - } - - - -
-

Am I Open

-

- Your IP address: {{ip}} - -

- - Check if your port is reachable: -
curl amiopen.now/<port>
+	 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>
+	 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>
+	 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
-
-
- - - +{% block scripts %} + +{% endblock %} From 94009fb08ea5fd15fb80ed0cae32d8b26b81e703 Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Wed, 20 Nov 2024 17:06:46 +0100 Subject: [PATCH 5/6] feat(templates): Add port templates --- src/routes/port.ts | 11 +++-- src/utils/renderer.ts | 2 +- templates/base.html.njk | 5 +++ templates/index.html.njk | 5 --- templates/port.html.njk | 96 ++++++++++++++++++++++++++++++++++++++++ templates/port.txt.njk | 1 + 6 files changed, 111 insertions(+), 9 deletions(-) create mode 100644 templates/port.html.njk create mode 100644 templates/port.txt.njk diff --git a/src/routes/port.ts b/src/routes/port.ts index 942ba1b..5f40846 100644 --- a/src/routes/port.ts +++ b/src/routes/port.ts @@ -4,6 +4,7 @@ import { z } from "zod"; import * as IP from "ip"; import realIP from "../middlewares/real-ip"; import presentation from "../middlewares/presentation"; +import render from "../utils/renderer"; export const portRoute = new Hono(); @@ -55,12 +56,14 @@ portRoute.get("/:port", realIP, presentation, async context => { const result = await connectToAddress(ip, port, { timeout }); - return context.json({ + return render(context, "port", { + port: port, isOpen: result.isOpen, + ip: ip, }); }); -portRoute.get("/:ip/:port", async context => { +portRoute.get("/:ip/:port", realIP, presentation, async context => { const rawData = { ip: context.req.param("ip"), port: context.req.param("port"), @@ -92,7 +95,9 @@ portRoute.get("/:ip/:port", async context => { const result = await connectToAddress(ip, port, { timeout }); - return context.json({ + return render(context, "port", { + port: port, isOpen: result.isOpen, + ip: ip, }); }); diff --git a/src/utils/renderer.ts b/src/utils/renderer.ts index 9e56dd3..7469c8b 100644 --- a/src/utils/renderer.ts +++ b/src/utils/renderer.ts @@ -4,7 +4,7 @@ import * as nunjucks from "nunjucks"; export default async function render( context: Context, - templateName: "index", + templateName: "index" | "port", ctx: Record, ) { const presentation = context.get("presentation") as PresentationType; diff --git a/templates/base.html.njk b/templates/base.html.njk index 12802e8..8018eca 100644 --- a/templates/base.html.njk +++ b/templates/base.html.njk @@ -33,6 +33,11 @@ border-radius: 1em; padding: 3em; } + + h2 { + margin-top: 0; + text-align: left; + } {% block header %}{% endblock %} diff --git a/templates/index.html.njk b/templates/index.html.njk index dfd7416..1d32b5a 100644 --- a/templates/index.html.njk +++ b/templates/index.html.njk @@ -2,11 +2,6 @@ {% block header %} +{% endblock %} + +{% block main %} +

Am I Open

+ + {{ ip }} + + {% if isOpen %} +
+
+ +

Port {{port}} is open

+
+ {% else %} +
+
+ +

Port {{port}} is closed

+
+ {% endif %} +{% endblock %} diff --git a/templates/port.txt.njk b/templates/port.txt.njk new file mode 100644 index 0000000..59d8068 --- /dev/null +++ b/templates/port.txt.njk @@ -0,0 +1 @@ +{% if isOpen %}open{% else %}closed{% endif %} From 790bd95a12fceecb70211ff1898017e12a6a70e5 Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Thu, 21 Nov 2024 20:22:04 +0100 Subject: [PATCH 6/6] feat(templates): Add social links --- src/utils/renderer.ts | 2 -- templates/base.html.njk | 28 ++++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/utils/renderer.ts b/src/utils/renderer.ts index 7469c8b..61d4ecf 100644 --- a/src/utils/renderer.ts +++ b/src/utils/renderer.ts @@ -19,9 +19,7 @@ export default async function render( switch (presentation) { case "browser": return context.html(content); - break; case "terminal": return context.text(content); - break; } } diff --git a/templates/base.html.njk b/templates/base.html.njk index 8018eca..70ff793 100644 --- a/templates/base.html.njk +++ b/templates/base.html.njk @@ -38,6 +38,25 @@ margin-top: 0; text-align: left; } + + .socials { + display: flex; + justify-content: end; + column-gap: 1em; + margin-top: 1.4em; + } + + .socials img { + color: #fff; + filter: invert(0.7); + border-radius: 50%; + + width: 1.4em; + } + + .socials img:hover { + filter: invert(1); + } {% block header %}{% endblock %} @@ -47,6 +66,15 @@ {% block main %}

Am I Open

{% endblock %} + + {% block scripts %} {% endblock %}