-
Notifications
You must be signed in to change notification settings - Fork 19
/
ngx_http_ip2location_module.c
544 lines (447 loc) · 12.2 KB
/
ngx_http_ip2location_module.c
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
/*
* IP2Location Nginx module is distributed under MIT license
* Copyright (c) 2013-2024 IP2Location.com. support at ip2location dot com
*
* This module is free software; you can redistribute it and/or
* modify it under the terms of the MIT license
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <IP2Location.h>
typedef struct {
IP2Location *handler;
ngx_array_t *proxies;
ngx_flag_t proxy_recursive;
} ngx_http_ip2location_conf_t;
typedef struct {
ngx_str_t *name;
uintptr_t data;
} ngx_http_ip2location_var_t;
static ngx_int_t ngx_http_ip2location_add_variables(ngx_conf_t *cf);
static ngx_int_t ngx_http_ip2location_get_str_value(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_http_ip2location_get_float_value(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data);
static IP2LocationRecord *ngx_http_ip2location_get_records(ngx_http_request_t *r);
static void *ngx_http_ip2location_create_conf(ngx_conf_t *cf);
static char *ngx_http_ip2location_init_conf(ngx_conf_t *cf, void *conf);
static char *ngx_http_ip2location_database(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static char *ngx_http_ip2location_proxy(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static ngx_int_t ngx_http_ip2location_cidr_value(ngx_conf_t *cf, ngx_str_t *net, ngx_cidr_t *cidr);
static void ngx_http_ip2location_cleanup(void *data);
static IP2Location *ip2location_bin_handler;
static ngx_command_t ngx_http_ip2location_commands[] = {
{
ngx_string("ip2location_database"),
NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE12,
ngx_http_ip2location_database,
NGX_HTTP_MAIN_CONF_OFFSET,
0,
NULL
},
{
ngx_string("ip2location_proxy"),
NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
ngx_http_ip2location_proxy,
NGX_HTTP_MAIN_CONF_OFFSET,
0,
NULL
},
{
ngx_string("ip2location_proxy_recursive"),
NGX_HTTP_MAIN_CONF|NGX_CONF_FLAG,
ngx_conf_set_flag_slot,
NGX_HTTP_MAIN_CONF_OFFSET,
offsetof(ngx_http_ip2location_conf_t, proxy_recursive),
NULL
},
ngx_null_command
};
static ngx_http_module_t ngx_http_ip2location_module_ctx = {
ngx_http_ip2location_add_variables, /* preconfiguration */
NULL, /* postconfiguration */
ngx_http_ip2location_create_conf, /* create main configuration */
ngx_http_ip2location_init_conf, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
NULL, /* create location configuration */
NULL /* merge location configuration */
};
ngx_module_t ngx_http_ip2location_module = {
NGX_MODULE_V1,
&ngx_http_ip2location_module_ctx, /* module context */
ngx_http_ip2location_commands, /* module directives */
NGX_HTTP_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_http_variable_t
ngx_http_ip2location_vars[] = {
{
ngx_string("ip2location_country_short"), NULL,
ngx_http_ip2location_get_str_value,
offsetof(IP2LocationRecord, country_short),
0, 0
},
{
ngx_string("ip2location_country_long"), NULL,
ngx_http_ip2location_get_str_value,
offsetof(IP2LocationRecord, country_long),
0, 0
},
{
ngx_string("ip2location_region"), NULL,
ngx_http_ip2location_get_str_value,
offsetof(IP2LocationRecord, region),
0, 0
},
{
ngx_string("ip2location_city"), NULL,
ngx_http_ip2location_get_str_value,
offsetof(IP2LocationRecord, city),
0, 0
},
{
ngx_string("ip2location_isp"), NULL,
ngx_http_ip2location_get_str_value,
offsetof(IP2LocationRecord, isp),
0, 0
},
{
ngx_string("ip2location_latitude"), NULL,
ngx_http_ip2location_get_float_value,
offsetof(IP2LocationRecord, latitude),
0, 0
},
{
ngx_string("ip2location_longitude"), NULL,
ngx_http_ip2location_get_float_value,
offsetof(IP2LocationRecord, longitude),
0, 0
},
{
ngx_string("ip2location_domain"), NULL,
ngx_http_ip2location_get_str_value,
offsetof(IP2LocationRecord, domain),
0, 0
},
{
ngx_string("ip2location_zipcode"), NULL,
ngx_http_ip2location_get_str_value,
offsetof(IP2LocationRecord, zipcode),
0, 0
},
{
ngx_string("ip2location_timezone"), NULL,
ngx_http_ip2location_get_str_value,
offsetof(IP2LocationRecord, timezone),
0, 0
},
{
ngx_string("ip2location_netspeed"), NULL,
ngx_http_ip2location_get_str_value,
offsetof(IP2LocationRecord, netspeed),
0, 0
},
{
ngx_string("ip2location_iddcode"), NULL,
ngx_http_ip2location_get_str_value,
offsetof(IP2LocationRecord, iddcode),
0, 0
},
{
ngx_string("ip2location_areacode"), NULL,
ngx_http_ip2location_get_str_value,
offsetof(IP2LocationRecord, areacode),
0, 0
},
{
ngx_string("ip2location_weatherstationcode"), NULL,
ngx_http_ip2location_get_str_value,
offsetof(IP2LocationRecord, weatherstationcode),
0, 0
},
{
ngx_string("ip2location_weatherstationname"), NULL,
ngx_http_ip2location_get_str_value,
offsetof(IP2LocationRecord, weatherstationname),
0, 0
},
{
ngx_string("ip2location_mcc"), NULL,
ngx_http_ip2location_get_str_value,
offsetof(IP2LocationRecord, mcc),
0, 0
},
{
ngx_string("ip2location_mnc"), NULL,
ngx_http_ip2location_get_str_value,
offsetof(IP2LocationRecord, mnc),
0, 0
},
{
ngx_string("ip2location_mobilebrand"), NULL,
ngx_http_ip2location_get_str_value,
offsetof(IP2LocationRecord, mobilebrand),
0, 0
},
{
ngx_string("ip2location_elevation"), NULL,
ngx_http_ip2location_get_float_value,
offsetof(IP2LocationRecord, elevation),
0, 0
},
{
ngx_string("ip2location_usagetype"), NULL,
ngx_http_ip2location_get_str_value,
offsetof(IP2LocationRecord, usagetype),
0, 0
},
{
ngx_string("ip2location_addresstype"), NULL,
ngx_http_ip2location_get_str_value,
offsetof(IP2LocationRecord, address_type),
0, 0
},
{
ngx_string("ip2location_category"), NULL,
ngx_http_ip2location_get_str_value,
offsetof(IP2LocationRecord, category),
0, 0
},
{
ngx_string("ip2location_district"), NULL,
ngx_http_ip2location_get_str_value,
offsetof(IP2LocationRecord, district),
0, 0
},
{
ngx_string("ip2location_asn"), NULL,
ngx_http_ip2location_get_str_value,
offsetof(IP2LocationRecord, asn),
0, 0
},
{
ngx_string("ip2location_as"), NULL,
ngx_http_ip2location_get_str_value,
offsetof(IP2LocationRecord, as),
0, 0
},
ngx_http_null_variable
};
static ngx_int_t
ngx_http_ip2location_get_str_value(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data)
{
char *val;
size_t len;
IP2LocationRecord *record;
record = ngx_http_ip2location_get_records(r);
if (record == NULL) {
goto not_found;
}
val = *(char **) ((char *) record + data);
if (val == NULL) {
goto no_value;
}
len = ngx_strlen(val);
v->data = ngx_pnalloc(r->pool, len);
if (v->data == NULL) {
IP2Location_free_record(record);
return NGX_ERROR;
}
ngx_memcpy(v->data, val, len);
v->len = len;
v->valid = 1;
v->no_cacheable = 0;
v->not_found = 0;
IP2Location_free_record(record);
return NGX_OK;
no_value:
IP2Location_free_record(record);
not_found:
v->not_found = 1;
return NGX_OK;
}
static ngx_int_t
ngx_http_ip2location_get_float_value(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data)
{
float val;
IP2LocationRecord *record;
record = ngx_http_ip2location_get_records(r);
if (record == NULL) {
v->not_found = 1;
return NGX_OK;
}
v->data = ngx_pnalloc(r->pool, NGX_INT64_LEN + 5);
if (v->data == NULL) {
IP2Location_free_record(record);
return NGX_ERROR;
}
val = *(float *) ((char *) record + data);
v->len = ngx_sprintf(v->data, "%.4f", val) - v->data;
v->valid = 1;
v->no_cacheable = 0;
v->not_found = 0;
IP2Location_free_record(record);
return NGX_OK;
}
static IP2LocationRecord *
ngx_http_ip2location_get_records(ngx_http_request_t *r)
{
ngx_http_ip2location_conf_t *gcf;
gcf = ngx_http_get_module_main_conf(r, ngx_http_ip2location_module);
if (gcf->handler) {
ngx_addr_t addr;
u_char p[NGX_INET6_ADDRSTRLEN + 1];
size_t size;
#if defined(nginx_version) && nginx_version >= 1023000
ngx_table_elt_t *xfwd;
#else
ngx_array_t *xfwd;
#endif
addr.sockaddr = r->connection->sockaddr;
addr.socklen = r->connection->socklen;
#if defined(nginx_version) && nginx_version >= 1023000
xfwd = r->headers_in.x_forwarded_for;
if (xfwd != NULL && gcf->proxies != NULL) {
#else
xfwd = &r->headers_in.x_forwarded_for;
if (xfwd->nelts > 0 && gcf->proxies != NULL) {
#endif
(void) ngx_http_get_forwarded_addr(r, &addr, xfwd, NULL, gcf->proxies, gcf->proxy_recursive);
}
#if defined(nginx_version) && (nginx_version) >= 1005003
size = ngx_sock_ntop(addr.sockaddr, addr.socklen, p, NGX_INET6_ADDRSTRLEN, 0);
#else
size = ngx_sock_ntop(addr.sockaddr, p, NGX_INET6_ADDRSTRLEN, 0);
#endif
p[size] = '\0';
ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "IP address detected by IP2Location: %s", p);
return IP2Location_get_all(gcf->handler, (char *)p);
}
return NULL;
}
static ngx_int_t
ngx_http_ip2location_add_variables(ngx_conf_t *cf)
{
ngx_http_variable_t *var, *v;
for (v = ngx_http_ip2location_vars; v->name.len; v++) {
var = ngx_http_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 void *
ngx_http_ip2location_create_conf(ngx_conf_t *cf)
{
ngx_pool_cleanup_t *cln;
ngx_http_ip2location_conf_t *conf;
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_ip2location_conf_t));
if (conf == NULL) {
return NULL;
}
conf->proxy_recursive = NGX_CONF_UNSET;
cln = ngx_pool_cleanup_add(cf->pool, 0);
if (cln == NULL) {
return NULL;
}
cln->handler = ngx_http_ip2location_cleanup;
cln->data = conf;
return conf;
}
static char *
ngx_http_ip2location_init_conf(ngx_conf_t *cf, void *conf)
{
ngx_http_ip2location_conf_t *gcf = conf;
ngx_conf_init_value(gcf->proxy_recursive, 0);
return NGX_CONF_OK;
}
static char *
ngx_http_ip2location_database(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_http_ip2location_conf_t *gcf = conf;
ngx_str_t *value;
if (gcf->handler) {
return "Duplicated";
}
if (ip2location_bin_handler){
//close the bin if it's still opened
IP2Location_close(ip2location_bin_handler);
ip2location_bin_handler = NULL;
}
value = cf->args->elts;
if (value[1].len == 0) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "No IP2Location database specified.");
return NGX_CONF_ERROR;
}
// Open IP2Location BIN database
gcf->handler = IP2Location_open((char *) value[1].data);
ip2location_bin_handler = gcf->handler;
if (gcf->handler == NULL) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "Unable to open database file \"%s\".", value[1].data);
return NGX_CONF_ERROR;
}
IP2Location_open_mem(gcf->handler, IP2LOCATION_CACHE_MEMORY);
return NGX_CONF_OK;
}
static char *
ngx_http_ip2location_proxy(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_http_ip2location_conf_t *gcf = conf;
ngx_str_t *value;
ngx_cidr_t cidr, *c;
value = cf->args->elts;
if (ngx_http_ip2location_cidr_value(cf, &value[1], &cidr) != NGX_OK) {
return NGX_CONF_ERROR;
}
if (gcf->proxies == NULL) {
gcf->proxies = ngx_array_create(cf->pool, 4, sizeof(ngx_cidr_t));
if (gcf->proxies == NULL) {
return NGX_CONF_ERROR;
}
}
c = ngx_array_push(gcf->proxies);
if (c == NULL) {
return NGX_CONF_ERROR;
}
*c = cidr;
return NGX_CONF_OK;
}
static ngx_int_t
ngx_http_ip2location_cidr_value(ngx_conf_t *cf, ngx_str_t *net, ngx_cidr_t *cidr)
{
ngx_int_t rc;
if (ngx_strcmp(net->data, "255.255.255.255") == 0) {
cidr->family = AF_INET;
cidr->u.in.addr = 0xffffffff;
cidr->u.in.mask = 0xffffffff;
return NGX_OK;
}
rc = ngx_ptocidr(net, cidr);
if (rc == NGX_ERROR) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "Invalid network \"%V\"", net);
return NGX_ERROR;
}
if (rc == NGX_DONE) {
ngx_conf_log_error(NGX_LOG_WARN, cf, 0, "Low address bits of %V are meaningless", net);
}
return NGX_OK;
}
static void
ngx_http_ip2location_cleanup(void *data)
{
//ngx_http_ip2location_conf_t *gcf = data;
// if (gcf->handler) {
// IP2Location_close(gcf->handler);
// gcf->handler = NULL;
// }
}