1
0
mirror of https://github.com/spacebarchat/server.git synced 2024-11-22 02:12:40 +01:00

Dev/post refactor fixes (#927)

* Re-introduce outgoing message logging

Signed-off-by: TheArcaneBrony <myrainbowdash949@gmail.com>

* Websocket dumping

* Sentry user count on API

* Generate session ID upon opening websocket, fix gateway dumps

* Async file io in src/gateway/events/Message.ts

Signed-off-by: TheArcaneBrony <myrainbowdash949@gmail.com>

* Async file io in src/util/util/Config.ts

Signed-off-by: TheArcaneBrony <myrainbowdash949@gmail.com>

* Make pre-commit hook executable

Signed-off-by: TheArcaneBrony <myrainbowdash949@gmail.com>

* Fixed sync file io in src/util/util/Config.ts

Signed-off-by: TheArcaneBrony <myrainbowdash949@gmail.com>

* Fixed missing await call in src/util/util/AutoUpdate.ts

Signed-off-by: TheArcaneBrony <myrainbowdash949@gmail.com>

* Add comment to src/gateway/events/Connection.ts

Signed-off-by: TheArcaneBrony <myrainbowdash949@gmail.com>

* Clean up gateway dumping code

Signed-off-by: TheArcaneBrony <myrainbowdash949@gmail.com>
Co-authored-by: Madeline <46743919+MaddyUnderStars@users.noreply.github.com>
This commit is contained in:
TheArcaneBrony 2023-01-12 13:46:36 +01:00 committed by GitHub
parent da9ce34933
commit 6122374e4d
9 changed files with 55 additions and 9 deletions

1
.gitignore vendored
View File

@ -17,3 +17,4 @@ build
*.log.ansi *.log.ansi
*.tmp *.tmp
tmp/ tmp/
dump/

0
.husky/pre-commit Normal file → Executable file
View File

View File

@ -1,6 +1,7 @@
import { NextFunction, Request, Response } from "express"; import { NextFunction, Request, Response } from "express";
import { HTTPError } from "lambert-server"; import { HTTPError } from "lambert-server";
import { checkToken, Config, Rights } from "@fosscord/util"; import { checkToken, Config, Rights } from "@fosscord/util";
import * as Sentry from "@sentry/node";
export const NO_AUTHORIZATION_ROUTES = [ export const NO_AUTHORIZATION_ROUTES = [
// Authentication routes // Authentication routes
@ -63,6 +64,8 @@ export async function Authentication(
if (!req.headers.authorization) if (!req.headers.authorization)
return next(new HTTPError("Missing Authorization Header", 401)); return next(new HTTPError("Missing Authorization Header", 401));
Sentry.setUser({ id: req.user_id });
try { try {
const { jwtSecret } = Config.get().security; const { jwtSecret } = Config.get().security;

View File

@ -1,5 +1,5 @@
import WS from "ws"; import WS from "ws";
import { WebSocket } from "@fosscord/gateway"; import { genSessionId, WebSocket } from "@fosscord/gateway";
import { Send } from "../util/Send"; import { Send } from "../util/Send";
import { CLOSECODES, OPCODES } from "../util/Constants"; import { CLOSECODES, OPCODES } from "../util/Constants";
import { setHeartbeat } from "../util/Heartbeat"; import { setHeartbeat } from "../util/Heartbeat";
@ -30,6 +30,10 @@ export async function Connection(
socket.ipAddress = ipAddress; socket.ipAddress = ipAddress;
//Create session ID when the connection is opened. This allows gateway dump to group the initial websocket messages with the rest of the conversation.
const session_id = genSessionId();
socket.session_id = session_id; //Set the session of the WebSocket object
try { try {
// @ts-ignore // @ts-ignore
socket.on("close", Close); socket.on("close", Close);

View File

@ -5,6 +5,8 @@ import WS from "ws";
import { PayloadSchema } from "@fosscord/util"; import { PayloadSchema } from "@fosscord/util";
import * as Sentry from "@sentry/node"; import * as Sentry from "@sentry/node";
import BigIntJson from "json-bigint"; import BigIntJson from "json-bigint";
import path from "path";
import fs from "fs/promises";
const bigIntJson = BigIntJson({ storeAsString: true }); const bigIntJson = BigIntJson({ storeAsString: true });
var erlpack: any; var erlpack: any;
@ -41,6 +43,21 @@ export async function Message(this: WebSocket, buffer: WS.Data) {
if (process.env.WS_VERBOSE) if (process.env.WS_VERBOSE)
console.log(`[Websocket] Incomming message: ${JSON.stringify(data)}`); console.log(`[Websocket] Incomming message: ${JSON.stringify(data)}`);
if (process.env.WS_DUMP) {
const id = this.session_id || "unknown";
await fs.mkdir(path.join("dump", this.session_id), { recursive: true });
await fs.writeFile(
path.join("dump", this.session_id, `${Date.now()}.in.json`),
JSON.stringify(data, null, 2),
);
if (!this.session_id)
console.log(
"[Gateway] Unknown session id, dumping to unknown folder",
);
}
check.call(this, PayloadSchema, data); check.call(this, PayloadSchema, data);
// @ts-ignore // @ts-ignore

View File

@ -55,9 +55,7 @@ export async function onIdentify(this: WebSocket, data: Payload) {
return this.close(CLOSECODES.Authentication_failed); return this.close(CLOSECODES.Authentication_failed);
} }
this.user_id = decoded.id; this.user_id = decoded.id;
let session_id = this.session_id;
const session_id = genSessionId();
this.session_id = session_id; //Set the session of the WebSocket object
const [user, read_states, members, recipients, session, application] = const [user, read_states, members, recipients, session, application] =
await Promise.all([ await Promise.all([

View File

@ -7,8 +7,27 @@ try {
); );
} }
import { Payload, WebSocket } from "@fosscord/gateway"; import { Payload, WebSocket } from "@fosscord/gateway";
import fs from "fs/promises";
import path from "path";
export function Send(socket: WebSocket, data: Payload) { export function Send(socket: WebSocket, data: Payload) {
if (process.env.WS_VERBOSE)
console.log(`[Websocket] Outgoing message: ${JSON.stringify(data)}`);
if (process.env.WS_DUMP) {
const id = socket.session_id || "unknown";
(async () => {
await fs.mkdir(path.join("dump", id), {
recursive: true,
});
await fs.writeFile(
path.join("dump", id, `${Date.now()}.out.json`),
JSON.stringify(data, null, 2),
);
})();
}
let buffer: Buffer | string; let buffer: Buffer | string;
if (socket.encoding === "etf") buffer = erlpack.pack(data); if (socket.encoding === "etf") buffer = erlpack.pack(data);
// TODO: encode circular object // TODO: encode circular object

View File

@ -60,7 +60,7 @@ async function download(url: string, dir: string) {
const response = await fetch(url, { agent }); const response = await fetch(url, { agent });
const buffer = await response.buffer(); const buffer = await response.buffer();
const tempDir = await fs.mkdtemp("fosscord"); const tempDir = await fs.mkdtemp("fosscord");
fs.writeFile(path.join(tempDir, "Fosscord.zip"), buffer); await fs.writeFile(path.join(tempDir, "Fosscord.zip"), buffer);
} catch (error) { } catch (error) {
console.error(`[Auto Update] download failed`, error); console.error(`[Auto Update] download failed`, error);
} }

View File

@ -1,5 +1,6 @@
import { ConfigEntity } from "../entities/Config"; import { ConfigEntity } from "../entities/Config";
import fs from "fs"; import fs from "fs/promises";
import syncFs from "fs";
import { ConfigValue } from "../config"; import { ConfigValue } from "../config";
// TODO: yaml instead of json // TODO: yaml instead of json
@ -31,11 +32,14 @@ export const Config = {
); );
try { try {
const overrideConfig = JSON.parse( const overrideConfig = JSON.parse(
fs.readFileSync(overridePath, { encoding: "utf8" }), await fs.readFile(overridePath, { encoding: "utf8" }),
); );
config = overrideConfig.merge(config); config = overrideConfig.merge(config);
} catch (error) { } catch (error) {
fs.writeFileSync(overridePath, JSON.stringify(config, null, 4)); await fs.writeFile(
overridePath,
JSON.stringify(config, null, 4),
);
} }
} }
@ -79,7 +83,7 @@ function applyConfig(val: ConfigValue) {
} }
if (process.env.CONFIG_PATH) if (process.env.CONFIG_PATH)
fs.writeFileSync(overridePath, JSON.stringify(val, null, 4)); syncFs.writeFileSync(overridePath, JSON.stringify(val, null, 4));
return apply(val); return apply(val);
} }