Skip to content

Commit c9841d8

Browse files
Supported IP2Location.io API
1 parent 02a470f commit c9841d8

8 files changed

Lines changed: 73 additions & 19 deletions

File tree

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2022 IP2Location.com
1+
Copyright (c) 2024 IP2Location.com
22

33
Licensed under the Apache License, Version 2.0 (the "License");
44
you may not use this file except in compliance with the License.

README.md

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# IP2Proxy Filter Plugin
2-
This is IP2Proxy filter plugin for Logstash that enables Logstash's users to reverse search of IP address to detect VPN servers, open proxies, web proxies, Tor exit nodes, search engine robots, data center ranges, residential proxies, consumer privacy networks, and enterprise private networks using IP2Proxy BIN database. Other information available includes proxy type, country, state, city, ISP, domain name, usage type, AS number, AS name, threats, last seen date and provider names. The library took the proxy IP address from **IP2Proxy BIN Data** file.
2+
This is IP2Proxy filter plugin for Logstash that enables Logstash's users to reverse search of IP address to detect VPN servers, open proxies, web proxies, Tor exit nodes, search engine robots, data center ranges, residential proxies, consumer privacy networks, and enterprise private networks using IP2Proxy BIN database. Other information available includes proxy type, country, state, city, ISP, domain name, usage type, AS number, AS name, threats, last seen date and provider names. The library took the proxy IP address from **IP2Proxy BIN Data** file and **IP2Location.io** data.
33

44
For the methods to use IP2Proxy filter plugin with Elastic Stack (Elasticsearch, Filebeat, Logstash, and Kibana), please take a look on this [tutorial](https://blog.ip2location.com/knowledge-base/how-to-use-ip2proxy-filter-plugin-with-elastic-stack).
55

@@ -11,6 +11,9 @@ This plugin requires IP2Proxy BIN data file to function. You may download the BI
1111
* IP2Proxy LITE BIN Data (Free): https://lite.ip2location.com
1212
* IP2Proxy Commercial BIN Data (Commercial): https://www.ip2location.com
1313

14+
## Dependencies (IP2LOCATION.IO DATA)
15+
This plugin requires API key to function. You may sign up for a free API key at https://www.ip2location.io/pricing.
16+
1417

1518
## Installation
1619
Install this plugin by the following code:
@@ -32,7 +35,33 @@ filter {
3235
match => { "message" => "%{COMBINEDAPACHELOG}"}
3336
}
3437
ip2proxy {
35-
source => "clientip"
38+
source => "[source][address]"
39+
}
40+
}
41+
42+
output {
43+
elasticsearch {
44+
hosts => [ "localhost:9200" ]
45+
}
46+
}
47+
```
48+
49+
## Config File Example using IP2Location.io
50+
```
51+
input {
52+
beats {
53+
port => "5043"
54+
}
55+
}
56+
57+
filter {
58+
grok {
59+
match => { "message" => "%{COMBINEDAPACHELOG}"}
60+
}
61+
ip2proxy {
62+
source => "[source][address]"
63+
lookup_type => "ws"
64+
api_key => "YOUR_API_KEY"
3665
}
3766
}
3867
@@ -51,12 +80,16 @@ output {
5180
|database|a valid filesystem path|No|
5281
|use_memory_mapped|boolean|No|
5382
|use_cache|boolean|No|
83+
|lookup_type|string|No|
84+
|api_key|string|No|
5485
|hide_unsupported_fields|boolean|No|
5586

5687
* **source** field is a required setting that containing the IP address or hostname to get the ip information.
5788
* **database** field is an optional setting that containing the path to the IP2Proxy BIN database file.
5889
* **use_memory_mapped** field is an optional setting that used to allow user to enable the use of memory mapped file. Default value is false.
5990
* **use_cache** field is an optional setting that used to allow user to enable the use of cache. Default value is true.
91+
* **lookup_type** field is an optional setting that used to allow user to decide the lookup method either using IP2Proxy BIN database file(db) or IP2Location.io data(ws). Default value is db.
92+
* **api_key** field is an optional setting that used to allow user to set the API Key of the IP2Location.io lookup.
6093
* **hide_unsupported_fields** field is an optional setting that used to allow user to hide unsupported fields. Default value is false.
6194

6295

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
require 'jar_dependencies'
22
require_jar('com.ip2proxy.ip2proxy', 'ip2proxy', '3.4.0')
3-
require_jar('org.logstash.filters', 'logstash-filter-ip2proxy', '2.3.2')
3+
require_jar('com.google.gson', 'gson', '2.11.0')
4+
require_jar('org.logstash.filters', 'logstash-filter-ip2proxy', '2.4.0')

lib/logstash/filters/ip2proxy.rb

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,41 +27,61 @@ class LogStash::Filters::IP2Proxy < LogStash::Filters::Base
2727
# The field used to allow user to hide unsupported fields.
2828
config :hide_unsupported_fields, :validate => :boolean, :default => false
2929

30+
# The field used to define lookup type.
31+
config :lookup_type, :validate => :string, :default => 'db'
32+
33+
# The field used to define the apikey of IP2location.io.
34+
config :api_key, :validate => :string, :default => ''
35+
3036
# The field used to define the size of the cache. It is not required and the default value is 10 000
3137
config :cache_size, :validate => :number, :required => false, :default => 10_000
3238

3339
public
3440
def register
35-
if @database.nil?
36-
@database = ::Dir.glob(::File.join(::File.expand_path("../../../vendor/", ::File.dirname(__FILE__)),"IP2PROXY-LITE-PX1.BIN")).first
37-
38-
if @database.nil? || !File.exists?(@database)
39-
raise "You must specify 'database => ...' in your ip2proxy filter (I looked for '#{@database}')"
41+
if @lookup_type == "ws"
42+
@logger.info("Using IP2Location.io API")
43+
if @api_key == ""
44+
raise "An IP2Location.io API key is required. You may sign up for a free API key at https://www.ip2location.io/pricing."
45+
end
46+
else
47+
if @database.nil?
48+
@database = ::Dir.glob(::File.join(::File.expand_path("../../../vendor/", ::File.dirname(__FILE__)),"IP2PROXY-LITE-PX1.BIN")).first
49+
50+
if @database.nil? || !File.exists?(@database)
51+
raise "You must specify 'database => ...' in your ip2proxy filter (I looked for '#{@database}')"
52+
end
4053
end
54+
@logger.info("Using ip2proxy database", :path => @database)
4155
end
4256

43-
@logger.info("Using ip2proxy database", :path => @database)
44-
45-
@ip2proxyfilter = org.logstash.filters.IP2ProxyFilter.new(@source, @target, @database, @use_memory_mapped, @hide_unsupported_fields)
57+
@ip2proxyfilter = org.logstash.filters.IP2ProxyFilter.new(@source, @target, @database, @use_memory_mapped, @hide_unsupported_fields, @lookup_type, @api_key)
4658
end
4759

4860
public
4961
def filter(event)
5062
ip = event.get(@source)
5163

5264
return unless filter?(event)
53-
if @use_cache
54-
if value = IP2ProxyCache.find(event, ip, @ip2proxyfilter, @cache_size).get('ip2proxy')
55-
event.set('ip2proxy', value)
65+
if @lookup_type == "ws"
66+
if @ip2proxyfilter.handleEvent(event)
5667
filter_matched(event)
5768
else
5869
tag_iplookup_unsuccessful(event)
5970
end
6071
else
61-
if @ip2proxyfilter.handleEvent(event)
62-
filter_matched(event)
72+
if @use_cache
73+
if value = IP2ProxyCache.find(event, ip, @ip2proxyfilter, @cache_size).get('ip2proxy')
74+
event.set('ip2proxy', value)
75+
filter_matched(event)
76+
else
77+
tag_iplookup_unsuccessful(event)
78+
end
6379
else
64-
tag_iplookup_unsuccessful(event)
80+
if @ip2proxyfilter.handleEvent(event)
81+
filter_matched(event)
82+
else
83+
tag_iplookup_unsuccessful(event)
84+
end
6585
end
6686
end
6787
end

logstash-filter-ip2proxy.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-ip2proxy'
4-
s.version = '2.3.2'
4+
s.version = '2.4.0'
55
s.licenses = ['Apache-2.0']
66
s.summary = "Logstash filter IP2Proxy"
77
s.description = "IP2Proxy filter plugin for Logstash enables Logstash's users to reverse search of IP address to detect VPN servers, open proxies, web proxies, Tor exit nodes, search engine robots, data center ranges and residential proxies using IP2Proxy BIN database."
Binary file not shown.

0 commit comments

Comments
 (0)