From 365bcde528fa91744f77c34a27142afcb39251a2 Mon Sep 17 00:00:00 2001 From: Patbox <39821509+Patbox@users.noreply.github.com> Date: Tue, 19 Dec 2023 20:16:50 +0100 Subject: [PATCH] Add some protocol checks --- gradle.properties | 6 +-- .../polymer/core/impl/ClientMetadataKeys.java | 2 +- .../impl/client/InternalClientRegistry.java | 2 +- .../core/impl/networking/S2CPackets.java | 4 +- .../impl/networking/payloads/PayloadUtil.java | 40 +++++++++++++++++++ .../api/payload/PayloadDecoder.java | 16 +++++++- .../networking/impl/UnhandledPayload.java | 12 ++++++ .../impl/client/ClientPacketRegistry.java | 2 + 8 files changed, 76 insertions(+), 8 deletions(-) create mode 100644 polymer-core/src/main/java/eu/pb4/polymer/core/impl/networking/payloads/PayloadUtil.java create mode 100644 polymer-networking/src/main/java/eu/pb4/polymer/networking/impl/UnhandledPayload.java diff --git a/gradle.properties b/gradle.properties index c8817f1e..ad5b7492 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ # Done to increase the memory available to gradle. -org.gradle.jvmargs=-Xmx2G +org.gradle.jvmargs=-Xmx3G # Fabric Properties # check these on https://fabricmc.net/use @@ -12,10 +12,10 @@ fabric_version=0.91.1+1.20.4 maven_group = eu.pb4 -mod_version = 0.7.2 +mod_version = 0.7.3 minecraft_version_supported = ">=1.20.3-" -packet_tweaker_version = 0.5.0+1.20.2-rc1 +packet_tweaker_version = 0.5.1+1.20.4 is_stable = true diff --git a/polymer-core/src/main/java/eu/pb4/polymer/core/impl/ClientMetadataKeys.java b/polymer-core/src/main/java/eu/pb4/polymer/core/impl/ClientMetadataKeys.java index 7255be20..c9e6801b 100644 --- a/polymer-core/src/main/java/eu/pb4/polymer/core/impl/ClientMetadataKeys.java +++ b/polymer-core/src/main/java/eu/pb4/polymer/core/impl/ClientMetadataKeys.java @@ -7,7 +7,7 @@ @ApiStatus.Internal public class ClientMetadataKeys { - public static final Identifier MINECRAFT_PROTOCOL = id("minecraft_protocol"); + public static final Identifier MINECRAFT_PROTOCOL = ServerMetadataKeys.MINECRAFT_PROTOCOL; public static final Identifier BLOCKSTATE_BITS = id("core/blockstate_bits"); public static final Identifier ADVANCED_TOOLTIP = id("core/advanced_tooltip"); } diff --git a/polymer-core/src/main/java/eu/pb4/polymer/core/impl/client/InternalClientRegistry.java b/polymer-core/src/main/java/eu/pb4/polymer/core/impl/client/InternalClientRegistry.java index 26e22b1d..0d61fefd 100644 --- a/polymer-core/src/main/java/eu/pb4/polymer/core/impl/client/InternalClientRegistry.java +++ b/polymer-core/src/main/java/eu/pb4/polymer/core/impl/client/InternalClientRegistry.java @@ -142,7 +142,7 @@ public static void setBlockAt(BlockPos pos, ClientPolymerBlock.State state) { public static void setVersion(String version, @Nullable NbtInt protocolVersion) { serverVersion = version; - serverHasPolymer = !version.isEmpty(); + serverHasPolymer = !version.isEmpty() && serverVersion.startsWith("0.7."); enabled = serverHasPolymer && protocolVersion != null && protocolVersion.intValue() == SharedConstants.getProtocolVersion(); } diff --git a/polymer-core/src/main/java/eu/pb4/polymer/core/impl/networking/S2CPackets.java b/polymer-core/src/main/java/eu/pb4/polymer/core/impl/networking/S2CPackets.java index f868293f..89b14064 100644 --- a/polymer-core/src/main/java/eu/pb4/polymer/core/impl/networking/S2CPackets.java +++ b/polymer-core/src/main/java/eu/pb4/polymer/core/impl/networking/S2CPackets.java @@ -38,8 +38,8 @@ public class S2CPackets { public static final Identifier DEBUG_VALIDATE_STATES = id("debug/validate_states"); - public static final void register(Identifier id, VersionedPayload.Decoder decoder, int... ver) { - PolymerNetworking.registerS2CPayload(id, IntList.of(ver), decoder); + public static void register(Identifier id, VersionedPayload.Decoder decoder, int... ver) { + PolymerNetworking.registerS2CPayload(id, IntList.of(ver), PayloadUtil.checked(decoder)); } static { diff --git a/polymer-core/src/main/java/eu/pb4/polymer/core/impl/networking/payloads/PayloadUtil.java b/polymer-core/src/main/java/eu/pb4/polymer/core/impl/networking/payloads/PayloadUtil.java new file mode 100644 index 00000000..70278109 --- /dev/null +++ b/polymer-core/src/main/java/eu/pb4/polymer/core/impl/networking/payloads/PayloadUtil.java @@ -0,0 +1,40 @@ +package eu.pb4.polymer.core.impl.networking.payloads; + +import eu.pb4.polymer.core.impl.PolymerImpl; +import eu.pb4.polymer.core.impl.ServerMetadataKeys; +import eu.pb4.polymer.core.impl.client.InternalClientRegistry; +import eu.pb4.polymer.networking.api.PolymerNetworking; +import eu.pb4.polymer.networking.api.payload.VersionedPayload; +import net.minecraft.SharedConstants; +import net.minecraft.nbt.NbtInt; +import net.minecraft.network.PacketByteBuf; +import net.minecraft.util.Identifier; +import xyz.nucleoid.packettweaker.PacketContext; + +public interface PayloadUtil { + int PROTOCOL = SharedConstants.getProtocolVersion(); + + static boolean matchesProtocol(PacketContext context) { + var data = PolymerNetworking.getMetadata(context.getClientConnection(), ServerMetadataKeys.MINECRAFT_PROTOCOL, NbtInt.TYPE); + return (data == null || data.intValue() == PROTOCOL) && clientCheck(); + } + + static boolean clientCheck() { + if (PolymerImpl.IS_CLIENT) { + return InternalClientRegistry.enabled; + } + + return true; + } + + static VersionedPayload.Decoder checked(VersionedPayload.Decoder decoder) { + return (PacketContext context, Identifier identifier, int version, PacketByteBuf buf) -> { + if (matchesProtocol(context)) { + try { + return decoder.readPacket(context, identifier, version, buf); + } catch (Throwable ignored) {} + } + return null; + }; + } +} diff --git a/polymer-networking/src/main/java/eu/pb4/polymer/networking/api/payload/PayloadDecoder.java b/polymer-networking/src/main/java/eu/pb4/polymer/networking/api/payload/PayloadDecoder.java index 082f1115..40a3c3df 100644 --- a/polymer-networking/src/main/java/eu/pb4/polymer/networking/api/payload/PayloadDecoder.java +++ b/polymer-networking/src/main/java/eu/pb4/polymer/networking/api/payload/PayloadDecoder.java @@ -1,5 +1,6 @@ package eu.pb4.polymer.networking.api.payload; +import eu.pb4.polymer.networking.impl.UnhandledPayload; import net.minecraft.network.PacketByteBuf; import net.minecraft.network.packet.CustomPayload; import net.minecraft.util.Identifier; @@ -10,6 +11,19 @@ public interface PayloadDecoder { T readPacket(Identifier identifier, PacketByteBuf buf); default PacketByteBuf.PacketReader forPacket(Identifier identifier) { - return (b) -> (CustomPayload) readPacket(identifier, b); + return (b) -> { + try { + var payload = readPacket(identifier, b); + if (payload != null) { + return (CustomPayload) payload; + } + } catch (Throwable e) { + e.printStackTrace(); + throw new RuntimeException("Failed to decode packet '" + identifier +"'", e); + } + + b.skipBytes(b.readableBytes()); + return new UnhandledPayload(identifier); + }; } } diff --git a/polymer-networking/src/main/java/eu/pb4/polymer/networking/impl/UnhandledPayload.java b/polymer-networking/src/main/java/eu/pb4/polymer/networking/impl/UnhandledPayload.java new file mode 100644 index 00000000..c0b19a23 --- /dev/null +++ b/polymer-networking/src/main/java/eu/pb4/polymer/networking/impl/UnhandledPayload.java @@ -0,0 +1,12 @@ +package eu.pb4.polymer.networking.impl; + +import net.minecraft.network.PacketByteBuf; +import net.minecraft.network.packet.CustomPayload; +import net.minecraft.util.Identifier; + +public record UnhandledPayload(Identifier id) implements CustomPayload { + @Override + public void write(PacketByteBuf buf) { + + } +} diff --git a/polymer-networking/src/main/java/eu/pb4/polymer/networking/impl/client/ClientPacketRegistry.java b/polymer-networking/src/main/java/eu/pb4/polymer/networking/impl/client/ClientPacketRegistry.java index 8749a7ce..51b8af43 100644 --- a/polymer-networking/src/main/java/eu/pb4/polymer/networking/impl/client/ClientPacketRegistry.java +++ b/polymer-networking/src/main/java/eu/pb4/polymer/networking/impl/client/ClientPacketRegistry.java @@ -125,10 +125,12 @@ public static void clear(@Nullable ClientCommonNetworkHandler handler) { public static void handleMetadata(MinecraftClient client, ClientCommonNetworkHandler handler, MetadataPayload payload) { synchronized (SERVER_METADATA) { + SERVER_METADATA.clear(); SERVER_METADATA.putAll(payload.map()); } var ext = (ExtClientConnection) ((CommonNetworkHandlerExt) handler).polymerCommon$getConnection(); + ext.polymerNet$getMetadataMap().clear(); ext.polymerNet$getMetadataMap().putAll(payload.map()); PolymerClientNetworking.AFTER_METADATA_RECEIVED.invoke(Runnable::run);