From 7279df5efbb785e8353c65c793fbce41c4f86859 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Tue, 6 Apr 2021 14:15:13 +0200 Subject: :bug: fix hearbeat gateway --- src/events/Message.ts | 15 +++++++-------- src/opcodes/Heartbeat.ts | 4 ++-- src/opcodes/Resume.ts | 2 +- src/opcodes/instanceOf.ts | 1 + 4 files changed, 11 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/events/Message.ts b/src/events/Message.ts index 6aa62a22..2d461e1d 100644 --- a/src/events/Message.ts +++ b/src/events/Message.ts @@ -3,6 +3,7 @@ import erlpack from "erlpack"; import OPCodeHandlers from "../opcodes"; import { Payload, CLOSECODES } from "../util/Constants"; import { instanceOf, Tuple } from "lambert-server"; +import { check } from "../opcodes/instanceOf"; const PayloadSchema = { op: Number, @@ -15,14 +16,12 @@ export async function Message(this: WebSocket, buffer: Data) { // TODO: compression var data: Payload; - try { - if (this.encoding === "etf" && buffer instanceof Buffer) data = erlpack.unpack(buffer); - else if (this.encoding === "json" && typeof buffer === "string") data = JSON.parse(buffer); - const result = instanceOf(PayloadSchema, data); - if (result !== true) throw "invalid data"; - } catch (error) { - return this.close(CLOSECODES.Decode_error); - } + if (this.encoding === "etf" && buffer instanceof Buffer) data = erlpack.unpack(buffer); + else if (this.encoding === "json" && typeof buffer === "string") data = JSON.parse(buffer); + + check.call(this, PayloadSchema, data); + + console.log(data); // @ts-ignore const OPCodeHandler = OPCodeHandlers[data.op]; diff --git a/src/opcodes/Heartbeat.ts b/src/opcodes/Heartbeat.ts index 4c8df739..015257b9 100644 --- a/src/opcodes/Heartbeat.ts +++ b/src/opcodes/Heartbeat.ts @@ -3,10 +3,10 @@ import { Send } from "../util/Send"; import { setHeartbeat } from "../util/setHeartbeat"; import WebSocket from "../util/WebSocket"; -export function onHeartbeat(this: WebSocket, data: Payload) { +export async function onHeartbeat(this: WebSocket, data: Payload) { // TODO: validate payload setHeartbeat(this); - Send(this, { op: 11 }); + await Send(this, { op: 11 }); } diff --git a/src/opcodes/Resume.ts b/src/opcodes/Resume.ts index 8badbb35..3c54b5c7 100644 --- a/src/opcodes/Resume.ts +++ b/src/opcodes/Resume.ts @@ -3,5 +3,5 @@ import { CLOSECODES, Payload } from "../util/Constants"; import WebSocket from "../util/WebSocket"; export function onResume(this: WebSocket, data: Payload) { - return this.close(CLOSECODES.Session_timed_out); + return this.close(CLOSECODES.Invalid_session); } diff --git a/src/opcodes/instanceOf.ts b/src/opcodes/instanceOf.ts index 752ea2b6..4a34477f 100644 --- a/src/opcodes/instanceOf.ts +++ b/src/opcodes/instanceOf.ts @@ -6,6 +6,7 @@ export function check(this: WebSocket, schema: any, data: any) { try { if (instanceOf(schema, data) !== true) throw "invalid"; } catch (error) { + console.error(error); // invalid payload this.close(CLOSECODES.Decode_error); return false; -- cgit 1.5.1