Skip to content

Commit

Permalink
Support Velocity's new LoginInboundConnection class.
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanKeir committed Nov 21, 2021
1 parent cf97e37 commit cfbd6ff
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 20 deletions.
9 changes: 2 additions & 7 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import org.yaml.snakeyaml.Yaml

import java.nio.charset.StandardCharsets


// --
// Building
// --
Expand All @@ -24,15 +23,13 @@ plugins {
id 'idea'
}


// --
// Variables
// --
version = '2.5.4'
version = '2.5.5'
group = 'net.tcpshield.tcpshield'
archivesBaseName = 'TCPShield'


// --
// Misc.
// --
Expand Down Expand Up @@ -68,7 +65,6 @@ compileJava {
options.encoding = 'UTF-8'
}


// --
// Dependencies
// --
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -180,4 +175,4 @@ void updateJsons() {
}
}

build.dependsOn updateVersion
build.dependsOn updateVersion
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -32,13 +36,30 @@ 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
}
}


private final InboundConnection inboundConnection;
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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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
Expand All @@ -63,6 +84,7 @@ public String getUUID() {

/**
* Unsupported with Velocity handshakes
*
* @return unknown
*/
@Override
Expand All @@ -75,31 +97,59 @@ 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) {
throw new PlayerManipulationException(e);
}
}

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;
}

}
2 changes: 1 addition & 1 deletion src/main/resources/bungee.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: TCPShield
version: 2.5.4
version: 2.5.5
main: net.tcpshield.tcpshield.bukkit.TCPShieldBukkit
softdepend:
- ProtocolLib
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/velocity-plugin.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down

0 comments on commit cfbd6ff

Please sign in to comment.