|
24 | 24 | import com.maxmind.geoip2.exception.GeoIp2Exception;
|
25 | 25 | import com.maxmind.geoip2.model.AsnResponse;
|
26 | 26 | import com.maxmind.geoip2.model.CityResponse;
|
| 27 | +import com.maxmind.geoip2.model.EnterpriseResponse; |
27 | 28 | import com.maxmind.geoip2.model.CountryResponse;
|
28 | 29 | import com.maxmind.geoip2.model.DomainResponse;
|
29 | 30 | import com.maxmind.geoip2.model.IspResponse;
|
| 31 | +import com.maxmind.geoip2.model.ConnectionTypeResponse.ConnectionType; |
30 | 32 | import com.maxmind.geoip2.record.*;
|
31 | 33 | import org.apache.logging.log4j.LogManager;
|
32 | 34 | import org.apache.logging.log4j.Logger;
|
@@ -58,6 +60,7 @@ public class GeoIPFilter {
|
58 | 60 | private static final String CITY_SOUTH_AMERICA_DB_TYPE = "GeoIP2-City-South-America";
|
59 | 61 | private static final String COUNTRY_DB_TYPE = "GeoIP2-Country";
|
60 | 62 | private static final String ISP_DB_TYPE = "GeoIP2-ISP";
|
| 63 | + private static final String DBIP_LOCATION_ISP_TYPE = "DBIP-Location-ISP (compat=Enterprise)"; |
61 | 64 | private static final String DOMAIN_DB_TYPE = "GeoIP2-Domain";
|
62 | 65 |
|
63 | 66 | private final String sourceField;
|
@@ -132,6 +135,11 @@ private Set<Fields> createDesiredFields(List<String> fields, final boolean ecsCo
|
132 | 135 | break;
|
133 | 136 | case DOMAIN_DB_TYPE:
|
134 | 137 | desiredFields = Fields.DEFAULT_DOMAIN_FIELDS;
|
| 138 | + case DBIP_LOCATION_ISP_TYPE: |
| 139 | + desiredFields = Fields.ALL_FIELDS; |
| 140 | + break; |
| 141 | + default: |
| 142 | + throw new IllegalStateException("Unsupported database type '" + databaseReader.getMetadata().getDatabaseType() + "'"); |
135 | 143 | }
|
136 | 144 | } else {
|
137 | 145 | for (String fieldName : fields) {
|
@@ -189,8 +197,11 @@ public boolean handleEvent(RubyEvent rubyEvent) {
|
189 | 197 | case DOMAIN_DB_TYPE:
|
190 | 198 | geoData = retrieveDomainGeoData(ipAddress);
|
191 | 199 | break;
|
| 200 | + case DBIP_LOCATION_ISP_TYPE: |
| 201 | + geoData = retrieveEnterpriseGeoData(ipAddress); |
| 202 | + break; |
192 | 203 | default:
|
193 |
| - throw new IllegalStateException("Unsupported database type " + databaseReader.getMetadata().getDatabaseType() + ""); |
| 204 | + throw new IllegalStateException("Unsupported database type '" + databaseReader.getMetadata().getDatabaseType() + "'"); |
194 | 205 | }
|
195 | 206 | } catch (UnknownHostException e) {
|
196 | 207 | logger.debug("IP Field contained invalid IP address or hostname. exception={}, field={}, event={}", e, sourceField, event);
|
@@ -352,6 +363,160 @@ private Map<Fields,Object> retrieveCityGeoData(InetAddress ipAddress) throws Geo
|
352 | 363 | return geoData;
|
353 | 364 | }
|
354 | 365 |
|
| 366 | + private Map<Fields,Object> retrieveEnterpriseGeoData(InetAddress ipAddress) throws GeoIp2Exception, IOException { |
| 367 | + EnterpriseResponse response = databaseReader.enterprise(ipAddress); |
| 368 | + Country country = response.getCountry(); |
| 369 | + City city = response.getCity(); |
| 370 | + |
| 371 | + Location location = response.getLocation(); |
| 372 | + Continent continent = response.getContinent(); |
| 373 | + Postal postal = response.getPostal(); |
| 374 | + Subdivision subdivision = response.getMostSpecificSubdivision(); |
| 375 | + Traits traits = response.getTraits(); |
| 376 | + |
| 377 | + Map<Fields, Object> geoData = new HashMap<>(); |
| 378 | + |
| 379 | + // if location is empty, there is no point populating geo data |
| 380 | + // and most likely all other fields are empty as well |
| 381 | + if (location.getLatitude() == null && location.getLongitude() == null) { |
| 382 | + return geoData; |
| 383 | + } |
| 384 | + |
| 385 | + for (Fields desiredField : this.desiredFields) { |
| 386 | + switch (desiredField) { |
| 387 | + case CITY_NAME: |
| 388 | + String cityName = city.getName(); |
| 389 | + if (cityName != null) { |
| 390 | + geoData.put(Fields.CITY_NAME, cityName); |
| 391 | + } |
| 392 | + break; |
| 393 | + case CONTINENT_CODE: |
| 394 | + String continentCode = continent.getCode(); |
| 395 | + if (continentCode != null) { |
| 396 | + geoData.put(Fields.CONTINENT_CODE, continentCode); |
| 397 | + } |
| 398 | + break; |
| 399 | + case CONTINENT_NAME: |
| 400 | + String continentName = continent.getName(); |
| 401 | + if (continentName != null) { |
| 402 | + geoData.put(Fields.CONTINENT_NAME, continentName); |
| 403 | + } |
| 404 | + break; |
| 405 | + case COUNTRY_NAME: |
| 406 | + String countryName = country.getName(); |
| 407 | + if (countryName != null) { |
| 408 | + geoData.put(Fields.COUNTRY_NAME, countryName); |
| 409 | + } |
| 410 | + break; |
| 411 | + case COUNTRY_CODE2: |
| 412 | + String countryCode2 = country.getIsoCode(); |
| 413 | + if (countryCode2 != null) { |
| 414 | + geoData.put(Fields.COUNTRY_CODE2, countryCode2); |
| 415 | + } |
| 416 | + break; |
| 417 | + case COUNTRY_CODE3: |
| 418 | + String countryCode3 = country.getIsoCode(); |
| 419 | + if (countryCode3 != null) { |
| 420 | + geoData.put(Fields.COUNTRY_CODE3, countryCode3); |
| 421 | + } |
| 422 | + break; |
| 423 | + case IP: |
| 424 | + geoData.put(Fields.IP, ipAddress.getHostAddress()); |
| 425 | + break; |
| 426 | + case POSTAL_CODE: |
| 427 | + String postalCode = postal.getCode(); |
| 428 | + if (postalCode != null) { |
| 429 | + geoData.put(Fields.POSTAL_CODE, postalCode); |
| 430 | + } |
| 431 | + break; |
| 432 | + case DMA_CODE: |
| 433 | + Integer dmaCode = location.getMetroCode(); |
| 434 | + if (dmaCode != null) { |
| 435 | + geoData.put(Fields.DMA_CODE, dmaCode); |
| 436 | + } |
| 437 | + break; |
| 438 | + case REGION_NAME: |
| 439 | + String subdivisionName = subdivision.getName(); |
| 440 | + if (subdivisionName != null) { |
| 441 | + geoData.put(Fields.REGION_NAME, subdivisionName); |
| 442 | + } |
| 443 | + break; |
| 444 | + case REGION_CODE: |
| 445 | + String subdivisionCode = subdivision.getIsoCode(); |
| 446 | + if (subdivisionCode != null) { |
| 447 | + geoData.put(Fields.REGION_CODE, subdivisionCode); |
| 448 | + } |
| 449 | + break; |
| 450 | + case REGION_ISO_CODE: |
| 451 | + String countryCodeForRegion = country.getIsoCode(); |
| 452 | + String regionCode2 = subdivision.getIsoCode(); |
| 453 | + if (countryCodeForRegion != null && regionCode2 != null) { |
| 454 | + geoData.put(Fields.REGION_ISO_CODE, String.format("%s-%s", countryCodeForRegion, regionCode2)); |
| 455 | + } |
| 456 | + break; |
| 457 | + case TIMEZONE: |
| 458 | + String locationTimeZone = location.getTimeZone(); |
| 459 | + if (locationTimeZone != null) { |
| 460 | + geoData.put(Fields.TIMEZONE, locationTimeZone); |
| 461 | + } |
| 462 | + break; |
| 463 | + case LOCATION: |
| 464 | + Double latitude = location.getLatitude(); |
| 465 | + Double longitude = location.getLongitude(); |
| 466 | + if (latitude != null && longitude != null) { |
| 467 | + Map<String, Object> locationObject = new HashMap<>(); |
| 468 | + locationObject.put("lat", latitude); |
| 469 | + locationObject.put("lon", longitude); |
| 470 | + geoData.put(Fields.LOCATION, locationObject); |
| 471 | + } |
| 472 | + break; |
| 473 | + case LATITUDE: |
| 474 | + Double lat = location.getLatitude(); |
| 475 | + if (lat != null) { |
| 476 | + geoData.put(Fields.LATITUDE, lat); |
| 477 | + } |
| 478 | + break; |
| 479 | + case LONGITUDE: |
| 480 | + Double lon = location.getLongitude(); |
| 481 | + if (lon != null) { |
| 482 | + geoData.put(Fields.LONGITUDE, lon); |
| 483 | + } |
| 484 | + break; |
| 485 | + case AUTONOMOUS_SYSTEM_NUMBER: |
| 486 | + Integer asn = traits.getAutonomousSystemNumber(); |
| 487 | + if (asn != null) { |
| 488 | + geoData.put(Fields.AUTONOMOUS_SYSTEM_NUMBER, asn); |
| 489 | + } |
| 490 | + break; |
| 491 | + case AUTONOMOUS_SYSTEM_ORGANIZATION: |
| 492 | + String aso = traits.getAutonomousSystemOrganization(); |
| 493 | + if (aso != null) { |
| 494 | + geoData.put(Fields.AUTONOMOUS_SYSTEM_ORGANIZATION, aso); |
| 495 | + } |
| 496 | + break; |
| 497 | + case ISP: |
| 498 | + String isp = traits.getIsp(); |
| 499 | + if (isp != null) { |
| 500 | + geoData.put(Fields.ISP, isp); |
| 501 | + } |
| 502 | + break; |
| 503 | + case ORGANIZATION: |
| 504 | + String org = traits.getOrganization(); |
| 505 | + if (org != null) { |
| 506 | + geoData.put(Fields.ORGANIZATION, org); |
| 507 | + } |
| 508 | + break; |
| 509 | + case CONNECTION_TYPE: |
| 510 | + ConnectionType type = traits.getConnectionType(); |
| 511 | + if (type != null) { |
| 512 | + geoData.put(Fields.CONNECTION_TYPE, type.toString()); |
| 513 | + } |
| 514 | + break; |
| 515 | + } |
| 516 | + } |
| 517 | + return geoData; |
| 518 | + } |
| 519 | + |
355 | 520 | private Map<Fields,Object> retrieveCountryGeoData(InetAddress ipAddress) throws GeoIp2Exception, IOException {
|
356 | 521 | CountryResponse response = databaseReader.country(ipAddress);
|
357 | 522 | Country country = response.getCountry();
|
|
0 commit comments