From adfb33ad21e0e4db4df24e7019dcbc80e17a1c6e Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Sun, 12 May 2019 10:05:22 -0400 Subject: [PATCH] Fix server-sent plugin message channels, fixing WDLCompanion and similar Closes #203 --- .../proxy/command/VelocityCommand.java | 2 +- .../backend/BackendPlaySessionHandler.java | 11 ++++++++++- .../backend/TransitionSessionHandler.java | 7 ++++--- .../proxy/connection/client/ConnectedPlayer.java | 15 +++++++++------ .../proxy/network/http/NettyHttpClient.java | 1 - .../proxy/protocol/packet/KeepAlive.java | 2 +- 6 files changed, 25 insertions(+), 13 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/command/VelocityCommand.java b/proxy/src/main/java/com/velocitypowered/proxy/command/VelocityCommand.java index a67582a0a..3c12b493a 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/command/VelocityCommand.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/command/VelocityCommand.java @@ -260,7 +260,7 @@ public class VelocityCommand implements Command { @Override public boolean hasPermission(CommandSource source, String @NonNull [] args) { - return source.getPermissionValue("velocity.command.plugins") == Tristate.TRUE; + return source.getPermissionValue("velocity.command.plugins").asBoolean(); } } } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendPlaySessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendPlaySessionHandler.java index e6d75c1e6..a2afbae6a 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendPlaySessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendPlaySessionHandler.java @@ -86,10 +86,19 @@ public class BackendPlaySessionHandler implements MinecraftSessionHandler { @Override public boolean handle(PluginMessage packet) { - if (!serverConn.getPlayer().canForwardPluginMessage(packet)) { + if (!serverConn.getPlayer().canForwardPluginMessage(serverConn.ensureConnected() + .getProtocolVersion(), packet)) { return true; } + // We need to specially handle REGISTER and UNREGISTER packets. Later on, we'll write them to + // the client. + if (PluginMessageUtil.isRegister(packet)) { + serverConn.getPlayer().getKnownChannels().addAll(PluginMessageUtil.getChannels(packet)); + } else if (PluginMessageUtil.isUnregister(packet)) { + serverConn.getPlayer().getKnownChannels().removeAll(PluginMessageUtil.getChannels(packet)); + } + if (PluginMessageUtil.isMcBrand(packet)) { PluginMessage rewritten = PluginMessageUtil.rewriteMinecraftBrand(packet, server.getVersion()); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/TransitionSessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/TransitionSessionHandler.java index 07a06c89e..42d063bce 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/TransitionSessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/TransitionSessionHandler.java @@ -131,15 +131,16 @@ public class TransitionSessionHandler implements MinecraftSessionHandler { @Override public boolean handle(PluginMessage packet) { - if (!serverConn.getPlayer().canForwardPluginMessage(packet)) { + if (!serverConn.getPlayer().canForwardPluginMessage(serverConn.ensureConnected() + .getProtocolVersion(), packet)) { return true; } - // We need to specially handle REGISTER and UNREGISTER packets. Later on, we'll write them to - // the client. if (PluginMessageUtil.isRegister(packet)) { + System.out.println("[TSH] I CAN HAZ REGISTER: " + PluginMessageUtil.getChannels(packet)); serverConn.getPlayer().getKnownChannels().addAll(PluginMessageUtil.getChannels(packet)); } else if (PluginMessageUtil.isUnregister(packet)) { + System.out.println("[TSH] I CAN HAZ UNREGISTER: " + PluginMessageUtil.getChannels(packet)); serverConn.getPlayer().getKnownChannels().removeAll(PluginMessageUtil.getChannels(packet)); } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java index e77c20fd4..f6f104bc3 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java @@ -43,6 +43,7 @@ import com.velocitypowered.proxy.protocol.packet.KeepAlive; import com.velocitypowered.proxy.protocol.packet.PluginMessage; import com.velocitypowered.proxy.protocol.packet.ResourcePackRequest; import com.velocitypowered.proxy.protocol.packet.TitlePacket; +import com.velocitypowered.proxy.protocol.util.PluginMessageUtil; import com.velocitypowered.proxy.server.VelocityRegisteredServer; import com.velocitypowered.proxy.tablist.VelocityTabList; import com.velocitypowered.proxy.util.VelocityMessages; @@ -650,16 +651,18 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { /** * Determines whether or not we can forward a plugin message onto the client. + * @param version the Minecraft protocol version * @param message the plugin message to forward to the client * @return {@code true} if the message can be forwarded, {@code false} otherwise */ - public boolean canForwardPluginMessage(PluginMessage message) { - // If we're forwarding a plugin message onto the client, that implies that we have a backend - // connection already. - MinecraftConnection mc = ensureBackendConnection(); - + public boolean canForwardPluginMessage(ProtocolVersion version, PluginMessage message) { boolean minecraftOrFmlMessage; - if (mc.getProtocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_12_2) <= 0) { + + if (PluginMessageUtil.isRegister(message) || PluginMessageUtil.isUnregister(message)) { + return true; + } + + if (version.compareTo(ProtocolVersion.MINECRAFT_1_12_2) <= 0) { String channel = message.getChannel(); minecraftOrFmlMessage = channel.startsWith("MC|") || channel .startsWith(LegacyForgeConstants.FORGE_LEGACY_HANDSHAKE_CHANNEL); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/http/NettyHttpClient.java b/proxy/src/main/java/com/velocitypowered/proxy/network/http/NettyHttpClient.java index fa66b533d..f64639957 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/http/NettyHttpClient.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/http/NettyHttpClient.java @@ -15,7 +15,6 @@ import io.netty.handler.ssl.SslContextBuilder; import io.netty.handler.ssl.SslHandler; import java.net.InetSocketAddress; import java.net.URL; -import java.util.Objects; import java.util.concurrent.CompletableFuture; import javax.net.ssl.SSLEngine; diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/KeepAlive.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/KeepAlive.java index e4b581617..ac6810a65 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/KeepAlive.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/KeepAlive.java @@ -32,7 +32,7 @@ public class KeepAlive implements MinecraftPacket { } else { randomId = ProtocolUtils.readVarInt(buf); } -} + } @Override public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion version) {