High-performance IP address matching with prefix-trie for OpenResty Lua. support longest prefix match
location / {
content_by_lua_block {
local ipmatcher = require "resty.ipmatcher"
local m = ipmatcher.new()
m:insert_ipv4_with_mask("192.168.3.1/24", 1)
m:remove_ipv4_with_mask("192.168.3.1/24")
local a = m:match_ipv4("192.168.3.1")
ngx.say(a)
}
}
Creates a new hash table to store IP addresses.
ok = ipmatcher.new()
local ip= ipmatcher.new()
add a host ipv4 to prefix-trie
ip is a ipv4 address such as '192.168.1.1' action is a number, such as 1 'allow' or 'deny' 2 delay is a number of automatic aging, default is 0 whichmeans never auto-aging
ipmatcher.insert_ipv4_host(ip, action, delay)
local ip = ipmatcher.new()
ip:insert_ipv4_host("192.168.3.1", 1, 1)
add a CIDR into a prefix-trie
ip is a CIDR address such as '192.168.1.0/24' action is a number, such as 1 'allow' or 'deny' 2 delay is a number of automatic aging, default is 0 whichmeans never auto-aging
ipmatcher.insert_ipv4_with_mask(ip, action, delay)
local ip = ipmatcher.new()
ip:insert_ipv4_with_mask("192.168.3.0/24", 1, 1)
check whether a ipv4 address matches a prefix-trie
ip is a host ipv4, "192.168.3.1"
local res = ipmatcher.match_ipv4(ip)
return action of the ip.
local ip = ipmatcher.new()
ip:insert_ipv4_with_mask("192.168.3.0/24", 1, 1)
local res = ipmatcher.match_ipv4('192.168.3.1')
local res = ipmatcher.match_ipv4('192.168.3.2')
remove a ipv4 host from the prefix-trie
ip is a host ipv4, "192.168.3.1"
local res = ipmatcher.remove_host(ip)
local ip = ipmatcher.new()
ip:insert_ipv4_host("192.168.3.1", 1, 1)
ip:remove_host("192.168.3.1")
remove a CIDR from the prefix-trie
ip is a CIDR ipv4, "192.168.3.1/24"
local res = ipmatcher.remove_ipv4_with_mask(ip)
local ip = ipmatcher.new()
ip:insert_ipv4_with_mask("192.168.3.0/24", 1, 1)
ip:remove_ipv4_with_mask("192.168.3.0/24")
you need besure installed the rust stable version before compile the lua-resty-ipmatcher2
luarocks install lua-resty-ipmatcher2
make install