Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,8 @@ Protection:
# Apply the protection also to registered usernames
enableProtectionRegistered: true
geoIpDatabase:
# Enable GeoIp database
enabled: true
# The MaxMind clientId used to download the GeoIp database,
# get one at https://www.maxmind.com/en/accounts/current/license-key
# The EssentialsX project has a very useful tutorial on how to generate
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/fr/xephi/authme/service/GeoIpService.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ public class GeoIpService {
* @return True if the data is available, false otherwise.
*/
private synchronized boolean isDataAvailable() {

// If this feature is disabled, just stop
if (!settings.getProperty(ProtectionSettings.ENABLE_GEOIP)) {
return false;
}

if (downloading) {
// we are currently downloading the database
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ public final class ProtectionSettings implements SettingsHolder {
public static final Property<Boolean> ENABLE_PROTECTION_REGISTERED =
newProperty("Protection.enableProtectionRegistered", true);

@Comment("Enable GeoIp database")
public static final Property<Boolean> ENABLE_GEOIP =
newProperty("Protection.geoIpDatabase.enabled", true);

@Comment({"The MaxMind clientId used to download the GeoIp database,",
"get one at https://www.maxmind.com/en/accounts/current/license-key",
"The EssentialsX project has a very useful tutorial on how to generate",
Expand Down
30 changes: 23 additions & 7 deletions src/test/java/fr/xephi/authme/service/GeoIpServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@
import com.maxmind.db.GeoIp2Provider;
import com.maxmind.db.model.Country;
import com.maxmind.db.model.CountryResponse;

import java.io.File;
import java.io.IOException;
import java.net.InetAddress;

import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.ProtectionSettings;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -17,14 +13,18 @@
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;

/**
* Test for {@link GeoIpService}.
Expand Down Expand Up @@ -65,6 +65,7 @@ public void shouldGetCountry() throws Exception {
CountryResponse response = mock(CountryResponse.class);
given(response.getCountry()).willReturn(country);
given(lookupService.getCountry(ip)).willReturn(response);
given(settings.getProperty(ProtectionSettings.ENABLE_GEOIP)).willReturn(true);

// when
String result = geoIpService.getCountryCode(ip.getHostAddress());
Expand Down Expand Up @@ -99,6 +100,7 @@ public void shouldLookUpCountryName() throws Exception {
CountryResponse response = mock(CountryResponse.class);
given(response.getCountry()).willReturn(country);
given(lookupService.getCountry(ip)).willReturn(response);
given(settings.getProperty(ProtectionSettings.ENABLE_GEOIP)).willReturn(true);

// when
String result = geoIpService.getCountryName(ip.getHostAddress());
Expand All @@ -120,4 +122,18 @@ public void shouldNotLookUpCountryNameForLocalhostIp() throws Exception {
assertThat(result, equalTo("LocalHost"));
verify(lookupService, never()).getCountry(ip);
}

@Test
public void shouldNotLookUpCountryNameIfDisabled() throws Exception {
// given
InetAddress ip = InetAddress.getByName("24.45.167.89");
given(settings.getProperty(ProtectionSettings.ENABLE_GEOIP)).willReturn(false);

// when
String result = geoIpService.getCountryName(ip.getHostAddress());

// then
assertThat(result, equalTo("N/A"));
verifyNoInteractions(lookupService);
}
}