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 && Settings.enableProtection) {
String countryCode = GeoLiteAPI.getCountryCode(playerIP);
if (!Settings.countriesBlacklist.isEmpty() && Settings.countriesBlacklist.contains(countryCode)) {
event.setLoginResult(AsyncPlayerPreLoginEvent.Result.KICK_OTHER);
event.setKickMessage(m.retrieveSingle(MessageKey.COUNTRY_BANNED_ERROR));
return;
}
if (!Settings.countries.isEmpty() && !Settings.countries.contains(countryCode)) {
event.setLoginResult(AsyncPlayerPreLoginEvent.Result.KICK_OTHER);
event.setKickMessage(m.retrieveSingle(MessageKey.COUNTRY_BANNED_ERROR));
return;
if (auth == null) {
if (!Settings.countriesBlacklist.isEmpty() || !Settings.countries.isEmpty()) {
String playerIP = event.getAddress().getHostAddress();
String countryCode = GeoLiteAPI.getCountryCode(playerIP);
if (Settings.countriesBlacklist.contains(countryCode)) {
event.setLoginResult(AsyncPlayerPreLoginEvent.Result.KICK_OTHER);
event.setKickMessage(m.retrieveSingle(MessageKey.COUNTRY_BANNED_ERROR));
return;
}
if (Settings.enableProtection && !Settings.countries.contains(countryCode)) {
event.setLoginResult(AsyncPlayerPreLoginEvent.Result.KICK_OTHER);
event.setKickMessage(m.retrieveSingle(MessageKey.COUNTRY_BANNED_ERROR));
return;
}
}
}

View File

@ -32,14 +32,15 @@ public class AuthMeServerListener implements Listener {
return;
}
String countryCode = GeoLiteAPI.getCountryCode(event.getAddress().getHostAddress());
if (!Settings.countriesBlacklist.isEmpty() && Settings.countriesBlacklist.contains(countryCode)) {
event.setMotd(m.retrieveSingle(MessageKey.COUNTRY_BANNED_ERROR));
return;
}
if (!Settings.countries.isEmpty() && !Settings.countries.contains(countryCode)) {
event.setMotd(m.retrieveSingle(MessageKey.COUNTRY_BANNED_ERROR));
if (!Settings.countriesBlacklist.isEmpty() || !Settings.countries.isEmpty()){
String countryCode = GeoLiteAPI.getCountryCode(event.getAddress().getHostAddress());
if( Settings.countriesBlacklist.contains(countryCode)) {
event.setMotd(m.retrieveSingle(MessageKey.COUNTRY_BANNED_ERROR));
return;
}
if (!Settings.countries.contains(countryCode)) {
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.
*
* @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) {
if (isDataAvailable()) {
if (!"127.0.0.1".equals(ip) && isDataAvailable()) {
try {
return databaseReader.country(InetAddress.getByName(ip)).getCountry().getIsoCode();
} catch (Exception e) {
ConsoleLogger.logException("Error while getting country code", e);
ConsoleLogger.writeStackTrace(e);
}
}
return "--";
@ -102,16 +102,16 @@ public class GeoLiteAPI {
/**
* 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) {
if (isDataAvailable()) {
if (!"127.0.0.1".equals(ip) && isDataAvailable()) {
try {
return databaseReader.country(InetAddress.getByName(ip)).getCountry().getName();
} catch (Exception e) {
ConsoleLogger.logException("Error while getting country name", e);
ConsoleLogger.writeStackTrace(e);
}
}
return "N/A";