use of com.maxmind.geoip2.model.CityResponse in project Essentials by drtshock.
the class EssentialsGeoIPPlayerListener method delayedJoin.
public void delayedJoin(Player player) {
User u = ess.getUser(player);
if (u.isAuthorized("essentials.geoip.hide") || player.getAddress() == null) {
return;
}
InetAddress address = player.getAddress().getAddress();
StringBuilder sb = new StringBuilder();
try {
if (config.getBoolean("database.show-cities", false)) {
CityResponse response = mmreader.city(address);
if (response == null) {
return;
}
String city;
String region;
String country;
city = response.getCity().getName();
region = response.getMostSpecificSubdivision().getName();
country = response.getCountry().getName();
if (city != null) {
sb.append(city).append(", ");
}
if (region != null) {
sb.append(region).append(", ");
}
sb.append(country);
} else {
CountryResponse response = mmreader.country(address);
sb.append(response.getCountry().getName());
}
} catch (AddressNotFoundException ex) {
// 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());
} catch (IOException | GeoIp2Exception ex) {
// GeoIP2 API forced this when address not found in their DB. jar will not complied without this.
logger.log(Level.SEVERE, tl("cantReadGeoIpDB") + " " + ex.getLocalizedMessage());
}
if (config.getBoolean("show-on-whois", true)) {
u.setGeoLocation(sb.toString());
}
if (config.getBoolean("show-on-login", true) && !u.isHidden()) {
for (Player onlinePlayer : player.getServer().getOnlinePlayers()) {
User user = ess.getUser(onlinePlayer);
if (user.isAuthorized("essentials.geoip.show")) {
user.sendMessage(tl("geoipJoinFormat", u.getDisplayName(), sb.toString()));
}
}
}
}
use of com.maxmind.geoip2.model.CityResponse in project graylog2-server by Graylog2.
the class MaxmindDataAdapter method doGet.
@Override
protected LookupResult doGet(Object key) {
final InetAddress addr;
if (key instanceof InetAddress) {
addr = (InetAddress) key;
} else {
// try to convert it somehow
try {
addr = InetAddresses.forString(key.toString());
} catch (IllegalArgumentException e) {
LOG.warn("Unable to parse IP address, returning empty result.");
return LookupResult.empty();
}
}
final IPLocationDatabaseAdapter reader = this.databaseAdapter.get();
switch(config.dbType()) {
case MAXMIND_CITY:
try {
final CityResponse city = reader.maxMindCity(addr);
if (city == null) {
LOG.debug("No city data for IP address {}, returning empty result.", addr);
return LookupResult.empty();
}
final Location location = city.getLocation();
final Map<Object, Object> map = new HashMap<>();
map.put("city", city.getCity());
map.put("continent", city.getContinent());
map.put("country", city.getCountry());
map.put("location", location);
map.put("postal", city.getPostal());
map.put("registered_country", city.getRegisteredCountry());
map.put("represented_country", city.getRepresentedCountry());
map.put("subdivisions", city.getSubdivisions());
map.put("traits", city.getTraits());
final String singleValue;
if (location == null || location.getLatitude() == null || location.getLongitude() == null) {
singleValue = null;
} else {
singleValue = location.getLatitude() + "," + location.getLongitude();
map.put("coordinates", singleValue);
}
return LookupResult.multi(singleValue, map);
} catch (AddressNotFoundException nfe) {
LOG.debug("Unable to look up city data for IP address {}, returning empty result.", addr, nfe);
return LookupResult.empty();
} catch (Exception e) {
LOG.warn("Unable to look up city data for IP address {}, returning empty result.", addr, e);
return LookupResult.empty();
}
case MAXMIND_COUNTRY:
try {
final CountryResponse countryResponse = reader.maxMindCountry(addr);
if (countryResponse == null) {
LOG.debug("No country data for IP address {}, returning empty result.", addr);
return LookupResult.empty();
}
final Country country = countryResponse.getCountry();
final Map<Object, Object> map = new HashMap<>();
map.put("continent", countryResponse.getContinent());
map.put("country", country);
map.put("registered_country", countryResponse.getRegisteredCountry());
map.put("represented_country", countryResponse.getRepresentedCountry());
map.put("traits", countryResponse.getTraits());
final String singleValue = country == null ? null : country.getIsoCode();
return LookupResult.multi(singleValue, map);
} catch (AddressNotFoundException nfe) {
LOG.debug("Unable to look up country data for IP address {}, returning empty result.", addr, nfe);
return LookupResult.empty();
} catch (Exception e) {
LOG.warn("Unable to look up country data for IP address {}, returning empty result.", addr, e);
return LookupResult.empty();
}
case MAXMIND_ASN:
try {
final AsnResponse asn = reader.maxMindASN(addr);
if (asn == null) {
LOG.debug("No ASN data for IP address {}, returning empty result.", addr);
return LookupResult.empty();
}
final ImmutableMap<Object, Object> map = ImmutableMap.of("as_number", asn.getAutonomousSystemNumber(), "as_organization", asn.getAutonomousSystemOrganization());
return LookupResult.multi(asn.getAutonomousSystemNumber(), map);
} catch (AddressNotFoundException nfe) {
LOG.debug("Unable to look up ASN data for IP address {}, returning empty result.", addr, nfe);
return LookupResult.empty();
} catch (Exception e) {
LOG.warn("Unable to look up ASN data for IP address {}, returning empty result.", addr, e);
return LookupResult.empty();
}
case IPINFO_ASN:
try {
final IPinfoASN ipInfo = reader.ipInfoASN(addr);
if (ipInfo == null) {
LOG.debug("No IPinfo location data for IP address {}, returning empty result.", addr);
return LookupResult.empty();
}
// Values can be null so we cannot use an ImmutableMap here
final Map<Object, Object> multiValue = new HashMap<>();
multiValue.put("asn", ipInfo.asn());
multiValue.put("asn_numeric", ipInfo.asnNumeric());
multiValue.put("name", ipInfo.name());
multiValue.put("domain", ipInfo.domain());
multiValue.put("type", ipInfo.type());
multiValue.put("route", ipInfo.route());
return LookupResult.multi(ipInfo.asnNumeric(), Collections.unmodifiableMap(multiValue));
} catch (AddressNotFoundException e) {
LOG.debug("Unable to look up IPinfo ASN data for IP address {}, returning empty result.", addr, e);
return LookupResult.empty();
} catch (Exception e) {
LOG.warn("Unable to look up IPinfo ASN data for IP address {}, returning empty result.", addr, e);
return LookupResult.empty();
}
case IPINFO_STANDARD_LOCATION:
try {
final IPinfoStandardLocation ipInfo = reader.ipInfoStandardLocation(addr);
if (ipInfo == null) {
LOG.debug("No IPinfo location data for IP address {}, returning empty result.", addr);
return LookupResult.empty();
}
// Values can be null so we cannot use an ImmutableMap here
final Map<Object, Object> multiValue = new HashMap<>();
multiValue.put("coordinates", ipInfo.coordinates());
multiValue.put("latitude", ipInfo.latitude());
multiValue.put("longitude", ipInfo.longitude());
multiValue.put("city", ipInfo.city());
multiValue.put("country", ipInfo.country());
multiValue.put("region", ipInfo.region());
multiValue.put("timezone", ipInfo.timezone());
multiValue.put("geoname_id", ipInfo.geoNameId());
return LookupResult.multi(ipInfo.coordinates(), Collections.unmodifiableMap(multiValue));
} catch (AddressNotFoundException e) {
LOG.debug("Unable to look up IPinfo location data for IP address {}, returning empty result.", addr, e);
return LookupResult.empty();
} catch (Exception e) {
LOG.warn("Unable to look up IPinfo location data for IP address {}, returning empty result.", addr, e);
return LookupResult.empty();
}
}
return LookupResult.empty();
}
Aggregations