From 2067fe27b3196a8dc0dd7e7a406dcc9c027301ed Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Wed, 6 Nov 2024 15:03:56 +0100 Subject: [PATCH] fix: Improvements --- package.json | 1 + src/index.ts | 2 ++ src/routes/port.ts | 13 +++++------ src/utils/connect-to-address.ts | 38 +++++++++++++++++---------------- 4 files changed, 30 insertions(+), 24 deletions(-) diff --git a/package.json b/package.json index ba142ad..b9a2da3 100644 --- a/package.json +++ b/package.json @@ -2,6 +2,7 @@ "name": "amiopen", "scripts": { "dev": "bun run --hot src/index.ts", + "start": "bun run src/index.ts", "prettier": "prettier --write ." }, "dependencies": { diff --git a/src/index.ts b/src/index.ts index 0bf799b..537adba 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,8 +1,10 @@ import { Hono } from "hono"; import { portRoute } from "./routes/port"; +import { timeout } from "hono/timeout"; const app = new Hono(); +app.use(timeout(90_000)); app.route("/", portRoute); export default app; diff --git a/src/routes/port.ts b/src/routes/port.ts index be1ec8d..4104614 100644 --- a/src/routes/port.ts +++ b/src/routes/port.ts @@ -23,7 +23,7 @@ const schema = z.object({ .string() .optional() .transform(val => (val === undefined ? 5_000 : Number(val))) - .pipe(z.number().min(1_000).max(60_000)), + .pipe(z.number().min(100).max(60_000)), }); portRoute.get("/:port", async context => { @@ -31,7 +31,7 @@ portRoute.get("/:port", async context => { const rawData = { ip: info.remote.address || "", port: context.req.param("port"), - timeout: context.req.query("timeout") || "", + timeout: context.req.query("timeout") || context.req.query("t") || "", }; const parsedData = schema.safeParse(rawData); @@ -44,9 +44,9 @@ portRoute.get("/:port", async context => { ); } - const { ip, port } = parsedData.data; + const { ip, port, timeout } = parsedData.data; - const result = await connectToAddress(ip, port); + const result = await connectToAddress(ip, port, { timeout }); return context.json({ isOpen: result.isOpen, @@ -57,6 +57,7 @@ portRoute.get("/:ip/:port", async context => { const rawData = { ip: context.req.param("ip"), port: context.req.param("port"), + timeout: context.req.query("timeout") || context.req.query("t") || "", }; const parsedData = schema.safeParse(rawData); @@ -69,9 +70,9 @@ portRoute.get("/:ip/:port", async context => { ); } - const { ip, port } = parsedData.data; + const { ip, port, timeout } = parsedData.data; - const result = await connectToAddress(ip, port); + const result = await connectToAddress(ip, port, { timeout }); return context.json({ isOpen: result.isOpen, diff --git a/src/utils/connect-to-address.ts b/src/utils/connect-to-address.ts index 6a412fc..67a3d6b 100644 --- a/src/utils/connect-to-address.ts +++ b/src/utils/connect-to-address.ts @@ -1,51 +1,53 @@ import * as net from "net"; +export interface ConnectionParams { + timeout: number; +} + export interface ConnectionResult { isOpen: boolean; } -const TIMEOUT = 5_000; - export default function connectToAddress( address: string, port: number, + { timeout }: ConnectionParams = { timeout: 5_000 }, ): Promise { return new Promise(async resolve => { - const onIsOpen = () => { + let $timeout: Timer; + + const socket = new net.Socket(); + const close = () => { + if ($timeout) { + clearTimeout($timeout); + } + if (!socket.destroyed) { socket.end(); socket.destroy(); } - + }; + const onIsOpen = () => { + close(); resolve({ isOpen: true, }); }; const onIsClosed = () => { - if (!socket.destroyed) { - socket.end(); - socket.destroy(); - } - + close(); resolve({ isOpen: false, }); }; - const socket = new net.Socket(); - // The `setTimeout` function from a socket does not work when connecting, // so we need to use a custom timeout function ourselves. - setTimeout(() => { - if (!socket.destroyed) { - socket.end(); - socket.destroy(); - } - + $timeout = setTimeout(() => { + close(); resolve({ isOpen: false, }); - }, TIMEOUT); + }, timeout); socket.on("ready", onIsOpen); socket.on("close", onIsClosed);