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
*/
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);
}

View File

@ -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

View File

@ -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();
}
}

View File

@ -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));