Download GeoLite2 Country database from playeranalytics.net

Affects issues:
- Fixed #3452
This commit is contained in:
Aurora Lahtela 2024-02-28 11:47:16 +02:00
parent 4042980379
commit 6ed23f0c0b
7 changed files with 30 additions and 42 deletions

View File

@ -20,7 +20,6 @@ import com.djrapitops.plan.exceptions.PreparationException;
import com.djrapitops.plan.settings.config.PlanConfig;
import com.djrapitops.plan.settings.config.paths.DataGatheringSettings;
import com.djrapitops.plan.storage.file.PlanFiles;
import com.djrapitops.plan.utilities.Base64Util;
import com.maxmind.geoip2.DatabaseReader;
import com.maxmind.geoip2.exception.GeoIp2Exception;
import com.maxmind.geoip2.model.CountryResponse;
@ -28,7 +27,6 @@ import com.maxmind.geoip2.record.Country;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.utils.IOUtils;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import javax.inject.Inject;
import javax.inject.Singleton;
@ -38,10 +36,10 @@ import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.zip.GZIPInputStream;
/**
@ -83,58 +81,41 @@ public class GeoLite2Geolocator implements Geolocator {
Files.delete(geolocationDB.toPath()); // Delete old data according to restriction 3. in EULA
}
}
downloadDatabase();
// Delete old Geolocation database file if it still exists (on success to avoid a no-file situation)
Files.deleteIfExists(files.getFileFromPluginFolder("GeoIP.dat").toPath());
}
private static String a(String c, String d) {
var o = new StandardPBEStringEncryptor();
g(c, q(o));
return o.decrypt(d);
}
private static void g(String h, Consumer<String> b) {
b.accept(l(h));
}
private static Consumer<String> q(StandardPBEStringEncryptor t) {
return t::setPassword;
}
private static String l(String f) {
return Base64Util.decode(f);
}
private void downloadDatabase() throws IOException {
// Avoid Socket leak with the parameters in case download url has proxy
// https://AuroraLS3.github.io/mishaps/java_socket_leak_incident
Properties properties = System.getProperties();
properties.setProperty("sun.net.client.defaultConnectTimeout", Long.toString(TimeUnit.MINUTES.toMillis(1L)));
properties.setProperty("sun.net.client.defaultReadTimeout", Long.toString(TimeUnit.MINUTES.toMillis(1L)));
properties.setProperty("sun.net.http.retryPost", Boolean.toString(false));
String key = getKey();
String downloadFrom = "https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-Country&license_key=" + key + "&suffix=tar.gz";
URL downloadSite = new URL(downloadFrom);
try (
InputStream in = downloadSite.openStream();
GZIPInputStream gzipIn = new GZIPInputStream(in);
TarArchiveInputStream tarIn = new TarArchiveInputStream(gzipIn);
FileOutputStream fos = new FileOutputStream(geolocationDB.getAbsoluteFile())
) {
findAndCopyFromTar(tarIn, fos);
String downloadURL = config.get(DataGatheringSettings.GEOLOCATION_DOWNLOAD_URL);
URL downloadSite = new URL(downloadURL);
if (downloadURL.startsWith("https://download.maxmind.com/app/geoip_download")) {
try (
InputStream in = downloadSite.openStream();
GZIPInputStream gzipIn = new GZIPInputStream(in);
TarArchiveInputStream tarIn = new TarArchiveInputStream(gzipIn);
FileOutputStream fos = new FileOutputStream(geolocationDB.getAbsoluteFile())
) {
findAndCopyFromTar(tarIn, fos);
}
} else {
URLConnection connection = downloadSite.openConnection();
connection.setRequestProperty("X-PLAN-GEODB-TOKEN", "68342d1f-5fc9-4853-bd1e-ba88c466b3a6");
try (
InputStream in = connection.getInputStream();
FileOutputStream fos = new FileOutputStream(geolocationDB.getAbsoluteFile())
) {
IOUtils.copy(in, fos);
}
}
}
private String getKey() throws IOException {
String y = "bGljZW5z";
String u = new String(files.getResourceFromJar(y + "ZV9wYXNz.txt").asBytes());
String h = new String(files.getResourceFromJar(y + "ZV9rZXlz.txt").asBytes());
return a(u, h);
}
private void findAndCopyFromTar(TarArchiveInputStream tarIn, FileOutputStream fos) throws IOException {
// Breadth first search
Queue<TarArchiveEntry> entries = new ArrayDeque<>();

View File

@ -18,6 +18,7 @@ package com.djrapitops.plan.settings.config.paths;
import com.djrapitops.plan.settings.config.paths.key.BooleanSetting;
import com.djrapitops.plan.settings.config.paths.key.Setting;
import com.djrapitops.plan.settings.config.paths.key.StringSetting;
/**
* {@link Setting} values that are in "Data_gathering" section.
@ -28,6 +29,7 @@ public class DataGatheringSettings {
public static final Setting<Boolean> GEOLOCATIONS = new BooleanSetting("Data_gathering.Geolocations");
public static final Setting<Boolean> ACCEPT_GEOLITE2_EULA = new BooleanSetting("Data_gathering.Accept_GeoLite2_EULA");
public static final Setting<String> GEOLOCATION_DOWNLOAD_URL = new StringSetting("Data_gathering.Geolocation_Download_URL");
public static final Setting<Boolean> PING = new BooleanSetting("Data_gathering.Ping");
public static final Setting<Boolean> DISK_SPACE = new BooleanSetting("Data_gathering.Disk_space");
public static final Setting<Boolean> LOG_UNKNOWN_COMMANDS = new BooleanSetting("Data_gathering.Commands.Log_unknown");

View File

@ -1 +0,0 @@
45VvUnNtiDHKZ+hq3vqx204q+tmLRE/koVskJLaT2+ipY8G1ThqcLZjUMuF79lYLpRIqpAt4KcY=

View File

@ -1 +0,0 @@
YEQ4eTdZPzUpUV4zcTp6NkE7XEw=

View File

@ -109,6 +109,9 @@ Data_gathering:
# Please accept the EULA to download GeoLite2 IP-Country Database
# https://www.maxmind.com/en/geolite2/eula
Accept_GeoLite2_EULA: false
# This can be changed to your own MaxMind URL if you have license https://dev.maxmind.com/geoip/updating-databases?lang=en#directly-downloading-databases
# e.g. https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-Country&license_key=YOUR_KEY&suffix=tar.gz
Geolocation_Download_URL: "https://geodb.playeranalytics.net/GeoLite2-Country.mmdb"
Ping: true
Disk_space: true
# Does not affect already gathered data

View File

@ -110,6 +110,9 @@ Data_gathering:
# Please accept the EULA to download GeoLite2 IP-Country Database
# https://www.maxmind.com/en/geolite2/eula
Accept_GeoLite2_EULA: false
# This can be changed to your own MaxMind URL if you have license https://dev.maxmind.com/geoip/updating-databases?lang=en#directly-downloading-databases
# e.g. https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-Country&license_key=YOUR_KEY&suffix=tar.gz
Geolocation_Download_URL: "https://geodb.playeranalytics.net/GeoLite2-Country.mmdb"
Ping: true
Disk_space: true
Commands:

View File

@ -70,6 +70,7 @@ class GeolocationTest {
void setUpCache(PlanFiles files, ConfigSystem configSystem, PlanConfig config) {
config.set(DataGatheringSettings.GEOLOCATIONS, true);
config.set(DataGatheringSettings.ACCEPT_GEOLITE2_EULA, true);
config.set(DataGatheringSettings.GEOLOCATION_DOWNLOAD_URL, "https://geodb.playeranalytics.net/GeoLite2-Country.mmdb");
GeoLite2Geolocator geoLite2Geolocator = new GeoLite2Geolocator(files, config);
PluginLogger logger = new TestPluginLogger();