mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2024-12-23 09:08:15 +01:00
GeoIP: ignore local addresses
This commit is contained in:
parent
001e5d0376
commit
cd4693eedf
@ -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";
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
@ -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));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user