voice: fix voice receive after enabling suffix and lite encryption modes

This commit is contained in:
Amish Shah
2018-06-13 20:43:18 +01:00
parent 14eadb3ce0
commit d69e906027
2 changed files with 17 additions and 5 deletions

View File

@@ -248,7 +248,7 @@ class StreamDispatcher extends Writable {
packetBuffer.writeUIntBE(this.player.voiceConnection.authentication.ssrc, 8, 4);
packetBuffer.copy(nonce, 0, 0, 12);
return Buffer.concat([packetBuffer, ...this._encrypt(buffer, sequence)]);
return Buffer.concat([packetBuffer, ...this._encrypt(buffer)]);
}
_sendPacket(packet) {

View File

@@ -1,4 +1,3 @@
const nonce = Buffer.alloc(24);
const secretbox = require('../util/Secretbox');
const EventEmitter = require('events');
@@ -7,6 +6,7 @@ class Readable extends require('stream').Readable { _read() {} } // eslint-disab
class PacketHandler extends EventEmitter {
constructor(receiver) {
super();
this.nonce = Buffer.alloc(24);
this.receiver = receiver;
this.streams = new Map();
}
@@ -27,10 +27,22 @@ class PacketHandler extends EventEmitter {
}
parseBuffer(buffer) {
// Reuse nonce buffer
buffer.copy(nonce, 0, 0, 12);
const { secretKey, encryptionMode } = this.receiver.connection.authentication;
let packet = secretbox.methods.open(buffer.slice(12), nonce, this.receiver.connection.authentication.secretKey);
// Choose correct nonce depending on encryption
let end;
if (encryptionMode === 'xsalsa20_poly1305_lite') {
buffer.copy(this.nonce, 0, buffer.length - 4);
end = buffer.length - 4;
} else if (encryptionMode === 'xsalsa20_poly1305_suffix') {
buffer.copy(this.nonce, 0, buffer.length - 24);
end = buffer.length - 24;
} else {
buffer.copy(this.nonce, 0, 0, 12);
}
// Open packet
let packet = secretbox.methods.open(buffer.slice(12, end), this.nonce, secretKey);
if (!packet) return new Error('Failed to decrypt voice packet');
packet = Buffer.from(packet);