-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathngx_stream_ipdb_module.c
More file actions
433 lines (326 loc) · 9.97 KB
/
ngx_stream_ipdb_module.c
File metadata and controls
433 lines (326 loc) · 9.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*
* Copyright (C) vislee
*/
#include "ngx_stream_ipdb_module.h"
#ifdef NGX_STREAM_IPDB_LUA
#include "ngx_stream_ipdb_lua.h"
#endif
#define NGX_IPDB_country_name 0
#define NGX_IPDB_region_name 1
#define NGX_IPDB_city_name 2
#define NGX_IPDB_owner_domain 3
#define NGX_IPDB_isp_domain 4
#define NGX_IPDB_latitude 5
#define NGX_IPDB_longitude 6
// all item
#define NGX_IPDB_raw 20
static ngx_int_t ngx_stream_ipdb_variable(ngx_stream_session_t *s,
ngx_stream_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_stream_ipdb_init(ngx_conf_t *cf);
static ngx_int_t ngx_stream_ipdb_add_variables(ngx_conf_t *cf);
static char *ngx_stream_ipdb_language(ngx_conf_t *cf, void *post, void *data);
static void *ngx_stream_ipdb_create_main_conf(ngx_conf_t *cf);
static void *ngx_stream_ipdb_create_srv_conf(ngx_conf_t *cf);
static char *ngx_stream_ipdb_merge_srv_conf(ngx_conf_t *cf, void *parent,
void *child);
static void ngx_stream_ipdb_cleanup(void *data);
static char *ngx_stream_ipdb_open(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
static ngx_conf_post_handler_pt ngx_stream_ipdb_language_p =
ngx_stream_ipdb_language;
static ngx_command_t ngx_stream_ipdb_commands[] = {
{ ngx_string("ipdb"),
NGX_STREAM_MAIN_CONF|NGX_CONF_TAKE1,
ngx_stream_ipdb_open,
NGX_STREAM_MAIN_CONF_OFFSET,
0,
NULL },
{ ngx_string("ipdb_language"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_stream_ipdb_srv_conf_t, lang),
&ngx_stream_ipdb_language_p },
ngx_null_command
};
static ngx_stream_module_t ngx_stream_ipdb_module_ctx = {
ngx_stream_ipdb_add_variables, /* preconfiguration */
ngx_stream_ipdb_init, /* postconfiguration */
ngx_stream_ipdb_create_main_conf, /* create main configuration */
NULL, /* init main configuration */
ngx_stream_ipdb_create_srv_conf, /* create server configuration */
ngx_stream_ipdb_merge_srv_conf /* merge server configuration */
};
ngx_module_t ngx_stream_ipdb_module = {
NGX_MODULE_V1,
&ngx_stream_ipdb_module_ctx, /* module context */
ngx_stream_ipdb_commands, /* module directives */
NGX_STREAM_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
static ngx_stream_variable_t ngx_stream_ipdb_vars[] = {
{ ngx_string("ipdb_country_name"), NULL,
ngx_stream_ipdb_variable,
NGX_IPDB_country_name, 0, 0 },
{ ngx_string("ipdb_region_name"), NULL,
ngx_stream_ipdb_variable,
NGX_IPDB_region_name, 0, 0 },
{ ngx_string("ipdb_city_name"), NULL,
ngx_stream_ipdb_variable,
NGX_IPDB_city_name, 0, 0 },
{ ngx_string("ipdb_isp_domain"), NULL,
ngx_stream_ipdb_variable,
NGX_IPDB_isp_domain, 0, 0 },
{ ngx_string("ipdb_raw"), NULL,
ngx_stream_ipdb_variable,
NGX_IPDB_raw, 0, 0 },
ngx_stream_null_variable
};
static ngx_int_t
ngx_stream_ipdb_add_variables(ngx_conf_t *cf)
{
ngx_stream_variable_t *var, *v;
for (v = ngx_stream_ipdb_vars; v->name.len; v++) {
var = ngx_stream_add_variable(cf, &v->name, v->flags);
if (var == NULL) {
return NGX_ERROR;
}
var->get_handler = v->get_handler;
var->data = v->data;
}
return NGX_OK;
}
static ngx_int_t
ngx_stream_ipdb_init(ngx_conf_t *cf)
{
#ifdef NGX_STREAM_IPDB_LUA
return ngx_stream_ipdb_lua_preload(cf);
#endif
return NGX_OK;
}
static void *
ngx_stream_ipdb_create_main_conf(ngx_conf_t *cf)
{
ngx_pool_cleanup_t *cln;
ngx_stream_ipdb_main_conf_t *conf;
conf = ngx_pcalloc(cf->pool, sizeof(ngx_stream_ipdb_main_conf_t));
if (conf == NULL) {
return NULL;
}
cln = ngx_pool_cleanup_add(cf->pool, 0);
if (cln == NULL) {
return NULL;
}
cln->handler = ngx_stream_ipdb_cleanup;
cln->data = conf;
return conf;
}
static void *
ngx_stream_ipdb_create_srv_conf(ngx_conf_t *cf)
{
ngx_stream_ipdb_srv_conf_t *conf;
conf = ngx_pcalloc(cf->pool, sizeof(ngx_stream_ipdb_srv_conf_t));
if (conf == NULL) {
return NULL;
}
return conf;
}
static char *
ngx_stream_ipdb_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
{
ngx_stream_ipdb_srv_conf_t *prev = parent;
ngx_stream_ipdb_srv_conf_t *conf = child;
ngx_conf_merge_str_value(conf->lang, prev->lang, "EN");
return NGX_CONF_OK;
}
static char *
ngx_stream_ipdb_open(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_int_t err;
ngx_stream_ipdb_main_conf_t *imcf = conf;
ngx_str_t *value;
if (imcf->ipdb) {
return "is duplicate";
}
value = cf->args->elts;
err = ipdb_reader_new((char *) value[1].data, &imcf->ipdb);
if (err || imcf->ipdb == NULL) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"ipdb_reader_new(\"%V\") failed", &value[1]);
return NGX_CONF_ERROR;
}
return NGX_CONF_OK;
}
static char *
ngx_stream_ipdb_language(ngx_conf_t *cf, void *post, void *data)
{
ngx_str_t *lang = data;
if (ngx_strcmp(lang->data, "EN") == 0
|| ngx_strcmp(lang->data, "CN") == 0) {
return NGX_CONF_OK;
}
return NGX_CONF_ERROR;
}
static char *
ngx_stream_ipdb_get_index_item(char *v, ngx_int_t idx)
{
char *p, *q;
ngx_int_t n = 0;
if (v == NULL) return NULL;
if (idx >= NGX_IPDB_raw) return v;
p = v;
q = p;
while (*p) {
if (*p == '\t') {
if (idx == n) {
*p = 0;
return q;
}
q = p + 1;
++n;
}
++p;
}
if (*p == 0 && idx == n) {
return q;
}
return NULL;
}
ngx_int_t
ngx_stream_ipdb_item_by_addr(ipdb_reader *reader, ngx_addr_t *addr,
const char *lang, char *body)
{
int node = 0;
ngx_int_t err, i;
off_t off = -1;
size_t p = 0, o = 0, s = 0, e = 0;
size_t len = reader->meta->fields_length;
const char *content;
for (i = 0; i < reader->meta->language_length; ++i) {
if (ngx_strcmp(lang, reader->meta->language[i].name) == 0) {
off = reader->meta->language[i].offset;
break;
}
}
if (off == -1) {
return ErrNoSupportLanguage;
}
if (addr->sockaddr->sa_family == AF_INET) {
if (!ipdb_reader_is_ipv4_support(reader)) {
return ErrNoSupportIPv4;
}
struct sockaddr_in *sin;
sin = (struct sockaddr_in *) addr->sockaddr;
// sin->sin_addr.s_addr
err = ipdb_search(reader, (const u_char *)&sin->sin_addr.s_addr, 32, &node);
if (err != ErrNoErr) {
return err;
}
}
#if (NGX_HAVE_INET6)
else if (addr->sockaddr->sa_family == AF_INET6) {
if (!ipdb_reader_is_ipv6_support(reader)) {
return ErrNoSupportIPv6;
}
struct in6_addr *inaddr6;
inaddr6 = &((struct sockaddr_in6 *) addr->sockaddr)->sin6_addr;
err = ipdb_search(reader, (const u_char *)&inaddr6->s6_addr, 128, &node);
if (err != ErrNoErr) {
return err;
}
}
#endif
else {
return ErrIPFormat;
}
err = ipdb_resolve(reader, node, &content);
if (err) {
return err;
}
while (*(content + p)) {
if (*(content + p) == '\t') {
++o;
}
if ((!e) && o == off + len) {
e = p;
}
++p;
if (off && (!s) && (off_t)o == off) {
s = p;
}
}
if (!e) e = p;
if (off + len > o + 1) {
err = ErrDatabaseError;
} else {
strncpy(body, content + s, e - s);
body[e - s] = 0;
}
return err;
}
static ngx_int_t
ngx_stream_ipdb_variable(ngx_stream_session_t *s,
ngx_stream_variable_value_t *v, uintptr_t data)
{
char *p;
char body[512];
ngx_int_t err;
ngx_addr_t addr;
ngx_stream_ipdb_srv_conf_t *iscf;
ngx_stream_ipdb_main_conf_t *imcf;
#if (NGX_DEBUG)
ngx_str_t debug;
#endif
imcf = ngx_stream_get_module_main_conf(s, ngx_stream_ipdb_module);
if (imcf == NULL || imcf->ipdb == NULL) {
goto not_found;
}
iscf = ngx_stream_get_module_srv_conf(s, ngx_stream_ipdb_module);
addr.sockaddr = s->connection->sockaddr;
addr.socklen = s->connection->socklen;
err = ngx_stream_ipdb_item_by_addr(imcf->ipdb, &addr,
(const char *)iscf->lang.data, body);
if (err) {
ngx_log_debug1(NGX_LOG_DEBUG_STREAM, s->connection->log, 0,
"ngx_stream_ipdb_variable, ipdb find error: %d", err);
goto not_found;
}
#if (NGX_DEBUG)
debug.len = ngx_strlen(body);
debug.data = (u_char *)body;
ngx_log_debug1(NGX_LOG_DEBUG_STREAM, s->connection->log, 0,
"ngx_stream_ipdb_variable, item: \"%V\"", &debug);
#endif
p = ngx_stream_ipdb_get_index_item(body, data);
if (p == NULL) {
goto not_found;
}
v->len = ngx_strlen(p);
v->data = ngx_pnalloc(s->connection->pool, v->len);
if (v->data == NULL) {
return NGX_ERROR;
}
ngx_memcpy(v->data, p, v->len);
v->valid = 1;
v->no_cacheable = 0;
v->not_found = 0;
return NGX_OK;
not_found:
v->not_found = 1;
return NGX_OK;
}
static void
ngx_stream_ipdb_cleanup(void *data)
{
ngx_stream_ipdb_main_conf_t *imcf = data;
if (imcf->ipdb) {
ipdb_reader_free(&imcf->ipdb);
}
}