diff --git a/gateway/src/events/Connection.ts b/gateway/src/events/Connection.ts
index c1bb73b6..508b4741 100644
--- a/gateway/src/events/Connection.ts
+++ b/gateway/src/events/Connection.ts
@@ -27,6 +27,21 @@ export async function Connection(
socket.on("close", Close);
// @ts-ignore
socket.on("message", Message);
+
+ if(process.env.WS_LOGEVENTS)
+ [
+ "close",
+ "error",
+ "upgrade",
+ //"message",
+ "open",
+ "ping",
+ "pong",
+ "unexpected-response"
+ ].forEach(x=>{
+ socket.on(x, y => console.log(x, y));
+ });
+
console.log(`[Gateway] Connections: ${this.clients.size}`);
const { searchParams } = new URL(`http://localhost${request.url}`);
diff --git a/gateway/src/events/Message.ts b/gateway/src/events/Message.ts
index 31328784..71b6f206 100644
--- a/gateway/src/events/Message.ts
+++ b/gateway/src/events/Message.ts
@@ -15,17 +15,33 @@ const PayloadSchema = {
$t: String,
};
-export async function Message(this: WebSocket, buffer: WS.Data) {
+export async function Message(this: WebSocket, buffer: WS.RawData) {
// TODO: compression
let data: Payload;
if (this.encoding === "etf" && buffer instanceof Buffer)
data = erlpack.unpack(buffer);
- else if (this.encoding === "json" && typeof buffer === "string")
- data = JSON.parse(buffer);
- else return;
+ else if (this.encoding === "json")
+ data = JSON.parse(buffer as unknown as string); //TODO: is this even correct?? seems to work for web clients...
+ else if(/--debug|--inspect/.test(process.execArgv.join(' '))) {
+ debugger;
+ return;
+ }
+ else {
+ console.log("Invalid gateway connection! Use a debugger to inspect!");
+ return;
+ }
- check.call(this, PayloadSchema, data);
+ if(process.env.WS_VERBOSE)
+ console.log(`[Websocket] Incomming message: ${JSON.stringify(data)}`);
+ if(data.op !== 1)
+ check.call(this, PayloadSchema, data);
+ else { //custom validation for numbers, because heartbeat
+ if(data.s || data.t || typeof data.d !== "number") {
+ console.log("Invalid heartbeat...");
+ this.close(CLOSECODES.Decode_error);
+ }
+ }
// @ts-ignore
const OPCodeHandler = OPCodeHandlers[data.op];
diff --git a/gateway/src/opcodes/Heartbeat.ts b/gateway/src/opcodes/Heartbeat.ts
index 50394130..42b72d4b 100644
--- a/gateway/src/opcodes/Heartbeat.ts
+++ b/gateway/src/opcodes/Heartbeat.ts
@@ -2,7 +2,7 @@ import { Payload, WebSocket } from "@fosscord/gateway";
import { setHeartbeat } from "../util/Heartbeat";
import { Send } from "../util/Send";
-export async function onHeartbeat(this: WebSocket, data: Payload) {
+export async function onHeartbeat(this: WebSocket, _data: Payload) {
// TODO: validate payload
setHeartbeat(this);
diff --git a/gateway/src/opcodes/Identify.ts b/gateway/src/opcodes/Identify.ts
index 13d55559..c6c24fcd 100644
--- a/gateway/src/opcodes/Identify.ts
+++ b/gateway/src/opcodes/Identify.ts
@@ -83,7 +83,7 @@ export async function onIdentify(this: WebSocket, data: Payload) {
// TODO: public user selection
}),
// save the session and delete it when the websocket is closed
- new Session({
+ Object.assign(new Session(),{
user_id: this.user_id,
session_id: session_id,
// TODO: check if status is only one of: online, dnd, offline, idle
diff --git a/gateway/src/opcodes/LazyRequest.ts b/gateway/src/opcodes/LazyRequest.ts
index 974769e9..0dbdb526 100644
--- a/gateway/src/opcodes/LazyRequest.ts
+++ b/gateway/src/opcodes/LazyRequest.ts
@@ -1,4 +1,4 @@
-import { getPermission, listenEvent, Member, Role } from "@fosscord/util";
+import { getPermission, listenEvent, Member, Role, initDatabase } from "@fosscord/util";
import { LazyRequest } from "../schema/LazyRequest";
import { Send } from "../util/Send";
import { OPCODES } from "../util/Constants";
@@ -15,8 +15,9 @@ async function getMembers(guild_id: string, range: [number, number]) {
throw new Error("range is not a valid array");
}
// TODO: wait for typeorm to implement ordering for .find queries https://github.com/typeorm/typeorm/issues/2620
+ // TODO: rewrite this, released in 0.3.0
- let members = await getRepository(Member)
+ let members = await (await initDatabase()).getRepository(Member)
.createQueryBuilder("member")
.where("member.guild_id = :guild_id", { guild_id })
.leftJoinAndSelect("member.roles", "role")
diff --git a/gateway/src/opcodes/VoiceStateUpdate.ts b/gateway/src/opcodes/VoiceStateUpdate.ts
index 7f7db9b0..1f7c2f1b 100644
--- a/gateway/src/opcodes/VoiceStateUpdate.ts
+++ b/gateway/src/opcodes/VoiceStateUpdate.ts
@@ -47,9 +47,9 @@ export async function onVoiceStateUpdate(this: WebSocket, data: Payload) {
//The event send by Discord's client on channel leave has both guild_id and channel_id as null
if (body.guild_id === null) body.guild_id = voiceState.guild_id;
- voiceState.assign(body);
+ voiceState = Object.assign(voiceState, body);
} catch (error) {
- voiceState = new VoiceState({
+ voiceState = Object.assign(new VoiceState(), {
...body,
user_id: this.user_id,
deaf: false,
diff --git a/gateway/src/util/Send.ts b/gateway/src/util/Send.ts
index dbe2e789..2a28d8e0 100644
--- a/gateway/src/util/Send.ts
+++ b/gateway/src/util/Send.ts
@@ -7,6 +7,8 @@ try {
import { Payload, WebSocket } from "@fosscord/gateway";
export async function Send(socket: WebSocket, data: Payload) {
+ if(process.env.WS_VERBOSE)
+ console.log(`[Websocket] Outgoing message: ${JSON.stringify(data)}`);
let buffer: Buffer | string;
if (socket.encoding === "etf") buffer = erlpack.pack(data);
// TODO: encode circular object
diff --git a/gateway/tsconfig.json b/gateway/tsconfig.json
index 70407c6b..9f855d33 100644
--- a/gateway/tsconfig.json
+++ b/gateway/tsconfig.json
@@ -18,7 +18,7 @@
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
"declaration": true /* Generates corresponding '.d.ts' file. */,
"declarationMap": false /* Generates a sourcemap for each corresponding '.d.ts' file. */,
- // "sourceMap": true /* Generates corresponding '.map' file. */,
+ "sourceMap": true /* Generates corresponding '.map' file. */,
// "outFile": "./", /* Concatenate and emit output to single file. */
"outDir": "./dist/" /* Redirect output structure to the directory. */,
"rootDir": "./src/" /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */,
|