mirror of
https://github.com/Myzel394/amiopen.now.git
synced 2025-06-18 15:35:27 +02:00
fix: Improvements
This commit is contained in:
parent
cb34b81209
commit
2067fe27b3
@ -2,6 +2,7 @@
|
||||
"name": "amiopen",
|
||||
"scripts": {
|
||||
"dev": "bun run --hot src/index.ts",
|
||||
"start": "bun run src/index.ts",
|
||||
"prettier": "prettier --write ."
|
||||
},
|
||||
"dependencies": {
|
||||
|
@ -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;
|
||||
|
@ -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,
|
||||
|
@ -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<ConnectionResult> {
|
||||
return new Promise<ConnectionResult>(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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user