├── .github └── workflows │ └── release.yml ├── .gitignore ├── LICENSE ├── README.md ├── dist.ini ├── lib └── resty │ └── maxminddb.lua └── rockspec ├── lua-resty-maxminddb-1.3.2-1.rockspec ├── lua-resty-maxminddb-1.3.3-6.rockspec └── lua-resty-maxminddb-1.3.4-1.rockspec /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: Release 2 | 3 | on: 4 | push: 5 | tags: 6 | - '*' 7 | workflow_dispatch: 8 | 9 | jobs: 10 | build: 11 | name: Build and Publish 12 | runs-on: ubuntu-latest 13 | 14 | steps: 15 | - name: Checkout code 16 | uses: actions/checkout@v3 17 | 18 | - name: Build and push opm 19 | run: | 20 | docker run --rm -t -v $(pwd):/tmp openresty/openresty:alpine-fat /bin/sh -c "echo -e 'github_account=${{ github.actor }}\ngithub_token=${{ secrets.OPM_GITHUB_TOKEN }}\nupload_server=https://opm.openresty.org\ndownload_server=https://opm.openresty.org' > ~/.opmrc && cd /tmp && opm build && opm upload" 21 | shell: bash 22 | 23 | - name: Build and push luarocks 24 | run: | 25 | rockspec=$(ls rockspec/ | grep rockspec | sort -k5,5 -r | head -n1) 26 | docker run --rm -t -v $(pwd):/tmp akorn/luarocks:lua5.1-alpine /bin/sh -c "apk --no-cache add alpine-sdk perl zip unzip && cd /tmp && luarocks install dkjson && luarocks upload rockspec/${rockspec} --api-key ${{ secrets.LUAROCKS_API_KEY }}" 27 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Lua sources 2 | luac.out 3 | 4 | # luarocks build files 5 | *.src.rock 6 | *.zip 7 | *.tar.gz 8 | 9 | # Object files 10 | *.o 11 | *.os 12 | *.ko 13 | *.obj 14 | *.elf 15 | 16 | # Precompiled Headers 17 | *.gch 18 | *.pch 19 | 20 | # Libraries 21 | *.lib 22 | *.a 23 | *.la 24 | *.lo 25 | *.def 26 | *.exp 27 | 28 | # Shared objects (inc. Windows DLLs) 29 | *.dll 30 | *.so 31 | *.so.* 32 | *.dylib 33 | 34 | # Executables 35 | *.exe 36 | *.out 37 | *.app 38 | *.i*86 39 | *.x86_64 40 | *.hex 41 | 42 | .idea/ 43 | 44 | .vscode/ 45 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2017-now anjia(anjia0532@gmail.com) 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Name 2 | --- 3 | lua-resty-maxminddb - A Lua library for reading [MaxMind's Geolocation database format](https://maxmind.github.io/MaxMind-DB/) (aka mmdb or geoip2). 4 | 5 | 6 | Prerequisites 7 | --- 8 | 9 | **Note** 10 | - [maxmind/libmaxminddb][] 11 | 12 | - [openresty][] 13 | 14 | - [GeoLite2 Free Downloadable Databases][linkGeolite2FreeDownloadableDatabases] 15 | 16 | - [maxmind/geoipupdate][] 17 | 18 | 19 | **Bug fixed** 20 | 21 | - [Error at lookup IP](https://github.com/anjia0532/lua-resty-maxminddb/issues/5) 22 | 23 | - [bad argument #1 to 'concat' (table expected, got nil)](https://github.com/anjia0532/lua-resty-maxminddb/issues/4) 24 | 25 | - [Memory leak](https://github.com/anjia0532/lua-resty-maxminddb/issues/6) 26 | 27 | - [Multiple subdivisions](https://github.com/anjia0532/lua-resty-maxminddb/issues/7) 28 | 29 | **Apology for infringement** 30 | - https://github.com/anjia0532/lua-resty-maxminddb/issues/25 31 | 32 | Installation 33 | --- 34 | ```bash 35 | 36 | # opm (manual install libmaxminddb and download GeoLite2-City.mmdb) 37 | # openresty/openresty:alpine and apache/apisix:2.13.0-alpine docker image need to install perl libmaxminddb 38 | # e.g. apk --no-cache add perl libmaxminddb && ln -s /usr/lib/libmaxminddb.so.0 /usr/lib/libmaxminddb.so 39 | opm get anjia0532/lua-resty-maxminddb 40 | 41 | # luarocks (manual download GeoLite2-City.mmdb) 42 | # openresty/openresty:alpine-fat docker image 43 | luarocks install lua-resty-maxminddb 44 | 45 | # openresty/openresty:alpine docker image need to install luarocks (ref https://github.com/openresty/docker-openresty/blob/master/alpine/Dockerfile.fat) 46 | 47 | # special apache/apisix:2.xx.0-alpine luarocks install lua-resty-maxminddb UNZIP=/usr/bin/unzip 48 | # e.g. apk --no-cache add perl alpine-sdk && luarocks install lua-resty-maxminddb UNZIP=/usr/bin/unzip 49 | ``` 50 | 51 | Synopsis 52 | --- 53 | 54 | ``` 55 | server { 56 | listen 80; 57 | server_name localhost; 58 | location / { 59 | content_by_lua_block{ 60 | local cjson = require 'cjson' 61 | local geo = require 'resty.maxminddb' 62 | if not geo.initted() then 63 | geo.init("/path/to/GeoLite2-City.mmdb") 64 | end 65 | local res,err = geo.lookup(ngx.var.arg_ip or ngx.var.remote_addr) --support ipv6 e.g. 2001:4860:0:1001::3004:ef68 66 | 67 | if not res then 68 | ngx.log(ngx.ERR,'failed to lookup by ip ,reason:',err) 69 | end 70 | ngx.say("full :",cjson.encode(res)) 71 | if ngx.var.arg_node then 72 | ngx.say("node name:",ngx.var.arg_node," ,value:", cjson.encode(res[ngx.var.arg_node] or {})) 73 | end 74 | } 75 | } 76 | } 77 | ``` 78 | 79 | ```bash 80 | #ipv4 81 | $ curl localhost/?ip=114.114.114.114&node=city 82 | 83 | #ipv6 84 | #$ curl localhost/?ip=2001:4860:0:1001::3004:ef68&node=country 85 | 86 | full :{"city":{"geoname_id":1799962,"names":{"en":"Nanjing","ru":"Нанкин","fr":"Nankin","pt-BR":"Nanquim","zh-CN":"南京","es":"Nankín","de":"Nanjing","ja":"南京市"}},"subdivisions":[{"geoname_id":1806260,"names":{"en":"Jiangsu","fr":"Province de Jiangsu","zh-CN":"江苏省"},"iso_code":"32"}],"country":{"geoname_id":1814991,"names":{"en":"China","ru":"Китай","fr":"Chine","pt-BR":"China","zh-CN":"中国","es":"China","de":"China","ja":"中国"},"iso_code":"CN"},"registered_country":{"geoname_id":1814991,"names":{"en":"China","ru":"Китай","fr":"Chine","pt-BR":"China","zh-CN":"中国","es":"China","de":"China","ja":"中国"},"iso_code":"CN"},"location":{"time_zone":"Asia\/Shanghai","longitude":118.7778,"accuracy_radius":50,"latitude":32.0617},"continent":{"geoname_id":6255147,"names":{"en":"Asia","ru":"Азия","fr":"Asie","pt-BR":"Ásia","zh-CN":"亚洲","es":"Asia","de":"Asien","ja":"アジア"},"code":"AS"}} 87 | node name:city ,value:{"geoname_id":1799962,"names":{"en":"Nanjing","ru":"Нанкин","fr":"Nankin","pt-BR":"Nanquim","zh-CN":"南京","es":"Nankín","de":"Nanjing","ja":"南京市"}} 88 | ``` 89 | 90 | prettify 91 | ```json 92 | full: { 93 | "city": { 94 | "geoname_id": 1799962, 95 | "names": { 96 | "en": "Nanjing", 97 | "ru": "Нанкин", 98 | "fr": "Nankin", 99 | "pt-BR": "Nanquim", 100 | "zh-CN": "南京", 101 | "es": "Nankín", 102 | "de": "Nanjing", 103 | "ja": "南京市" 104 | } 105 | }, 106 | "subdivisions": [{ 107 | "geoname_id": 1806260, 108 | "names": { 109 | "en": "Jiangsu", 110 | "fr": "Province de Jiangsu", 111 | "zh-CN": "江苏省" 112 | }, 113 | "iso_code": "32" 114 | } 115 | ], 116 | "country": { 117 | "geoname_id": 1814991, 118 | "names": { 119 | "en": "China", 120 | "ru": "Китай", 121 | "fr": "Chine", 122 | "pt-BR": "China", 123 | "zh-CN": "中国", 124 | "es": "China", 125 | "de": "China", 126 | "ja": "中国" 127 | }, 128 | "iso_code": "CN" 129 | }, 130 | "registered_country": { 131 | "geoname_id": 1814991, 132 | "names": { 133 | "en": "China", 134 | "ru": "Китай", 135 | "fr": "Chine", 136 | "pt-BR": "China", 137 | "zh-CN": "中国", 138 | "es": "China", 139 | "de": "China", 140 | "ja": "中国" 141 | }, 142 | "iso_code": "CN" 143 | }, 144 | "location": { 145 | "time_zone": "Asia\/Shanghai", 146 | "longitude": 118.7778, 147 | "accuracy_radius": 50, 148 | "latitude": 32.0617 149 | }, 150 | "continent": { 151 | "geoname_id": 6255147, 152 | "names": { 153 | "en": "Asia", 154 | "ru": "Азия", 155 | "fr": "Asie", 156 | "pt-BR": "Ásia", 157 | "zh-CN": "亚洲", 158 | "es": "Asia", 159 | "de": "Asien", 160 | "ja": "アジア" 161 | }, 162 | "code": "AS" 163 | } 164 | } 165 | node name: city, value: { 166 | "geoname_id": 1799962, 167 | "names": { 168 | "en": "Nanjing", 169 | "ru": "Нанкин", 170 | "fr": "Nankin", 171 | "pt-BR": "Nanquim", 172 | "zh-CN": "南京", 173 | "es": "Nankín", 174 | "de": "Nanjing", 175 | "ja": "南京市" 176 | } 177 | } 178 | 179 | ``` 180 | 181 | References 182 | --- 183 | 184 | - [GeoIP2 City and Country CSV Databases][linkGeoip2CityAndCountryCsvDatabases] 185 | - [lilien1010/lua-resty-maxminddb][] 186 | - [maxmind/libmaxminddb#source#lookup_and_print][] 187 | - [maxmind/libmaxminddb#source#dump_entry_data_list][] 188 | 189 | Bug Reports 190 | --- 191 | Please report bugs by filing an issue with our GitHub issue tracker at https://github.com/anjia0532/lua-resty-maxminddb/issues 192 | 193 | If the bug is casued by libmaxminddb tracker at https://github.com/maxmind/libmaxminddb/issues 194 | 195 | Copyright and License 196 | ===================== 197 | 198 | This module is licensed under the apache LICENSE-2.0 license. 199 | 200 | Copyright 2017-now anjia (anjia0532@gmail.com) 201 | 202 | Licensed under the Apache License, Version 2.0 (the "License"); 203 | 204 | you may not use this file except in compliance with the License. 205 | 206 | You may obtain a copy of the License at 207 | 208 | http://www.apache.org/licenses/LICENSE-2.0 209 | 210 | Unless required by applicable law or agreed to in writing, software 211 | 212 | distributed under the License is distributed on an "AS IS" BASIS, 213 | 214 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 215 | 216 | See the License for the specific language governing permissions and 217 | 218 | limitations under the License. 219 | 220 | [maxmind/libmaxminddb]: https://github.com/maxmind/libmaxminddb 221 | [openresty]: https://openresty.org/en/installation.html 222 | [linkGeolite2FreeDownloadableDatabases]: http://dev.maxmind.com/geoip/geoip2/geolite2/ 223 | [maxmind/geoipupdate]: https://github.com/maxmind/geoipupdate 224 | [linkGeoip2CityAndCountryCsvDatabases]: https://dev.maxmind.com/geoip/geoip2/geoip2-city-country-csv-databases/ 225 | [maxmind/libmaxminddb#source#lookup_and_print]: https://github.com/maxmind/libmaxminddb/blob/master/bin/mmdblookup.c#L262 226 | [maxmind/libmaxminddb#source#dump_entry_data_list]: https://github.com/maxmind/libmaxminddb/blob/master/src/maxminddb.c#L1938 227 | [lilien1010/lua-resty-maxminddb]: https://github.com/lilien1010/lua-resty-maxminddb 228 | -------------------------------------------------------------------------------- /dist.ini: -------------------------------------------------------------------------------- 1 | name = lua-resty-maxminddb 2 | abstract = A Lua library for reading MaxMind's Geolocation database 3 | author = AnJia(anjia0532@gmail.com) 4 | is_original = yes 5 | license = 2bsd 6 | lib_dir = lib 7 | doc_dir = lib 8 | repo_link = https://github.com/anjia0532/lua-resty-maxminddb 9 | main_module = lib/resty/maxminddb.lua -------------------------------------------------------------------------------- /lib/resty/maxminddb.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | Copyright 2017-now anjia (anjia0532@gmail.com) 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | Unless required by applicable law or agreed to in writing, software 9 | distributed under the License is distributed on an "AS IS" BASIS, 10 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | See the License for the specific language governing permissions and 12 | limitations under the License. 13 | ]] 14 | 15 | -- copy from https://github.com/lilien1010/lua-resty-maxminddb/blob/f96633e2428f8f7bcc1e2a7a28b747b33233a8db/resty/maxminddb.lua#L5-L12 16 | 17 | local ffi = require ('ffi') 18 | local ffi_new = ffi.new 19 | local ffi_str = ffi.string 20 | local ffi_cast = ffi.cast 21 | local ffi_gc = ffi.gc 22 | local C = ffi.C 23 | 24 | local tab_isarray = require ('table.isarray') 25 | local tab_nkeys = require ('table.nkeys') 26 | 27 | local _M ={} 28 | _M._VERSION = '1.3.4' 29 | local mt = { __index = _M } 30 | 31 | -- copy from https://github.com/lilien1010/lua-resty-maxminddb/blob/f96633e2428f8f7bcc1e2a7a28b747b33233a8db/resty/maxminddb.lua#L36-L126 32 | ffi.cdef[[ 33 | 34 | typedef long int ssize_t; 35 | 36 | typedef unsigned int mmdb_uint128_t __attribute__ ((__mode__(TI))); 37 | 38 | typedef struct MMDB_entry_s { 39 | struct MMDB_s *mmdb; 40 | uint32_t offset; 41 | } MMDB_entry_s; 42 | 43 | typedef struct MMDB_lookup_result_s { 44 | bool found_entry; 45 | MMDB_entry_s entry; 46 | uint16_t netmask; 47 | } MMDB_lookup_result_s; 48 | 49 | typedef struct MMDB_entry_data_s { 50 | bool has_data; 51 | union { 52 | uint32_t pointer; 53 | const char *utf8_string; 54 | double double_value; 55 | const uint8_t *bytes; 56 | uint16_t uint16; 57 | uint32_t uint32; 58 | int32_t int32; 59 | uint64_t uint64; 60 | mmdb_uint128_t uint128; 61 | bool boolean; 62 | float float_value; 63 | }; 64 | 65 | uint32_t offset; 66 | uint32_t offset_to_next; 67 | uint32_t data_size; 68 | uint32_t type; 69 | } MMDB_entry_data_s; 70 | 71 | typedef struct MMDB_entry_data_list_s { 72 | MMDB_entry_data_s entry_data; 73 | struct MMDB_entry_data_list_s *next; 74 | } MMDB_entry_data_list_s; 75 | 76 | typedef struct MMDB_description_s { 77 | const char *language; 78 | const char *description; 79 | } MMDB_description_s; 80 | 81 | typedef struct MMDB_metadata_s { 82 | uint32_t node_count; 83 | uint16_t record_size; 84 | uint16_t ip_version; 85 | const char *database_type; 86 | struct { 87 | size_t count; 88 | const char **names; 89 | } languages; 90 | uint16_t binary_format_major_version; 91 | uint16_t binary_format_minor_version; 92 | uint64_t build_epoch; 93 | struct { 94 | size_t count; 95 | MMDB_description_s **descriptions; 96 | } description; 97 | } MMDB_metadata_s; 98 | 99 | typedef struct MMDB_ipv4_start_node_s { 100 | uint16_t netmask; 101 | uint32_t node_value; 102 | } MMDB_ipv4_start_node_s; 103 | 104 | typedef struct MMDB_s { 105 | uint32_t flags; 106 | const char *filename; 107 | ssize_t file_size; 108 | const uint8_t *file_content; 109 | const uint8_t *data_section; 110 | uint32_t data_section_size; 111 | const uint8_t *metadata_section; 112 | uint32_t metadata_section_size; 113 | uint16_t full_record_byte_size; 114 | uint16_t depth; 115 | MMDB_ipv4_start_node_s ipv4_start_node; 116 | MMDB_metadata_s metadata; 117 | } MMDB_s; 118 | 119 | typedef char * pchar; 120 | 121 | MMDB_lookup_result_s MMDB_lookup_string(MMDB_s *const mmdb, const char *const ipstr, int *const gai_error,int *const mmdb_error); 122 | int MMDB_open(const char *const filename, uint32_t flags, MMDB_s *const mmdb); 123 | int MMDB_aget_value(MMDB_entry_s *const start, MMDB_entry_data_s *const entry_data, const char *const *const path); 124 | char *MMDB_strerror(int error_code); 125 | 126 | int MMDB_get_entry_data_list(MMDB_entry_s *start, MMDB_entry_data_list_s **const entry_data_list); 127 | void MMDB_free_entry_data_list(MMDB_entry_data_list_s *const entry_data_list); 128 | void MMDB_close(MMDB_s *const mmdb); 129 | const char *gai_strerror(int errcode); 130 | ]] 131 | 132 | -- error codes 133 | -- https://github.com/maxmind/libmaxminddb/blob/master/include/maxminddb.h#L66 134 | local MMDB_SUCCESS = 0 135 | local MMDB_FILE_OPEN_ERROR = 1 136 | local MMDB_CORRUPT_SEARCH_TREE_ERROR = 2 137 | local MMDB_INVALID_METADATA_ERROR = 3 138 | local MMDB_IO_ERROR = 4 139 | local MMDB_OUT_OF_MEMORY_ERROR = 5 140 | local MMDB_UNKNOWN_DATABASE_FORMAT_ERROR = 6 141 | local MMDB_INVALID_DATA_ERROR = 7 142 | local MMDB_INVALID_LOOKUP_PATH_ERROR = 8 143 | local MMDB_LOOKUP_PATH_DOES_NOT_MATCH_DATA_ERROR = 9 144 | local MMDB_INVALID_NODE_NUMBER_ERROR = 10 145 | local MMDB_IPV6_LOOKUP_IN_IPV4_DATABASE_ERROR = 11 146 | 147 | -- data type 148 | -- https://github.com/maxmind/libmaxminddb/blob/master/include/maxminddb.h#L40 149 | local MMDB_DATA_TYPE_EXTENDED = 0 150 | local MMDB_DATA_TYPE_POINTER = 1 151 | local MMDB_DATA_TYPE_UTF8_STRING = 2 152 | local MMDB_DATA_TYPE_DOUBLE = 3 153 | local MMDB_DATA_TYPE_BYTES = 4 154 | local MMDB_DATA_TYPE_UINT16 = 5 155 | local MMDB_DATA_TYPE_UINT32 = 6 156 | local MMDB_DATA_TYPE_MAP = 7 157 | local MMDB_DATA_TYPE_INT32 = 8 158 | local MMDB_DATA_TYPE_UINT64 = 9 159 | local MMDB_DATA_TYPE_UINT128 = 10 160 | local MMDB_DATA_TYPE_ARRAY = 11 161 | local MMDB_DATA_TYPE_CONTAINER = 12 162 | local MMDB_DATA_TYPE_END_MARKER = 13 163 | local MMDB_DATA_TYPE_BOOLEAN = 14 164 | local MMDB_DATA_TYPE_FLOAT = 15 165 | 166 | -- copy from https://github.com/lilien1010/lua-resty-maxminddb/blob/f96633e2428f8f7bcc1e2a7a28b747b33233a8db/resty/maxminddb.lua#L136-L138 167 | -- you should install the libmaxminddb to your system 168 | local maxm = ffi.load('libmaxminddb') 169 | --https://github.com/maxmind/libmaxminddb 170 | local mmdb = ffi_new('MMDB_s') 171 | local initted = false 172 | 173 | local function mmdb_strerror(rc) 174 | return ffi_str(maxm.MMDB_strerror(rc)) 175 | end 176 | 177 | local function gai_strerror(rc) 178 | return ffi_str(C.gai_strerror(rc)) 179 | end 180 | 181 | function _M.init(dbfile) 182 | if not initted then 183 | local maxmind_ready = maxm.MMDB_open(dbfile,0,mmdb) 184 | 185 | if maxmind_ready ~= MMDB_SUCCESS then 186 | return nil, mmdb_strerror(maxmind_ready) 187 | end 188 | 189 | initted = true 190 | 191 | ffi_gc(mmdb, maxm.MMDB_close) 192 | end 193 | return initted 194 | end 195 | 196 | function _M.initted() 197 | return initted 198 | end 199 | 200 | -- https://github.com/maxmind/libmaxminddb/blob/master/src/maxminddb.c#L1938 201 | -- LOCAL MMDB_entry_data_list_s *dump_entry_data_list( FILE *stream, MMDB_entry_data_list_s *entry_data_list, int indent, int *status) 202 | local function _dump_entry_data_list(entry_data_list,status) 203 | 204 | if not entry_data_list then 205 | return nil,MMDB_INVALID_DATA_ERROR 206 | end 207 | 208 | local entry_data_item = entry_data_list[0].entry_data 209 | local data_type = entry_data_item.type 210 | local data_size = entry_data_item.data_size 211 | local result 212 | 213 | if data_type == MMDB_DATA_TYPE_MAP then 214 | result = {} 215 | 216 | local size = entry_data_item.data_size 217 | 218 | entry_data_list = entry_data_list[0].next 219 | 220 | while(size > 0 and entry_data_list) 221 | do 222 | entry_data_item = entry_data_list[0].entry_data 223 | data_type = entry_data_item.type 224 | data_size = entry_data_item.data_size 225 | 226 | if MMDB_DATA_TYPE_UTF8_STRING ~= data_type then 227 | return nil,MMDB_INVALID_DATA_ERROR 228 | end 229 | 230 | local key = ffi_str(entry_data_item.utf8_string,data_size) 231 | 232 | if not key then 233 | return nil,MMDB_OUT_OF_MEMORY_ERROR 234 | end 235 | 236 | local val 237 | entry_data_list = entry_data_list[0].next 238 | entry_data_list,status,val = _dump_entry_data_list(entry_data_list) 239 | 240 | if status ~= MMDB_SUCCESS then 241 | return nil,status 242 | end 243 | 244 | result[key] = val 245 | 246 | size = size -1 247 | end 248 | 249 | 250 | elseif entry_data_list[0].entry_data.type == MMDB_DATA_TYPE_ARRAY then 251 | local size = entry_data_list[0].entry_data.data_size 252 | result = {} 253 | 254 | entry_data_list = entry_data_list[0].next 255 | 256 | local i = 1 257 | while(i <= size and entry_data_list) 258 | do 259 | local val 260 | entry_data_list,status,val = _dump_entry_data_list(entry_data_list) 261 | 262 | if status ~= MMDB_SUCCESS then 263 | return nil,nil,val 264 | end 265 | 266 | result[i] = val 267 | i = i + 1 268 | end 269 | 270 | 271 | else 272 | entry_data_item = entry_data_list[0].entry_data 273 | data_type = entry_data_item.type 274 | data_size = entry_data_item.data_size 275 | 276 | local val 277 | -- string type "key":"val" 278 | -- other type "key":val 279 | -- default other type 280 | if data_type == MMDB_DATA_TYPE_UTF8_STRING then 281 | val = ffi_str(entry_data_item.utf8_string,data_size) 282 | if not val then 283 | status = MMDB_OUT_OF_MEMORY_ERROR 284 | return nil,status 285 | end 286 | elseif data_type == MMDB_DATA_TYPE_BYTES then 287 | val = ffi_str(ffi_cast('char * ',entry_data_item.bytes),data_size) 288 | if not val then 289 | status = MMDB_OUT_OF_MEMORY_ERROR 290 | return nil,status 291 | end 292 | elseif data_type == MMDB_DATA_TYPE_DOUBLE then 293 | val = entry_data_item.double_value 294 | elseif data_type == MMDB_DATA_TYPE_FLOAT then 295 | val = entry_data_item.float_value 296 | elseif data_type == MMDB_DATA_TYPE_UINT16 then 297 | val = entry_data_item.uint16 298 | elseif data_type == MMDB_DATA_TYPE_UINT32 then 299 | val = entry_data_item.uint32 300 | elseif data_type == MMDB_DATA_TYPE_BOOLEAN then 301 | val = entry_data_item.boolean 302 | elseif data_type == MMDB_DATA_TYPE_UINT64 then 303 | val = entry_data_item.uint64 304 | elseif data_type == MMDB_DATA_TYPE_INT32 then 305 | val = entry_data_item.int32 306 | else 307 | return nil,MMDB_INVALID_DATA_ERROR 308 | end 309 | 310 | result = val 311 | entry_data_list = entry_data_list[0].next 312 | end 313 | 314 | status = MMDB_SUCCESS 315 | return entry_data_list,status,result 316 | end 317 | 318 | function _M.lookup(ip, lookup_path) 319 | 320 | if not initted then 321 | return nil, "not initialized" 322 | end 323 | 324 | -- copy from https://github.com/lilien1010/lua-resty-maxminddb/blob/f96633e2428f8f7bcc1e2a7a28b747b33233a8db/resty/maxminddb.lua#L159-L176 325 | local gai_error = ffi_new('int[1]') 326 | local mmdb_error = ffi_new('int[1]') 327 | 328 | local result = maxm.MMDB_lookup_string(mmdb,ip,gai_error,mmdb_error) 329 | 330 | if mmdb_error[0] ~= MMDB_SUCCESS then 331 | return nil,'lookup failed: ' .. mmdb_strerror(mmdb_error[0]) 332 | end 333 | 334 | if gai_error[0] ~= MMDB_SUCCESS then 335 | return nil,'lookup failed: ' .. gai_strerror(gai_error[0]) 336 | end 337 | 338 | if true ~= result.found_entry then 339 | return nil,'not found' 340 | end 341 | 342 | local entry_data_list = ffi_cast('MMDB_entry_data_list_s **const',ffi_new("MMDB_entry_data_list_s")) 343 | local status 344 | 345 | if lookup_path then 346 | if not tab_isarray(lookup_path) then 347 | return nil, 'lookup path expected array' 348 | end 349 | 350 | local lookup_path_len = tab_nkeys(lookup_path) 351 | local entry_data = ffi_cast('MMDB_entry_data_s *const', ffi_new("MMDB_entry_data_s")) 352 | local path = ffi_new('const char * [?]', lookup_path_len+1, lookup_path) -- +1 for null termination 353 | path[lookup_path_len] = ffi.NULL 354 | 355 | status = maxm.MMDB_aget_value(result.entry, entry_data, path) 356 | if status == MMDB_SUCCESS then 357 | if entry_data.offset == 0 then 358 | return nil, 'no data found at the lookup path' 359 | end 360 | local entry = ffi_new("MMDB_entry_s", {mmdb=mmdb, offset=entry_data.offset}) 361 | status = maxm.MMDB_get_entry_data_list(entry, entry_data_list) 362 | end 363 | 364 | else 365 | status = maxm.MMDB_get_entry_data_list(result.entry,entry_data_list) 366 | end 367 | 368 | if status ~= MMDB_SUCCESS then 369 | return nil,'get entry data failed: ' .. mmdb_strerror(status) 370 | end 371 | 372 | local head = entry_data_list[0] -- Save so this can be passed to free fn. 373 | local _,status,result = _dump_entry_data_list(entry_data_list) 374 | maxm.MMDB_free_entry_data_list(head) 375 | 376 | if status ~= MMDB_SUCCESS then 377 | return nil,'dump entry data failed: ' .. mmdb_strerror(status) 378 | end 379 | 380 | 381 | return result 382 | end 383 | 384 | -- copy from https://github.com/lilien1010/lua-resty-maxminddb/blob/master/resty/maxminddb.lua#L208 385 | -- https://www.maxmind.com/en/geoip2-databases you should download the mmdb file from maxmind 386 | 387 | return _M; 388 | -------------------------------------------------------------------------------- /rockspec/lua-resty-maxminddb-1.3.2-1.rockspec: -------------------------------------------------------------------------------- 1 | package = "lua-resty-maxminddb" 2 | version = "1.3.2-1" 3 | supported_platforms = {"linux", "macosx"} 4 | 5 | source = { 6 | url = "git://github.com/anjia0532/lua-resty-maxminddb", 7 | tag = "v1.3.2" 8 | } 9 | 10 | description = { 11 | summary = "About A Lua library for reading MaxMind's Geolocation database", 12 | detailed = [[ 13 | lua-resty-maxminddb - A Lua library for reading MaxMind's Geolocation database format (aka mmdb or geoip2). 14 | ]], 15 | homepage = "https://github.com/anjia0532/lua-resty-maxminddb", 16 | license = "Apache License 2.0" 17 | } 18 | dependencies = { 19 | "lua >= 5.1, < 5.2" 20 | -- If you depend on other rocks, add them here 21 | } 22 | build = { 23 | type = "command", 24 | build_command = [[ 25 | wget -O libmaxminddb-1.6.0.tar.gz \ 26 | https://github.com/maxmind/libmaxminddb/releases/download/1.6.0/libmaxminddb-1.6.0.tar.gz && \ 27 | tar zxf libmaxminddb-1.6.0.tar.gz && \ 28 | cd libmaxminddb-1.6.0 && \ 29 | ./configure && \ 30 | $(MAKE) -j && \ 31 | $(MAKE) check 32 | ]], 33 | install = { 34 | lua = { 35 | ["resty.maxminddb"] = "lib/resty/maxminddb.lua", 36 | }, 37 | lib = { 38 | ["libmaxminddb.so"] = "libmaxminddb-1.6.0/src/.libs/libmaxminddb.so.0.0.7", 39 | ["libmaxminddb.so.0 "] = "libmaxminddb-1.6.0/src/.libs/libmaxminddb.so.0.0.7", 40 | ["libmaxminddb.so.0.0.7"] = "libmaxminddb-1.6.0/src/.libs/libmaxminddb.so.0.0.7", 41 | }, 42 | }, 43 | install_command = "cd libmaxminddb-1.6.0 && $(MAKE) install" 44 | } 45 | -------------------------------------------------------------------------------- /rockspec/lua-resty-maxminddb-1.3.3-6.rockspec: -------------------------------------------------------------------------------- 1 | package = "lua-resty-maxminddb" 2 | version = "1.3.3-6" 3 | supported_platforms = {"linux", "macosx"} 4 | 5 | source = { 6 | url = "git://github.com/anjia0532/lua-resty-maxminddb", 7 | tag = "v1.3.3" 8 | } 9 | 10 | description = { 11 | summary = "About A Lua library for reading MaxMind's Geolocation database", 12 | detailed = [[ 13 | lua-resty-maxminddb - A Lua library for reading MaxMind's Geolocation database format (aka mmdb or geoip2). 14 | ]], 15 | homepage = "https://github.com/anjia0532/lua-resty-maxminddb", 16 | license = "Apache License 2.0" 17 | } 18 | dependencies = { 19 | "lua >= 5.1, < 5.2" 20 | -- If you depend on other rocks, add them here 21 | } 22 | build = { 23 | type = "command", 24 | build_command = [[ 25 | wget -O libmaxminddb-1.6.0.tar.gz \ 26 | https://github.com/maxmind/libmaxminddb/releases/download/1.6.0/libmaxminddb-1.6.0.tar.gz && \ 27 | tar zxf libmaxminddb-1.6.0.tar.gz && \ 28 | cd libmaxminddb-1.6.0 && \ 29 | ./configure && \ 30 | $(MAKE) -j && \ 31 | $(MAKE) check 32 | ]], 33 | install = { 34 | lua = { 35 | ["resty.maxminddb"] = "lib/resty/maxminddb.lua", 36 | }, 37 | lib = { 38 | ["libmaxminddb.so"] = "libmaxminddb-1.6.0/src/.libs/libmaxminddb.so.0.0.7", 39 | ["libmaxminddb.so.0 "] = "libmaxminddb-1.6.0/src/.libs/libmaxminddb.so.0.0.7", 40 | ["libmaxminddb.so.0.0.7"] = "libmaxminddb-1.6.0/src/.libs/libmaxminddb.so.0.0.7", 41 | }, 42 | }, 43 | install_command = "cd libmaxminddb-1.6.0 && $(MAKE) install" 44 | } 45 | -------------------------------------------------------------------------------- /rockspec/lua-resty-maxminddb-1.3.4-1.rockspec: -------------------------------------------------------------------------------- 1 | package = "lua-resty-maxminddb" 2 | version = "1.3.4-1" 3 | supported_platforms = {"linux", "macosx"} 4 | 5 | source = { 6 | url = "git://github.com/anjia0532/lua-resty-maxminddb", 7 | tag = "v1.3.4" 8 | } 9 | 10 | description = { 11 | summary = "About A Lua library for reading MaxMind's Geolocation database", 12 | detailed = [[ 13 | lua-resty-maxminddb - A Lua library for reading MaxMind's Geolocation database format (aka mmdb or geoip2). 14 | ]], 15 | homepage = "https://github.com/anjia0532/lua-resty-maxminddb", 16 | license = "Apache License 2.0" 17 | } 18 | dependencies = { 19 | "lua >= 5.1, < 5.2" 20 | -- If you depend on other rocks, add them here 21 | } 22 | build = { 23 | type = "command", 24 | build_command = [[ 25 | wget -O libmaxminddb-1.11.0.tar.gz \ 26 | https://github.com/maxmind/libmaxminddb/releases/download/1.11.0/libmaxminddb-1.11.0.tar.gz && \ 27 | tar zxf libmaxminddb-1.11.0.tar.gz && \ 28 | cd libmaxminddb-1.11.0 && \ 29 | ./configure && \ 30 | $(MAKE) -j && \ 31 | $(MAKE) check 32 | ]], 33 | install = { 34 | lua = { 35 | ["resty.maxminddb"] = "lib/resty/maxminddb.lua", 36 | }, 37 | lib = { 38 | ["libmaxminddb.so"] = "libmaxminddb-1.11.0/src/.libs/libmaxminddb.so.0.0.7", 39 | ["libmaxminddb.so.0 "] = "libmaxminddb-1.11.0/src/.libs/libmaxminddb.so.0.0.7", 40 | ["libmaxminddb.so.0.0.7"] = "libmaxminddb-1.11.0/src/.libs/libmaxminddb.so.0.0.7", 41 | }, 42 | }, 43 | install_command = "cd libmaxminddb-1.11.0 && $(MAKE) install" 44 | } 45 | --------------------------------------------------------------------------------