geoip

LuaJIT 与 MaxMind GeoIP 的绑定

$ opm get leafo/geoip

LuaJIT 与 MaxMind 的 GeoIP 和 GeoIP2 (libmaxminddb) 库的绑定

  • https://github.com/maxmind/libmaxminddb

  • https://github.com/maxmind/geoip-api-c — 遗留库

为了使用此库,您需要 LuaJIT、您尝试使用的 GeoIP 库以及相应库的数据库文件。您应该能够在您的包管理器中找到这些文件。

我建议使用 libmaxminddb,因为旧版 GeoIP 数据库不再更新。

安装

    luarocks install luajit-geoip

参考

libmaxminddb

模块名为 geoip.mmdb

    local geoip = require "geoip.mmdb"

此模块在 OpenResty 中运行良好。您需要在模块级别保留对已加载 GeoIP 数据库对象的引用,以避免在每次请求时重新加载数据库。

<details>

<summary>查看 OpenResty 示例</summary>

为您的 GeoIP 数据库创建一个新的模块

geoip_helper.lua**

    local geoip = require "geoip.mmdb"
    
    return {
      country_db = assert(geoip.load_database("/var/lib/GeoIP/GeoLite2-Country.mmdb")),
      -- load more databases if necessary:
      -- asnum_db = ...
      -- etc.
    }

OpenResty 请求处理程序

    -- this module will be cached in `package.loaded`, and the databases will only be loaded on first access
    local result = require("geoip_helper").country_db.lookup_addr(ngx.var.remote_addr)
    if result then
      ngx.say("Your country:" .. result.country.iso_code)
    end

> 注意:如果您使用带有 x-forwarded-for 的代理,则需要调整 > 您访问用户 IP 地址的方式

</details>

db, err = load_database(file_name)

从文件路径加载数据库。如果数据库无法加载,则返回 nil 和错误消息。

数据库文件的位置因系统和数据库类型而异。在此示例中,我们将使用位于 /var/lib/GeoIP/GeoLite2-Country.mmdb 的国家/地区数据库。

    local mmdb = assert(geoip.load_database("/var/lib/GeoIP/GeoLite2-Country.mmdb"))

数据库对象具有以下方法

object, err = mmdb:lookup(address)

    local result = assert(mmdb:lookup("8.8.8.8"))
    
    -- print the country code 
    print(result.country.iso_code) --> US

查找地址(作为字符串),并将其所有数据作为 Lua 表返回。如果无法查找地址,或者该地址没有信息,则返回 nil 和错误。

> 注意:您可以查找 IPv4 和 IPv6 地址

输出的结构取决于使用的数据库。(如果需要快速检查,它与 mmdblookup 实用程序的输出结构匹配)

value, err = mmdb:lookup_value(address, ...)

    -- prints the country code
    print(assert(mmdb:lookup_value("8.8.8.8", "country", "iso_code"))) --> US

使用 varargs ... 中指定的路径查找地址的单个值。如果地址无效或在路径中未找到值,则返回 nil 和错误。此方法避免扫描整个对象以查找地址的条目,因此如果需要数据库中的特定值,则可能更有效。

geoip — 遗留

*此库的数据库不再更新,我强烈建议使用上面提到的 mmdb 功能*

模块名为 geoip

    local geoip = require "geoip"

GeoIP 支持许多不同类型的数据库。可用的查找数据库会自动从系统位置加载。

仅支持国家/地区和 ASNUM 数据库。欢迎创建请求以支持更多数据库。

res, err = lookup_addr(ip_address)

查找有关地址的信息。返回一个包含从所有可用数据库中提取的有关该地址的属性的表。

    local geoip = require "geoip"
    local res = geoip.lookup_addr("8.8.8.8")
    
    print(res.country_code)

返回值的结构如下所示

    {
      country_code = "US",
      country_name = "United States",
      asnum = "AS15169 Google Inc."
    }

控制数据库缓存

您可以通过手动实例化 GeoIP 对象并直接调用 load_databases 方法来控制数据库的加载方式。lookup_addr 仅在数据库尚未加载时自动加载数据库。

    local geoip = require("geoip")
    
    local gi = geoip.GeoIP()
    gi:load_databases("memory")
    
    local res = gi:lookup_addr("8.8.8.8")

> 默认情况下,使用 STANDARD 模式,该模式在每次查找时都会从磁盘读取

版本历史

  • 2.1 (2020年8月28日) — 修复解析 mmdb 中布尔值的错误 (#3) michaeljmartin

  • 2.0 (2020年4月6日) — 支持 mmdb (libmaxminddb),修复 geoip 中的内存泄漏

  • 1.0 (2018年4月4日) — 初始版本,支持 geoip

联系方式

许可证:MIT,版权所有 2020 作者:Leaf Corcoran (leafo) (@moonscript) 电子邮件:leafot@gmail.com 主页:<http://leafo.net>

POD 错误

您好!以上文档存在一些编码错误,解释如下:

大约在第 60 行

未终止的 B<...> 序列

作者

Leaf Corcoran (leafo)

许可证

mit

版本