ip2location-resty

OpenResty 的 IP2Location 地理位置库

$ opm get ip2location/ip2location-resty

IP2Location OpenResty 包

此 OpenResty 包使用 IP2Location 数据库,提供了一种快速查找国家、地区、城市、纬度、经度、邮政编码、时区、ISP、域名、连接类型、IDD 代码、区号、气象站代码、站名、MCC、MNC、移动品牌、海拔、使用类型、地址类型、IAB 类别、区、自治系统编号 (ASN) 和自治系统 (AS) 等信息的方案。此包使用基于文件的数据库,可在 IP2Location.com 获取。该数据库简单地包含 IP 块作为键,以及其他信息(如国家、地区、城市、纬度、经度、邮政编码、时区、ISP、域名、连接类型、IDD 代码、区号、气象站代码、站名、MCC、MNC、移动品牌、海拔、使用类型、地址类型、IAB 类别、区、自治系统编号 (ASN) 和自治系统 (AS))作为值。它支持 IPv4 和 IPv6 两种 IP 地址。

此包可用于多种类型的项目,例如

  • 选择地理位置最近的镜像

  • 分析 Web 服务器日志以确定访问者的国家/地区

  • 信用卡欺诈检测

  • 软件出口管制

  • 显示本地语言和货币

  • 防止密码共享和滥用服务

  • 广告中的地理定位

数据库将每月更新一次,以提高准确性。注册后,可在 https://lite.ip2location.com/ 获取免费的 LITE 数据库。

付费数据库可在 https://www.ip2location.com 的高级订阅套餐中获得。

作为替代方案,此包还可以调用 IP2Location Web 服务。这需要一个 API 密钥。如果您没有现有的 API 密钥,可以在以下位置订阅一个

https://www.ip2location.com/web-service/ip2location

安装

    opm get ip2location/ip2location-resty

使用 BIN 文件查询

依赖项

此包需要 IP2Location BIN 数据文件才能运行。您可以在以下位置下载 BIN 数据文件:

  • IP2Location LITE BIN 数据(免费):https://lite.ip2location.com

  • IP2Location 商业 BIN 数据(全面):https://www.ip2location.com

IPv4 BIN 与 IPv6 BIN

如果您只需要查询 IPv4 地址,请使用 IPv4 BIN 文件。

如果您需要查询 IPv4 和 IPv6 地址,请使用 IPv6 BIN 文件。

方法

以下是此包支持的方法。

|方法名称|描述| |---|---| |get_all|以对象的形式返回地理位置信息。| |get_country_short|返回国家代码。| |get_country_long|返回国家名称。| |get_region|返回地区名称。| |get_city|返回城市名称。| |get_isp|返回 ISP 名称。| |get_latitude|返回纬度。| |get_longitude|返回经度。| |get_domain|返回域名。| |get_zipcode|返回邮政编码。| |get_timezone|返回时区。| |get_netspeed|返回网速。| |get_iddcode|返回 IDD 代码。| |get_areacode|返回区号。| |get_weatherstationcode|返回气象站代码。| |get_weatherstationname|返回气象站名称。| |get_mcc|返回移动国家代码。| |get_mnc|返回移动网络代码。| |get_mobilebrand|返回移动品牌。| |get_elevation|返回以米为单位的海拔高度。| |get_usagetype|返回使用类型。| |get_addresstype|返回地址类型。| |get_category|返回 IAB 类别。| |get_district|返回区名称。| |get_asn|返回自治系统编号 (ASN)。| |get_as|返回自治系统 (AS)。| |close|关闭 BIN 文件并重置元数据。|

用法

    worker_processes  1;
    error_log logs/error.log;
    events {
        worker_connections 1024;
    }
    http {
        server {
            listen 8080 reuseport;
            location / {
                default_type text/html;
                content_by_lua_block {
                    local ip2location = require('ip2location')
                    local ip2loc = ip2location:new('/usr/local/ip2location/IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE-ADDRESSTYPE-CATEGORY-DISTRICT-ASN.BIN')
                    local result = ip2loc:get_all('8.8.8.8')
                    ngx.say("country_short: " .. result.country_short)
                    ngx.say("country_long: " .. result.country_long)
                    ngx.say("region: " .. result.region)
                    ngx.say("city: " .. result.city)
                    ngx.say("isp: " .. result.isp)
                    ngx.say("latitude: " .. result.latitude)
                    ngx.say("longitude: " .. result.longitude)
                    ngx.say("domain: " .. result.domain)
                    ngx.say("zipcode: " .. result.zipcode)
                    ngx.say("timezone: " .. result.timezone)
                    ngx.say("netspeed: " .. result.netspeed)
                    ngx.say("iddcode: " .. result.iddcode)
                    ngx.say("areacode: " .. result.areacode)
                    ngx.say("weatherstationcode: " .. result.weatherstationcode)
                    ngx.say("weatherstationname: " .. result.weatherstationname)
                    ngx.say("mcc: " .. result.mcc)
                    ngx.say("mnc: " .. result.mnc)
                    ngx.say("mobilebrand: " .. result.mobilebrand)
                    ngx.say("elevation: " .. result.elevation)
                    ngx.say("usagetype: " .. result.usagetype)
                    ngx.say("addresstype: " .. result.addresstype)
                    ngx.say("category: " .. result.category)
                    ngx.say("district: " .. result.district)
                    ngx.say("asn: " .. result.asn)
                    ngx.say("as: " .. result.as)
                    ip2loc:close()
                }
            }
        }
    }

使用 IP2Location Web 服务查询

方法

以下是此包支持的方法。

|方法名称|描述| |---|---| |open| 3 个输入参数:<ol><li>IP2Location API 密钥。</li><li>套餐 (WS1 - WS25)</li></li><li>使用 HTTPS 或 HTTP</li></ol> | |lookup|查询 IP 地址。此方法返回一个包含地理位置信息的物体。 <ul><li>country_code</li><li>country_name</li><li>region_name</li><li>city_name</li><li>latitude</li><li>longitude</li><li>zip_code</li><li>time_zone</li><li>isp</li><li>domain</li><li>net_speed</li><li>idd_code</li><li>area_code</li><li>weather_station_code</li><li>weather_station_name</li><li>mcc</li><li>mnc</li><li>mobile_brand</li><li>elevation</li><li>usage_type</li><li>address_type</li><li>category</li><li>continent<ul><li>name</li><li>code</li><li>hemisphere</li><li>translations</li></ul></li><li>country<ul><li>name</li><li>alpha3_code</li><li>numeric_code</li><li>demonym</li><li>flag</li><li>capital</li><li>total_area</li><li>population</li><li>currency<ul><li>code</li><li>name</li><li>symbol</li></ul></li><li>language<ul><li>code</li><li>name</li></ul></li><li>idd_code</li><li>tld</li><li>is_eu</li><li>translations</li></ul></li><li>region<ul><li>name</li><li>code</li><li>translations</li></ul></li><li>city<ul><li>name</li><li>translations</li></ul></li><li>geotargeting<ul><li>metro</li></ul></li><li>country_groupings</li><li>time_zone_info<ul><li>olson</li><li>current_time</li><li>gmt_offset</li><li>is_dst</li><li>sunrise</li><li>sunset</li></ul></li><ul>| |get_credit|此方法以对象的形式返回 Web 服务的信用余额。|

用法

    worker_processes  1;
    error_log logs/error.log;
    events {
        worker_connections 1024;
    }
    http {
        resolver 8.8.8.8;
        server {
            listen 8080 reuseport;
            location / {
                default_type text/html;
                content_by_lua_block {
                    local ip2locationwebservice = require('ip2locationwebservice')
                    local apikey = 'YOUR_API_KEY'
                    local apipackage = 'WS25'
                    local usessl = true
                    local lang = 'fr' -- leave blank if no need
                    local addon = 'continent,country,region,city,geotargeting,country_groupings,time_zone_info' -- leave blank if no need
    
                    local ip = '8.8.8.8'
                    local ws = ip2locationwebservice:open(apikey, apipackage, usessl)
                    local result = ws:lookup(ip, addon, lang)
    
                    if result["response"] == nil then
                        ngx.say("Error: Unknown error.")
                    elseif result.response == "OK" then
                        -- standard results
                        ngx.say("response: " .. result.response)
                        ngx.say("country_code: " .. result.country_code)
                        ngx.say("country_name: " .. result.country_name)
                        ngx.say("region_name: " .. result.region_name)
                        ngx.say("city_name: " .. result.city_name)
                        ngx.say("latitude: " .. result.latitude)
                        ngx.say("longitude: " .. result.longitude)
                        ngx.say("zip_code: " .. result.zip_code)
                        ngx.say("time_zone: " .. result.time_zone)
                        ngx.say("isp: " .. result.isp)
                        ngx.say("domain: " .. result.domain)
                        ngx.say("net_speed: " .. result.net_speed)
                        ngx.say("idd_code: " .. result.idd_code)
                        ngx.say("area_code: " .. result.area_code)
                        ngx.say("weather_station_code: " .. result.weather_station_code)
                        ngx.say("weather_station_name: " .. result.weather_station_name)
                        ngx.say("mcc: " .. result.mcc)
                        ngx.say("mnc: " .. result.mnc)
                        ngx.say("mobile_brand: " .. result.mobile_brand)
                        ngx.say("elevation: " .. result.elevation)
                        ngx.say("usage_type: " .. result.usage_type)
                        ngx.say("address_type: " .. result.address_type)
                        ngx.say("category: " .. result.category)
                        ngx.say("category_name: " .. result.category_name)
                        ngx.say("credits_consumed: " .. result.credits_consumed)
    
                        -- continent addon
                        if result["continent"] ~= nil then
                            ngx.say("continent => name: " .. result.continent.name)
                            ngx.say("continent => code: " .. result.continent.code)
                            ngx.say("continent => hemisphere: " .. table.concat(result.continent.hemisphere,","))
                            if lang ~= '' and result.continent.translations[lang] ~= nil then
                                ngx.say("continent => translations => " .. lang .. ": " .. result.continent.translations[lang])
                            end
                        end
    
                        -- country addon
                        if result["country"] ~= nil then
                            ngx.say("country => name: " .. result.country.name)
                            ngx.say("country => alpha3_code: " .. result.country.alpha3_code)
                            ngx.say("country => numeric_code: " .. result.country.numeric_code)
                            ngx.say("country => demonym: " .. result.country.demonym)
                            ngx.say("country => flag: " .. result.country.flag)
                            ngx.say("country => capital: " .. result.country.capital)
                            ngx.say("country => total_area: " .. result.country.total_area)
                            ngx.say("country => population: " .. result.country.population)
                            ngx.say("country => idd_code: " .. result.country.idd_code)
                            ngx.say("country => tld: " .. result.country.tld)
                            ngx.say("country => is_eu: " .. tostring(result.country.is_eu))
                            if lang ~= '' and result.country.translations[lang] ~= nil then
                                ngx.say("country => translations => " .. lang .. ": " .. result.country.translations[lang])
                            end
    
                            ngx.say("country => currency => code: " .. result.country.currency.code)
                            ngx.say("country => currency => name: " .. result.country.currency.name)
                            ngx.say("country => currency => symbol: " .. result.country.currency.symbol)
    
                            ngx.say("country => language => code: " .. result.country.language.code)
                            ngx.say("country => language => name: " .. result.country.language.name)
                        end
    
                        -- region addon
                        if result["region"] ~= nil then
                            ngx.say("region => name: " .. result.region.name)
                            ngx.say("region => code: " .. result.region.code)
                            if lang ~= '' and result.region.translations[lang] ~= nil then
                                ngx.say("region => translations => " .. lang .. ": " .. result.region.translations[lang])
                            end
                        end
    
                        -- city addon
                        if result["city"] ~= nil then
                            ngx.say("city => name: " .. result.city.name)
                            if lang ~= '' and result.city.translations[lang] ~= nil then
                                ngx.say("city => translations => " .. lang .. ": " .. result.city.translations[lang])
                            end
                        end
    
                        -- geotargeting addon
                        if result["geotargeting"] ~= nil then
                            ngx.say("geotargeting => metro: " .. result.geotargeting.metro)
                        end
    
                        -- country_groupings addon
                        if result["country_groupings"] ~= nil then
                            for i,v in ipairs(result.country_groupings) do
                                ngx.say("country_groupings => #" .. i .. " => acronym: " .. result.country_groupings[i].acronym)
                                ngx.say("country_groupings => #" .. i .. " => name: " .. result.country_groupings[i].name)
                            end
                        end
    
                        -- time_zone_info addon
                        if result["time_zone_info"] ~= nil then
                            ngx.say("time_zone_info => olson: " .. result.time_zone_info.olson)
                            ngx.say("time_zone_info => current_time: " .. result.time_zone_info.current_time)
                            ngx.say("time_zone_info => gmt_offset: " .. result.time_zone_info.gmt_offset)
                            ngx.say("time_zone_info => is_dst: " .. result.time_zone_info.is_dst)
                            ngx.say("time_zone_info => sunrise: " .. result.time_zone_info.sunrise)
                            ngx.say("time_zone_info => sunset: " .. result.time_zone_info.sunset)
                        end
    
                        local result2 = ws:get_credit()
                        ngx.say("Credit Balance: " .. result2.response)
                    else
                        ngx.say("Error: " .. result.response)
                    end
                }
            }
        }
    }

作者

IP2Location.com

许可证

mit

依赖项

版本