mirror of
https://code.chipmunk.land/ChomeNS/chipmunkmod.git
synced 2025-11-13 22:16:14 +00:00
feat: re-add ChomeNSAuth
refactor: improve the chat stuff in ClientPlayNetworkHandlerMixin a bit
This commit is contained in:
parent
aaf7a44d95
commit
2723483b7b
2 changed files with 132 additions and 12 deletions
|
|
@ -21,6 +21,7 @@ import net.minecraft.network.packet.s2c.play.GameJoinS2CPacket;
|
||||||
import net.minecraft.network.packet.s2c.play.GameMessageS2CPacket;
|
import net.minecraft.network.packet.s2c.play.GameMessageS2CPacket;
|
||||||
import net.minecraft.registry.DynamicRegistryManager;
|
import net.minecraft.registry.DynamicRegistryManager;
|
||||||
import net.minecraft.resource.featuretoggle.FeatureSet;
|
import net.minecraft.resource.featuretoggle.FeatureSet;
|
||||||
|
import net.minecraft.text.PlainTextContent;
|
||||||
import net.minecraft.text.Text;
|
import net.minecraft.text.Text;
|
||||||
import net.minecraft.text.TranslatableTextContent;
|
import net.minecraft.text.TranslatableTextContent;
|
||||||
import org.spongepowered.asm.mixin.Final;
|
import org.spongepowered.asm.mixin.Final;
|
||||||
|
|
@ -57,6 +58,7 @@ public class ClientPlayNetworkHandlerMixin {
|
||||||
CommandCore.INSTANCE.init();
|
CommandCore.INSTANCE.init();
|
||||||
SongPlayer.INSTANCE.coreReady();
|
SongPlayer.INSTANCE.coreReady();
|
||||||
RainbowName.INSTANCE.init();
|
RainbowName.INSTANCE.init();
|
||||||
|
ChomeNSAuth.INSTANCE.init();
|
||||||
ChomeNSBotCommandSuggestions.INSTANCE.init();
|
ChomeNSBotCommandSuggestions.INSTANCE.init();
|
||||||
CustomChat.INSTANCE.init();
|
CustomChat.INSTANCE.init();
|
||||||
}
|
}
|
||||||
|
|
@ -71,36 +73,46 @@ public class ClientPlayNetworkHandlerMixin {
|
||||||
final Text message = packet.content();
|
final Text message = packet.content();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (RainbowName.INSTANCE.enabled) {
|
if (
|
||||||
if (message.getString().contains("Your nickname is now ") || message.getString().contains("Nickname changed.")) {
|
RainbowName.INSTANCE.enabled &&
|
||||||
ci.cancel();
|
(
|
||||||
return;
|
message.getString().contains("Your nickname is now ") ||
|
||||||
}
|
message.getString().contains("Nickname changed.")
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
ci.cancel();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
if (message.getContent() instanceof TranslatableTextContent translatableTextContent) {
|
||||||
final String key = ((TranslatableTextContent) message.getContent()).getKey();
|
final String key = translatableTextContent.getKey();
|
||||||
|
|
||||||
if (key.equals("advMode.setCommand.success") || key.equals("قيادة المجموعة: %s")) {
|
if (key.equals("advMode.setCommand.success") || key.equals("قيادة المجموعة: %s")) {
|
||||||
ci.cancel();
|
ci.cancel();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} catch (ClassCastException ignored) {
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (Listener listener : ListenerManager.listeners) {
|
for (Listener listener : ListenerManager.listeners) {
|
||||||
listener.chatMessageReceived(message);
|
listener.chatMessageReceived(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
if (message.getSiblings().size() > 1) {
|
||||||
final String suggestionId = message.getSiblings().getFirst().getString();
|
final String suggestionId = message.getSiblings().getFirst().getString();
|
||||||
|
|
||||||
if (suggestionId.equals(ChomeNSBotCommandSuggestions.ID)) {
|
if (suggestionId.equals(ChomeNSBotCommandSuggestions.ID)) {
|
||||||
ci.cancel();
|
ci.cancel();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
} catch (Exception ignored) {
|
|
||||||
}
|
}
|
||||||
} catch (Exception ignored) {
|
|
||||||
}
|
if (
|
||||||
|
message.getContent() instanceof PlainTextContent plainTextContent &&
|
||||||
|
plainTextContent.string().equals(ChomeNSAuth.ID)
|
||||||
|
) {
|
||||||
|
ci.cancel();
|
||||||
|
}
|
||||||
|
} catch (Exception ignored) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Inject(method = "sendChatMessage", at = @At("HEAD"), cancellable = true)
|
@Inject(method = "sendChatMessage", at = @At("HEAD"), cancellable = true)
|
||||||
|
|
|
||||||
108
src/main/java/land/chipmunk/chipmunkmod/modules/ChomeNSAuth.java
Normal file
108
src/main/java/land/chipmunk/chipmunkmod/modules/ChomeNSAuth.java
Normal file
|
|
@ -0,0 +1,108 @@
|
||||||
|
package land.chipmunk.chipmunkmod.modules;
|
||||||
|
|
||||||
|
import land.chipmunk.chipmunkmod.listeners.Listener;
|
||||||
|
import land.chipmunk.chipmunkmod.listeners.ListenerManager;
|
||||||
|
import net.fabricmc.loader.api.FabricLoader;
|
||||||
|
import net.kyori.adventure.text.Component;
|
||||||
|
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
||||||
|
import net.minecraft.text.PlainTextContent;
|
||||||
|
import net.minecraft.text.Text;
|
||||||
|
import net.minecraft.text.TextContent;
|
||||||
|
|
||||||
|
import javax.crypto.Cipher;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.security.KeyFactory;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.security.PublicKey;
|
||||||
|
import java.security.spec.InvalidKeySpecException;
|
||||||
|
import java.security.spec.X509EncodedKeySpec;
|
||||||
|
import java.util.Base64;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class ChomeNSAuth implements Listener {
|
||||||
|
public static ChomeNSAuth INSTANCE = new ChomeNSAuth();
|
||||||
|
|
||||||
|
private static PublicKey PUBLIC_KEY;
|
||||||
|
private static final Path PUBLIC_KEY_PATH = FabricLoader.getInstance()
|
||||||
|
.getGameDir()
|
||||||
|
.resolve("data")
|
||||||
|
.resolve("chipmunkmod")
|
||||||
|
.resolve("chomens_auth")
|
||||||
|
.resolve("public.key");
|
||||||
|
|
||||||
|
private static final String BEGIN_PUBLIC_KEY = "-----BEGIN CHOMENS BOT PUBLIC KEY-----";
|
||||||
|
private static final String END_PUBLIC_KEY = "-----END CHOMENS BOT PUBLIC KEY-----";
|
||||||
|
|
||||||
|
public static final String ID = "chomens_bot_verify";
|
||||||
|
|
||||||
|
static {
|
||||||
|
try {
|
||||||
|
if (Files.exists(PUBLIC_KEY_PATH)) {
|
||||||
|
final String publicKeyString = new String(Files.readAllBytes(PUBLIC_KEY_PATH))
|
||||||
|
.replace(BEGIN_PUBLIC_KEY + "\n", "")
|
||||||
|
.replace("\n" + END_PUBLIC_KEY, "")
|
||||||
|
.replace("\n", "")
|
||||||
|
.trim();
|
||||||
|
|
||||||
|
final byte[] publicKeyBytes = Base64.getDecoder().decode(publicKeyString);
|
||||||
|
final KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
||||||
|
|
||||||
|
PUBLIC_KEY = keyFactory.generatePublic(new X509EncodedKeySpec(publicKeyBytes));
|
||||||
|
}
|
||||||
|
} catch (IOException | InvalidKeySpecException | NoSuchAlgorithmException | IllegalArgumentException ignored) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
public ChomeNSAuth () {
|
||||||
|
ListenerManager.addListener(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void init () {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void chatMessageReceived (Text message) {
|
||||||
|
if (PUBLIC_KEY == null) return;
|
||||||
|
|
||||||
|
if (message.getContent() == null || !(message.getContent() instanceof PlainTextContent idTextContent)) return;
|
||||||
|
|
||||||
|
final String id = idTextContent.string();
|
||||||
|
|
||||||
|
if (!id.equals(ID)) return;
|
||||||
|
|
||||||
|
final List<Text> children = message.getSiblings();
|
||||||
|
|
||||||
|
if (children.size() != 1) return;
|
||||||
|
|
||||||
|
if (!(children.getFirst().getContent() instanceof PlainTextContent selectorTextContent)) return;
|
||||||
|
|
||||||
|
final String selector = selectorTextContent.string();
|
||||||
|
|
||||||
|
try {
|
||||||
|
final String encrypted = encrypt(ID.getBytes(StandardCharsets.UTF_8));
|
||||||
|
|
||||||
|
final Component component = Component
|
||||||
|
.text(ID)
|
||||||
|
.append(Component.text(encrypted));
|
||||||
|
|
||||||
|
CommandCore.INSTANCE.run(
|
||||||
|
String.format(
|
||||||
|
"tellraw %s %s",
|
||||||
|
selector,
|
||||||
|
GsonComponentSerializer.gson()
|
||||||
|
.serialize(component)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
} catch (Exception ignored) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
public String encrypt (byte[] data) throws Exception {
|
||||||
|
final Cipher cipher = Cipher.getInstance("RSA");
|
||||||
|
cipher.init(Cipher.ENCRYPT_MODE, PUBLIC_KEY);
|
||||||
|
|
||||||
|
final byte[] encryptedBytes = cipher.doFinal(data);
|
||||||
|
|
||||||
|
return Base64.getEncoder().encodeToString(encryptedBytes);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue