public class IpMatchOperator extends Object
The IpMatch operator lets you implement black or white IPV$ list very easily and efficiently. All you have to do is to create a resource Json file with the list of IP you need. It can contain a mix of regular IP addresses, as well as CIDR notations. For example:
[ "10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16", "127.0.0.1/32" ]
Assuming you included that in a "blacklist.json" file, here is how to check a given IP address is in that list :
{
if (ipmatch(getResourceTuple("blacklist.json")).contains("172.16.0.2")) {
// "172.16.0.2" is in your list.
...
}
}
For compactness you can provide directly the resource tuple path. You can also directly pass in a Tuple assuming it
contains the IP you look for.
I.e.
{
if (ipmatch("blacklist.json").contains([obs][ip])) {
// [obs][ip] is in your list.
...
}
}
Internally it relies on a Patricia Trie Structure for efficient lookup. Note that you can achieve the same result
with the FindByIntervalOperator
. However the IpMatch is much more compact to deal with IP addresses.
A very common case is to retrieve some attachment information to each ip address range you define. Here is useful example. Define a (say) ranges.json resource file as follows:
[
{ "address" : "10.0.0.0/8", "attachment" : "paris" },
{ "address" : "172.16.0.0/12", "attachment" : "london" },
{ "address" : "192.168.0.0/16", "attachment" : "montreal" },
{ "address" : "127.0.0.1/32", "attachment" : "sophia" }
]
Here is how you can retrieve "london" for an input ip address in the "192.168.0.0/16" range.
{
Tuple result = ipmatch(getResourceTuple("ranges")).getAttachementFor("172.16.0.2");
// result contains a string leaf with "london".
}
This works not only for string attachement. You can define arbitrary json value, i.e:
[
{ "address" : "10.0.0.0/8", "attachement" : { "city" : "paris", "country" : france" },
{ "address" : "172.16.0.0/12", "attachement" : { "city" : "london", "country" : great britain" },
{ "address" : "192.168.0.0/16", "attachement" : { "city" : "montreal", "country" : canada" },
{ "address" : "127.0.0.1/32", "attachement" : { "city" : "sophia", "country" : france" }
]
Constructor and Description |
---|
IpMatchOperator(Tuple sourceTuple)
Constructor
|
Modifier and Type | Method and Description |
---|---|
boolean |
contains(String input)
fire the operator on the provided string.
|
boolean |
contains(Tuple tuple)
fire the operator on the provided tuple.
|
Tuple |
getAttachmentFor(String input)
fire the operator on the provided string.
|
Tuple |
getAttachmentFor(Tuple tuple)
fire the operator on the provided string.
|
public IpMatchOperator(Tuple sourceTuple)
sourceTuple
- the source i.e. input tuplepublic boolean contains(Tuple tuple)
The tuple must contain a string leaf.
tuple
- the searched value as a tuplepublic boolean contains(String input)
input
- the searched valuepublic Tuple getAttachmentFor(String input)
This variant takes an String as input.
input
- the searched valueCopyright © 2022. All rights reserved.