Merge pull request #104 from AuthMe-Team/geoip2

GeoIP2
This commit is contained in:
Gabriele C 2016-02-16 08:43:40 +01:00
commit 110ac36bef
2 changed files with 43 additions and 24 deletions

View File

@ -145,7 +145,7 @@
<version>2.4.3</version> <version>2.4.3</version>
<configuration> <configuration>
<createDependencyReducedPom>false</createDependencyReducedPom> <createDependencyReducedPom>false</createDependencyReducedPom>
<minimizeJar>false</minimizeJar>´ <minimizeJar>false</minimizeJar>
</configuration> </configuration>
<executions> <executions>
<execution> <execution>
@ -410,9 +410,9 @@
<!-- Maxmind GeoIp API --> <!-- Maxmind GeoIp API -->
<dependency> <dependency>
<groupId>com.maxmind.geoip</groupId> <groupId>com.maxmind.geoip2</groupId>
<artifactId>geoip-api</artifactId> <artifactId>geoip2</artifactId>
<version>1.3.1</version> <version>2.6.0</version>
<scope>compile</scope> <scope>compile</scope>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>

View File

@ -1,47 +1,57 @@
package fr.xephi.authme.util; package fr.xephi.authme.util;
import com.maxmind.geoip.LookupService; import com.maxmind.geoip2.DatabaseReader;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.Settings;
import org.bukkit.Bukkit;
import java.io.*; import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.util.concurrent.TimeUnit;
import java.util.zip.GZIPInputStream; import java.util.zip.GZIPInputStream;
public class GeoLiteAPI { public class GeoLiteAPI {
private static final String LICENSE = "[LICENSE] This product uses data from the GeoLite API created by MaxMind, " + private static final String LICENSE = "[LICENSE] This product includes GeoLite2 data created by MaxMind," +
"available at http://www.maxmind.com"; " available from http://www.maxmind.com";
private static final String GEOIP_URL = "http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry" + private static final String GEOIP_URL = "http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.mmdb.gz";
"/GeoIP.dat.gz"; private static DatabaseReader databaseReader;
private static final AuthMe plugin = AuthMe.getInstance(); private static Thread downloadTask;
private static LookupService lookupService;
/** /**
* Download (if absent) the GeoIpLite data file and then try to load it. * Download (if absent) the GeoIpLite data file and then try to load it.
* *
* @return True if the data is available, false otherwise. * @return True if the data is available, false otherwise.
*/ */
public static boolean isDataAvailable() { public synchronized static boolean isDataAvailable() {
if (lookupService != null) { if (downloadTask != null && downloadTask.isAlive()) {
return false;
}
if (databaseReader != null) {
return true; return true;
} }
final File data = new File(Settings.PLUGIN_FOLDER, "GeoIP.dat"); final File data = new File(Settings.PLUGIN_FOLDER, "GeoLite2-Country.mmdb");
boolean dataIsOld = (System.currentTimeMillis() - data.lastModified()) > TimeUnit.DAYS.toMillis(30);
if (dataIsOld && !data.delete()) {
ConsoleLogger.showError("Failed to delete GeoLiteAPI database");
}
if (data.exists()) { if (data.exists()) {
try { try {
lookupService = new LookupService(data); databaseReader = new DatabaseReader.Builder(data).build();
plugin.getLogger().info(LICENSE); ConsoleLogger.info(LICENSE);
return true; return true;
} catch (IOException e) { } catch (IOException e) {
ConsoleLogger.logException("Could not find/download GeoLiteAPI", e); ConsoleLogger.logException("Failed to load GeoLiteAPI database", e);
return false; return false;
} }
} }
// Ok, let's try to download the data file! // Ok, let's try to download the data file!
Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() { downloadTask = new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
try { try {
@ -63,10 +73,11 @@ public class GeoLiteAPI {
output.close(); output.close();
input.close(); input.close();
} catch (IOException e) { } catch (IOException e) {
ConsoleLogger.logException("Could not download GeoLiteAPI", e); ConsoleLogger.logException("Could not download GeoLiteAPI database", e);
} }
} }
}); });
downloadTask.start();
return false; return false;
} }
@ -79,7 +90,11 @@ public class GeoLiteAPI {
*/ */
public static String getCountryCode(String ip) { public static String getCountryCode(String ip) {
if (isDataAvailable()) { if (isDataAvailable()) {
return lookupService.getCountry(ip).getCode(); try {
return databaseReader.country(InetAddress.getByName(ip)).getCountry().getIsoCode();
} catch (Exception e) {
ConsoleLogger.logException("Error while getting country code", e);
}
} }
return "--"; return "--";
} }
@ -93,7 +108,11 @@ public class GeoLiteAPI {
*/ */
public static String getCountryName(String ip) { public static String getCountryName(String ip) {
if (isDataAvailable()) { if (isDataAvailable()) {
return lookupService.getCountry(ip).getName(); try {
return databaseReader.country(InetAddress.getByName(ip)).getCountry().getName();
} catch (Exception e) {
ConsoleLogger.logException("Error while getting country name", e);
}
} }
return "N/A"; return "N/A";
} }