122
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9 #include <ngx_http.h>
|
|
10
|
|
11
|
420
|
12 #define NGX_HTTP_REALIP_XREALIP 0
|
|
13 #define NGX_HTTP_REALIP_XFWD 1
|
|
14 #define NGX_HTTP_REALIP_HEADER 2
|
|
15
|
|
16
|
122
|
17 typedef struct {
|
400
|
18 in_addr_t mask;
|
|
19 in_addr_t addr;
|
122
|
20 } ngx_http_realip_from_t;
|
|
21
|
|
22
|
|
23 typedef struct {
|
400
|
24 ngx_array_t *from; /* array of ngx_http_realip_from_t */
|
420
|
25 ngx_uint_t type;
|
|
26 ngx_uint_t hash;
|
|
27 ngx_str_t header;
|
552
|
28 #if (NGX_HAVE_UNIX_DOMAIN)
|
|
29 ngx_uint_t unixsock; /* unsigned unixsock:1; */
|
|
30 #endif
|
122
|
31 } ngx_http_realip_loc_conf_t;
|
|
32
|
|
33
|
400
|
34 typedef struct {
|
|
35 ngx_connection_t *connection;
|
552
|
36 struct sockaddr *sockaddr;
|
|
37 socklen_t socklen;
|
400
|
38 ngx_str_t addr_text;
|
|
39 } ngx_http_realip_ctx_t;
|
|
40
|
|
41
|
122
|
42 static ngx_int_t ngx_http_realip_handler(ngx_http_request_t *r);
|
552
|
43 static ngx_int_t ngx_http_realip_set_addr(ngx_http_request_t *r, u_char *ip,
|
|
44 size_t len);
|
400
|
45 static void ngx_http_realip_cleanup(void *data);
|
122
|
46 static char *ngx_http_realip_from(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
47 void *conf);
|
420
|
48 static char *ngx_http_realip(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
|
122
|
49 static void *ngx_http_realip_create_loc_conf(ngx_conf_t *cf);
|
|
50 static char *ngx_http_realip_merge_loc_conf(ngx_conf_t *cf,
|
|
51 void *parent, void *child);
|
230
|
52 static ngx_int_t ngx_http_realip_init(ngx_conf_t *cf);
|
122
|
53
|
|
54
|
|
55 static ngx_command_t ngx_http_realip_commands[] = {
|
|
56
|
|
57 { ngx_string("set_real_ip_from"),
|
|
58 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
59 ngx_http_realip_from,
|
|
60 NGX_HTTP_LOC_CONF_OFFSET,
|
|
61 0,
|
|
62 NULL },
|
|
63
|
|
64 { ngx_string("real_ip_header"),
|
|
65 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
420
|
66 ngx_http_realip,
|
122
|
67 NGX_HTTP_LOC_CONF_OFFSET,
|
420
|
68 0,
|
|
69 NULL },
|
122
|
70
|
|
71 ngx_null_command
|
|
72 };
|
|
73
|
|
74
|
|
75
|
216
|
76 static ngx_http_module_t ngx_http_realip_module_ctx = {
|
122
|
77 NULL, /* preconfiguration */
|
230
|
78 ngx_http_realip_init, /* postconfiguration */
|
122
|
79
|
|
80 NULL, /* create main configuration */
|
|
81 NULL, /* init main configuration */
|
|
82
|
|
83 NULL, /* create server configuration */
|
|
84 NULL, /* merge server configuration */
|
|
85
|
|
86 ngx_http_realip_create_loc_conf, /* create location configuration */
|
|
87 ngx_http_realip_merge_loc_conf /* merge location configuration */
|
|
88 };
|
|
89
|
|
90
|
|
91 ngx_module_t ngx_http_realip_module = {
|
|
92 NGX_MODULE_V1,
|
|
93 &ngx_http_realip_module_ctx, /* module context */
|
|
94 ngx_http_realip_commands, /* module directives */
|
|
95 NGX_HTTP_MODULE, /* module type */
|
|
96 NULL, /* init master */
|
230
|
97 NULL, /* init module */
|
122
|
98 NULL, /* init process */
|
|
99 NULL, /* init thread */
|
|
100 NULL, /* exit thread */
|
|
101 NULL, /* exit process */
|
|
102 NULL, /* exit master */
|
|
103 NGX_MODULE_V1_PADDING
|
|
104 };
|
|
105
|
|
106
|
|
107 static ngx_int_t
|
|
108 ngx_http_realip_handler(ngx_http_request_t *r)
|
|
109 {
|
|
110 u_char *ip, *p;
|
|
111 size_t len;
|
420
|
112 ngx_uint_t i, hash;
|
|
113 ngx_list_part_t *part;
|
|
114 ngx_table_elt_t *header;
|
122
|
115 struct sockaddr_in *sin;
|
400
|
116 ngx_connection_t *c;
|
|
117 ngx_http_realip_ctx_t *ctx;
|
122
|
118 ngx_http_realip_from_t *from;
|
|
119 ngx_http_realip_loc_conf_t *rlcf;
|
|
120
|
400
|
121 ctx = ngx_http_get_module_ctx(r, ngx_http_realip_module);
|
|
122
|
|
123 if (ctx) {
|
272
|
124 return NGX_DECLINED;
|
122
|
125 }
|
|
126
|
|
127 rlcf = ngx_http_get_module_loc_conf(r, ngx_http_realip_module);
|
|
128
|
|
129 if (rlcf->from == NULL) {
|
272
|
130 return NGX_DECLINED;
|
122
|
131 }
|
|
132
|
420
|
133 switch (rlcf->type) {
|
|
134
|
|
135 case NGX_HTTP_REALIP_XREALIP:
|
|
136
|
122
|
137 if (r->headers_in.x_real_ip == NULL) {
|
272
|
138 return NGX_DECLINED;
|
122
|
139 }
|
|
140
|
|
141 len = r->headers_in.x_real_ip->value.len;
|
|
142 ip = r->headers_in.x_real_ip->value.data;
|
|
143
|
420
|
144 break;
|
|
145
|
|
146 case NGX_HTTP_REALIP_XFWD:
|
|
147
|
122
|
148 if (r->headers_in.x_forwarded_for == NULL) {
|
272
|
149 return NGX_DECLINED;
|
122
|
150 }
|
|
151
|
|
152 len = r->headers_in.x_forwarded_for->value.len;
|
|
153 ip = r->headers_in.x_forwarded_for->value.data;
|
|
154
|
286
|
155 for (p = ip + len - 1; p > ip; p--) {
|
122
|
156 if (*p == ' ' || *p == ',') {
|
286
|
157 p++;
|
|
158 len -= p - ip;
|
|
159 ip = p;
|
|
160 break;
|
122
|
161 }
|
|
162 }
|
420
|
163
|
|
164 break;
|
|
165
|
|
166 default: /* NGX_HTTP_REALIP_HEADER */
|
|
167
|
|
168 part = &r->headers_in.headers.part;
|
|
169 header = part->elts;
|
|
170
|
|
171 hash = rlcf->hash;
|
|
172 len = rlcf->header.len;
|
|
173 p = rlcf->header.data;
|
|
174
|
|
175 for (i = 0; /* void */ ; i++) {
|
|
176
|
|
177 if (i >= part->nelts) {
|
|
178 if (part->next == NULL) {
|
|
179 break;
|
|
180 }
|
|
181
|
|
182 part = part->next;
|
|
183 header = part->elts;
|
|
184 i = 0;
|
|
185 }
|
|
186
|
|
187 if (hash == header[i].hash
|
|
188 && len == header[i].key.len
|
|
189 && ngx_strncmp(p, header[i].lowcase_key, len) == 0)
|
|
190 {
|
|
191 len = header[i].value.len;
|
|
192 ip = header[i].value.data;
|
|
193
|
|
194 goto found;
|
|
195 }
|
|
196 }
|
|
197
|
|
198 return NGX_DECLINED;
|
122
|
199 }
|
|
200
|
420
|
201 found:
|
|
202
|
400
|
203 c = r->connection;
|
|
204
|
|
205 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "realip: \"%s\"", ip);
|
286
|
206
|
122
|
207 /* AF_INET only */
|
|
208
|
552
|
209 if (c->sockaddr->sa_family == AF_INET) {
|
|
210 sin = (struct sockaddr_in *) c->sockaddr;
|
|
211
|
|
212 from = rlcf->from->elts;
|
|
213 for (i = 0; i < rlcf->from->nelts; i++) {
|
|
214
|
|
215 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
216 "realip: %08XD %08XD %08XD",
|
|
217 sin->sin_addr.s_addr, from[i].mask, from[i].addr);
|
|
218
|
|
219 if ((sin->sin_addr.s_addr & from[i].mask) == from[i].addr) {
|
|
220 return ngx_http_realip_set_addr(r, ip, len);
|
|
221 }
|
|
222 }
|
|
223 }
|
|
224
|
|
225 #if (NGX_HAVE_UNIX_DOMAIN)
|
|
226
|
|
227 if (c->sockaddr->sa_family == AF_UNIX && rlcf->unixsock) {
|
|
228 return ngx_http_realip_set_addr(r, ip, len);
|
457
|
229 }
|
|
230
|
552
|
231 #endif
|
122
|
232
|
552
|
233 return NGX_DECLINED;
|
|
234 }
|
122
|
235
|
|
236
|
552
|
237 static ngx_int_t
|
|
238 ngx_http_realip_set_addr(ngx_http_request_t *r, u_char *ip, size_t len)
|
|
239 {
|
|
240 u_char *p;
|
|
241 ngx_int_t rc;
|
|
242 ngx_addr_t addr;
|
|
243 ngx_connection_t *c;
|
|
244 ngx_pool_cleanup_t *cln;
|
|
245 ngx_http_realip_ctx_t *ctx;
|
286
|
246
|
552
|
247 cln = ngx_pool_cleanup_add(r->pool, sizeof(ngx_http_realip_ctx_t));
|
|
248 if (cln == NULL) {
|
|
249 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
250 }
|
286
|
251
|
552
|
252 ctx = cln->data;
|
|
253 ngx_http_set_ctx(r, ctx, ngx_http_realip_module);
|
|
254
|
|
255 c = r->connection;
|
|
256
|
|
257 rc = ngx_parse_addr(c->pool, &addr, ip, len);
|
122
|
258
|
552
|
259 switch (rc) {
|
|
260 case NGX_DECLINED:
|
|
261 return NGX_DECLINED;
|
|
262 case NGX_ERROR:
|
|
263 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
264 default: /* NGX_OK */
|
|
265 break;
|
|
266 }
|
122
|
267
|
552
|
268 p = ngx_pnalloc(c->pool, len);
|
|
269 if (p == NULL) {
|
|
270 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
271 }
|
400
|
272
|
552
|
273 ngx_memcpy(p, ip, len);
|
400
|
274
|
552
|
275 cln->handler = ngx_http_realip_cleanup;
|
286
|
276
|
552
|
277 ctx->connection = c;
|
|
278 ctx->sockaddr = c->sockaddr;
|
|
279 ctx->socklen = c->socklen;
|
|
280 ctx->addr_text = c->addr_text;
|
122
|
281
|
552
|
282 c->sockaddr = addr.sockaddr;
|
|
283 c->socklen = addr.socklen;
|
|
284 c->addr_text.len = len;
|
|
285 c->addr_text.data = p;
|
122
|
286
|
272
|
287 return NGX_DECLINED;
|
122
|
288 }
|
|
289
|
|
290
|
400
|
291 static void
|
|
292 ngx_http_realip_cleanup(void *data)
|
|
293 {
|
|
294 ngx_http_realip_ctx_t *ctx = data;
|
|
295
|
552
|
296 ngx_connection_t *c;
|
400
|
297
|
|
298 c = ctx->connection;
|
|
299
|
552
|
300 c->sockaddr = ctx->sockaddr;
|
|
301 c->socklen = ctx->socklen;
|
400
|
302 c->addr_text = ctx->addr_text;
|
|
303 }
|
|
304
|
|
305
|
122
|
306 static char *
|
|
307 ngx_http_realip_from(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
308 {
|
|
309 ngx_http_realip_loc_conf_t *rlcf = conf;
|
|
310
|
420
|
311 ngx_int_t rc;
|
|
312 ngx_str_t *value;
|
463
|
313 ngx_cidr_t cidr;
|
420
|
314 ngx_http_realip_from_t *from;
|
122
|
315
|
552
|
316 value = cf->args->elts;
|
|
317
|
|
318 #if (NGX_HAVE_UNIX_DOMAIN)
|
|
319
|
|
320 if (ngx_strcmp(value[1].data, "unix:") == 0) {
|
|
321 rlcf->unixsock = 1;
|
|
322 return NGX_CONF_OK;
|
|
323 }
|
|
324
|
|
325 #endif
|
|
326
|
122
|
327 if (rlcf->from == NULL) {
|
|
328 rlcf->from = ngx_array_create(cf->pool, 2,
|
|
329 sizeof(ngx_http_realip_from_t));
|
|
330 if (rlcf->from == NULL) {
|
|
331 return NGX_CONF_ERROR;
|
|
332 }
|
|
333 }
|
|
334
|
|
335 from = ngx_array_push(rlcf->from);
|
|
336 if (from == NULL) {
|
|
337 return NGX_CONF_ERROR;
|
|
338 }
|
|
339
|
463
|
340 rc = ngx_ptocidr(&value[1], &cidr);
|
326
|
341
|
|
342 if (rc == NGX_ERROR) {
|
122
|
343 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid parameter \"%V\"",
|
|
344 &value[1]);
|
|
345 return NGX_CONF_ERROR;
|
|
346 }
|
|
347
|
463
|
348 if (cidr.family != AF_INET) {
|
|
349 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
552
|
350 "\"set_real_ip_from\" supports IPv4 only");
|
463
|
351 return NGX_CONF_ERROR;
|
|
352 }
|
|
353
|
326
|
354 if (rc == NGX_DONE) {
|
|
355 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
356 "low address bits of %V are meaningless", &value[1]);
|
|
357 }
|
|
358
|
463
|
359 from->mask = cidr.u.in.mask;
|
|
360 from->addr = cidr.u.in.addr;
|
122
|
361
|
|
362 return NGX_CONF_OK;
|
|
363 }
|
|
364
|
|
365
|
420
|
366 static char *
|
|
367 ngx_http_realip(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
368 {
|
|
369 ngx_http_realip_loc_conf_t *rlcf = conf;
|
|
370
|
|
371 ngx_str_t *value;
|
|
372
|
|
373 value = cf->args->elts;
|
|
374
|
|
375 if (ngx_strcmp(value[1].data, "X-Real-IP") == 0) {
|
|
376 rlcf->type = NGX_HTTP_REALIP_XREALIP;
|
|
377 return NGX_CONF_OK;
|
|
378 }
|
|
379
|
|
380 if (ngx_strcmp(value[1].data, "X-Forwarded-For") == 0) {
|
|
381 rlcf->type = NGX_HTTP_REALIP_XFWD;
|
|
382 return NGX_CONF_OK;
|
|
383 }
|
|
384
|
|
385 rlcf->type = NGX_HTTP_REALIP_HEADER;
|
|
386 rlcf->hash = ngx_hash_strlow(value[1].data, value[1].data, value[1].len);
|
|
387 rlcf->header = value[1];
|
|
388
|
|
389 return NGX_CONF_OK;
|
|
390 }
|
|
391
|
|
392
|
122
|
393 static void *
|
|
394 ngx_http_realip_create_loc_conf(ngx_conf_t *cf)
|
|
395 {
|
|
396 ngx_http_realip_loc_conf_t *conf;
|
|
397
|
|
398 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_realip_loc_conf_t));
|
|
399 if (conf == NULL) {
|
505
|
400 return NULL;
|
122
|
401 }
|
|
402
|
|
403 /*
|
|
404 * set by ngx_pcalloc():
|
|
405 *
|
|
406 * conf->from = NULL;
|
420
|
407 * conf->hash = 0;
|
|
408 * conf->header = { 0, NULL };
|
552
|
409 * conf->unixsock = 0;
|
122
|
410 */
|
|
411
|
420
|
412 conf->type = NGX_CONF_UNSET_UINT;
|
122
|
413
|
|
414 return conf;
|
|
415 }
|
|
416
|
|
417
|
|
418 static char *
|
|
419 ngx_http_realip_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
420 {
|
|
421 ngx_http_realip_loc_conf_t *prev = parent;
|
|
422 ngx_http_realip_loc_conf_t *conf = child;
|
|
423
|
|
424 if (conf->from == NULL) {
|
|
425 conf->from = prev->from;
|
552
|
426 #if (NGX_HAVE_UNIX_DOMAIN)
|
|
427 conf->unixsock = prev->unixsock;
|
|
428 #endif
|
122
|
429 }
|
|
430
|
420
|
431 ngx_conf_merge_uint_value(conf->type, prev->type, NGX_HTTP_REALIP_XREALIP);
|
|
432
|
|
433 if (conf->header.len == 0) {
|
|
434 conf->hash = prev->hash;
|
|
435 conf->header = prev->header;
|
|
436 }
|
122
|
437
|
|
438 return NGX_CONF_OK;
|
|
439 }
|
|
440
|
|
441
|
|
442 static ngx_int_t
|
230
|
443 ngx_http_realip_init(ngx_conf_t *cf)
|
122
|
444 {
|
|
445 ngx_http_handler_pt *h;
|
|
446 ngx_http_core_main_conf_t *cmcf;
|
|
447
|
230
|
448 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
|
122
|
449
|
|
450 h = ngx_array_push(&cmcf->phases[NGX_HTTP_POST_READ_PHASE].handlers);
|
|
451 if (h == NULL) {
|
|
452 return NGX_ERROR;
|
|
453 }
|
|
454
|
|
455 *h = ngx_http_realip_handler;
|
|
456
|
130
|
457 h = ngx_array_push(&cmcf->phases[NGX_HTTP_PREACCESS_PHASE].handlers);
|
122
|
458 if (h == NULL) {
|
|
459 return NGX_ERROR;
|
|
460 }
|
|
461
|
|
462 *h = ngx_http_realip_handler;
|
|
463
|
|
464 return NGX_OK;
|
|
465 }
|