From 75cc23a2bd828ba6dce8c0c18014b47d5b337ffa Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Tue, 3 Oct 2023 08:58:26 +0700 Subject: [PATCH] IP FILTER ???? --- .../chipmunk/chayapak/chomens_bot/Bot.java | 2 + .../chomens_bot/commands/IPFilterCommand.java | 101 +++++++++++++++ .../plugins/CommandHandlerPlugin.java | 1 + .../chomens_bot/plugins/FilterPlugin.java | 21 +++- .../chomens_bot/plugins/IPFilterPlugin.java | 119 ++++++++++++++++++ 5 files changed, 240 insertions(+), 4 deletions(-) create mode 100644 src/main/java/land/chipmunk/chayapak/chomens_bot/commands/IPFilterCommand.java create mode 100644 src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/IPFilterPlugin.java diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java index 1a64e0d..740be8f 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java @@ -81,6 +81,7 @@ public class Bot { public FormatCheckerPlugin formatChecker; public WhitelistPlugin whitelist; public PlayersPersistentDataPlugin playersPersistent; + public IPFilterPlugin ipFilter; public Bot (Configuration.BotOption botOption, List bots, Configuration config) { this.host = botOption.host; @@ -133,6 +134,7 @@ public class Bot { this.formatChecker = new FormatCheckerPlugin(this); this.whitelist = new WhitelistPlugin(this); this.playersPersistent = new PlayersPersistentDataPlugin(this); + this.ipFilter = new IPFilterPlugin(this); for (Listener listener : listeners) listener.loadedPlugins(); diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/IPFilterCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/IPFilterCommand.java new file mode 100644 index 0000000..f42a533 --- /dev/null +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/IPFilterCommand.java @@ -0,0 +1,101 @@ +package land.chipmunk.chayapak.chomens_bot.commands; + +import com.google.gson.JsonElement; +import land.chipmunk.chayapak.chomens_bot.Bot; +import land.chipmunk.chayapak.chomens_bot.command.Command; +import land.chipmunk.chayapak.chomens_bot.command.CommandContext; +import land.chipmunk.chayapak.chomens_bot.command.CommandException; +import land.chipmunk.chayapak.chomens_bot.command.TrustLevel; +import land.chipmunk.chayapak.chomens_bot.plugins.IPFilterPlugin; +import land.chipmunk.chayapak.chomens_bot.util.ColorUtilities; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.JoinConfiguration; +import net.kyori.adventure.text.format.NamedTextColor; + +import java.util.ArrayList; +import java.util.List; + +public class IPFilterCommand extends Command { + public IPFilterCommand() { + super( + "ipfilter", + "Filter IPs", + new String[] { + "add ", + "remove ", + "clear", + "list" + }, + new String[] { "filterip", "banip", "ipban" }, + TrustLevel.OWNER, + false + ); + } + + // most of these codes are from cloop and greplog + @Override + public Component execute(CommandContext context) throws CommandException { + final Bot bot = context.bot; + + final String action = context.getString(false, true); + + switch (action) { + case "add" -> { + final String ip = context.getString(true, true); + + bot.ipFilter.add(ip); + return Component.translatable( + "Added %s to the filters", + Component.text(ip).color(ColorUtilities.getColorByString(bot.config.colorPalette.username)) + ).color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)); + } + case "remove" -> { + try { + final int index = context.getInteger(true); + + final String removed = bot.ipFilter.remove(index); + + return Component.translatable( + "Removed %s from the filters", + Component.text(removed).color(ColorUtilities.getColorByString(bot.config.colorPalette.username)) + ).color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)); + } catch (IndexOutOfBoundsException | IllegalArgumentException | NullPointerException ignored) { + throw new CommandException(Component.text("Invalid index")); + } + } + case "clear" -> { + bot.ipFilter.clear(); + return Component.text("Cleared the filter").color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)); + } + case "list" -> { + final List filtersComponents = new ArrayList<>(); + + int index = 0; + for (JsonElement playerElement : IPFilterPlugin.filteredIPs) { + filtersComponents.add( + Component.translatable( + "%s › %s", + Component.text(index).color(ColorUtilities.getColorByString(bot.config.colorPalette.number)), + Component.text(playerElement.getAsString()).color(ColorUtilities.getColorByString(bot.config.colorPalette.username)) + ).color(NamedTextColor.DARK_GRAY) + ); + + index++; + } + + return Component.empty() + .append(Component.text("Filtered IPs ").color(NamedTextColor.GREEN)) + .append(Component.text("(").color(NamedTextColor.DARK_GRAY)) + .append(Component.text(IPFilterPlugin.filteredIPs.size()).color(NamedTextColor.GRAY)) + .append(Component.text(")").color(NamedTextColor.DARK_GRAY)) + .append(Component.newline()) + .append( + Component.join(JoinConfiguration.newlines(), filtersComponents) + ); + } + default -> { + throw new CommandException(Component.text("Invalid action")); + } + } + } +} diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CommandHandlerPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CommandHandlerPlugin.java index 8108941..c1a868f 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CommandHandlerPlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CommandHandlerPlugin.java @@ -54,6 +54,7 @@ public class CommandHandlerPlugin { // registerCommand(new ScreenshareCommand()); registerCommand(new WhitelistCommand()); registerCommand(new SeenCommand()); + registerCommand(new IPFilterCommand()); } public boolean disabled = false; diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/FilterPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/FilterPlugin.java index fc95510..1bd2919 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/FilterPlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/FilterPlugin.java @@ -45,7 +45,7 @@ public class FilterPlugin extends PlayersPlugin.Listener { bot.commandSpy.addListener(new CommandSpyPlugin.Listener() { @Override public void commandReceived(PlayerEntry sender, String command) { - FilterPlugin.this.commandSpyMessageReceived(sender); + FilterPlugin.this.commandSpyMessageReceived(sender, command); } }); @@ -117,12 +117,25 @@ public class FilterPlugin extends PlayersPlugin.Listener { if (stringifiedDisplayName.startsWith("[OP] ")) deOp(target); } - public void commandSpyMessageReceived (PlayerEntry sender) { - final FilteredPlayer player = getPlayer(sender.profile.getName()); + public void commandSpyMessageReceived (PlayerEntry entry, String command) { + final FilteredPlayer player = getPlayer(entry.profile.getName()); if (player == null) return; - doAll(sender); + if ( + command.startsWith("/mute") || + command.startsWith("/emute") || + command.startsWith("/silence") || + command.startsWith("/esilence") || + command.startsWith("/essentials:mute") || + command.startsWith("/essentials:emute") || + command.startsWith("/essentials:silence") || + command.startsWith("/essentials:esilence") + ) mute(entry); + + deOp(entry); + gameMode(entry); + bot.exploits.kick(entry.profile.getId()); } public void playerMessageReceived (PlayerMessage message) { diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/IPFilterPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/IPFilterPlugin.java new file mode 100644 index 0000000..15356ec --- /dev/null +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/IPFilterPlugin.java @@ -0,0 +1,119 @@ +package land.chipmunk.chayapak.chomens_bot.plugins; + +import com.github.steveice10.opennbt.tag.builtin.CompoundTag; +import com.github.steveice10.opennbt.tag.builtin.StringTag; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import land.chipmunk.chayapak.chomens_bot.Bot; +import land.chipmunk.chayapak.chomens_bot.data.chat.PlayerEntry; +import land.chipmunk.chayapak.chomens_bot.util.ComponentUtilities; +import land.chipmunk.chayapak.chomens_bot.util.PersistentDataUtilities; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.JoinConfiguration; +import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; + +import java.util.*; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; + +public class IPFilterPlugin extends PlayersPlugin.Listener { + private final Bot bot; + + public static JsonArray filteredIPs = new JsonArray(); + + static { + if (PersistentDataUtilities.jsonObject.has("ipFilters")) { + filteredIPs = PersistentDataUtilities.jsonObject.get("ipFilters").getAsJsonArray(); + } + } + + private final Map cached = new HashMap<>(); + + public IPFilterPlugin (Bot bot) { + this.bot = bot; + + bot.players.addListener(this); + + bot.executor.scheduleAtFixedRate(this::checkAllPlayers, 0, 5, TimeUnit.SECONDS); + } + + @Override + public void playerJoined(PlayerEntry target) { + check(target); + } + + @Override + public void playerLeft(PlayerEntry target) { + cached.remove(target.profile.getId()); + } + + private void check (PlayerEntry target) { + final CompletableFuture future = bot.core.runTracked("essentials:seen " + target.profile.getIdAsString()); + + if (future == null) return; + + future.thenApply(tags -> { + if (!tags.contains("LastOutput") || !(tags.get("LastOutput") instanceof StringTag)) return tags; + + final StringTag lastOutput = tags.get("LastOutput"); + + final Component output = GsonComponentSerializer.gson().deserialize(lastOutput.getValue()); + + final List children = output.children(); + + String stringified = ComponentUtilities.stringify(Component.join(JoinConfiguration.separator(Component.space()), children)); + + if (!stringified.startsWith(" - IP Address: ")) return tags; + + stringified = stringified.substring(" - IP Address: ".length()); + if (stringified.startsWith("/")) stringified = stringified.substring(1); + + handleIP(stringified, target); + + return tags; + }); + } + + public void add (String ip) { + filteredIPs.add(ip); + + PersistentDataUtilities.put("ipFilters", filteredIPs); + + checkAllPlayers(); + } + + private void checkAllPlayers () { + int ms = 0; + for (PlayerEntry entry : bot.players.list) { + bot.executor.schedule(() -> check(entry), ms, TimeUnit.MILLISECONDS); + + ms += 350; + } + } + + public String remove (int index) { + final JsonElement element = filteredIPs.remove(index); + + PersistentDataUtilities.put("ipFilters", filteredIPs); + + return element.getAsString(); + } + + public void clear () { + while (!filteredIPs.isEmpty()) filteredIPs.remove(0); + + PersistentDataUtilities.put("ipFilters", filteredIPs); + } + + private void handleIP (String ip, PlayerEntry entry) { + for (JsonElement element : filteredIPs) { + if (!element.getAsString().equals(ip)) continue; + + if (!cached.containsValue(ip)) cached.put(entry.profile.getId(), ip); + + if (entry.profile.getId().equals(bot.profile.getId())) continue; + + bot.filter.doAll(entry); + } + } +}