-
Notifications
You must be signed in to change notification settings - Fork 13
/
ngx_http_qqwry_module.c
366 lines (299 loc) · 10.2 KB
/
ngx_http_qqwry_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
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
typedef struct {
void *data;
ngx_int_t index;
} ngx_http_qqwry_ctx_t;
typedef struct {
char *var1;
char *var2;
} ngx_http_qqwry_result_t;
static char *ngx_http_qqwry(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static ngx_int_t ngx_http_qqwry_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_http_qqwry_query(void *data, in_addr_t ip_addr, ngx_http_qqwry_result_t *ret);
static ngx_command_t ngx_http_qqwry_commands[] = {
{ ngx_string("qqwry"),
NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE23,
ngx_http_qqwry,
NGX_HTTP_MAIN_CONF_OFFSET,
0,
NULL },
ngx_null_command
};
static ngx_http_module_t ngx_http_qqwry_module_ctx = {
NULL, /* preconfiguration */
NULL, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
NULL, /* create location configuration */
NULL /* merge location configuration */
};
ngx_module_t ngx_http_qqwry_module = {
NGX_MODULE_V1,
&ngx_http_qqwry_module_ctx, /* module context */
ngx_http_qqwry_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 char *
ngx_http_qqwry(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_http_qqwry_ctx_t *qqwry;
ngx_http_variable_t *var;
ngx_str_t *value;
ngx_str_t name;
ngx_str_t path;
qqwry = ngx_palloc(cf->pool, sizeof(ngx_http_qqwry_ctx_t));
if (qqwry == NULL) {
return NGX_CONF_ERROR;
}
value = cf->args->elts;
name = value[1];
name.len--;
name.data++;
if (cf->args->nelts == 4) {
qqwry->index = ngx_http_get_variable_index(cf, &name);
if (qqwry->index == NGX_ERROR) {
return NGX_CONF_ERROR;
}
name = value[2];
name.len--;
name.data++;
path = value[3];
} else {
qqwry->index = -1;
path = value[2];
}
var = ngx_http_add_variable(cf, &name, NGX_HTTP_VAR_CHANGEABLE);
if (var == NULL) {
return NGX_CONF_ERROR;
}
var->get_handler = ngx_http_qqwry_variable;
var->data = (uintptr_t)qqwry;
//
FILE *fd;
size_t len;
void *buf;
if (!(fd = fopen((char *)path.data, "rb"))) {
return NGX_CONF_ERROR;
}
fseek(fd, 0, SEEK_END);
len = ftell(fd);
fseek(fd, 0, SEEK_SET);
buf = ngx_palloc(cf->pool, len);
if (buf == NULL) {
fclose(fd);
return NGX_CONF_ERROR;
}
if (len != fread(buf, 1, len, fd)) {
fclose(fd);
return NGX_CONF_ERROR;
}
fclose(fd);
qqwry->data = buf;
//
return NGX_CONF_OK;
}
static in_addr_t
ngx_http_qqwry_real_addr(ngx_http_request_t *r, ngx_http_qqwry_ctx_t *qqwry)
{
struct sockaddr_in *sin;
ngx_http_variable_value_t *v;
if (qqwry->index == -1) {
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http qqwry started: %V", &r->connection->addr_text);
if (r->connection->sockaddr->sa_family != AF_INET) {
return 0;
}
sin = (struct sockaddr_in *) r->connection->sockaddr;
return ntohl(sin->sin_addr.s_addr);
}
v = ngx_http_get_flushed_variable(r, qqwry->index);
if (v == NULL || v->not_found) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http qqwry not found");
return 0;
}
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http qqwry started: %v", v);
return ntohl(ngx_inet_addr(v->data, v->len));
}
static ngx_int_t
ngx_http_qqwry_is_public_addr(in_addr_t addr) {
static uint32_t proxies_mask[] = {0xff000000, 0xfff00000, 0xffff0000, 0x0f000000, 0xffffffff};
static uint32_t proxies_addr[] = {0x0A000000, 0xAC100000, 0xC0A80000, 0x0F000000, 0xFFFFFFFF};
ngx_int_t i;
for (i = 0; i < 5; i++) {
if ((addr & proxies_mask[i]) == proxies_addr[i]) {
return 0;
}
}
return 1;
}
static in_addr_t
ngx_http_qqwry_addr(ngx_http_request_t *r, ngx_http_qqwry_ctx_t *qqwry)
{
u_char *ip;
u_char *p;
u_char *pe;
size_t len;
in_addr_t addr;
in_addr_t addr_tmp;
ngx_table_elt_t *xfwd;
addr = ngx_http_qqwry_real_addr(r, qqwry);
//if (ngx_http_qqwry_is_public_addr(addr)) {
// return addr;
//}
xfwd = r->headers_in.x_forwarded_for;
if (xfwd == NULL) {
return addr;
}
for (pe = xfwd->value.data + xfwd->value.len; pe > xfwd->value.data && (*pe < '0' || *pe > '9'); pe--);
p = pe - 1;
while (p >= xfwd->value.data) {
if ((*p < '0' || *p > '9') && *p != '.') {
len = pe - p;
ip = p + 1;
} else if (p == xfwd->value.data) {
len = pe - p + 1;
ip = p;
} else {
p--;
continue;
}
addr_tmp = ntohl(ngx_inet_addr(ip, len));
if (ngx_http_qqwry_is_public_addr(addr_tmp)) {
return addr_tmp;
}
for (pe = p; pe > xfwd->value.data && (*pe < '0' || *pe > '9'); pe--);
p = pe - 1;
}
return addr;
}
static ngx_int_t
ngx_http_qqwry_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data)
{
ngx_http_qqwry_ctx_t *qqwry = (ngx_http_qqwry_ctx_t *)data;
in_addr_t ip = ngx_http_qqwry_addr(r, qqwry);
if (ip == 0) {
v->not_found = 1;
return NGX_ERROR;
}
ngx_http_qqwry_result_t result;
ngx_http_qqwry_query(qqwry->data, ip, &result);
ngx_int_t len = ngx_strlen(result.var1) + ngx_strlen(result.var2);
v->data = ngx_pnalloc(r->pool, len + 2);
if (v->data == NULL) {
return NGX_ERROR;
}
v->len = len + 1;
char *p = (char *)v->data;
p = stpcpy(p, result.var1);
*p = '|';
p++;
stpcpy(p, result.var2);
v->valid = 1;
v->no_cacheable = 0;
v->not_found = 0;
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "ngx_http_qqwry_variable: %v, ", v);
return NGX_OK;
}
#define qqwry_read_uint32(X) ((uint32_t)*(uint8_t *)(X) |\
(uint32_t)*((uint8_t *)(X)+1)<<8 |\
(uint32_t)*((uint8_t *)(X)+2)<<16 |\
(uint32_t)*((uint8_t *)(X)+3)<<24)
#define qqwry_read_uint24(X) ((uint32_t)*(uint8_t *)(X) |\
(uint32_t)*((uint8_t *)(X)+1)<<8 |\
(uint32_t)*((uint8_t *)(X)+2)<<16)
#define qqwry_read_uint8(X) (*(uint8_t *)(X))
#define qqwry_setpos(X,Y) ((uint8_t *)(X) + (Y))
static ngx_int_t
ngx_http_qqwry_query(void *data, in_addr_t ip, ngx_http_qqwry_result_t *result) {
uint8_t *p = data;
result->var1 = (char *)p;
uint32_t idx_first = qqwry_read_uint32(p);
uint32_t idx_last = qqwry_read_uint32(p + 4);
uint32_t idx_found = idx_last;
unsigned int h = (idx_last - idx_first) / 7;
unsigned int l = 0;
unsigned int m;
while (l <= h) {
m = (l + h) / 2;
p = qqwry_setpos(data, idx_first + m * 7);
if (ip < qqwry_read_uint32(p)) {
h = m - 1;
} else {
p = qqwry_setpos(data, qqwry_read_uint24(p + 4));
if (ip > qqwry_read_uint32(p)) {
l = m + 1;
} else {
/* found */
idx_found = idx_first + m * 7;
break;
}
}
}
uint32_t record_offset;
uint32_t country_offset;
char *country;
char *area;
p = qqwry_setpos(data, idx_found + 4);
record_offset = qqwry_read_uint24(p);
p = qqwry_setpos(data, record_offset + 4);
uint8_t flag;
switch (flag = qqwry_read_uint8(p)) {
case 0x01:
country_offset = qqwry_read_uint24(p + 1);
p = qqwry_setpos(data, country_offset);
switch (flag = qqwry_read_uint8(p)) {
case 0x02:
/* Country information redirected again */
p = qqwry_setpos(data, qqwry_read_uint24(p + 1));
country = (char *)(p);
p = qqwry_setpos(data, country_offset + 4);
break;
default:
country = (char *)(p);
p += strlen(country) + 1;
break;
}
break;
case 0x02:
p = qqwry_setpos(data, qqwry_read_uint24(p + 1));
country = (char *)(p);
/* Skip 4 bytes ip and 4 bytes country offset */
p = qqwry_setpos(data, record_offset + 8);
break;
default:
country = (char *)(p);
p += strlen(country) + 1;
break;
}
/* Read area information */
switch (flag = qqwry_read_uint8(p)) {
case 0x00:
area = NULL;
break;
case 0x01:
case 0x02:
p = qqwry_setpos(data, qqwry_read_uint24(p + 1));
area = (char *)(p);
break;
default:
area = (char *)(p);
break;
}
result->var1 = country;
result->var2 = area;
return NGX_OK;
}