3
0
Mirror von https://github.com/GeyserMC/Geyser.git synchronisiert 2024-12-25 15:50:14 +01:00

Query improvements

- Improved detection of query packet
- Don't initialize the QueryPacketHandler class until we verified we have query data
- Encode strings like the vanilla Minecraft server
Dieser Commit ist enthalten in:
Camotoy 2021-08-12 10:42:58 -04:00
Ursprung 0c5b39f35b
Commit b95cd8e0c1
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 7EEFB66FE798081F
2 geänderte Dateien mit 45 neuen und 31 gelöschten Zeilen

Datei anzeigen

@ -28,6 +28,7 @@ package org.geysermc.connector.network;
import com.nukkitx.protocol.bedrock.BedrockPong; import com.nukkitx.protocol.bedrock.BedrockPong;
import com.nukkitx.protocol.bedrock.BedrockServerEventHandler; import com.nukkitx.protocol.bedrock.BedrockServerEventHandler;
import com.nukkitx.protocol.bedrock.BedrockServerSession; import com.nukkitx.protocol.bedrock.BedrockServerSession;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.socket.DatagramPacket; import io.netty.channel.socket.DatagramPacket;
import org.geysermc.connector.GeyserConnector; import org.geysermc.connector.GeyserConnector;
@ -38,6 +39,7 @@ import org.geysermc.connector.network.translators.chat.MessageTranslator;
import org.geysermc.connector.ping.IGeyserPingPassthrough; import org.geysermc.connector.ping.IGeyserPingPassthrough;
import org.geysermc.connector.utils.LanguageUtils; import org.geysermc.connector.utils.LanguageUtils;
import javax.annotation.Nonnull;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.List; import java.util.List;
@ -166,7 +168,10 @@ public class ConnectorServerEventHandler implements BedrockServerEventHandler {
} }
@Override @Override
public void onUnhandledDatagram(ChannelHandlerContext ctx, DatagramPacket packet) { public void onUnhandledDatagram(@Nonnull ChannelHandlerContext ctx, DatagramPacket packet) {
new QueryPacketHandler(connector, packet.sender(), packet.content()); ByteBuf content = packet.content();
if (QueryPacketHandler.isQueryPacket(content)) {
new QueryPacketHandler(connector, packet.sender(), content);
}
} }
} }

Datei anzeigen

@ -27,12 +27,13 @@ package org.geysermc.connector.network;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator; import io.netty.buffer.ByteBufAllocator;
import org.geysermc.connector.common.ping.GeyserPingInfo;
import org.geysermc.connector.GeyserConnector; import org.geysermc.connector.GeyserConnector;
import org.geysermc.connector.common.ping.GeyserPingInfo;
import org.geysermc.connector.network.translators.chat.MessageTranslator; import org.geysermc.connector.network.translators.chat.MessageTranslator;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
@ -46,27 +47,24 @@ import java.util.Random;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
public class QueryPacketHandler { public class QueryPacketHandler {
public static final byte HANDSHAKE = 0x09; public static final byte HANDSHAKE = 0x09;
public static final byte STATISTICS = 0x00; public static final byte STATISTICS = 0x00;
private GeyserConnector connector; private final GeyserConnector connector;
private InetSocketAddress sender; private final InetSocketAddress sender;
private byte type; private final byte type;
private int sessionId; private final int sessionId;
private byte[] token; private byte[] token;
/** /**
* The Query packet handler instance * 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.
* *
* @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
*/ */
public QueryPacketHandler(GeyserConnector connector, InetSocketAddress sender, ByteBuf buffer) { public QueryPacketHandler(GeyserConnector connector, InetSocketAddress sender, ByteBuf buffer) {
if (!isQueryPacket(buffer))
return;
this.connector = connector; this.connector = connector;
this.sender = sender; this.sender = sender;
this.type = buffer.readByte(); this.type = buffer.readByte();
@ -82,8 +80,9 @@ public class QueryPacketHandler {
* @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) { public static boolean isQueryPacket(ByteBuf buffer) {
return buffer.readableBytes() >= 2 && buffer.readUnsignedShort() == 0xFEFD; // 2 for magic short, 1 for type byte and 4 for session ID int
return buffer.readableBytes() >= (2 + 1 + 4) && buffer.readUnsignedShort() == 0xFEFD;
} }
/** /**
@ -115,15 +114,18 @@ public class QueryPacketHandler {
* Sends the query data to the sender * Sends the query data to the sender
*/ */
private void sendQueryData() { private void sendQueryData() {
ByteBuf reply = ByteBufAllocator.DEFAULT.ioBuffer(64); byte[] gameData = getGameData();
byte[] playerData = getPlayers();
ByteBuf reply = ByteBufAllocator.DEFAULT.ioBuffer(1 + 4 + gameData.length + playerData.length);
reply.writeByte(STATISTICS); reply.writeByte(STATISTICS);
reply.writeInt(sessionId); reply.writeInt(sessionId);
// Game Info // Game Info
reply.writeBytes(getGameData()); reply.writeBytes(gameData);
// Players // Players
reply.writeBytes(getPlayers()); reply.writeBytes(playerData);
sendPacket(reply); sendPacket(reply);
} }
@ -164,13 +166,13 @@ public class QueryPacketHandler {
// If passthrough protocol name is enabled let's get the protocol name from the ping response. // If passthrough protocol name is enabled let's get the protocol name from the ping response.
if (connector.getConfig().isPassthroughProtocolName() && pingInfo != null) { if (connector.getConfig().isPassthroughProtocolName() && pingInfo != null) {
map = String.valueOf((pingInfo.getVersion().getName())); map = pingInfo.getVersion().getName();
} else { } else {
map = GeyserConnector.NAME; map = GeyserConnector.NAME;
} }
// Create a hashmap of all game data needed in the query // Create a hashmap of all game data needed in the query
Map<String, String> gameData = new HashMap<String, String>(); Map<String, String> gameData = new HashMap<>();
gameData.put("hostname", motd); gameData.put("hostname", motd);
gameData.put("gametype", "SMP"); gameData.put("gametype", "SMP");
gameData.put("game_id", "MINECRAFT"); gameData.put("game_id", "MINECRAFT");
@ -183,18 +185,14 @@ public class QueryPacketHandler {
gameData.put("hostip", connector.getConfig().getBedrock().getAddress()); gameData.put("hostip", connector.getConfig().getBedrock().getAddress());
try { try {
// Blank Buffer Bytes writeString(query, "GeyserMC");
query.write("GeyserMC".getBytes());
query.write((byte) 0x00);
query.write((byte) 0x80); query.write((byte) 0x80);
query.write((byte) 0x00); query.write((byte) 0x00);
// Fills the game data // Fills the game data
for (Map.Entry<String, String> entry : gameData.entrySet()) { for (Map.Entry<String, String> entry : gameData.entrySet()) {
query.write(entry.getKey().getBytes()); writeString(query, entry.getKey());
query.write((byte) 0x00); writeString(query, entry.getValue());
query.write(entry.getValue().getBytes());
query.write((byte) 0x00);
} }
// Final byte to show the end of the game data // Final byte to show the end of the game data
@ -221,14 +219,13 @@ public class QueryPacketHandler {
try { try {
// Start the player section // Start the player section
query.write("player_".getBytes()); writeString(query, "player_");
query.write(new byte[] { 0x00, 0x00 }); query.write((byte) 0x00);
// Fill player names // Fill player names
if (pingInfo != null) { if (pingInfo != null) {
for (String username : pingInfo.getPlayerList()) { for (String username : pingInfo.getPlayerList()) {
query.write(username.getBytes()); writeString(query, username);
query.write((byte) 0x00);
} }
} }
@ -241,6 +238,18 @@ public class QueryPacketHandler {
} }
} }
/**
* 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);
}
/** /**
* Sends a packet to the sender * Sends a packet to the sender
* *