2022-01-10 20:21:47 +00:00
|
|
|
/**
|
|
|
|
* Implementation of the RakNet ping/pong protocol.
|
2024-03-30 22:56:03 +00:00
|
|
|
* @see https://wiki.vg/Raknet_Protocol
|
2022-01-10 20:21:47 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
import dgram from 'dgram';
|
|
|
|
|
2024-03-30 22:56:03 +00:00
|
|
|
const MAGIC = "00ffff00fefefefefdfdfdfd12345678";
|
2022-01-10 20:21:47 +00:00
|
|
|
const START_TIME = new Date().getTime();
|
|
|
|
|
2023-10-21 23:29:20 +00:00
|
|
|
/**
|
2024-03-30 22:56:03 +00:00
|
|
|
* Creates an Unconnected Ping packet.
|
|
|
|
* @param {number} pingId
|
|
|
|
* @returns {Buffer}
|
2023-10-21 23:29:20 +00:00
|
|
|
* @see {@link https://wiki.vg/Raknet_Protocol#Unconnected_Ping}
|
2022-01-10 20:21:47 +00:00
|
|
|
*/
|
2024-03-30 22:56:03 +00:00
|
|
|
const createUnconnectedPingFrame = (pingId) => {
|
2023-12-09 09:57:43 +00:00
|
|
|
const buffer = Buffer.alloc(33);
|
|
|
|
buffer.writeUInt8(0x01, 0);
|
|
|
|
buffer.writeBigInt64LE(BigInt(pingId), 1);
|
2024-03-30 22:56:03 +00:00
|
|
|
Buffer.from(MAGIC, "hex").copy(buffer, 9);
|
2023-12-09 09:57:43 +00:00
|
|
|
buffer.writeBigInt64LE(BigInt(0), 25);
|
2023-10-21 23:29:20 +00:00
|
|
|
return buffer;
|
2022-01-10 20:21:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2024-03-30 22:56:03 +00:00
|
|
|
* Extract Modt from Unconnected Pong Packet and convert to an object
|
|
|
|
* @param {Buffer} unconnectedPongPacket
|
|
|
|
* @returns {Object}
|
2023-10-21 23:29:20 +00:00
|
|
|
* @see {@link https://wiki.vg/Raknet_Protocol#Unconnected_Pong}
|
2022-01-10 20:21:47 +00:00
|
|
|
*/
|
2024-03-30 22:56:03 +00:00
|
|
|
const extractModt = (unconnectedPongPacket) => {
|
|
|
|
// Skip everything to Modt
|
|
|
|
const offset = 33;
|
|
|
|
const length = unconnectedPongPacket.readUInt16BE(offset);
|
|
|
|
let modt = unconnectedPongPacket.toString("utf-8", offset + 2, offset + 2 + length);
|
|
|
|
|
|
|
|
const components = modt.split(';');
|
|
|
|
const parsedComponents = {
|
|
|
|
edition: components[0],
|
|
|
|
name: components[1],
|
|
|
|
version: {
|
|
|
|
protocolVersion: Number(components[2]),
|
|
|
|
minecraftVersion: components[3],
|
|
|
|
},
|
|
|
|
players: {
|
|
|
|
online: Number(components[4]),
|
|
|
|
max: Number(components[5])
|
|
|
|
},
|
|
|
|
serverId: components[6],
|
|
|
|
mapName: components[7],
|
|
|
|
gameMode: components[8]
|
|
|
|
};
|
2022-01-10 20:21:47 +00:00
|
|
|
|
2024-03-30 22:56:03 +00:00
|
|
|
return parsedComponents;
|
2022-01-10 20:21:47 +00:00
|
|
|
};
|
|
|
|
|
2023-10-21 23:29:20 +00:00
|
|
|
/**
|
|
|
|
* Sends a ping request to the specified host and port.
|
|
|
|
* @param {string} host - The IP address or hostname of the server.
|
|
|
|
* @param {number} [port=19132] - The port number.
|
|
|
|
* @param {function} cb - The callback function to handle the response.
|
|
|
|
* @param {number} [timeout=5000] - The timeout duration in milliseconds.
|
|
|
|
*/
|
|
|
|
const ping = (host, port = 19132, cb, timeout = 5000) => {
|
2022-01-10 20:21:47 +00:00
|
|
|
const socket = dgram.createSocket('udp4');
|
|
|
|
|
2023-10-22 18:32:00 +00:00
|
|
|
// Set manual timeout interval.
|
|
|
|
// This ensures the connection will NEVER hang regardless of internal state
|
2022-01-10 20:21:47 +00:00
|
|
|
const timeoutTask = setTimeout(() => {
|
|
|
|
socket.emit('error', new Error('Socket timeout'));
|
|
|
|
}, timeout);
|
|
|
|
|
|
|
|
const closeSocket = () => {
|
2023-10-22 18:32:00 +00:00
|
|
|
socket.close();
|
|
|
|
clearTimeout(timeoutTask);
|
2022-01-10 20:21:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Generic error handler
|
|
|
|
// This protects multiple error callbacks given the complex socket state
|
|
|
|
// This is mostly dangerous since it can swallow errors
|
|
|
|
let didFireError = false;
|
|
|
|
|
2023-10-22 18:32:00 +00:00
|
|
|
/**
|
|
|
|
* Handle any error that occurs during the ping process.
|
|
|
|
* @param {Error} err The error that occurred.
|
|
|
|
*/
|
2022-01-10 20:21:47 +00:00
|
|
|
const handleError = (err) => {
|
|
|
|
closeSocket();
|
|
|
|
|
|
|
|
if (!didFireError) {
|
|
|
|
didFireError = true;
|
|
|
|
cb(null, err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
2024-03-30 22:56:03 +00:00
|
|
|
const ping = createUnconnectedPingFrame(new Date().getTime() - START_TIME);
|
2023-10-21 23:29:20 +00:00
|
|
|
socket.send(ping, 0, ping.length, port, host);
|
2022-01-10 20:21:47 +00:00
|
|
|
} catch (err) {
|
|
|
|
handleError(err);
|
|
|
|
}
|
|
|
|
|
2024-03-30 22:56:03 +00:00
|
|
|
socket.on('message', (pongPacket) => {
|
|
|
|
const id = pongPacket[0];
|
2022-01-10 20:21:47 +00:00
|
|
|
|
|
|
|
switch (id) {
|
|
|
|
case 0x1c: {
|
2024-03-30 22:56:03 +00:00
|
|
|
const modtObject = extractModt(pongPacket);
|
2022-01-10 20:21:47 +00:00
|
|
|
closeSocket();
|
2024-03-30 22:56:03 +00:00
|
|
|
cb(modtObject, null);
|
2022-01-10 20:21:47 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default: {
|
|
|
|
handleError(new Error('Received unexpected packet'));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-10-22 18:32:00 +00:00
|
|
|
socket.on('error', handleError);
|
2023-10-21 23:29:20 +00:00
|
|
|
};
|
2022-01-10 20:21:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Asynchronously ping Minecraft Bedrock server.
|
|
|
|
* The optional `options` argument can be an object with a `ping` (default is `19132`) or/and `timeout` (default is `5000`) property.
|
|
|
|
* @param {string} host The Bedrock server address.
|
2023-10-22 18:32:00 +00:00
|
|
|
* @param {import('../types/index.js').PingOptions} options The configuration for pinging Minecraft Bedrock server.
|
|
|
|
* @returns {Promise<import('../types/index.js').BedrockPingResponse>}
|
2022-01-10 20:21:47 +00:00
|
|
|
*/
|
2023-10-21 23:29:20 +00:00
|
|
|
export const pingBedrock = (host, options = {}) => {
|
2022-01-10 20:21:47 +00:00
|
|
|
if (!host) throw new Error('Host argument is not provided');
|
|
|
|
|
|
|
|
const { port = 19132, timeout = 5000 } = options;
|
|
|
|
|
2023-10-08 20:22:18 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
ping(host, port, (res, err) => {
|
|
|
|
err ? reject(err) : resolve(res);
|
2022-01-10 20:21:47 +00:00
|
|
|
}, timeout);
|
|
|
|
});
|
2023-10-21 23:29:20 +00:00
|
|
|
};
|