2020-05-23 21:50:04 +00:00
|
|
|
/*
|
2022-01-01 19:03:05 +00:00
|
|
|
* Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
|
2020-05-23 21:50:04 +00:00
|
|
|
*
|
2020-07-30 20:10:15 +00:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2020-05-23 21:50:04 +00:00
|
|
|
*
|
2020-07-30 20:10:15 +00:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
2020-05-23 21:50:04 +00:00
|
|
|
*
|
2020-07-30 20:10:15 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
2020-05-23 21:50:04 +00:00
|
|
|
*
|
2020-07-30 20:10:15 +00:00
|
|
|
* @author GeyserMC
|
|
|
|
* @link https://github.com/GeyserMC/Geyser
|
2020-05-23 21:50:04 +00:00
|
|
|
*/
|
|
|
|
|
2021-11-20 21:34:30 +00:00
|
|
|
package org.geysermc.geyser.network;
|
2020-05-23 21:50:04 +00:00
|
|
|
|
|
|
|
import io.netty.buffer.ByteBuf;
|
|
|
|
import io.netty.buffer.ByteBufAllocator;
|
2021-11-20 21:34:30 +00:00
|
|
|
import org.geysermc.geyser.GeyserImpl;
|
2021-11-20 23:29:46 +00:00
|
|
|
import org.geysermc.geyser.ping.GeyserPingInfo;
|
|
|
|
import org.geysermc.geyser.translator.text.MessageTranslator;
|
2020-05-23 21:50:04 +00:00
|
|
|
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.IOException;
|
2021-08-12 14:42:58 +00:00
|
|
|
import java.io.OutputStream;
|
2020-05-23 21:50:04 +00:00
|
|
|
import java.net.InetAddress;
|
|
|
|
import java.net.InetSocketAddress;
|
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
import java.security.MessageDigest;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.concurrent.ThreadLocalRandom;
|
|
|
|
|
|
|
|
public class QueryPacketHandler {
|
|
|
|
public static final byte HANDSHAKE = 0x09;
|
|
|
|
public static final byte STATISTICS = 0x00;
|
|
|
|
|
2021-11-20 21:34:30 +00:00
|
|
|
private final GeyserImpl geyser;
|
2021-08-12 14:42:58 +00:00
|
|
|
private final InetSocketAddress sender;
|
|
|
|
private final byte type;
|
|
|
|
private final int sessionId;
|
2020-05-23 21:50:04 +00:00
|
|
|
private byte[] token;
|
|
|
|
|
|
|
|
/**
|
2021-08-12 14:42:58 +00:00
|
|
|
* The Query packet handler instance. The unsigned short magic handshake should already be read at this point,
|
|
|
|
* and the packet should be verified to have enough buffer space to be a qualified query packet.
|
2020-06-05 00:04:38 +00:00
|
|
|
*
|
2021-11-20 21:34:30 +00:00
|
|
|
* @param geyser Geyser
|
2020-05-23 21:50:04 +00:00
|
|
|
* @param sender The Sender IP/Port for the Query
|
|
|
|
* @param buffer The Query data
|
|
|
|
*/
|
2021-11-20 21:34:30 +00:00
|
|
|
public QueryPacketHandler(GeyserImpl geyser, InetSocketAddress sender, ByteBuf buffer) {
|
|
|
|
this.geyser = geyser;
|
2020-05-23 21:50:04 +00:00
|
|
|
this.sender = sender;
|
|
|
|
this.type = buffer.readByte();
|
|
|
|
this.sessionId = buffer.readInt();
|
|
|
|
|
|
|
|
regenerateToken();
|
|
|
|
handle();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks the packet is in fact a query packet
|
2020-06-05 00:04:38 +00:00
|
|
|
*
|
2020-05-23 21:50:04 +00:00
|
|
|
* @param buffer Query data
|
|
|
|
* @return if the packet is a query packet
|
|
|
|
*/
|
2021-08-12 14:42:58 +00:00
|
|
|
public static boolean isQueryPacket(ByteBuf buffer) {
|
|
|
|
// 2 for magic short, 1 for type byte and 4 for session ID int
|
|
|
|
return buffer.readableBytes() >= (2 + 1 + 4) && buffer.readUnsignedShort() == 0xFEFD;
|
2020-05-23 21:50:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles the query
|
|
|
|
*/
|
|
|
|
private void handle() {
|
|
|
|
switch (type) {
|
|
|
|
case HANDSHAKE:
|
|
|
|
sendToken();
|
2022-06-14 23:51:45 +00:00
|
|
|
break;
|
2020-05-23 21:50:04 +00:00
|
|
|
case STATISTICS:
|
|
|
|
sendQueryData();
|
2022-06-14 23:51:45 +00:00
|
|
|
break;
|
2020-05-23 21:50:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends the token to the sender
|
|
|
|
*/
|
|
|
|
private void sendToken() {
|
|
|
|
ByteBuf reply = ByteBufAllocator.DEFAULT.ioBuffer(10);
|
|
|
|
reply.writeByte(HANDSHAKE);
|
|
|
|
reply.writeInt(sessionId);
|
|
|
|
reply.writeBytes(getTokenString(this.token, this.sender.getAddress()));
|
|
|
|
reply.writeByte(0);
|
|
|
|
|
|
|
|
sendPacket(reply);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends the query data to the sender
|
|
|
|
*/
|
|
|
|
private void sendQueryData() {
|
2021-08-12 14:42:58 +00:00
|
|
|
byte[] gameData = getGameData();
|
|
|
|
byte[] playerData = getPlayers();
|
|
|
|
|
|
|
|
ByteBuf reply = ByteBufAllocator.DEFAULT.ioBuffer(1 + 4 + gameData.length + playerData.length);
|
2020-05-23 21:50:04 +00:00
|
|
|
reply.writeByte(STATISTICS);
|
|
|
|
reply.writeInt(sessionId);
|
|
|
|
|
|
|
|
// Game Info
|
2021-08-12 14:42:58 +00:00
|
|
|
reply.writeBytes(gameData);
|
2020-05-23 21:50:04 +00:00
|
|
|
|
|
|
|
// Players
|
2021-08-12 14:42:58 +00:00
|
|
|
reply.writeBytes(playerData);
|
2020-05-23 21:50:04 +00:00
|
|
|
|
|
|
|
sendPacket(reply);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the game data for the query
|
2020-06-05 00:04:38 +00:00
|
|
|
*
|
2020-05-23 21:50:04 +00:00
|
|
|
* @return the game data for the query
|
|
|
|
*/
|
|
|
|
private byte[] getGameData() {
|
|
|
|
ByteArrayOutputStream query = new ByteArrayOutputStream();
|
|
|
|
|
|
|
|
GeyserPingInfo pingInfo = null;
|
|
|
|
String motd;
|
|
|
|
String currentPlayerCount;
|
|
|
|
String maxPlayerCount;
|
2020-08-18 03:36:15 +00:00
|
|
|
String map;
|
2020-05-23 21:50:04 +00:00
|
|
|
|
2021-11-20 21:34:30 +00:00
|
|
|
if (geyser.getConfig().isPassthroughMotd() || geyser.getConfig().isPassthroughPlayerCounts()) {
|
|
|
|
pingInfo = geyser.getBootstrap().getGeyserPingPassthrough().getPingInformation();
|
2020-05-23 21:50:04 +00:00
|
|
|
}
|
|
|
|
|
2021-11-20 21:34:30 +00:00
|
|
|
if (geyser.getConfig().isPassthroughMotd() && pingInfo != null) {
|
2020-11-16 23:57:57 +00:00
|
|
|
String[] javaMotd = MessageTranslator.convertMessageLenient(pingInfo.getDescription()).split("\n");
|
2020-05-23 21:50:04 +00:00
|
|
|
motd = javaMotd[0].trim(); // First line of the motd.
|
|
|
|
} else {
|
2022-07-09 22:39:02 +00:00
|
|
|
motd = geyser.getConfig().getBedrock().primaryMotd();
|
2020-05-23 21:50:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If passthrough player counts is enabled lets get players from the server
|
2021-11-20 21:34:30 +00:00
|
|
|
if (geyser.getConfig().isPassthroughPlayerCounts() && pingInfo != null) {
|
2020-07-04 21:35:48 +00:00
|
|
|
currentPlayerCount = String.valueOf(pingInfo.getPlayers().getOnline());
|
|
|
|
maxPlayerCount = String.valueOf(pingInfo.getPlayers().getMax());
|
2020-05-23 21:50:04 +00:00
|
|
|
} else {
|
2021-11-20 21:34:30 +00:00
|
|
|
currentPlayerCount = String.valueOf(geyser.getSessionManager().getSessions().size());
|
|
|
|
maxPlayerCount = String.valueOf(geyser.getConfig().getMaxPlayers());
|
2020-05-23 21:50:04 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 03:36:15 +00:00
|
|
|
// If passthrough protocol name is enabled let's get the protocol name from the ping response.
|
2021-11-20 21:34:30 +00:00
|
|
|
if (geyser.getConfig().isPassthroughProtocolName() && pingInfo != null) {
|
2021-08-12 14:42:58 +00:00
|
|
|
map = pingInfo.getVersion().getName();
|
2020-08-18 03:36:15 +00:00
|
|
|
} else {
|
2021-11-20 21:34:30 +00:00
|
|
|
map = GeyserImpl.NAME;
|
2020-08-18 03:36:15 +00:00
|
|
|
}
|
|
|
|
|
2020-05-23 21:50:04 +00:00
|
|
|
// Create a hashmap of all game data needed in the query
|
2021-08-12 14:42:58 +00:00
|
|
|
Map<String, String> gameData = new HashMap<>();
|
2020-05-23 21:50:04 +00:00
|
|
|
gameData.put("hostname", motd);
|
|
|
|
gameData.put("gametype", "SMP");
|
|
|
|
gameData.put("game_id", "MINECRAFT");
|
2022-03-20 02:55:29 +00:00
|
|
|
gameData.put("version", GeyserImpl.NAME + " (" + GeyserImpl.GIT_VERSION + ") " + GameProtocol.DEFAULT_BEDROCK_CODEC.getMinecraftVersion());
|
2020-05-23 21:50:04 +00:00
|
|
|
gameData.put("plugins", "");
|
2020-08-18 03:36:15 +00:00
|
|
|
gameData.put("map", map);
|
2020-05-23 21:50:04 +00:00
|
|
|
gameData.put("numplayers", currentPlayerCount);
|
|
|
|
gameData.put("maxplayers", maxPlayerCount);
|
2022-07-09 22:39:02 +00:00
|
|
|
gameData.put("hostport", String.valueOf(geyser.getConfig().getBedrock().port()));
|
|
|
|
gameData.put("hostip", geyser.getConfig().getBedrock().address());
|
2020-05-23 21:50:04 +00:00
|
|
|
|
|
|
|
try {
|
2021-08-12 14:42:58 +00:00
|
|
|
writeString(query, "GeyserMC");
|
2020-06-05 00:04:38 +00:00
|
|
|
query.write((byte) 0x80);
|
2020-05-23 21:50:04 +00:00
|
|
|
query.write((byte) 0x00);
|
|
|
|
|
|
|
|
// Fills the game data
|
2021-08-12 14:42:58 +00:00
|
|
|
for (Map.Entry<String, String> entry : gameData.entrySet()) {
|
|
|
|
writeString(query, entry.getKey());
|
|
|
|
writeString(query, entry.getValue());
|
2020-05-23 21:50:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Final byte to show the end of the game data
|
2020-06-05 00:04:38 +00:00
|
|
|
query.write(new byte[] { 0x00, 0x01 });
|
2020-05-23 21:50:04 +00:00
|
|
|
return query.toByteArray();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
return new byte[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-05 00:04:38 +00:00
|
|
|
/**
|
|
|
|
* Generate a byte[] storing the player names
|
|
|
|
*
|
|
|
|
* @return The byte[] representation of players
|
|
|
|
*/
|
2020-05-23 21:50:04 +00:00
|
|
|
private byte[] getPlayers() {
|
|
|
|
ByteArrayOutputStream query = new ByteArrayOutputStream();
|
|
|
|
|
|
|
|
GeyserPingInfo pingInfo = null;
|
2021-11-20 21:34:30 +00:00
|
|
|
if (geyser.getConfig().isPassthroughMotd() || geyser.getConfig().isPassthroughPlayerCounts()) {
|
|
|
|
pingInfo = geyser.getBootstrap().getGeyserPingPassthrough().getPingInformation();
|
2020-05-23 21:50:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Start the player section
|
2021-08-12 14:42:58 +00:00
|
|
|
writeString(query, "player_");
|
|
|
|
query.write((byte) 0x00);
|
2020-05-23 21:50:04 +00:00
|
|
|
|
|
|
|
// Fill player names
|
2021-02-17 01:09:18 +00:00
|
|
|
if (pingInfo != null) {
|
2020-07-04 21:35:48 +00:00
|
|
|
for (String username : pingInfo.getPlayerList()) {
|
2021-08-12 14:42:58 +00:00
|
|
|
writeString(query, username);
|
2020-05-23 21:50:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Final byte to show the end of the player data
|
|
|
|
query.write((byte) 0x00);
|
|
|
|
return query.toByteArray();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
return new byte[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-12 14:42:58 +00:00
|
|
|
/**
|
|
|
|
* Partially mimics {@link java.io.DataOutputStream#writeBytes(String)} which is what the Minecraft server uses as of 1.17.1.
|
|
|
|
*/
|
|
|
|
private void writeString(OutputStream stream, String value) throws IOException {
|
|
|
|
int length = value.length();
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
stream.write((byte) value.charAt(i));
|
|
|
|
}
|
|
|
|
// Padding to indicate the end of the string
|
|
|
|
stream.write((byte) 0x00);
|
|
|
|
}
|
|
|
|
|
2020-05-23 21:50:04 +00:00
|
|
|
/**
|
|
|
|
* Sends a packet to the sender
|
2020-06-05 00:04:38 +00:00
|
|
|
*
|
2020-05-23 21:50:04 +00:00
|
|
|
* @param data packet data
|
|
|
|
*/
|
|
|
|
private void sendPacket(ByteBuf data) {
|
2022-10-30 00:23:21 +00:00
|
|
|
// geyser.getBedrockServer().getRakNet().send(sender, data);
|
2020-05-23 21:50:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Regenerates a token
|
|
|
|
*/
|
|
|
|
public void regenerateToken() {
|
|
|
|
byte[] token = new byte[16];
|
|
|
|
for (int i = 0; i < 16; i++) {
|
2021-08-27 01:43:53 +00:00
|
|
|
token[i] = (byte) ThreadLocalRandom.current().nextInt(255);
|
2020-05-23 21:50:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.token = token;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets an MD5 token for the current IP/Port.
|
|
|
|
* 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.
|
2020-06-05 00:04:38 +00:00
|
|
|
*
|
2020-05-23 21:50:04 +00:00
|
|
|
* @param token the token
|
|
|
|
* @param address the address
|
|
|
|
* @return an MD5 token for the current IP/Port
|
|
|
|
*/
|
|
|
|
public static byte[] getTokenString(byte[] token, InetAddress address) {
|
|
|
|
try {
|
2020-06-05 00:04:38 +00:00
|
|
|
// Generate an MD5 hash from the address
|
2020-05-23 21:50:04 +00:00
|
|
|
MessageDigest digest = MessageDigest.getInstance("MD5");
|
|
|
|
digest.update(address.toString().getBytes(StandardCharsets.UTF_8));
|
|
|
|
digest.update(token);
|
2020-06-05 00:04:38 +00:00
|
|
|
|
|
|
|
// 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();
|
2020-05-23 21:50:04 +00:00
|
|
|
} catch (NoSuchAlgorithmException e) {
|
2020-06-05 00:04:38 +00:00
|
|
|
return (ByteBuffer.allocate(4).putInt(ThreadLocalRandom.current().nextInt()).getInt() + "\0").getBytes();
|
2020-05-23 21:50:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|