advanced parsing of doom

This commit is contained in:
Flummi 2019-11-19 04:11:45 +01:00
parent 09bb65a4d2
commit 4247b8e82d

View File

@ -30,18 +30,15 @@ export default class slack extends EventEmitter {
} }
async connect() { async connect() {
const res = await (await fetch(`${this.api}/rtm.start?token=${this.token}`)).json(); const res = await (await fetch(`${this.api}/rtm.start?token=${this.token}`)).json();
res.channels.forEach(channel => {
this.server.channel.set(channel.id, channel.name); res.channels.forEach(channel => this.server.channel.set(channel.id, channel.name));
}); res.users.forEach(user => this.server.user.set(user.id, {
res.users.forEach(user => {
this.server.user.set(user.id, {
account: user.name, account: user.name,
nickname: user.real_name nickname: user.real_name
}); }));
});
if (!res.ok) if (!res.ok)
throw this.emit("data", [ "error", res.description ]); // more infos return this.emit("data", [ "error", res.description ]); // more infos
this.server.wss.url = url.parse(res.url); this.server.wss.url = url.parse(res.url);
@ -63,8 +60,22 @@ export default class slack extends EventEmitter {
this.server.wss.socket.on("data", async data => { this.server.wss.socket.on("data", async data => {
try { try {
data = data.toString("utf-8").replace(/\0/g, ""); if(data.length < 2)
data = JSON.parse(data.substr(data.indexOf("{"))); throw "payload too short";
if(data[1] & 0x80)
throw "we no accept masked data";
let offset = 2;
let length = data[1] & 0x7F;
if(length === 126) {
offset = 4;
length = data.readUInt16BE(2);
}
else if(length === 127)
throw "payload too long";
if(data.length < length + offset)
throw "payload shorter than given length";
//console.log(data, offset, length);
data = JSON.parse(data.slice(offset, length + offset).toString().replace(/\0+$/, "")); // trim null bytes at the end
//console.log(data, data.type); //console.log(data, data.type);
@ -81,10 +92,6 @@ export default class slack extends EventEmitter {
}) })
.on("end", () => this.emit("data", [ "debug", "stream ended" ])) .on("end", () => this.emit("data", [ "debug", "stream ended" ]))
.on("error", err => this.emit("data", [ "error", err ])); .on("error", err => this.emit("data", [ "error", err ]));
/*.on("drain", console.log)
.on("close", console.log)
.on("finish", console.log)
.on("timeout", console.log)*/
}); });
} }