From 9a517bd4f8329d31a4ece880d05f9053a21491b7 Mon Sep 17 00:00:00 2001 From: Pieter Vander Vennet Date: Fri, 19 Jul 2024 20:43:46 +0200 Subject: [PATCH] Add CSP endpoint --- scripts/server.ts | 5 ++- scripts/serverErrorReport.ts | 79 ++++++++++++++++++++++-------------- 2 files changed, 52 insertions(+), 32 deletions(-) diff --git a/scripts/server.ts b/scripts/server.ts index 879bd7685..d60419ea8 100644 --- a/scripts/server.ts +++ b/scripts/server.ts @@ -7,7 +7,8 @@ export interface Handler { handle: ( path: string, queryParams: URLSearchParams, - req: http.IncomingMessage + req: http.IncomingMessage, + body: string | undefined ) => Promise } @@ -101,7 +102,7 @@ export class Server { res.end() return } - let body = undefined + let body: string | undefined = undefined if (req.method === "POST" || req.method === "UPDATE") { body = await ServerUtils.getBody(req) } diff --git a/scripts/serverErrorReport.ts b/scripts/serverErrorReport.ts index 1701783a7..a9e845e4d 100644 --- a/scripts/serverErrorReport.ts +++ b/scripts/serverErrorReport.ts @@ -3,8 +3,12 @@ import Script from "./Script" import { appendFileSync, existsSync, mkdirSync, readFileSync, writeFileSync } from "fs" import { mkdir } from "node:fs" import ScriptUtils from "./ScriptUtils" +import { IncomingMessage } from "node:http" class ServerErrorReport extends Script { + + private errorReport = 0 + constructor() { super("A server which receives and logs error reports") } @@ -20,6 +24,32 @@ class ServerErrorReport extends Script { ".lines.json" } + public reportError(path: string, queryParams: URLSearchParams, req: IncomingMessage, body: string | undefined, logDirectory: string) { + if (!body) { + throw "{\"error\": \"No body; use a post request\"}" + } + console.log(body) + const ip = req.headers["x-forwarded-for"] + + try { + body = JSON.parse(body) + } catch (e) { + // could not parse, we'll save it as is + } + const d = new Date() + const file = this.getFilename(logDirectory, d) + const date = d.toISOString() + const contents = + "\n" + JSON.stringify({ ip, index: this.errorReport, date, message: body }) + if (!existsSync(file)) { + writeFileSync(file, contents) + } else { + appendFileSync(file, contents) + } + this. errorReport++ + return `{"status":"ok", "nr": ${this.errorReport}}` + } + async main(args: string[]): Promise { const logDirectory = args[0] ?? "./error_logs" console.log("Logging to directory", logDirectory) @@ -28,7 +58,11 @@ class ServerErrorReport extends Script { console.log("Created this directory") } - let errorReport = 0 + if (!existsSync(logDirectory+"/csp")) { + mkdirSync(logDirectory+"/csp") + console.log("Created this directory") + } + new Server(2348, {}, [ { mustMatch: "status", @@ -42,39 +76,24 @@ class ServerErrorReport extends Script { } return JSON.stringify({ "online": true, - "errors_today": errorsToday + "errors_today": errorsToday, }) - } + }, }, - { + { mustMatch: "report", mimetype: "application/json", - handle: async (_, queryParams, req, body) => { - if (!body) { - throw "{\"error\": \"No body; use a post request\"}" - } - console.log(body) - const ip = req.headers["x-forwarded-for"] - - try { - body = JSON.parse(body) - } catch (e) { - // could not parse, we'll save it as is - } - const d = new Date() - const file = this.getFilename(logDirectory, d) - const date = d.toISOString() - const contents = - "\n" + JSON.stringify({ ip, index: errorReport, date, message: body }) - if (!existsSync(file)) { - writeFileSync(file, contents) - } else { - appendFileSync(file, contents) - } - errorReport++ - return `{"status":"ok", "nr": ${errorReport}}` - } - } + handle(path: string, queryParams: URLSearchParams, req: IncomingMessage, body: string | undefined) { + return this.reportError(queryParams, req, body, logDirectory) + }, + }, + { + mustMatch: "csp", + mimetype: "application/json", + handle(path: string, queryParams: URLSearchParams, req: IncomingMessage, body: string | undefined) { + return this.reportError(queryParams, req, body, logDirectory+"/csp") + }, + }, ]) } }