GithubHelp home page GithubHelp logo

ara4sh / lua-resty-ipmatcher Goto Github PK

View Code? Open in Web Editor NEW

This project forked from api7/lua-resty-ipmatcher

0.0 2.0 0.0 30 KB

High performance match IP address for OpenResty Lua.

Home Page: https://www.iresty.com/

License: Apache License 2.0

Makefile 3.02% Lua 52.50% Perl 6 2.33% Perl 42.16%

lua-resty-ipmatcher's 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 host byte order.

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 host byte order. The given IPv6 address can be wrapped by square brackets like [::1].

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

lua-resty-ipmatcher's People

Contributors

membphis avatar spacewander avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.