From afa2c3025f2997855614ce7081a6088c23e0a1ac Mon Sep 17 00:00:00 2001 From: xzeldon Date: Sat, 9 Dec 2023 12:57:43 +0300 Subject: [PATCH] fix(bedrock.js): resolve UNCONNECTED_PING formation issue - Simplify UNCONNECTED_PING function - Address an issue where certain servers, particularly those based on Pocketmine, were unresponsive to Unconnected Ping requests --- lib/bedrock.js | 40 +++++----------------------------------- 1 file changed, 5 insertions(+), 35 deletions(-) diff --git a/lib/bedrock.js b/lib/bedrock.js index 204a6ed..f100e75 100644 --- a/lib/bedrock.js +++ b/lib/bedrock.js @@ -12,37 +12,6 @@ import dgram from 'dgram'; const START_TIME = new Date().getTime(); -/** - * Creates a buffer with the specified length. - * @param {number} length - The length of the buffer. - * @returns {Buffer} - The created buffer. - */ -const createBuffer = (length) => { - const buffer = Buffer.alloc(length); - buffer[0] = 0x01; - return buffer; -}; - -/** - * Writes a BigInt value to the buffer at the specified offset using big-endian byte order. - * @param {Buffer} buffer - The buffer to write to. - * @param {number} value - The BigInt value to write. - * @param {number} offset - The offset in the buffer to write the value. - */ -const writeBigInt64BE = (buffer, value, offset) => { - buffer.writeBigInt64BE(BigInt(value), offset); -}; - -/** - * Copies the specified hex value to the buffer at the specified offset. - * @param {Buffer} buffer - The buffer to copy to. - * @param {string} hex - The hex value to copy. - * @param {number} offset - The offset in the buffer to copy the value. - */ -const copyHexToBuffer = (buffer, hex, offset) => { - Buffer.from(hex, 'hex').copy(buffer, offset); -}; - /** * Reads a BigInt value from the buffer at the specified offset using big-endian byte order. * @param {Buffer} buffer - The buffer to read from. @@ -90,10 +59,11 @@ const parseAdvertiseString = (advertiseStr) => { * @see {@link https://wiki.vg/Raknet_Protocol#Unconnected_Ping} */ const UNCONNECTED_PING = (pingId) => { - const buffer = createBuffer(35); - writeBigInt64BE(buffer, pingId, 1); - copyHexToBuffer(buffer, '00ffff00fefefefefdfdfdfd12345678', 9); - writeBigInt64BE(buffer, 0, 25); + const buffer = Buffer.alloc(33); + buffer.writeUInt8(0x01, 0); + buffer.writeBigInt64LE(BigInt(pingId), 1); + Buffer.from("00ffff00fefefefefdfdfdfd12345678", "hex").copy(buffer, 9); + buffer.writeBigInt64LE(BigInt(0), 25); return buffer; };