2024-06-20 14:22:33 +02:00
|
|
|
import { Handler, Server } from "./server"
|
|
|
|
import Script from "./Script"
|
2024-07-14 03:10:10 +02:00
|
|
|
import { appendFileSync, existsSync, mkdirSync, readFileSync, writeFileSync } from "fs"
|
2024-06-20 14:22:33 +02:00
|
|
|
import { mkdir } from "node:fs"
|
|
|
|
import ScriptUtils from "./ScriptUtils"
|
2024-07-19 20:43:46 +02:00
|
|
|
import { IncomingMessage } from "node:http"
|
2024-06-20 14:22:33 +02:00
|
|
|
|
|
|
|
class ServerErrorReport extends Script {
|
2024-07-19 20:43:46 +02:00
|
|
|
private errorReport = 0
|
|
|
|
|
2024-06-20 14:22:33 +02:00
|
|
|
constructor() {
|
|
|
|
super("A server which receives and logs error reports")
|
|
|
|
}
|
|
|
|
|
2024-07-14 03:10:10 +02:00
|
|
|
private getFilename(logDirectory: string, d: Date): string {
|
2024-07-21 10:52:51 +02:00
|
|
|
return (
|
|
|
|
logDirectory +
|
2024-07-14 03:10:10 +02:00
|
|
|
"/" +
|
|
|
|
d.getUTCFullYear() +
|
|
|
|
"_" +
|
|
|
|
(d.getUTCMonth() + 1) +
|
|
|
|
"_" +
|
|
|
|
d.getUTCDate() +
|
|
|
|
".lines.json"
|
2024-07-21 10:52:51 +02:00
|
|
|
)
|
2024-07-14 03:10:10 +02:00
|
|
|
}
|
|
|
|
|
2024-07-21 10:52:51 +02:00
|
|
|
public reportError(
|
|
|
|
path: string,
|
|
|
|
queryParams: URLSearchParams,
|
|
|
|
req: IncomingMessage,
|
|
|
|
body: string | undefined,
|
|
|
|
logDirectory: string
|
|
|
|
): string {
|
2024-07-19 20:43:46 +02:00
|
|
|
if (!body) {
|
2024-07-21 10:52:51 +02:00
|
|
|
throw '{"error": "No body; use a post request"}'
|
2024-07-19 20:43:46 +02:00
|
|
|
}
|
|
|
|
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()
|
2024-07-21 10:52:51 +02:00
|
|
|
const contents = "\n" + JSON.stringify({ ip, index: this.errorReport, date, message: body })
|
2024-07-19 20:43:46 +02:00
|
|
|
if (!existsSync(file)) {
|
|
|
|
writeFileSync(file, contents)
|
|
|
|
} else {
|
|
|
|
appendFileSync(file, contents)
|
|
|
|
}
|
2024-07-21 10:52:51 +02:00
|
|
|
this.errorReport++
|
2024-07-19 20:43:46 +02:00
|
|
|
return `{"status":"ok", "nr": ${this.errorReport}}`
|
|
|
|
}
|
|
|
|
|
2024-06-20 14:22:33 +02:00
|
|
|
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")
|
|
|
|
}
|
2024-07-14 03:10:10 +02:00
|
|
|
|
2024-07-21 10:52:51 +02:00
|
|
|
if (!existsSync(logDirectory + "/csp")) {
|
|
|
|
mkdirSync(logDirectory + "/csp")
|
2024-07-19 20:43:46 +02:00
|
|
|
console.log("Created this directory")
|
|
|
|
}
|
|
|
|
|
2024-06-24 13:11:35 +02:00
|
|
|
new Server(2348, {}, [
|
2024-07-14 03:10:10 +02:00
|
|
|
{
|
|
|
|
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({
|
2024-07-21 10:52:51 +02:00
|
|
|
online: true,
|
|
|
|
errors_today: errorsToday,
|
2024-07-14 03:10:10 +02:00
|
|
|
})
|
2024-07-19 20:43:46 +02:00
|
|
|
},
|
2024-07-14 03:10:10 +02:00
|
|
|
},
|
2024-07-19 20:43:46 +02:00
|
|
|
{
|
2024-06-20 14:22:33 +02:00
|
|
|
mustMatch: "report",
|
|
|
|
mimetype: "application/json",
|
2024-07-21 10:52:51 +02:00
|
|
|
handle: async (
|
|
|
|
path: string,
|
|
|
|
queryParams: URLSearchParams,
|
|
|
|
req: IncomingMessage,
|
|
|
|
body: string | undefined
|
|
|
|
) => {
|
2024-07-20 16:30:39 +02:00
|
|
|
return this.reportError(path, queryParams, req, body, logDirectory)
|
2024-07-19 20:43:46 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
mustMatch: "csp",
|
|
|
|
mimetype: "application/json",
|
2024-07-21 10:52:51 +02:00
|
|
|
handle: async (
|
|
|
|
path: string,
|
|
|
|
queryParams: URLSearchParams,
|
|
|
|
req: IncomingMessage,
|
|
|
|
body: string | undefined
|
|
|
|
) => {
|
|
|
|
return this.reportError(path, queryParams, req, body, logDirectory + "/csp")
|
2024-07-19 20:43:46 +02:00
|
|
|
},
|
|
|
|
},
|
2024-06-24 13:11:35 +02:00
|
|
|
])
|
2024-06-20 14:22:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
new ServerErrorReport().run()
|