From b3fd6170fe5c4fb19fe183741bf4959848c32916 Mon Sep 17 00:00:00 2001 From: DNx5 Date: Tue, 18 Oct 2016 02:53:20 +0700 Subject: [PATCH] Use timestamp to determine if antibot should be activated - removed handleJoin method. --- .../xephi/authme/listener/OnJoinVerifier.java | 4 +- .../xephi/authme/listener/PlayerListener.java | 14 +-- .../xephi/authme/service/AntiBotService.java | 94 ++++++++----------- 3 files changed, 47 insertions(+), 65 deletions(-) diff --git a/src/main/java/fr/xephi/authme/listener/OnJoinVerifier.java b/src/main/java/fr/xephi/authme/listener/OnJoinVerifier.java index 93783346d..755d2a6e2 100644 --- a/src/main/java/fr/xephi/authme/listener/OnJoinVerifier.java +++ b/src/main/java/fr/xephi/authme/listener/OnJoinVerifier.java @@ -9,14 +9,14 @@ import fr.xephi.authme.message.Messages; import fr.xephi.authme.permission.PermissionsManager; import fr.xephi.authme.permission.PlayerStatePermission; import fr.xephi.authme.service.AntiBotService; +import fr.xephi.authme.service.BukkitService; +import fr.xephi.authme.service.ValidationService; import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.properties.ProtectionSettings; import fr.xephi.authme.settings.properties.RegistrationSettings; import fr.xephi.authme.settings.properties.RestrictionSettings; -import fr.xephi.authme.service.BukkitService; import fr.xephi.authme.util.StringUtils; import fr.xephi.authme.util.Utils; -import fr.xephi.authme.service.ValidationService; import org.bukkit.Server; import org.bukkit.entity.Player; import org.bukkit.event.player.PlayerLoginEvent; diff --git a/src/main/java/fr/xephi/authme/listener/PlayerListener.java b/src/main/java/fr/xephi/authme/listener/PlayerListener.java index b6484fb35..73a552f03 100644 --- a/src/main/java/fr/xephi/authme/listener/PlayerListener.java +++ b/src/main/java/fr/xephi/authme/listener/PlayerListener.java @@ -6,14 +6,14 @@ import fr.xephi.authme.message.MessageKey; import fr.xephi.authme.message.Messages; import fr.xephi.authme.process.Management; import fr.xephi.authme.service.AntiBotService; +import fr.xephi.authme.service.BukkitService; +import fr.xephi.authme.service.TeleportationService; +import fr.xephi.authme.service.ValidationService; import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.SpawnLoader; import fr.xephi.authme.settings.properties.HooksSettings; import fr.xephi.authme.settings.properties.RegistrationSettings; import fr.xephi.authme.settings.properties.RestrictionSettings; -import fr.xephi.authme.service.BukkitService; -import fr.xephi.authme.service.TeleportationService; -import fr.xephi.authme.service.ValidationService; import org.bukkit.Location; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -233,7 +233,6 @@ public class PlayerListener implements Listener { return; } - antiBotService.handlePlayerJoin(); teleportationService.teleportOnJoin(player); } @@ -306,12 +305,7 @@ public class PlayerListener implements Listener { * @note little hack cause InventoryOpenEvent cannot be cancelled for * real, cause no packet is send to server by client for the main inv */ - bukkitService.scheduleSyncDelayedTask(new Runnable() { - @Override - public void run() { - player.closeInventory(); - } - }, 1); + bukkitService.scheduleSyncDelayedTask(player::closeInventory, 1); } @EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST) diff --git a/src/main/java/fr/xephi/authme/service/AntiBotService.java b/src/main/java/fr/xephi/authme/service/AntiBotService.java index 24118d40c..9a0db2e8d 100644 --- a/src/main/java/fr/xephi/authme/service/AntiBotService.java +++ b/src/main/java/fr/xephi/authme/service/AntiBotService.java @@ -7,10 +7,11 @@ import fr.xephi.authme.permission.AdminPermission; import fr.xephi.authme.permission.PermissionsManager; import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.properties.ProtectionSettings; -import org.bukkit.entity.Player; import org.bukkit.scheduler.BukkitTask; import javax.inject.Inject; +import java.time.Instant; +import java.time.temporal.ChronoUnit; import java.util.concurrent.CopyOnWriteArrayList; import static fr.xephi.authme.service.BukkitService.TICKS_PER_MINUTE; @@ -25,18 +26,17 @@ public class AntiBotService implements SettingsDependent { private final Messages messages; private final PermissionsManager permissionsManager; private final BukkitService bukkitService; - + private final CopyOnWriteArrayList antibotKicked = new CopyOnWriteArrayList<>(); // Settings private int duration; private int sensibility; private int delay; - // Service status private AntiBotStatus antiBotStatus; private boolean startup; private BukkitTask disableTask; - private int antibotPlayers; - private final CopyOnWriteArrayList antibotKicked = new CopyOnWriteArrayList<>(); + private Instant lastFlaggedJoin; + private int flagged = 0; @Inject AntiBotService(Settings settings, Messages messages, PermissionsManager permissionsManager, @@ -47,7 +47,7 @@ public class AntiBotService implements SettingsDependent { this.bukkitService = bukkitService; // Initial status disableTask = null; - antibotPlayers = 0; + flagged = 0; antiBotStatus = AntiBotStatus.DISABLED; startup = true; // Load settings and start if required @@ -71,15 +71,10 @@ public class AntiBotService implements SettingsDependent { } // Bot activation task - Runnable enableTask = new Runnable() { - @Override - public void run() { - antiBotStatus = AntiBotStatus.LISTENING; - } - }; + Runnable enableTask = () -> antiBotStatus = AntiBotStatus.LISTENING; // Delay the schedule on first start - if(startup) { + if (startup) { bukkitService.scheduleSyncDelayedTask(enableTask, delay * TICKS_PER_SECOND); startup = false; } else { @@ -94,19 +89,12 @@ public class AntiBotService implements SettingsDependent { antiBotStatus = AntiBotStatus.ACTIVE; // Inform admins - for (Player player : bukkitService.getOnlinePlayers()) { - if (permissionsManager.hasPermission(player, AdminPermission.ANTIBOT_MESSAGES)) { - messages.send(player, MessageKey.ANTIBOT_AUTO_ENABLED_MESSAGE); - } - } + bukkitService.getOnlinePlayers().stream() + .filter(player -> permissionsManager.hasPermission(player, AdminPermission.ANTIBOT_MESSAGES)) + .forEach(player -> messages.send(player, MessageKey.ANTIBOT_AUTO_ENABLED_MESSAGE)); // Schedule auto-disable - disableTask = bukkitService.runTaskLater(new Runnable() { - @Override - public void run() { - stopProtection(); - } - }, duration * TICKS_PER_MINUTE); + disableTask = bukkitService.runTaskLater(this::stopProtection, duration * TICKS_PER_MINUTE); } private void stopProtection() { @@ -116,7 +104,7 @@ public class AntiBotService implements SettingsDependent { // Change status antiBotStatus = AntiBotStatus.LISTENING; - antibotPlayers = 0; + flagged = 0; antibotKicked.clear(); // Cancel auto-disable task @@ -124,11 +112,10 @@ public class AntiBotService implements SettingsDependent { disableTask = null; // Inform admins - for (Player player : bukkitService.getOnlinePlayers()) { - if (permissionsManager.hasPermission(player, AdminPermission.ANTIBOT_MESSAGES)) { - messages.send(player, MessageKey.ANTIBOT_AUTO_DISABLED_MESSAGE, Integer.toString(duration)); - } - } + String durationString = Integer.toString(duration); + bukkitService.getOnlinePlayers().stream() + .filter(player -> permissionsManager.hasPermission(player, AdminPermission.ANTIBOT_MESSAGES)) + .forEach(player -> messages.send(player, MessageKey.ANTIBOT_AUTO_DISABLED_MESSAGE, durationString)); } /** @@ -155,36 +142,37 @@ public class AntiBotService implements SettingsDependent { } } - /** - * Handles a player joining the server and checks if AntiBot needs to be activated. - */ - public void handlePlayerJoin() { - if (antiBotStatus != AntiBotStatus.LISTENING) { - return; - } - - antibotPlayers++; - if (antibotPlayers > sensibility) { - startProtection(); - return; - } - - bukkitService.scheduleSyncDelayedTask(new Runnable() { - @Override - public void run() { - antibotPlayers--; - } - }, 5 * TICKS_PER_SECOND); - } - /** * Returns if a player should be kicked due to antibot service. * * @param isAuthAvailable if the player is registered + * * @return if the player should be kicked */ public boolean shouldKick(boolean isAuthAvailable) { - return !isAuthAvailable && (antiBotStatus == AntiBotStatus.ACTIVE); + if (antiBotStatus == AntiBotStatus.DISABLED || isAuthAvailable) { + return false; + } + + if (antiBotStatus == AntiBotStatus.ACTIVE) { + return true; + } + + if (lastFlaggedJoin == null) { + lastFlaggedJoin = Instant.now(); + } + if (ChronoUnit.SECONDS.between(Instant.now(), lastFlaggedJoin) <= 5) { + flagged++; + } else { + // reset to 1 because this player is also count as not registered + flagged = 1; + lastFlaggedJoin = null; + } + if (flagged > sensibility) { + startProtection(); + return true; + } + return false; } /**