Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev/post refactor fixes #927

Merged
merged 11 commits into from
Jan 12, 2023
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ build
*.log.ansi
*.tmp
tmp/
dump/
3 changes: 3 additions & 0 deletions src/api/middlewares/Authentication.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NextFunction, Request, Response } from "express";
import { HTTPError } from "lambert-server";
import { checkToken, Config, Rights } from "@fosscord/util";
import * as Sentry from "@sentry/node";

export const NO_AUTHORIZATION_ROUTES = [
// Authentication routes
Expand Down Expand Up @@ -63,6 +64,8 @@ export async function Authentication(
if (!req.headers.authorization)
return next(new HTTPError("Missing Authorization Header", 401));

Sentry.setUser({ id: req.user_id });
MaddyUnderStars marked this conversation as resolved.
Show resolved Hide resolved

try {
const { jwtSecret } = Config.get().security;

Expand Down
6 changes: 5 additions & 1 deletion src/gateway/events/Connection.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -30,6 +30,10 @@ export async function Connection(

socket.ipAddress = ipAddress;


const session_id = genSessionId();
socket.session_id = session_id; //Set the session of the WebSocket object
MaddyUnderStars marked this conversation as resolved.
Show resolved Hide resolved

try {
// @ts-ignore
socket.on("close", Close);
Expand Down
29 changes: 21 additions & 8 deletions src/gateway/events/Message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ 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";
const bigIntJson = BigIntJson({ storeAsString: true });

var erlpack: any;
try {
erlpack = require("@yukikaze-bot/erlpack");
} catch (error) {}
} catch (error) { }

export async function Message(this: WebSocket, buffer: WS.Data) {
// TODO: compression
Expand Down Expand Up @@ -40,6 +42,17 @@ 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) {
if (this.session_id) {
fs.mkdirSync(path.join("dump", this.session_id), { recursive: true });
fs.writeFileSync(path.join("dump", this.session_id, `${Date.now()}.in.json`), JSON.stringify(data, null, 2));
}
else {
fs.mkdirSync(path.join("dump", "unknown"), { recursive: true });
fs.writeFileSync(path.join("dump", "unknown", `${Date.now()}.in.json`), JSON.stringify(data, null, 2));
console.log("Unknown session id, dumping to unknown folder");
}
}
MaddyUnderStars marked this conversation as resolved.
Show resolved Hide resolved

check.call(this, PayloadSchema, data);

Expand All @@ -55,13 +68,13 @@ export async function Message(this: WebSocket, buffer: WS.Data) {
const transaction =
data.op != 1
? Sentry.startTransaction({
op: OPCODES[data.op],
name: `GATEWAY ${OPCODES[data.op]}`,
data: {
...data.d,
token: data?.d?.token ? "[Redacted]" : undefined,
},
})
op: OPCODES[data.op],
name: `GATEWAY ${OPCODES[data.op]}`,
data: {
...data.d,
token: data?.d?.token ? "[Redacted]" : undefined,
},
})
: undefined;

try {
Expand Down
4 changes: 1 addition & 3 deletions src/gateway/opcodes/Identify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down
15 changes: 15 additions & 0 deletions src/gateway/util/Send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,23 @@ try {
);
}
import { Payload, WebSocket } from "@fosscord/gateway";
import fs from "fs";
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) {
if(socket.session_id) {
fs.mkdirSync(path.join("dump", socket.session_id), { recursive: true });
fs.writeFileSync(path.join("dump", socket.session_id, `${Date.now()}.out.json`), JSON.stringify(data, null, 2));
}
else {
fs.mkdirSync(path.join("dump", "unknown"), { recursive: true });
fs.writeFileSync(path.join("dump", "unknown", `${Date.now()}.out.json`), JSON.stringify(data, null, 2));
MaddyUnderStars marked this conversation as resolved.
Show resolved Hide resolved
console.log("Unknown session ID, dumping to unknown folder!");
}

}
let buffer: Buffer | string;
if (socket.encoding === "etf") buffer = erlpack.pack(data);
// TODO: encode circular object
Expand Down