Skip to content

Commit ce80c73

Browse files
committed
Move media identify/resume to on socket open instead of when we first receive a hello packet
Something changed which causes us to never receive the ready packet if we identify/resume after a hello (on the media socket)
1 parent 4495cdd commit ce80c73

File tree

4 files changed

+32
-77
lines changed

4 files changed

+32
-77
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "detritus-client-socket",
3-
"version": "0.3.1",
3+
"version": "0.3.2",
44
"description": "A TypeScript NodeJS library to interact with Discord's Gateway",
55
"main": "lib/index.js",
66
"types": "lib/index.d.ts",

src/constants.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export const Package = Object.freeze({
22
URL: 'https://github.com/detritusjs/client-socket',
3-
VERSION: '0.3.1',
3+
VERSION: '0.3.2',
44
});
55

66
function normalize(object: {[key: string]: any}) {
@@ -206,7 +206,6 @@ export const MEDIA_CODECS_VIDEO = [
206206
];
207207

208208
export const MediaEncryptionModes = Object.freeze({
209-
PLAIN: 'plain',
210209
XSALSA20_POLY1305_LITE: 'xsalsa20_poly1305_lite',
211210
XSALSA20_POLY1305_SUFFIX: 'xsalsa20_poly1305_suffix',
212211
XSALSA20_POLY1305: 'xsalsa20_poly1305',

src/media.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -364,11 +364,6 @@ export class Socket extends EventEmitter {
364364
}; break;
365365
case MediaOpCodes.HELLO: {
366366
this.setHeartbeat(packet.d);
367-
if (this.identified && this.transport) {
368-
this.resume();
369-
} else {
370-
this.identify();
371-
}
372367
}; break;
373368
case MediaOpCodes.HEARTBEAT_ACK: {
374369
if (packet.d !== this._heartbeat.nonce) {
@@ -494,6 +489,11 @@ export class Socket extends EventEmitter {
494489
this.emit(SocketEvents.OPEN, target);
495490
if (this.socket === target) {
496491
this.setState(SocketStates.OPEN);
492+
if (this.identified && this.transport) {
493+
this.resume();
494+
} else {
495+
this.identify();
496+
}
497497
} else {
498498
target.close(SocketInternalCloseCodes.OTHER_SOCKET_OPEN);
499499
}

src/mediaudp.ts

Lines changed: 25 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -378,35 +378,24 @@ export class Socket extends EventEmitter {
378378
onPacket(packet: Buffer, from: UDPFrom): void {
379379
if (!this.receiveEnabled) {return;}
380380
if (from.address !== this.remote.ip || from.port !== this.remote.port) {
381-
this.emit(SocketEvents.WARN, new MediaPacketError(
382-
'Received a packet from an unknown IP/Port',
383-
from,
384-
packet,
385-
));
381+
const error = new MediaPacketError('Received a packet from an unknown IP/Port', from, packet);
382+
this.emit(SocketEvents.WARN, error);
386383
return;
387384
}
388385
if (!this.key) {
389-
this.emit(SocketEvents.WARN, new MediaPacketError(
390-
'Received a packet before the Session Description',
391-
from,
392-
packet,
393-
));
386+
const error = new MediaPacketError('Received a packet before the Session Description', from, packet);
387+
this.emit(SocketEvents.WARN, error);
394388
return;
395389
}
396390
if (packet.length <= 12) {
397-
this.emit(SocketEvents.WARN, new MediaPacketError(
398-
'Received an rtp packet that\'s way too small to be valid',
399-
from,
400-
packet,
401-
));
391+
const error = new MediaPacketError('Received an rtp packet that\'s way too small to be valid', from, packet);
392+
this.emit(SocketEvents.WARN, error);
402393
return;
403394
}
404395
if (!isValidRTPHeader(packet)) {
405-
this.emit(SocketEvents.WARN, new MediaPacketError(
406-
'Invalid RTP Packet',
407-
from,
408-
packet,
409-
));
396+
const error = new MediaPacketError('Invalid RTP Packet', from, packet);
397+
this.emit(SocketEvents.WARN, error);
398+
return;
410399
}
411400

412401
const packetType = packet.readUIntBE(1, 1);
@@ -427,12 +416,8 @@ export class Socket extends EventEmitter {
427416
}
428417
*/
429418
if (!RTP_PAYLOAD_TYPES.includes(payloadType)) {
430-
this.emit(SocketEvents.WARN, new MediaRTPError(
431-
'Unknown RTP Packet Payload Type',
432-
from,
433-
packet,
434-
rtp,
435-
));
419+
const error = new MediaRTPError('Unknown RTP Packet Payload Type', from, packet, rtp);
420+
this.emit(SocketEvents.WARN, error);
436421
return;
437422
}
438423

@@ -458,22 +443,13 @@ export class Socket extends EventEmitter {
458443
}
459444

460445
if (format === MediaCodecTypes.VIDEO && !this.videoEnabled) {
461-
this.emit(SocketEvents.LOG, new MediaRTPError(
462-
'Dropping video packet due to video not being enabled',
463-
from,
464-
packet,
465-
rtp,
466-
));
446+
const error = new MediaRTPError('Dropping video packet due to video not being enabled', from, packet, rtp);
447+
this.emit(SocketEvents.LOG, error);
467448
return;
468449
}
469450

470451
rtp.nonce = Buffer.alloc(24);
471452
switch (this.mode) {
472-
case MediaEncryptionModes.PLAIN: {
473-
// I assume theres no nonce?
474-
// only included cuz the docs have it in the examples lol
475-
rtp.payload = packet.slice(12);
476-
}; break;
477453
case MediaEncryptionModes.XSALSA20_POLY1305_LITE: {
478454
// last 4 bytes
479455
packet.copy(rtp.nonce, 0, packet.length - 4);
@@ -491,33 +467,20 @@ export class Socket extends EventEmitter {
491467
rtp.payload = packet.slice(12);
492468
}; break;
493469
default: {
494-
this.emit(SocketEvents.WARN, new MediaRTPError(
495-
`${this.mode} is not supported for decoding.`,
496-
from,
497-
packet,
498-
rtp,
499-
));
470+
const error = new MediaRTPError(`${this.mode} is not supported for decoding.`, from, packet, rtp);
471+
this.emit(SocketEvents.WARN, error);
500472
return;
501473
};
502474
}
503475

504-
let data: Buffer | null = null;
505-
if (this.mode === MediaEncryptionModes.PLAIN) {
506-
data = rtp.payload;
507-
} else {
508-
data = RTPCrypto.decrypt(
509-
<Uint8Array> this.key,
510-
<Buffer> rtp.payload,
511-
<Buffer> rtp.nonce,
512-
);
513-
}
514-
if (data === null) {
515-
this.emit(SocketEvents.WARN, new MediaRTPError(
516-
'Packet failed to decrypt',
517-
from,
518-
packet,
519-
rtp,
520-
));
476+
let data: Buffer | null = RTPCrypto.decrypt(
477+
<Uint8Array> this.key,
478+
<Buffer> rtp.payload,
479+
<Buffer> rtp.nonce,
480+
);
481+
if (!data) {
482+
const error = new MediaRTPError('Packet failed to decrypt', from, packet, rtp);
483+
this.emit(SocketEvents.WARN, error);
521484
return;
522485
}
523486

@@ -567,12 +530,8 @@ export class Socket extends EventEmitter {
567530
// using two bytes, 0x10 and 0x00 instead
568531
// if appbits is all 0s, ignore, so rn ignore this packet
569532

570-
this.emit(SocketEvents.LOG, new MediaRTPError(
571-
'Received Two Byte header with appbits being 0, ignoring',
572-
from,
573-
packet,
574-
rtp,
575-
));
533+
const error = new MediaRTPError('Received Two Byte header with appbits being 0, ignoring', from, packet, rtp);
534+
this.emit(SocketEvents.LOG, error);
576535
return;
577536
/*
578537
// handle the two byte
@@ -749,9 +708,6 @@ export class Socket extends EventEmitter {
749708

750709
let nonce: Buffer;
751710
switch (this.mode) {
752-
case MediaEncryptionModes.PLAIN: {
753-
nonce = Buffer.alloc(0);
754-
}; break;
755711
case MediaEncryptionModes.XSALSA20_POLY1305_LITE: {
756712
if (!useCache && options.nonce === undefined) {
757713
throw new Error(`You must use cache if you do not send in an incrementing nonce with the Encryption mode being ${this.mode}`);

0 commit comments

Comments
 (0)