diff options
| author | Aizistral | 2022-09-18 01:08:03 +0200 |
|---|---|---|
| committer | Aizistral | 2022-09-18 01:08:03 +0200 |
| commit | 8e221ee3c86df941f647f86dbbb858c93a6178bd (patch) | |
| tree | 8b3bfe71e8b24eff8f3e30c4d62f75c1d9249474 /src | |
| parent | ff9550b7a68ddd663e418879715481ba98f1a26d (diff) | |
More work on chat encryption
Diffstat (limited to 'src')
12 files changed, 222 insertions, 11 deletions
diff --git a/src/main/java/com/aizistral/nochatreports/NoChatReportsClient.java b/src/main/java/com/aizistral/nochatreports/NoChatReportsClient.java index bc43f21..55a31f5 100644 --- a/src/main/java/com/aizistral/nochatreports/NoChatReportsClient.java +++ b/src/main/java/com/aizistral/nochatreports/NoChatReportsClient.java @@ -19,6 +19,7 @@ import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents; import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; import net.fabricmc.fabric.api.client.screen.v1.ScreenEvents; import net.fabricmc.fabric.api.networking.v1.PacketSender; +import net.minecraft.client.GuiMessageTag; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.screens.ConnectScreen; import net.minecraft.client.gui.screens.DisconnectedScreen; diff --git a/src/main/java/com/aizistral/nochatreports/config/NCRConfigClient.java b/src/main/java/com/aizistral/nochatreports/config/NCRConfigClient.java index 371a16c..e8472ad 100644 --- a/src/main/java/com/aizistral/nochatreports/config/NCRConfigClient.java +++ b/src/main/java/com/aizistral/nochatreports/config/NCRConfigClient.java @@ -1,14 +1,19 @@ package com.aizistral.nochatreports.config; +import java.security.InvalidKeyException; import java.util.ArrayList; import java.util.List; +import java.util.Optional; import com.aizistral.nochatreports.core.ServerSafetyLevel; +import com.aizistral.nochatreports.encryption.AESChatEncryption; +import com.aizistral.nochatreports.encryption.ChatEncryption; import com.aizistral.nochatreports.gui.UnsafeServerScreen; import com.aizistral.nochatreports.mixins.client.MixinChatListener; import com.aizistral.nochatreports.mixins.client.MixinToastComponent; import net.minecraft.client.multiplayer.resolver.ServerAddress; +import net.minecraft.util.StringUtil; public final class NCRConfigClient extends JSONConfig { protected static final String FILE_NAME = "NoChatReports/NCR-Client.json"; @@ -17,7 +22,8 @@ public final class NCRConfigClient extends JSONConfig { hideYellowChatIndicators = true, hideGrayChatIndicators = true, hideWarningToast = true, alwaysHideReportButton = false, disableTelemetry = true, showReloadButton = true, whitelistAllServers = false, verifiedIconEnabled = true, showNCRButton = true, - enableMod = true; + enableMod = true, enableChatEncryption = false; + protected String chatEncryptionKey = "", chatEncryptionAlgorithm = "AES+Base64"; protected int verifiedIconOffsetX = 0, verifiedIconOffsetY = 0; protected NCRConfigClient() { @@ -195,4 +201,43 @@ public final class NCRConfigClient extends JSONConfig { return this.enableMod; } + /** + * @return Whether or not chat encryption is enabled. + */ + + public boolean enableChatEncryption() { + return this.enableChatEncryption; + } + + /** + * @return Current chat encryption algorithm. + */ + + public String chatEncryptionAlgorithm() { + return this.chatEncryptionAlgorithm; + } + + /** + * @return Base64-encoded AES encryption key (hopefully). + */ + + public String chatEncryptionKey() { + return this.chatEncryptionKey; + } + + /** + * @return Chat encryption, if such is enabled and user specified valid key in config. + */ + + public Optional<ChatEncryption> chatEncryption() { + if (!this.enableChatEncryption() || StringUtil.isNullOrEmpty(this.chatEncryptionKey)) + return Optional.empty(); + + try { + return Optional.of(new AESChatEncryption(this.chatEncryptionKey)); + } catch (InvalidKeyException ex) { + return Optional.empty(); + } + } + } diff --git a/src/main/java/com/aizistral/nochatreports/encryption/AESChatEncryption.java b/src/main/java/com/aizistral/nochatreports/encryption/AESChatEncryption.java index d899235..fd3b7e5 100644 --- a/src/main/java/com/aizistral/nochatreports/encryption/AESChatEncryption.java +++ b/src/main/java/com/aizistral/nochatreports/encryption/AESChatEncryption.java @@ -18,13 +18,11 @@ import javax.crypto.spec.SecretKeySpec; import net.minecraft.server.network.ServerLoginPacketListenerImpl; public class AESChatEncryption extends ChatEncryption { - private static final Base64.Encoder ENCODER = Base64.getEncoder(); - private static final Base64.Decoder DECODER = Base64.getDecoder(); private final SecretKey key; private final Cipher encryptor, decryptor; public AESChatEncryption(String key) throws InvalidKeyException { - this(new SecretKeySpec(key.getBytes(), "AES")); + this(new SecretKeySpec(BASE64_DECODER.decode(key), "AES")); } public AESChatEncryption(SecretKey key) throws InvalidKeyException { @@ -46,7 +44,7 @@ public class AESChatEncryption extends ChatEncryption { @Override public String encrypt(String message) { try { - return ENCODER.encodeToString(this.encryptor.doFinal(message.getBytes())); + return BASE64_ENCODER.encodeToString(this.encryptor.doFinal(message.getBytes())); } catch (IllegalBlockSizeException | BadPaddingException ex) { throw new RuntimeException(ex); } @@ -55,7 +53,7 @@ public class AESChatEncryption extends ChatEncryption { @Override public String decrypt(String message) { try { - return new String(this.decryptor.doFinal(DECODER.decode(message)), StandardCharsets.UTF_8); + return new String(this.decryptor.doFinal(BASE64_DECODER.decode(message)), StandardCharsets.UTF_8); } catch (IllegalBlockSizeException | BadPaddingException ex) { throw new RuntimeException(ex); } @@ -63,12 +61,12 @@ public class AESChatEncryption extends ChatEncryption { @Override public String getAlgorithm() { - return this.key.getAlgorithm() + "Base64"; + return this.key.getAlgorithm() + "+Base64"; } @Override public String getKey() { - return new String(this.key.getEncoded(), StandardCharsets.UTF_8); + return BASE64_ENCODER.encodeToString(this.key.getEncoded()); } public static AESChatEncryption withRandomKey() { diff --git a/src/main/java/com/aizistral/nochatreports/encryption/ChatEncryption.java b/src/main/java/com/aizistral/nochatreports/encryption/ChatEncryption.java index 6a8582f..44a50bc 100644 --- a/src/main/java/com/aizistral/nochatreports/encryption/ChatEncryption.java +++ b/src/main/java/com/aizistral/nochatreports/encryption/ChatEncryption.java @@ -1,6 +1,11 @@ package com.aizistral.nochatreports.encryption; +import java.util.Base64; +import java.util.List; + public abstract class ChatEncryption { + protected static final Base64.Encoder BASE64_ENCODER = Base64.getEncoder(); + protected static final Base64.Decoder BASE64_DECODER = Base64.getDecoder(); protected ChatEncryption() { // NO-OP diff --git a/src/main/java/com/aizistral/nochatreports/mixins/client/MixinChatComponent.java b/src/main/java/com/aizistral/nochatreports/mixins/client/MixinChatComponent.java new file mode 100644 index 0000000..088d28d --- /dev/null +++ b/src/main/java/com/aizistral/nochatreports/mixins/client/MixinChatComponent.java @@ -0,0 +1,92 @@ +package com.aizistral.nochatreports.mixins.client; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.ModifyArg; +import org.spongepowered.asm.mixin.injection.ModifyArgs; +import org.spongepowered.asm.mixin.injection.ModifyVariable; +import org.spongepowered.asm.mixin.injection.At.Shift; + +import com.aizistral.nochatreports.config.NCRConfig; +import com.aizistral.nochatreports.config.NCRConfigClient; +import com.aizistral.nochatreports.encryption.ChatEncryption; + +import net.minecraft.ChatFormatting; +import net.minecraft.client.GuiMessage; +import net.minecraft.client.GuiMessageTag; +import net.minecraft.client.gui.components.ChatComponent; +import net.minecraft.network.chat.CommonComponents; +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.ComponentContents; +import net.minecraft.network.chat.FormattedText; +import net.minecraft.network.chat.MutableComponent; +import net.minecraft.network.chat.contents.LiteralContents; +import net.minecraft.network.chat.contents.TranslatableContents; + +@Mixin(ChatComponent.class) +public class MixinChatComponent { + private static final GuiMessageTag.Icon ENCRYPTED_ICON = GuiMessageTag.Icon.valueOf("CHAT_NCR_ENCRYPTED"); + private boolean lastMessageEncrypted; + private Component lastMessageOriginal; + + @ModifyVariable(method = "addRecentChat", at = @At("HEAD"), argsOnly = true) + private String onAddRecentChat(String message) { + if (!message.startsWith("/")) + return NCRConfig.getClient().chatEncryption().map(e -> e.decrypt(message)).orElse(message); + else + return message; + } + + @ModifyVariable(method = "addMessage(Lnet/minecraft/network/chat/Component;" + + "Lnet/minecraft/network/chat/MessageSignature;ILnet/minecraft/client/GuiMessageTag;Z)V", + at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/components/ComponentRenderUtils;" + + "wrapComponents(Lnet/minecraft/network/chat/FormattedText;ILnet/minecraft/client/gui/Font;" + + ")Ljava/util/List;", ordinal = 0, shift = Shift.AFTER), argsOnly = true) + private GuiMessageTag modifyGUITag(GuiMessageTag tag) { + if (this.lastMessageEncrypted) { + this.lastMessageEncrypted = false; + Component tooltip = Component.empty().append(Component.translatable("tag.nochatreports.encrypted", + Component.literal(NCRConfig.getClient().chatEncryptionAlgorithm()) + .withStyle(ChatFormatting.BOLD))).append(CommonComponents.NEW_LINE).append( + Component.translatable("tag.nochatreports.encrypted_original", + this.lastMessageOriginal)); + return new GuiMessageTag(0x8B3EC7, ENCRYPTED_ICON, tooltip, "Encrypted"); + } else + return tag; + } + + @ModifyArg(index = 0, method = "addMessage(Lnet/minecraft/network/chat/Component;" + + "Lnet/minecraft/network/chat/MessageSignature;ILnet/minecraft/client/GuiMessageTag;Z)V", + at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/components/ComponentRenderUtils;" + + "wrapComponents(Lnet/minecraft/network/chat/FormattedText;I" + + "Lnet/minecraft/client/gui/Font;)Ljava/util/List;", ordinal = 0)) + private FormattedText modifyGUIMessage(FormattedText msg) { + var optional = NCRConfig.getClient().chatEncryption(); + + if (optional.isEmpty()) + return msg; + ChatEncryption encryption = optional.get(); + this.lastMessageOriginal = Component.Serializer.fromJson(Component.Serializer.toStableJson(((Component) msg))); + ComponentContents contents = ((Component)msg).getContents(); + + if (contents instanceof TranslatableContents translatable) { + for (Object arg : translatable.args) { + if (arg instanceof MutableComponent mutable + && mutable.getContents() instanceof LiteralContents literal) { + try { + String decrypted = encryption.decrypt(literal.text()); + if (decrypted.startsWith("#%")) { + mutable.contents = new LiteralContents(decrypted.substring(2, decrypted.length())); + this.lastMessageEncrypted = true; + } + } catch (Exception ex) { + continue; + } + } + } + } + + return msg; + } + +} diff --git a/src/main/java/com/aizistral/nochatreports/mixins/client/MixinChatScreen.java b/src/main/java/com/aizistral/nochatreports/mixins/client/MixinChatScreen.java index e5437b6..7ff6207 100644 --- a/src/main/java/com/aizistral/nochatreports/mixins/client/MixinChatScreen.java +++ b/src/main/java/com/aizistral/nochatreports/mixins/client/MixinChatScreen.java @@ -7,8 +7,10 @@ import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; import com.aizistral.nochatreports.config.NCRConfig; +import com.aizistral.nochatreports.config.NCRConfigClient; import com.aizistral.nochatreports.core.ServerSafetyLevel; import com.aizistral.nochatreports.core.ServerSafetyState; import com.mojang.blaze3d.systems.RenderSystem; @@ -44,6 +46,16 @@ public abstract class MixinChatScreen extends Screen { throw new IllegalStateException("Can't touch this"); } + @Inject(method = "normalizeChatMessage", at = @At("RETURN"), cancellable = true) + public void onBeforeMessage(String original, CallbackInfoReturnable<String> info) { + String message = info.getReturnValue(); + + if (!message.isEmpty() && !message.startsWith("/")) { + NCRConfig.getClient().chatEncryption().ifPresent(e -> + info.setReturnValue(e.encrypt("#%" + message))); + } + } + @Inject(method = "init", at = @At("HEAD")) private void onInit(CallbackInfo info) { if (!NCRConfig.getClient().showServerSafety() || !NCRConfig.getClient().enableMod()) diff --git a/src/main/java/com/aizistral/nochatreports/mixins/client/MixinGuiMessageTag.java b/src/main/java/com/aizistral/nochatreports/mixins/client/MixinGuiMessageTag.java index f6baf82..1c18c61 100644 --- a/src/main/java/com/aizistral/nochatreports/mixins/client/MixinGuiMessageTag.java +++ b/src/main/java/com/aizistral/nochatreports/mixins/client/MixinGuiMessageTag.java @@ -7,7 +7,10 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; import com.aizistral.nochatreports.config.NCRConfig; +import net.minecraft.ChatFormatting; import net.minecraft.client.GuiMessageTag; +import net.minecraft.client.GuiMessageTag.Icon; +import net.minecraft.network.chat.Component; @Mixin(GuiMessageTag.class) public class MixinGuiMessageTag { diff --git a/src/main/java/com/aizistral/nochatreports/mixins/client/MixinGuiMessageTagIcon.java b/src/main/java/com/aizistral/nochatreports/mixins/client/MixinGuiMessageTagIcon.java new file mode 100644 index 0000000..6ef1488 --- /dev/null +++ b/src/main/java/com/aizistral/nochatreports/mixins/client/MixinGuiMessageTagIcon.java @@ -0,0 +1,47 @@ +package com.aizistral.nochatreports.mixins.client; + +import java.util.Arrays; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Mutable; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.gen.Invoker; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import com.mojang.blaze3d.systems.RenderSystem; +import com.mojang.blaze3d.vertex.PoseStack; + +import net.minecraft.client.GuiMessageTag; +import net.minecraft.client.GuiMessageTag.Icon; +import net.minecraft.client.gui.GuiComponent; +import net.minecraft.resources.ResourceLocation; + +@Mixin(GuiMessageTag.Icon.class) +public abstract class MixinGuiMessageTagIcon { + private static final ResourceLocation TEXTURE_NCR = new ResourceLocation("nochatreports", "textures/gui/encrypted_tag.png"); + @Shadow(aliases = "field_39768") @Mutable static Icon[] $VALUES; + + @Invoker("<init>") + static Icon create(String name, int ordinal, int u, int v, int width, int height) { + throw new IllegalStateException("Invoker transformation failed"); + } + + @Inject(method = "draw", at = @At("HEAD"), cancellable = true) + public void onDraw(PoseStack stack, int x, int y, CallbackInfo info) { + if (((Icon)(Object)this).name().equals("CHAT_NCR_ENCRYPTED")) { + RenderSystem.setShaderTexture(0, TEXTURE_NCR); + GuiComponent.blit(stack, x, y, 0, 0, 9, 9, 9, 9); + info.cancel(); + } + } + + static { + var ordinal = $VALUES.length; + var values = Arrays.copyOf($VALUES, ordinal + 1); + values[ordinal] = create("CHAT_NCR_ENCRYPTED", ordinal, 9, 0, 9, 9); + $VALUES = values; + } + +} diff --git a/src/main/resources/assets/nochatreports/lang/en_us.json b/src/main/resources/assets/nochatreports/lang/en_us.json index 58d07fd..c5920ce 100644 --- a/src/main/resources/assets/nochatreports/lang/en_us.json +++ b/src/main/resources/assets/nochatreports/lang/en_us.json @@ -13,9 +13,11 @@ "gui.nochatreports.unsafe_server.check": "Do not show again for this server", "gui.nochatrepords.reload_config_tooltip": "Reload No Chat Reports config. Use carefully.", "gui.nochatreports.verified_server": "§a§lSafe Server§r\nWe could verify that this server has No Chat Reports installed, or prevents chat reports in some other known way. You can chat there safely.", - "gui.nochatreports.ncr_toggle_tooltip": "§lNo Chat Reports:§r %1$s\nDisable or enable most of the mod's client-sided functionality. You may have to do this to join some Paper/Velocity servers, as they are known for causing issues with NCR.\nCertain networking-unrelated things, such as chat trust status tweaks and removal of vanilla's \"Chat messages can't be verified\" toast will still work; they can be disabled in config individually.", + "gui.nochatreports.ncr_toggle_tooltip": "§lNo Chat Reports:§r %1$s\nDisable or enable most of the mod's client-sided functionality. You may have to do this to join some Paper/Velocity servers, as they are known for causing issues with NCR.\nCertain networking-unrelated things, such as encryption, chat trust status tweaks and removal of vanilla's \"Chat messages can't be verified\" toast will still work; they can be disabled in config individually.", "gui.nochatreports.ncr_state_on": "§a§lEnabled§r", "gui.nochatreports.ncr_state_off": "§c§lDisabled§r", + "tag.nochatreports.encrypted": "Encrypted using: %1$s", + "tag.nochatreports.encrypted_original": "Undecrypted: %1$s", "configuration.NoChatReports.config": "No Chat Reports", diff --git a/src/main/resources/assets/nochatreports/textures/gui/encrypted_tag.png b/src/main/resources/assets/nochatreports/textures/gui/encrypted_tag.png Binary files differnew file mode 100644 index 0000000..1b4f8c5 --- /dev/null +++ b/src/main/resources/assets/nochatreports/textures/gui/encrypted_tag.png diff --git a/src/main/resources/nochatreports.accesswidener b/src/main/resources/nochatreports.accesswidener index de7f190..67473bb 100644 --- a/src/main/resources/nochatreports.accesswidener +++ b/src/main/resources/nochatreports.accesswidener @@ -5,4 +5,8 @@ accessible field net/minecraft/client/gui/screens/SimpleOptionsSubScreen list Ln accessible field net/minecraft/client/gui/components/ImageButton xTexStart I accessible field net/minecraft/client/gui/components/ImageButton yTexStart I mutable field net/minecraft/client/gui/components/ImageButton xTexStart I -mutable field net/minecraft/client/gui/components/ImageButton yTexStart I
\ No newline at end of file +mutable field net/minecraft/client/gui/components/ImageButton yTexStart I +accessible field net/minecraft/network/chat/contents/TranslatableContents args [Ljava/lang/Object; +mutable field net/minecraft/network/chat/contents/TranslatableContents args [Ljava/lang/Object; +accessible field net/minecraft/network/chat/MutableComponent contents Lnet/minecraft/network/chat/ComponentContents; +mutable field net/minecraft/network/chat/MutableComponent contents Lnet/minecraft/network/chat/ComponentContents;
\ No newline at end of file diff --git a/src/main/resources/nochatreports.mixins.json b/src/main/resources/nochatreports.mixins.json index 113959c..4b877e3 100644 --- a/src/main/resources/nochatreports.mixins.json +++ b/src/main/resources/nochatreports.mixins.json @@ -30,7 +30,9 @@ "client.MixinRealmsConnect", "client.MixinServerData", "client.MixinServerStatusPinger$1", - "client.MixinOnlineServerEntry" + "client.MixinOnlineServerEntry", + "client.MixinChatComponent", + "client.MixinGuiMessageTagIcon" ], "server": [ "server.MixinDedicatedServer" |
