Add configurable antibot interval (#826) lost while merging master

- Add interval configuration
- Small refactoring: make OnJoinVerifier not call antibot if a PlayerAuth is available
This commit is contained in:
ljacqu 2016-10-30 09:24:31 +01:00
parent 7a43703d52
commit 195e409efd
4 changed files with 31 additions and 35 deletions

View File

@ -68,10 +68,10 @@ class OnJoinVerifier implements Reloadable {
* @param isAuthAvailable whether or not the player is registered * @param isAuthAvailable whether or not the player is registered
*/ */
public void checkAntibot(Player player, boolean isAuthAvailable) throws FailedVerificationException { 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; return;
} }
if (antiBotService.shouldKick(isAuthAvailable)) { if (antiBotService.shouldKick()) {
antiBotService.addPlayerKick(player.getName()); antiBotService.addPlayerKick(player.getName());
throw new FailedVerificationException(MessageKey.KICK_ANTIBOT); throw new FailedVerificationException(MessageKey.KICK_ANTIBOT);
} }

View File

@ -31,6 +31,7 @@ public class AntiBotService implements SettingsDependent {
private int duration; private int duration;
private int sensibility; private int sensibility;
private int delay; private int delay;
private int interval;
// Service status // Service status
private AntiBotStatus antiBotStatus; private AntiBotStatus antiBotStatus;
private boolean startup; private boolean startup;
@ -60,6 +61,7 @@ public class AntiBotService implements SettingsDependent {
duration = settings.getProperty(ProtectionSettings.ANTIBOT_DURATION); duration = settings.getProperty(ProtectionSettings.ANTIBOT_DURATION);
sensibility = settings.getProperty(ProtectionSettings.ANTIBOT_SENSIBILITY); sensibility = settings.getProperty(ProtectionSettings.ANTIBOT_SENSIBILITY);
delay = settings.getProperty(ProtectionSettings.ANTIBOT_DELAY); delay = settings.getProperty(ProtectionSettings.ANTIBOT_DELAY);
interval = settings.getProperty(ProtectionSettings.ANTIBOT_INTERVAL);
// Stop existing protection // Stop existing protection
stopProtection(); stopProtection();
@ -145,23 +147,19 @@ public class AntiBotService implements SettingsDependent {
/** /**
* Returns if a player should be kicked due to antibot service. * 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 * @return if the player should be kicked
*/ */
public boolean shouldKick(boolean isAuthAvailable) { public boolean shouldKick() {
if (antiBotStatus == AntiBotStatus.DISABLED || isAuthAvailable) { if (antiBotStatus == AntiBotStatus.DISABLED) {
return false; return false;
} } else if (antiBotStatus == AntiBotStatus.ACTIVE) {
if (antiBotStatus == AntiBotStatus.ACTIVE) {
return true; return true;
} }
if (lastFlaggedJoin == null) { if (lastFlaggedJoin == null) {
lastFlaggedJoin = Instant.now(); lastFlaggedJoin = Instant.now();
} }
if (ChronoUnit.SECONDS.between(lastFlaggedJoin, Instant.now()) <= 5) { if (ChronoUnit.SECONDS.between(lastFlaggedJoin, Instant.now()) <= interval) {
flagged++; flagged++;
} else { } else {
// reset to 1 because this player is also count as not registered // reset to 1 because this player is also count as not registered

View File

@ -8,12 +8,12 @@ import fr.xephi.authme.message.Messages;
import fr.xephi.authme.permission.PermissionsManager; import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.permission.PlayerStatePermission; import fr.xephi.authme.permission.PlayerStatePermission;
import fr.xephi.authme.service.AntiBotService; 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.Settings;
import fr.xephi.authme.settings.properties.ProtectionSettings; import fr.xephi.authme.settings.properties.ProtectionSettings;
import fr.xephi.authme.settings.properties.RegistrationSettings; import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.settings.properties.RestrictionSettings; 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.Server;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerLoginEvent; import org.bukkit.event.player.PlayerLoginEvent;
@ -381,14 +381,27 @@ public class OnJoinVerifierTest {
Player player = newPlayerWithName("Bobby"); Player player = newPlayerWithName("Bobby");
boolean isAuthAvailable = false; boolean isAuthAvailable = false;
given(permissionsManager.hasPermission(player, PlayerStatePermission.BYPASS_ANTIBOT)).willReturn(false); given(permissionsManager.hasPermission(player, PlayerStatePermission.BYPASS_ANTIBOT)).willReturn(false);
given(antiBotService.shouldKick(isAuthAvailable)).willReturn(false); given(antiBotService.shouldKick()).willReturn(false);
// when // when
onJoinVerifier.checkAntibot(player, isAuthAvailable); onJoinVerifier.checkAntibot(player, isAuthAvailable);
// then // then
verify(permissionsManager).hasPermission(player, PlayerStatePermission.BYPASS_ANTIBOT); 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 @Test
@ -397,13 +410,13 @@ public class OnJoinVerifierTest {
Player player = newPlayerWithName("Steward"); Player player = newPlayerWithName("Steward");
boolean isAuthAvailable = false; boolean isAuthAvailable = false;
given(permissionsManager.hasPermission(player, PlayerStatePermission.BYPASS_ANTIBOT)).willReturn(true); given(permissionsManager.hasPermission(player, PlayerStatePermission.BYPASS_ANTIBOT)).willReturn(true);
given(antiBotService.shouldKick(isAuthAvailable)).willReturn(true);
// when // when
onJoinVerifier.checkAntibot(player, isAuthAvailable); onJoinVerifier.checkAntibot(player, isAuthAvailable);
// then // then
verify(permissionsManager).hasPermission(player, PlayerStatePermission.BYPASS_ANTIBOT); verify(permissionsManager).hasPermission(player, PlayerStatePermission.BYPASS_ANTIBOT);
verifyZeroInteractions(antiBotService);
} }
@Test @Test
@ -412,7 +425,7 @@ public class OnJoinVerifierTest {
Player player = newPlayerWithName("D3"); Player player = newPlayerWithName("D3");
boolean isAuthAvailable = false; boolean isAuthAvailable = false;
given(permissionsManager.hasPermission(player, PlayerStatePermission.BYPASS_ANTIBOT)).willReturn(false); given(permissionsManager.hasPermission(player, PlayerStatePermission.BYPASS_ANTIBOT)).willReturn(false);
given(antiBotService.shouldKick(isAuthAvailable)).willReturn(true); given(antiBotService.shouldKick()).willReturn(true);
// when / then // when / then
try { try {
@ -420,7 +433,7 @@ public class OnJoinVerifierTest {
fail("Expected exception to be thrown"); fail("Expected exception to be thrown");
} catch (FailedVerificationException e) { } catch (FailedVerificationException e) {
verify(permissionsManager).hasPermission(player, PlayerStatePermission.BYPASS_ANTIBOT); verify(permissionsManager).hasPermission(player, PlayerStatePermission.BYPASS_ANTIBOT);
verify(antiBotService).shouldKick(isAuthAvailable); verify(antiBotService).shouldKick();
} }
} }

View File

@ -131,27 +131,12 @@ public class AntiBotServiceTest {
runSyncDelayedTaskWithDelay(bukkitService); runSyncDelayedTaskWithDelay(bukkitService);
// when // when
boolean result = antiBotService.shouldKick(false); boolean result = antiBotService.shouldKick();
// then // then
assertThat(result, equalTo(false)); 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 @Test
public void shouldActivateAntibotAfterThreshold() { public void shouldActivateAntibotAfterThreshold() {
// given // given
@ -162,12 +147,12 @@ public class AntiBotServiceTest {
runSyncDelayedTaskWithDelay(bukkitService); runSyncDelayedTaskWithDelay(bukkitService);
for (int i = 0; i < sensitivity; ++i) { for (int i = 0; i < sensitivity; ++i) {
antiBotService.shouldKick(false); antiBotService.shouldKick();
} }
assertThat(antiBotService.getAntiBotStatus(), equalTo(AntiBotService.AntiBotStatus.LISTENING)); assertThat(antiBotService.getAntiBotStatus(), equalTo(AntiBotService.AntiBotStatus.LISTENING));
// when // when
antiBotService.shouldKick(false); antiBotService.shouldKick();
// then // then
assertThat(antiBotService.getAntiBotStatus(), equalTo(AntiBotService.AntiBotStatus.ACTIVE)); assertThat(antiBotService.getAntiBotStatus(), equalTo(AntiBotService.AntiBotStatus.ACTIVE));