Fix query not following normal MC standards (#736)

Changed the query token generation to generate a 4 byte int represented as a null terminated string
This commit is contained in:
rtm516 2020-06-05 01:04:38 +01:00 committed by GitHub
parent 1d8995efe6
commit ccb44f604e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -60,6 +60,7 @@ public class QueryPacketHandler {
/** /**
* The Query packet handler instance * The Query packet handler instance
*
* @param connector Geyser Connector * @param connector Geyser Connector
* @param sender The Sender IP/Port for the Query * @param sender The Sender IP/Port for the Query
* @param buffer The Query data * @param buffer The Query data
@ -79,11 +80,12 @@ public class QueryPacketHandler {
/** /**
* Checks the packet is in fact a query packet * Checks the packet is in fact a query packet
*
* @param buffer Query data * @param buffer Query data
* @return if the packet is a query packet * @return if the packet is a query packet
*/ */
private boolean isQueryPacket(ByteBuf buffer) { private boolean isQueryPacket(ByteBuf buffer) {
return (buffer.readableBytes() >= 2) ? buffer.readUnsignedShort() == 65277 : false; return (buffer.readableBytes() >= 2) ? buffer.readUnsignedShort() == 0xFEFD : false;
} }
/** /**
@ -130,6 +132,7 @@ public class QueryPacketHandler {
/** /**
* Gets the game data for the query * Gets the game data for the query
*
* @return the game data for the query * @return the game data for the query
*/ */
private byte[] getGameData() { private byte[] getGameData() {
@ -177,7 +180,7 @@ public class QueryPacketHandler {
// Blank Buffer Bytes // Blank Buffer Bytes
query.write("GeyserMC".getBytes()); query.write("GeyserMC".getBytes());
query.write((byte) 0x00); query.write((byte) 0x00);
query.write((byte) 128); query.write((byte) 0x80);
query.write((byte) 0x00); query.write((byte) 0x00);
// Fills the game data // Fills the game data
@ -189,7 +192,7 @@ public class QueryPacketHandler {
} }
// Final byte to show the end of the game data // Final byte to show the end of the game data
query.write(new byte[]{0x00, 0x01}); query.write(new byte[] { 0x00, 0x01 });
return query.toByteArray(); return query.toByteArray();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
@ -197,6 +200,11 @@ public class QueryPacketHandler {
} }
} }
/**
* Generate a byte[] storing the player names
*
* @return The byte[] representation of players
*/
private byte[] getPlayers() { private byte[] getPlayers() {
ByteArrayOutputStream query = new ByteArrayOutputStream(); ByteArrayOutputStream query = new ByteArrayOutputStream();
@ -208,7 +216,7 @@ public class QueryPacketHandler {
try { try {
// Start the player section // Start the player section
query.write("player_".getBytes()); query.write("player_".getBytes());
query.write(new byte[]{0x00, 0x00}); query.write(new byte[] { 0x00, 0x00 });
// Fill player names // Fill player names
if(pingInfo != null) { if(pingInfo != null) {
@ -229,6 +237,7 @@ public class QueryPacketHandler {
/** /**
* Sends a packet to the sender * Sends a packet to the sender
*
* @param data packet data * @param data packet data
*/ */
private void sendPacket(ByteBuf data) { private void sendPacket(ByteBuf data) {
@ -251,18 +260,28 @@ public class QueryPacketHandler {
* Gets an MD5 token for the current IP/Port. * Gets an MD5 token for the current IP/Port.
* This should reset every 30 seconds but a new one is generated per instance * This should reset every 30 seconds but a new one is generated per instance
* Seems wasteful to code something in to clear it when it has no use. * Seems wasteful to code something in to clear it when it has no use.
*
* @param token the token * @param token the token
* @param address the address * @param address the address
* @return an MD5 token for the current IP/Port * @return an MD5 token for the current IP/Port
*/ */
public static byte[] getTokenString(byte[] token, InetAddress address) { public static byte[] getTokenString(byte[] token, InetAddress address) {
try { try {
// Generate an MD5 hash from the address
MessageDigest digest = MessageDigest.getInstance("MD5"); MessageDigest digest = MessageDigest.getInstance("MD5");
digest.update(address.toString().getBytes(StandardCharsets.UTF_8)); digest.update(address.toString().getBytes(StandardCharsets.UTF_8));
digest.update(token); digest.update(token);
return Arrays.copyOf(digest.digest(), 4);
// Get the first 4 bytes of the digest
byte[] digestBytes = Arrays.copyOf(digest.digest(), 4);
// Convert the bytes to a buffer
ByteBuffer byteBuffer = ByteBuffer.wrap(digestBytes);
// Turn the number into a null terminated string
return (byteBuffer.getInt() + "\0").getBytes();
} catch (NoSuchAlgorithmException e) { } catch (NoSuchAlgorithmException e) {
return ByteBuffer.allocate(4).putInt(ThreadLocalRandom.current().nextInt()).array(); return (ByteBuffer.allocate(4).putInt(ThreadLocalRandom.current().nextInt()).getInt() + "\0").getBytes();
} }
} }
} }