ip2location-io-resty
用于 OpenResty 的 IP2Location.io 地理位置库
$ opm get ip2location/ip2location-io-resty
IP2Location.io OpenResty SDK
此 OpenResty 包使用户能够查询丰富的数据集,例如国家、地区、区、城市、纬度和经度、邮政编码、时区、ASN、ISP、域名、网络速度、IDD 代码、区号、气象站数据、MNC、MCC、手机品牌、海拔高度、使用类型、地址类型、广告类别和代理数据以及 IP 地址。它支持 IPv4 和 IPv6 地址查找。
此包需要 API 密钥才能运行。您可以在 https://www.ip2location.io/pricing 上注册免费的 API 密钥。
安装
opm get ip2location/ip2location-io-resty
使用示例
查找 IP 地址地理位置数据
worker_processes 1;
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
resolver 8.8.8.8 ipv6=off;
server {
listen 8080 reuseport;
location / {
default_type text/html;
content_by_lua_block {
local cjson = require("cjson")
local configuration = require("configuration")
local ipgeolocation = require("ipgeolocation")
local apikey = "YOUR_API_KEY"
local lang = "fr" -- leave as blank if not needed
configuration.api_key = apikey
local ip = "8.8.8.8"
local ipl = ipgeolocation:open(configuration)
local result = ipl:lookup(ip, lang)
ngx.say("version: " .. configuration.version)
ngx.say("ip: " .. result.ip)
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("asn: " .. result.asn)
ngx.say("as: " .. result.as)
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("district: " .. result.district)
ngx.say("ads_category: " .. result.ads_category)
ngx.say("ads_category_name: " .. result.ads_category_name)
ngx.say("is_proxy: " .. tostring(result.is_proxy))
-- 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.translation.lang ~= cjson.null then
ngx.say("continent => translation => lang: " .. result.continent.translation.lang)
ngx.say("continent => translation => value: " .. result.continent.translation.value)
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 => tld: " .. result.country.tld)
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)
if lang ~= "" and result.country.translation.lang ~= cjson.null then
ngx.say("country => translation => lang: " .. result.country.translation.lang)
ngx.say("country => translation => value: " .. result.country.translation.value)
end
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.translation.lang ~= cjson.null then
ngx.say("region => translation => lang: " .. result.region.translation.lang)
ngx.say("region => translation => value: " .. result.region.translation.value)
end
end
-- city addon
if result["city"] ~= nil then
ngx.say("city => name: " .. result.city.name)
if lang ~= "" and result.city.translation.lang ~= cjson.null then
ngx.say("city => translations => lang: " .. result.city.translation.lang)
ngx.say("city => translations => value: " .. result.city.translation.value)
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: " .. tostring(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
-- geotargeting addon
if result["geotargeting"] ~= nil then
ngx.say("geotargeting => metro: " .. result.geotargeting.metro)
end
-- proxy addon
if result["proxy"] ~= nil then
ngx.say("proxy => last_seen: " .. result.proxy.last_seen)
ngx.say("proxy => proxy_type: " .. result.proxy.proxy_type)
ngx.say("proxy => threat: " .. result.proxy.threat)
ngx.say("proxy => provider: " .. result.proxy.provider)
ngx.say("proxy => is_vpn: " .. tostring(result.proxy.is_vpn))
ngx.say("proxy => is_tor: " .. tostring(result.proxy.is_tor))
ngx.say("proxy => is_data_center: " .. tostring(result.proxy.is_data_center))
ngx.say("proxy => is_public_proxy: " .. tostring(result.proxy.is_public_proxy))
ngx.say("proxy => is_web_proxy: " .. tostring(result.proxy.is_web_proxy))
ngx.say("proxy => is_web_crawler: " .. tostring(result.proxy.is_web_crawler))
ngx.say("proxy => is_residential_proxy: " .. tostring(result.proxy.is_residential_proxy))
ngx.say("proxy => is_spammer: " .. tostring(result.proxy.is_spammer))
ngx.say("proxy => is_scanner: " .. tostring(result.proxy.is_scanner))
ngx.say("proxy => is_botnet: " .. tostring(result.proxy.is_botnet))
end
}
}
}
}
响应参数
IP 地理位置查找函数
| 参数 | 类型 | 描述 | |---|---|---| |ip|字符串|IP 地址。| |country_code|字符串|基于 ISO 3166 的两位国家代码。| |country_name|字符串|基于 ISO 3166 的国家名称。| |region_name|字符串|地区或州名称。| |city_name|字符串|城市名称。| |latitude|双精度|城市纬度。如果未知城市,则默认为首都纬度。| |longitude|双精度|城市经度。如果未知城市,则默认为首都经度。| |zip_code|字符串|邮政编码。| |time_zone|字符串|UTC 时区(支持夏令时)。| |asn|字符串|自治系统编号 (ASN)。| |as|字符串|自治系统 (AS) 名称。| |isp|字符串|互联网服务提供商或公司名称。| |domain|字符串|与 IP 地址范围关联的互联网域名。| |net_speed|字符串|互联网连接类型。DIAL =拨号,DSL =宽带/电缆/光纤/移动,COMP =公司/T1| |idd_code|字符串|从另一个国家/地区致电该城市的 IDD 前缀。| |area_code|字符串|分配给地理区域的长度可变的号码,用于城市之间的通话。| |weather_station_code|字符串|识别最近气象观测站的特殊代码。| |weather_station_name|字符串|最近气象观测站的名称。| |mcc|字符串|移动国家代码 (MCC),如 ITU E.212 中所定义,用于识别无线电话网络(特别是 GSM 和 UMTS 网络)中的移动台。| |mnc|字符串|移动网络代码 (MNC) 与移动国家代码 (MCC) 结合使用,以唯一标识移动电话运营商或运营商。| |mobile_brand|字符串|与移动运营商关联的商业品牌。| |elevation|整数|城市平均海拔高度(米)。| |usage_type|字符串|ISP 或公司的使用类型分类。| |address_type|字符串|在互联网协议版本 4 (IPv4) 和互联网协议版本 6 (IPv6) 中定义的 IP 地址类型。| |continent.name|字符串|大陆名称。| |continent.code|字符串|两位大陆代码。| |continent.hemisphere|数组|国家/地区所在半球。数据采用数组格式,第一项表示(北/南)半球,第二项表示(东/西)半球信息。| |continent.translation|对象|基于给定语言代码的翻译数据。| |district|字符串|区或县名称。| |country.name|字符串|基于 ISO 3166 的国家名称。| |country.alpha3_code|字符串|基于 ISO 3166 的三位国家代码。| |country.numeric_code|字符串|基于 ISO 3166 的三位国家数字代码。| |country.demonym|字符串|该国家的国民。| |country.flag|字符串|国家国旗图像的 URL。| |country.capital|字符串|该国的首都。| |country.total_area|整数|总面积(平方公里)。| |country.population|整数|该国人口。| |country.currency|对象|该国的货币。| |country.language|对象|该国的语言。| |country.tld|字符串|国家代码顶级域名。| |country.translation|对象|基于给定语言代码的翻译数据。| |region.name|字符串|地区或州名称。| |region.code|字符串|ISO3166-2 代码。| |region.translation|对象|基于给定语言代码的翻译数据。| |city.name|字符串|城市名称。| |city.translation|对象|基于给定语言代码的翻译数据。| |time_zone_info.olson|字符串|Olson 格式的时区。| |time_zone_info.current_time|字符串|ISO 8601 格式的当前时间。| |time_zone_info.gmt_offset|整数|以秒为单位的 GMT 偏移值。| |time_zone_info.is_dst|布尔值|指示时区值是否处于夏令时。| |time_zone_info.sunrise|字符串|日出时间。(当地时间 hh:mm 格式,例如 07:47)| |time_zone_info.sunset|字符串|日落时间。(当地时间 hh:mm 格式,例如 19:50)| |geotargeting.metro|字符串|基于邮政编码的大都市代码。| |ads_category|字符串|基于 IAB Tech Lab 内容分类法的域名类别代码。| |ads_category_name|字符串|基于 IAB Tech Lab 内容分类法的域名类别。这些类别包括广泛用于广告、互联网安全和过滤设备等服务的一级和二级(如果可用)类别。| |is_proxy|布尔值|是否为代理。| |proxy.last_seen|整数|代理上次可见的天数。| |proxy.proxy_type|字符串|代理类型。| |proxy.threat|字符串|报告的安全威胁。| |proxy.provider|字符串|如果可用,则为 VPN 提供商的名称。| |proxy.is_vpn|布尔值|匿名 VPN 服务。| |proxy.is_tor|布尔值|Tor 出口节点。| |proxy.is_data_center|布尔值|托管服务提供商、数据中心或内容交付网络。| |proxy.is_public_proxy|布尔值|公共代理。| |proxy.is_web_proxy|布尔值|Web 代理。| |proxy.is_web_crawler|布尔值|搜索引擎机器人。| |proxy.is_residential_proxy|布尔值|住宅代理。| |proxy.is_spammer|布尔值|电子邮件和论坛垃圾邮件发送者。| |proxy.is_scanner|布尔值|网络安全扫描程序。| |proxy.is_botnet|布尔值|恶意软件感染的设备。|
{
"ip": "8.8.8.8",
"country_code": "US",
"country_name": "United States of America",
"region_name": "California",
"city_name": "Mountain View",
"latitude": 37.405992,
"longitude": -122.078515,
"zip_code": "94043",
"time_zone": "-07:00",
"asn": "15169",
"as": "Google LLC",
"isp": "Google LLC",
"domain": "google.com",
"net_speed": "T1",
"idd_code": "1",
"area_code": "650",
"weather_station_code": "USCA0746",
"weather_station_name": "Mountain View",
"mcc": "-",
"mnc": "-",
"mobile_brand": "-",
"elevation": 32,
"usage_type": "DCH",
"address_type": "Anycast",
"continent": {
"name": "North America",
"code": "NA",
"hemisphere": [
"north",
"west"
],
"translation": {
"lang": "es",
"value": "Norteamérica"
}
},
"district": "Santa Clara County",
"country": {
"name": "United States of America",
"alpha3_code": "USA",
"numeric_code": 840,
"demonym": "Americans",
"flag": "https://cdn.ip2location.io/assets/img/flags/us.png",
"capital": "Washington, D.C.",
"total_area": 9826675,
"population": 331002651,
"currency": {
"code": "USD",
"name": "United States Dollar",
"symbol": "$"
},
"language": {
"code": "EN",
"name": "English"
},
"tld": "us",
"translation": {
"lang": "es",
"value": "Estados Unidos de América (los)"
}
},
"region": {
"name": "California",
"code": "US-CA",
"translation": {
"lang": "es",
"value": "California"
}
},
"city": {
"name": "Mountain View",
"translation": {
"lang": null,
"value": null
}
},
"time_zone_info": {
"olson": "America/Los_Angeles",
"current_time": "2023-09-03T18:21:13-07:00",
"gmt_offset": -25200,
"is_dst": true,
"sunrise": "06:41",
"sunset": "19:33"
},
"geotargeting": {
"metro": "807"
},
"ads_category": "IAB19-11",
"ads_category_name": "Data Centers",
"is_proxy": false,
"proxy": {
"last_seen": 3,
"proxy_type": "DCH",
"threat": "-",
"provider": "-",
"is_vpn": false,
"is_tor": false,
"is_data_center": true,
"is_public_proxy": false,
"is_web_proxy": false,
"is_web_crawler": false,
"is_residential_proxy": false,
"is_spammer": false,
"is_scanner": false,
"is_botnet": false
}
}
许可证
请参阅 LICENSE 文件。
作者
IP2Location.com
许可证
mit
依赖项
ledgetech/lua-resty-http,openresty >= 1.19.9.1
版本
-
用于 OpenResty 的 IP2Location.io 地理位置库 2023-09-12 06:45:46