Skip to content

Commit cfbd6ff

Browse files
committed
Support Velocity's new LoginInboundConnection class.
1 parent cf97e37 commit cfbd6ff

File tree

7 files changed

+86
-20
lines changed

7 files changed

+86
-20
lines changed

build.gradle

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import org.yaml.snakeyaml.Yaml
55

66
import java.nio.charset.StandardCharsets
77

8-
98
// --
109
// Building
1110
// --
@@ -24,15 +23,13 @@ plugins {
2423
id 'idea'
2524
}
2625

27-
2826
// --
2927
// Variables
3028
// --
31-
version = '2.5.4'
29+
version = '2.5.5'
3230
group = 'net.tcpshield.tcpshield'
3331
archivesBaseName = 'TCPShield'
3432

35-
3633
// --
3734
// Misc.
3835
// --
@@ -68,7 +65,6 @@ compileJava {
6865
options.encoding = 'UTF-8'
6966
}
7067

71-
7268
// --
7369
// Dependencies
7470
// --
@@ -97,7 +93,6 @@ repositories {
9793
}
9894

9995
dependencies {
100-
10196
// Bukkit
10297
compileOnly group: 'org.spigotmc', name: 'spigot-api', version: '1.11-R0.1-SNAPSHOT'
10398
compileOnly group: 'com.comphenix.protocol', name: 'ProtocolLib', version: '4.4.0'
@@ -180,4 +175,4 @@ void updateJsons() {
180175
}
181176
}
182177

183-
build.dependsOn updateVersion
178+
build.dependsOn updateVersion

src/main/java/net/tcpshield/tcpshield/velocity/handler/VelocityHandshakeHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public void onProxyPing(ProxyPingEvent e) {
4343

4444
private void handleEvent(InboundConnection connection, String debugSource) {
4545
VelocityPlayer player = new VelocityPlayer(connection);
46-
if (player.isLegacy()) {
46+
if (player.getConnectionType() == VelocityPlayer.ConnectionType.LEGACY) {
4747
player.disconnect();
4848
return;
4949
}

src/main/java/net/tcpshield/tcpshield/velocity/handler/VelocityPacket.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ public class VelocityPacket implements PacketProvider {
2222
private static final Field HOSTNAME_FIELD;
2323
private static final Field CLEANED_ADDRESS_FIELD;
2424

25+
// new velocity support
26+
private static Class<?> LOGIN_INBOUND_CONNECTION_CLASS;
27+
private static Field LOGIN_INBOUND_CONNECTION_DELEGATE_FIELD;
28+
2529
static {
2630
try {
2731
Class<?> inboundConnection = Class.forName("com.velocitypowered.proxy.connection.client.InitialInboundConnection");
@@ -32,13 +36,30 @@ public class VelocityPacket implements PacketProvider {
3236
} catch (Exception e) {
3337
throw new InitializationException(new ReflectionException(e));
3438
}
39+
40+
// LoginInboundConnection support
41+
try {
42+
LOGIN_INBOUND_CONNECTION_CLASS = Class.forName("com.velocitypowered.proxy.connection.client.LoginInboundConnection");
43+
LOGIN_INBOUND_CONNECTION_DELEGATE_FIELD = ReflectionUtil.getPrivateField(LOGIN_INBOUND_CONNECTION_CLASS, "delegate");
44+
} catch (Exception e) {
45+
// ignore for old versions of velocity
46+
}
3547
}
3648

3749

3850
private final InboundConnection inboundConnection;
3951
private final String rawPayload;
4052

4153
public VelocityPacket(InboundConnection inboundConnection) {
54+
// support new velocity connection type
55+
if (inboundConnection.getClass() == LOGIN_INBOUND_CONNECTION_CLASS) {
56+
try {
57+
inboundConnection = (InboundConnection) LOGIN_INBOUND_CONNECTION_DELEGATE_FIELD.get(inboundConnection);
58+
} catch (IllegalAccessException e) {
59+
e.printStackTrace();
60+
}
61+
}
62+
4263
this.inboundConnection = inboundConnection;
4364
try {
4465
this.rawPayload = (String) HOSTNAME_FIELD.get(HANDSHAKE_FIELD.get(inboundConnection));

src/main/java/net/tcpshield/tcpshield/velocity/handler/VelocityPlayer.java

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ public class VelocityPlayer implements PlayerProvider {
2626
private static final Field REMOTE_ADDRESS_FIELD;
2727
private static final Method CLOSE_CHANNEL_METHOD;
2828

29+
// new velocity support
30+
private static Class<?> LOGIN_INBOUND_CONNECTION_CLASS;
31+
private static Field LOGIN_INBOUND_CONNECTION_DELEGATE_FIELD;
32+
2933
static {
3034
try {
3135
INITIAL_INBOUND_CONNECTION_CLASS = Class.forName("com.velocitypowered.proxy.connection.client.InitialInboundConnection");
@@ -38,22 +42,39 @@ public class VelocityPlayer implements PlayerProvider {
3842
} catch (Exception e) {
3943
throw new InitializationException(new ReflectionException(e));
4044
}
45+
46+
// LoginInboundConnection support
47+
try {
48+
LOGIN_INBOUND_CONNECTION_CLASS = Class.forName("com.velocitypowered.proxy.connection.client.LoginInboundConnection");
49+
LOGIN_INBOUND_CONNECTION_DELEGATE_FIELD = ReflectionUtil.getPrivateField(LOGIN_INBOUND_CONNECTION_CLASS, "delegate");
50+
} catch (Exception e) {
51+
// ignore for old versions of velocity
52+
}
4153
}
4254

4355

4456
private final InboundConnection inboundConnection;
45-
private final boolean legacy;
57+
// private final boolean legacy;
58+
private final ConnectionType connectionType;
4659
private String ip;
4760

4861
public VelocityPlayer(InboundConnection inboundConnection) {
4962
this.inboundConnection = inboundConnection;
50-
this.legacy = inboundConnection.getClass() != INITIAL_INBOUND_CONNECTION_CLASS;
63+
// this.legacy = inboundConnection.getClass() != INITIAL_INBOUND_CONNECTION_CLASS && inboundConnection.getClass() != LOGIN_INBOUND_CONNECTION_CLASS;
5164
this.ip = inboundConnection.getRemoteAddress().getAddress().getHostAddress();
52-
}
5365

66+
if (this.inboundConnection.getClass() == INITIAL_INBOUND_CONNECTION_CLASS) {
67+
this.connectionType = ConnectionType.INITIAL_INBOUND;
68+
} else if (this.inboundConnection.getClass() == LOGIN_INBOUND_CONNECTION_CLASS) {
69+
this.connectionType = ConnectionType.LOGIN_INBOUND;
70+
} else {
71+
this.connectionType = ConnectionType.LEGACY;
72+
}
73+
}
5474

5575
/**
5676
* Unsupported with Velocity handshakes
77+
*
5778
* @return unknown
5879
*/
5980
@Override
@@ -63,6 +84,7 @@ public String getUUID() {
6384

6485
/**
6586
* Unsupported with Velocity handshakes
87+
*
6688
* @return unknown
6789
*/
6890
@Override
@@ -75,31 +97,59 @@ public String getIP() {
7597
return ip;
7698
}
7799

78-
public boolean isLegacy() {
79-
return legacy;
80-
}
81-
82100
@Override
83101
public void setIP(InetSocketAddress ip) throws PlayerManipulationException {
84102
try {
85103
this.ip = ip.getAddress().getHostAddress();
86104

87-
Object minecraftConnection = MINECRAFT_CONNECTION_FIELD.get(inboundConnection);
105+
Object minecraftConnection = this.getMinecraftConnection();
88106
REMOTE_ADDRESS_FIELD.set(minecraftConnection, ip);
89107
} catch (Exception e) {
90108
throw new PlayerManipulationException(e);
91109
}
92110
}
93111

112+
// public boolean isLegacy() {
113+
// return legacy;
114+
// }
115+
94116
@Override
95117
public void disconnect() {
96118
try {
97-
Object minecraftConnection = legacy ? LEGACY_MINECRAFT_CONNECTION_FIELD.get(inboundConnection) : MINECRAFT_CONNECTION_FIELD.get(inboundConnection);
119+
Object minecraftConnection = this.getMinecraftConnection();
98120

99121
CLOSE_CHANNEL_METHOD.invoke(minecraftConnection);
100122
} catch (Exception e) {
101123
throw new PlayerManipulationException(e);
102124
}
103125
}
104126

127+
private Object getMinecraftConnection() {
128+
try {
129+
switch (this.connectionType) {
130+
case LEGACY:
131+
return LEGACY_MINECRAFT_CONNECTION_FIELD.get(inboundConnection);
132+
case INITIAL_INBOUND:
133+
return MINECRAFT_CONNECTION_FIELD.get(inboundConnection);
134+
case LOGIN_INBOUND: {
135+
// starts as login_inbound, get delegate initial_inbound
136+
Object initialInboundConnection = LOGIN_INBOUND_CONNECTION_DELEGATE_FIELD.get(this.inboundConnection);
137+
return MINECRAFT_CONNECTION_FIELD.get(initialInboundConnection);
138+
}
139+
}
140+
} catch (IllegalAccessException e) {
141+
e.printStackTrace();
142+
}
143+
144+
return null;
145+
}
146+
147+
public ConnectionType getConnectionType() {
148+
return connectionType;
149+
}
150+
151+
enum ConnectionType {
152+
LOGIN_INBOUND, INITIAL_INBOUND, LEGACY;
153+
}
154+
105155
}

src/main/resources/bungee.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: TCPShield
2-
version: 2.5.4
2+
version: 2.5.5
33
main: net.tcpshield.tcpshield.bungee.TCPShieldBungee
44
author: https://tcpshield.com
55
softdepends:

src/main/resources/plugin.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: TCPShield
2-
version: 2.5.4
2+
version: 2.5.5
33
main: net.tcpshield.tcpshield.bukkit.TCPShieldBukkit
44
softdepend:
55
- ProtocolLib

src/main/resources/velocity-plugin.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "tcpshield",
33
"name": "TCPShield",
4-
"version": "2.5.4",
4+
"version": "2.5.5",
55
"description": "TCPShield IP parsing capabilities for Velocity",
66
"authors": [
77
"TCPShield"

0 commit comments

Comments
 (0)