fix geoip country returning "." if the user is joining from localhost (#2494) @latiku

Implements a check to see if the user's address is `localhost` or `127.0.0.1`, and if so return that the country is unknown.

**Edit by @md678685: fixes #2471.**
This commit is contained in:
latiku 2019-04-22 04:38:06 -04:00 committed by md678685
parent 0aa11b58b9
commit b4baa28f01
2 changed files with 28 additions and 5 deletions

View File

@ -141,6 +141,7 @@ gcmax=\u00a76Maximum memory\:\u00a7c {0} MB.
gctotal=\u00a76Allocated memory\:\u00a7c {0} MB.
gcWorld=\u00a76{0} "\u00a7c{1}\u00a76"\: \u00a7c{2}\u00a76 chunks, \u00a7c{3}\u00a76 entities, \u00a7c{4}\u00a76 tiles.
geoipJoinFormat=\u00a76Player \u00a7c{0} \u00a76comes from \u00a7c{1}\u00a76.
geoipCantFind=\u00a76Player \u00a7c{0} \u00a76comes from \u00a7an unknown country\u00a76.
geoIpUrlEmpty=GeoIP download url is empty.
geoIpUrlInvalid=GeoIP download url is invalid.
givenSkull=\u00a76You have been given the skull of \u00a7c{0}\u00a76.

View File

@ -15,10 +15,7 @@ import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import java.io.*;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.*;
import java.util.logging.Level;
import java.util.Date;
import java.util.logging.Logger;
@ -58,6 +55,8 @@ public class EssentialsGeoIPPlayerListener implements Listener, IConf {
}
InetAddress address = player.getAddress().getAddress();
StringBuilder sb = new StringBuilder();
try {
if (config.getBoolean("database.show-cities", false)) {
CityResponse response = mmreader.city(address);
@ -82,6 +81,16 @@ public class EssentialsGeoIPPlayerListener implements Listener, IConf {
sb.append(response.getCountry().getName());
}
} catch (AddressNotFoundException ex) {
if (checkIfLocal(address)) {
for (Player online : player.getServer().getOnlinePlayers()) {
User user = ess.getUser(online);
if (user.isAuthorized("essentials.geoip.show")) {
user.sendMessage(tl("geoipCantFind", u.getDisplayName()));
}
}
return;
}
// GeoIP2 API forced this when address not found in their DB. jar will not complied without this.
// TODO: Maybe, we can set a new custom msg about addr-not-found in messages.properties.
logger.log(Level.INFO, tl("cantReadGeoIpDB") + " " + ex.getLocalizedMessage());
@ -205,4 +214,17 @@ public class EssentialsGeoIPPlayerListener implements Listener, IConf {
logger.log(Level.SEVERE, tl("connectionFailed"), ex);
}
}
}
private boolean checkIfLocal(InetAddress address) {
if (address.isAnyLocalAddress() || address.isLoopbackAddress()) {
return true;
}
// Double checks if address is defined on any interface
try {
return NetworkInterface.getByInetAddress(address) != null;
} catch (SocketException e) {
return false;
}
}
}