diff --git a/src/gateway/events/Connection.ts b/src/gateway/events/Connection.ts
index 41b2ff3d..fff5aacf 100644
--- a/src/gateway/events/Connection.ts
+++ b/src/gateway/events/Connection.ts
@@ -1,5 +1,5 @@
import WS from "ws";
-import { WebSocket } from "@fosscord/gateway";
+import { genSessionId, WebSocket } from "@fosscord/gateway";
import { Send } from "../util/Send";
import { CLOSECODES, OPCODES } from "../util/Constants";
import { setHeartbeat } from "../util/Heartbeat";
@@ -30,6 +30,10 @@ export async function Connection(
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 {
// @ts-ignore
socket.on("close", Close);
diff --git a/src/gateway/events/Message.ts b/src/gateway/events/Message.ts
index 6645dd22..97bbba83 100644
--- a/src/gateway/events/Message.ts
+++ b/src/gateway/events/Message.ts
@@ -5,6 +5,8 @@ import WS from "ws";
import { PayloadSchema } from "@fosscord/util";
import * as Sentry from "@sentry/node";
import BigIntJson from "json-bigint";
+import path from "path";
+import fs from "fs/promises";
const bigIntJson = BigIntJson({ storeAsString: true });
var erlpack: any;
@@ -41,6 +43,21 @@ export async function Message(this: WebSocket, buffer: WS.Data) {
if (process.env.WS_VERBOSE)
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);
// @ts-ignore
diff --git a/src/gateway/opcodes/Identify.ts b/src/gateway/opcodes/Identify.ts
index 29b17643..1c3cab28 100644
--- a/src/gateway/opcodes/Identify.ts
+++ b/src/gateway/opcodes/Identify.ts
@@ -55,9 +55,7 @@ export async function onIdentify(this: WebSocket, data: Payload) {
return this.close(CLOSECODES.Authentication_failed);
}
this.user_id = decoded.id;
-
- const session_id = genSessionId();
- this.session_id = session_id; //Set the session of the WebSocket object
+ let session_id = this.session_id;
const [user, read_states, members, recipients, session, application] =
await Promise.all([
diff --git a/src/gateway/util/Send.ts b/src/gateway/util/Send.ts
index 1c0f33c3..c31233c8 100644
--- a/src/gateway/util/Send.ts
+++ b/src/gateway/util/Send.ts
@@ -7,8 +7,27 @@ try {
);
}
import { Payload, WebSocket } from "@fosscord/gateway";
+import fs from "fs/promises";
+import path from "path";
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;
if (socket.encoding === "etf") buffer = erlpack.pack(data);
// TODO: encode circular object
|