lua-resty-ipmatcher2

High-performance IP address matching with prefix-trie for OpenResty Lua

$ opm get potats0/lua-resty-ipmatcher2

Name

High-performance IP address matching with prefix-trie for OpenResty Lua. support longest prefix match

Synopsis

    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)
        }
    }

Methods

ipmatcher.new

Creates a new hash table to store IP addresses.

Usage

    ok = ipmatcher.new()

Example

    local ip= ipmatcher.new()

ipmatcher.insert_ipv4_host

add a host ipv4 to prefix-trie

Usage

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)

Example

    local ip = ipmatcher.new()
    ip:insert_ipv4_host("192.168.3.1", 1, 1)
    

ipmatcher.insert_ipv4_with_mask

add a CIDR into a prefix-trie

Usage

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)

Example

    local ip = ipmatcher.new()
    ip:insert_ipv4_with_mask("192.168.3.0/24", 1, 1)
    

ipmatcher:match_ipv4

check whether a ipv4 address matches a prefix-trie

Usage

ip is a host ipv4, "192.168.3.1"

    local res = ipmatcher.match_ipv4(ip)

return action of the ip.

Example

    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')
    

ipmatcher:remove_host

remove a ipv4 host from the prefix-trie

Usage

ip is a host ipv4, "192.168.3.1"

    local res = ipmatcher.remove_host(ip)

Example

    local ip = ipmatcher.new()
    ip:insert_ipv4_host("192.168.3.1", 1, 1)
    ip:remove_host("192.168.3.1")
    

ipmatcher:remove_ipv4_with_mask

remove a CIDR from the prefix-trie

Usage

ip is a CIDR ipv4, "192.168.3.1/24"

    local res = ipmatcher.remove_ipv4_with_mask(ip)

Example

    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")
    

Installation

you need besure installed the rust stable version before compile the lua-resty-ipmatcher2

From LuaRocks

    luarocks install lua-resty-ipmatcher2

From Source

    make install

Authors

potatso

License

apache2

Dependencies

luajit

Versions