lua-resty-ipip

用于 OpenResty 的 ipip.net(17MonIP.datx) 解析库。

$ opm get linsir/lua-resty-ipip

lua-resty-ipip

lua-resty-ipip - 用于 OpenResty 的 ipip.net(17MonIP) 解析库。

状态

已准备好进行测试。在大多数情况下可能已准备好投入生产,尽管尚未在实际环境中得到验证。请检查问题列表,如果您有任何问题/疑问,请告诉我。

描述

基于 http://www.ipip.net(中国最好的 IP 数据库)的 IP 查询。

安装

opm install pintsized/lua-resty-http opm get linsir/lua-resty-ipip # 使用 root 权限

概要

``lua lua_package_path "/usr/local/openresty/demo/?.lua;;"; lua_code_cache on; resolver 223.5.5.5; init_by_lua ' local ipip = require "resty.ipip.ipip" cjson = require "cjson" local opts = { path = "/path/to/17monipdb.datx", token = "your token", timeout = "2000", } ipipc = ipip:new(opts) '; server { listen 8000; charset utf-8; # server_name ip.linsir.org; default_type text/plain; root /usr/local/openresty/nginx/html;

        location /ip {
            content_by_lua '
                local ipipc = ipipc
                local cjson = cjson
                local res, err = ipipc:query_file("202.103.026.255")
                if not res then
                    ngx.say(err)
                    return
                end
                ngx.say(cjson.encode(res))
            ';
        }

        location /free_api {
            content_by_lua '
                local ipipc = ipipc
                local cjson = cjson
                local res, err = ipipc:query_free_api("202.103.026.255")
                if not res then
                    ngx.say(err)
                    return
                end
                ngx.say(cjson.encode(res))
            ';
        }

        location /api {
            content_by_lua '
                local ipipc = ipipc
                local cjson = cjson
                local res, err = ipipc:query_api("202.103.026.255")
                if not res then
                    ngx.say(err)
                    return
                end
                ngx.say(cjson.encode(res))
            ';
        }

        location /api_status {
            content_by_lua '
                local ipipc = ipipc
                local cjson = cjson
                local res, err = ipipc:api_status()
                # local res, err = ipipc:api_status("your token")
                if not res then
                    ngx.say(err)
                    return
                end
                ngx.say(cjson.encode(res))
            ';
        }

        error_log  logs/ip_error.log info;
    }

} ``

  • 上面定义的 /ip 位置的典型输出是

    {"country":"中国","city":"武汉","province":"湖北"}
  • 上面定义的 /free_api 位置的典型输出是

    {"place":"","country":"中国","city":"武汉","province":"湖北","carriers":"电信"}
  • 上面定义的 /api 位置的典型输出是

    {"carriers":"电信","longitude":"114.298572","city":"武汉","province":"湖北","china_area_code":"420100","place":"","country":"中国","nation_code":"CN","phone_code":"86","tz_name":"Asia\/Shanghai","continents_code":"AP","latitude":"30.584355","tz_utc":"UTC+8"}
  • 上面定义的 /api_status 位置的典型输出是

    {"ret":"ok","service":{"service_id":10,"expired":"2019-08-13"},"data":{"day":1,"hour":1,"limit":false}}

方法

new

语法:ipip:new(opts)

    local opts = {
        path = "/path/to/17monipdb.datx",
        token = "your token",
        timeout  = "2000",
    }
    ipipc = ipip:new(opts)
    
  • path:设置 17monipdb.datx (下载免费版本) 文件路径。

  • token:ipip.net 的令牌。

  • timeout:http 请求的超时时间。

query

语法:res, err = ipipc:query(ip)

    data, err = ipipc:query_free_api(ip)
    data, err = ipipc:query_api(ip)
    data, err = ipipc:query_file(ip)
    

api 状态

    data, err = ipipc:api_status()
    data, err = ipipc:api_status("9a8bc1a059db4a14b4feb0f38db38bbf4d5353ab1")

作者

Linsir <root@linsir.org>

许可证

该模块根据 2 条款 BSD 许可证发布。

版权所有 (c) 2017, Linsir <root@linsir.org>

保留所有权利。

在满足以下条件的情况下,允许以源代码和二进制形式重新分发和使用,无论是否修改:

  • 源代码的重新分发必须保留上述版权声明、此条件列表和以下免责声明。

  • 二进制形式的重新分发必须在随分发提供的文档和/或其他材料中复制上述版权声明、此条件列表和以下免责声明。

本软件由版权持有人和贡献者“按原样”提供,并且不提供任何明示或暗示的担保,包括但不限于适销性和特定用途适用性的暗示担保。在任何情况下,版权持有人或贡献者均不对任何直接、间接、附带、特殊、惩罚性或后果性损害(包括但不限于替代商品或服务的采购;使用、数据或利润损失;或业务中断)负责,无论这些损害是如何造成的以及基于何种责任理论,无论是合同、严格责任还是侵权行为(包括疏忽或其他原因),即使已告知可能发生此类损害。

作者

Linsir

许可证

2bsd

依赖项

pintsized/lua-resty-http,openresty >= 1.11.2.1

版本