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: (
path: string,
queryParams: URLSearchParams,
req: http.IncomingMessage
req: http.IncomingMessage,
body: string | undefined
) => Promise<string>
}
@ -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)
}

View file

@ -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 = <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> {
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,
})
}
},
},
<Handler>{
{
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 = <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: 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")
},
},
])
}
}