From 2bd687b19d99d290b482ded71777b4a532d6c604 Mon Sep 17 00:00:00 2001 From: EnZaXD <60033407+FlorianMichael@users.noreply.github.com> Date: Sun, 26 Nov 2023 15:34:55 +0100 Subject: [PATCH] Implemented ViaVersion's SignableCommandArgumentsProvider (#633) --- .../Protocol1_18_2To1_19.java | 40 ++++++++++++----- .../Protocol1_19_1To1_19_3.java | 27 +++++++++++- .../Protocol1_19To1_19_1.java | 44 ++++++++++++++++--- 3 files changed, 93 insertions(+), 18 deletions(-) diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_18_2to1_19/Protocol1_18_2To1_19.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_18_2to1_19/Protocol1_18_2To1_19.java index edfcb124..1f39b908 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_18_2to1_19/Protocol1_18_2To1_19.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_18_2to1_19/Protocol1_18_2To1_19.java @@ -29,9 +29,11 @@ import com.viaversion.viabackwards.protocol.protocol1_18_2to1_19.packets.EntityP import com.viaversion.viabackwards.protocol.protocol1_18_2to1_19.storage.DimensionRegistryStorage; import com.viaversion.viabackwards.protocol.protocol1_18_2to1_19.storage.NonceStorage; import com.viaversion.viabackwards.protocol.protocol1_19to1_19_1.Protocol1_19To1_19_1; +import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.minecraft.RegistryType; import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_19; +import com.viaversion.viaversion.api.minecraft.signature.SignableCommandArgumentsProvider; import com.viaversion.viaversion.api.minecraft.signature.model.DecoratableMessage; import com.viaversion.viaversion.api.minecraft.signature.model.MessageMetadata; import com.viaversion.viaversion.api.minecraft.signature.storage.ChatSession1_19_0; @@ -53,8 +55,10 @@ import com.viaversion.viaversion.rewriter.CommandRewriter; import com.viaversion.viaversion.rewriter.ComponentRewriter; import com.viaversion.viaversion.rewriter.StatisticsRewriter; import com.viaversion.viaversion.rewriter.TagRewriter; +import com.viaversion.viaversion.util.Pair; import java.time.Instant; +import java.util.List; import java.util.UUID; import java.util.concurrent.ThreadLocalRandom; @@ -227,28 +231,44 @@ public final class Protocol1_18_2To1_19 extends BackwardsProtocol wrapper.write(Type.LONG, Instant.now().toEpochMilli())); // Timestamp - create(Type.LONG, 0L); // Salt handler(wrapper -> { final ChatSession1_19_0 chatSession = wrapper.user().get(ChatSession1_19_0.class); + final UUID sender = wrapper.user().getProtocolInfo().getUuid(); + final Instant timestamp = Instant.now(); + final long salt = ThreadLocalRandom.current().nextLong(); + + wrapper.write(Type.LONG, timestamp.toEpochMilli()); // Timestamp + wrapper.write(Type.LONG, chatSession != null ? salt : 0L); // Salt + final String message = wrapper.get(Type.STRING, 0); if (!message.isEmpty() && message.charAt(0) == '/') { + final String command = message.substring(1); + wrapper.setPacketType(ServerboundPackets1_19.CHAT_COMMAND); - wrapper.set(Type.STRING, 0, message.substring(1)); - wrapper.write(Type.VAR_INT, 0); // No signatures + wrapper.set(Type.STRING, 0, command); + + final SignableCommandArgumentsProvider argumentsProvider = Via.getManager().getProviders().get(SignableCommandArgumentsProvider.class); + if (chatSession != null && argumentsProvider != null) { + final MessageMetadata metadata = new MessageMetadata(sender, timestamp, salt); + + final List> arguments = argumentsProvider.getSignableArguments(command); + wrapper.write(Type.VAR_INT, arguments.size()); + for (final Pair argument : arguments) { + final byte[] signature = chatSession.signChatMessage(metadata, new DecoratableMessage(argument.value())); + + wrapper.write(Type.STRING, argument.key()); + wrapper.write(Type.BYTE_ARRAY_PRIMITIVE, signature); + } + } else { + wrapper.write(Type.VAR_INT, 0); // No signatures + } } else { if (chatSession != null) { - final UUID sender = wrapper.user().getProtocolInfo().getUuid(); - final Instant timestamp = Instant.now(); - final long salt = ThreadLocalRandom.current().nextLong(); - final MessageMetadata metadata = new MessageMetadata(sender, timestamp, salt); final DecoratableMessage decoratableMessage = new DecoratableMessage(message); final byte[] signature = chatSession.signChatMessage(metadata, decoratableMessage); - wrapper.set(Type.LONG, 0, timestamp.toEpochMilli()); // Timestamp - wrapper.set(Type.LONG, 1, salt); // Salt wrapper.write(Type.BYTE_ARRAY_PRIMITIVE, signature); // Signature } else { wrapper.write(Type.BYTE_ARRAY_PRIMITIVE, EMPTY_BYTES); // Signature diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_1to1_19_3/Protocol1_19_1To1_19_3.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_1to1_19_3/Protocol1_19_1To1_19_3.java index 7242ef87..869e597a 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_1to1_19_3/Protocol1_19_1To1_19_3.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19_1to1_19_3/Protocol1_19_1To1_19_3.java @@ -28,11 +28,13 @@ import com.viaversion.viabackwards.protocol.protocol1_19_1to1_19_3.storage.ChatS import com.viaversion.viabackwards.protocol.protocol1_19_1to1_19_3.storage.ChatTypeStorage1_19_3; import com.viaversion.viabackwards.protocol.protocol1_19_1to1_19_3.storage.NonceStorage; import com.viaversion.viabackwards.protocol.protocol1_19to1_19_1.Protocol1_19To1_19_1; +import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.minecraft.PlayerMessageSignature; import com.viaversion.viaversion.api.minecraft.ProfileKey; import com.viaversion.viaversion.api.minecraft.RegistryType; import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_19_3; +import com.viaversion.viaversion.api.minecraft.signature.SignableCommandArgumentsProvider; import com.viaversion.viaversion.api.minecraft.signature.model.MessageMetadata; import com.viaversion.viaversion.api.minecraft.signature.storage.ChatSession1_19_3; import com.viaversion.viaversion.api.protocol.packet.State; @@ -55,8 +57,10 @@ import com.viaversion.viaversion.rewriter.ComponentRewriter; import com.viaversion.viaversion.rewriter.StatisticsRewriter; import com.viaversion.viaversion.rewriter.TagRewriter; import com.viaversion.viaversion.util.CipherUtil; +import com.viaversion.viaversion.util.Pair; import java.util.BitSet; +import java.util.List; public final class Protocol1_19_1To1_19_3 extends BackwardsProtocol { @@ -286,14 +290,35 @@ public final class Protocol1_19_1To1_19_3 extends BackwardsProtocol { + final ChatSession1_19_3 chatSession = wrapper.user().get(ChatSession1_19_3.class); + final SignableCommandArgumentsProvider argumentsProvider = Via.getManager().getProviders().get(SignableCommandArgumentsProvider.class); + + final String command = wrapper.get(Type.STRING, 0); + final long timestamp = wrapper.get(Type.LONG, 0); + final long salt = wrapper.get(Type.LONG, 1); + final int signatures = wrapper.read(Type.VAR_INT); - wrapper.write(Type.VAR_INT, 0); for (int i = 0; i < signatures; i++) { wrapper.read(Type.STRING); // Name wrapper.read(Type.BYTE_ARRAY_PRIMITIVE); // Signature } wrapper.read(Type.BOOLEAN); // Signed preview + if (chatSession != null && argumentsProvider != null) { + final MessageMetadata metadata = new MessageMetadata(null, timestamp, salt); + + final List> arguments = argumentsProvider.getSignableArguments(command); + wrapper.write(Type.VAR_INT, arguments.size()); + for (final Pair argument : arguments) { + final byte[] signature = chatSession.signChatMessage(metadata, argument.value(), new PlayerMessageSignature[0]); + + wrapper.write(Type.STRING, argument.key()); + wrapper.write(Protocol1_19_1To1_19_3.SIGNATURE_BYTES_TYPE, signature); + } + } else { + wrapper.write(Type.VAR_INT, 0); // No signatures + } + final int offset = 0; final BitSet acknowledged = new BitSet(20); wrapper.write(Type.VAR_INT, offset); diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19to1_19_1/Protocol1_19To1_19_1.java b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19to1_19_1/Protocol1_19To1_19_1.java index ffac604c..7a2f6ffb 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19to1_19_1/Protocol1_19To1_19_1.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/protocol1_19to1_19_1/Protocol1_19To1_19_1.java @@ -26,10 +26,12 @@ import com.viaversion.viabackwards.protocol.protocol1_19to1_19_1.storage.ChatReg import com.viaversion.viabackwards.protocol.protocol1_19to1_19_1.storage.ChatRegistryStorage1_19_1; import com.viaversion.viabackwards.protocol.protocol1_19to1_19_1.storage.NonceStorage; import com.viaversion.viabackwards.protocol.protocol1_19to1_19_1.storage.ReceivedMessagesStorage; +import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.minecraft.PlayerMessageSignature; import com.viaversion.viaversion.api.minecraft.ProfileKey; import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_19; +import com.viaversion.viaversion.api.minecraft.signature.SignableCommandArgumentsProvider; import com.viaversion.viaversion.api.minecraft.signature.model.DecoratableMessage; import com.viaversion.viaversion.api.minecraft.signature.model.MessageMetadata; import com.viaversion.viaversion.api.minecraft.signature.storage.ChatSession1_19_1; @@ -55,6 +57,7 @@ import com.viaversion.viaversion.protocols.protocol1_19to1_18_2.ServerboundPacke import com.viaversion.viaversion.protocols.protocol1_19to1_18_2.packets.EntityPackets; import com.viaversion.viaversion.rewriter.ComponentRewriter; import com.viaversion.viaversion.util.CipherUtil; +import com.viaversion.viaversion.util.Pair; import org.checkerframework.checker.nullness.qual.Nullable; import java.util.ArrayList; @@ -230,18 +233,45 @@ public final class Protocol1_19To1_19_1 extends BackwardsProtocol { - final int signatures = wrapper.passthrough(Type.VAR_INT); - for (int i = 0; i < signatures; i++) { - wrapper.passthrough(Type.STRING); // Argument name + final ReceivedMessagesStorage messagesStorage = wrapper.user().get(ReceivedMessagesStorage.class); + final ChatSession1_19_1 chatSession = wrapper.user().get(ChatSession1_19_1.class); + final SignableCommandArgumentsProvider argumentsProvider = Via.getManager().getProviders().get(SignableCommandArgumentsProvider.class); - // Set empty signature - wrapper.read(Type.BYTE_ARRAY_PRIMITIVE); - wrapper.write(Type.BYTE_ARRAY_PRIMITIVE, EMPTY_BYTES); + if (chatSession != null && argumentsProvider != null) { + final int signatures = wrapper.read(Type.VAR_INT); + for (int i = 0; i < signatures; i++) { + wrapper.read(Type.STRING); // Argument name + wrapper.read(Type.BYTE_ARRAY_PRIMITIVE); // Signature + } + + final UUID sender = wrapper.user().getProtocolInfo().getUuid(); + final String command = wrapper.get(Type.STRING, 0); + final long timestamp = wrapper.get(Type.LONG, 0); + final long salt = wrapper.get(Type.LONG, 1); + + final MessageMetadata metadata = new MessageMetadata(sender, timestamp, salt); + + final List> arguments = argumentsProvider.getSignableArguments(command); + wrapper.write(Type.VAR_INT, arguments.size()); + for (final Pair argument : arguments) { + final byte[] signature = chatSession.signChatMessage(metadata, new DecoratableMessage(argument.value()), messagesStorage.lastSignatures()); + + wrapper.write(Type.STRING, argument.key()); + wrapper.write(Type.BYTE_ARRAY_PRIMITIVE, signature); + } + } else { + final int signatures = wrapper.passthrough(Type.VAR_INT); + for (int i = 0; i < signatures; i++) { + wrapper.passthrough(Type.STRING); // Argument name + + // Set empty signature + wrapper.read(Type.BYTE_ARRAY_PRIMITIVE); + wrapper.write(Type.BYTE_ARRAY_PRIMITIVE, EMPTY_BYTES); + } } wrapper.passthrough(Type.BOOLEAN); // Signed preview - final ReceivedMessagesStorage messagesStorage = wrapper.user().get(ReceivedMessagesStorage.class); messagesStorage.resetUnacknowledgedCount(); wrapper.write(Type.PLAYER_MESSAGE_SIGNATURE_ARRAY, messagesStorage.lastSignatures()); wrapper.write(Type.OPTIONAL_PLAYER_MESSAGE_SIGNATURE, null); // No last unacknowledged