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

《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 2

lua一天就能学会

7年前 评论

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

7年前 评论

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