Add all BENNC message types with unit tests
This commit is contained in:
85
src/messages/userDataRequest.ts
Normal file
85
src/messages/userDataRequest.ts
Normal file
@@ -0,0 +1,85 @@
|
||||
import Color from 'color'
|
||||
import { decrypt, encrypt } from 'romulus-js'
|
||||
import { DEFAULT_KEY, MessageTypes } from '../common'
|
||||
import { numberToUint16BE } from '../utilities/number'
|
||||
import { SmartBuffer } from '../utilities/smart-buffer'
|
||||
import { packOutgoingPacket } from './packet'
|
||||
|
||||
const MESSAGE_TYPE = numberToUint16BE(MessageTypes.UserDataRequest)
|
||||
|
||||
export interface UserDataRequestMessage {
|
||||
username: string
|
||||
colour: Color
|
||||
clientId: string
|
||||
success?: boolean
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an outgoing user data request (0x0002) packet.
|
||||
* @param properties The properties for the message.
|
||||
* @param key The key to encrypt the data with.
|
||||
* @returns An outgoing user data request (0x0002) packet.
|
||||
*/
|
||||
export function packUserDataRequestMessage (properties: UserDataRequestMessage, key: Buffer = DEFAULT_KEY): Buffer {
|
||||
// Prepare data in correct format.
|
||||
const username = Buffer.from(properties.username, 'utf-8')
|
||||
const usernameLength = numberToUint16BE(username.length)
|
||||
const colour = Buffer.from(properties.colour.array())
|
||||
const clientId = Buffer.from(properties.clientId, 'utf-8')
|
||||
const clientIdLength = numberToUint16BE(clientId.length)
|
||||
|
||||
// Pack data.
|
||||
const packedData = Buffer.concat([
|
||||
usernameLength,
|
||||
username,
|
||||
colour,
|
||||
clientIdLength,
|
||||
clientId
|
||||
])
|
||||
|
||||
// Encrypt the data.
|
||||
const data = encrypt(packedData, MESSAGE_TYPE, key)
|
||||
|
||||
return packOutgoingPacket({
|
||||
messageType: MESSAGE_TYPE,
|
||||
data: data
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Unpack the data section of an incoming user data request (0x0002) message
|
||||
* @param data The data section of an incoming user data request (0x0002) message
|
||||
* @param key The key to decrypt the data with.
|
||||
* @returns An unpacked user data request (0x0002) message
|
||||
*/
|
||||
export function unpackUserDataRequestMessage (data: Buffer, key: Buffer = DEFAULT_KEY): UserDataRequestMessage {
|
||||
// Decrypt the incoming data.
|
||||
const message = decrypt(data, MESSAGE_TYPE, key)
|
||||
|
||||
// Guard to check if decryption was successful.
|
||||
if (!message.success) {
|
||||
return {
|
||||
username: '',
|
||||
colour: Color('black'),
|
||||
clientId: '',
|
||||
success: false
|
||||
}
|
||||
}
|
||||
|
||||
// Unpack and read data in correct format.
|
||||
const packedData = SmartBuffer.from(message.plaintext)
|
||||
|
||||
const usernameLength = packedData.readUInt16()
|
||||
const username = packedData.readBytes(usernameLength)
|
||||
const colour = packedData.readBytes(3)
|
||||
const clientIdLength = packedData.readUInt16()
|
||||
const clientId = packedData.readBytes(clientIdLength)
|
||||
|
||||
// Return data in correct format.
|
||||
return {
|
||||
username: username.toString(),
|
||||
colour: Color.rgb(colour),
|
||||
clientId: clientId.toString(),
|
||||
success: message.success
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user