mirror of
https://github.com/Myzel394/amiopen.now.git
synced 2025-06-18 07:25:27 +02:00
fix: Improvements; Add prettier; Improve timeouts
This commit is contained in:
parent
c60913b796
commit
cb34b81209
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,2 +1,4 @@
|
||||
# deps
|
||||
node_modules/
|
||||
|
||||
debug.log
|
||||
|
7
.prettierrc.json
Normal file
7
.prettierrc.json
Normal file
@ -0,0 +1,7 @@
|
||||
{
|
||||
"tabWidth": 4,
|
||||
"useTabs": true,
|
||||
"singleQuote": false,
|
||||
"trailingComma": "all",
|
||||
"arrowParens": "avoid"
|
||||
}
|
@ -1,9 +1,11 @@
|
||||
To install dependencies:
|
||||
|
||||
```sh
|
||||
bun install
|
||||
```
|
||||
|
||||
To run:
|
||||
|
||||
```sh
|
||||
bun run dev
|
||||
```
|
||||
|
28
package.json
28
package.json
@ -1,15 +1,17 @@
|
||||
{
|
||||
"name": "amiopen",
|
||||
"scripts": {
|
||||
"dev": "bun run --hot src/index.ts"
|
||||
},
|
||||
"dependencies": {
|
||||
"hono": "^4.6.9",
|
||||
"ip": "^2.0.1",
|
||||
"zod": "^3.23.8"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/bun": "latest",
|
||||
"@types/ip": "^1.1.3"
|
||||
}
|
||||
"name": "amiopen",
|
||||
"scripts": {
|
||||
"dev": "bun run --hot src/index.ts",
|
||||
"prettier": "prettier --write ."
|
||||
},
|
||||
"dependencies": {
|
||||
"hono": "^4.6.9",
|
||||
"ip": "^2.0.1",
|
||||
"zod": "^3.23.8"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/bun": "latest",
|
||||
"@types/ip": "^1.1.3",
|
||||
"prettier": "^3.3.3"
|
||||
}
|
||||
}
|
||||
|
13
src/index.ts
13
src/index.ts
@ -1,11 +1,8 @@
|
||||
import { Hono } from 'hono'
|
||||
import { portRoute } from './routes/port'
|
||||
import { Hono } from "hono";
|
||||
import { portRoute } from "./routes/port";
|
||||
|
||||
const app = new Hono()
|
||||
const app = new Hono();
|
||||
|
||||
app.route(
|
||||
"/",
|
||||
portRoute,
|
||||
)
|
||||
app.route("/", portRoute);
|
||||
|
||||
export default app
|
||||
export default app;
|
||||
|
@ -2,52 +2,78 @@ 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 * as IP from "ip";
|
||||
|
||||
export const portRoute = new Hono();
|
||||
|
||||
const schema = z.object({
|
||||
ip: z.string().refine(ip => IP.isPublic(ip), "This IP address is not valid"),
|
||||
port: z.string().transform(Number).pipe(z.number().min(1).max(2**16 - 1)),
|
||||
ip: z
|
||||
.string()
|
||||
.refine(ip => IP.isPublic(ip), "This IP address is not valid"),
|
||||
port: z
|
||||
.string()
|
||||
.transform(Number)
|
||||
.pipe(
|
||||
z
|
||||
.number()
|
||||
.min(1)
|
||||
.max(2 ** 16 - 1),
|
||||
),
|
||||
timeout: z
|
||||
.string()
|
||||
.optional()
|
||||
.transform(val => (val === undefined ? 5_000 : Number(val)))
|
||||
.pipe(z.number().min(1_000).max(60_000)),
|
||||
});
|
||||
|
||||
portRoute.get("/:port", async context => {
|
||||
const info = getConnInfo(context)
|
||||
const rawData = {
|
||||
ip: info.remote.address || "",
|
||||
port: context.req.param("port"),
|
||||
};
|
||||
const parsedData = schema.safeParse(rawData);
|
||||
const info = getConnInfo(context);
|
||||
const rawData = {
|
||||
ip: info.remote.address || "",
|
||||
port: context.req.param("port"),
|
||||
timeout: context.req.query("timeout") || "",
|
||||
};
|
||||
const parsedData = schema.safeParse(rawData);
|
||||
|
||||
if (!parsedData.success) {
|
||||
return context.json({ error: parsedData.error }, 401);
|
||||
}
|
||||
if (!parsedData.success) {
|
||||
return context.json(
|
||||
{
|
||||
error: parsedData.error,
|
||||
},
|
||||
401,
|
||||
);
|
||||
}
|
||||
|
||||
const { ip, port } = parsedData.data;
|
||||
const { ip, port } = parsedData.data;
|
||||
|
||||
const result = await connectToAddress(ip, port)
|
||||
const result = await connectToAddress(ip, port);
|
||||
|
||||
return context.json({
|
||||
isOpen: result.isOpen
|
||||
})
|
||||
return context.json({
|
||||
isOpen: result.isOpen,
|
||||
});
|
||||
});
|
||||
|
||||
portRoute.get("/:ip/:port", async context => {
|
||||
const rawData = {
|
||||
ip: context.req.param("ip"),
|
||||
port: context.req.param("port"),
|
||||
};
|
||||
const parsedData = schema.safeParse(rawData);
|
||||
const rawData = {
|
||||
ip: context.req.param("ip"),
|
||||
port: context.req.param("port"),
|
||||
};
|
||||
const parsedData = schema.safeParse(rawData);
|
||||
|
||||
if (!parsedData.success) {
|
||||
return context.json({ error: parsedData.error }, 401);
|
||||
}
|
||||
if (!parsedData.success) {
|
||||
return context.json(
|
||||
{
|
||||
error: parsedData.error,
|
||||
},
|
||||
401,
|
||||
);
|
||||
}
|
||||
|
||||
const { ip, port } = parsedData.data;
|
||||
const { ip, port } = parsedData.data;
|
||||
|
||||
const result = await connectToAddress(ip, port)
|
||||
const result = await connectToAddress(ip, port);
|
||||
|
||||
return context.json({
|
||||
isOpen: result.isOpen
|
||||
})
|
||||
})
|
||||
return context.json({
|
||||
isOpen: result.isOpen,
|
||||
});
|
||||
});
|
||||
|
@ -1,54 +1,57 @@
|
||||
import * as net from "net"
|
||||
import * as net from "net";
|
||||
|
||||
export interface ConnectionResult {
|
||||
isOpen: boolean
|
||||
isOpen: boolean;
|
||||
}
|
||||
|
||||
const TIMEOUT = 5_000
|
||||
const TIMEOUT = 5_000;
|
||||
|
||||
export default function connectToAddress(address: string, port: number): Promise<ConnectionResult> {
|
||||
return new Promise<ConnectionResult>(async (resolve) => {
|
||||
const onIsOpen = () => {
|
||||
if (!socket.destroyed) {
|
||||
socket.end()
|
||||
socket.destroy()
|
||||
}
|
||||
export default function connectToAddress(
|
||||
address: string,
|
||||
port: number,
|
||||
): Promise<ConnectionResult> {
|
||||
return new Promise<ConnectionResult>(async resolve => {
|
||||
const onIsOpen = () => {
|
||||
if (!socket.destroyed) {
|
||||
socket.end();
|
||||
socket.destroy();
|
||||
}
|
||||
|
||||
resolve({
|
||||
isOpen: true
|
||||
})
|
||||
}
|
||||
const onIsClosed = () => {
|
||||
if (!socket.destroyed) {
|
||||
socket.end()
|
||||
socket.destroy()
|
||||
}
|
||||
resolve({
|
||||
isOpen: true,
|
||||
});
|
||||
};
|
||||
const onIsClosed = () => {
|
||||
if (!socket.destroyed) {
|
||||
socket.end();
|
||||
socket.destroy();
|
||||
}
|
||||
|
||||
resolve({
|
||||
isOpen: false
|
||||
})
|
||||
}
|
||||
resolve({
|
||||
isOpen: false,
|
||||
});
|
||||
};
|
||||
|
||||
const socket = new net.Socket()
|
||||
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()
|
||||
}
|
||||
// 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();
|
||||
}
|
||||
|
||||
resolve({
|
||||
isOpen: false
|
||||
})
|
||||
}, TIMEOUT)
|
||||
resolve({
|
||||
isOpen: false,
|
||||
});
|
||||
}, TIMEOUT);
|
||||
|
||||
socket.on("ready", onIsOpen)
|
||||
socket.on("close", onIsClosed)
|
||||
socket.on("error", onIsClosed)
|
||||
socket.on("timeout", onIsClosed)
|
||||
socket.on("ready", onIsOpen);
|
||||
socket.on("close", onIsClosed);
|
||||
socket.on("error", onIsClosed);
|
||||
socket.on("timeout", onIsClosed);
|
||||
|
||||
socket.connect(port, address)
|
||||
})
|
||||
socket.connect(port, address);
|
||||
});
|
||||
}
|
||||
|
@ -1,4 +1,3 @@
|
||||
export default function timeout(ms: number): Promise<void> {
|
||||
return new Promise(resolve => setTimeout(resolve, ms))
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"strict": true,
|
||||
"jsx": "react-jsx",
|
||||
"jsxImportSource": "hono/jsx"
|
||||
}
|
||||
}
|
||||
"compilerOptions": {
|
||||
"strict": true,
|
||||
"jsx": "react-jsx",
|
||||
"jsxImportSource": "hono/jsx"
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user