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,20 +253,22 @@ public class AuthMePlayerListener implements Listener {
} }
} }
if (auth == null) {
if (!Settings.countriesBlacklist.isEmpty() || !Settings.countries.isEmpty()) {
String playerIP = event.getAddress().getHostAddress(); String playerIP = event.getAddress().getHostAddress();
if (auth == null && Settings.enableProtection) {
String countryCode = GeoLiteAPI.getCountryCode(playerIP); String countryCode = GeoLiteAPI.getCountryCode(playerIP);
if (!Settings.countriesBlacklist.isEmpty() && Settings.countriesBlacklist.contains(countryCode)) { if (Settings.countriesBlacklist.contains(countryCode)) {
event.setLoginResult(AsyncPlayerPreLoginEvent.Result.KICK_OTHER); event.setLoginResult(AsyncPlayerPreLoginEvent.Result.KICK_OTHER);
event.setKickMessage(m.retrieveSingle(MessageKey.COUNTRY_BANNED_ERROR)); event.setKickMessage(m.retrieveSingle(MessageKey.COUNTRY_BANNED_ERROR));
return; return;
} }
if (!Settings.countries.isEmpty() && !Settings.countries.contains(countryCode)) { if (Settings.enableProtection && !Settings.countries.contains(countryCode)) {
event.setLoginResult(AsyncPlayerPreLoginEvent.Result.KICK_OTHER); event.setLoginResult(AsyncPlayerPreLoginEvent.Result.KICK_OTHER);
event.setKickMessage(m.retrieveSingle(MessageKey.COUNTRY_BANNED_ERROR)); event.setKickMessage(m.retrieveSingle(MessageKey.COUNTRY_BANNED_ERROR));
return; return;
} }
} }
}
final String name = event.getName().toLowerCase(); final String name = event.getName().toLowerCase();
final Player player = Utils.getPlayer(name); final Player player = Utils.getPlayer(name);

View File

@ -32,16 +32,17 @@ public class AuthMeServerListener implements Listener {
return; return;
} }
if (!Settings.countriesBlacklist.isEmpty() || !Settings.countries.isEmpty()){
String countryCode = GeoLiteAPI.getCountryCode(event.getAddress().getHostAddress()); String countryCode = GeoLiteAPI.getCountryCode(event.getAddress().getHostAddress());
if (!Settings.countriesBlacklist.isEmpty() && Settings.countriesBlacklist.contains(countryCode)) { if( Settings.countriesBlacklist.contains(countryCode)) {
event.setMotd(m.retrieveSingle(MessageKey.COUNTRY_BANNED_ERROR)); event.setMotd(m.retrieveSingle(MessageKey.COUNTRY_BANNED_ERROR));
return; return;
} }
if (!Settings.countries.contains(countryCode)) {
if (!Settings.countries.isEmpty() && !Settings.countries.contains(countryCode)) {
event.setMotd(m.retrieveSingle(MessageKey.COUNTRY_BANNED_ERROR)); event.setMotd(m.retrieveSingle(MessageKey.COUNTRY_BANNED_ERROR));
} }
} }
}
@EventHandler(priority = EventPriority.HIGHEST) @EventHandler(priority = EventPriority.HIGHEST)
public void onPluginDisable(PluginDisableEvent event) { public void onPluginDisable(PluginDisableEvent event) {

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";