move GeoIP function into Utils

This commit is contained in:
DNx5 2015-09-20 21:50:39 +07:00
parent 2cea7387c9
commit 878ab70c2f
4 changed files with 71 additions and 55 deletions

View File

@ -175,7 +175,7 @@ public class AuthMe extends JavaPlugin {
} }
// Download GeoIp.dat file // Download GeoIp.dat file
downloadGeoIp(); Utils.checkGeoIP();
// Load MailApi if needed // Load MailApi if needed
if (!Settings.getmailAccount.isEmpty() && !Settings.getmailPassword.isEmpty()) { if (!Settings.getmailAccount.isEmpty() && !Settings.getmailPassword.isEmpty()) {
@ -673,54 +673,6 @@ public class AuthMe extends JavaPlugin {
return player.getWorld().getSpawnLocation(); 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) { public void switchAntiBotMod(boolean mode) {
this.antibotMod = mode; this.antibotMod = mode;
Settings.switchAntiBotMod(mode); Settings.switchAntiBotMod(mode);
@ -759,7 +711,7 @@ public class AuthMe extends JavaPlugin {
message = message.replace("{WORLD}", player.getWorld().getName()); message = message.replace("{WORLD}", player.getWorld().getName());
message = message.replace("{SERVER}", server.getServerName()); message = message.replace("{SERVER}", server.getServerName());
message = message.replace("{VERSION}", server.getBukkitVersion()); message = message.replace("{VERSION}", server.getBukkitVersion());
message = message.replace("{COUNTRY}", this.getCountryName(getIP(player))); message = message.replace("{COUNTRY}", Utils.getCountryName(getIP(player)));
return message; return message;
} }

View File

@ -1,5 +1,6 @@
package fr.xephi.authme; package fr.xephi.authme;
import com.maxmind.geoip.LookupService;
import fr.xephi.authme.cache.auth.PlayerCache; import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.cache.limbo.LimboCache; import fr.xephi.authme.cache.limbo.LimboCache;
import fr.xephi.authme.cache.limbo.LimboPlayer; import fr.xephi.authme.cache.limbo.LimboPlayer;
@ -13,19 +14,28 @@ import org.bukkit.entity.Entity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.io.File; import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLConnection;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.zip.GZIPInputStream;
public class Utils { public class Utils {
public static AuthMe plugin;
private static boolean getOnlinePlayersIsCollection; private static boolean getOnlinePlayersIsCollection;
private static Method getOnlinePlayers; private static Method getOnlinePlayers;
public static AuthMe plugin; private static LookupService lookupService;
static { static {
plugin = AuthMe.getInstance(); plugin = AuthMe.getInstance();
checkGeoIP();
try { try {
Method m = Bukkit.class.getDeclaredMethod("getOnlinePlayers"); Method m = Bukkit.class.getDeclaredMethod("getOnlinePlayers");
getOnlinePlayersIsCollection = m.getReturnType() == Collection.class; 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) { public static void setGroup(Player player, GroupType group) {
if (!Settings.isPermissionCheckEnabled) if (!Settings.isPermissionCheckEnabled)
return; return;

View File

@ -249,7 +249,7 @@ public class AuthMePlayerListener implements Listener {
return; return;
if (!Settings.countriesBlacklist.isEmpty()) { 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")) { if (((code == null) || (Settings.countriesBlacklist.contains(code) && !isAuthAvailable)) && !plugin.authmePermissible(player, "authme.bypassantibot")) {
event.setKickMessage(m.send("country_banned")[0]); event.setKickMessage(m.send("country_banned")[0]);
event.setResult(PlayerLoginEvent.Result.KICK_OTHER); event.setResult(PlayerLoginEvent.Result.KICK_OTHER);
@ -257,7 +257,7 @@ public class AuthMePlayerListener implements Listener {
} }
} }
if (Settings.enableProtection && !Settings.countries.isEmpty()) { 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")) { if (((code == null) || (!Settings.countries.contains(code) && !isAuthAvailable)) && !plugin.authmePermissible(player, "authme.bypassantibot")) {
event.setKickMessage(m.send("country_banned")[0]); event.setKickMessage(m.send("country_banned")[0]);
event.setResult(PlayerLoginEvent.Result.KICK_OTHER); event.setResult(PlayerLoginEvent.Result.KICK_OTHER);

View File

@ -2,6 +2,7 @@ package fr.xephi.authme.listener;
import fr.xephi.authme.AuthMe; import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.Utils;
import fr.xephi.authme.settings.Messages; import fr.xephi.authme.settings.Messages;
import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.Settings;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
@ -27,10 +28,10 @@ public class AuthMeServerListener implements Listener {
if (Settings.countries.isEmpty()) if (Settings.countries.isEmpty())
return; return;
if (!Settings.countriesBlacklist.isEmpty()) { 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]); 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()); event.setMotd(plugin.getServer().getMotd());
} else { } else {
event.setMotd(m.send("country_banned")[0]); event.setMotd(m.send("country_banned")[0]);