mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2025-01-29 19:41:35 +01:00
Removed IP2C Geolocator that has not worked for a long time
This commit is contained in:
parent
12a264b1ea
commit
579d99bc6c
@ -52,7 +52,6 @@ public class GeolocationCache implements SubSystem {
|
||||
private final Cache<String, String> cache;
|
||||
|
||||
private final Geolocator geoLite2Geolocator;
|
||||
private final Geolocator ip2cGeolocator;
|
||||
|
||||
private Geolocator inUseGeolocator;
|
||||
|
||||
@ -61,14 +60,12 @@ public class GeolocationCache implements SubSystem {
|
||||
Locale locale,
|
||||
PlanConfig config,
|
||||
GeoLite2Geolocator geoLite2Geolocator,
|
||||
IP2CGeolocator ip2cGeolocator,
|
||||
PluginLogger logger,
|
||||
RunnableFactory runnableFactory
|
||||
) {
|
||||
this.locale = locale;
|
||||
this.config = config;
|
||||
this.geoLite2Geolocator = geoLite2Geolocator;
|
||||
this.ip2cGeolocator = ip2cGeolocator;
|
||||
this.logger = logger;
|
||||
this.runnableFactory = runnableFactory;
|
||||
|
||||
@ -84,7 +81,6 @@ public class GeolocationCache implements SubSystem {
|
||||
@Override
|
||||
public void run() {
|
||||
if (inUseGeolocator == null) tryToPrepareGeoLite2();
|
||||
if (inUseGeolocator == null) tryToPrepareIP2CGeolocator();
|
||||
if (inUseGeolocator == null) logger.error("Failed to enable geolocation.");
|
||||
}
|
||||
}).runTaskAsynchronously();
|
||||
@ -97,24 +93,12 @@ public class GeolocationCache implements SubSystem {
|
||||
return inUseGeolocator != null;
|
||||
}
|
||||
|
||||
private void tryToPrepareIP2CGeolocator() {
|
||||
logger.warn("Fallback: using IP2C for Geolocation (doesn't support IPv6).");
|
||||
try {
|
||||
ip2cGeolocator.prepare();
|
||||
inUseGeolocator = ip2cGeolocator;
|
||||
} catch (PreparationException e) {
|
||||
logger.warn(e.getMessage());
|
||||
} catch (IOException e) {
|
||||
logger.error("Fallback to IP2C failed: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void tryToPrepareGeoLite2() {
|
||||
try {
|
||||
geoLite2Geolocator.prepare();
|
||||
inUseGeolocator = geoLite2Geolocator;
|
||||
} catch (PreparationException e) {
|
||||
logger.info(e.getMessage());
|
||||
logger.warn(e.getMessage());
|
||||
} catch (UnknownHostException e) {
|
||||
logger.error(locale.getString(PluginLang.ENABLE_NOTIFY_GEOLOCATIONS_INTERNET_REQUIRED));
|
||||
} catch (IOException e) {
|
||||
|
@ -1,114 +0,0 @@
|
||||
/*
|
||||
* This file is part of Player Analytics (Plan).
|
||||
*
|
||||
* Plan is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License v3 as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Plan is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.djrapitops.plan.gathering.geolocation;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.Inet6Address;
|
||||
import java.net.InetAddress;
|
||||
import java.net.URL;
|
||||
import java.util.Optional;
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* Fallback {@link Geolocator} implementation using ip2c.
|
||||
*
|
||||
* @author Rsl1122
|
||||
* @see <a href="about.ip2c.org"></a>
|
||||
*/
|
||||
@Singleton
|
||||
public class IP2CGeolocator implements Geolocator {
|
||||
|
||||
@Inject
|
||||
public IP2CGeolocator() {
|
||||
// Inject constructor required for Dagger
|
||||
}
|
||||
|
||||
@Override
|
||||
public void prepare() throws IOException {
|
||||
// Avoid Socket leak with the parameters in case download url has proxy
|
||||
// https://rsl1122.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));
|
||||
|
||||
// Run a test to see if Internet is available.
|
||||
readIPFromURL("0.0.0.0");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<String> getCountry(InetAddress inetAddress) {
|
||||
if (inetAddress instanceof Inet6Address) return Optional.empty();
|
||||
String address = inetAddress.getHostAddress();
|
||||
return getCountry(address);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<String> getCountry(String address) {
|
||||
try {
|
||||
return readIPFromURL(address);
|
||||
} catch (IOException e) {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
public Optional<String> readIPFromURL(String address) throws IOException {
|
||||
HttpURLConnection connection = null;
|
||||
try {
|
||||
connection = (HttpURLConnection) new URL("http://ip2c.org/" + address).openConnection();
|
||||
connection.setConnectTimeout((int) TimeUnit.MINUTES.toMillis(1L));
|
||||
connection.setReadTimeout((int) TimeUnit.MINUTES.toMillis(1L));
|
||||
connection.setUseCaches(false);
|
||||
connection.connect();
|
||||
try (
|
||||
InputStream in = connection.getInputStream();
|
||||
OutputStream out = connection.getOutputStream()
|
||||
) {
|
||||
String answer = readAnswer(in);
|
||||
out.close();
|
||||
return resolveIP(answer);
|
||||
}
|
||||
} finally {
|
||||
if (connection != null) connection.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
public Optional<String> resolveIP(String s) {
|
||||
switch (s.charAt(0)) {
|
||||
case '1':
|
||||
String[] reply = s.split(";");
|
||||
return reply.length >= 4 ? Optional.of(reply[3]) : Optional.empty();
|
||||
case '0': // No reply
|
||||
case '2': // Not in database
|
||||
default: // Not known char
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
public String readAnswer(InputStream is) throws IOException {
|
||||
int read;
|
||||
StringBuilder answer = new StringBuilder();
|
||||
while ((read = is.read()) != -1) answer.append((char) read);
|
||||
return answer.toString();
|
||||
}
|
||||
}
|
@ -86,7 +86,7 @@ class GeolocationTest {
|
||||
assertTrue(config.isTrue(DataGatheringSettings.GEOLOCATIONS));
|
||||
|
||||
GeoLite2Geolocator geoLite2Geolocator = new GeoLite2Geolocator(files, config);
|
||||
underTest = new GeolocationCache(new Locale(), config, geoLite2Geolocator, new IP2CGeolocator(), new TestPluginLogger(), TestRunnableFactory.forSameThread());
|
||||
underTest = new GeolocationCache(new Locale(), config, geoLite2Geolocator, new TestPluginLogger(), TestRunnableFactory.forSameThread());
|
||||
underTest.enable();
|
||||
|
||||
assertTrue(underTest.canGeolocate());
|
||||
|
Loading…
Reference in New Issue
Block a user