All Projects → api7 → Lua Resty Ipmatcher

api7 / Lua Resty Ipmatcher

Licence: apache-2.0
High-performance match IP address for Nginx + Lua

Programming Languages

lua
6591 projects

Projects that are alternatives of or similar to Lua Resty Ipmatcher

Protodate
Better Javascript Dates.
Stars: ✭ 14 (-72.55%)
Mutual labels:  parse
Cve Api
Unofficial api for cve.mitre.org
Stars: ✭ 36 (-29.41%)
Mutual labels:  parse
Vanilla
An OpenResty Lua MVC Web Framework
Stars: ✭ 1,018 (+1896.08%)
Mutual labels:  openresty
Byte
A low-level, zero-copy, panic-free, binary serializer and deserializer. (parser and encoder)
Stars: ✭ 29 (-43.14%)
Mutual labels:  parse
Edgeos Blacklist
Automatically updates IP blacklist for EdgeOS (supports IPv4 & IPv6)
Stars: ✭ 34 (-33.33%)
Mutual labels:  ipv4
Csv
A modern, fast, RFC 4180 compliant parser for JS
Stars: ✭ 38 (-25.49%)
Mutual labels:  parse
Parse Code Context
Parse code context in a single line of javascript, for functions, variable declarations, methods, prototype properties, prototype methods etc.
Stars: ✭ 7 (-86.27%)
Mutual labels:  parse
Parse Graphql
Parse GraphQL Query.
Stars: ✭ 49 (-3.92%)
Mutual labels:  parse
Lua Resty Ctxdump
Stash and apply the old ngx.ctx for avoiding being destoried after Nginx internal redirect happens.
Stars: ✭ 35 (-31.37%)
Mutual labels:  openresty
Tika Python
Tika-Python is a Python binding to the Apache Tika™ REST services allowing Tika to be called natively in the Python community.
Stars: ✭ 997 (+1854.9%)
Mutual labels:  parse
Lua Resty Post
HTTP post utility for openresty
Stars: ✭ 30 (-41.18%)
Mutual labels:  openresty
Hev Socks5 Server
A simple, lightweight socks5 server for Unix (Linux/BSD/macOS)
Stars: ✭ 33 (-35.29%)
Mutual labels:  ipv4
Ineter
Fast Java library for working with IP addresses, ranges, and subnets
Stars: ✭ 39 (-23.53%)
Mutual labels:  ipv4
Algebra Latex
Parse and calculate latex formatted math
Stars: ✭ 20 (-60.78%)
Mutual labels:  parse
Lua Libcidr Ffi
LuaJIT FFI bindings to libcidr. Provides CIDR calculations for IPv4 and IPv6.
Stars: ✭ 43 (-15.69%)
Mutual labels:  openresty
Html React Parser
📝 HTML to React parser.
Stars: ✭ 846 (+1558.82%)
Mutual labels:  parse
Extract Comments
Extract JavaScript code comments from a string or glob of files.
Stars: ✭ 36 (-29.41%)
Mutual labels:  parse
Lua Resty Waf
High-performance WAF built on the OpenResty stack
Stars: ✭ 1,053 (+1964.71%)
Mutual labels:  openresty
Commandline
CommandLine parser
Stars: ✭ 45 (-11.76%)
Mutual labels:  parse
Foundatio.parsers
A lucene style query parser that is extensible and allows modifying the query.
Stars: ✭ 39 (-23.53%)
Mutual labels:  parse

lua-resty-ipmatcher

High performance match IP address for OpenResty Lua.

API

local ipmatcher = require("resty.ipmatcher")
local ip = ipmatcher.new({
    "127.0.0.1",
    "192.168.0.0/16",
    "::1",
    "fe80::/32",
})

ngx.say(ip:match("127.0.0.1"))
ngx.say(ip:match("192.168.1.100"))
ngx.say(ip:match("::1"))

ipmatcher.new

syntax: ok, err = ipmatcher.new(ips)

The ips is a array table, like {ip1, ip2, ip3, ...}, each element in the array is a string IP address.

local ip, err = ipmatcher.new({"127.0.0.1", "192.168.0.0/16"})

Returns nil and error message if failed to create new ipmatcher instance.

It supports any CIDR format for IPv4 and IPv6.

local ip, err = ipmatcher.new({
        "127.0.0.1", "192.168.0.0/16",
        "::1", "fe80::/16",
    })

ipmatcher.new_with_value

syntax: matcher, err = ipmatcher.new_with_value(ips)

The ips is a hash table, like {[ip1] = val1, [ip2] = val2, ...}, each key in the hash is a string IP address.

When the matcher is created by new_with_value, calling match or match_bin on it will return the corresponding value of matched CIDR range instead of true.

local ip, err = ipmatcher.new_with_value({
    ["127.0.0.1"] = {info = "a"},
    ["192.168.0.0/16"] = {info = "b"},
})
local data, err = ip:match("192.168.0.1")
print(data.info) -- the value is "b"

Returns nil and error message if failed to create new ipmatcher instance.

It supports any CIDR format for IPv4 and IPv6.

local ip, err = ipmatcher.new_with_value({
    ["127.0.0.1"] = {info = "a"},
    ["192.168.0.0/16"] = {info = "b"},
    ["::1"] = 1,
    ["fe80::/32"] = "xx",
})

If the ip address can be satified by multiple CIDR ranges, the returned value is undefined (depended on the internal implementation). For instance,

local ip, err = ipmatcher.new_with_value({
    ["192.168.0.1"] = {info = "a"},
    ["192.168.0.0/16"] = {info = "b"},
})
local data, err = ip:match("192.168.0.1")
print(data.info) -- the value can be "a" or "b"

ip.match

syntax: ok, err = ip:match(ip)

Returns a true if the IP exists within any of the specified IP list. Returns a false if the IP doesn't exist within any of the specified IP list. Returns false and an error message with an invalid IP address.

local ok, err = ip:match("127.0.0.1")

ip.match_bin

syntax: ok, err = ip:match_bin(bin_ip)

Returns a true if the binary format IP exists within any of the specified IP list.

Returns nil and an error message with an invalid binary IP address.

local ok, err = ip:match_bin(ngx.var.binary_remote_addr)

ipmatcher.parse_ipv4

syntax: res = ipmatcher.parse_ipv4(ip)

Tries to parse an IPv4 address to a host byte order FFI uint32_t type integer.

Returns a false if the ip is not a valid IPv4 address.

ipmatcher.parse_ipv6

syntax: res = ipmatcher.parse_ipv6(ip)

Tries to parse an IPv6 address to a table with four host byte order FFI uint32_t type integer. The given IPv6 address can be wrapped by square brackets like [::1].

Returns a false if the ip is not a valid IPv6 address.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].