Dieser Commit ist enthalten in:
Ursprung
676e247e98
Commit
04b03ce06d
@ -19,8 +19,6 @@
|
|||||||
|
|
||||||
package de.steamwar.core;
|
package de.steamwar.core;
|
||||||
|
|
||||||
import com.comphenix.tinyprotocol.Reflection;
|
|
||||||
import com.comphenix.tinyprotocol.TinyProtocol;
|
|
||||||
import net.minecraft.network.chat.IChatMutableComponent;
|
import net.minecraft.network.chat.IChatMutableComponent;
|
||||||
import net.minecraft.network.chat.contents.LiteralContents;
|
import net.minecraft.network.chat.contents.LiteralContents;
|
||||||
|
|
||||||
@ -29,10 +27,4 @@ public class ChatWrapper19 implements ChatWrapper {
|
|||||||
public Object stringToChatComponent(String text) {
|
public Object stringToChatComponent(String text) {
|
||||||
return IChatMutableComponent.a(new LiteralContents(text));
|
return IChatMutableComponent.a(new LiteralContents(text));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final Reflection.FieldAccessor<String> getName = Reflection.getField(TinyProtocol.PACKET_LOGIN_IN_START, String.class, 0);
|
|
||||||
@Override
|
|
||||||
public String getNameByLoginPacket(Object packet) {
|
|
||||||
return getName.get(packet);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -20,8 +20,6 @@
|
|||||||
package de.steamwar.core;
|
package de.steamwar.core;
|
||||||
|
|
||||||
import com.comphenix.tinyprotocol.Reflection;
|
import com.comphenix.tinyprotocol.Reflection;
|
||||||
import com.comphenix.tinyprotocol.TinyProtocol;
|
|
||||||
import com.mojang.authlib.GameProfile;
|
|
||||||
|
|
||||||
public class ChatWrapper8 implements ChatWrapper {
|
public class ChatWrapper8 implements ChatWrapper {
|
||||||
|
|
||||||
@ -30,10 +28,4 @@ public class ChatWrapper8 implements ChatWrapper {
|
|||||||
public Object stringToChatComponent(String text) {
|
public Object stringToChatComponent(String text) {
|
||||||
return chatComponentConstructor.invoke(text);
|
return chatComponentConstructor.invoke(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final Reflection.FieldAccessor<GameProfile> getGameProfile = Reflection.getField(TinyProtocol.PACKET_LOGIN_IN_START, GameProfile.class, 0);
|
|
||||||
@Override
|
|
||||||
public String getNameByLoginPacket(Object packet) {
|
|
||||||
return getGameProfile.get(packet).getName();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -2,113 +2,64 @@ package com.comphenix.tinyprotocol;
|
|||||||
|
|
||||||
import com.comphenix.tinyprotocol.Reflection.FieldAccessor;
|
import com.comphenix.tinyprotocol.Reflection.FieldAccessor;
|
||||||
import com.comphenix.tinyprotocol.Reflection.MethodInvoker;
|
import com.comphenix.tinyprotocol.Reflection.MethodInvoker;
|
||||||
import com.google.common.collect.Lists;
|
|
||||||
import com.google.common.collect.MapMaker;
|
|
||||||
import de.steamwar.core.ChatWrapper;
|
|
||||||
import de.steamwar.core.Core;
|
import de.steamwar.core.Core;
|
||||||
import io.netty.channel.*;
|
import io.netty.channel.Channel;
|
||||||
import org.bukkit.Bukkit;
|
import io.netty.channel.ChannelDuplexHandler;
|
||||||
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
|
import io.netty.channel.ChannelPromise;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.EventPriority;
|
import org.bukkit.event.EventPriority;
|
||||||
import org.bukkit.event.HandlerList;
|
import org.bukkit.event.HandlerList;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
import org.bukkit.event.player.PlayerLoginEvent;
|
import org.bukkit.event.player.PlayerLoginEvent;
|
||||||
|
import org.bukkit.event.player.PlayerQuitEvent;
|
||||||
import org.bukkit.event.server.PluginDisableEvent;
|
import org.bukkit.event.server.PluginDisableEvent;
|
||||||
import org.bukkit.plugin.Plugin;
|
import org.bukkit.plugin.Plugin;
|
||||||
import org.bukkit.scheduler.BukkitRunnable;
|
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
|
||||||
import java.util.function.BiFunction;
|
import java.util.function.BiFunction;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
|
||||||
/**
|
public class TinyProtocol implements Listener {
|
||||||
* Represents a very tiny alternative to ProtocolLib.
|
|
||||||
* <p>
|
|
||||||
* It now supports intercepting packets during login and status ping (such as OUT_SERVER_PING)!
|
|
||||||
*
|
|
||||||
* @author Kristian
|
|
||||||
*/
|
|
||||||
public class TinyProtocol {
|
|
||||||
private static final AtomicInteger ID = new AtomicInteger(0);
|
|
||||||
|
|
||||||
// Used in order to lookup a channel
|
|
||||||
private static final MethodInvoker getPlayerHandle = Reflection.getMethod("{obc}.entity.CraftPlayer", "getHandle");
|
|
||||||
private static final Class<Object> playerConnection = Reflection.getUntypedClass("{nms.server.network}.PlayerConnection");
|
|
||||||
private static final FieldAccessor<Object> getConnection = Reflection.getField("{nms.server.level}.EntityPlayer", playerConnection, 0);
|
|
||||||
private static final Class<Object> networkManager = Reflection.getUntypedClass("{nms.network}.NetworkManager");
|
|
||||||
private static final FieldAccessor<Object> getManager = Reflection.getField(playerConnection, networkManager, 0);
|
|
||||||
private static final FieldAccessor<Channel> getChannel = Reflection.getField(networkManager, Channel.class, 0);
|
|
||||||
|
|
||||||
// Looking up ServerConnection
|
|
||||||
private static final Class<Object> minecraftServerClass = Reflection.getUntypedClass("{nms.server}.MinecraftServer");
|
|
||||||
private static final Class<Object> serverConnectionClass = Reflection.getUntypedClass("{nms.server.network}.ServerConnection");
|
|
||||||
private static final FieldAccessor<Object> getMinecraftServer = Reflection.getField("{obc}.CraftServer", minecraftServerClass, 0);
|
|
||||||
private static final FieldAccessor<Object> getServerConnection = Reflection.getField(minecraftServerClass, serverConnectionClass, 0);
|
|
||||||
private static final FieldAccessor<Collection> getNetworkMarkers = Reflection.getField(serverConnectionClass, Collection.class, 1);
|
|
||||||
|
|
||||||
// Packets we have to intercept
|
|
||||||
public static final Class<?> PACKET_LOGIN_IN_START = Reflection.getClass("{nms.network.protocol.login}.PacketLoginInStart");
|
|
||||||
|
|
||||||
// Speedup channel lookup
|
|
||||||
private final Map<String, Channel> channelLookup = new MapMaker().weakValues().makeMap();
|
|
||||||
private final Listener listener;
|
|
||||||
|
|
||||||
// Channels that have already been removed
|
|
||||||
private final Set<Channel> uninjectedChannels = Collections.newSetFromMap(new MapMaker().weakKeys().<Channel, Boolean>makeMap());
|
|
||||||
|
|
||||||
// List of network markers
|
|
||||||
private Collection<Object> networkManagers;
|
|
||||||
|
|
||||||
// Injected channel handlers
|
|
||||||
private final List<Channel> serverChannels = Lists.newArrayList();
|
|
||||||
private ChannelInboundHandlerAdapter serverChannelHandler;
|
|
||||||
private ChannelInitializer<Channel> beginInitProtocol;
|
|
||||||
private ChannelInitializer<Channel> endInitProtocol;
|
|
||||||
|
|
||||||
// Current handler name
|
|
||||||
private final String handlerName;
|
|
||||||
private volatile boolean closed;
|
|
||||||
private final Plugin plugin;
|
|
||||||
|
|
||||||
private final Map<Class<?>, List<BiFunction<Player, Object, Object>>> packetFilters = new HashMap<>();
|
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
private PacketFilter inFilter = (player, channel, packet) -> packet;
|
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
private PacketFilter outFilter = (player, channel, packet) -> packet;
|
|
||||||
|
|
||||||
public static final TinyProtocol instance = new TinyProtocol(Core.getInstance());
|
public static final TinyProtocol instance = new TinyProtocol(Core.getInstance());
|
||||||
|
private static int id = 0;
|
||||||
|
|
||||||
public static void init() {
|
public static void init() {
|
||||||
//enforce init
|
//enforce init
|
||||||
}
|
}
|
||||||
|
|
||||||
public TinyProtocol(final Plugin plugin) {
|
private final Plugin plugin;
|
||||||
|
private final String handlerName;
|
||||||
|
private boolean closed;
|
||||||
|
|
||||||
|
private final Map<Class<?>, List<BiFunction<Player, Object, Object>>> packetFilters = new HashMap<>();
|
||||||
|
private final Map<Player, PacketInterceptor> playerInterceptors = new HashMap<>();
|
||||||
|
|
||||||
|
private TinyProtocol(final Plugin plugin) {
|
||||||
this.plugin = plugin;
|
this.plugin = plugin;
|
||||||
|
|
||||||
// Compute handler name
|
// Compute handler name
|
||||||
this.handlerName = "tiny-" + plugin.getName() + "-" + ID.incrementAndGet();
|
this.handlerName = "tiny-" + plugin.getName() + "-" + ++id;
|
||||||
|
|
||||||
// Prepare existing players
|
// Prepare existing players
|
||||||
listener = new Listener() {
|
plugin.getServer().getPluginManager().registerEvents(this, plugin);
|
||||||
|
|
||||||
|
for (Player player : plugin.getServer().getOnlinePlayers()) {
|
||||||
|
new PacketInterceptor(player);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@EventHandler(priority = EventPriority.LOWEST)
|
@EventHandler(priority = EventPriority.LOWEST)
|
||||||
public void onPlayerLogin(PlayerLoginEvent e) {
|
public void onPlayerLogin(PlayerLoginEvent e) {
|
||||||
injectPlayerOnLogin(
|
if(closed)
|
||||||
e.getPlayer(),
|
return;
|
||||||
() -> Bukkit.getScheduler().runTask(plugin, () -> injectPlayerOnLogin(
|
new PacketInterceptor(e.getPlayer());
|
||||||
e.getPlayer(),
|
|
||||||
() -> Bukkit.getScheduler().runTaskLater(plugin, () -> injectPlayerOnLogin(
|
|
||||||
e.getPlayer(),
|
|
||||||
() -> {
|
|
||||||
//Give up
|
|
||||||
}
|
}
|
||||||
), 1)
|
|
||||||
))
|
@EventHandler(priority = EventPriority.MONITOR)
|
||||||
);
|
public void onPlayerDisconnect(PlayerQuitEvent e) {
|
||||||
|
getInterceptor(e.getPlayer()).ifPresent(PacketInterceptor::close);
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler
|
@EventHandler
|
||||||
@ -117,139 +68,6 @@ public class TinyProtocol {
|
|||||||
close();
|
close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
|
||||||
plugin.getServer().getPluginManager().registerEvents(listener, plugin);
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void injectPlayerOnLogin(Player player, Runnable errorHandler) {
|
|
||||||
if (closed)
|
|
||||||
return;
|
|
||||||
|
|
||||||
try {
|
|
||||||
Channel channel = getChannel(player);
|
|
||||||
|
|
||||||
// Don't inject players that have been explicitly uninjected
|
|
||||||
if (!uninjectedChannels.contains(channel)) {
|
|
||||||
injectPlayer(player);
|
|
||||||
}
|
|
||||||
} catch (NullPointerException ex) {
|
|
||||||
errorHandler.run();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void createServerChannelHandler() {
|
|
||||||
// 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) {
|
|
||||||
channel.eventLoop().submit(() -> injectChannelInternal(channel));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} 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() {
|
|
||||||
|
|
||||||
@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);
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
private void registerChannelHandler() {
|
|
||||||
Object mcServer = getMinecraftServer.get(Bukkit.getServer());
|
|
||||||
Object serverConnection = getServerConnection.get(mcServer);
|
|
||||||
boolean looking = true;
|
|
||||||
|
|
||||||
// We need to synchronize against this list
|
|
||||||
networkManagers = getNetworkMarkers.get(serverConnection);
|
|
||||||
createServerChannelHandler();
|
|
||||||
|
|
||||||
// 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);
|
|
||||||
|
|
||||||
for (Object item : list) {
|
|
||||||
if (!(item instanceof ChannelFuture))
|
|
||||||
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(() -> {
|
|
||||||
try {
|
|
||||||
pipeline.remove(serverChannelHandler);
|
|
||||||
} catch (NoSuchElementException e) {
|
|
||||||
// That's fine
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void registerPlayers(Plugin plugin) {
|
|
||||||
for (Player player : plugin.getServer().getOnlinePlayers()) {
|
|
||||||
injectPlayer(player);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addFilter(Class<?> packetType, BiFunction<Player, Object, Object> filter) {
|
public void addFilter(Class<?> packetType, BiFunction<Player, Object, Object> filter) {
|
||||||
packetFilters.computeIfAbsent(packetType, c -> new ArrayList<>(1)).add(filter);
|
packetFilters.computeIfAbsent(packetType, c -> new ArrayList<>(1)).add(filter);
|
||||||
@ -259,107 +77,67 @@ public class TinyProtocol {
|
|||||||
packetFilters.getOrDefault(packetType, Collections.emptyList()).remove(filter);
|
packetFilters.getOrDefault(packetType, Collections.emptyList()).remove(filter);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object onPacketOutAsync(Player receiver, Channel channel, Object packet) {
|
|
||||||
return filterPacket(outFilter, receiver, channel, packet);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Object onPacketInAsync(Player sender, Channel channel, Object packet) {
|
|
||||||
return filterPacket(inFilter, sender, channel, packet);
|
|
||||||
}
|
|
||||||
|
|
||||||
private Object filterPacket(PacketFilter handler, Player player, Channel channel, Object packet) {
|
|
||||||
List<BiFunction<Player, Object, Object>> filters = packetFilters.getOrDefault(packet.getClass(), Collections.emptyList());
|
|
||||||
|
|
||||||
for(BiFunction<Player, Object, Object> filter : filters) {
|
|
||||||
packet = filter.apply(player, packet);
|
|
||||||
|
|
||||||
if(packet == null)
|
|
||||||
return packet;
|
|
||||||
}
|
|
||||||
|
|
||||||
return handler.onPacket(player, channel, packet);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
public void setInFilter(PacketFilter filter) {
|
|
||||||
inFilter = filter;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
public void setOutFilter(PacketFilter filter) {
|
|
||||||
outFilter = filter;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void sendPacket(Player player, Object packet) {
|
public void sendPacket(Player player, Object packet) {
|
||||||
sendPacket(getChannel(player), packet);
|
getInterceptor(player).ifPresent(i -> i.sendPacket(packet));
|
||||||
}
|
|
||||||
|
|
||||||
public void sendPacket(Channel channel, Object packet) {
|
|
||||||
channel.pipeline().writeAndFlush(packet);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void receivePacket(Player player, Object packet) {
|
public void receivePacket(Player player, Object packet) {
|
||||||
receivePacket(getChannel(player), packet);
|
getInterceptor(player).ifPresent(i -> i.receivePacket(packet));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void receivePacket(Channel channel, Object packet) {
|
public final void close() {
|
||||||
|
if(closed)
|
||||||
|
return;
|
||||||
|
closed = true;
|
||||||
|
|
||||||
|
HandlerList.unregisterAll(this);
|
||||||
|
|
||||||
|
for (Player player : plugin.getServer().getOnlinePlayers()) {
|
||||||
|
getInterceptor(player).ifPresent(PacketInterceptor::close);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Optional<PacketInterceptor> getInterceptor(Player player) {
|
||||||
|
synchronized (playerInterceptors) {
|
||||||
|
return Optional.ofNullable(playerInterceptors.get(player));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final MethodInvoker getPlayerHandle = Reflection.getMethod("{obc}.entity.CraftPlayer", "getHandle");
|
||||||
|
private static final Class<Object> playerConnection = Reflection.getUntypedClass("{nms.server.network}.PlayerConnection");
|
||||||
|
private static final FieldAccessor<Object> getConnection = Reflection.getField("{nms.server.level}.EntityPlayer", playerConnection, 0);
|
||||||
|
private static final Class<Object> networkManager = Reflection.getUntypedClass("{nms.network}.NetworkManager");
|
||||||
|
private static final FieldAccessor<Object> getManager = Reflection.getField(playerConnection, networkManager, 0);
|
||||||
|
private static final FieldAccessor<Channel> getChannel = Reflection.getField(networkManager, Channel.class, 0);
|
||||||
|
|
||||||
|
private final class PacketInterceptor extends ChannelDuplexHandler {
|
||||||
|
private final Player player;
|
||||||
|
private final Channel channel;
|
||||||
|
|
||||||
|
private PacketInterceptor(Player player) {
|
||||||
|
this.player = player;
|
||||||
|
|
||||||
|
Object connection = getConnection.get(getPlayerHandle.invoke(player));
|
||||||
|
Object manager = getManager.get(connection);
|
||||||
|
channel = getChannel.get(manager);
|
||||||
|
|
||||||
|
synchronized (playerInterceptors) {
|
||||||
|
playerInterceptors.put(player, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
channel.pipeline().addBefore("packet_handler", handlerName, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void sendPacket(Object packet) {
|
||||||
|
channel.pipeline().writeAndFlush(packet);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void receivePacket(Object packet) {
|
||||||
channel.pipeline().context("encoder").fireChannelRead(packet);
|
channel.pipeline().context("encoder").fireChannelRead(packet);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void injectPlayer(Player player) {
|
private void close() {
|
||||||
injectChannelInternal(getChannel(player)).player = player;
|
if(channel.isActive()) {
|
||||||
}
|
|
||||||
|
|
||||||
public void injectChannel(Channel channel) {
|
|
||||||
injectChannelInternal(channel);
|
|
||||||
}
|
|
||||||
|
|
||||||
private PacketInterceptor injectChannelInternal(Channel channel) {
|
|
||||||
try {
|
|
||||||
PacketInterceptor interceptor = (PacketInterceptor) channel.pipeline().get(handlerName);
|
|
||||||
|
|
||||||
// Inject our packet interceptor
|
|
||||||
if (interceptor == null) {
|
|
||||||
interceptor = new PacketInterceptor();
|
|
||||||
if(!channel.isActive())
|
|
||||||
return interceptor;
|
|
||||||
|
|
||||||
channel.pipeline().addBefore("packet_handler", handlerName, interceptor);
|
|
||||||
uninjectedChannels.remove(channel);
|
|
||||||
}
|
|
||||||
|
|
||||||
return interceptor;
|
|
||||||
} catch (IllegalArgumentException e) {
|
|
||||||
// Try again
|
|
||||||
return (PacketInterceptor) channel.pipeline().get(handlerName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void uninjectPlayer(Player player) {
|
|
||||||
uninjectChannel(getChannel(player));
|
|
||||||
}
|
|
||||||
|
|
||||||
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(() -> {
|
channel.eventLoop().execute(() -> {
|
||||||
try {
|
try {
|
||||||
channel.pipeline().remove(handlerName);
|
channel.pipeline().remove(handlerName);
|
||||||
@ -369,48 +147,17 @@ public class TinyProtocol {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean hasInjected(Player player) {
|
synchronized (playerInterceptors) {
|
||||||
return hasInjected(getChannel(player));
|
playerInterceptors.remove(player, this);
|
||||||
}
|
|
||||||
|
|
||||||
public boolean hasInjected(Channel channel) {
|
|
||||||
return channel.pipeline().get(handlerName) != null;
|
|
||||||
}
|
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
public interface PacketFilter {
|
|
||||||
Object onPacket(Player player, Channel channel, Object packet);
|
|
||||||
}
|
|
||||||
|
|
||||||
private final class PacketInterceptor extends ChannelDuplexHandler {
|
|
||||||
// Updated by the login event
|
|
||||||
public volatile Player player;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
||||||
// Intercept channel
|
|
||||||
final Channel channel = ctx.channel();
|
|
||||||
handleLoginStart(channel, msg);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
msg = onPacketInAsync(player, channel, msg);
|
msg = filterPacket(player, msg);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
plugin.getLogger().log(Level.SEVERE, "Error in onPacketInAsync().", e);
|
plugin.getLogger().log(Level.SEVERE, "Error during incoming packet processing", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (msg != null) {
|
if (msg != null) {
|
||||||
@ -421,9 +168,9 @@ public class TinyProtocol {
|
|||||||
@Override
|
@Override
|
||||||
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
|
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
|
||||||
try {
|
try {
|
||||||
msg = onPacketOutAsync(player, ctx.channel(), msg);
|
msg = filterPacket(player, msg);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
plugin.getLogger().log(Level.SEVERE, "Error in onPacketOutAsync().", e);
|
plugin.getLogger().log(Level.SEVERE, "Error during outgoing packet processing", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (msg != null) {
|
if (msg != null) {
|
||||||
@ -431,10 +178,17 @@ public class TinyProtocol {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleLoginStart(Channel channel, Object packet) {
|
private Object filterPacket(Player player, Object packet) {
|
||||||
if (PACKET_LOGIN_IN_START.isInstance(packet)) {
|
List<BiFunction<Player, Object, Object>> filters = packetFilters.getOrDefault(packet.getClass(), Collections.emptyList());
|
||||||
channelLookup.put(ChatWrapper.impl.getNameByLoginPacket(packet), channel);
|
|
||||||
|
for(BiFunction<Player, Object, Object> filter : filters) {
|
||||||
|
packet = filter.apply(player, packet);
|
||||||
|
|
||||||
|
if(packet == null)
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return packet;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,5 +23,4 @@ public interface ChatWrapper {
|
|||||||
ChatWrapper impl = VersionDependent.getVersionImpl(Core.getInstance());
|
ChatWrapper impl = VersionDependent.getVersionImpl(Core.getInstance());
|
||||||
|
|
||||||
Object stringToChatComponent(String text);
|
Object stringToChatComponent(String text);
|
||||||
String getNameByLoginPacket(Object packet);
|
|
||||||
}
|
}
|
||||||
|
@ -37,7 +37,7 @@ public class ProtocolUtils {
|
|||||||
private ProtocolUtils() {}
|
private ProtocolUtils() {}
|
||||||
|
|
||||||
public static void broadcastPacket(Object packet) {
|
public static void broadcastPacket(Object packet) {
|
||||||
Bukkit.getOnlinePlayers().stream().map(TinyProtocol.instance::getChannel).filter(TinyProtocol.instance::hasInjected).forEach(channel -> TinyProtocol.instance.sendPacket(channel, packet));
|
Bukkit.getOnlinePlayers().forEach(player -> TinyProtocol.instance.sendPacket(player, packet));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static BiFunction<Object, UnaryOperator<Object>, Object> arrayCloneGenerator(Class<?> elementClass) {
|
public static BiFunction<Object, UnaryOperator<Object>, Object> arrayCloneGenerator(Class<?> elementClass) {
|
||||||
|
In neuem Issue referenzieren
Einen Benutzer sperren