From d987be251f0a17d1ab85104c78649f6d8db0ef0a Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Mon, 21 Aug 2023 19:18:43 +0700 Subject: [PATCH] chomens auth i forgor to commit i made this since yesterday and modified it a bit today few hours ago but forgor to commit :skull: --- .../chipmunk/chipmunkmod/Configuration.java | 14 +++- .../mixin/ClientPlayNetworkHandlerMixin.java | 6 +- .../chipmunkmod/modules/ChomeNSAuth.java | 75 +++++++++++++++++++ .../util/BotValidationUtilities.java | 2 +- src/main/resources/default_config.json | 2 +- 5 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 src/main/java/land/chipmunk/chipmunkmod/modules/ChomeNSAuth.java diff --git a/src/main/java/land/chipmunk/chipmunkmod/Configuration.java b/src/main/java/land/chipmunk/chipmunkmod/Configuration.java index d9f56a6..f7f2021 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/Configuration.java +++ b/src/main/java/land/chipmunk/chipmunkmod/Configuration.java @@ -25,11 +25,23 @@ public class Configuration { public BotInfo hbot = new BotInfo("#", null); public BotInfo sbot = new BotInfo(":", null); public BotInfo chipmunk = new BotInfo("'", null); - public BotInfo chomens = new BotInfo("*", null); + public ChomeNSBotInfo chomens = new ChomeNSBotInfo("*", null, null); public BotInfo kittycorp = new BotInfo("^", null); public TestBotInfo testbot = new TestBotInfo("-", null); } + public static class ChomeNSBotInfo { + public String prefix; + public String key; + public String authKey; + + public ChomeNSBotInfo (String prefix, String key, String authKey) { + this.prefix = prefix; + this.key = key; + this.authKey = authKey; + } + } + public static class TestBotInfo { public String prefix; public String webhookUrl; diff --git a/src/main/java/land/chipmunk/chipmunkmod/mixin/ClientPlayNetworkHandlerMixin.java b/src/main/java/land/chipmunk/chipmunkmod/mixin/ClientPlayNetworkHandlerMixin.java index 020de43..299145e 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/mixin/ClientPlayNetworkHandlerMixin.java +++ b/src/main/java/land/chipmunk/chipmunkmod/mixin/ClientPlayNetworkHandlerMixin.java @@ -48,6 +48,7 @@ public class ClientPlayNetworkHandlerMixin { SongPlayer.INSTANCE.coreReady(); RainbowName.INSTANCE.init(); ChomeNSBotCommandSuggestions.INSTANCE.init(); + ChomeNSAuth.INSTANCE.init(); } @Inject(method = "onPlayerRemove", at = @At("HEAD"), cancellable = true) @@ -78,7 +79,10 @@ public class ClientPlayNetworkHandlerMixin { } try { - if (((TextComponent) message.asComponent().children().get(0)).content().equals(ChomeNSBotCommandSuggestions.ID)) { + final TextComponent suggestionId = ((TextComponent) message.asComponent().children().get(0)); + final TextComponent authId = (TextComponent) message.asComponent(); + + if (suggestionId.content().equals(ChomeNSBotCommandSuggestions.ID) || authId.content().equals(ChomeNSAuth.INSTANCE.id)) { ci.cancel(); } } catch (Exception ignored) {} diff --git a/src/main/java/land/chipmunk/chipmunkmod/modules/ChomeNSAuth.java b/src/main/java/land/chipmunk/chipmunkmod/modules/ChomeNSAuth.java new file mode 100644 index 0000000..5579c15 --- /dev/null +++ b/src/main/java/land/chipmunk/chipmunkmod/modules/ChomeNSAuth.java @@ -0,0 +1,75 @@ +package land.chipmunk.chipmunkmod.modules; + +import com.google.common.hash.Hashing; +import land.chipmunk.chipmunkmod.ChipmunkMod; +import land.chipmunk.chipmunkmod.listeners.Listener; +import land.chipmunk.chipmunkmod.listeners.ListenerManager; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.TextComponent; +import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; +import net.minecraft.text.Text; + +import java.nio.charset.StandardCharsets; +import java.util.List; + +public class ChomeNSAuth extends Listener { + public static final ChomeNSAuth INSTANCE = new ChomeNSAuth(); + + public final String id = "chomens_bot_verify"; + + public ChomeNSAuth () { + ListenerManager.addListener(this); + } + + public void init () {} + + @Override + public void chatMessageReceived(Text message) { + final String authKey = ChipmunkMod.CONFIG.bots.chomens.authKey; + + if (authKey == null) return; + + final Component component = message.asComponent(); + + if (!(component instanceof TextComponent)) return; + + final String id = ((TextComponent) component).content(); + + if (!id.equals(this.id)) return; + + final List children = component.children(); + + if (children.size() != 2) return; + + if (!(children.get(0) instanceof TextComponent)) return; + + final String hash = ((TextComponent) children.get(0)).content(); + + final long time = System.currentTimeMillis() / 10_000; + + final String actual = Hashing.sha256() + // very pro hash input + .hashString(authKey + time, StandardCharsets.UTF_8) + .toString() + .substring(0, 8); + + if (!hash.equals(actual)) return; + + if (!(children.get(1) instanceof TextComponent)) return; + + final String selector = ((TextComponent) children.get(1)).content(); + + final String toSendHash = Hashing.sha256() + // very pro hash input + .hashString(authKey + authKey + time + time, StandardCharsets.UTF_8) + .toString() + .substring(0, 8); + + final Component toSend = Component.text(id) + .append(Component.text(toSendHash)); + + final String toSendString = GsonComponentSerializer.gson().serialize(toSend); + + CommandCore.INSTANCE.run("tellraw " + selector + " " + toSendString); + } +} diff --git a/src/main/java/land/chipmunk/chipmunkmod/util/BotValidationUtilities.java b/src/main/java/land/chipmunk/chipmunkmod/util/BotValidationUtilities.java index 4467000..f74dcac 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/util/BotValidationUtilities.java +++ b/src/main/java/land/chipmunk/chipmunkmod/util/BotValidationUtilities.java @@ -65,7 +65,7 @@ public class BotValidationUtilities { } public static int chomens (String command) throws RuntimeException { - final Configuration.BotInfo info = ChipmunkMod.CONFIG.bots.chomens; + final Configuration.ChomeNSBotInfo info = ChipmunkMod.CONFIG.bots.chomens; final MinecraftClient client = MinecraftClient.getInstance(); diff --git a/src/main/resources/default_config.json b/src/main/resources/default_config.json index 7042c7a..35bf4af 100644 --- a/src/main/resources/default_config.json +++ b/src/main/resources/default_config.json @@ -14,7 +14,7 @@ "hbot": { "prefix": "#", "key": null }, "sbot": { "prefix": ":", "key": null }, "chipmunk": { "prefix": "'", "key": null }, - "chomens": { "prefix": "*", "key": null }, + "chomens": { "prefix": "*", "key": null, "authKey": null }, "kittycorp": { "prefix": "^", "key": null }, "testbot": { "prefix": "-", "webhookUrl": null } },