mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 05:49:16 +01:00
refactor(config): ♻️ Redo config structure from scratch, simplify validation code, improve checks, add support for loading sensitive data from paths
This commit is contained in:
parent
d4afd84019
commit
54fd81f076
118 changed files with 3892 additions and 5291 deletions
|
|
@ -1,6 +1,6 @@
|
|||
import { createMiddleware } from "hono/factory";
|
||||
import { ApiError } from "~/classes/errors/api-error";
|
||||
import { config } from "~/packages/config-manager";
|
||||
import { config } from "~/config.ts";
|
||||
|
||||
export const agentBans = createMiddleware(async (context, next) => {
|
||||
// Check for banned user agents (regex)
|
||||
|
|
|
|||
|
|
@ -1,51 +0,0 @@
|
|||
import { getLogger } from "@logtape/logtape";
|
||||
import type { BunFile, SocketAddress } from "bun";
|
||||
import { createMiddleware } from "hono/factory";
|
||||
import { matches } from "ip-matching";
|
||||
import { config } from "~/packages/config-manager";
|
||||
|
||||
const baitFile = async (): Promise<BunFile | undefined> => {
|
||||
const file = Bun.file(config.http.bait.send_file || "./beemovie.txt");
|
||||
|
||||
if (await file.exists()) {
|
||||
return file;
|
||||
}
|
||||
|
||||
const logger = getLogger("server");
|
||||
|
||||
logger.error`Bait file not found: ${config.http.bait.send_file}`;
|
||||
};
|
||||
|
||||
export const bait = createMiddleware(async (context, next) => {
|
||||
const requestIp = context.env?.ip as SocketAddress | undefined | null;
|
||||
|
||||
if (!config.http.bait.enabled) {
|
||||
return await next();
|
||||
}
|
||||
|
||||
const file = await baitFile();
|
||||
|
||||
if (!file) {
|
||||
return await next();
|
||||
}
|
||||
|
||||
// Check for bait IPs
|
||||
if (requestIp?.address) {
|
||||
for (const ip of config.http.bait.bait_ips) {
|
||||
if (matches(ip, requestIp.address)) {
|
||||
return context.body(file.stream());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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)) {
|
||||
return context.body(file.stream());
|
||||
}
|
||||
}
|
||||
|
||||
await next();
|
||||
});
|
||||
|
|
@ -4,7 +4,7 @@ import type { SocketAddress } from "bun";
|
|||
import { createMiddleware } from "hono/factory";
|
||||
import { matches } from "ip-matching";
|
||||
import { ApiError } from "~/classes/errors/api-error";
|
||||
import { config } from "~/packages/config-manager";
|
||||
import { config } from "~/config.ts";
|
||||
|
||||
export const ipBans = createMiddleware(async (context, next) => {
|
||||
// Check for banned IPs
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
import { getLogger } from "@logtape/logtape";
|
||||
import chalk from "chalk";
|
||||
import { createMiddleware } from "hono/factory";
|
||||
import { config } from "~/packages/config-manager";
|
||||
import { config } from "~/config.ts";
|
||||
|
||||
export const logger = createMiddleware(async (context, next) => {
|
||||
if (config.logging.log_requests) {
|
||||
if (config.logging.types.requests) {
|
||||
const serverLogger = getLogger("server");
|
||||
const body = await context.req.raw.clone().text();
|
||||
|
||||
|
|
@ -25,7 +25,7 @@ export const logger = createMiddleware(async (context, next) => {
|
|||
|
||||
const bodyLog = `${chalk.bold("Body")}: ${chalk.gray(body)}`;
|
||||
|
||||
if (config.logging.log_requests_verbose) {
|
||||
if (config.logging.types.requests_content) {
|
||||
serverLogger.debug`${urlAndMethod}\n${hash}\n${headers}\n${bodyLog}`;
|
||||
} else {
|
||||
serverLogger.debug`${urlAndMethod}`;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import { createMiddleware } from "hono/factory";
|
||||
import { config } from "~/packages/config-manager";
|
||||
import { config } from "~/config.ts";
|
||||
|
||||
export const urlCheck = createMiddleware(async (context, next) => {
|
||||
// Check that request URL matches base_url
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue