From d72d6ddf5ae8be7f8222d19a964b8c81e9aa094f Mon Sep 17 00:00:00 2001 From: ljacqu Date: Sun, 26 Jun 2016 09:25:52 +0200 Subject: [PATCH] AntiBot - make public field private --- src/main/java/fr/xephi/authme/AntiBot.java | 23 +++++++++- .../authme/listener/AuthMePlayerListener.java | 4 +- .../xephi/authme/listener/OnJoinVerifier.java | 2 +- .../authme/listener/OnJoinVerifierTest.java | 46 +++++++++++++++++++ 4 files changed, 71 insertions(+), 4 deletions(-) diff --git a/src/main/java/fr/xephi/authme/AntiBot.java b/src/main/java/fr/xephi/authme/AntiBot.java index d85cc7843..37f048724 100644 --- a/src/main/java/fr/xephi/authme/AntiBot.java +++ b/src/main/java/fr/xephi/authme/AntiBot.java @@ -24,7 +24,7 @@ public class AntiBot { private final Messages messages; private final PermissionsManager permissionsManager; private final BukkitService bukkitService; - public final CopyOnWriteArrayList antibotKicked = new CopyOnWriteArrayList(); + private final CopyOnWriteArrayList antibotKicked = new CopyOnWriteArrayList(); private final CopyOnWriteArrayList antibotPlayers = new CopyOnWriteArrayList(); private AntiBotStatus antiBotStatus = AntiBotStatus.DISABLED; @@ -112,6 +112,27 @@ public class AntiBot { }, 15 * TICKS_PER_SECOND); } + /** + * Returns whether the player was kicked because of activated antibot. The list is reset + * when antibot is deactivated. + * + * @param name the name to check + * @return true if the given name has been kicked because of Antibot + */ + public boolean wasPlayerKicked(String name) { + return antibotKicked.contains(name.toLowerCase()); + } + + /** + * Adds a name to the list of players kicked by antibot. Should only be used when a player + * is determined to be kicked because of failed antibot verification. + * + * @param name the name to add + */ + public void addPlayerKick(String name) { + antibotKicked.addIfAbsent(name.toLowerCase()); + } + public enum AntiBotStatus { LISTENING, DISABLED, diff --git a/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java b/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java index 59e01376a..fba9e6fc7 100644 --- a/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java +++ b/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java @@ -239,7 +239,7 @@ public class AuthMePlayerListener implements Listener { event.setQuitMessage(null); } - if (antiBot.antibotKicked.contains(player.getName())) { + if (antiBot.wasPlayerKicked(player.getName())) { return; } @@ -250,7 +250,7 @@ public class AuthMePlayerListener implements Listener { public void onPlayerKick(PlayerKickEvent event) { Player player = event.getPlayer(); - if (!antiBot.antibotKicked.contains(player.getName())) { + if (!antiBot.wasPlayerKicked(player.getName())) { management.performQuit(player, true); } } diff --git a/src/main/java/fr/xephi/authme/listener/OnJoinVerifier.java b/src/main/java/fr/xephi/authme/listener/OnJoinVerifier.java index 38dd7d9c4..73bf93e38 100644 --- a/src/main/java/fr/xephi/authme/listener/OnJoinVerifier.java +++ b/src/main/java/fr/xephi/authme/listener/OnJoinVerifier.java @@ -68,7 +68,7 @@ class OnJoinVerifier implements Reloadable { */ public void checkAntibot(String playerName, boolean isAuthAvailable) throws FailedVerificationException { if (antiBot.getAntiBotStatus() == AntiBot.AntiBotStatus.ACTIVE && !isAuthAvailable) { - antiBot.antibotKicked.addIfAbsent(playerName); + antiBot.addPlayerKick(playerName); throw new FailedVerificationException(MessageKey.KICK_ANTIBOT); } } diff --git a/src/test/java/fr/xephi/authme/listener/OnJoinVerifierTest.java b/src/test/java/fr/xephi/authme/listener/OnJoinVerifierTest.java index 6d5d27e44..436f62f58 100644 --- a/src/test/java/fr/xephi/authme/listener/OnJoinVerifierTest.java +++ b/src/test/java/fr/xephi/authme/listener/OnJoinVerifierTest.java @@ -36,6 +36,7 @@ import java.util.List; import static org.hamcrest.Matchers.equalTo; import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; @@ -377,6 +378,51 @@ public class OnJoinVerifierTest { verifyZeroInteractions(bukkitService); } + @Test + public void shouldCheckAntiBot() throws FailedVerificationException { + // given + String name = "user123"; + boolean hasAuth = false; + given(antiBot.getAntiBotStatus()).willReturn(AntiBot.AntiBotStatus.LISTENING); + + // when + onJoinVerifier.checkAntibot(name, hasAuth); + + // then + verify(antiBot).getAntiBotStatus(); + } + + @Test + public void shouldAllowUserWithAuth() throws FailedVerificationException { + // given + String name = "Bobby"; + boolean hasAuth = true; + given(antiBot.getAntiBotStatus()).willReturn(AntiBot.AntiBotStatus.ACTIVE); + + // when + onJoinVerifier.checkAntibot(name, hasAuth); + + // then + verify(antiBot).getAntiBotStatus(); + } + + @Test + public void shouldThrowForActiveAntiBot() { + // given + String name = "Bobby"; + boolean hasAuth = false; + given(antiBot.getAntiBotStatus()).willReturn(AntiBot.AntiBotStatus.ACTIVE); + + // when / then + try { + onJoinVerifier.checkAntibot(name, hasAuth); + fail("Expected exception to be thrown"); + } catch (FailedVerificationException e) { + assertThat(e, exceptionWithData(MessageKey.KICK_ANTIBOT)); + verify(antiBot).addPlayerKick(name); + } + } + private static Player newPlayerWithName(String name) { Player player = mock(Player.class); given(player.getName()).willReturn(name);