From bb6a1ec40ab42c8b57ee4a427683410302b43774 Mon Sep 17 00:00:00 2001 From: Konicai <71294714+Konicai@users.noreply.github.com> Date: Mon, 13 Nov 2023 18:17:40 -0500 Subject: [PATCH] Simply pingpassthrough logic, add fabric ping passthrough (#3930) * Check if PingPassthrough is null * Remove QueryPacketHandler * Fabric ping passthrough --- .../GeyserBungeePingPassthrough.java | 12 +- .../platform/fabric/GeyserFabricMod.java | 6 +- .../platform/fabric/ModPingPassthrough.java | 110 +++++++ .../spigot/GeyserPaperPingPassthrough.java | 12 +- .../spigot/GeyserSpigotPingPassthrough.java | 8 +- .../GeyserVelocityPingPassthrough.java | 14 +- .../org/geysermc/geyser/GeyserBootstrap.java | 9 +- .../configuration/GeyserConfiguration.java | 3 - .../GeyserJacksonConfiguration.java | 3 - .../geyser/network/QueryPacketHandler.java | 303 ------------------ .../geyser/network/netty/GeyserServer.java | 21 +- .../geysermc/geyser/ping/GeyserPingInfo.java | 39 +-- .../geyser/ping/IGeyserPingPassthrough.java | 11 - core/src/main/resources/config.yml | 3 - 14 files changed, 160 insertions(+), 394 deletions(-) create mode 100644 bootstrap/fabric/src/main/java/org/geysermc/geyser/platform/fabric/ModPingPassthrough.java delete mode 100644 core/src/main/java/org/geysermc/geyser/network/QueryPacketHandler.java diff --git a/bootstrap/bungeecord/src/main/java/org/geysermc/geyser/platform/bungeecord/GeyserBungeePingPassthrough.java b/bootstrap/bungeecord/src/main/java/org/geysermc/geyser/platform/bungeecord/GeyserBungeePingPassthrough.java index 39fb9e134..e14e8ff66 100644 --- a/bootstrap/bungeecord/src/main/java/org/geysermc/geyser/platform/bungeecord/GeyserBungeePingPassthrough.java +++ b/bootstrap/bungeecord/src/main/java/org/geysermc/geyser/platform/bungeecord/GeyserBungeePingPassthrough.java @@ -40,7 +40,6 @@ import org.geysermc.geyser.ping.IGeyserPingPassthrough; import java.net.InetSocketAddress; import java.net.SocketAddress; -import java.util.Arrays; import java.util.UUID; import java.util.concurrent.CompletableFuture; @@ -61,16 +60,11 @@ public class GeyserBungeePingPassthrough implements IGeyserPingPassthrough, List })); ProxyPingEvent event = future.join(); ServerPing response = event.getResponse(); - GeyserPingInfo geyserPingInfo = new GeyserPingInfo( + return new GeyserPingInfo( response.getDescriptionComponent().toLegacyText(), - new GeyserPingInfo.Players(response.getPlayers().getMax(), response.getPlayers().getOnline()), - new GeyserPingInfo.Version(response.getVersion().getName(), response.getVersion().getProtocol()) + response.getPlayers().getMax(), + response.getPlayers().getOnline() ); - if (event.getResponse().getPlayers().getSample() != null) { - Arrays.stream(event.getResponse().getPlayers().getSample()).forEach(proxiedPlayer -> - geyserPingInfo.getPlayerList().add(proxiedPlayer.getName())); - } - return geyserPingInfo; } // This is static so pending connection can use it diff --git a/bootstrap/fabric/src/main/java/org/geysermc/geyser/platform/fabric/GeyserFabricMod.java b/bootstrap/fabric/src/main/java/org/geysermc/geyser/platform/fabric/GeyserFabricMod.java index 0bbe73f16..af0c9efca 100644 --- a/bootstrap/fabric/src/main/java/org/geysermc/geyser/platform/fabric/GeyserFabricMod.java +++ b/bootstrap/fabric/src/main/java/org/geysermc/geyser/platform/fabric/GeyserFabricMod.java @@ -144,7 +144,11 @@ public class GeyserFabricMod implements ModInitializer, GeyserBootstrap { GeyserImpl.start(); - this.geyserPingPassthrough = GeyserLegacyPingPassthrough.init(geyser); + if (geyserConfig.isLegacyPingPassthrough()) { + this.geyserPingPassthrough = GeyserLegacyPingPassthrough.init(geyser); + } else { + this.geyserPingPassthrough = new ModPingPassthrough(server, geyserLogger); + } this.geyserCommandManager = new GeyserCommandManager(geyser); this.geyserCommandManager.init(); diff --git a/bootstrap/fabric/src/main/java/org/geysermc/geyser/platform/fabric/ModPingPassthrough.java b/bootstrap/fabric/src/main/java/org/geysermc/geyser/platform/fabric/ModPingPassthrough.java new file mode 100644 index 000000000..e74be7fb7 --- /dev/null +++ b/bootstrap/fabric/src/main/java/org/geysermc/geyser/platform/fabric/ModPingPassthrough.java @@ -0,0 +1,110 @@ +/* + * Copyright (c) 2019-2023 GeyserMC. http://geysermc.org + * + * 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: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * 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. + * + * @author GeyserMC + * @link https://github.com/GeyserMC/Geyser + */ + +package org.geysermc.geyser.platform.fabric; + +import lombok.AllArgsConstructor; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; +import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; +import net.minecraft.network.Connection; +import net.minecraft.network.PacketSendListener; +import net.minecraft.network.protocol.Packet; +import net.minecraft.network.protocol.PacketFlow; +import net.minecraft.network.protocol.status.ClientboundStatusResponsePacket; +import net.minecraft.network.protocol.status.ServerStatus; +import net.minecraft.network.protocol.status.ServerStatusPacketListener; +import net.minecraft.network.protocol.status.ServerboundStatusRequestPacket; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.network.ServerStatusPacketListenerImpl; +import org.geysermc.geyser.GeyserLogger; +import org.geysermc.geyser.ping.GeyserPingInfo; +import org.geysermc.geyser.ping.IGeyserPingPassthrough; +import org.jetbrains.annotations.Nullable; + +import java.net.InetSocketAddress; +import java.util.Objects; + +@AllArgsConstructor +public class ModPingPassthrough implements IGeyserPingPassthrough { + + private static final GsonComponentSerializer GSON_SERIALIZER = GsonComponentSerializer.gson(); + private static final LegacyComponentSerializer LEGACY_SERIALIZER = LegacyComponentSerializer.legacySection(); + + private final MinecraftServer server; + private final GeyserLogger logger; + + @Nullable + @Override + public GeyserPingInfo getPingInformation(InetSocketAddress inetSocketAddress) { + ServerStatus status = server.getStatus(); + if (status == null) { + return null; + } + + try { + StatusInterceptor connection = new StatusInterceptor(); + ServerStatusPacketListener statusPacketListener = new ServerStatusPacketListenerImpl(status, connection); + + statusPacketListener.handleStatusRequest(new ServerboundStatusRequestPacket()); + // mods like MiniMOTD (that inject into the above method) have now processed the response + status = Objects.requireNonNull(connection.status, "status response"); + } catch (Exception e) { + if (logger.isDebug()) { + logger.debug("Failed to listen for modified ServerStatus: " + e.getMessage()); + e.printStackTrace(); + } + } + + String jsonDescription = net.minecraft.network.chat.Component.Serializer.toJson(status.description()); + String legacyDescription = LEGACY_SERIALIZER.serialize(GSON_SERIALIZER.deserializeOr(jsonDescription, Component.empty())); + + return new GeyserPingInfo( + legacyDescription, + status.players().map(ServerStatus.Players::max).orElse(1), + status.players().map(ServerStatus.Players::online).orElse(0) + ); + } + + /** + * Custom Connection that intercepts the status response right before it is sent + */ + private static class StatusInterceptor extends Connection { + + ServerStatus status; + + StatusInterceptor() { + super(PacketFlow.SERVERBOUND); // we are the server. + } + + @Override + public void send(Packet packet, @Nullable PacketSendListener packetSendListener, boolean bl) { + if (packet instanceof ClientboundStatusResponsePacket statusResponse) { + status = statusResponse.status(); + } + super.send(packet, packetSendListener, bl); + } + } +} diff --git a/bootstrap/spigot/src/main/java/org/geysermc/geyser/platform/spigot/GeyserPaperPingPassthrough.java b/bootstrap/spigot/src/main/java/org/geysermc/geyser/platform/spigot/GeyserPaperPingPassthrough.java index bb0f30e70..60e0ae519 100644 --- a/bootstrap/spigot/src/main/java/org/geysermc/geyser/platform/spigot/GeyserPaperPingPassthrough.java +++ b/bootstrap/spigot/src/main/java/org/geysermc/geyser/platform/spigot/GeyserPaperPingPassthrough.java @@ -27,7 +27,6 @@ package org.geysermc.geyser.platform.spigot; import com.destroystokyo.paper.event.server.PaperServerListPingEvent; import com.destroystokyo.paper.network.StatusClient; -import com.destroystokyo.paper.profile.PlayerProfile; import org.bukkit.Bukkit; import org.geysermc.geyser.network.GameProtocol; import org.geysermc.geyser.ping.GeyserPingInfo; @@ -81,16 +80,7 @@ public final class GeyserPaperPingPassthrough implements IGeyserPingPassthrough players = new GeyserPingInfo.Players(event.getMaxPlayers(), event.getNumPlayers()); } - GeyserPingInfo geyserPingInfo = new GeyserPingInfo(event.getMotd(), players, - new GeyserPingInfo.Version(Bukkit.getVersion(), GameProtocol.getJavaProtocolVersion())); - - if (!event.shouldHidePlayers()) { - for (PlayerProfile profile : event.getPlayerSample()) { - geyserPingInfo.getPlayerList().add(profile.getName()); - } - } - - return geyserPingInfo; + return new GeyserPingInfo(event.getMotd(), players); } catch (Exception | LinkageError e) { // LinkageError in the event that method/constructor signatures change logger.debug("Error while getting Paper ping passthrough: " + e); return null; diff --git a/bootstrap/spigot/src/main/java/org/geysermc/geyser/platform/spigot/GeyserSpigotPingPassthrough.java b/bootstrap/spigot/src/main/java/org/geysermc/geyser/platform/spigot/GeyserSpigotPingPassthrough.java index 1e6a0ad6c..df197f137 100644 --- a/bootstrap/spigot/src/main/java/org/geysermc/geyser/platform/spigot/GeyserSpigotPingPassthrough.java +++ b/bootstrap/spigot/src/main/java/org/geysermc/geyser/platform/spigot/GeyserSpigotPingPassthrough.java @@ -30,7 +30,6 @@ import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.event.server.ServerListPingEvent; import org.bukkit.util.CachedServerIcon; -import org.geysermc.geyser.network.GameProtocol; import org.geysermc.geyser.ping.GeyserPingInfo; import org.geysermc.geyser.ping.IGeyserPingPassthrough; @@ -50,12 +49,7 @@ public class GeyserSpigotPingPassthrough implements IGeyserPingPassthrough { try { ServerListPingEvent event = new GeyserPingEvent(inetSocketAddress.getAddress(), Bukkit.getMotd(), Bukkit.getOnlinePlayers().size(), Bukkit.getMaxPlayers()); Bukkit.getPluginManager().callEvent(event); - GeyserPingInfo geyserPingInfo = new GeyserPingInfo(event.getMotd(), - new GeyserPingInfo.Players(event.getMaxPlayers(), event.getNumPlayers()), - new GeyserPingInfo.Version(Bukkit.getVersion(), GameProtocol.getJavaProtocolVersion()) // thanks Spigot for not exposing this, just default to latest - ); - Bukkit.getOnlinePlayers().stream().map(Player::getName).forEach(geyserPingInfo.getPlayerList()::add); - return geyserPingInfo; + return new GeyserPingInfo(event.getMotd(), event.getMaxPlayers(), event.getNumPlayers()); } catch (Exception | LinkageError e) { // LinkageError in the event that method/constructor signatures change logger.debug("Error while getting Bukkit ping passthrough: " + e); return null; diff --git a/bootstrap/velocity/src/main/java/org/geysermc/geyser/platform/velocity/GeyserVelocityPingPassthrough.java b/bootstrap/velocity/src/main/java/org/geysermc/geyser/platform/velocity/GeyserVelocityPingPassthrough.java index 1a9b9bf26..8944ea134 100644 --- a/bootstrap/velocity/src/main/java/org/geysermc/geyser/platform/velocity/GeyserVelocityPingPassthrough.java +++ b/bootstrap/velocity/src/main/java/org/geysermc/geyser/platform/velocity/GeyserVelocityPingPassthrough.java @@ -54,19 +54,11 @@ public class GeyserVelocityPingPassthrough implements IGeyserPingPassthrough { } catch (ExecutionException | InterruptedException e) { throw new RuntimeException(e); } - GeyserPingInfo geyserPingInfo = new GeyserPingInfo( + return new GeyserPingInfo( LegacyComponentSerializer.legacy('ยง').serialize(event.getPing().getDescriptionComponent()), - new GeyserPingInfo.Players( - event.getPing().getPlayers().orElseThrow(IllegalStateException::new).getMax(), - event.getPing().getPlayers().orElseThrow(IllegalStateException::new).getOnline() - ), - new GeyserPingInfo.Version( - event.getPing().getVersion().getName(), - event.getPing().getVersion().getProtocol() - ) + event.getPing().getPlayers().map(ServerPing.Players::getMax).orElse(1), + event.getPing().getPlayers().map(ServerPing.Players::getOnline).orElse(0) ); - event.getPing().getPlayers().get().getSample().stream().map(ServerPing.SamplePlayer::getName).forEach(geyserPingInfo.getPlayerList()::add); - return geyserPingInfo; } private static class GeyserInboundConnection implements InboundConnection { diff --git a/core/src/main/java/org/geysermc/geyser/GeyserBootstrap.java b/core/src/main/java/org/geysermc/geyser/GeyserBootstrap.java index e4baeebb5..1827cfb36 100644 --- a/core/src/main/java/org/geysermc/geyser/GeyserBootstrap.java +++ b/core/src/main/java/org/geysermc/geyser/GeyserBootstrap.java @@ -25,6 +25,8 @@ package org.geysermc.geyser; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import org.geysermc.geyser.command.GeyserCommandManager; import org.geysermc.geyser.configuration.GeyserConfiguration; import org.geysermc.geyser.dump.BootstrapDumpInfo; @@ -32,8 +34,6 @@ import org.geysermc.geyser.level.GeyserWorldManager; import org.geysermc.geyser.level.WorldManager; import org.geysermc.geyser.ping.IGeyserPingPassthrough; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; import java.io.InputStream; import java.net.SocketAddress; import java.nio.file.Path; @@ -79,6 +79,7 @@ public interface GeyserBootstrap { * * @return The current PingPassthrough manager */ + @Nullable IGeyserPingPassthrough getGeyserPingPassthrough(); /** @@ -151,7 +152,7 @@ public interface GeyserBootstrap { * @param resource Resource to get * @return InputStream of the given resource */ - default @Nonnull InputStream getResource(String resource) { + default @NonNull InputStream getResource(String resource) { InputStream stream = getResourceOrNull(resource); if (stream == null) { throw new AssertionError("Unable to find resource: " + resource); @@ -162,7 +163,7 @@ public interface GeyserBootstrap { /** * @return the bind address being used by the Java server. */ - @Nonnull + @NonNull String getServerBindAddress(); /** diff --git a/core/src/main/java/org/geysermc/geyser/configuration/GeyserConfiguration.java b/core/src/main/java/org/geysermc/geyser/configuration/GeyserConfiguration.java index e36ec819b..51cd7d4ae 100644 --- a/core/src/main/java/org/geysermc/geyser/configuration/GeyserConfiguration.java +++ b/core/src/main/java/org/geysermc/geyser/configuration/GeyserConfiguration.java @@ -57,9 +57,6 @@ public interface GeyserConfiguration { @JsonIgnore boolean isPassthroughMotd(); - @JsonIgnore - boolean isPassthroughProtocolName(); - @JsonIgnore boolean isPassthroughPlayerCounts(); diff --git a/core/src/main/java/org/geysermc/geyser/configuration/GeyserJacksonConfiguration.java b/core/src/main/java/org/geysermc/geyser/configuration/GeyserJacksonConfiguration.java index 268304844..95ac7b1de 100644 --- a/core/src/main/java/org/geysermc/geyser/configuration/GeyserJacksonConfiguration.java +++ b/core/src/main/java/org/geysermc/geyser/configuration/GeyserJacksonConfiguration.java @@ -75,9 +75,6 @@ public abstract class GeyserJacksonConfiguration implements GeyserConfiguration @JsonProperty("passthrough-player-counts") private boolean isPassthroughPlayerCounts = false; - @JsonProperty("passthrough-protocol-name") - private boolean isPassthroughProtocolName = false; - @JsonProperty("legacy-ping-passthrough") private boolean isLegacyPingPassthrough = false; diff --git a/core/src/main/java/org/geysermc/geyser/network/QueryPacketHandler.java b/core/src/main/java/org/geysermc/geyser/network/QueryPacketHandler.java deleted file mode 100644 index 74e1430a2..000000000 --- a/core/src/main/java/org/geysermc/geyser/network/QueryPacketHandler.java +++ /dev/null @@ -1,303 +0,0 @@ -/* - * Copyright (c) 2019-2022 GeyserMC. http://geysermc.org - * - * 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: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * 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. - * - * @author GeyserMC - * @link https://github.com/GeyserMC/Geyser - */ - -package org.geysermc.geyser.network; - -import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufAllocator; -import org.geysermc.geyser.GeyserImpl; -import org.geysermc.geyser.ping.GeyserPingInfo; -import org.geysermc.geyser.translator.text.MessageTranslator; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.OutputStream; -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; - - private final GeyserImpl geyser; - private final InetSocketAddress sender; - private final byte type; - private final int sessionId; - private byte[] token; - - /** - * 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 geyser Geyser - * @param sender The Sender IP/Port for the Query - * @param buffer The Query data - */ - public QueryPacketHandler(GeyserImpl geyser, InetSocketAddress sender, ByteBuf buffer) { - this.geyser = geyser; - this.sender = sender; - this.type = buffer.readByte(); - this.sessionId = buffer.readInt(); - - regenerateToken(); - handle(); - } - - /** - * Checks the packet is in fact a query packet - * - * @param buffer Query data - * @return if the packet is a query packet - */ - 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; - } - - /** - * Handles the query - */ - private void handle() { - switch (type) { - case HANDSHAKE: - sendToken(); - break; - case STATISTICS: - sendQueryData(); - break; - } - } - - /** - * 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() { - byte[] gameData = getGameData(); - byte[] playerData = getPlayers(); - - ByteBuf reply = ByteBufAllocator.DEFAULT.ioBuffer(1 + 4 + gameData.length + playerData.length); - reply.writeByte(STATISTICS); - reply.writeInt(sessionId); - - // Game Info - reply.writeBytes(gameData); - - // Players - reply.writeBytes(playerData); - - sendPacket(reply); - } - - /** - * Gets the game data for the query - * - * @return the game data for the query - */ - private byte[] getGameData() { - ByteArrayOutputStream query = new ByteArrayOutputStream(); - - GeyserPingInfo pingInfo = null; - String motd; - String currentPlayerCount; - String maxPlayerCount; - String map; - - if (geyser.getConfig().isPassthroughMotd() || geyser.getConfig().isPassthroughPlayerCounts()) { - pingInfo = geyser.getBootstrap().getGeyserPingPassthrough().getPingInformation(); - } - - if (geyser.getConfig().isPassthroughMotd() && pingInfo != null) { - String[] javaMotd = MessageTranslator.convertMessageLenient(pingInfo.getDescription()).split("\n"); - motd = javaMotd[0].trim(); // First line of the motd. - } else { - motd = geyser.getConfig().getBedrock().primaryMotd(); - } - - // If passthrough player counts is enabled lets get players from the server - if (geyser.getConfig().isPassthroughPlayerCounts() && pingInfo != null) { - currentPlayerCount = String.valueOf(pingInfo.getPlayers().getOnline()); - maxPlayerCount = String.valueOf(pingInfo.getPlayers().getMax()); - } else { - currentPlayerCount = String.valueOf(geyser.getSessionManager().getSessions().size()); - maxPlayerCount = String.valueOf(geyser.getConfig().getMaxPlayers()); - } - - // If passthrough protocol name is enabled let's get the protocol name from the ping response. - if (geyser.getConfig().isPassthroughProtocolName() && pingInfo != null) { - map = pingInfo.getVersion().getName(); - } else { - map = GeyserImpl.NAME; - } - - // Create a hashmap of all game data needed in the query - Map gameData = new HashMap<>(); - gameData.put("hostname", motd); - gameData.put("gametype", "SMP"); - gameData.put("game_id", "MINECRAFT"); - gameData.put("version", GeyserImpl.NAME + " (" + GeyserImpl.GIT_VERSION + ") " + GameProtocol.DEFAULT_BEDROCK_CODEC.getMinecraftVersion()); - gameData.put("plugins", ""); - gameData.put("map", map); - gameData.put("numplayers", currentPlayerCount); - gameData.put("maxplayers", maxPlayerCount); - gameData.put("hostport", String.valueOf(geyser.getConfig().getBedrock().port())); - gameData.put("hostip", geyser.getConfig().getBedrock().address()); - - try { - writeString(query, "GeyserMC"); - query.write((byte) 0x80); - query.write((byte) 0x00); - - // Fills the game data - for (Map.Entry entry : gameData.entrySet()) { - writeString(query, entry.getKey()); - writeString(query, entry.getValue()); - } - - // Final byte to show the end of the game data - query.write(new byte[] { 0x00, 0x01 }); - return query.toByteArray(); - } catch (IOException e) { - e.printStackTrace(); - return new byte[0]; - } - } - - /** - * Generate a byte[] storing the player names - * - * @return The byte[] representation of players - */ - private byte[] getPlayers() { - ByteArrayOutputStream query = new ByteArrayOutputStream(); - - GeyserPingInfo pingInfo = null; - if (geyser.getConfig().isPassthroughMotd() || geyser.getConfig().isPassthroughPlayerCounts()) { - pingInfo = geyser.getBootstrap().getGeyserPingPassthrough().getPingInformation(); - } - - try { - // Start the player section - writeString(query, "player_"); - query.write((byte) 0x00); - - // Fill player names - if (pingInfo != null) { - for (String username : pingInfo.getPlayerList()) { - writeString(query, username); - } - } - - // 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]; - } - } - - /** - * 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 - * - * @param data packet data - */ - private void sendPacket(ByteBuf data) { - // geyser.getBedrockServer().getRakNet().send(sender, data); - } - - /** - * Regenerates a token - */ - public void regenerateToken() { - byte[] token = new byte[16]; - for (int i = 0; i < 16; i++) { - token[i] = (byte) ThreadLocalRandom.current().nextInt(255); - } - - 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. - * - * @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 { - // Generate an MD5 hash from the address - MessageDigest digest = MessageDigest.getInstance("MD5"); - digest.update(address.toString().getBytes(StandardCharsets.UTF_8)); - digest.update(token); - - // 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) { - return (ByteBuffer.allocate(4).putInt(ThreadLocalRandom.current().nextInt()).getInt() + "\0").getBytes(); - } - } -} diff --git a/core/src/main/java/org/geysermc/geyser/network/netty/GeyserServer.java b/core/src/main/java/org/geysermc/geyser/network/netty/GeyserServer.java index c89da0bdd..a811d8437 100644 --- a/core/src/main/java/org/geysermc/geyser/network/netty/GeyserServer.java +++ b/core/src/main/java/org/geysermc/geyser/network/netty/GeyserServer.java @@ -69,6 +69,7 @@ import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; import java.util.function.IntFunction; +import java.util.function.Supplier; public final class GeyserServer { private static final boolean PRINT_DEBUG_PINGS = Boolean.parseBoolean(System.getProperty("Geyser.PrintPingsInDebugMode", "true")); @@ -163,8 +164,9 @@ public final class GeyserServer { if (System.getProperties().contains("disableNativeEventLoop")) { this.geyser.getLogger().debug("EventLoop type is NIO because native event loops are disabled."); } else { - this.geyser.getLogger().debug("Reason for no Epoll: " + Epoll.unavailabilityCause().toString()); - this.geyser.getLogger().debug("Reason for no KQueue: " + KQueue.unavailabilityCause().toString()); + // Use lambda here, not method reference, or else NoClassDefFoundError for Epoll/KQueue will not be caught + this.geyser.getLogger().debug("Reason for no Epoll: " + throwableOrCaught(() -> Epoll.unavailabilityCause())); + this.geyser.getLogger().debug("Reason for no KQueue: " + throwableOrCaught(() -> KQueue.unavailabilityCause())); } } } @@ -230,7 +232,9 @@ public final class GeyserServer { GeyserPingInfo pingInfo = null; if (config.isPassthroughMotd() || config.isPassthroughPlayerCounts()) { IGeyserPingPassthrough pingPassthrough = geyser.getBootstrap().getGeyserPingPassthrough(); - pingInfo = pingPassthrough.getPingInformation(inetSocketAddress); + if (pingPassthrough != null) { + pingInfo = pingPassthrough.getPingInformation(inetSocketAddress); + } } BedrockPong pong = new BedrockPong() @@ -312,6 +316,17 @@ public final class GeyserServer { return pong; } + /** + * @return the throwable from the given supplier, or the throwable caught while calling the supplier. + */ + private static Throwable throwableOrCaught(Supplier supplier) { + try { + return supplier.get(); + } catch (Throwable throwable) { + return throwable; + } + } + private static Transport compatibleTransport() { TransportHelper.TransportMethod transportMethod = TransportHelper.determineTransportMethod(); if (transportMethod == TransportHelper.TransportMethod.EPOLL) { diff --git a/core/src/main/java/org/geysermc/geyser/ping/GeyserPingInfo.java b/core/src/main/java/org/geysermc/geyser/ping/GeyserPingInfo.java index d444e554c..9d8da114d 100644 --- a/core/src/main/java/org/geysermc/geyser/ping/GeyserPingInfo.java +++ b/core/src/main/java/org/geysermc/geyser/ping/GeyserPingInfo.java @@ -25,34 +25,37 @@ package org.geysermc.geyser.ping; -import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonSetter; import com.fasterxml.jackson.databind.JsonNode; import lombok.Data; +import org.checkerframework.checker.nullness.qual.Nullable; -import java.util.ArrayList; -import java.util.Collection; - +/** + * The structure of this class and its nested classes are specifically + * designed for the format received by {@link GeyserLegacyPingPassthrough}. + */ @Data @JsonIgnoreProperties(ignoreUnknown = true) public class GeyserPingInfo { + @Nullable private String description; private Players players; - private Version version; - - @JsonIgnore - private Collection playerList = new ArrayList<>(); public GeyserPingInfo() { + // for json mapping } - public GeyserPingInfo(String description, Players players, Version version) { + public GeyserPingInfo(@Nullable String description, Players players) { this.description = description; this.players = players; - this.version = version; + } + + public GeyserPingInfo(@Nullable String description, int maxPlayers, int onlinePlayers) { + this.description = description; + this.players = new Players(maxPlayers, onlinePlayers); } @JsonSetter("description") @@ -68,6 +71,7 @@ public class GeyserPingInfo { private int online; public Players() { + // for json mapping } public Players(int max, int online) { @@ -75,19 +79,4 @@ public class GeyserPingInfo { this.online = online; } } - - @Data - public static class Version { - - private String name; - private int protocol; - - public Version() { - } - - public Version(String name, int protocol) { - this.name = name; - this.protocol = protocol; - } - } } diff --git a/core/src/main/java/org/geysermc/geyser/ping/IGeyserPingPassthrough.java b/core/src/main/java/org/geysermc/geyser/ping/IGeyserPingPassthrough.java index d414b7fa8..b6095cce2 100644 --- a/core/src/main/java/org/geysermc/geyser/ping/IGeyserPingPassthrough.java +++ b/core/src/main/java/org/geysermc/geyser/ping/IGeyserPingPassthrough.java @@ -26,7 +26,6 @@ package org.geysermc.geyser.ping; import javax.annotation.Nullable; -import java.net.Inet4Address; import java.net.InetSocketAddress; /** @@ -34,16 +33,6 @@ import java.net.InetSocketAddress; */ public interface IGeyserPingPassthrough { - /** - * Get the MOTD of the server displayed on the multiplayer screen. It uses a fake remote, as the remote isn't important in this context. - * - * @return string of the MOTD - */ - @Nullable - default GeyserPingInfo getPingInformation() { - return this.getPingInformation(new InetSocketAddress(Inet4Address.getLoopbackAddress(), 69)); - } - /** * Get the MOTD of the server displayed on the multiplayer screen * diff --git a/core/src/main/resources/config.yml b/core/src/main/resources/config.yml index 8e4db5e38..218e13833 100644 --- a/core/src/main/resources/config.yml +++ b/core/src/main/resources/config.yml @@ -91,9 +91,6 @@ command-suggestions: true # The following three options enable "ping passthrough" - the MOTD, player count and/or protocol name gets retrieved from the Java server. # Relay the MOTD from the remote server to Bedrock players. passthrough-motd: false -# Relay the protocol name (e.g. BungeeCord [X.X], Paper 1.X) - only really useful when using a custom protocol name! -# This will also show up on sites like MCSrvStatus. -passthrough-protocol-name: false # Relay the player count and max players from the remote server to Bedrock players. passthrough-player-counts: false # Enable LEGACY ping passthrough. There is no need to enable this unless your MOTD or player count does not appear properly.