🐛 fix hearbeat gateway

This commit is contained in:
Flam3rboy 2021-04-06 14:15:13 +02:00
parent 509394b592
commit 7279df5efb
6 changed files with 12 additions and 12 deletions

BIN
package-lock.json generated

Binary file not shown.

View File

@ -17,7 +17,7 @@
"fosscord-server-util": "github:fosscord/fosscord-server-util",
"jsonwebtoken": "^8.5.1",
"lambert-db": "^1.1.8",
"lambert-server": "^1.1.6",
"lambert-server": "^1.1.7",
"missing-native-js-functions": "^1.2.3",
"node-fetch": "^2.6.1",
"uuid": "^8.3.2",

View File

@ -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];

View File

@ -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 });
}

View File

@ -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);
}

View File

@ -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;