More reliable IP address limit.

This commit is contained in:
ryanhamshire 2016-08-28 13:10:04 -07:00
parent 7f95d70d4d
commit a76b834495
4 changed files with 21 additions and 31 deletions

View File

@ -1614,6 +1614,7 @@ public abstract class DataStore
this.addDefault(defaults, Messages.IsIgnoringYou, "That player is ignoring you.", null); 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.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.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 //load the config file
FileConfiguration config = YamlConfiguration.loadConfiguration(new File(messagesFilePath)); FileConfiguration config = YamlConfiguration.loadConfiguration(new File(messagesFilePath));

View File

@ -245,5 +245,6 @@ public enum Messages
TooManyActiveBlocksInClaim, TooManyActiveBlocksInClaim,
ConsoleOnlyCommand, ConsoleOnlyCommand,
WorldNotFound, WorldNotFound,
AdjustBlocksAllSuccess AdjustBlocksAllSuccess,
TooMuchIpOverlap
} }

View File

@ -142,9 +142,6 @@ public class PlayerData
//profanity warning, once per play session //profanity warning, once per play session
boolean profanityWarned = false; 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 //whether or not this player is "in" pvp combat
public boolean inPvpCombat() public boolean inPvpCombat()
{ {

View File

@ -563,9 +563,6 @@ class PlayerEventHandler implements Listener
private ConcurrentHashMap<UUID, Date> lastLoginThisServerSessionMap = new ConcurrentHashMap<UUID, Date>(); private ConcurrentHashMap<UUID, Date> lastLoginThisServerSessionMap = new ConcurrentHashMap<UUID, Date>();
//counts how many players are using each IP address connected to the server right now
private ConcurrentHashMap<String, Integer> ipCountHash = new ConcurrentHashMap<String, Integer>();
//when a player attempts to join the server... //when a player attempts to join the server...
@EventHandler(priority = EventPriority.HIGHEST) @EventHandler(priority = EventPriority.HIGHEST)
void onPlayerLogin (PlayerLoginEvent event) void onPlayerLogin (PlayerLoginEvent event)
@ -727,27 +724,34 @@ class PlayerEventHandler implements Listener
InetAddress ipAddress = playerData.ipAddress; InetAddress ipAddress = playerData.ipAddress;
if(ipAddress != null) if(ipAddress != null)
{ {
String ipAddressString = ipAddress.toString();
int ipLimit = GriefPrevention.instance.config_ipLimit; int ipLimit = GriefPrevention.instance.config_ipLimit;
if(ipLimit > 0 && GriefPrevention.isNewToServer(player)) if(ipLimit > 0 && GriefPrevention.isNewToServer(player))
{ {
Integer ipCount = this.ipCountHash.get(ipAddressString); int ipCount = 0;
if(ipCount == null) ipCount = 0;
@SuppressWarnings("unchecked")
Collection<Player> players = (Collection<Player>)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) if(ipCount >= ipLimit)
{ {
//kick player //kick player
PlayerKickBanTask task = new PlayerKickBanTask(player, "Sorry, there are too many players logged in with your IP address.", "GriefPrevention IP-sharing limit.", false); PlayerKickBanTask task = new PlayerKickBanTask(player, GriefPrevention.instance.dataStore.getMessage(Messages.TooMuchIpOverlap), "GriefPrevention IP-sharing limit.", false);
GriefPrevention.instance.getServer().getScheduler().scheduleSyncDelayedTask(GriefPrevention.instance, task, 10L); GriefPrevention.instance.getServer().getScheduler().scheduleSyncDelayedTask(GriefPrevention.instance, task, 100L);
//silence join message //silence join message
event.setJoinMessage(null); event.setJoinMessage(null);
return; 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 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 //drop data about this player
this.dataStore.clearCachedPlayerData(playerID); this.dataStore.clearCachedPlayerData(playerID);