From 878ab70c2fcaba170a546558fc674cb2a880825e Mon Sep 17 00:00:00 2001 From: DNx5 Date: Sun, 20 Sep 2015 21:50:39 +0700 Subject: [PATCH] move GeoIP function into Utils --- src/main/java/fr/xephi/authme/AuthMe.java | 52 +-------------- src/main/java/fr/xephi/authme/Utils.java | 65 ++++++++++++++++++- .../authme/listener/AuthMePlayerListener.java | 4 +- .../authme/listener/AuthMeServerListener.java | 5 +- 4 files changed, 71 insertions(+), 55 deletions(-) diff --git a/src/main/java/fr/xephi/authme/AuthMe.java b/src/main/java/fr/xephi/authme/AuthMe.java index 0cf325976..737299d4f 100644 --- a/src/main/java/fr/xephi/authme/AuthMe.java +++ b/src/main/java/fr/xephi/authme/AuthMe.java @@ -175,7 +175,7 @@ public class AuthMe extends JavaPlugin { } // Download GeoIp.dat file - downloadGeoIp(); + Utils.checkGeoIP(); // Load MailApi if needed if (!Settings.getmailAccount.isEmpty() && !Settings.getmailPassword.isEmpty()) { @@ -673,54 +673,6 @@ public class AuthMe extends JavaPlugin { return player.getWorld().getSpawnLocation(); } - // Download GeoIp data - public void downloadGeoIp() { - ConsoleLogger.info("[LICENSE] This product uses data from the GeoLite API created by MaxMind, available at http://www.maxmind.com"); - File file = new File(getDataFolder(), "GeoIP.dat"); - try { - if (file.exists()) { - if (lookupService == null) { - lookupService = new LookupService(file); - } - } else { - String url = "http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz"; - URL downloadUrl = new URL(url); - URLConnection conn = downloadUrl.openConnection(); - conn.setConnectTimeout(10000); - conn.connect(); - InputStream input = conn.getInputStream(); - if (url.endsWith(".gz")) { - input = new GZIPInputStream(input); - } - OutputStream output = new FileOutputStream(file); - byte[] buffer = new byte[2048]; - int length = input.read(buffer); - while (length >= 0) { - output.write(buffer, 0, length); - length = input.read(buffer); - } - output.close(); - input.close(); - } - } catch (Exception e) { - ConsoleLogger.writeStackTrace(e); - } - } - - public String getCountryCode(String ip) { - if (lookupService != null) { - return lookupService.getCountry(ip).getCode(); - } - return "--"; - } - - public String getCountryName(String ip) { - if (lookupService != null) { - return lookupService.getCountry(ip).getName(); - } - return "N/A"; - } - public void switchAntiBotMod(boolean mode) { this.antibotMod = mode; Settings.switchAntiBotMod(mode); @@ -759,7 +711,7 @@ public class AuthMe extends JavaPlugin { message = message.replace("{WORLD}", player.getWorld().getName()); message = message.replace("{SERVER}", server.getServerName()); message = message.replace("{VERSION}", server.getBukkitVersion()); - message = message.replace("{COUNTRY}", this.getCountryName(getIP(player))); + message = message.replace("{COUNTRY}", Utils.getCountryName(getIP(player))); return message; } diff --git a/src/main/java/fr/xephi/authme/Utils.java b/src/main/java/fr/xephi/authme/Utils.java index 9540e448e..bdd156ff2 100644 --- a/src/main/java/fr/xephi/authme/Utils.java +++ b/src/main/java/fr/xephi/authme/Utils.java @@ -1,5 +1,6 @@ package fr.xephi.authme; +import com.maxmind.geoip.LookupService; import fr.xephi.authme.cache.auth.PlayerCache; import fr.xephi.authme.cache.limbo.LimboCache; import fr.xephi.authme.cache.limbo.LimboPlayer; @@ -13,19 +14,28 @@ import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import java.io.File; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.io.OutputStream; import java.lang.reflect.Method; +import java.net.URL; +import java.net.URLConnection; import java.util.Arrays; import java.util.Collection; import java.util.Collections; +import java.util.zip.GZIPInputStream; public class Utils { + public static AuthMe plugin; + private static boolean getOnlinePlayersIsCollection; private static Method getOnlinePlayers; - public static AuthMe plugin; + private static LookupService lookupService; static { plugin = AuthMe.getInstance(); + checkGeoIP(); try { Method m = Bukkit.class.getDeclaredMethod("getOnlinePlayers"); getOnlinePlayersIsCollection = m.getReturnType() == Collection.class; @@ -33,6 +43,59 @@ public class Utils { } } + // Check and Download GeoIP data if not exist + public static boolean checkGeoIP() { + if (lookupService != null) { + return true; + } + ConsoleLogger.info("[LICENSE] This product uses data from the GeoLite API created by MaxMind, available at http://www.maxmind.com"); + File file = new File(Settings.PLUGIN_FOLDER, "GeoIP.dat"); + try { + if (file.exists()) { + if (lookupService == null) { + lookupService = new LookupService(file); + return true; + } + } + String url = "http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz"; + URL downloadUrl = new URL(url); + URLConnection conn = downloadUrl.openConnection(); + conn.setConnectTimeout(10000); + conn.connect(); + InputStream input = conn.getInputStream(); + if (conn.getURL().toString().endsWith(".gz")) { + input = new GZIPInputStream(input); + } + OutputStream output = new FileOutputStream(file); + byte[] buffer = new byte[2048]; + int length = input.read(buffer); + while (length >= 0) { + output.write(buffer, 0, length); + length = input.read(buffer); + } + output.close(); + input.close(); + } catch (Exception e) { + ConsoleLogger.writeStackTrace(e); + return false; + } + return checkGeoIP(); + } + + public static String getCountryCode(String ip) { + if (checkGeoIP()) { + return lookupService.getCountry(ip).getCode(); + } + return "--"; + } + + public static String getCountryName(String ip) { + if (checkGeoIP()) { + return lookupService.getCountry(ip).getName(); + } + return "N/A"; + } + public static void setGroup(Player player, GroupType group) { if (!Settings.isPermissionCheckEnabled) return; diff --git a/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java b/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java index 390154e52..b7723522e 100644 --- a/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java +++ b/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java @@ -249,7 +249,7 @@ public class AuthMePlayerListener implements Listener { return; if (!Settings.countriesBlacklist.isEmpty()) { - String code = plugin.getCountryCode(event.getAddress().getHostAddress()); + String code = Utils.getCountryCode(event.getAddress().getHostAddress()); if (((code == null) || (Settings.countriesBlacklist.contains(code) && !isAuthAvailable)) && !plugin.authmePermissible(player, "authme.bypassantibot")) { event.setKickMessage(m.send("country_banned")[0]); event.setResult(PlayerLoginEvent.Result.KICK_OTHER); @@ -257,7 +257,7 @@ public class AuthMePlayerListener implements Listener { } } if (Settings.enableProtection && !Settings.countries.isEmpty()) { - String code = plugin.getCountryCode(event.getAddress().getHostAddress()); + String code = Utils.getCountryCode(event.getAddress().getHostAddress()); if (((code == null) || (!Settings.countries.contains(code) && !isAuthAvailable)) && !plugin.authmePermissible(player, "authme.bypassantibot")) { event.setKickMessage(m.send("country_banned")[0]); event.setResult(PlayerLoginEvent.Result.KICK_OTHER); diff --git a/src/main/java/fr/xephi/authme/listener/AuthMeServerListener.java b/src/main/java/fr/xephi/authme/listener/AuthMeServerListener.java index 8e7960210..d13e87383 100644 --- a/src/main/java/fr/xephi/authme/listener/AuthMeServerListener.java +++ b/src/main/java/fr/xephi/authme/listener/AuthMeServerListener.java @@ -2,6 +2,7 @@ package fr.xephi.authme.listener; import fr.xephi.authme.AuthMe; import fr.xephi.authme.ConsoleLogger; +import fr.xephi.authme.Utils; import fr.xephi.authme.settings.Messages; import fr.xephi.authme.settings.Settings; import org.bukkit.event.EventHandler; @@ -27,10 +28,10 @@ public class AuthMeServerListener implements Listener { if (Settings.countries.isEmpty()) return; if (!Settings.countriesBlacklist.isEmpty()) { - if (Settings.countriesBlacklist.contains(plugin.getCountryCode(event.getAddress().getHostAddress()))) + if (Settings.countriesBlacklist.contains(Utils.getCountryCode(event.getAddress().getHostAddress()))) event.setMotd(m.send("country_banned")[0]); } - if (Settings.countries.contains(plugin.getCountryCode(event.getAddress().getHostAddress()))) { + if (Settings.countries.contains(Utils.getCountryCode(event.getAddress().getHostAddress()))) { event.setMotd(plugin.getServer().getMotd()); } else { event.setMotd(m.send("country_banned")[0]);