• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

api7/lua-resty-ipmatcher: High-performance match IP address for Nginx + Lua

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

api7/lua-resty-ipmatcher

开源软件地址(OpenSource Url):

https://github.com/api7/lua-resty-ipmatcher

开源编程语言(OpenSource Language):

Lua 54.3%

开源软件介绍(OpenSource Introduction):

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.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
dnaeon/gru: Orchestration made easy with Go and Lua发布时间:2022-08-16
下一篇:
api7/jsonschema: Pure Lua JSON schema validator for Lua/LuaJIT发布时间:2022-08-16
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap