From 195e409efde09131f3f9bd0958f2d0328d208e31 Mon Sep 17 00:00:00 2001 From: ljacqu Date: Sun, 30 Oct 2016 09:24:31 +0100 Subject: [PATCH] Add configurable antibot interval (#826) lost while merging master - Add interval configuration - Small refactoring: make OnJoinVerifier not call antibot if a PlayerAuth is available --- .../xephi/authme/listener/OnJoinVerifier.java | 4 +-- .../xephi/authme/service/AntiBotService.java | 14 +++++----- .../authme/listener/OnJoinVerifierTest.java | 27 ++++++++++++++----- .../authme/service/AntiBotServiceTest.java | 21 +++------------ 4 files changed, 31 insertions(+), 35 deletions(-) diff --git a/src/main/java/fr/xephi/authme/listener/OnJoinVerifier.java b/src/main/java/fr/xephi/authme/listener/OnJoinVerifier.java index 755d2a6e2..7e68aae42 100644 --- a/src/main/java/fr/xephi/authme/listener/OnJoinVerifier.java +++ b/src/main/java/fr/xephi/authme/listener/OnJoinVerifier.java @@ -68,10 +68,10 @@ class OnJoinVerifier implements Reloadable { * @param isAuthAvailable whether or not the player is registered */ public void checkAntibot(Player player, boolean isAuthAvailable) throws FailedVerificationException { - if (permissionsManager.hasPermission(player, PlayerStatePermission.BYPASS_ANTIBOT)) { + if (isAuthAvailable || permissionsManager.hasPermission(player, PlayerStatePermission.BYPASS_ANTIBOT)) { return; } - if (antiBotService.shouldKick(isAuthAvailable)) { + if (antiBotService.shouldKick()) { antiBotService.addPlayerKick(player.getName()); throw new FailedVerificationException(MessageKey.KICK_ANTIBOT); } diff --git a/src/main/java/fr/xephi/authme/service/AntiBotService.java b/src/main/java/fr/xephi/authme/service/AntiBotService.java index a329e5de3..91f80041d 100644 --- a/src/main/java/fr/xephi/authme/service/AntiBotService.java +++ b/src/main/java/fr/xephi/authme/service/AntiBotService.java @@ -31,6 +31,7 @@ public class AntiBotService implements SettingsDependent { private int duration; private int sensibility; private int delay; + private int interval; // Service status private AntiBotStatus antiBotStatus; private boolean startup; @@ -60,6 +61,7 @@ public class AntiBotService implements SettingsDependent { duration = settings.getProperty(ProtectionSettings.ANTIBOT_DURATION); sensibility = settings.getProperty(ProtectionSettings.ANTIBOT_SENSIBILITY); delay = settings.getProperty(ProtectionSettings.ANTIBOT_DELAY); + interval = settings.getProperty(ProtectionSettings.ANTIBOT_INTERVAL); // Stop existing protection stopProtection(); @@ -145,23 +147,19 @@ public class AntiBotService implements SettingsDependent { /** * 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) { - if (antiBotStatus == AntiBotStatus.DISABLED || isAuthAvailable) { + public boolean shouldKick() { + if (antiBotStatus == AntiBotStatus.DISABLED) { return false; - } - - if (antiBotStatus == AntiBotStatus.ACTIVE) { + } else if (antiBotStatus == AntiBotStatus.ACTIVE) { return true; } if (lastFlaggedJoin == null) { lastFlaggedJoin = Instant.now(); } - if (ChronoUnit.SECONDS.between(lastFlaggedJoin, Instant.now()) <= 5) { + if (ChronoUnit.SECONDS.between(lastFlaggedJoin, Instant.now()) <= interval) { flagged++; } else { // reset to 1 because this player is also count as not registered diff --git a/src/test/java/fr/xephi/authme/listener/OnJoinVerifierTest.java b/src/test/java/fr/xephi/authme/listener/OnJoinVerifierTest.java index 6271f0922..3b764bd75 100644 --- a/src/test/java/fr/xephi/authme/listener/OnJoinVerifierTest.java +++ b/src/test/java/fr/xephi/authme/listener/OnJoinVerifierTest.java @@ -8,12 +8,12 @@ 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.service.ValidationService; import org.bukkit.Server; import org.bukkit.entity.Player; import org.bukkit.event.player.PlayerLoginEvent; @@ -381,14 +381,27 @@ public class OnJoinVerifierTest { Player player = newPlayerWithName("Bobby"); boolean isAuthAvailable = false; given(permissionsManager.hasPermission(player, PlayerStatePermission.BYPASS_ANTIBOT)).willReturn(false); - given(antiBotService.shouldKick(isAuthAvailable)).willReturn(false); + given(antiBotService.shouldKick()).willReturn(false); // when onJoinVerifier.checkAntibot(player, isAuthAvailable); // then verify(permissionsManager).hasPermission(player, PlayerStatePermission.BYPASS_ANTIBOT); - verify(antiBotService).shouldKick(isAuthAvailable); + verify(antiBotService).shouldKick(); + } + + @Test + public void shouldAllowUserWithAuth() throws FailedVerificationException { + // given + Player player = newPlayerWithName("Lacey"); + boolean isAuthAvailable = true; + + // when + onJoinVerifier.checkAntibot(player, isAuthAvailable); + + // then + verifyZeroInteractions(permissionsManager, antiBotService); } @Test @@ -397,13 +410,13 @@ public class OnJoinVerifierTest { Player player = newPlayerWithName("Steward"); boolean isAuthAvailable = false; given(permissionsManager.hasPermission(player, PlayerStatePermission.BYPASS_ANTIBOT)).willReturn(true); - given(antiBotService.shouldKick(isAuthAvailable)).willReturn(true); // when onJoinVerifier.checkAntibot(player, isAuthAvailable); // then verify(permissionsManager).hasPermission(player, PlayerStatePermission.BYPASS_ANTIBOT); + verifyZeroInteractions(antiBotService); } @Test @@ -412,7 +425,7 @@ public class OnJoinVerifierTest { Player player = newPlayerWithName("D3"); boolean isAuthAvailable = false; given(permissionsManager.hasPermission(player, PlayerStatePermission.BYPASS_ANTIBOT)).willReturn(false); - given(antiBotService.shouldKick(isAuthAvailable)).willReturn(true); + given(antiBotService.shouldKick()).willReturn(true); // when / then try { @@ -420,7 +433,7 @@ public class OnJoinVerifierTest { fail("Expected exception to be thrown"); } catch (FailedVerificationException e) { verify(permissionsManager).hasPermission(player, PlayerStatePermission.BYPASS_ANTIBOT); - verify(antiBotService).shouldKick(isAuthAvailable); + verify(antiBotService).shouldKick(); } } diff --git a/src/test/java/fr/xephi/authme/service/AntiBotServiceTest.java b/src/test/java/fr/xephi/authme/service/AntiBotServiceTest.java index 7dfa0b0d3..db7f3e7d0 100644 --- a/src/test/java/fr/xephi/authme/service/AntiBotServiceTest.java +++ b/src/test/java/fr/xephi/authme/service/AntiBotServiceTest.java @@ -131,27 +131,12 @@ public class AntiBotServiceTest { runSyncDelayedTaskWithDelay(bukkitService); // when - boolean result = antiBotService.shouldKick(false); + boolean result = antiBotService.shouldKick(); // then assertThat(result, equalTo(false)); } - @Test - public void shouldRejectPlayerWithoutAuth() { - // given - active antibot - runSyncDelayedTaskWithDelay(bukkitService); - antiBotService.overrideAntiBotStatus(true); - - // when - boolean kickWithoutAuth = antiBotService.shouldKick(false); - boolean kickWithAuth = antiBotService.shouldKick(true); - - // then - assertThat(kickWithoutAuth, equalTo(true)); - assertThat(kickWithAuth, equalTo(false)); - } - @Test public void shouldActivateAntibotAfterThreshold() { // given @@ -162,12 +147,12 @@ public class AntiBotServiceTest { runSyncDelayedTaskWithDelay(bukkitService); for (int i = 0; i < sensitivity; ++i) { - antiBotService.shouldKick(false); + antiBotService.shouldKick(); } assertThat(antiBotService.getAntiBotStatus(), equalTo(AntiBotService.AntiBotStatus.LISTENING)); // when - antiBotService.shouldKick(false); + antiBotService.shouldKick(); // then assertThat(antiBotService.getAntiBotStatus(), equalTo(AntiBotService.AntiBotStatus.ACTIVE));