Skip to content

Commit 39a977f

Browse files
authored
Merge pull request #8 from Fuzzlemann/pr
TCPShield v2.1
2 parents d39f520 + 2178727 commit 39a977f

10 files changed

Lines changed: 43 additions & 9 deletions

File tree

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ configurations {
6565
testImplementation.extendsFrom compileOnly
6666
}
6767

68-
version = '2.0'
68+
version = '2.1'
6969
group = 'net.tcpshield.tcpshield'
7070
archivesBaseName = 'TCPShield'
7171

src/main/java/net/tcpshield/tcpshield/HandshakePacketHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public void onHandshake(IPacket packet, IPlayer player) {
9292
private void handleInvalidTimestamp(IPlayer player, long timestamp, long currentTime) {
9393
if (config.isDebug()) {
9494
this.logger.warning(String.format("%s[%s/%s] provided valid handshake information, but timestamp was not valid. " +
95-
"Provided timestamp: %d vs. system timestamp: %d. Please check your machine time.", player.getName(), player.getUUID(), player.getIP(), timestamp, currentTime));
95+
"Provided timestamp: %d vs. system timestamp: %d. Please check your machine time. Timestamp validation mode: %s", player.getName(), player.getUUID(), player.getIP(), timestamp, currentTime, config.getTimestampValidationMode()));
9696
}
9797

9898
if (config.isOnlyProxy()) {

src/main/java/net/tcpshield/tcpshield/ReflectionUtils.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,18 @@ public static Field getDeclaredField(Class<?> clazz, String fieldName) throws No
5858
Field cachedField = CACHED_FIELDS.get(clazz, fieldName);
5959
if (cachedField != null) return cachedField;
6060

61-
Field field = clazz.getDeclaredField(fieldName);
61+
Field field;
62+
try {
63+
field = clazz.getDeclaredField(fieldName);
64+
} catch (NoSuchFieldException e) {
65+
Class<?> superclass = clazz.getSuperclass();
66+
if (superclass != null) {
67+
return getDeclaredField(superclass, fieldName);
68+
} else {
69+
throw e;
70+
}
71+
}
72+
6273
CACHED_FIELDS.put(clazz, fieldName, field);
6374
return field;
6475
}

src/main/java/net/tcpshield/tcpshield/bukkit/TCPShieldBukkit.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ public void onEnable() {
1616
}
1717

1818
private boolean isPaper() {
19+
if (true) return false; // disabled for now
20+
1921
try {
2022
Class.forName("com.destroystokyo.paper.event.player.PlayerHandshakeEvent");
2123
return true;

src/main/java/net/tcpshield/tcpshield/bukkit/paper/impl/PaperPlayerImpl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public void setIP(InetSocketAddress ip) {
3939

4040
@Override
4141
public void disconnect() {
42+
handshakeEvent.setCancelled(false);
4243
handshakeEvent.setFailMessage("Connection failed. Please try again or contract an administrator.");
4344
handshakeEvent.setFailed(true);
4445
}

src/main/java/net/tcpshield/tcpshield/velocity/VelocityHandshakePacketHandler.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import com.velocitypowered.api.proxy.InboundConnection;
77
import net.tcpshield.tcpshield.HandshakePacketHandler;
88
import net.tcpshield.tcpshield.abstraction.IPacket;
9-
import net.tcpshield.tcpshield.abstraction.IPlayer;
109
import net.tcpshield.tcpshield.velocity.impl.VelocityConfigImpl;
1110
import net.tcpshield.tcpshield.velocity.impl.VelocityPacketImpl;
1211
import net.tcpshield.tcpshield.velocity.impl.VelocityPlayerImpl;
@@ -35,7 +34,12 @@ public void onProxyPing(ProxyPingEvent e) {
3534
}
3635

3736
private void handleEvent(InboundConnection connection) {
38-
IPlayer player = new VelocityPlayerImpl(connection);
37+
VelocityPlayerImpl player = new VelocityPlayerImpl(connection);
38+
if (player.isLegacy()) {
39+
player.disconnect();
40+
return;
41+
}
42+
3943
IPacket packet = new VelocityPacketImpl(connection);
4044

4145
handshakePacketHandler.onHandshake(packet, player);

src/main/java/net/tcpshield/tcpshield/velocity/impl/VelocityPacketImpl.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@ public class VelocityPacketImpl implements IPacket {
1111

1212
private static final Field HANDSHAKE_FIELD;
1313
private static final Field HOSTNAME_FIELD;
14+
private static final Field CLEANED_ADDRESS_FIELD;
1415

1516
static {
1617
try {
17-
HANDSHAKE_FIELD = ReflectionUtils.getPrivateField(Class.forName("com.velocitypowered.proxy.connection.client.InitialInboundConnection"), "handshake");
18+
Class<?> inboundConnection = Class.forName("com.velocitypowered.proxy.connection.client.InitialInboundConnection");
19+
20+
HANDSHAKE_FIELD = ReflectionUtils.getPrivateField(inboundConnection, "handshake");
1821
HOSTNAME_FIELD = ReflectionUtils.getPrivateField(Class.forName("com.velocitypowered.proxy.protocol.packet.Handshake"), "serverAddress");
22+
CLEANED_ADDRESS_FIELD = ReflectionUtils.getPrivateField(inboundConnection, "cleanedAddress");
1923
} catch (NoSuchFieldException | ClassNotFoundException e) {
2024
throw new TCPShieldInitializationException(e);
2125
}
@@ -40,6 +44,8 @@ public String getRawPayload() {
4044

4145
@Override
4246
public void modifyOriginalPacket(String hostname) throws Exception {
47+
ReflectionUtils.setFinalField(inboundConnection, CLEANED_ADDRESS_FIELD, hostname);
48+
4349
Object handshake = HANDSHAKE_FIELD.get(inboundConnection);
4450
HOSTNAME_FIELD.set(handshake, hostname);
4551
}

src/main/java/net/tcpshield/tcpshield/velocity/impl/VelocityPlayerImpl.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@
1313
public class VelocityPlayerImpl implements IPlayer {
1414

1515
private static final Field MINECRAFT_CONNECTION_FIELD;
16+
private static final Field LEGACY_MINECRAFT_CONNECTION_FIELD;
1617
private static final Field REMOTE_ADDRESS_FIELD;
1718
private static final Method CLOSE_CHANNEL_METHOD;
1819

1920
static {
2021
try {
2122
MINECRAFT_CONNECTION_FIELD = ReflectionUtils.getPrivateField(Class.forName("com.velocitypowered.proxy.connection.client.InitialInboundConnection"), "connection");
23+
LEGACY_MINECRAFT_CONNECTION_FIELD = ReflectionUtils.getPrivateField(Class.forName("com.velocitypowered.proxy.connection.client.HandshakeSessionHandler$LegacyInboundConnection"), "connection");
2224

2325
Class<?> minecraftConnection = Class.forName("com.velocitypowered.proxy.connection.MinecraftConnection");
2426
REMOTE_ADDRESS_FIELD = ReflectionUtils.getPrivateField(minecraftConnection, "remoteAddress");
@@ -29,10 +31,12 @@ public class VelocityPlayerImpl implements IPlayer {
2931
}
3032

3133
private final InboundConnection inboundConnection;
34+
private final boolean legacy;
3235
private String ip;
3336

3437
public VelocityPlayerImpl(InboundConnection inboundConnection) {
3538
this.inboundConnection = inboundConnection;
39+
this.legacy = inboundConnection.getProtocolVersion().isUnknown() || inboundConnection.getProtocolVersion().isLegacy();
3640
this.ip = inboundConnection.getRemoteAddress().getAddress().getHostAddress();
3741
}
3842

@@ -41,6 +45,7 @@ public String getUUID() {
4145
return "unknown"; // not supported
4246
}
4347

48+
@Override
4449
public String getName() {
4550
return "unknown"; // not supported
4651
}
@@ -50,6 +55,10 @@ public String getIP() {
5055
return ip;
5156
}
5257

58+
public boolean isLegacy() {
59+
return legacy;
60+
}
61+
5362
@Override
5463
public void setIP(InetSocketAddress ip) throws IPModificationFailureException {
5564
this.ip = ip.getAddress().getHostAddress();
@@ -65,7 +74,8 @@ public void setIP(InetSocketAddress ip) throws IPModificationFailureException {
6574
@Override
6675
public void disconnect() {
6776
try {
68-
Object minecraftConnection = MINECRAFT_CONNECTION_FIELD.get(inboundConnection);
77+
Object minecraftConnection = legacy ? LEGACY_MINECRAFT_CONNECTION_FIELD.get(inboundConnection) : MINECRAFT_CONNECTION_FIELD.get(inboundConnection);
78+
6979
CLOSE_CHANNEL_METHOD.invoke(minecraftConnection);
7080
} catch (Exception e) {
7181
throw new RuntimeException(e); // pass exception on

src/main/resources/bungee.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
name: TCPShield
2-
version: '2.0'
2+
version: '2.1'
33
main: net.tcpshield.tcpshield.bungee.TCPShieldBungee
44
author: https://tcpshield.com

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.0'
2+
version: '2.1'
33
main: net.tcpshield.tcpshield.bukkit.TCPShieldBukkit
44
softdepend:
55
- ProtocolLib

0 commit comments

Comments
 (0)