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:
Jesse Wierzbinski 2025-02-15 02:47:29 +01:00
parent d4afd84019
commit 54fd81f076
No known key found for this signature in database
118 changed files with 3892 additions and 5291 deletions

View file

@ -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)

View file

@ -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();
});

View file

@ -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

View file

@ -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}`;

View file

@ -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