mirror of
https://github.com/Myzel394/amiopen.now.git
synced 2025-06-18 07:25:27 +02:00
feat: Add first poc
This commit is contained in:
parent
b79a7293af
commit
1390ef247c
@ -4,9 +4,12 @@
|
||||
"dev": "bun run --hot src/index.ts"
|
||||
},
|
||||
"dependencies": {
|
||||
"hono": "^4.6.9"
|
||||
"hono": "^4.6.9",
|
||||
"id": "^0.0.0",
|
||||
"zod": "^3.23.8"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/bun": "latest"
|
||||
"@types/bun": "latest",
|
||||
"@types/ip": "^1.1.3"
|
||||
}
|
||||
}
|
14
src/index.ts
14
src/index.ts
@ -1,9 +1,19 @@
|
||||
import { Hono } from 'hono'
|
||||
import {getConnInfo} from "hono/bun"
|
||||
import connectToAddress from './utils/connect-to-address'
|
||||
|
||||
const app = new Hono()
|
||||
|
||||
app.get('/', (c) => {
|
||||
return c.text('Hello Hono!')
|
||||
app.get('/:port', async context => {
|
||||
const port = Number(context.req.param("port"))
|
||||
const info = getConnInfo(context)
|
||||
const ipAddress = info.remote.address!
|
||||
|
||||
const result = await connectToAddress(ipAddress, port)
|
||||
|
||||
return context.json({
|
||||
isOpen: result.isOpen
|
||||
})
|
||||
})
|
||||
|
||||
export default app
|
||||
|
33
src/routes/port.ts
Normal file
33
src/routes/port.ts
Normal file
@ -0,0 +1,33 @@
|
||||
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"
|
||||
|
||||
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.coerce.number().min(1).max(2**16 - 1),
|
||||
});
|
||||
|
||||
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);
|
||||
|
||||
if (!parsedData.success) {
|
||||
return context.json({ error: parsedData.error }, 401);
|
||||
}
|
||||
|
||||
const { ip, port } = parsedData.data;
|
||||
|
||||
const result = await connectToAddress(ip, port)
|
||||
|
||||
return context.json({
|
||||
isOpen: result.isOpen
|
||||
})
|
||||
});
|
43
src/utils/connect-to-address.ts
Normal file
43
src/utils/connect-to-address.ts
Normal file
@ -0,0 +1,43 @@
|
||||
export interface ConnectionResult {
|
||||
isOpen: boolean
|
||||
}
|
||||
|
||||
const TIMEOUT = 5
|
||||
|
||||
export default function connectToAddress(address: string, port: number): Promise<ConnectionResult> {
|
||||
return new Promise<ConnectionResult>(async (resolve) => {
|
||||
const socket = await Bun.connect({
|
||||
hostname: address,
|
||||
port: port,
|
||||
|
||||
socket: {
|
||||
open(socket) {
|
||||
socket.end()
|
||||
resolve({
|
||||
isOpen: true
|
||||
})
|
||||
},
|
||||
data() {
|
||||
},
|
||||
connectError() {
|
||||
resolve({
|
||||
isOpen: false
|
||||
})
|
||||
},
|
||||
error(socket) {
|
||||
socket.end()
|
||||
resolve({
|
||||
isOpen: false
|
||||
})
|
||||
},
|
||||
timeout() {
|
||||
resolve({
|
||||
isOpen: false
|
||||
})
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
socket.timeout(TIMEOUT)
|
||||
})
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user