Migrate to GEO IP 2 (Related #1471) (#1529)

* Migrate to GEO IP 2, because support will drop in April

* Change all links of maxmind to https
* Update maxmind database dependency and add javatar to extract
the database from the tar archive
(now only a small difference in jar file size -> ~80KB smaller)
* Verify downloaded archive using MD5 (There are no other checksums available)
* Migrate to Java NIO instead of old java file I/O (Feedback?)
* Internal Optional usage for nullable values (Feedback?)

Minor:
* Schedule a Bukkit async task instead of creating a thread manually
* Validate ip input string
* Extract validation into single method
* Close all resources safely using try-resources

* More https links

* Add documentation

* Set the same last modification as in the tar archive

* Fix tests

* Comment how the legacy API responded to unknown entries

* Document missing function param

* Document our maxmind dependency modifications

* Include time unit into constant

* More logging for downloading the database

* Add missing return if the database cannot be found

* Delete temporarily file after working with it
This commit is contained in:
games647
2018-03-17 03:00:24 +01:00
committed by Gabriele C
parent 7ff5801cfe
commit f33446ee25
6 changed files with 262 additions and 107 deletions
@@ -1,7 +1,13 @@
package fr.xephi.authme.service;
import com.maxmind.geoip.Country;
import com.maxmind.geoip.LookupService;
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 org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
@@ -10,13 +16,11 @@ import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.io.File;
import java.io.IOException;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
@@ -29,8 +33,12 @@ public class GeoIpServiceTest {
private GeoIpService geoIpService;
private File dataFolder;
@Mock
private LookupService lookupService;
private GeoIp2Provider lookupService;
@Mock
private BukkitService bukkitService;
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
@@ -38,20 +46,24 @@ public class GeoIpServiceTest {
@Before
public void initializeGeoLiteApi() throws IOException {
dataFolder = temporaryFolder.newFolder();
geoIpService = new GeoIpService(dataFolder, lookupService);
geoIpService = new GeoIpService(dataFolder, bukkitService, lookupService);
}
@Test
public void shouldGetCountry() {
public void shouldGetCountry() throws Exception {
// given
String ip = "123.45.67.89";
InetAddress ip = InetAddress.getByName("123.45.67.89");
String countryCode = "XX";
Country country = mock(Country.class);
given(country.getCode()).willReturn(countryCode);
given(lookupService.getCountry(ip)).willReturn(country);
given(country.getIsoCode()).willReturn(countryCode);
CountryResponse response = mock(CountryResponse.class);
given(response.getCountry()).willReturn(country);
given(lookupService.getCountry(ip)).willReturn(response);
// when
String result = geoIpService.getCountryCode(ip);
String result = geoIpService.getCountryCode(ip.getHostAddress());
// then
assertThat(result, equalTo(countryCode));
@@ -59,7 +71,7 @@ public class GeoIpServiceTest {
}
@Test
public void shouldNotLookUpCountryForLocalhostIp() {
public void shouldNotLookUpCountryForLocalhostIp() throws Exception {
// given
String ip = "127.0.0.1";
@@ -68,20 +80,24 @@ public class GeoIpServiceTest {
// then
assertThat(result, equalTo("--"));
verify(lookupService, never()).getCountry(anyString());
verify(lookupService, never()).getCountry(any());
}
@Test
public void shouldLookUpCountryName() {
public void shouldLookUpCountryName() throws Exception {
// given
String ip = "24.45.167.89";
InetAddress ip = InetAddress.getByName("24.45.167.89");
String countryName = "Ecuador";
Country country = mock(Country.class);
given(country.getName()).willReturn(countryName);
given(lookupService.getCountry(ip)).willReturn(country);
CountryResponse response = mock(CountryResponse.class);
given(response.getCountry()).willReturn(country);
given(lookupService.getCountry(ip)).willReturn(response);
// when
String result = geoIpService.getCountryName(ip);
String result = geoIpService.getCountryName(ip.getHostAddress());
// then
assertThat(result, equalTo(countryName));
@@ -89,16 +105,15 @@ public class GeoIpServiceTest {
}
@Test
public void shouldNotLookUpCountryNameForLocalhostIp() {
public void shouldNotLookUpCountryNameForLocalhostIp() throws Exception {
// given
String ip = "127.0.0.1";
InetAddress ip = InetAddress.getByName("127.0.0.1");
// when
String result = geoIpService.getCountryName(ip);
String result = geoIpService.getCountryName(ip.getHostAddress());
// then
assertThat(result, equalTo("N/A"));
verify(lookupService, never()).getCountry(ip);
}
}