2013-12-21 19:37:02 +01:00
|
|
|
package com.comphenix.tinyprotocol;
|
|
|
|
|
2014-12-08 22:24:27 +01:00
|
|
|
import io.netty.channel.Channel;
|
|
|
|
import io.netty.channel.ChannelDuplexHandler;
|
|
|
|
import io.netty.channel.ChannelFuture;
|
|
|
|
import io.netty.channel.ChannelHandlerContext;
|
|
|
|
import io.netty.channel.ChannelInboundHandlerAdapter;
|
|
|
|
import io.netty.channel.ChannelInitializer;
|
|
|
|
import io.netty.channel.ChannelPipeline;
|
|
|
|
import io.netty.channel.ChannelPromise;
|
|
|
|
|
2023-05-07 00:26:49 +02:00
|
|
|
import java.lang.reflect.Field;
|
2014-05-17 17:10:17 +02:00
|
|
|
import java.util.Collections;
|
2014-04-10 01:37:43 +02:00
|
|
|
import java.util.List;
|
2014-01-28 00:58:57 +01:00
|
|
|
import java.util.Map;
|
2014-05-21 23:04:29 +02:00
|
|
|
import java.util.NoSuchElementException;
|
2014-05-17 17:10:17 +02:00
|
|
|
import java.util.Set;
|
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
2014-01-28 00:58:57 +01:00
|
|
|
import java.util.logging.Level;
|
2013-12-21 19:37:02 +01:00
|
|
|
|
2014-04-10 01:37:43 +02:00
|
|
|
import org.bukkit.Bukkit;
|
2013-12-21 19:37:02 +01:00
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.event.EventHandler;
|
2014-01-28 00:58:57 +01:00
|
|
|
import org.bukkit.event.EventPriority;
|
2014-02-01 16:38:24 +01:00
|
|
|
import org.bukkit.event.HandlerList;
|
2013-12-21 19:37:02 +01:00
|
|
|
import org.bukkit.event.Listener;
|
2014-04-10 01:37:43 +02:00
|
|
|
import org.bukkit.event.player.PlayerLoginEvent;
|
2013-12-21 19:37:02 +01:00
|
|
|
import org.bukkit.event.server.PluginDisableEvent;
|
|
|
|
import org.bukkit.plugin.Plugin;
|
2015-09-23 03:48:55 +02:00
|
|
|
import org.bukkit.scheduler.BukkitRunnable;
|
2013-12-21 19:37:02 +01:00
|
|
|
|
2014-02-01 16:38:24 +01:00
|
|
|
import com.comphenix.tinyprotocol.Reflection.FieldAccessor;
|
|
|
|
import com.comphenix.tinyprotocol.Reflection.MethodInvoker;
|
2014-04-10 01:37:43 +02:00
|
|
|
import com.google.common.collect.Lists;
|
2014-01-28 00:58:57 +01:00
|
|
|
import com.google.common.collect.MapMaker;
|
2014-12-08 22:24:27 +01:00
|
|
|
import com.mojang.authlib.GameProfile;
|
2013-12-21 19:37:02 +01:00
|
|
|
|
2014-02-01 16:38:24 +01:00
|
|
|
/**
|
2015-02-17 22:53:09 +01:00
|
|
|
* Represents a very tiny alternative to ProtocolLib.
|
2014-02-01 16:38:24 +01:00
|
|
|
* <p>
|
2014-04-10 01:37:43 +02:00
|
|
|
* It now supports intercepting packets during login and status ping (such as OUT_SERVER_PING)!
|
2015-02-17 22:53:09 +01:00
|
|
|
*
|
2014-02-01 16:38:24 +01:00
|
|
|
* @author Kristian
|
|
|
|
*/
|
2014-05-17 17:10:17 +02:00
|
|
|
public abstract class TinyProtocol {
|
|
|
|
private static final AtomicInteger ID = new AtomicInteger(0);
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2023-05-07 00:26:49 +02:00
|
|
|
// 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");
|
|
|
|
|
2014-01-28 00:58:57 +01:00
|
|
|
// Used in order to lookup a channel
|
2014-04-10 01:37:43 +02:00
|
|
|
private static final MethodInvoker getPlayerHandle = Reflection.getMethod("{obc}.entity.CraftPlayer", "getHandle");
|
2023-05-07 00:26:49 +02:00
|
|
|
private static final FieldAccessor<?> getConnection = Reflection.getField(entityPlayerClass, null, playerConnectionClass);
|
|
|
|
private static final FieldAccessor<?> getManager = Reflection.getField(playerConnectionClass, null, networkManagerClass);
|
|
|
|
private static final FieldAccessor<Channel> getChannel = Reflection.getField(networkManagerClass, Channel.class, 0);
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2014-04-10 01:37:43 +02:00
|
|
|
// Looking up ServerConnection
|
2023-05-07 00:26:49 +02:00
|
|
|
private static final Class<Object> minecraftServerClass = Reflection.getUntypedClass("{nms}.MinecraftServer", "net.minecraft.server.MinecraftServer");
|
|
|
|
private static final Class<Object> serverConnectionClass = Reflection.getUntypedClass("{nms}.ServerConnection", "net.minecraft.server.network.ServerConnection");
|
2014-04-10 01:37:43 +02:00
|
|
|
private static final FieldAccessor<Object> getMinecraftServer = Reflection.getField("{obc}.CraftServer", minecraftServerClass, 0);
|
|
|
|
private static final FieldAccessor<Object> getServerConnection = Reflection.getField(minecraftServerClass, serverConnectionClass, 0);
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2014-04-10 01:37:43 +02:00
|
|
|
// Packets we have to intercept
|
2023-05-07 00:26:49 +02:00
|
|
|
private static final Class<?> PACKET_LOGIN_IN_START = Reflection.getClass("{nms}.PacketLoginInStart", "net.minecraft.network.protocol.login.PacketLoginInStart");
|
2014-04-10 01:37:43 +02:00
|
|
|
private static final FieldAccessor<GameProfile> getGameProfile = Reflection.getField(PACKET_LOGIN_IN_START, GameProfile.class, 0);
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2014-01-28 00:58:57 +01:00
|
|
|
// Speedup channel lookup
|
2014-05-17 17:10:17 +02:00
|
|
|
private Map<String, Channel> channelLookup = new MapMaker().weakValues().makeMap();
|
2014-02-01 16:38:24 +01:00
|
|
|
private Listener listener;
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2014-05-17 17:10:17 +02:00
|
|
|
// Channels that have already been removed
|
|
|
|
private Set<Channel> uninjectedChannels = Collections.newSetFromMap(new MapMaker().weakKeys().<Channel, Boolean>makeMap());
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2014-04-10 01:37:43 +02:00
|
|
|
// List of network markers
|
|
|
|
private List<Object> networkManagers;
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2014-04-10 01:37:43 +02:00
|
|
|
// Injected channel handlers
|
2022-06-25 17:32:42 +02:00
|
|
|
private List<Channel> serverChannels = new ArrayList<>();
|
2014-04-10 01:37:43 +02:00
|
|
|
private ChannelInboundHandlerAdapter serverChannelHandler;
|
|
|
|
private ChannelInitializer<Channel> beginInitProtocol;
|
|
|
|
private ChannelInitializer<Channel> endInitProtocol;
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2014-05-17 17:10:17 +02:00
|
|
|
// Current handler name
|
|
|
|
private String handlerName;
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2014-04-10 01:37:43 +02:00
|
|
|
protected volatile boolean closed;
|
2014-02-01 16:38:24 +01:00
|
|
|
protected Plugin plugin;
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2014-05-17 17:10:17 +02:00
|
|
|
/**
|
2014-12-08 22:24:27 +01:00
|
|
|
* Construct a new instance of TinyProtocol, and start intercepting packets for all connected clients and future clients.
|
2014-05-17 17:10:17 +02:00
|
|
|
* <p>
|
|
|
|
* You can construct multiple instances per plugin.
|
2015-02-17 22:53:09 +01:00
|
|
|
*
|
2014-05-17 17:10:17 +02:00
|
|
|
* @param plugin - the plugin.
|
|
|
|
*/
|
2015-09-23 03:48:55 +02:00
|
|
|
public TinyProtocol(final Plugin plugin) {
|
2013-12-21 19:37:02 +01:00
|
|
|
this.plugin = plugin;
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2014-05-17 17:10:17 +02:00
|
|
|
// Compute handler name
|
|
|
|
this.handlerName = getHandlerName();
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2013-12-21 19:37:02 +01:00
|
|
|
// Prepare existing players
|
2014-05-17 17:10:17 +02:00
|
|
|
registerBukkitEvents();
|
2015-09-23 03:48:55 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
registerChannelHandler();
|
|
|
|
registerPlayers(plugin);
|
|
|
|
} catch (IllegalArgumentException ex) {
|
|
|
|
// Damn you, late bind
|
|
|
|
plugin.getLogger().info("[TinyProtocol] Delaying server channel injection due to late bind.");
|
2017-06-14 20:30:31 +02:00
|
|
|
|
2015-09-23 03:48:55 +02:00
|
|
|
new BukkitRunnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
registerChannelHandler();
|
|
|
|
registerPlayers(plugin);
|
|
|
|
plugin.getLogger().info("[TinyProtocol] Late bind injection successful.");
|
|
|
|
}
|
|
|
|
}.runTask(plugin);
|
|
|
|
}
|
2014-04-10 01:37:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void createServerChannelHandler() {
|
2015-02-17 22:53:09 +01:00
|
|
|
// Handle connected channels
|
|
|
|
endInitProtocol = new ChannelInitializer<Channel>() {
|
|
|
|
|
|
|
|
@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) {
|
2017-06-14 20:30:31 +02:00
|
|
|
channel.eventLoop().submit(() -> injectChannelInternal(channel));
|
2015-02-17 22:53:09 +01:00
|
|
|
}
|
2014-04-10 01:37:43 +02:00
|
|
|
}
|
2015-02-17 22:53:09 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
plugin.getLogger().log(Level.SEVERE, "Cannot inject incomming channel " + channel, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
// This is executed before Minecraft's channel handler
|
|
|
|
beginInitProtocol = new ChannelInitializer<Channel>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void initChannel(Channel channel) throws Exception {
|
|
|
|
channel.pipeline().addLast(endInitProtocol);
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
serverChannelHandler = new ChannelInboundHandlerAdapter() {
|
2014-04-10 01:37:43 +02:00
|
|
|
|
2015-02-17 22:53:09 +01:00
|
|
|
@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);
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2014-05-17 17:10:17 +02:00
|
|
|
/**
|
|
|
|
* Register bukkit events.
|
|
|
|
*/
|
|
|
|
private void registerBukkitEvents() {
|
|
|
|
listener = new Listener() {
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2014-05-17 17:10:17 +02:00
|
|
|
@EventHandler(priority = EventPriority.LOWEST)
|
|
|
|
public final void onPlayerLogin(PlayerLoginEvent e) {
|
2014-12-08 22:24:27 +01:00
|
|
|
if (closed)
|
2014-05-17 17:10:17 +02:00
|
|
|
return;
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2014-05-17 17:10:17 +02:00
|
|
|
Channel channel = getChannel(e.getPlayer());
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2014-05-17 17:10:17 +02:00
|
|
|
// Don't inject players that have been explicitly uninjected
|
|
|
|
if (!uninjectedChannels.contains(channel)) {
|
2014-06-19 15:28:27 +02:00
|
|
|
injectPlayer(e.getPlayer());
|
2014-05-17 17:10:17 +02:00
|
|
|
}
|
|
|
|
}
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2014-05-17 17:10:17 +02:00
|
|
|
@EventHandler
|
|
|
|
public final void onPluginDisable(PluginDisableEvent e) {
|
|
|
|
if (e.getPlugin().equals(plugin)) {
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
}
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2014-05-17 17:10:17 +02:00
|
|
|
};
|
2015-02-17 22:53:09 +01:00
|
|
|
|
|
|
|
plugin.getServer().getPluginManager().registerEvents(listener, plugin);
|
2014-05-17 17:10:17 +02:00
|
|
|
}
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2014-04-10 01:37:43 +02:00
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
private void registerChannelHandler() {
|
|
|
|
Object mcServer = getMinecraftServer.get(Bukkit.getServer());
|
|
|
|
Object serverConnection = getServerConnection.get(mcServer);
|
|
|
|
boolean looking = true;
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2023-05-07 00:26:49 +02:00
|
|
|
try {
|
|
|
|
Field field = Reflection.getParameterizedField(serverConnectionClass, List.class, networkManagerClass);
|
|
|
|
field.setAccessible(true);
|
|
|
|
|
|
|
|
networkManagers = (List<Object>) 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<Object>) method.invoke(null, serverConnection);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (networkManagers == null) {
|
|
|
|
throw new IllegalArgumentException("Failed to obtain list of network managers");
|
|
|
|
}
|
2014-04-10 01:37:43 +02:00
|
|
|
// We need to synchronize against this list
|
|
|
|
createServerChannelHandler();
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2014-04-10 01:37:43 +02:00
|
|
|
// Find the correct list, or implicitly throw an exception
|
|
|
|
for (int i = 0; looking; i++) {
|
|
|
|
List<Object> list = Reflection.getField(serverConnection.getClass(), List.class, i).get(serverConnection);
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2014-04-10 01:37:43 +02:00
|
|
|
for (Object item : list) {
|
2014-12-08 22:24:27 +01:00
|
|
|
if (!ChannelFuture.class.isInstance(item))
|
2014-04-10 01:37:43 +02:00
|
|
|
break;
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2014-04-10 01:37:43 +02:00
|
|
|
// Channel future that contains the server connection
|
2015-02-17 22:53:09 +01:00
|
|
|
Channel serverChannel = ((ChannelFuture) item).channel();
|
|
|
|
|
2014-04-10 01:37:43 +02:00
|
|
|
serverChannels.add(serverChannel);
|
|
|
|
serverChannel.pipeline().addFirst(serverChannelHandler);
|
|
|
|
looking = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2014-04-10 01:37:43 +02:00
|
|
|
private void unregisterChannelHandler() {
|
|
|
|
if (serverChannelHandler == null)
|
|
|
|
return;
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2014-04-10 01:37:43 +02:00
|
|
|
for (Channel serverChannel : serverChannels) {
|
|
|
|
final ChannelPipeline pipeline = serverChannel.pipeline();
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2014-04-10 01:37:43 +02:00
|
|
|
// Remove channel handler
|
|
|
|
serverChannel.eventLoop().execute(new Runnable() {
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2014-12-08 22:24:27 +01:00
|
|
|
@Override
|
2014-04-10 01:37:43 +02:00
|
|
|
public void run() {
|
2014-05-21 23:04:29 +02:00
|
|
|
try {
|
|
|
|
pipeline.remove(serverChannelHandler);
|
|
|
|
} catch (NoSuchElementException e) {
|
|
|
|
// That's fine
|
|
|
|
}
|
2014-04-10 01:37:43 +02:00
|
|
|
}
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2014-04-10 01:37:43 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2014-04-10 01:37:43 +02:00
|
|
|
private void registerPlayers(Plugin plugin) {
|
2013-12-21 19:37:02 +01:00
|
|
|
for (Player player : plugin.getServer().getOnlinePlayers()) {
|
|
|
|
injectPlayer(player);
|
|
|
|
}
|
|
|
|
}
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2014-02-01 16:38:24 +01:00
|
|
|
/**
|
|
|
|
* Invoked when the server is starting to send a packet to a player.
|
|
|
|
* <p>
|
|
|
|
* Note that this is not executed on the main thread.
|
2015-02-17 22:53:09 +01:00
|
|
|
*
|
2017-06-14 20:30:31 +02:00
|
|
|
* @param receiver - the receiving player, NULL for early login/status packets.
|
2015-11-24 21:35:23 +01:00
|
|
|
* @param channel - the channel that received the packet. Never NULL.
|
2014-02-01 16:38:24 +01:00
|
|
|
* @param packet - the packet being sent.
|
|
|
|
* @return The packet to send instead, or NULL to cancel the transmission.
|
|
|
|
*/
|
2017-06-14 20:30:31 +02:00
|
|
|
public Object onPacketOutAsync(Player receiver, Channel channel, Object packet) {
|
2014-02-01 16:38:24 +01:00
|
|
|
return packet;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoked when the server has received a packet from a given player.
|
2014-04-10 01:37:43 +02:00
|
|
|
* <p>
|
|
|
|
* Use {@link Channel#remoteAddress()} to get the remote address of the client.
|
2015-02-17 22:53:09 +01:00
|
|
|
*
|
2014-04-10 01:37:43 +02:00
|
|
|
* @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.
|
2014-02-01 16:38:24 +01:00
|
|
|
* @return The packet to recieve instead, or NULL to cancel.
|
|
|
|
*/
|
2014-04-10 01:37:43 +02:00
|
|
|
public Object onPacketInAsync(Player sender, Channel channel, Object packet) {
|
2014-02-01 16:38:24 +01:00
|
|
|
return packet;
|
|
|
|
}
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2014-02-01 16:38:24 +01:00
|
|
|
/**
|
|
|
|
* Send a packet to a particular player.
|
|
|
|
* <p>
|
2017-06-14 20:30:31 +02:00
|
|
|
* Note that {@link #onPacketOutAsync(Player, Channel, Object)} will be invoked with this packet.
|
2015-02-17 22:53:09 +01:00
|
|
|
*
|
2014-02-01 16:38:24 +01:00
|
|
|
* @param player - the destination player.
|
|
|
|
* @param packet - the packet to send.
|
|
|
|
*/
|
|
|
|
public void sendPacket(Player player, Object packet) {
|
2014-04-10 01:37:43 +02:00
|
|
|
sendPacket(getChannel(player), packet);
|
2014-02-01 16:38:24 +01:00
|
|
|
}
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2014-04-10 01:37:43 +02:00
|
|
|
/**
|
|
|
|
* Send a packet to a particular client.
|
|
|
|
* <p>
|
2017-06-14 20:30:31 +02:00
|
|
|
* Note that {@link #onPacketOutAsync(Player, Channel, Object)} will be invoked with this packet.
|
2015-02-17 22:53:09 +01:00
|
|
|
*
|
2014-04-10 01:37:43 +02:00
|
|
|
* @param channel - client identified by a channel.
|
|
|
|
* @param packet - the packet to send.
|
|
|
|
*/
|
|
|
|
public void sendPacket(Channel channel, Object packet) {
|
|
|
|
channel.pipeline().writeAndFlush(packet);
|
|
|
|
}
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2014-02-01 16:38:24 +01:00
|
|
|
/**
|
|
|
|
* Pretend that a given packet has been received from a player.
|
|
|
|
* <p>
|
2017-06-14 20:30:31 +02:00
|
|
|
* Note that {@link #onPacketInAsync(Player, Channel, Object)} will be invoked with this packet.
|
2015-02-17 22:53:09 +01:00
|
|
|
*
|
2014-02-01 16:38:24 +01:00
|
|
|
* @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) {
|
2014-04-10 01:37:43 +02:00
|
|
|
receivePacket(getChannel(player), packet);
|
|
|
|
}
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2014-04-10 01:37:43 +02:00
|
|
|
/**
|
|
|
|
* Pretend that a given packet has been received from a given client.
|
|
|
|
* <p>
|
2017-06-14 20:30:31 +02:00
|
|
|
* Note that {@link #onPacketInAsync(Player, Channel, Object)} will be invoked with this packet.
|
2015-02-17 22:53:09 +01:00
|
|
|
*
|
2014-04-10 01:37:43 +02:00
|
|
|
* @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);
|
2014-02-01 16:38:24 +01:00
|
|
|
}
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2014-02-01 16:38:24 +01:00
|
|
|
/**
|
2014-05-17 17:10:17 +02:00
|
|
|
* Retrieve the name of the channel injector, default implementation is "tiny-" + plugin name + "-" + a unique ID.
|
2014-02-01 16:38:24 +01:00
|
|
|
* <p>
|
2014-05-17 17:10:17 +02:00
|
|
|
* Note that this method will only be invoked once. It is no longer necessary to override this to support multiple instances.
|
2015-02-17 22:53:09 +01:00
|
|
|
*
|
2014-02-01 16:38:24 +01:00
|
|
|
* @return A unique channel handler name.
|
|
|
|
*/
|
|
|
|
protected String getHandlerName() {
|
2014-05-17 17:10:17 +02:00
|
|
|
return "tiny-" + plugin.getName() + "-" + ID.incrementAndGet();
|
2014-02-01 16:38:24 +01:00
|
|
|
}
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2014-02-01 16:38:24 +01:00
|
|
|
/**
|
2015-02-17 22:53:09 +01:00
|
|
|
* Add a custom channel handler to the given player's channel pipeline, allowing us to intercept sent and received packets.
|
2014-05-17 17:10:17 +02:00
|
|
|
* <p>
|
|
|
|
* This will automatically be called when a player has logged in.
|
2015-02-17 22:53:09 +01:00
|
|
|
*
|
2014-02-01 16:38:24 +01:00
|
|
|
* @param player - the player to inject.
|
|
|
|
*/
|
2014-05-17 17:10:17 +02:00
|
|
|
public void injectPlayer(Player player) {
|
|
|
|
injectChannelInternal(getChannel(player)).player = player;
|
2014-04-10 01:37:43 +02:00
|
|
|
}
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2014-04-10 01:37:43 +02:00
|
|
|
/**
|
|
|
|
* Add a custom channel handler to the given channel.
|
2015-02-17 22:53:09 +01:00
|
|
|
*
|
2017-06-14 20:30:31 +02:00
|
|
|
* @param channel - the channel to inject.
|
2014-05-17 17:10:17 +02:00
|
|
|
* @return The intercepted channel, or NULL if it has already been injected.
|
2014-04-10 01:37:43 +02:00
|
|
|
*/
|
2014-05-17 17:10:17 +02:00
|
|
|
public void injectChannel(Channel channel) {
|
|
|
|
injectChannelInternal(channel);
|
|
|
|
}
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2014-05-17 17:10:17 +02:00
|
|
|
/**
|
|
|
|
* Add a custom channel handler to the given channel.
|
2015-02-17 22:53:09 +01:00
|
|
|
*
|
2017-06-14 20:30:31 +02:00
|
|
|
* @param channel - the channel to inject.
|
2014-05-17 17:10:17 +02:00
|
|
|
* @return The packet interceptor.
|
|
|
|
*/
|
|
|
|
private PacketInterceptor injectChannelInternal(Channel channel) {
|
2014-04-10 01:37:43 +02:00
|
|
|
try {
|
|
|
|
PacketInterceptor interceptor = (PacketInterceptor) channel.pipeline().get(handlerName);
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2014-04-10 01:37:43 +02:00
|
|
|
// Inject our packet interceptor
|
|
|
|
if (interceptor == null) {
|
|
|
|
interceptor = new PacketInterceptor();
|
|
|
|
channel.pipeline().addBefore("packet_handler", handlerName, interceptor);
|
2014-05-17 17:10:17 +02:00
|
|
|
uninjectedChannels.remove(channel);
|
2013-12-21 19:37:02 +01:00
|
|
|
}
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2014-12-08 22:24:27 +01:00
|
|
|
return interceptor;
|
2014-04-10 01:37:43 +02:00
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
// Try again
|
|
|
|
return (PacketInterceptor) channel.pipeline().get(handlerName);
|
|
|
|
}
|
2013-12-21 19:37:02 +01:00
|
|
|
}
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2014-01-28 00:58:57 +01:00
|
|
|
/**
|
|
|
|
* Retrieve the Netty channel associated with a player. This is cached.
|
2015-02-17 22:53:09 +01:00
|
|
|
*
|
2014-01-28 00:58:57 +01:00
|
|
|
* @param player - the player.
|
|
|
|
* @return The Netty channel.
|
|
|
|
*/
|
2014-05-17 17:10:17 +02:00
|
|
|
public Channel getChannel(Player player) {
|
2014-04-10 01:37:43 +02:00
|
|
|
Channel channel = channelLookup.get(player.getName());
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2014-01-28 00:58:57 +01:00
|
|
|
// Lookup channel again
|
|
|
|
if (channel == null) {
|
|
|
|
Object connection = getConnection.get(getPlayerHandle.invoke(player));
|
|
|
|
Object manager = getManager.get(connection);
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2014-04-10 01:37:43 +02:00
|
|
|
channelLookup.put(player.getName(), channel = getChannel.get(manager));
|
2014-01-28 00:58:57 +01:00
|
|
|
}
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2014-01-28 00:58:57 +01:00
|
|
|
return channel;
|
2013-12-21 19:37:02 +01:00
|
|
|
}
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2013-12-21 19:37:02 +01:00
|
|
|
/**
|
2014-05-17 17:10:17 +02:00
|
|
|
* Uninject a specific player.
|
2015-02-17 22:53:09 +01:00
|
|
|
*
|
2014-05-17 17:10:17 +02:00
|
|
|
* @param player - the injected player.
|
2013-12-21 19:37:02 +01:00
|
|
|
*/
|
2014-05-17 17:10:17 +02:00
|
|
|
public void uninjectPlayer(Player player) {
|
|
|
|
uninjectChannel(getChannel(player));
|
|
|
|
}
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2014-05-17 17:10:17 +02:00
|
|
|
/**
|
|
|
|
* Uninject a specific channel.
|
|
|
|
* <p>
|
|
|
|
* This will also disable the automatic channel injection that occurs when a player has properly logged in.
|
2015-02-17 22:53:09 +01:00
|
|
|
*
|
2014-05-17 17:10:17 +02:00
|
|
|
* @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);
|
|
|
|
}
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2014-05-17 17:10:17 +02:00
|
|
|
// See ChannelInjector in ProtocolLib, line 590
|
|
|
|
channel.eventLoop().execute(new Runnable() {
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2014-05-17 17:10:17 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
channel.pipeline().remove(handlerName);
|
2014-02-01 16:38:24 +01:00
|
|
|
}
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2014-05-17 17:10:17 +02:00
|
|
|
});
|
|
|
|
}
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2014-05-17 17:10:17 +02:00
|
|
|
/**
|
|
|
|
* Determine if the given player has been injected by TinyProtocol.
|
2015-02-17 22:53:09 +01:00
|
|
|
*
|
2014-05-17 17:10:17 +02:00
|
|
|
* @param player - the player.
|
|
|
|
* @return TRUE if it is, FALSE otherwise.
|
|
|
|
*/
|
|
|
|
public boolean hasInjected(Player player) {
|
|
|
|
return hasInjected(getChannel(player));
|
|
|
|
}
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2014-05-17 17:10:17 +02:00
|
|
|
/**
|
|
|
|
* Determine if the given channel has been injected by TinyProtocol.
|
2015-02-17 22:53:09 +01:00
|
|
|
*
|
2014-05-17 17:10:17 +02:00
|
|
|
* @param channel - the channel.
|
|
|
|
* @return TRUE if it is, FALSE otherwise.
|
|
|
|
*/
|
|
|
|
public boolean hasInjected(Channel channel) {
|
|
|
|
return channel.pipeline().get(handlerName) != null;
|
2013-12-21 19:37:02 +01:00
|
|
|
}
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2013-12-21 19:37:02 +01:00
|
|
|
/**
|
2014-01-28 00:58:57 +01:00
|
|
|
* Cease listening for packets. This is called automatically when your plugin is disabled.
|
|
|
|
*/
|
|
|
|
public final void close() {
|
|
|
|
if (!closed) {
|
|
|
|
closed = true;
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2014-01-28 00:58:57 +01:00
|
|
|
// Remove our handlers
|
|
|
|
for (Player player : plugin.getServer().getOnlinePlayers()) {
|
2014-05-17 17:10:17 +02:00
|
|
|
uninjectPlayer(player);
|
2014-01-28 00:58:57 +01:00
|
|
|
}
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2014-02-01 16:38:24 +01:00
|
|
|
// Clean up Bukkit
|
|
|
|
HandlerList.unregisterAll(listener);
|
2014-04-10 01:37:43 +02:00
|
|
|
unregisterChannelHandler();
|
|
|
|
}
|
|
|
|
}
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2014-04-10 01:37:43 +02:00
|
|
|
/**
|
|
|
|
* Channel handler that is inserted into the player's channel pipeline, allowing us to intercept sent and received packets.
|
2015-02-17 22:53:09 +01:00
|
|
|
*
|
2014-04-10 01:37:43 +02:00
|
|
|
* @author Kristian
|
|
|
|
*/
|
2014-12-08 22:24:27 +01:00
|
|
|
private final class PacketInterceptor extends ChannelDuplexHandler {
|
2014-04-10 01:37:43 +02:00
|
|
|
// Updated by the login event
|
|
|
|
public volatile Player player;
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2014-04-10 01:37:43 +02:00
|
|
|
@Override
|
|
|
|
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
|
|
|
// Intercept channel
|
|
|
|
final Channel channel = ctx.channel();
|
|
|
|
handleLoginStart(channel, msg);
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2014-04-10 01:37:43 +02:00
|
|
|
try {
|
|
|
|
msg = onPacketInAsync(player, channel, msg);
|
|
|
|
} catch (Exception e) {
|
|
|
|
plugin.getLogger().log(Level.SEVERE, "Error in onPacketInAsync().", e);
|
|
|
|
}
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2014-04-10 01:37:43 +02:00
|
|
|
if (msg != null) {
|
|
|
|
super.channelRead(ctx, msg);
|
|
|
|
}
|
|
|
|
}
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2014-04-10 01:37:43 +02:00
|
|
|
@Override
|
2014-12-08 22:24:27 +01:00
|
|
|
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
|
2014-04-10 01:37:43 +02:00
|
|
|
try {
|
|
|
|
msg = onPacketOutAsync(player, ctx.channel(), msg);
|
|
|
|
} catch (Exception e) {
|
|
|
|
plugin.getLogger().log(Level.SEVERE, "Error in onPacketOutAsync().", e);
|
|
|
|
}
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2014-04-10 01:37:43 +02:00
|
|
|
if (msg != null) {
|
|
|
|
super.write(ctx, msg, promise);
|
|
|
|
}
|
|
|
|
}
|
2015-02-17 22:53:09 +01:00
|
|
|
|
2014-04-10 01:37:43 +02:00
|
|
|
private void handleLoginStart(Channel channel, Object packet) {
|
|
|
|
if (PACKET_LOGIN_IN_START.isInstance(packet)) {
|
|
|
|
GameProfile profile = getGameProfile.get(packet);
|
|
|
|
channelLookup.put(profile.getName(), channel);
|
|
|
|
}
|
2014-01-28 00:58:57 +01:00
|
|
|
}
|
|
|
|
}
|
2016-05-21 23:37:13 +02:00
|
|
|
}
|