From be233c65fe1ca053644bd764f4c4b5f6a1a92895 Mon Sep 17 00:00:00 2001 From: YoyoNow Date: Sat, 16 May 2026 11:18:40 +0200 Subject: [PATCH] Update TinyProtocol --- .../comphenix/tinyprotocol/TinyProtocol.java | 637 +++++++++++++----- 1 file changed, 464 insertions(+), 173 deletions(-) diff --git a/SpigotCore/SpigotCore_Main/src/com/comphenix/tinyprotocol/TinyProtocol.java b/SpigotCore/SpigotCore_Main/src/com/comphenix/tinyprotocol/TinyProtocol.java index 8140400c..b47d54f0 100644 --- a/SpigotCore/SpigotCore_Main/src/com/comphenix/tinyprotocol/TinyProtocol.java +++ b/SpigotCore/SpigotCore_Main/src/com/comphenix/tinyprotocol/TinyProtocol.java @@ -19,14 +19,11 @@ package com.comphenix.tinyprotocol; +import com.google.common.collect.MapMaker; +import com.mojang.authlib.GameProfile; import de.steamwar.Reflection; import de.steamwar.Reflection.Field; -import de.steamwar.core.Core; -import io.netty.channel.Channel; -import io.netty.channel.ChannelDuplexHandler; -import io.netty.channel.ChannelHandlerContext; -import io.netty.channel.ChannelPromise; -import lombok.Getter; +import io.netty.channel.*; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -34,190 +31,490 @@ import org.bukkit.event.EventPriority; import org.bukkit.event.HandlerList; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerLoginEvent; -import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.server.PluginDisableEvent; import org.bukkit.plugin.Plugin; +import org.bukkit.scheduler.BukkitRunnable; import java.util.*; -import java.util.concurrent.CopyOnWriteArrayList; -import java.util.function.BiFunction; +import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Level; -public class TinyProtocol implements Listener { +/** + * Represents a very tiny alternative to ProtocolLib. + *

+ * It now supports intercepting packets during login and status ping (such as OUT_SERVER_PING)! + * + * @author Kristian + */ +public abstract class TinyProtocol { + private static final AtomicInteger ID = new AtomicInteger(0); - private static final Class craftServer = Reflection.getClass("org.bukkit.craftbukkit.CraftServer"); - private static final Class dedicatedPlayerList = Reflection.getClass("net.minecraft.server.dedicated.DedicatedPlayerList"); - private static final Field getPlayerList = Reflection.getField(craftServer, dedicatedPlayerList, 0); - private static final Class playerList = Reflection.getClass("net.minecraft.server.players.PlayerList"); - private static final Class minecraftServer = Reflection.getClass("net.minecraft.server.MinecraftServer"); - private static final Field getMinecraftServer = Reflection.getField(playerList, minecraftServer, 0); - public static final Class serverConnection = Reflection.getClass("net.minecraft.server.network.ServerConnectionListener"); - private static final Field getServerConnection = Reflection.getField(minecraftServer, serverConnection, 0); - public static Object getServerConnection(Plugin plugin) { - return getServerConnection.get(getMinecraftServer.get(getPlayerList.get(plugin.getServer()))); - } - private static final Class networkManager = Reflection.getClass("net.minecraft.network.NetworkManager"); - public static final Field networkManagers = Reflection.getField(serverConnection, List.class, 0, networkManager); + // Required Minecraft classes + private static final Class entityPlayerClass = Reflection.getClass("{nms}.EntityPlayer", "net.minecraft.server.level.EntityPlayer"); + private static final Class playerConnectionClass = Reflection.getClass("{nms}.PlayerConnection", "net.minecraft.server.network.PlayerConnection"); + private static final Class networkManagerClass = Reflection.getClass("{nms}.NetworkManager", "net.minecraft.network.NetworkManager"); - private static final String HANDLER_NAME = "tiny-steamwar"; - public static final TinyProtocol instance = new TinyProtocol(Core.getInstance()); + // Used in order to lookup a channel + private static final MethodInvoker getPlayerHandle = Reflection.getMethod("{obc}.entity.CraftPlayer", "getHandle"); + private static final FieldAccessor getConnection = Reflection.getField(entityPlayerClass, null, playerConnectionClass); + private static final FieldAccessor getManager = Reflection.getField(playerConnectionClass, null, networkManagerClass); + private static final FieldAccessor getChannel = Reflection.getField(networkManagerClass, Channel.class, 0); - public static void init() { - //enforce init - } + // Looking up ServerConnection + private static final Class minecraftServerClass = Reflection.getUntypedClass("{nms}.MinecraftServer", "net.minecraft.server.MinecraftServer"); + private static final Class serverConnectionClass = Reflection.getUntypedClass("{nms}.ServerConnection", "net.minecraft.server.network.ServerConnection"); + private static final FieldAccessor getMinecraftServer = Reflection.getField("{obc}.CraftServer", minecraftServerClass, 0); + private static final FieldAccessor getServerConnection = Reflection.getField(minecraftServerClass, serverConnectionClass, 0); - private final Plugin plugin; - private final List connections; - private boolean closed; + // Packets we have to intercept + private static final Class PACKET_LOGIN_IN_START = Reflection.getClass("{nms}.PacketLoginInStart", "net.minecraft.network.protocol.login.PacketLoginInStart"); + private static final FieldAccessor getGameProfile = Reflection.getField(PACKET_LOGIN_IN_START, GameProfile.class, 0); - private final Map, List>> packetFilters = new HashMap<>(); - @Getter - private final Map playerInterceptors = new HashMap<>(); + // Speedup channel lookup + private Map channelLookup = new MapMaker().weakValues().makeMap(); + private Listener listener; - @Override - public String toString() { - return "TinyProtocol{" + - "plugin=" + plugin + - ", connections=" + connections + - ", closed=" + closed + - ", packetFilters=" + packetFilters + - ", playerInterceptors=" + playerInterceptors + - '}'; - } + // Channels that have already been removed + private Set uninjectedChannels = Collections.newSetFromMap(new MapMaker().weakKeys().makeMap()); - private TinyProtocol(final Plugin plugin) { + // List of network markers + private List networkManagers; + + // Injected channel handlers + private List serverChannels = new ArrayList<>(); + private ChannelInboundHandlerAdapter serverChannelHandler; + private ChannelInitializer beginInitProtocol; + private ChannelInitializer endInitProtocol; + + // Current handler name + private String handlerName; + + protected volatile boolean closed; + protected Plugin plugin; + + /** + * Construct a new instance of TinyProtocol, and start intercepting packets for all connected clients and future clients. + *

+ * You can construct multiple instances per plugin. + * + * @param plugin - the plugin. + */ + public TinyProtocol(final Plugin plugin) { this.plugin = plugin; - this.connections = networkManagers.get(getServerConnection(plugin)); - plugin.getServer().getPluginManager().registerEvents(this, plugin); + // Compute handler name + this.handlerName = getHandlerName(); - for (Player player : plugin.getServer().getOnlinePlayers()) { - new PacketInterceptor(player); + // Prepare existing players + registerBukkitEvents(); + + try { + registerChannelHandler(); + registerPlayers(plugin); + } catch (IllegalArgumentException ex) { + // Damn you, late bind + plugin.getLogger().info("[TinyProtocol] Delaying server channel injection due to late bind."); + + new BukkitRunnable() { + @Override + public void run() { + registerChannelHandler(); + registerPlayers(plugin); + plugin.getLogger().info("[TinyProtocol] Late bind injection successful."); + } + }.runTask(plugin); } } - @EventHandler(priority = EventPriority.LOWEST) - public void onPlayerLogin(PlayerLoginEvent e) { - plugin.getLogger().info("Creating PacketInterceptor for: " + e.getPlayer().getName() + " (" + closed + ")"); - if(closed) - return; - new PacketInterceptor(e.getPlayer()); - } + private void createServerChannelHandler() { + // Handle connected channels + endInitProtocol = new ChannelInitializer() { - @EventHandler(priority = EventPriority.MONITOR) - public void onPlayerDisconnect(PlayerQuitEvent e) { - getInterceptor(e.getPlayer()).ifPresent(PacketInterceptor::close); - } - - @EventHandler - public void onPluginDisable(PluginDisableEvent e) { - if (e.getPlugin().equals(plugin)) { - close(); - } - } - - public void addTypedFilter(Class packetType, BiFunction filter) { - packetFilters.computeIfAbsent(packetType, c -> new CopyOnWriteArrayList<>()).add((BiFunction) filter); - } - - public void addFilter(Class packetType, BiFunction filter) { - packetFilters.computeIfAbsent(packetType, c -> new CopyOnWriteArrayList<>()).add(filter); - } - - public void removeFilter(Class packetType, BiFunction filter) { - packetFilters.getOrDefault(packetType, Collections.emptyList()).remove(filter); - } - - public void sendPacket(Player player, Object packet) { - getInterceptor(player).ifPresent(i -> i.sendPacket(packet)); - } - - public void receivePacket(Player player, Object packet) { - getInterceptor(player).ifPresent(i -> i.receivePacket(packet)); - } - - public final void close() { - plugin.getLogger().log(Level.INFO, "Closing PacketInterceptor", new Exception("Stacktrace")); - - if(closed) - return; - closed = true; - - HandlerList.unregisterAll(this); - - for (Player player : plugin.getServer().getOnlinePlayers()) { - getInterceptor(player).ifPresent(PacketInterceptor::close); - } - } - - private Optional getInterceptor(Player player) { - synchronized (playerInterceptors) { - return Optional.ofNullable(playerInterceptors.get(player)); - } - } - - private static final Field getChannel = Reflection.getField(networkManager, Channel.class, 0); - private static final Field getUUID = Reflection.getField(networkManager, UUID.class, 0); - - public final class PacketInterceptor extends ChannelDuplexHandler { - private final Player player; - @Getter - private final Channel channel; - - private PacketInterceptor(Player player) { - this.player = player; - - channel = connections.stream().filter(connection -> player.getUniqueId().equals(getUUID.get(connection))).map(getChannel::get).filter(Channel::isActive).findAny().orElseThrow(() -> { - Bukkit.getScheduler().runTask(plugin, () -> player.kickPlayer("Connection failure.")); - return new SecurityException("Could not find channel for player " + player.getName()); - }); - - if(!channel.isActive()) - return; - - synchronized (playerInterceptors) { - playerInterceptors.put(player, this); - } - plugin.getLogger().info("Adding Techhider for: " + player.getName()); - - try { - channel.pipeline().addBefore("packet_handler", HANDLER_NAME, this); - } catch (IllegalArgumentException | NoSuchElementException e) { - Bukkit.getScheduler().runTask(plugin, () -> player.kickPlayer("Connection failure.")); - throw new SecurityException(e); - } - } - - private void sendPacket(Object packet) { - channel.pipeline().writeAndFlush(packet); - } - - private void receivePacket(Object packet) { - channel.pipeline().context("encoder").fireChannelRead(packet); - } - - private void close() { - if(channel.isActive()) { - channel.eventLoop().execute(() -> { - try { - channel.pipeline().remove(HANDLER_NAME); - } catch (NoSuchElementException e) { - // ignore + @Override + protected void initChannel(Channel channel) throws Exception { + try { + // This can take a while, so we need to stop the main thread from interfering + synchronized (networkManagers) { + // Stop injecting channels + if (!closed) { + channel.eventLoop().submit(() -> injectChannelInternal(channel)); + } } - }); + } catch (Exception e) { + plugin.getLogger().log(Level.SEVERE, "Cannot inject incomming channel " + channel, e); + } } - synchronized (playerInterceptors) { - playerInterceptors.remove(player, this); + }; + + // This is executed before Minecraft's channel handler + beginInitProtocol = new ChannelInitializer() { + + @Override + protected void initChannel(Channel channel) throws Exception { + channel.pipeline().addLast(endInitProtocol); + } + + }; + + serverChannelHandler = new ChannelInboundHandlerAdapter() { + + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + Channel channel = (Channel) msg; + + // Prepare to initialize ths channel + channel.pipeline().addFirst(beginInitProtocol); + ctx.fireChannelRead(msg); + } + + }; + } + + /** + * Register bukkit events. + */ + private void registerBukkitEvents() { + listener = new Listener() { + + @EventHandler(priority = EventPriority.LOWEST) + public final void onPlayerLogin(PlayerLoginEvent e) { + if (closed) + return; + + Channel channel = getChannel(e.getPlayer()); + + // Don't inject players that have been explicitly uninjected + if (!uninjectedChannels.contains(channel)) { + injectPlayer(e.getPlayer()); + } + } + + @EventHandler + public final void onPluginDisable(PluginDisableEvent e) { + if (e.getPlugin().equals(plugin)) { + close(); + } + } + + }; + + plugin.getServer().getPluginManager().registerEvents(listener, plugin); + } + + @SuppressWarnings("unchecked") + private void registerChannelHandler() { + Object mcServer = getMinecraftServer.get(Bukkit.getServer()); + Object serverConnection = getServerConnection.get(mcServer); + boolean looking = true; + + try { + Field field = Reflection.getParameterizedField(serverConnectionClass, List.class, networkManagerClass); + field.setAccessible(true); + + networkManagers = (List) field.get(serverConnection); + } catch (Exception ex) { + plugin.getLogger().info("Encountered an exception checking list fields" + ex); + MethodInvoker method = Reflection.getTypedMethod(serverConnectionClass, null, List.class, serverConnectionClass); + + networkManagers = (List) method.invoke(null, serverConnection); + } + + if (networkManagers == null) { + throw new IllegalArgumentException("Failed to obtain list of network managers"); + } + // We need to synchronize against this list + createServerChannelHandler(); + + // Find the correct list, or implicitly throw an exception + for (int i = 0; looking; i++) { + List list = Reflection.getField(serverConnection.getClass(), List.class, i).get(serverConnection); + + for (Object item : list) { + if (!ChannelFuture.class.isInstance(item)) + break; + + // Channel future that contains the server connection + Channel serverChannel = ((ChannelFuture) item).channel(); + + serverChannels.add(serverChannel); + serverChannel.pipeline().addFirst(serverChannelHandler); + looking = false; } } + } + + private void unregisterChannelHandler() { + if (serverChannelHandler == null) + return; + + for (Channel serverChannel : serverChannels) { + final ChannelPipeline pipeline = serverChannel.pipeline(); + + // Remove channel handler + serverChannel.eventLoop().execute(new Runnable() { + + @Override + public void run() { + try { + pipeline.remove(serverChannelHandler); + } catch (NoSuchElementException e) { + // That's fine + } + } + + }); + } + } + + private void registerPlayers(Plugin plugin) { + for (Player player : plugin.getServer().getOnlinePlayers()) { + injectPlayer(player); + } + } + + /** + * Invoked when the server is starting to send a packet to a player. + *

+ * Note that this is not executed on the main thread. + * + * @param receiver - the receiving player, NULL for early login/status packets. + * @param channel - the channel that received the packet. Never NULL. + * @param packet - the packet being sent. + * @return The packet to send instead, or NULL to cancel the transmission. + */ + public Object onPacketOutAsync(Player receiver, Channel channel, Object packet) { + return packet; + } + + /** + * Invoked when the server has received a packet from a given player. + *

+ * Use {@link Channel#remoteAddress()} to get the remote address of the client. + * + * @param sender - the player that sent the packet, NULL for early login/status packets. + * @param channel - channel that received the packet. Never NULL. + * @param packet - the packet being received. + * @return The packet to recieve instead, or NULL to cancel. + */ + public Object onPacketInAsync(Player sender, Channel channel, Object packet) { + return packet; + } + + /** + * Send a packet to a particular player. + *

+ * Note that {@link #onPacketOutAsync(Player, Channel, Object)} will be invoked with this packet. + * + * @param player - the destination player. + * @param packet - the packet to send. + */ + public void sendPacket(Player player, Object packet) { + sendPacket(getChannel(player), packet); + } + + /** + * Send a packet to a particular client. + *

+ * Note that {@link #onPacketOutAsync(Player, Channel, Object)} will be invoked with this packet. + * + * @param channel - client identified by a channel. + * @param packet - the packet to send. + */ + public void sendPacket(Channel channel, Object packet) { + channel.pipeline().writeAndFlush(packet); + } + + /** + * Pretend that a given packet has been received from a player. + *

+ * Note that {@link #onPacketInAsync(Player, Channel, Object)} will be invoked with this packet. + * + * @param player - the player that sent the packet. + * @param packet - the packet that will be received by the server. + */ + public void receivePacket(Player player, Object packet) { + receivePacket(getChannel(player), packet); + } + + /** + * Pretend that a given packet has been received from a given client. + *

+ * Note that {@link #onPacketInAsync(Player, Channel, Object)} will be invoked with this packet. + * + * @param channel - client identified by a channel. + * @param packet - the packet that will be received by the server. + */ + public void receivePacket(Channel channel, Object packet) { + channel.pipeline().context("encoder").fireChannelRead(packet); + } + + /** + * Retrieve the name of the channel injector, default implementation is "tiny-" + plugin name + "-" + a unique ID. + *

+ * Note that this method will only be invoked once. It is no longer necessary to override this to support multiple instances. + * + * @return A unique channel handler name. + */ + protected String getHandlerName() { + return "tiny-" + plugin.getName() + "-" + ID.incrementAndGet(); + } + + /** + * Add a custom channel handler to the given player's channel pipeline, allowing us to intercept sent and received packets. + *

+ * This will automatically be called when a player has logged in. + * + * @param player - the player to inject. + */ + public void injectPlayer(Player player) { + injectChannelInternal(getChannel(player)).player = player; + } + + /** + * Add a custom channel handler to the given channel. + * + * @param channel - the channel to inject. + * @return The intercepted channel, or NULL if it has already been injected. + */ + public void injectChannel(Channel channel) { + injectChannelInternal(channel); + } + + /** + * Add a custom channel handler to the given channel. + * + * @param channel - the channel to inject. + * @return The packet interceptor. + */ + private PacketInterceptor injectChannelInternal(Channel channel) { + try { + PacketInterceptor interceptor = (PacketInterceptor) channel.pipeline().get(handlerName); + + // Inject our packet interceptor + if (interceptor == null) { + interceptor = new PacketInterceptor(); + channel.pipeline().addBefore("packet_handler", handlerName, interceptor); + uninjectedChannels.remove(channel); + } + + return interceptor; + } catch (IllegalArgumentException e) { + // Try again + return (PacketInterceptor) channel.pipeline().get(handlerName); + } + } + + /** + * Retrieve the Netty channel associated with a player. This is cached. + * + * @param player - the player. + * @return The Netty channel. + */ + public Channel getChannel(Player player) { + Channel channel = channelLookup.get(player.getName()); + + // Lookup channel again + if (channel == null) { + Object connection = getConnection.get(getPlayerHandle.invoke(player)); + Object manager = getManager.get(connection); + + channelLookup.put(player.getName(), channel = getChannel.get(manager)); + } + + return channel; + } + + /** + * Uninject a specific player. + * + * @param player - the injected player. + */ + public void uninjectPlayer(Player player) { + uninjectChannel(getChannel(player)); + } + + /** + * Uninject a specific channel. + *

+ * This will also disable the automatic channel injection that occurs when a player has properly logged in. + * + * @param channel - the injected channel. + */ + public void uninjectChannel(final Channel channel) { + // No need to guard against this if we're closing + if (!closed) { + uninjectedChannels.add(channel); + } + + // See ChannelInjector in ProtocolLib, line 590 + channel.eventLoop().execute(new Runnable() { + + @Override + public void run() { + channel.pipeline().remove(handlerName); + } + + }); + } + + /** + * Determine if the given player has been injected by TinyProtocol. + * + * @param player - the player. + * @return TRUE if it is, FALSE otherwise. + */ + public boolean hasInjected(Player player) { + return hasInjected(getChannel(player)); + } + + /** + * Determine if the given channel has been injected by TinyProtocol. + * + * @param channel - the channel. + * @return TRUE if it is, FALSE otherwise. + */ + public boolean hasInjected(Channel channel) { + return channel.pipeline().get(handlerName) != null; + } + + /** + * Cease listening for packets. This is called automatically when your plugin is disabled. + */ + public final void close() { + if (!closed) { + closed = true; + + // Remove our handlers + for (Player player : plugin.getServer().getOnlinePlayers()) { + uninjectPlayer(player); + } + + // Clean up Bukkit + HandlerList.unregisterAll(listener); + unregisterChannelHandler(); + } + } + + /** + * Channel handler that is inserted into the player's channel pipeline, allowing us to intercept sent and received packets. + * + * @author Kristian + */ + private final class PacketInterceptor extends ChannelDuplexHandler { + // Updated by the login event + public volatile Player player; @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + // Intercept channel + final Channel channel = ctx.channel(); + handleLoginStart(channel, msg); + try { - msg = filterPacket(player, msg); + msg = onPacketInAsync(player, channel, msg); } catch (Exception e) { - plugin.getLogger().log(Level.SEVERE, "Error during incoming packet processing", e); + plugin.getLogger().log(Level.SEVERE, "Error in onPacketInAsync().", e); } if (msg != null) { @@ -228,9 +525,9 @@ public class TinyProtocol implements Listener { @Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { try { - msg = filterPacket(player, msg); + msg = onPacketOutAsync(player, ctx.channel(), msg); } catch (Exception e) { - plugin.getLogger().log(Level.SEVERE, "Error during outgoing packet processing", e); + plugin.getLogger().log(Level.SEVERE, "Error in onPacketOutAsync().", e); } if (msg != null) { @@ -238,17 +535,11 @@ public class TinyProtocol implements Listener { } } - private Object filterPacket(Player player, Object packet) { - List> filters = packetFilters.getOrDefault(packet.getClass(), Collections.emptyList()); - - for(BiFunction filter : filters) { - packet = filter.apply(player, packet); - - if(packet == null) - break; + private void handleLoginStart(Channel channel, Object packet) { + if (PACKET_LOGIN_IN_START.isInstance(packet)) { + GameProfile profile = getGameProfile.get(packet); + channelLookup.put(profile.getName(), channel); } - - return packet; } } -} +} \ No newline at end of file