Fix outbound user data request/response

This commit is contained in:
Jack Hadrill 2022-03-18 22:31:50 +00:00
parent 0b19b83271
commit 3a163df991
5 changed files with 11 additions and 6 deletions

View File

@ -33,9 +33,11 @@ export function packUserDataRequestMessage (properties: UserDataRequestMessage,
const packedData = new SmartBuffer() const packedData = new SmartBuffer()
packedData.writeBytes(usernameLength) packedData.writeBytes(usernameLength)
packedData.writeBytes(username) packedData.writeBytes(username)
packedData.pad(32 - username.length)
packedData.writeBytes(colour) packedData.writeBytes(colour)
packedData.writeBytes(clientIdLength) packedData.writeBytes(clientIdLength)
packedData.writeBytes(clientId) packedData.writeBytes(clientId)
packedData.pad(32 - clientId.length)
// Encrypt the data. // Encrypt the data.
const data = encrypt(packedData.data, MESSAGE_TYPE, key) const data = encrypt(packedData.data, MESSAGE_TYPE, key)

View File

@ -33,9 +33,11 @@ export function packUserDataResponseMessage (properties: UserDataResponseMessage
const packedData = new SmartBuffer() const packedData = new SmartBuffer()
packedData.writeBytes(usernameLength) packedData.writeBytes(usernameLength)
packedData.writeBytes(username) packedData.writeBytes(username)
packedData.pad(32 - username.length)
packedData.writeBytes(colour) packedData.writeBytes(colour)
packedData.writeBytes(clientIdLength) packedData.writeBytes(clientIdLength)
packedData.writeBytes(clientId) packedData.writeBytes(clientId)
packedData.pad(32 - clientId.length)
// Return encrypted data. // Return encrypted data.
const data = encrypt(packedData.data, MESSAGE_TYPE, key) const data = encrypt(packedData.data, MESSAGE_TYPE, key)

View File

@ -79,7 +79,8 @@ export class SmartBuffer {
* @param length The number of bytes to pad. * @param length The number of bytes to pad.
*/ */
pad (length: number): void { pad (length: number): void {
this._data.push(...Array<number>(length)) this._data.push(...Array<number>(length).fill(0))
this.cursor += length
} }
/** /**
@ -100,7 +101,7 @@ export class SmartBuffer {
*/ */
splice (start: number, deleteCount: number, ...items: number[]): void { splice (start: number, deleteCount: number, ...items: number[]): void {
if (this.length < start) { if (this.length < start) {
this.pad(start) this._data.push(...Array<number>(start))
} }
this._data.splice(this.cursor, deleteCount, ...items) this._data.splice(this.cursor, deleteCount, ...items)
} }

View File

@ -23,10 +23,10 @@ test('Create a user data request (0x0002) packet.', () => {
// Then // Then
// We can't check the contents of the data as it's encrypted with a random nonce. // We can't check the contents of the data as it's encrypted with a random nonce.
// Check the message type and length. // Check the message type and length.
expect(packedPacket.slice(0, 4)).toMatchObject(new Uint8Array([0x00, 0x02, 0x00, 0x3A])) expect(packedPacket.slice(0, 4)).toMatchObject(new Uint8Array([0x00, 0x02, 0x00, 0x67]))
// Check the total length is as expected. // Check the total length is as expected.
expect(packedPacket.length).toBe(62) expect(packedPacket.length).toBe(107)
}) })
test('Parse a user data request (0x0002).', () => { test('Parse a user data request (0x0002).', () => {

View File

@ -23,10 +23,10 @@ test('Create a user data response (0x0003) packet.', () => {
// Then // Then
// We can't check the contents of the data as it's encrypted with a random nonce. // We can't check the contents of the data as it's encrypted with a random nonce.
// Check the message type and length. // Check the message type and length.
expect(packedPacket.slice(0, 4)).toMatchObject(new Uint8Array([0x00, 0x03, 0x00, 0x3A])) expect(packedPacket.slice(0, 4)).toMatchObject(new Uint8Array([0x00, 0x03, 0x00, 0x67]))
// Check the total length is as expected. // Check the total length is as expected.
expect(packedPacket.length).toBe(62) expect(packedPacket.length).toBe(107)
}) })
test('Parse a user data response (0x0003).', () => { test('Parse a user data response (0x0003).', () => {