From ea21a1c96dd0c04034c10b54a26f341536db08ba Mon Sep 17 00:00:00 2001 From: jusax23 Date: Fri, 17 Mar 2023 23:48:01 +0100 Subject: [PATCH] log api --- package.json | 2 +- src/api/acts/rooms.ts | 2 +- src/api/get.ts | 30 +++++++++++- src/html/log.ts | 103 ++++++++++++++++++++++++++++++++++++++++++ src/main.ts | 6 ++- src/sys/log.ts | 33 +++++++++++--- 6 files changed, 165 insertions(+), 11 deletions(-) create mode 100644 src/html/log.ts diff --git a/package.json b/package.json index 58ff144..24415ef 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "scripts": { "prepublish": "tsc", "main": "tsc && node . -c config.juml", - "debug": "tsc && node . -c config.juml -d", + "debug": "tsc && node . -c config.juml -d -l 256", "setup": "tsc && node . -c config.juml -s", "bundleRelease": "mkdir build/bundle & esbuild src/main.ts --platform=node --bundle --minify --outfile=build/bundle/main.js", "build-linux-x64": "mkdir build/bin & pkg -t node18-linux-x64 -o build/bin/outbag-linux-x64 build/bundle/main.js", diff --git a/src/api/acts/rooms.ts b/src/api/acts/rooms.ts index ae9a3d2..4d78b31 100644 --- a/src/api/acts/rooms.ts +++ b/src/api/acts/rooms.ts @@ -106,7 +106,7 @@ export const joinRoom: Act = { await remove(roomOTAs) .where(eq(roomOTAs.usesLeft, 0)) .query(db); - if (req[1].affectedRows == 0) { + if (req.affectedRows == 0) { return void aws("error", "ota"); } let queryx = await insert( diff --git a/src/api/get.ts b/src/api/get.ts index 3db0cd5..c3b7371 100644 --- a/src/api/get.ts +++ b/src/api/get.ts @@ -3,8 +3,9 @@ import express, { Request, Response } from "express"; import { suspectRequest } from "../sys/bruteforce.js"; import { oConf } from "../sys/config.js"; import { accounts, db, rooms } from "../sys/db.js"; -import { error } from "../sys/log.js"; +import { error, logList } from "../sys/log.js"; import { getSettings, SETTINGS } from "../sys/settings.js"; +import logHTML from "../html/log.js"; var Methods: ([string, (req: Request, res: Response) => Promise])[] = [ ["users", async (req: Request, res: Response) => { @@ -50,7 +51,32 @@ var Methods: ([string, (req: Request, res: Response) => Promise])[] = [ return null; }); res.json(out.filter(d => d != null)); - }] + }], + ["log/json", async (req: Request, res: Response) => { + if (global.provideLog == 0) return void res.status(500).send("disabled"); + res.json(logList); + }], + ["log/ansi", async (req: Request, res: Response) => { + if (global.provideLog == 0) return void res.status(500).send("disabled"); + res.send(logList.map(([id, type, date, name, args]) => { + let out = `\x1b[33m${date}\x1b[0m\x1b[1m`; + if (type == "debug") { + out += `\x1b[1m\x1b[32m [${name}]: \x1b[0m`; + } else if (type == "log") { + out += `\x1b[1m\x1b[36m [${name}]: \x1b[0m`; + } else if (type == "warn") { + out += `\x1b[1m\x1b[36m [${name}]: \x1b[0m`; + } else { + out += ` \x1b[1m\x1b[41m[${name}]: \x1b[0m\x1b[41m`; + } + out += args.map(a => `${typeof a == 'object' ? JSON.stringify(a) : a}`).join(" "); + out += "\x1b[0m"; + return out; + }).join("\x1b[0m\n")); + }], + ["log/html", async (req: Request, res: Response) => { + res.send(logHTML); + }], ]; export const addGetMethods = (server: express.Express) => { diff --git a/src/html/log.ts b/src/html/log.ts new file mode 100644 index 0000000..c7f5cd8 --- /dev/null +++ b/src/html/log.ts @@ -0,0 +1,103 @@ +export default /*html*/ ` + + + + + + + + Outbag log + + + + +

+    
+
+
+
+`;
\ No newline at end of file
diff --git a/src/main.ts b/src/main.ts
index 2731ea2..1ac080a 100644
--- a/src/main.ts
+++ b/src/main.ts
@@ -1,5 +1,6 @@
 declare global {
     var debug: boolean;
+    var provideLog: number;
 }
 import express from "express";
 import https from "https";
@@ -20,6 +21,7 @@ import { activateWS, wsOnConnection } from "./api/ws.js";
 import { startUpdateCert } from "./server/serverCerts.js";
 import { wait } from "./sys/tools.js";
 
+global.provideLog = 10;
 
 const config = {
     version: "0.0.1"
@@ -34,10 +36,12 @@ program
 program
     .option("-c, --config ", "Start the Outbag server with a config file.")
     .option("-d, --debug", "Start the Outbag server with more log output.")
+    .option("-l, --log ", "Amount of log entires provided in get api! -1 = inifinity")
     .option("-s, --setup", "Start the Server Setup Tool")
-    .action(({ config, debug, setup }) => {
+    .action(({ config, debug, log: provideLog, setup }) => {
         let dofullSetup = false;
         global.debug = debug != null;
+        global.provideLog = provideLog != null ? provideLog : 0;
         if (global.debug) {
             process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = "0";
         }
diff --git a/src/sys/log.ts b/src/sys/log.ts
index 2d15479..a682758 100644
--- a/src/sys/log.ts
+++ b/src/sys/log.ts
@@ -1,48 +1,69 @@
+import { addShutdownTask } from "nman";
+
+export const logList: ([number, "debug" | "log" | "warn" | "error", string, string, any[]])[] = [
+    //[id, "type", "Date", "Titel", ...data]
+];
+let logID = 0;
+
 export const debug = (name: string, ...args: any[]) => {
     if (!global.debug) return;
     args = consorArgs(args);
+    let dateString = (new Date()).toLocaleString();
     console.log(
         "\x1b[33m%s\x1b[0m" +
         "\x1b[1m\x1b[32m%s\x1b[0m",
-        (new Date()).toLocaleString(),
+        dateString,
         ` [${name}]:`,
         ...args
     );
+    if (global.provideLog != 0) logList.push([logID++, "debug", dateString, name, args]);
 };
 
 export const log = (name: string, ...args: string[]) => {
     args = consorArgs(args);
+    let dateString = (new Date()).toLocaleString();
     console.log(
         "\x1b[33m%s\x1b[0m" +
         "\x1b[1m\x1b[36m%s\x1b[0m",
-        (new Date()).toLocaleString(),
+        dateString,
         ` [${name}]:`,
         ...args
     );
+    if (global.provideLog != 0) logList.push([logID++, "log", dateString, name, args]);
 };
 
 export const warn = (name: string, ...args: any[]) => {
     args = consorArgs(args);
+    let dateString = (new Date()).toLocaleString();
     console.warn(
         "\x1b[33m%s\x1b[0m" +
         "\x1b[1m\x1b[36m%s\x1b[0m",
-        (new Date()).toLocaleString(),
+        dateString,
         ` [${name}]:`,
         ...args
     );
+    if (global.provideLog != 0) logList.push([logID++, "warn", dateString, name, args]);
 };
 
 export const error = (name: string, ...args: any[]) => {
+    let dateString = (new Date()).toLocaleString();
     console.error(
         "\x1b[33m%s\x1b[0m" +
         "\x1b[1m\x1b[41m%s\x1b[0m\x1b[41m",
-        (new Date()).toLocaleString() + " ",
+        dateString + " ",
         `[${name}]:`,
         ...args,
         "\x1b[0m"
     );
+    if (global.provideLog != 0) logList.push([logID++, "error", dateString, name, args]);
 };
 
+let clearLogID = setInterval(() => {
+    if (global.provideLog >= -1)
+        logList.splice(0, logList.length - global.provideLog)
+}, 60000);
+addShutdownTask(()=>clearInterval(clearLogID));
+
 const consorArgs = (args: any[]) => {
     let out = [];
     for (let i = 0; i < args.length; i++) {
@@ -52,10 +73,10 @@ const consorArgs = (args: any[]) => {
     return out;
 }
 const censorLogArg = (arg: any) => {
-    if (typeof arg != "object" || arg == null) return arg;
+    if (typeof arg != "object" || arg == null || arg instanceof Error) return arg;
     let out: any = {};
     for (let key in arg) {
-        if (key == "accountKey") out[key] = new Array(arg[key].length).fill("*").join("");
+        if ((["accountKey", "sign", "publicKey", "token"]).includes(key)) out[key] = new Array(Math.min(arg[key].length, 50)).fill("*").join("");
         else out[key] = censorLogArg(arg[key]);
     }
     return out;