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 》

`

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:locationApi("your sid", "your uid")
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

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

body返回参数详情

参数 类型 备注
country string 国家
city string 省会或直辖市(国内)
region string 地区或城市 (国内)
place string 学校或单位 (国内)
operator string 运营商字段
latitude string 纬度
longitude string 经度
timeZone string 时区一, 可能不存在
timeZoneCode string 时区码
administrativeAreaCode string 中国行政区划代码
internationalPhoneCode string 国际电话代码
countryTwoDigitCode string 国家二位代码
worldContinentCode string 世界大洲代码

返回结果参考:

{
    "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

《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 2

lua一天就能学会

7年前 评论

@mingyun 对啊,lua 就是一门轻量级小众语言

7年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!