mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 05:49:16 +01:00
refactor(api): 🎨 Finish Hono refactor
This commit is contained in:
parent
826a260e90
commit
959dd27ad6
20 changed files with 309 additions and 316 deletions
16
middlewares/agent-bans.ts
Normal file
16
middlewares/agent-bans.ts
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
import { errorResponse } from "@response";
|
||||
import { createMiddleware } from "hono/factory";
|
||||
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)) {
|
||||
return errorResponse("Forbidden", 403);
|
||||
}
|
||||
}
|
||||
|
||||
await next();
|
||||
});
|
||||
73
middlewares/bait.ts
Normal file
73
middlewares/bait.ts
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
import { logger } from "@loggers";
|
||||
import { errorResponse, response } from "@response";
|
||||
import type { SocketAddress } from "bun";
|
||||
import { createMiddleware } from "hono/factory";
|
||||
import { matches } from "ip-matching";
|
||||
import { config } from "~packages/config-manager";
|
||||
import { LogLevel } from "~packages/log-manager";
|
||||
|
||||
export const bait = createMiddleware(async (context, next) => {
|
||||
const request_ip = context.env?.ip as SocketAddress | undefined | null;
|
||||
|
||||
if (config.http.bait.enabled) {
|
||||
// Check for bait IPs
|
||||
if (request_ip?.address) {
|
||||
for (const ip of config.http.bait.bait_ips) {
|
||||
try {
|
||||
if (matches(ip, request_ip.address)) {
|
||||
const file = Bun.file(
|
||||
config.http.bait.send_file || "./beemovie.txt",
|
||||
);
|
||||
|
||||
if (await file.exists()) {
|
||||
return response(file);
|
||||
}
|
||||
await logger.log(
|
||||
LogLevel.ERROR,
|
||||
"Server.Bait",
|
||||
`Bait file not found: ${config.http.bait.send_file}`,
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
logger.log(
|
||||
LogLevel.ERROR,
|
||||
"Server.IPCheck",
|
||||
`Error while parsing bait IP "${ip}" `,
|
||||
);
|
||||
logger.logError(
|
||||
LogLevel.ERROR,
|
||||
"Server.IPCheck",
|
||||
e as Error,
|
||||
);
|
||||
|
||||
return errorResponse(
|
||||
`A server error occured: ${(e as Error).message}`,
|
||||
500,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check for bait user agents (regex)
|
||||
const ua = context.req.header("user-agent") ?? "";
|
||||
|
||||
for (const agent of config.http.bait.bait_user_agents) {
|
||||
if (new RegExp(agent).test(ua)) {
|
||||
const file = Bun.file(
|
||||
config.http.bait.send_file || "./beemovie.txt",
|
||||
);
|
||||
|
||||
if (await file.exists()) {
|
||||
return response(file);
|
||||
}
|
||||
await logger.log(
|
||||
LogLevel.ERROR,
|
||||
"Server.Bait",
|
||||
`Bait file not found: ${config.http.bait.send_file}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await next();
|
||||
});
|
||||
40
middlewares/ip-bans.ts
Normal file
40
middlewares/ip-bans.ts
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
import { logger } from "@loggers";
|
||||
import { errorResponse } from "@response";
|
||||
import type { SocketAddress } from "bun";
|
||||
import { createMiddleware } from "hono/factory";
|
||||
import { matches } from "ip-matching";
|
||||
import { config } from "~packages/config-manager";
|
||||
import { LogLevel } from "~packages/log-manager";
|
||||
|
||||
export const ipBans = createMiddleware(async (context, next) => {
|
||||
// Check for banned IPs
|
||||
|
||||
const request_ip = context.env?.ip as SocketAddress | undefined | null;
|
||||
|
||||
if (!request_ip?.address) {
|
||||
await next();
|
||||
return;
|
||||
}
|
||||
|
||||
for (const ip of config.http.banned_ips) {
|
||||
try {
|
||||
if (matches(ip, request_ip?.address)) {
|
||||
return errorResponse("Forbidden", 403);
|
||||
}
|
||||
} catch (e) {
|
||||
logger.log(
|
||||
LogLevel.ERROR,
|
||||
"Server.IPCheck",
|
||||
`Error while parsing banned IP "${ip}" `,
|
||||
);
|
||||
logger.logError(LogLevel.ERROR, "Server.IPCheck", e as Error);
|
||||
|
||||
return errorResponse(
|
||||
`A server error occured: ${(e as Error).message}`,
|
||||
500,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
await next();
|
||||
});
|
||||
18
middlewares/logger.ts
Normal file
18
middlewares/logger.ts
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
import { dualLogger } from "@loggers";
|
||||
import type { SocketAddress } from "bun";
|
||||
import { createMiddleware } from "hono/factory";
|
||||
import { config } from "~packages/config-manager";
|
||||
|
||||
export const logger = createMiddleware(async (context, next) => {
|
||||
const request_ip = context.env?.ip as SocketAddress | undefined | null;
|
||||
|
||||
if (config.logging.log_requests) {
|
||||
await dualLogger.logRequest(
|
||||
context.req.raw,
|
||||
config.logging.log_ip ? request_ip?.address : undefined,
|
||||
config.logging.log_requests_verbose,
|
||||
);
|
||||
}
|
||||
|
||||
await next();
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue