server/middlewares/agent-bans.ts

17 lines
515 B
TypeScript
Raw Normal View History

2024-12-18 20:42:40 +01:00
import { createMiddleware } from "hono/factory";
import { ApiError } from "~/classes/errors/api-error";
import { config } from "~/packages/config-manager";
export const agentBans = createMiddleware(async (context, next) => {
// Check for banned user agents (regex)
const ua = context.req.header("user-agent") ?? "";
for (const agent of config.http.banned_user_agents) {
if (new RegExp(agent).test(ua)) {
throw new ApiError(403, "Forbidden");
}
}
await next();
});