This commit is contained in:
jusax23 2023-03-17 23:48:01 +01:00
parent b91ad7e015
commit ea21a1c96d
Signed by: jusax23
GPG key ID: 499E2AA870C1CD41
6 changed files with 165 additions and 11 deletions

View file

@ -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",

View file

@ -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(

View file

@ -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<void>])[] = [
["users", async (req: Request, res: Response) => {
@ -50,7 +51,32 @@ var Methods: ([string, (req: Request, res: Response) => Promise<void>])[] = [
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) => {

103
src/html/log.ts Normal file
View file

@ -0,0 +1,103 @@
export default /*html*/ `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Outbag log</title>
<style>
body {
color: white;
background-color: black;
}
pre {
white-space: pre-wrap;
}
.date {
color: rgb(187, 187, 0)
}
.log {
font-weight: bold;
color: rgb(0, 187, 187)
}
.warn {
font-weight: bold;
color: rgb(0, 187, 187)
}
.debug {
font-weight: bold;
color: rgb(0, 187, 0)
}
.error {
font-weight: bold;
background-color: rgb(187, 0, 0)
}
.errorText {
background-color: rgb(187, 0, 0)
}
</style>
</head>
<body>
<pre></pre>
<script>
let pre = document.querySelector("pre");
let loaded = [];
function parse([id, type, date, name, args ]){
let dateSpan = document.createElement("span");
dateSpan.className = "date";
dateSpan.innerText = date+" ";
pre.appendChild(dateSpan);
let nameSpan = document.createElement("span");
nameSpan.className = type;
nameSpan.innerText = "["+name+"]: ";
pre.appendChild(nameSpan);
let argsSpan = document.createElement("span");
if(type=="error") argsSpan.className = "errorText";
argsSpan.innerText = args.map(a => typeof a == 'object' ? JSON.stringify(a, null, 2) : a).join(" ");
pre.appendChild(argsSpan);
pre.appendChild(document.createElement("br"));
}
async function refresh(){
let resp = await fetch("json");
if(resp.status != 200){
pre.innerText = "Outbag Log Api is disabled";
return;
}
let json = await resp.json();
let scrollPosition = document.documentElement.getBoundingClientRect().bottom;
let heigth = window.innerHeight;
let scroll = heigth + 50 >= scrollPosition;
json.forEach(d=>{
if(loaded.includes(d[0])) return;
loaded.push(d[0]);
parse(d);
});
if(scroll) document.documentElement.scrollIntoView(false);
}
setInterval(refresh, 1000);
</script>
</body>
</html>
`;

View file

@ -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 <path>", "Start the Outbag server with a config file.")
.option("-d, --debug", "Start the Outbag server with more log output.")
.option("-l, --log <number>", "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";
}

View file

@ -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;