mirror of
https://github.com/Myzel394/amiopen.now.git
synced 2025-06-18 15:35:27 +02:00
feat: Add check for real ip
This commit is contained in:
parent
dc31bf880c
commit
346b14bd53
@ -1,5 +1,6 @@
|
||||
import { Hono } from "hono";
|
||||
import { portRoute } from "./routes/port";
|
||||
import realIP from "./middlewares/real-ip";
|
||||
|
||||
const app = new Hono();
|
||||
|
||||
@ -8,4 +9,4 @@ app.route("/", portRoute);
|
||||
Bun.serve({
|
||||
...app,
|
||||
idleTimeout: 90,
|
||||
})
|
||||
});
|
||||
|
22
src/middlewares/real-ip.ts
Normal file
22
src/middlewares/real-ip.ts
Normal file
@ -0,0 +1,22 @@
|
||||
import { getConnInfo } from "hono/bun";
|
||||
import { createMiddleware } from "hono/factory";
|
||||
|
||||
const realIP = createMiddleware<{
|
||||
Variables: {
|
||||
ip: string;
|
||||
};
|
||||
}>(async (context, next) => {
|
||||
const ip =
|
||||
context.req.header("x-forwarded-for") ||
|
||||
getConnInfo(context).remote.address;
|
||||
|
||||
if (!ip) {
|
||||
return context.json({ error: "Your IP could not be found" }, 401);
|
||||
}
|
||||
|
||||
context.set("ip", ip);
|
||||
|
||||
await next();
|
||||
});
|
||||
|
||||
export default realIP;
|
@ -3,6 +3,7 @@ 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";
|
||||
|
||||
export const portRoute = new Hono();
|
||||
|
||||
@ -33,15 +34,12 @@ const schema = z.object({
|
||||
.pipe(z.number().min(100).max(60_000)),
|
||||
});
|
||||
|
||||
portRoute.get("/:port", async context => {
|
||||
const info = getConnInfo(context);
|
||||
portRoute.get("/:port", realIP, async context => {
|
||||
const rawData = {
|
||||
ip: info.remote.address || "",
|
||||
ip: context.get("ip"),
|
||||
port: context.req.param("port"),
|
||||
timeout: context.req.query("timeout") || context.req.query("t") || "",
|
||||
};
|
||||
console.log(info.remote.address);
|
||||
console.log(context.req.header());
|
||||
const parsedData = schema.safeParse(rawData);
|
||||
|
||||
if (!parsedData.success) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user