Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If the IP address is too short, an error message is displayed! #283

Open
Mc-andan opened this issue Jan 10, 2022 · 1 comment
Open

If the IP address is too short, an error message is displayed! #283

Mc-andan opened this issue Jan 10, 2022 · 1 comment

Comments

@Mc-andan
Copy link

E:\MineTrack\node_modules\bytebuffer\dist\ByteBufferNB.js:633
                throw RangeError("Illegal offset: 0 <= "+offset+" (+"+2+") <= "+this.buffer.length);
                ^

RangeError: Illegal offset: 0 <= 33 (+2) <= 33
    at ByteBuffer.module.exports.ByteBufferPrototype.readInt16 (E:\MineTrack\node_modules\bytebuffer\dist\ByteBufferNB.js:633:23)
    at UNCONNECTED_PONG.decode (E:\MineTrack\node_modules\mcpe-ping-fixed\index.js:68:29)
    at E:\MineTrack\node_modules\mcpe-ping-fixed\index.js:140:14
    at Socket.emit (node:events:390:28)
    at UDP.onMessage [as onmessage] (node:dgram:939:8)

Node.js v17.2.0
{
	"name": "test",
	"ip": "106.2.xx.xx",
	"type": "PE"
}


@hugmanrique
Copy link
Contributor

I've taken a look at the code, and I suspect the server is replying with corrupt data. The exception is thrown here:

// ...
UNCONNECTED_PONG.prototype.decode = function () {
  this.pingId = this.bb.readLong();
  this.serverId = this.bb.readLong();
  this.bb.offset += 16;
  this.nameLength = this.bb.readShort(); // <-- HERE
  try {
    this.advertiseString = this.bb.readUTF8String(this.nameLength);
  }
  catch(e){ //FIXME
    this.advertiseString = this.bb.readUTF8String(parseInt(e.message.substr(e.message.indexOf(",")+2, 3)));
  }
//...
};

The "Unconnected Pong" packet format is given in https://github.com/NiclasOlofsson/MiNET/blob/fc80766d69d27e02b04b57276e5744a5f17e3b28/src/MiNET/MiNET/Net/MCPE%20Protocol.xml#L45-L50. Note that we first read the ping ID, then the server ID, skip the 16-byte long OFFLINE_MESSAGE_DATA_ID and then attempt to read the length of the Server Name field. In this case, the buffer ends at this position, meaning there's no length to be read. wiki.vg specifies this field must be present even if the string is empty.

Unrelated, but the solution to the FIXME below is to pass ByteBuffer.METRICS_BYTES as the second argument to bb#readUTF8String (it expects the number of characters by default), i.e. the updated method is

UNCONNECTED_PONG.prototype.decode = function () {
  this.pingId = this.bb.readLong();
  this.serverId = this.bb.readLong();
  this.bb.offset += 16;
  this.nameLength = this.bb.readShort();
  this.advertiseString = this.bb.readUTF8String(this.nameLength, ByteBuffer.METRICS_BYTES);
  var splitString = this.advertiseString.split(/;/g);
  this.gameId = splitString[0];
  this.name = splitString[1];
  this.unknownId = splitString[2];
  this.gameVersion = splitString[3];
  this.currentPlayers = splitString[4];
  this.maxPlayers = splitString[5];
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants