From f34df4f2963567624cf5a78b73b460bfb221337a Mon Sep 17 00:00:00 2001 From: Taah Date: Tue, 16 Jan 2024 22:23:31 -0800 Subject: [PATCH] Add IP Banning by getting every IP punishment through SQL and checking if it's the same IP, if it's a ban / tempban, and if it's active Make the ban command add the most recent IP used by a PlexPlayer if the player being banned is not online, else use online player's IP --- .../java/dev/plex/command/impl/BanCMD.java | 5 +--- .../dev/plex/listener/impl/BanListener.java | 12 ++++---- .../plex/punishment/PunishmentManager.java | 23 ++++++--------- .../storage/punishment/SQLPunishment.java | 28 +++++++++++++++++++ 4 files changed, 45 insertions(+), 23 deletions(-) diff --git a/server/src/main/java/dev/plex/command/impl/BanCMD.java b/server/src/main/java/dev/plex/command/impl/BanCMD.java index dd61fad..ceb38a7 100644 --- a/server/src/main/java/dev/plex/command/impl/BanCMD.java +++ b/server/src/main/java/dev/plex/command/impl/BanCMD.java @@ -80,10 +80,7 @@ public class BanCMD extends PlexCommand punishment.setEndDate(date.plusDays(1)); punishment.setCustomTime(false); punishment.setActive(true); - if (player != null) - { - punishment.setIp(player.getAddress().getAddress().getHostAddress().trim()); - } + punishment.setIp(player != null ? player.getAddress().getAddress().getHostAddress().trim() : plexPlayer.getIps().get(plexPlayer.getIps().size() - 1)); plugin.getPunishmentManager().punish(plexPlayer, punishment); PlexUtils.broadcast(messageComponent("banningPlayer", sender.getName(), plexPlayer.getName())); Bukkit.getScheduler().runTask(Plex.get(), () -> diff --git a/server/src/main/java/dev/plex/listener/impl/BanListener.java b/server/src/main/java/dev/plex/listener/impl/BanListener.java index cd93fc1..91cc4b2 100644 --- a/server/src/main/java/dev/plex/listener/impl/BanListener.java +++ b/server/src/main/java/dev/plex/listener/impl/BanListener.java @@ -7,6 +7,8 @@ import dev.plex.player.PlexPlayer; import dev.plex.punishment.Punishment; import dev.plex.punishment.PunishmentManager; import dev.plex.punishment.PunishmentType; +import dev.plex.util.PlexLog; +import it.unimi.dsi.fastutil.Pair; import org.bukkit.Bukkit; import org.bukkit.event.EventHandler; import org.bukkit.event.player.AsyncPlayerPreLoginEvent; @@ -51,18 +53,18 @@ public class BanListener extends PlexListener player.getPunishments().stream().filter(punishment -> (punishment.getType() == PunishmentType.BAN || punishment.getType() == PunishmentType.TEMPBAN) && punishment.isActive()).findFirst().ifPresent(punishment -> event.disallow(AsyncPlayerPreLoginEvent.Result.KICK_BANNED, Punishment.generateBanMessage(punishment))); + return; } - else if (plugin.getPunishmentManager().isBanned(event.getAddress().getHostAddress())) + Punishment ipBannedPunishment = plugin.getPunishmentManager().getBanByIP(event.getAddress().getHostAddress()); + if (ipBannedPunishment != null) { // Don't check if the other account that's banned has bypass abilities, check if current has only if (Plex.get().getPermissions() != null && Plex.get().getPermissions().playerHas(null, Bukkit.getOfflinePlayer(event.getUniqueId()), "plex.ban.bypass")) { return; } - PlexPlayer player = DataUtils.getPlayerByIP(event.getAddress().getHostAddress()); - player.getPunishments().stream().filter(punishment -> (punishment.getType() == PunishmentType.BAN || punishment.getType() == PunishmentType.TEMPBAN) && punishment.isActive()).findFirst().ifPresent(punishment -> - event.disallow(AsyncPlayerPreLoginEvent.Result.KICK_BANNED, - Punishment.generateBanMessage(punishment))); + event.disallow(AsyncPlayerPreLoginEvent.Result.KICK_BANNED, + Punishment.generateBanMessage(ipBannedPunishment)); } } } \ No newline at end of file diff --git a/server/src/main/java/dev/plex/punishment/PunishmentManager.java b/server/src/main/java/dev/plex/punishment/PunishmentManager.java index 32e194a..40ab6b5 100644 --- a/server/src/main/java/dev/plex/punishment/PunishmentManager.java +++ b/server/src/main/java/dev/plex/punishment/PunishmentManager.java @@ -3,6 +3,7 @@ package dev.plex.punishment; import com.google.common.collect.Lists; import com.google.common.reflect.TypeToken; import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import dev.plex.Plex; import dev.plex.PlexBase; import dev.plex.cache.DataUtils; @@ -20,6 +21,11 @@ import java.util.Collection; import java.util.List; import java.util.UUID; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.atomic.AtomicReference; +import java.util.function.Consumer; + +import dev.plex.util.adapter.ZonedDateTimeAdapter; +import it.unimi.dsi.fastutil.Pair; import lombok.Data; import lombok.Getter; import org.apache.commons.io.FileUtils; @@ -139,21 +145,10 @@ public class PunishmentManager implements PlexBase return DataUtils.getPlayer(uuid).getPunishments().stream().anyMatch(punishment -> (punishment.getType() == PunishmentType.BAN || punishment.getType() == PunishmentType.TEMPBAN) && punishment.isActive()); } - /*public void isIPBanned(PlexPlayer player) + public Punishment getBanByIP(String ip) { - return getActiveBans().whenComplete(((punishments, throwable) -> - punishments.forEach(punishment -> - player.getIps().stream().forEach()) - }*/ - - public boolean isBanned(String ip) - { - final PlexPlayer player = DataUtils.getPlayerByIP(ip); - if (player == null) - { - return false; - } - return player.getPunishments().stream().filter(punishment -> punishment.getType() == PunishmentType.BAN || punishment.getType() == PunishmentType.TEMPBAN).anyMatch(Punishment::isActive); + final Gson gson = new GsonBuilder().registerTypeAdapter(ZonedDateTime.class, new ZonedDateTimeAdapter()).setPrettyPrinting().create(); + return plugin.getSqlPunishment().getPunishments(ip).stream().filter(punishment -> punishment.getType() == PunishmentType.TEMPBAN || punishment.getType() == PunishmentType.BAN).filter(Punishment::isActive).filter(punishment -> punishment.getIp().equals(ip)).findFirst().orElse(null); } public boolean isBanned(PlexPlayer player) diff --git a/server/src/main/java/dev/plex/storage/punishment/SQLPunishment.java b/server/src/main/java/dev/plex/storage/punishment/SQLPunishment.java index db5e2a2..cd83920 100644 --- a/server/src/main/java/dev/plex/storage/punishment/SQLPunishment.java +++ b/server/src/main/java/dev/plex/storage/punishment/SQLPunishment.java @@ -21,6 +21,7 @@ import java.util.concurrent.CompletableFuture; public class SQLPunishment { private static final String SELECT = "SELECT * FROM `punishments` WHERE punished=?"; + private static final String SELECT_BY_IP = "SELECT * FROM `punishments` WHERE ip=?"; private static final String SELECT_BY = "SELECT * FROM `punishments` WHERE punisher=?"; private static final String INSERT = "INSERT INTO `punishments` (`punished`, `punisher`, `punishedUsername`, `ip`, `type`, `reason`, `customTime`, `active`, `endDate`) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)"; @@ -84,6 +85,33 @@ public class SQLPunishment } return punishments; } + public List getPunishments(String ip) + { + List punishments = Lists.newArrayList(); + try (Connection con = Plex.get().getSqlConnection().getCon()) + { + PreparedStatement statement = con.prepareStatement(SELECT_BY_IP); + statement.setString(1, ip); + ResultSet set = statement.executeQuery(); + while (set.next()) + { + Punishment punishment = new Punishment(UUID.fromString(set.getString("punished")), set.getString("punisher") == null ? null : UUID.fromString(set.getString("punisher"))); + punishment.setActive(set.getBoolean("active")); + punishment.setType(PunishmentType.valueOf(set.getString("type"))); + punishment.setCustomTime(set.getBoolean("customTime")); + punishment.setPunishedUsername(set.getString("punishedUsername")); + punishment.setEndDate(ZonedDateTime.ofInstant(Instant.ofEpochMilli(set.getLong("endDate")), ZoneId.of(TimeUtils.TIMEZONE))); + punishment.setReason(set.getString("reason")); + punishment.setIp(set.getString("ip")); + punishments.add(punishment); + } + } + catch (SQLException e) + { + e.printStackTrace(); + } + return punishments; + } public CompletableFuture insertPunishment(Punishment punishment) {