Skip to content

Commit b0eeb4b

Browse files
authored
Enable the use of MaxMind GeoIP2-Domain databases (#162)
* Enable the use of MaxMind GeoIP2-Domain databases
1 parent 3f02ff4 commit b0eeb4b

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 6.0.4
2+
- Enable the use of MaxMind GeoIP2-Domain databases [#162](https://github.com/logstash-plugins/logstash-filter-geoip/pull/162)
3+
14
## 6.0.3
25
- Fixed docs for missing region_code [#158](https://github.com/logstash-plugins/logstash-filter-geoip/pull/158)
36

logstash-filter-geoip.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Gem::Specification.new do |s|
22

33
s.name = 'logstash-filter-geoip'
4-
s.version = '6.0.3'
4+
s.version = '6.0.4'
55
s.licenses = ['Apache License (2.0)']
66
s.summary = "Adds geographical information about an IP address"
77
s.description = "This gem is a Logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/logstash-plugin install gemname. This gem is not a stand-alone program"

src/main/java/org/logstash/filters/Fields.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ enum Fields {
3232
CONTINENT_NAME("continent_name"),
3333
COUNTRY_CODE2("country_code2"),
3434
COUNTRY_CODE3("country_code3"),
35+
DOMAIN("domain"),
3536
IP("ip"),
3637
ISP("isp"),
3738
POSTAL_CODE("postal_code"),
@@ -70,6 +71,8 @@ public String fieldName() {
7071
static final EnumSet<Fields> DEFAULT_ASN_LITE_FIELDS = EnumSet.of(Fields.IP, Fields.AUTONOMOUS_SYSTEM_NUMBER,
7172
Fields.AUTONOMOUS_SYSTEM_ORGANIZATION);
7273

74+
static final EnumSet<Fields> DEFAULT_DOMAIN_FIELDS = EnumSet.of(Fields.DOMAIN);
75+
7376
public static Fields parseField(String value) {
7477
try {
7578
return valueOf(value.toUpperCase(Locale.ROOT));

src/main/java/org/logstash/filters/GeoIPFilter.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.maxmind.geoip2.model.AsnResponse;
2626
import com.maxmind.geoip2.model.CityResponse;
2727
import com.maxmind.geoip2.model.CountryResponse;
28+
import com.maxmind.geoip2.model.DomainResponse;
2829
import com.maxmind.geoip2.model.IspResponse;
2930
import com.maxmind.geoip2.record.*;
3031
import org.apache.logging.log4j.LogManager;
@@ -56,6 +57,7 @@ public class GeoIPFilter {
5657
private static final String CITY_SOUTH_AMERICA_DB_TYPE = "GeoIP2-City-South-America";
5758
private static final String COUNTRY_DB_TYPE = "GeoIP2-Country";
5859
private static final String ISP_DB_TYPE = "GeoIP2-ISP";
60+
private static final String DOMAIN_DB_TYPE = "GeoIP2-Domain";
5961

6062
private final String sourceField;
6163
private final String targetField;
@@ -99,6 +101,8 @@ private Set<Fields> createDesiredFields(List<String> fields) {
99101
case ASN_LITE_DB_TYPE:
100102
desiredFields = Fields.DEFAULT_ASN_LITE_FIELDS;
101103
break;
104+
case DOMAIN_DB_TYPE:
105+
desiredFields = Fields.DEFAULT_DOMAIN_FIELDS;
102106
}
103107
} else {
104108
for (String fieldName : fields) {
@@ -153,6 +157,9 @@ public boolean handleEvent(RubyEvent rubyEvent) {
153157
case ISP_DB_TYPE:
154158
geoData = retrieveIspGeoData(ipAddress);
155159
break;
160+
case DOMAIN_DB_TYPE:
161+
geoData = retrieveDomainGeoData(ipAddress);
162+
break;
156163
default:
157164
throw new IllegalStateException("Unsupported database type " + databaseReader.getMetadata().getDatabaseType() + "");
158165
}
@@ -401,4 +408,19 @@ private Map<String, Object> retrieveAsnGeoData(InetAddress ipAddress) throws Geo
401408

402409
return geoData;
403410
}
411+
412+
private Map<String, Object> retrieveDomainGeoData(InetAddress ipAddress) throws GeoIp2Exception, IOException {
413+
DomainResponse response = databaseReader.domain(ipAddress);
414+
Map<String, Object> geoData = new HashMap<>();
415+
for (Fields desiredField : this.desiredFields) {
416+
switch (desiredField) {
417+
case DOMAIN:
418+
String domain = response.getDomain();
419+
geoData.put(Fields.DOMAIN.fieldName(), domain);
420+
break;
421+
}
422+
}
423+
424+
return geoData;
425+
}
404426
}

0 commit comments

Comments
 (0)