From a76b83449545e9a478c30b7d692ac656112b7b67 Mon Sep 17 00:00:00 2001 From: ryanhamshire Date: Sun, 28 Aug 2016 13:10:04 -0700 Subject: [PATCH] More reliable IP address limit. --- .../GriefPrevention/DataStore.java | 1 + .../GriefPrevention/Messages.java | 3 +- .../GriefPrevention/PlayerData.java | 3 -- .../GriefPrevention/PlayerEventHandler.java | 45 ++++++++----------- 4 files changed, 21 insertions(+), 31 deletions(-) diff --git a/src/me/ryanhamshire/GriefPrevention/DataStore.java b/src/me/ryanhamshire/GriefPrevention/DataStore.java index 951c902..d544744 100644 --- a/src/me/ryanhamshire/GriefPrevention/DataStore.java +++ b/src/me/ryanhamshire/GriefPrevention/DataStore.java @@ -1614,6 +1614,7 @@ public abstract class DataStore this.addDefault(defaults, Messages.IsIgnoringYou, "That player is ignoring you.", null); this.addDefault(defaults, Messages.ConsoleOnlyCommand, "That command may only be executed from the server console.", null); this.addDefault(defaults, Messages.WorldNotFound, "World not found.", null); + this.addDefault(defaults, Messages.TooMuchIpOverlap, "Sorry, there are too many players logged in with your IP address.", null); //load the config file FileConfiguration config = YamlConfiguration.loadConfiguration(new File(messagesFilePath)); diff --git a/src/me/ryanhamshire/GriefPrevention/Messages.java b/src/me/ryanhamshire/GriefPrevention/Messages.java index abcbeb6..32e2d92 100644 --- a/src/me/ryanhamshire/GriefPrevention/Messages.java +++ b/src/me/ryanhamshire/GriefPrevention/Messages.java @@ -245,5 +245,6 @@ public enum Messages TooManyActiveBlocksInClaim, ConsoleOnlyCommand, WorldNotFound, - AdjustBlocksAllSuccess + AdjustBlocksAllSuccess, + TooMuchIpOverlap } diff --git a/src/me/ryanhamshire/GriefPrevention/PlayerData.java b/src/me/ryanhamshire/GriefPrevention/PlayerData.java index d3a9ce0..67fa154 100644 --- a/src/me/ryanhamshire/GriefPrevention/PlayerData.java +++ b/src/me/ryanhamshire/GriefPrevention/PlayerData.java @@ -142,9 +142,6 @@ public class PlayerData //profanity warning, once per play session boolean profanityWarned = false; - //true when the player's IP address was counted against the re-use limit when he joined - boolean ipLimited = false; - //whether or not this player is "in" pvp combat public boolean inPvpCombat() { diff --git a/src/me/ryanhamshire/GriefPrevention/PlayerEventHandler.java b/src/me/ryanhamshire/GriefPrevention/PlayerEventHandler.java index 10e06f4..f21211e 100644 --- a/src/me/ryanhamshire/GriefPrevention/PlayerEventHandler.java +++ b/src/me/ryanhamshire/GriefPrevention/PlayerEventHandler.java @@ -563,9 +563,6 @@ class PlayerEventHandler implements Listener private ConcurrentHashMap lastLoginThisServerSessionMap = new ConcurrentHashMap(); - //counts how many players are using each IP address connected to the server right now - private ConcurrentHashMap ipCountHash = new ConcurrentHashMap(); - //when a player attempts to join the server... @EventHandler(priority = EventPriority.HIGHEST) void onPlayerLogin (PlayerLoginEvent event) @@ -588,7 +585,7 @@ class PlayerEventHandler implements Listener long millisecondsSinceLastLogin = now - lastLoginThisSession.getTime(); long secondsSinceLastLogin = millisecondsSinceLastLogin / 1000; long cooldownRemaining = GriefPrevention.instance.config_spam_loginCooldownSeconds - secondsSinceLastLogin; - + //if cooldown remaining if(cooldownRemaining > 0) { @@ -727,27 +724,34 @@ class PlayerEventHandler implements Listener InetAddress ipAddress = playerData.ipAddress; if(ipAddress != null) { - String ipAddressString = ipAddress.toString(); int ipLimit = GriefPrevention.instance.config_ipLimit; if(ipLimit > 0 && GriefPrevention.isNewToServer(player)) { - Integer ipCount = this.ipCountHash.get(ipAddressString); - if(ipCount == null) ipCount = 0; + int ipCount = 0; + + @SuppressWarnings("unchecked") + Collection players = (Collection)GriefPrevention.instance.getServer().getOnlinePlayers(); + for(Player onlinePlayer : players) + { + if(onlinePlayer.getUniqueId().equals(player.getUniqueId())) continue; + + PlayerData otherData = GriefPrevention.instance.dataStore.getPlayerData(onlinePlayer.getUniqueId()); + if(ipAddress.equals(otherData.ipAddress) && GriefPrevention.isNewToServer(onlinePlayer)) + { + ipCount++; + } + } + if(ipCount >= ipLimit) { //kick player - PlayerKickBanTask task = new PlayerKickBanTask(player, "Sorry, there are too many players logged in with your IP address.", "GriefPrevention IP-sharing limit.", false); - GriefPrevention.instance.getServer().getScheduler().scheduleSyncDelayedTask(GriefPrevention.instance, task, 10L); + PlayerKickBanTask task = new PlayerKickBanTask(player, GriefPrevention.instance.dataStore.getMessage(Messages.TooMuchIpOverlap), "GriefPrevention IP-sharing limit.", false); + GriefPrevention.instance.getServer().getScheduler().scheduleSyncDelayedTask(GriefPrevention.instance, task, 100L); //silence join message event.setJoinMessage(null); return; } - else - { - this.ipCountHash.put(ipAddressString, ipCount + 1); - playerData.ipLimited = true; - } } } @@ -912,19 +916,6 @@ class PlayerEventHandler implements Listener if(player.getHealth() > 0) player.setHealth(0); //might already be zero from above, this avoids a double death message } - //reduce count of players with that player's IP address - if(GriefPrevention.instance.config_ipLimit > 0 && playerData.ipLimited) - { - InetAddress ipAddress = playerData.ipAddress; - if(ipAddress != null) - { - String ipAddressString = ipAddress.toString(); - Integer count = this.ipCountHash.get(ipAddressString); - if(count == null) count = 1; - this.ipCountHash.put(ipAddressString, count - 1); - } - } - //drop data about this player this.dataStore.clearCachedPlayerData(playerID);