Merge pull request #106 from AuthMe-Team/geoip

Going back to GeoIP legacy. :)
This commit is contained in:
DNx 2016-02-19 22:27:28 +07:00
commit 5ea032840d
2 changed files with 14 additions and 23 deletions

View File

@ -416,9 +416,9 @@
<!-- Maxmind GeoIp API --> <!-- Maxmind GeoIp API -->
<dependency> <dependency>
<groupId>com.maxmind.geoip2</groupId> <groupId>com.maxmind.geoip</groupId>
<artifactId>geoip2</artifactId> <artifactId>geoip-api</artifactId>
<version>2.6.0</version> <version>1.3.1</version>
<scope>compile</scope> <scope>compile</scope>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>

View File

@ -1,6 +1,6 @@
package fr.xephi.authme.util; package fr.xephi.authme.util;
import com.maxmind.geoip2.DatabaseReader; import com.maxmind.geoip.LookupService;
import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.Settings;
@ -9,18 +9,17 @@ import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; 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.concurrent.TimeUnit;
import java.util.zip.GZIPInputStream; import java.util.zip.GZIPInputStream;
public class GeoLiteAPI { public class GeoLiteAPI {
private static final String LICENSE =
private static final String LICENSE = "[LICENSE] This product includes GeoLite2 data created by MaxMind," + "[LICENSE] This product uses data from the GeoLite API created by MaxMind, available at http://www.maxmind.com";
" available from http://www.maxmind.com"; private static final String GEOIP_URL =
private static final String GEOIP_URL = "http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.mmdb.gz"; "http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz";
private static DatabaseReader databaseReader; private static LookupService lookupService;
private static Thread downloadTask; private static Thread downloadTask;
/** /**
@ -32,17 +31,17 @@ public class GeoLiteAPI {
if (downloadTask != null && downloadTask.isAlive()) { if (downloadTask != null && downloadTask.isAlive()) {
return false; return false;
} }
if (databaseReader != null) { if (lookupService != null) {
return true; return true;
} }
final File data = new File(Settings.PLUGIN_FOLDER, "GeoLite2-Country.mmdb"); final File data = new File(Settings.PLUGIN_FOLDER, "GeoIP.dat");
boolean dataIsOld = (System.currentTimeMillis() - data.lastModified()) > TimeUnit.DAYS.toMillis(30); boolean dataIsOld = (System.currentTimeMillis() - data.lastModified()) > TimeUnit.DAYS.toMillis(30);
if (dataIsOld && !data.delete()) { if (dataIsOld && !data.delete()) {
ConsoleLogger.showError("Failed to delete GeoLiteAPI database"); ConsoleLogger.showError("Failed to delete GeoLiteAPI database");
} }
if (data.exists()) { if (data.exists()) {
try { try {
databaseReader = new DatabaseReader.Builder(data).build(); lookupService = new LookupService(data);
ConsoleLogger.info(LICENSE); ConsoleLogger.info(LICENSE);
return true; return true;
} catch (IOException e) { } catch (IOException e) {
@ -90,11 +89,7 @@ public class GeoLiteAPI {
*/ */
public static String getCountryCode(String ip) { public static String getCountryCode(String ip) {
if (!"127.0.0.1".equals(ip) && isDataAvailable()) { if (!"127.0.0.1".equals(ip) && isDataAvailable()) {
try { return lookupService.getCountry(ip).getCode();
return databaseReader.country(InetAddress.getByName(ip)).getCountry().getIsoCode();
} catch (Exception e) {
ConsoleLogger.writeStackTrace(e);
}
} }
return "--"; return "--";
} }
@ -108,11 +103,7 @@ public class GeoLiteAPI {
*/ */
public static String getCountryName(String ip) { public static String getCountryName(String ip) {
if (!"127.0.0.1".equals(ip) && isDataAvailable()) { if (!"127.0.0.1".equals(ip) && isDataAvailable()) {
try { return lookupService.getCountry(ip).getName();
return databaseReader.country(InetAddress.getByName(ip)).getCountry().getName();
} catch (Exception e) {
ConsoleLogger.writeStackTrace(e);
}
} }
return "N/A"; return "N/A";
} }