Merge pull request #105 from AuthMe-Team/geoip2

This commit is contained in:
DNx 2016-02-18 10:43:52 +07:00
commit 10fedfb66d
3 changed files with 31 additions and 28 deletions

View File

@ -253,18 +253,20 @@ public class AuthMePlayerListener implements Listener {
} }
} }
String playerIP = event.getAddress().getHostAddress(); if (auth == null) {
if (auth == null && Settings.enableProtection) { if (!Settings.countriesBlacklist.isEmpty() || !Settings.countries.isEmpty()) {
String countryCode = GeoLiteAPI.getCountryCode(playerIP); String playerIP = event.getAddress().getHostAddress();
if (!Settings.countriesBlacklist.isEmpty() && Settings.countriesBlacklist.contains(countryCode)) { String countryCode = GeoLiteAPI.getCountryCode(playerIP);
event.setLoginResult(AsyncPlayerPreLoginEvent.Result.KICK_OTHER); if (Settings.countriesBlacklist.contains(countryCode)) {
event.setKickMessage(m.retrieveSingle(MessageKey.COUNTRY_BANNED_ERROR)); event.setLoginResult(AsyncPlayerPreLoginEvent.Result.KICK_OTHER);
return; event.setKickMessage(m.retrieveSingle(MessageKey.COUNTRY_BANNED_ERROR));
} return;
if (!Settings.countries.isEmpty() && !Settings.countries.contains(countryCode)) { }
event.setLoginResult(AsyncPlayerPreLoginEvent.Result.KICK_OTHER); if (Settings.enableProtection && !Settings.countries.contains(countryCode)) {
event.setKickMessage(m.retrieveSingle(MessageKey.COUNTRY_BANNED_ERROR)); event.setLoginResult(AsyncPlayerPreLoginEvent.Result.KICK_OTHER);
return; event.setKickMessage(m.retrieveSingle(MessageKey.COUNTRY_BANNED_ERROR));
return;
}
} }
} }

View File

@ -32,14 +32,15 @@ public class AuthMeServerListener implements Listener {
return; return;
} }
String countryCode = GeoLiteAPI.getCountryCode(event.getAddress().getHostAddress()); if (!Settings.countriesBlacklist.isEmpty() || !Settings.countries.isEmpty()){
if (!Settings.countriesBlacklist.isEmpty() && Settings.countriesBlacklist.contains(countryCode)) { String countryCode = GeoLiteAPI.getCountryCode(event.getAddress().getHostAddress());
event.setMotd(m.retrieveSingle(MessageKey.COUNTRY_BANNED_ERROR)); if( Settings.countriesBlacklist.contains(countryCode)) {
return; event.setMotd(m.retrieveSingle(MessageKey.COUNTRY_BANNED_ERROR));
} return;
}
if (!Settings.countries.isEmpty() && !Settings.countries.contains(countryCode)) { if (!Settings.countries.contains(countryCode)) {
event.setMotd(m.retrieveSingle(MessageKey.COUNTRY_BANNED_ERROR)); event.setMotd(m.retrieveSingle(MessageKey.COUNTRY_BANNED_ERROR));
}
} }
} }

View File

@ -84,16 +84,16 @@ public class GeoLiteAPI {
/** /**
* Get the country code of the given IP address. * Get the country code of the given IP address.
* *
* @param ip Ip address * @param ip textual IP address to lookup.
* *
* @return String * @return two-character ISO 3166-1 alpha code for the country.
*/ */
public static String getCountryCode(String ip) { public static String getCountryCode(String ip) {
if (isDataAvailable()) { if (!"127.0.0.1".equals(ip) && isDataAvailable()) {
try { try {
return databaseReader.country(InetAddress.getByName(ip)).getCountry().getIsoCode(); return databaseReader.country(InetAddress.getByName(ip)).getCountry().getIsoCode();
} catch (Exception e) { } catch (Exception e) {
ConsoleLogger.logException("Error while getting country code", e); ConsoleLogger.writeStackTrace(e);
} }
} }
return "--"; return "--";
@ -102,16 +102,16 @@ public class GeoLiteAPI {
/** /**
* Get the country name of the given IP address. * Get the country name of the given IP address.
* *
* @param ip Ip address * @param ip textual IP address to lookup.
* *
* @return String * @return The name of the country.
*/ */
public static String getCountryName(String ip) { public static String getCountryName(String ip) {
if (isDataAvailable()) { if (!"127.0.0.1".equals(ip) && isDataAvailable()) {
try { try {
return databaseReader.country(InetAddress.getByName(ip)).getCountry().getName(); return databaseReader.country(InetAddress.getByName(ip)).getCountry().getName();
} catch (Exception e) { } catch (Exception e) {
ConsoleLogger.logException("Error while getting country name", e); ConsoleLogger.writeStackTrace(e);
} }
} }
return "N/A"; return "N/A";