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,36 +24,7 @@ class ServerErrorReport extends Script {
".lines.json" ".lines.json"
} }
async main(args: string[]): Promise<void> { public reportError(path: string, queryParams: URLSearchParams, req: IncomingMessage, body: string | undefined, logDirectory: string) {
const logDirectory = args[0] ?? "./error_logs"
console.log("Logging to directory", logDirectory)
if (!existsSync(logDirectory)) {
mkdirSync(logDirectory)
console.log("Created this directory")
}
let errorReport = 0
new Server(2348, {}, [
{
mustMatch: "status",
mimetype: "application/json",
handle: async () => {
const filename = this.getFilename(logDirectory, new Date())
let errorsToday = 0
if (existsSync(filename)) {
const contents = readFileSync(filename, "utf8")
errorsToday = contents.split("\n").length
}
return JSON.stringify({
"online": true,
"errors_today": errorsToday
})
}
},
<Handler>{
mustMatch: "report",
mimetype: "application/json",
handle: async (_, queryParams, req, body) => {
if (!body) { if (!body) {
throw "{\"error\": \"No body; use a post request\"}" throw "{\"error\": \"No body; use a post request\"}"
} }
@ -65,16 +40,60 @@ class ServerErrorReport extends Script {
const file = this.getFilename(logDirectory, d) const file = this.getFilename(logDirectory, d)
const date = d.toISOString() const date = d.toISOString()
const contents = const contents =
"\n" + JSON.stringify({ ip, index: errorReport, date, message: body }) "\n" + JSON.stringify({ ip, index: this.errorReport, date, message: body })
if (!existsSync(file)) { if (!existsSync(file)) {
writeFileSync(file, contents) writeFileSync(file, contents)
} else { } else {
appendFileSync(file, contents) appendFileSync(file, contents)
} }
errorReport++ this. errorReport++
return `{"status":"ok", "nr": ${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)
if (!existsSync(logDirectory)) {
mkdirSync(logDirectory)
console.log("Created this directory")
} }
if (!existsSync(logDirectory+"/csp")) {
mkdirSync(logDirectory+"/csp")
console.log("Created this directory")
}
new Server(2348, {}, [
{
mustMatch: "status",
mimetype: "application/json",
handle: async () => {
const filename = this.getFilename(logDirectory, new Date())
let errorsToday = 0
if (existsSync(filename)) {
const contents = readFileSync(filename, "utf8")
errorsToday = contents.split("\n").length
}
return JSON.stringify({
"online": true,
"errors_today": errorsToday,
})
},
},
{
mustMatch: "report",
mimetype: "application/json",
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")
},
},
]) ])
} }
} }