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
This commit is contained in:
Timofey Gelazoniya 2023-12-09 12:57:43 +03:00
parent 6c297d0b8c
commit afa2c3025f
Signed by: zeldon
GPG Key ID: 047886915281DD2A

View File

@ -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;
};