mirror of
https://github.com/versia-pub/server.git
synced 2026-04-28 13:19:16 +02:00
refactor: 🚚 Organize code into sub-packages, instead of a single large package
This commit is contained in:
parent
79742f47dc
commit
a6d3ebbeef
366 changed files with 942 additions and 833 deletions
40
packages/api/middlewares/ip-bans.ts
Normal file
40
packages/api/middlewares/ip-bans.ts
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
import { getLogger } from "@logtape/logtape";
|
||||
import { ApiError } from "@versia/kit";
|
||||
import { config } from "@versia-server/config";
|
||||
import type { SocketAddress } from "bun";
|
||||
import { createMiddleware } from "hono/factory";
|
||||
import { matches } from "ip-matching";
|
||||
import { sentry } from "@/sentry";
|
||||
|
||||
export const ipBans = createMiddleware(async (context, next) => {
|
||||
// Check for banned IPs
|
||||
|
||||
const requestIp = context.env?.ip as SocketAddress | undefined | null;
|
||||
|
||||
if (!requestIp?.address) {
|
||||
await next();
|
||||
return;
|
||||
}
|
||||
|
||||
for (const ip of config.http.banned_ips) {
|
||||
try {
|
||||
if (matches(ip, requestIp?.address)) {
|
||||
throw new ApiError(403, "Forbidden");
|
||||
}
|
||||
} catch (e) {
|
||||
const logger = getLogger("server");
|
||||
|
||||
logger.error`Error while parsing banned IP "${ip}" `;
|
||||
logger.error`${e}`;
|
||||
sentry?.captureException(e);
|
||||
|
||||
return context.json(
|
||||
{ error: `A server error occured: ${(e as Error).message}` },
|
||||
500,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
await next();
|
||||
return;
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue