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,36 +24,7 @@ class ServerErrorReport extends Script {
".lines.json"
}
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")
}
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) => {
public reportError(path: string, queryParams: URLSearchParams, req: IncomingMessage, body: string | undefined, logDirectory: string) {
if (!body) {
throw "{\"error\": \"No body; use a post request\"}"
}
@ -65,16 +40,60 @@ class ServerErrorReport extends Script {
const file = this.getFilename(logDirectory, d)
const date = d.toISOString()
const contents =
"\n" + JSON.stringify({ ip, index: errorReport, date, message: body })
"\n" + JSON.stringify({ ip, index: this.errorReport, date, message: body })
if (!existsSync(file)) {
writeFileSync(file, contents)
} else {
appendFileSync(file, contents)
}
errorReport++
return `{"status":"ok", "nr": ${errorReport}}`
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)
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")
},
},
])
}
}