IP FILTER ????
This commit is contained in:
parent
fe81e3ae35
commit
75cc23a2bd
5 changed files with 240 additions and 4 deletions
|
@ -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<Bot> 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();
|
||||
|
||||
|
|
|
@ -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 <ip>",
|
||||
"remove <index>",
|
||||
"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<Component> 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"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -54,6 +54,7 @@ public class CommandHandlerPlugin {
|
|||
// registerCommand(new ScreenshareCommand());
|
||||
registerCommand(new WhitelistCommand());
|
||||
registerCommand(new SeenCommand());
|
||||
registerCommand(new IPFilterCommand());
|
||||
}
|
||||
|
||||
public boolean disabled = false;
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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<UUID, String> 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<CompoundTag> 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<Component> 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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue