Add CSP endpoint

This commit is contained in:
Pieter Vander Vennet 2024-07-19 20:43:46 +02:00
parent 1853af06a0
commit 9a517bd4f8
2 changed files with 52 additions and 32 deletions

View file

@ -7,7 +7,8 @@ export interface Handler {
handle: ( handle: (
path: string, path: string,
queryParams: URLSearchParams, queryParams: URLSearchParams,
req: http.IncomingMessage req: http.IncomingMessage,
body: string | undefined
) => Promise<string> ) => Promise<string>
} }
@ -101,7 +102,7 @@ export class Server {
res.end() res.end()
return return
} }
let body = undefined let body: string | undefined = undefined
if (req.method === "POST" || req.method === "UPDATE") { if (req.method === "POST" || req.method === "UPDATE") {
body = await ServerUtils.getBody(req) body = await ServerUtils.getBody(req)
} }

View file

@ -3,8 +3,12 @@ import Script from "./Script"
import { appendFileSync, existsSync, mkdirSync, readFileSync, writeFileSync } from "fs" import { appendFileSync, existsSync, mkdirSync, readFileSync, writeFileSync } from "fs"
import { mkdir } from "node:fs" import { mkdir } from "node:fs"
import ScriptUtils from "./ScriptUtils" import ScriptUtils from "./ScriptUtils"
import { IncomingMessage } from "node:http"
class ServerErrorReport extends Script { class ServerErrorReport extends Script {
private errorReport = 0
constructor() { constructor() {
super("A server which receives and logs error reports") super("A server which receives and logs error reports")
} }
@ -20,6 +24,32 @@ class ServerErrorReport extends Script {
".lines.json" ".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 = <string>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<void> { async main(args: string[]): Promise<void> {
const logDirectory = args[0] ?? "./error_logs" const logDirectory = args[0] ?? "./error_logs"
console.log("Logging to directory", logDirectory) console.log("Logging to directory", logDirectory)
@ -28,7 +58,11 @@ class ServerErrorReport extends Script {
console.log("Created this directory") console.log("Created this directory")
} }
let errorReport = 0 if (!existsSync(logDirectory+"/csp")) {
mkdirSync(logDirectory+"/csp")
console.log("Created this directory")
}
new Server(2348, {}, [ new Server(2348, {}, [
{ {
mustMatch: "status", mustMatch: "status",
@ -42,39 +76,24 @@ class ServerErrorReport extends Script {
} }
return JSON.stringify({ return JSON.stringify({
"online": true, "online": true,
"errors_today": errorsToday "errors_today": errorsToday,
}) })
} },
}, },
<Handler>{ {
mustMatch: "report", mustMatch: "report",
mimetype: "application/json", mimetype: "application/json",
handle: async (_, queryParams, req, body) => { handle(path: string, queryParams: URLSearchParams, req: IncomingMessage, body: string | undefined) {
if (!body) { return this.reportError(queryParams, req, body, logDirectory)
throw "{\"error\": \"No body; use a post request\"}" },
} },
console.log(body) {
const ip = <string>req.headers["x-forwarded-for"] mustMatch: "csp",
mimetype: "application/json",
try { handle(path: string, queryParams: URLSearchParams, req: IncomingMessage, body: string | undefined) {
body = JSON.parse(body) return this.reportError(queryParams, req, body, logDirectory+"/csp")
} 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}}`
}
}
]) ])
} }
} }