openResty IP數據庫

openResty IP數據庫

ipip.net IP數據庫之openresty版,爲了方便nginx使用ip 數據所封裝的拓展包。nginx

使用方式

本腳本封裝了ipip.net的主要接口配製好後就能夠直接使用,並不須要再去複雜的內部實現。git

須要注意的是若是使用api訪問須要http拓展包。github

代碼我已提交至GitHub: https://github.com/icowan/lua-resty-17mon數據庫

若是對lua不瞭解請看我前段時間寫的: 《Lua基礎學習方式 (一天學會) 》json

若是還沒安裝openresty請看: 《優雅的安裝openresty 》api

`數據結構

nginx 配製

要使用首先得載入本身寫義的lualib,這個根據你安裝的或使用方式去修改路徑。app

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提供的腳本post

-- /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

參數 類型 備註
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

相關文章
相關標籤/搜索