diff --git a/src/main/java/fr/xephi/authme/service/GeoIpService.java b/src/main/java/fr/xephi/authme/service/GeoIpService.java index f7aeaedc..9c43ee17 100644 --- a/src/main/java/fr/xephi/authme/service/GeoIpService.java +++ b/src/main/java/fr/xephi/authme/service/GeoIpService.java @@ -5,6 +5,7 @@ import com.maxmind.geoip.LookupService; import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.initialization.DataFolder; import fr.xephi.authme.util.FileUtils; +import fr.xephi.authme.util.InternetProtocolUtils; import javax.inject.Inject; import java.io.File; @@ -118,7 +119,7 @@ public class GeoIpService { * @return two-character ISO 3166-1 alpha code for the country. */ public String getCountryCode(String ip) { - if (!"127.0.0.1".equals(ip) && isDataAvailable()) { + if (!InternetProtocolUtils.isLocalAddress(ip) && isDataAvailable()) { return lookupService.getCountry(ip).getCode(); } return "--"; @@ -132,7 +133,7 @@ public class GeoIpService { * @return The name of the country. */ public String getCountryName(String ip) { - if (!"127.0.0.1".equals(ip) && isDataAvailable()) { + if (!InternetProtocolUtils.isLocalAddress(ip) && isDataAvailable()) { return lookupService.getCountry(ip).getName(); } return "N/A"; diff --git a/src/main/java/fr/xephi/authme/util/InternetProtocolUtils.java b/src/main/java/fr/xephi/authme/util/InternetProtocolUtils.java new file mode 100644 index 00000000..33ab451e --- /dev/null +++ b/src/main/java/fr/xephi/authme/util/InternetProtocolUtils.java @@ -0,0 +1,28 @@ +package fr.xephi.authme.util; + +import java.util.regex.Pattern; + +/** + * Utility class about the InternetProtocol + */ +public class InternetProtocolUtils { + + private final static Pattern LOCAL_ADDRESS_PATTERN = + Pattern.compile("(^127\\.)|(^(0)?10\\.)|(^172\\.(0)?1[6-9]\\.)|(^172\\.(0)?2[0-9]\\.)" + + "|(^172\\.(0)?3[0-1]\\.)|(^169\\.254\\.)|(^192\\.168\\.)"); + + // Utility class + private InternetProtocolUtils() { + } + + /** + * Checks if the specified address is a private or loopback address + * + * @param address address to check + * + * @return true if the address is a local or loopback address, false otherwise + */ + public static boolean isLocalAddress(String address) { + return LOCAL_ADDRESS_PATTERN.matcher(address).find(); + } +} diff --git a/src/test/java/fr/xephi/authme/util/InternetProtocolUtilsTest.java b/src/test/java/fr/xephi/authme/util/InternetProtocolUtilsTest.java new file mode 100644 index 00000000..e71f69c1 --- /dev/null +++ b/src/test/java/fr/xephi/authme/util/InternetProtocolUtilsTest.java @@ -0,0 +1,22 @@ +package fr.xephi.authme.util; + +import org.junit.Test; + +import static org.junit.Assert.assertThat; +import static org.hamcrest.Matchers.equalTo; + +/** + * Test for {@link InternetProtocolUtils} + */ +public class InternetProtocolUtilsTest { + + @Test + public void shouldCheckLocalAddress() { + assertThat(InternetProtocolUtils.isLocalAddress("127.0.0.1"), equalTo(true)); + assertThat(InternetProtocolUtils.isLocalAddress("10.0.0.1"), equalTo(true)); + assertThat(InternetProtocolUtils.isLocalAddress("172.0.0.1"), equalTo(false)); + assertThat(InternetProtocolUtils.isLocalAddress("172.16.0.1"), equalTo(true)); + assertThat(InternetProtocolUtils.isLocalAddress("192.168.0.1"), equalTo(true)); + assertThat(InternetProtocolUtils.isLocalAddress("94.32.34.5"), equalTo(false)); + } +}