From 76b1b8c3b4361ad1cc3a19421b0c909a93318312 Mon Sep 17 00:00:00 2001 From: Jesse Wierzbinski Date: Thu, 14 Sep 2023 17:21:38 -1000 Subject: [PATCH] Add logging options --- .gitignore | 2 +- index.ts | 39 +++++++++++++++++++++++++++++++++++++++ utils/config.ts | 26 ++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 3cf3b5b5..f591ed03 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,7 @@ # Logs -logs +logs/* _.log npm-debug.log_ yarn-debug.log* diff --git a/index.ts b/index.ts index 058a7193..0252a8a5 100644 --- a/index.ts +++ b/index.ts @@ -1,4 +1,5 @@ import { getConfig } from "@config"; +import { appendFile } from "fs/promises"; import "reflect-metadata"; import { AppDataSource } from "~database/datasource"; @@ -10,6 +11,12 @@ const router = new Bun.FileSystemRouter({ console.log("[+] Starting FediProject..."); const config = getConfig(); +const requests_log = Bun.file(process.cwd() + "/logs/requests.log"); + +if (!(await requests_log.exists())) { + console.log("[+] requests.log does not exist, creating it..."); + await Bun.write(process.cwd() + "/logs/requests.log", ""); +} if (!AppDataSource.isInitialized) await AppDataSource.initialize(); @@ -17,6 +24,38 @@ Bun.serve({ port: config.http.port, hostname: config.http.base_url || "0.0.0.0", // defaults to "0.0.0.0" async fetch(req) { + if (config.logging.log_requests_verbose) { + await appendFile( + `${process.cwd()}/logs/requests.log`, + `[${new Date().toISOString()}] ${req.method} ${ + req.url + }\n\tHeaders:\n` + ); + + // Add headers + + const headers = req.headers.entries(); + + for (const [key, value] of headers) { + await appendFile( + `${process.cwd()}/logs/requests.log`, + `\t\t${key}: ${value}\n` + ); + } + + const body = await req.clone().text(); + + await appendFile( + `${process.cwd()}/logs/requests.log`, + `\tBody:\n\t${body}\n` + ); + } else if (config.logging.log_requests) { + await appendFile( + process.cwd() + "/logs/requests.log", + `[${new Date().toISOString()}] ${req.method} ${req.url}\n` + ); + } + const matchedRoute = router.match(req); if (matchedRoute) { diff --git a/utils/config.ts b/utils/config.ts index 393291b6..577ae714 100644 --- a/utils/config.ts +++ b/utils/config.ts @@ -34,6 +34,20 @@ export interface ConfigType { activitypub: { use_tombstones: boolean; }; + + filters: { + note_filters: string[]; + username_filters: string[]; + displayname_filters: string[]; + bio_filters: string[]; + emoji_filters: string[]; + }; + + logging: { + log_requests: boolean; + log_requests_verbose: boolean; + log_filters: boolean; + }; [key: string]: unknown; } @@ -115,6 +129,18 @@ export const configDefaults: ConfigType = { activitypub: { use_tombstones: true, }, + filters: { + note_filters: [], + username_filters: [], + displayname_filters: [], + bio_filters: [], + emoji_filters: [], + }, + logging: { + log_requests: false, + log_requests_verbose: false, + log_filters: true, + }, }; export const getConfig = () => {