mirror of
https://github.com/minescope/mineping.git
synced 2025-07-13 14:54:35 +03:00
feat: implement SRV record lookup and improve ping logic
Add support for DNS SRV record lookups (`_minecraft._tcp`) to automatically resolve the correct server host and port, falling back to the provided address if no record is found. This makes server discovery compliant with standard Minecraft client behavior. The Java ping implementation has been refactored: - The `varint` module is rewritten to throw specific error codes and its `decodeVarInt` function now returns bytes read, which simplifies parsing logic. - The core ping logic is now promise-based and modular, breaking out packet creation and response processing into helper functions. - The TCP stream handler now robustly processes chunked data by catching recoverable decoder errors and waiting for more data, preventing crashes on incomplete packets. - Error handling is improved.
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import varint from "../lib/varint.js";
|
||||
import * as varint from "../lib/varint.js";
|
||||
|
||||
describe("varint.js", () => {
|
||||
it("should encode and decode integers symmetrically (round-trip)", () => {
|
||||
@ -16,8 +16,8 @@ describe("varint.js", () => {
|
||||
];
|
||||
|
||||
testValues.forEach((value) => {
|
||||
const encoded = varint.encodeInt(value);
|
||||
const decoded = varint.decodeInt(encoded, 0);
|
||||
const encoded = varint.encodeVarInt(value);
|
||||
const { value: decoded } = varint.decodeVarInt(encoded, 0);
|
||||
expect(decoded, `Value ${value} failed round-trip`).toBe(value);
|
||||
});
|
||||
});
|
||||
@ -25,52 +25,37 @@ describe("varint.js", () => {
|
||||
it("should decode an integer from a non-zero offset", () => {
|
||||
// [255 (invalid varint), 128 (valid varint), 127 (valid varint)]
|
||||
const buffer = Buffer.from([0xff, 0x80, 0x01, 0x7f]);
|
||||
expect(varint.decodeInt(buffer, 1)).toBe(128);
|
||||
const { value: decoded } = varint.decodeVarInt(buffer, 1);
|
||||
expect(decoded).toBe(128);
|
||||
});
|
||||
|
||||
it("should throw an error for a malformed varint that is too long", () => {
|
||||
const invalidBuffer = Buffer.from([0x80, 0x80, 0x80, 0x80, 0x80, 0x80]);
|
||||
expect(() => varint.decodeInt(invalidBuffer, 0)).toThrow(
|
||||
"VarInt is too big"
|
||||
expect(() => varint.decodeVarInt(invalidBuffer, 0)).toThrow(
|
||||
"VarInt is too big or malformed"
|
||||
);
|
||||
});
|
||||
|
||||
it("should correctly predict the encoded length of a varint", () => {
|
||||
const boundaries = [0, 127, 128, 16383, 16384, 2097151, 2097152];
|
||||
boundaries.forEach((value) => {
|
||||
const predictedLength = varint.decodeLength(value);
|
||||
const actualLength = varint.encodeInt(value).length;
|
||||
expect(predictedLength).toBe(actualLength);
|
||||
});
|
||||
});
|
||||
|
||||
it("should encode 16-bit unsigned shorts in big-endian format", () => {
|
||||
expect(varint.encodeUShort(0)).toEqual(Buffer.from([0x00, 0x00]));
|
||||
expect(varint.encodeUShort(256)).toEqual(Buffer.from([0x01, 0x00]));
|
||||
expect(varint.encodeUShort(65535)).toEqual(Buffer.from([0xff, 0xff]));
|
||||
});
|
||||
|
||||
it("should correctly assemble and parse a Minecraft handshake packet", () => {
|
||||
const protocolVersion = -1;
|
||||
const virtualHost = "mc.example.com";
|
||||
const port = 25565;
|
||||
|
||||
const payload = Buffer.concat([
|
||||
varint.encodeInt(0),
|
||||
varint.encodeInt(protocolVersion),
|
||||
varint.encodeInt(virtualHost.length),
|
||||
varint.encodeString(virtualHost),
|
||||
varint.encodeUShort(port),
|
||||
varint.encodeInt(1),
|
||||
]);
|
||||
|
||||
const finalPacket = varint.concat([payload]);
|
||||
|
||||
const decodedPacketLength = varint.decodeInt(finalPacket, 0);
|
||||
it("should correctly assemble a Minecraft packet with a length prefix", () => {
|
||||
const payloadParts = [
|
||||
varint.encodeVarInt(0), // protocol
|
||||
varint.encodeString("mc.example.com"), // host
|
||||
varint.encodeUShort(25565), // port
|
||||
];
|
||||
const payload = Buffer.concat(payloadParts);
|
||||
const finalPacket = varint.concatPackets(payloadParts);
|
||||
const { value: decodedPacketLength, bytesRead } = varint.decodeVarInt(
|
||||
finalPacket,
|
||||
0
|
||||
);
|
||||
expect(decodedPacketLength).toBe(payload.length);
|
||||
|
||||
const lengthOfPacketLength = varint.decodeLength(decodedPacketLength);
|
||||
const decodedPayload = finalPacket.subarray(lengthOfPacketLength);
|
||||
const decodedPayload = finalPacket.subarray(bytesRead);
|
||||
expect(decodedPayload).toEqual(payload);
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user