openship 0.4.8 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (112) hide show
  1. package/README.md +27 -1
  2. package/dist/bare-FSUAM5V5.js +23 -0
  3. package/dist/{bowser-O3AEA6AN.js → bowser-HNOTPGJO.js} +2 -2
  4. package/dist/{chunk-5MZ624VX.js → chunk-2GWYYHOR.js} +1 -1
  5. package/dist/chunk-3FYIXVX5.js +8051 -0
  6. package/dist/{chunk-ILFETZOM.js → chunk-3LDWWY7U.js} +21 -1
  7. package/dist/{chunk-W73X43R7.js → chunk-3NQQXMDR.js} +3 -3
  8. package/dist/{chunk-OWED2GRM.js → chunk-42WQABXM.js} +2 -2
  9. package/dist/{chunk-TYVME3XD.js → chunk-6FH4AXVY.js} +20 -3
  10. package/dist/{chunk-OXWDY22G.js → chunk-6ODUBKUB.js} +3 -3
  11. package/dist/{chunk-DYPH4HCI.js → chunk-AO252K7Z.js} +1 -1
  12. package/dist/{chunk-XPPZ7B2J.js → chunk-B3CE2OMR.js} +14 -12
  13. package/dist/{chunk-IXCHURVK.js → chunk-DOD2BXHQ.js} +4 -4
  14. package/dist/{chunk-VWPEKOH6.js → chunk-DRK3MEDS.js} +3 -3
  15. package/dist/{chunk-V6AN6XLZ.js → chunk-EMXI4UCU.js} +125 -47
  16. package/dist/{chunk-GKHUSZCD.js → chunk-EP2TNALB.js} +2 -2
  17. package/dist/{chunk-SLTYVHU7.js → chunk-GUQ3QCJ7.js} +942 -519
  18. package/dist/chunk-I3DPMTSF.js +29 -0
  19. package/dist/{chunk-L2VOGB2X.js → chunk-IW2UKBVF.js} +3 -3
  20. package/dist/{chunk-VDMQJHPI.js → chunk-IXM5245F.js} +228 -13
  21. package/dist/{chunk-MZ2WEHHY.js → chunk-JIMUYEKN.js} +4 -4
  22. package/dist/chunk-OGY63DGD.js +705 -0
  23. package/dist/{chunk-O6ZZF52V.js → chunk-ONKEYSMY.js} +2 -2
  24. package/dist/{chunk-6APULWSU.js → chunk-OTLSJYAC.js} +1744 -118
  25. package/dist/{chunk-EI575GHN.js → chunk-PDW4L3NV.js} +3 -3
  26. package/dist/{chunk-5NYUCONN.js → chunk-PSZSKEPA.js} +33 -51
  27. package/dist/{chunk-KI2EY3WB.js → chunk-PXKCZB2I.js} +1 -1
  28. package/dist/{chunk-FXTBHTWY.js → chunk-QEH4QUBX.js} +649 -85
  29. package/dist/{chunk-6APIPIJY.js → chunk-QWHBRY2K.js} +5 -5
  30. package/dist/{chunk-6B5YCFFV.js → chunk-RVER2EOO.js} +5 -5
  31. package/dist/{chunk-Q2CIRAOK.js → chunk-RXQBD4ZY.js} +1 -1
  32. package/dist/{chunk-TGIINPVM.js → chunk-TWJCLD4W.js} +60 -10
  33. package/dist/{chunk-4Z5QO7N6.js → chunk-VDDMADOF.js} +3 -3
  34. package/dist/{chunk-BBHX7T4R.js → chunk-VE2YOK4I.js} +991 -254
  35. package/dist/{chunk-2D3I5G6P.js → chunk-VZ2ZHPS2.js} +73 -43
  36. package/dist/{chunk-AVIB7CT7.js → chunk-WNWQBB7F.js} +2 -2
  37. package/dist/{chunk-FRNAR33O.js → chunk-XZYFZ6A5.js} +3 -3
  38. package/dist/{chunk-RPHRPFEH.js → chunk-ZCFVHA25.js} +1 -1
  39. package/dist/{chunk-FPRHYBY2.js → chunk-ZFJXPCQP.js} +1 -1
  40. package/dist/{chunk-JYGD5WSN.js → chunk-ZWLDLTJP.js} +1 -1
  41. package/dist/cloud-E5M6VMCG.js +31 -0
  42. package/dist/{cloud-7QYOGF7F.js → cloud-KRTZ2IL5.js} +3 -3
  43. package/dist/{detect-AZLOHUFX.js → detect-ALF4GXRX.js} +7 -8
  44. package/dist/{dist-IQJ6H6UZ.js → dist-BM6NPWIH.js} +3 -3
  45. package/dist/{dist-es-X3B5QYKB.js → dist-es-2YRDXMUU.js} +6 -6
  46. package/dist/{dist-es-OSNJBLXB.js → dist-es-CCVZEFRZ.js} +5 -5
  47. package/dist/{dist-es-VNKHQVIB.js → dist-es-IOH6G4GM.js} +4 -4
  48. package/dist/{dist-es-RYHIPOTN.js → dist-es-JGBPABJI.js} +14 -14
  49. package/dist/{dist-es-JSTDUS7Q.js → dist-es-KWSMDPIP.js} +6 -6
  50. package/dist/{dist-es-5DY7K32L.js → dist-es-UYDDOVZ3.js} +6 -6
  51. package/dist/{dist-es-WV4AMOII.js → dist-es-XUOR4NQN.js} +8 -8
  52. package/dist/docker-QECQJ5P4.js +32 -0
  53. package/dist/docker-edge-executor-62N64MGS.js +17 -0
  54. package/dist/edge-import-CQWHN2Q3.js +48 -0
  55. package/dist/ensure-container-edge-MJVXJQG5.js +32 -0
  56. package/dist/{event-streams-EHRAKL75.js → event-streams-TR4GAC53.js} +4 -4
  57. package/dist/{executor-XJ6RP4Q2.js → executor-JELARVKC.js} +8 -8
  58. package/dist/index.js +1209 -560
  59. package/dist/{loadSso-46FQRIZS.js → loadSso-UGF4JMRK.js} +10 -10
  60. package/dist/{local-executor-V7U5VOZW.js → local-executor-WOFZD72X.js} +4 -4
  61. package/dist/nginx-AXGNWRKZ.js +31 -0
  62. package/dist/node-entry.js +4 -0
  63. package/dist/{noop-FMMFG2EZ.js → noop-NJ72F46T.js} +3 -3
  64. package/dist/openresty-lua-IE4P5YQX.js +81 -0
  65. package/dist/server/index.js +116248 -94749
  66. package/dist/server/lua/geo_country.lua +31 -6
  67. package/dist/server/lua/maxminddb.lua +403 -0
  68. package/dist/server/lua/mgmt_api.lua +431 -68
  69. package/dist/server/lua/pipe_log.lua +10 -4
  70. package/dist/server/lua/site_logger.lua +220 -12
  71. package/dist/server/migrations/0073_project_volumes_object_storage.sql +3 -0
  72. package/dist/server/migrations/0074_project_compose_path.sql +1 -0
  73. package/dist/server/migrations/0075_service_command_argv.sql +1 -0
  74. package/dist/server/migrations/0076_rollback_retention.sql +17 -0
  75. package/dist/server/migrations/0077_domain_redirect.sql +13 -0
  76. package/dist/server/migrations/0078_grant_source_scope.sql +30 -0
  77. package/dist/server/migrations/0079_project_readiness.sql +18 -0
  78. package/dist/server/migrations/0080_deployment_error_classification.sql +26 -0
  79. package/dist/server/migrations/0081_project_readiness_repair.sql +34 -0
  80. package/dist/server/migrations/0082_edge_target_verification.sql +39 -0
  81. package/dist/server/migrations/0083_service_incident.sql +84 -0
  82. package/dist/server/migrations/0084_analytics_daily_rollup.sql +72 -0
  83. package/dist/server/migrations/0085_resource_usage.sql +68 -0
  84. package/dist/server/migrations/0086_remote_infra_updates.sql +41 -0
  85. package/dist/server/migrations/0087_server_container_version_columns.sql +17 -0
  86. package/dist/server/migrations/0088_audit_source_and_settings.sql +27 -0
  87. package/dist/server/migrations/0089_auto_scan_infra.sql +7 -0
  88. package/dist/server/migrations/0090_project_collect_paths.sql +18 -0
  89. package/dist/server/migrations/0091_project_server_id.sql +26 -0
  90. package/dist/server/migrations/0092_project_internal_alias.sql +13 -0
  91. package/dist/server/migrations/0093_service_incident_org_index.sql +13 -0
  92. package/dist/server/migrations/0094_backup_restore_meta.sql +15 -0
  93. package/dist/server/migrations/0095_backup_restore_cancel.sql +19 -0
  94. package/dist/server/migrations/0096_backup_policy_retention_defaults.sql +20 -0
  95. package/dist/server/migrations/0097_update_status_upstream_only.sql +35 -0
  96. package/dist/server/migrations/meta/_journal.json +175 -0
  97. package/dist/setup-FA4PW4NU.js +21 -0
  98. package/dist/{signin-ITOPKKUB.js → signin-3M3UPB6K.js} +10 -10
  99. package/dist/{sso-oidc-NON2PD7Y.js → sso-oidc-3VDIYOEJ.js} +10 -10
  100. package/dist/{sts-HHFFSAY2.js → sts-B3WIYLXR.js} +9 -9
  101. package/package.json +6 -3
  102. package/dist/bare-JY5Y5ELU.js +0 -23
  103. package/dist/chunk-SJIFLLX4.js +0 -2931
  104. package/dist/chunk-VFINNTUL.js +0 -362
  105. package/dist/cloud-KYWY3RP4.js +0 -30
  106. package/dist/docker-LODK5Y2S.js +0 -16
  107. package/dist/docker-edge-executor-VHW3JPJS.js +0 -13
  108. package/dist/edge-import-ABUDKKJI.js +0 -45
  109. package/dist/ensure-container-edge-SW6HENSG.js +0 -31
  110. package/dist/nginx-3L4SIBJA.js +0 -24
  111. package/dist/openresty-lua-4WLGDSER.js +0 -42
  112. package/dist/setup-NU6T2PDU.js +0 -20
@@ -4,25 +4,40 @@
4
4
  -- DB and cache are per-worker only: one load per worker, shared by all
5
5
  -- requests. No reload per stream or per request.
6
6
  --
7
- -- Dependencies (installed by deployLuaScripts):
8
- -- opm: anjia0532/lua-resty-maxminddb
9
- -- apt: libmaxminddb0 libmaxminddb-dev (C library for FFI)
10
- -- Database: /usr/share/GeoIP/GeoLite2-Country.mmdb
7
+ -- Dependencies:
8
+ -- Lua binding — `openship.maxminddb`, vendored beside this file. Falls back to
9
+ -- `resty.maxminddb` for a bare box that has the opm package
10
+ -- installed from before we vendored it.
11
+ -- C library — libmaxminddb.so (`apk add libmaxminddb` in apps/edge/Dockerfile;
12
+ -- installGeoDeps() for bare boxes)
13
+ -- Database — /usr/share/GeoIP/GeoLite2-Country.mmdb (baked into the edge
14
+ -- image from apps/api/assets/geoip, so no runtime download)
11
15
  --
12
16
  -- Falls back gracefully - if the library or database is missing,
13
17
  -- get_country_code() returns nil. Both site_logger.lua and pipe_log.lua
14
18
  -- wrap this module in pcall(require, ...) so a missing DB never crashes
15
- -- the request pipeline.
19
+ -- the request pipeline. mgmt_api `GET /status` reports whether geo actually
20
+ -- resolved, so a silently-dark edge is visible instead of just yielding an
21
+ -- empty country map.
16
22
 
17
23
  local _M = {}
18
24
 
19
25
  local DB_PATH = "/usr/share/GeoIP/GeoLite2-Country.mmdb"
20
26
 
21
- local mmdb_ok, maxmind = pcall(require, "resty.maxminddb")
27
+ -- Vendored copy first, opm-installed copy second. `ffi.load('libmaxminddb')`
28
+ -- runs at require time in both, so a missing .so surfaces here as mmdb_ok=false.
29
+ local mmdb_ok, maxmind = pcall(require, "openship.maxminddb")
30
+ if not mmdb_ok then
31
+ mmdb_ok, maxmind = pcall(require, "resty.maxminddb")
32
+ end
22
33
  local lrucache_ok, lrucache = pcall(require, "resty.lrucache")
23
34
 
24
35
  local cache -- one LRU per worker
25
36
  local geo_initted = false -- DB opened once per worker on first lookup
37
+ -- Why geo is off, for `GET /status`. Distinguishing "no FFI binding / no .so"
38
+ -- from "no database" matters: they need different fixes, and both used to
39
+ -- present identically as an empty country map.
40
+ local geo_reason = "not initialised"
26
41
 
27
42
  -- Init MaxMind once per worker; subsequent lookups (any stream) reuse.
28
43
  local function ensure_geo_init()
@@ -30,16 +45,19 @@ local function ensure_geo_init()
30
45
  return true
31
46
  end
32
47
  if not mmdb_ok or not maxmind then
48
+ geo_reason = "maxminddb FFI binding unavailable (libmaxminddb.so missing?)"
33
49
  return false
34
50
  end
35
51
  local ok, err = maxmind.init(DB_PATH)
36
52
  if not ok then
53
+ geo_reason = "database unreadable at " .. DB_PATH .. ": " .. (err or "unknown")
37
54
  ngx.log(ngx.WARN,
38
55
  "[geo_country] GeoLite2 database not found at " .. DB_PATH ..
39
56
  " - geo lookups disabled: " .. (err or "unknown"))
40
57
  return false
41
58
  end
42
59
  geo_initted = true
60
+ geo_reason = "ok"
43
61
  ngx.log(ngx.INFO, "[geo_country] GeoLite2-Country loaded from " .. DB_PATH)
44
62
  return true
45
63
  end
@@ -98,4 +116,11 @@ function _M.ensure_geo_init()
98
116
  return ensure_geo_init()
99
117
  end
100
118
 
119
+ --- Health for `GET /status`: does geo actually resolve, and if not, why.
120
+ -- @return boolean available, string reason, string db_path
121
+ function _M.status()
122
+ local ok = ensure_geo_init()
123
+ return ok, geo_reason, DB_PATH
124
+ end
125
+
101
126
  return _M
@@ -0,0 +1,403 @@
1
+ -- maxminddb.lua — VENDORED THIRD-PARTY. Do not edit; re-vendor instead.
2
+ --
3
+ -- Upstream: anjia0532/lua-resty-maxminddb v1.3.2 (opm). Loaded as
4
+ -- `openship.maxminddb` (see geo_country.lua). Verbatim except this header and ONE
5
+ -- marked change to the `ffi.load` call — search "OPENSHIP LOCAL MODIFICATION".
6
+ --
7
+ -- Why vendored rather than `opm get` at image build: `opm` needs perl, which the
8
+ -- openresty alpine base does not ship, and a build-time fetch would make the
9
+ -- image non-reproducible. Everything else in this directory is already shipped
10
+ -- by us (and base64-embedded into the JS bundle), so a pinned copy is the house
11
+ -- pattern — same reasoning as the vendored GeoLite2 DB in apps/api/assets/geoip.
12
+ --
13
+ -- Needs libmaxminddb.so at runtime (`apk add libmaxminddb`, in apps/edge/Dockerfile).
14
+ -- `ffi.load` below runs at REQUIRE time, so a missing .so throws on require —
15
+ -- geo_country.lua pcalls it for exactly that reason.
16
+ --
17
+ -- Re-vendor with:
18
+ -- curl -fsSL https://opm.openresty.org/api/pkg/tarball/anjia0532/lua-resty-maxminddb-<v>.opm.tar.gz \
19
+ -- | tar xz -O '*/lib/resty/maxminddb.lua' > packages/adapters/src/infra/lua/maxminddb.lua
20
+ -- # then re-apply this header and run `bun run embed:lua`
21
+ --[[
22
+ Copyright 2017-now anjia (anjia0532@gmail.com)
23
+
24
+ Licensed under the Apache License, Version 2.0 (the "License");
25
+ you may not use this file except in compliance with the License.
26
+ You may obtain a copy of the License at
27
+ http://www.apache.org/licenses/LICENSE-2.0
28
+ Unless required by applicable law or agreed to in writing, software
29
+ distributed under the License is distributed on an "AS IS" BASIS,
30
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31
+ See the License for the specific language governing permissions and
32
+ limitations under the License.
33
+ ]]
34
+
35
+ -- copy from https://github.com/lilien1010/lua-resty-maxminddb/blob/f96633e2428f8f7bcc1e2a7a28b747b33233a8db/resty/maxminddb.lua#L5-L12
36
+
37
+ local ffi = require ('ffi')
38
+ local ffi_new = ffi.new
39
+ local ffi_str = ffi.string
40
+ local ffi_cast = ffi.cast
41
+ local ffi_gc = ffi.gc
42
+ local C = ffi.C
43
+
44
+ local _M ={}
45
+ _M._VERSION = '1.3.2'
46
+ local mt = { __index = _M }
47
+
48
+ -- copy from https://github.com/lilien1010/lua-resty-maxminddb/blob/f96633e2428f8f7bcc1e2a7a28b747b33233a8db/resty/maxminddb.lua#L36-L126
49
+ ffi.cdef[[
50
+
51
+ typedef long int ssize_t;
52
+
53
+ typedef unsigned int mmdb_uint128_t __attribute__ ((__mode__(TI)));
54
+
55
+ typedef struct MMDB_entry_s {
56
+ struct MMDB_s *mmdb;
57
+ uint32_t offset;
58
+ } MMDB_entry_s;
59
+
60
+ typedef struct MMDB_lookup_result_s {
61
+ bool found_entry;
62
+ MMDB_entry_s entry;
63
+ uint16_t netmask;
64
+ } MMDB_lookup_result_s;
65
+
66
+ typedef struct MMDB_entry_data_s {
67
+ bool has_data;
68
+ union {
69
+ uint32_t pointer;
70
+ const char *utf8_string;
71
+ double double_value;
72
+ const uint8_t *bytes;
73
+ uint16_t uint16;
74
+ uint32_t uint32;
75
+ int32_t int32;
76
+ uint64_t uint64;
77
+ mmdb_uint128_t uint128;
78
+ bool boolean;
79
+ float float_value;
80
+ };
81
+
82
+ uint32_t offset;
83
+ uint32_t offset_to_next;
84
+ uint32_t data_size;
85
+ uint32_t type;
86
+ } MMDB_entry_data_s;
87
+
88
+ typedef struct MMDB_entry_data_list_s {
89
+ MMDB_entry_data_s entry_data;
90
+ struct MMDB_entry_data_list_s *next;
91
+ } MMDB_entry_data_list_s;
92
+
93
+ typedef struct MMDB_description_s {
94
+ const char *language;
95
+ const char *description;
96
+ } MMDB_description_s;
97
+
98
+ typedef struct MMDB_metadata_s {
99
+ uint32_t node_count;
100
+ uint16_t record_size;
101
+ uint16_t ip_version;
102
+ const char *database_type;
103
+ struct {
104
+ size_t count;
105
+ const char **names;
106
+ } languages;
107
+ uint16_t binary_format_major_version;
108
+ uint16_t binary_format_minor_version;
109
+ uint64_t build_epoch;
110
+ struct {
111
+ size_t count;
112
+ MMDB_description_s **descriptions;
113
+ } description;
114
+ } MMDB_metadata_s;
115
+
116
+ typedef struct MMDB_ipv4_start_node_s {
117
+ uint16_t netmask;
118
+ uint32_t node_value;
119
+ } MMDB_ipv4_start_node_s;
120
+
121
+ typedef struct MMDB_s {
122
+ uint32_t flags;
123
+ const char *filename;
124
+ ssize_t file_size;
125
+ const uint8_t *file_content;
126
+ const uint8_t *data_section;
127
+ uint32_t data_section_size;
128
+ const uint8_t *metadata_section;
129
+ uint32_t metadata_section_size;
130
+ uint16_t full_record_byte_size;
131
+ uint16_t depth;
132
+ MMDB_ipv4_start_node_s ipv4_start_node;
133
+ MMDB_metadata_s metadata;
134
+ } MMDB_s;
135
+
136
+ typedef char * pchar;
137
+
138
+ MMDB_lookup_result_s MMDB_lookup_string(MMDB_s *const mmdb, const char *const ipstr, int *const gai_error,int *const mmdb_error);
139
+ int MMDB_open(const char *const filename, uint32_t flags, MMDB_s *const mmdb);
140
+ int MMDB_aget_value(MMDB_entry_s *const start, MMDB_entry_data_s *const entry_data, const char *const *const path);
141
+ char *MMDB_strerror(int error_code);
142
+
143
+ int MMDB_get_entry_data_list(MMDB_entry_s *start, MMDB_entry_data_list_s **const entry_data_list);
144
+ void MMDB_free_entry_data_list(MMDB_entry_data_list_s *const entry_data_list);
145
+ void MMDB_close(MMDB_s *const mmdb);
146
+ const char *gai_strerror(int errcode);
147
+ ]]
148
+
149
+ -- error codes
150
+ -- https://github.com/maxmind/libmaxminddb/blob/master/include/maxminddb.h#L66
151
+ local MMDB_SUCCESS = 0
152
+ local MMDB_FILE_OPEN_ERROR = 1
153
+ local MMDB_CORRUPT_SEARCH_TREE_ERROR = 2
154
+ local MMDB_INVALID_METADATA_ERROR = 3
155
+ local MMDB_IO_ERROR = 4
156
+ local MMDB_OUT_OF_MEMORY_ERROR = 5
157
+ local MMDB_UNKNOWN_DATABASE_FORMAT_ERROR = 6
158
+ local MMDB_INVALID_DATA_ERROR = 7
159
+ local MMDB_INVALID_LOOKUP_PATH_ERROR = 8
160
+ local MMDB_LOOKUP_PATH_DOES_NOT_MATCH_DATA_ERROR = 9
161
+ local MMDB_INVALID_NODE_NUMBER_ERROR = 10
162
+ local MMDB_IPV6_LOOKUP_IN_IPV4_DATABASE_ERROR = 11
163
+
164
+ -- data type
165
+ -- https://github.com/maxmind/libmaxminddb/blob/master/include/maxminddb.h#L40
166
+ local MMDB_DATA_TYPE_EXTENDED = 0
167
+ local MMDB_DATA_TYPE_POINTER = 1
168
+ local MMDB_DATA_TYPE_UTF8_STRING = 2
169
+ local MMDB_DATA_TYPE_DOUBLE = 3
170
+ local MMDB_DATA_TYPE_BYTES = 4
171
+ local MMDB_DATA_TYPE_UINT16 = 5
172
+ local MMDB_DATA_TYPE_UINT32 = 6
173
+ local MMDB_DATA_TYPE_MAP = 7
174
+ local MMDB_DATA_TYPE_INT32 = 8
175
+ local MMDB_DATA_TYPE_UINT64 = 9
176
+ local MMDB_DATA_TYPE_UINT128 = 10
177
+ local MMDB_DATA_TYPE_ARRAY = 11
178
+ local MMDB_DATA_TYPE_CONTAINER = 12
179
+ local MMDB_DATA_TYPE_END_MARKER = 13
180
+ local MMDB_DATA_TYPE_BOOLEAN = 14
181
+ local MMDB_DATA_TYPE_FLOAT = 15
182
+
183
+ -- copy from https://github.com/lilien1010/lua-resty-maxminddb/blob/f96633e2428f8f7bcc1e2a7a28b747b33233a8db/resty/maxminddb.lua#L136-L138
184
+ -- you should install the libmaxminddb to your system
185
+ --
186
+ -- ── OPENSHIP LOCAL MODIFICATION (the only deviation from upstream) ───────────
187
+ -- Upstream is `ffi.load('libmaxminddb')`, which resolves the LINKER name
188
+ -- `libmaxminddb.so` — and that unversioned symlink ships in the -dev package, not
189
+ -- the runtime one. Alpine's `libmaxminddb` and Debian's `libmaxminddb0` both give
190
+ -- only `libmaxminddb.so.0`, so the bare name fails on a normal runtime install and
191
+ -- geo goes silently dark (geo_country pcalls this require). Try the soname too so
192
+ -- the runtime package alone is enough on every distro.
193
+ local maxm
194
+ do
195
+ local candidates = { 'libmaxminddb', 'libmaxminddb.so.0', 'libmaxminddb.so' }
196
+ local last_err
197
+ for i = 1, #candidates do
198
+ local ok, lib = pcall(ffi.load, candidates[i])
199
+ if ok then maxm = lib; break end
200
+ last_err = lib
201
+ end
202
+ if not maxm then
203
+ error('libmaxminddb not loadable (tried ' .. table.concat(candidates, ', ') ..
204
+ '): ' .. tostring(last_err), 2)
205
+ end
206
+ end
207
+ --https://github.com/maxmind/libmaxminddb
208
+ local mmdb = ffi_new('MMDB_s')
209
+ local initted = false
210
+
211
+ local function mmdb_strerror(rc)
212
+ return ffi_str(maxm.MMDB_strerror(rc))
213
+ end
214
+
215
+ local function gai_strerror(rc)
216
+ return ffi_str(C.gai_strerror(rc))
217
+ end
218
+
219
+ function _M.init(dbfile)
220
+ if not initted then
221
+ local maxmind_ready = maxm.MMDB_open(dbfile,0,mmdb)
222
+
223
+ if maxmind_ready ~= MMDB_SUCCESS then
224
+ return nil, mmdb_strerror(maxmind_ready)
225
+ end
226
+
227
+ initted = true
228
+
229
+ ffi_gc(mmdb, maxm.MMDB_close)
230
+ end
231
+ return initted
232
+ end
233
+
234
+ function _M.initted()
235
+ return initted
236
+ end
237
+
238
+ -- https://github.com/maxmind/libmaxminddb/blob/master/src/maxminddb.c#L1938
239
+ -- LOCAL MMDB_entry_data_list_s *dump_entry_data_list( FILE *stream, MMDB_entry_data_list_s *entry_data_list, int indent, int *status)
240
+ local function _dump_entry_data_list(entry_data_list,status)
241
+
242
+ if not entry_data_list then
243
+ return nil,MMDB_INVALID_DATA_ERROR
244
+ end
245
+
246
+ local entry_data_item = entry_data_list[0].entry_data
247
+ local data_type = entry_data_item.type
248
+ local data_size = entry_data_item.data_size
249
+ local result
250
+
251
+ if data_type == MMDB_DATA_TYPE_MAP then
252
+ result = {}
253
+
254
+ local size = entry_data_item.data_size
255
+
256
+ entry_data_list = entry_data_list[0].next
257
+
258
+ while(size > 0 and entry_data_list)
259
+ do
260
+ entry_data_item = entry_data_list[0].entry_data
261
+ data_type = entry_data_item.type
262
+ data_size = entry_data_item.data_size
263
+
264
+ if MMDB_DATA_TYPE_UTF8_STRING ~= data_type then
265
+ return nil,MMDB_INVALID_DATA_ERROR
266
+ end
267
+
268
+ local key = ffi_str(entry_data_item.utf8_string,data_size)
269
+
270
+ if not key then
271
+ return nil,MMDB_OUT_OF_MEMORY_ERROR
272
+ end
273
+
274
+ local val
275
+ entry_data_list = entry_data_list[0].next
276
+ entry_data_list,status,val = _dump_entry_data_list(entry_data_list)
277
+
278
+ if status ~= MMDB_SUCCESS then
279
+ return nil,status
280
+ end
281
+
282
+ result[key] = val
283
+
284
+ size = size -1
285
+ end
286
+
287
+
288
+ elseif entry_data_list[0].entry_data.type == MMDB_DATA_TYPE_ARRAY then
289
+ local size = entry_data_list[0].entry_data.data_size
290
+ result = {}
291
+
292
+ entry_data_list = entry_data_list[0].next
293
+
294
+ local i = 1
295
+ while(i <= size and entry_data_list)
296
+ do
297
+ local val
298
+ entry_data_list,status,val = _dump_entry_data_list(entry_data_list)
299
+
300
+ if status ~= MMDB_SUCCESS then
301
+ return nil,nil,val
302
+ end
303
+
304
+ result[i] = val
305
+ i = i + 1
306
+ end
307
+
308
+
309
+ else
310
+ entry_data_item = entry_data_list[0].entry_data
311
+ data_type = entry_data_item.type
312
+ data_size = entry_data_item.data_size
313
+
314
+ local val
315
+ -- string type "key":"val"
316
+ -- other type "key":val
317
+ -- default other type
318
+ if data_type == MMDB_DATA_TYPE_UTF8_STRING then
319
+ val = ffi_str(entry_data_item.utf8_string,data_size)
320
+ if not val then
321
+ status = MMDB_OUT_OF_MEMORY_ERROR
322
+ return nil,status
323
+ end
324
+ elseif data_type == MMDB_DATA_TYPE_BYTES then
325
+ val = ffi_str(ffi_cast('char * ',entry_data_item.bytes),data_size)
326
+ if not val then
327
+ status = MMDB_OUT_OF_MEMORY_ERROR
328
+ return nil,status
329
+ end
330
+ elseif data_type == MMDB_DATA_TYPE_DOUBLE then
331
+ val = entry_data_item.double_value
332
+ elseif data_type == MMDB_DATA_TYPE_FLOAT then
333
+ val = entry_data_item.float_value
334
+ elseif data_type == MMDB_DATA_TYPE_UINT16 then
335
+ val = entry_data_item.uint16
336
+ elseif data_type == MMDB_DATA_TYPE_UINT32 then
337
+ val = entry_data_item.uint32
338
+ elseif data_type == MMDB_DATA_TYPE_BOOLEAN then
339
+ val = entry_data_item.boolean
340
+ elseif data_type == MMDB_DATA_TYPE_UINT64 then
341
+ val = entry_data_item.uint64
342
+ elseif data_type == MMDB_DATA_TYPE_INT32 then
343
+ val = entry_data_item.int32
344
+ else
345
+ return nil,MMDB_INVALID_DATA_ERROR
346
+ end
347
+
348
+ result = val
349
+ entry_data_list = entry_data_list[0].next
350
+ end
351
+
352
+ status = MMDB_SUCCESS
353
+ return entry_data_list,status,result
354
+ end
355
+
356
+ function _M.lookup(ip)
357
+
358
+ if not initted then
359
+ return nil, "not initialized"
360
+ end
361
+
362
+ -- copy from https://github.com/lilien1010/lua-resty-maxminddb/blob/f96633e2428f8f7bcc1e2a7a28b747b33233a8db/resty/maxminddb.lua#L159-L176
363
+ local gai_error = ffi_new('int[1]')
364
+ local mmdb_error = ffi_new('int[1]')
365
+
366
+ local result = maxm.MMDB_lookup_string(mmdb,ip,gai_error,mmdb_error)
367
+
368
+ if mmdb_error[0] ~= MMDB_SUCCESS then
369
+ return nil,'lookup failed: ' .. mmdb_strerror(mmdb_error[0])
370
+ end
371
+
372
+ if gai_error[0] ~= MMDB_SUCCESS then
373
+ return nil,'lookup failed: ' .. gai_strerror(gai_error[0])
374
+ end
375
+
376
+ if true ~= result.found_entry then
377
+ return nil,'not found'
378
+ end
379
+
380
+ local entry_data_list = ffi_cast('MMDB_entry_data_list_s **const',ffi_new("MMDB_entry_data_list_s"))
381
+
382
+ local status = maxm.MMDB_get_entry_data_list(result.entry,entry_data_list)
383
+
384
+ if status ~= MMDB_SUCCESS then
385
+ return nil,'get entry data failed: ' .. mmdb_strerror(status)
386
+ end
387
+
388
+ local head = entry_data_list[0] -- Save so this can be passed to free fn.
389
+ local _,status,result = _dump_entry_data_list(entry_data_list)
390
+ maxm.MMDB_free_entry_data_list(head)
391
+
392
+ if status ~= MMDB_SUCCESS then
393
+ return nil,'dump entry data failed: ' .. mmdb_strerror(status)
394
+ end
395
+
396
+
397
+ return result
398
+ end
399
+
400
+ -- copy from https://github.com/lilien1010/lua-resty-maxminddb/blob/master/resty/maxminddb.lua#L208
401
+ -- https://www.maxmind.com/en/geoip2-databases you should download the mmdb file from maxmind
402
+
403
+ return _M;