mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2024-12-29 12:07:35 +01:00
GeoIP2 update
This commit is contained in:
parent
814bc8df4e
commit
7b8e5f8e4f
8
pom.xml
8
pom.xml
@ -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>
|
||||||
|
@ -1,47 +1,52 @@
|
|||||||
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.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");
|
||||||
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 +68,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 +85,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 +103,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";
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user