openResty IP数据库

openResty IP数据库

ipip.net IP数据库之openresty版,为了方便nginx使用ip 数据所封装的拓展包。

使用方式

本脚本封装了ipip.net的主要接口配制好后就可以直接使用,并不需要再去复杂的内部实现。

需要注意的是如果使用api访问需要http拓展包。

代码我已提交至GitHub: https://github.com/icowan/lua-resty-17mon

如果对lua不了解请看我前段时间写的: 《Lua基础学习方式 (一天学会) 》

如果还没安装openresty请看: 《优雅的安装openresty 》

《openResty IP数据库》`

nginx 配制

要使用首先得载入自己写义的lualib,这个根据你安装的或使用方式去修改路径。

lua_package_path "/usr/local/openresty/lualib/?.lua;/var/www/lua-resty-17mon/lualib/?.lua;;";
lua_package_cpath "/usr/local/openresty/lualib/?.so;;";

error_log /var/www/lua-resty-17mon/logs/lua-resty-17mon.debug.log debug;

server {
    listen 8080;
    server_name localhost;
    charset utf-8;
    location /ipLocation {
        resolver 8.8.8.8; # 如果要使用api的话 需要dns 这可以改成中国的会快一些
        default_type "text/plain";
        content_by_lua_file "/var/www/lua-resty-17mon/script/ip_location.lua";
    }
}

lua 脚本使用

-- /var/www/lua-resty-17mon/script/ip_location.lua

ngx.req.read_body()
ngx.header.content_type = "application/json;charset=UTF-8"

local cjson = require "cjson"

local success = function(con)
    return cjson.encode({
        success = true,
        body = con
    })
end

local failure = function(err)
    return cjson.encode({
        success = false,
        errors = err
    })
end

-- 参数获取
local request_args = ngx.req.get_uri_args()
local ip_address = request_args['ip']

-- 如果不需要验证可以不用此拓展
local checkIp = require("ip_check"):new(ip_address)

-- 验证ip
local ok, err = checkIp:checkIp()
if not ok then
    ngx.say(failure(err))
    return
end

-- 使用本地数据库
local ipdetail, err = require("ip_location"):new(ip_address, "/var/www/lua-resty-17mon/file/17monipdb.dat")
if not ipdetail then
    ngx.log(ngx.ERR, err)
    ngx.say(failure(err))
    return
end

local ipLocation, err = ipdetail:location()
if not ipLocation then
    ngx.log(ngx.ERR, err)
    ngx.say(failure(err))
    return
end

ngx.say(success(ipLocation))

通过免费api获取ip信息

如果通过api获取数据需要使用http服务,这里需要使用lua-resty-http
这里我已经把它直接放到lualib/resty目录了,可以直接使用 感谢pintsized提供的脚本

-- /var/www/lua-resty-17mon/script/ip_location.lua

local ipdetail, err = require("ip_location"):new(ip_address)
if not ipdetail then
    ngx.log(ngx.ERR, err)
    ngx.say(failure(err))
    return
end

local ipLocation, err = ipdetail:locationApiFree()
if not ipLocation then
    ngx.log(ngx.ERR, err)
    ngx.say(failure(err))
    return
end

ngx.say(success(ipLocation))

通过付费api获取ip信息

-- /var/www/lua-resty-17mon/script/ip_location.lua

local ipdetail, err = require("ip_location"):new(ip_address, "", "your token")
if not ipdetail then
    ngx.log(ngx.ERR, err)
    ngx.say(failure(err))
    return
end

local ipLocation, err = ipdetail:locationApiFree()
if not ipLocation then
    ngx.log(ngx.ERR, err)
    ngx.say(failure(err))
    return
end

ngx.say(success(ipLocation))

获取aip使用状态

-- /var/www/lua-resty-17mon/script/ip_location.lua

local ipdetail, err = require("ip_location"):new(ip_address, "your token")
if not ipdetail then
    ngx.log(ngx.ERR, err)
    ngx.say(failure(err))
    return
end

local ipLocation, err = ipdetail:apiStatus()
if not ipLocation then
    ngx.log(ngx.ERR, err)
    ngx.say(failure(err))
    return
end

ngx.say(success(ipLocation))

返回数据结构

Response

返回类型: JSON

参数类型备注
successbool true or false
errors or bodystring当success为false时errors有值否则返回body

body返回参数详情

参数类型备注
countrystring国家
citystring省会或直辖市(国内)
regionstring地区或城市 (国内)
placestring学校或单位 (国内)
operatorstring运营商字段
latitudestring纬度
longitudestring经度
timeZonestring时区一, 可能不存在
timeZoneCodestring时区码
administrativeAreaCodestring中国行政区划代码
internationalPhoneCodestring国际电话代码
countryTwoDigitCodestring国家二位代码
worldContinentCodestring世界大洲代码

返回结果参考:

{
    "success": true,
    "body": {
        "country": "",  // 国家
        "city": "",  // 省会或直辖市(国内)
        "region": "",  // 地区或城市 (国内)
        "place": "",  // 学校或单位 (国内)
        "operator": "",  // 运营商字段(只有购买了带有运营商版本的数据库才会有)
        "latitude": "",  // 纬度     (每日版本提供)
        "longitude": "",  // 经度     (每日版本提供)
        "timeZone": "",  // 时区一, 可能不存在  (每日版本提供)
        "timeZoneCode": "",  // 时区码, 可能不存在  (每日版本提供)
        "administrativeAreaCode": "",  // 中国行政区划代码    (每日版本提供)
        "internationalPhoneCode": "",  // 国际电话代码        (每日版本提供)
        "countryTwoDigitCode": "",  // 国家二位代码        (每日版本提供)
        "worldContinentCode": ""  // 世界大洲代码        (每日版本提供)
    }
}

ERROR结果参考

{
    "success": false,
    "erros": "retun messages..."
}

查询状态结果参考

{
    "success": true,
    "body": {
        "limit": false, // 是否已受访问限制
       "hour": 99680,  // 一个小时内剩余次数
       "day": 999680,  // 24小时内剩余次数
    }
}

尾巴

难得为开源社区贡献一份力量,希望有同样需求的同学们可以不用再去写复杂的底层逻辑,应该更多的把时间估计业务流程上。

同样希望这个脚本能提供给ipip.net官方推荐脚本。

或访问我 网站: LatteCake

本文地址: http://lattecake.com/post/20102

    原文作者:拿铁味的摩卡蛋糕
    原文地址: https://segmentfault.com/a/1190000007246354
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞