From cfbd6ffac5d9787608beda2f24a5455cc378f964 Mon Sep 17 00:00:00 2001 From: Dylan Keir Date: Sun, 21 Nov 2021 17:13:47 +0000 Subject: [PATCH] Support Velocity's new LoginInboundConnection class. --- build.gradle | 9 +-- .../handler/VelocityHandshakeHandler.java | 2 +- .../velocity/handler/VelocityPacket.java | 21 ++++++ .../velocity/handler/VelocityPlayer.java | 68 ++++++++++++++++--- src/main/resources/bungee.yml | 2 +- src/main/resources/plugin.yml | 2 +- src/main/resources/velocity-plugin.json | 2 +- 7 files changed, 86 insertions(+), 20 deletions(-) diff --git a/build.gradle b/build.gradle index 0fd5ffb..317d553 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,6 @@ import org.yaml.snakeyaml.Yaml import java.nio.charset.StandardCharsets - // -- // Building // -- @@ -24,15 +23,13 @@ plugins { id 'idea' } - // -- // Variables // -- -version = '2.5.4' +version = '2.5.5' group = 'net.tcpshield.tcpshield' archivesBaseName = 'TCPShield' - // -- // Misc. // -- @@ -68,7 +65,6 @@ compileJava { options.encoding = 'UTF-8' } - // -- // Dependencies // -- @@ -97,7 +93,6 @@ repositories { } dependencies { - // Bukkit compileOnly group: 'org.spigotmc', name: 'spigot-api', version: '1.11-R0.1-SNAPSHOT' compileOnly group: 'com.comphenix.protocol', name: 'ProtocolLib', version: '4.4.0' @@ -180,4 +175,4 @@ void updateJsons() { } } -build.dependsOn updateVersion \ No newline at end of file +build.dependsOn updateVersion diff --git a/src/main/java/net/tcpshield/tcpshield/velocity/handler/VelocityHandshakeHandler.java b/src/main/java/net/tcpshield/tcpshield/velocity/handler/VelocityHandshakeHandler.java index 7d166ef..fe8b8df 100644 --- a/src/main/java/net/tcpshield/tcpshield/velocity/handler/VelocityHandshakeHandler.java +++ b/src/main/java/net/tcpshield/tcpshield/velocity/handler/VelocityHandshakeHandler.java @@ -43,7 +43,7 @@ public void onProxyPing(ProxyPingEvent e) { private void handleEvent(InboundConnection connection, String debugSource) { VelocityPlayer player = new VelocityPlayer(connection); - if (player.isLegacy()) { + if (player.getConnectionType() == VelocityPlayer.ConnectionType.LEGACY) { player.disconnect(); return; } diff --git a/src/main/java/net/tcpshield/tcpshield/velocity/handler/VelocityPacket.java b/src/main/java/net/tcpshield/tcpshield/velocity/handler/VelocityPacket.java index a10ecb4..13ae5f3 100644 --- a/src/main/java/net/tcpshield/tcpshield/velocity/handler/VelocityPacket.java +++ b/src/main/java/net/tcpshield/tcpshield/velocity/handler/VelocityPacket.java @@ -22,6 +22,10 @@ public class VelocityPacket implements PacketProvider { private static final Field HOSTNAME_FIELD; private static final Field CLEANED_ADDRESS_FIELD; + // new velocity support + private static Class LOGIN_INBOUND_CONNECTION_CLASS; + private static Field LOGIN_INBOUND_CONNECTION_DELEGATE_FIELD; + static { try { Class inboundConnection = Class.forName("com.velocitypowered.proxy.connection.client.InitialInboundConnection"); @@ -32,6 +36,14 @@ public class VelocityPacket implements PacketProvider { } catch (Exception e) { throw new InitializationException(new ReflectionException(e)); } + + // LoginInboundConnection support + try { + LOGIN_INBOUND_CONNECTION_CLASS = Class.forName("com.velocitypowered.proxy.connection.client.LoginInboundConnection"); + LOGIN_INBOUND_CONNECTION_DELEGATE_FIELD = ReflectionUtil.getPrivateField(LOGIN_INBOUND_CONNECTION_CLASS, "delegate"); + } catch (Exception e) { + // ignore for old versions of velocity + } } @@ -39,6 +51,15 @@ public class VelocityPacket implements PacketProvider { private final String rawPayload; public VelocityPacket(InboundConnection inboundConnection) { + // support new velocity connection type + if (inboundConnection.getClass() == LOGIN_INBOUND_CONNECTION_CLASS) { + try { + inboundConnection = (InboundConnection) LOGIN_INBOUND_CONNECTION_DELEGATE_FIELD.get(inboundConnection); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } + this.inboundConnection = inboundConnection; try { this.rawPayload = (String) HOSTNAME_FIELD.get(HANDSHAKE_FIELD.get(inboundConnection)); diff --git a/src/main/java/net/tcpshield/tcpshield/velocity/handler/VelocityPlayer.java b/src/main/java/net/tcpshield/tcpshield/velocity/handler/VelocityPlayer.java index fda9395..0879c33 100644 --- a/src/main/java/net/tcpshield/tcpshield/velocity/handler/VelocityPlayer.java +++ b/src/main/java/net/tcpshield/tcpshield/velocity/handler/VelocityPlayer.java @@ -26,6 +26,10 @@ public class VelocityPlayer implements PlayerProvider { private static final Field REMOTE_ADDRESS_FIELD; private static final Method CLOSE_CHANNEL_METHOD; + // new velocity support + private static Class LOGIN_INBOUND_CONNECTION_CLASS; + private static Field LOGIN_INBOUND_CONNECTION_DELEGATE_FIELD; + static { try { INITIAL_INBOUND_CONNECTION_CLASS = Class.forName("com.velocitypowered.proxy.connection.client.InitialInboundConnection"); @@ -38,22 +42,39 @@ public class VelocityPlayer implements PlayerProvider { } catch (Exception e) { throw new InitializationException(new ReflectionException(e)); } + + // LoginInboundConnection support + try { + LOGIN_INBOUND_CONNECTION_CLASS = Class.forName("com.velocitypowered.proxy.connection.client.LoginInboundConnection"); + LOGIN_INBOUND_CONNECTION_DELEGATE_FIELD = ReflectionUtil.getPrivateField(LOGIN_INBOUND_CONNECTION_CLASS, "delegate"); + } catch (Exception e) { + // ignore for old versions of velocity + } } private final InboundConnection inboundConnection; - private final boolean legacy; + // private final boolean legacy; + private final ConnectionType connectionType; private String ip; public VelocityPlayer(InboundConnection inboundConnection) { this.inboundConnection = inboundConnection; - this.legacy = inboundConnection.getClass() != INITIAL_INBOUND_CONNECTION_CLASS; +// this.legacy = inboundConnection.getClass() != INITIAL_INBOUND_CONNECTION_CLASS && inboundConnection.getClass() != LOGIN_INBOUND_CONNECTION_CLASS; this.ip = inboundConnection.getRemoteAddress().getAddress().getHostAddress(); - } + if (this.inboundConnection.getClass() == INITIAL_INBOUND_CONNECTION_CLASS) { + this.connectionType = ConnectionType.INITIAL_INBOUND; + } else if (this.inboundConnection.getClass() == LOGIN_INBOUND_CONNECTION_CLASS) { + this.connectionType = ConnectionType.LOGIN_INBOUND; + } else { + this.connectionType = ConnectionType.LEGACY; + } + } /** * Unsupported with Velocity handshakes + * * @return unknown */ @Override @@ -63,6 +84,7 @@ public String getUUID() { /** * Unsupported with Velocity handshakes + * * @return unknown */ @Override @@ -75,26 +97,26 @@ public String getIP() { return ip; } - public boolean isLegacy() { - return legacy; - } - @Override public void setIP(InetSocketAddress ip) throws PlayerManipulationException { try { this.ip = ip.getAddress().getHostAddress(); - Object minecraftConnection = MINECRAFT_CONNECTION_FIELD.get(inboundConnection); + Object minecraftConnection = this.getMinecraftConnection(); REMOTE_ADDRESS_FIELD.set(minecraftConnection, ip); } catch (Exception e) { throw new PlayerManipulationException(e); } } +// public boolean isLegacy() { +// return legacy; +// } + @Override public void disconnect() { try { - Object minecraftConnection = legacy ? LEGACY_MINECRAFT_CONNECTION_FIELD.get(inboundConnection) : MINECRAFT_CONNECTION_FIELD.get(inboundConnection); + Object minecraftConnection = this.getMinecraftConnection(); CLOSE_CHANNEL_METHOD.invoke(minecraftConnection); } catch (Exception e) { @@ -102,4 +124,32 @@ public void disconnect() { } } + private Object getMinecraftConnection() { + try { + switch (this.connectionType) { + case LEGACY: + return LEGACY_MINECRAFT_CONNECTION_FIELD.get(inboundConnection); + case INITIAL_INBOUND: + return MINECRAFT_CONNECTION_FIELD.get(inboundConnection); + case LOGIN_INBOUND: { + // starts as login_inbound, get delegate initial_inbound + Object initialInboundConnection = LOGIN_INBOUND_CONNECTION_DELEGATE_FIELD.get(this.inboundConnection); + return MINECRAFT_CONNECTION_FIELD.get(initialInboundConnection); + } + } + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + + return null; + } + + public ConnectionType getConnectionType() { + return connectionType; + } + + enum ConnectionType { + LOGIN_INBOUND, INITIAL_INBOUND, LEGACY; + } + } diff --git a/src/main/resources/bungee.yml b/src/main/resources/bungee.yml index c9a6bdc..8e92a62 100644 --- a/src/main/resources/bungee.yml +++ b/src/main/resources/bungee.yml @@ -1,5 +1,5 @@ name: TCPShield -version: 2.5.4 +version: 2.5.5 main: net.tcpshield.tcpshield.bungee.TCPShieldBungee author: https://tcpshield.com softdepends: diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 57bd645..cdde08c 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,5 +1,5 @@ name: TCPShield -version: 2.5.4 +version: 2.5.5 main: net.tcpshield.tcpshield.bukkit.TCPShieldBukkit softdepend: - ProtocolLib diff --git a/src/main/resources/velocity-plugin.json b/src/main/resources/velocity-plugin.json index b92b930..5c0f1ea 100644 --- a/src/main/resources/velocity-plugin.json +++ b/src/main/resources/velocity-plugin.json @@ -1,7 +1,7 @@ { "id": "tcpshield", "name": "TCPShield", - "version": "2.5.4", + "version": "2.5.5", "description": "TCPShield IP parsing capabilities for Velocity", "authors": [ "TCPShield"