122
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
660
|
4 * Copyright (C) Nginx, Inc.
|
122
|
5 */
|
|
6
|
|
7
|
|
8 #include <ngx_config.h>
|
|
9 #include <ngx_core.h>
|
|
10 #include <ngx_http.h>
|
|
11
|
|
12
|
412
|
13 #define NGX_HTTP_REALIP_XREALIP 0
|
|
14 #define NGX_HTTP_REALIP_XFWD 1
|
|
15 #define NGX_HTTP_REALIP_HEADER 2
|
|
16
|
|
17
|
122
|
18 typedef struct {
|
674
|
19 ngx_array_t *from; /* array of ngx_cidr_t */
|
412
|
20 ngx_uint_t type;
|
|
21 ngx_uint_t hash;
|
|
22 ngx_str_t header;
|
674
|
23 ngx_flag_t recursive;
|
122
|
24 } ngx_http_realip_loc_conf_t;
|
|
25
|
|
26
|
396
|
27 typedef struct {
|
|
28 ngx_connection_t *connection;
|
540
|
29 struct sockaddr *sockaddr;
|
|
30 socklen_t socklen;
|
396
|
31 ngx_str_t addr_text;
|
|
32 } ngx_http_realip_ctx_t;
|
|
33
|
|
34
|
122
|
35 static ngx_int_t ngx_http_realip_handler(ngx_http_request_t *r);
|
674
|
36 static ngx_int_t ngx_http_realip_set_addr(ngx_http_request_t *r,
|
|
37 ngx_addr_t *addr);
|
396
|
38 static void ngx_http_realip_cleanup(void *data);
|
122
|
39 static char *ngx_http_realip_from(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
40 void *conf);
|
412
|
41 static char *ngx_http_realip(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
|
122
|
42 static void *ngx_http_realip_create_loc_conf(ngx_conf_t *cf);
|
|
43 static char *ngx_http_realip_merge_loc_conf(ngx_conf_t *cf,
|
|
44 void *parent, void *child);
|
230
|
45 static ngx_int_t ngx_http_realip_init(ngx_conf_t *cf);
|
122
|
46
|
|
47
|
|
48 static ngx_command_t ngx_http_realip_commands[] = {
|
|
49
|
|
50 { ngx_string("set_real_ip_from"),
|
|
51 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
52 ngx_http_realip_from,
|
|
53 NGX_HTTP_LOC_CONF_OFFSET,
|
|
54 0,
|
|
55 NULL },
|
|
56
|
|
57 { ngx_string("real_ip_header"),
|
|
58 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
412
|
59 ngx_http_realip,
|
122
|
60 NGX_HTTP_LOC_CONF_OFFSET,
|
412
|
61 0,
|
|
62 NULL },
|
122
|
63
|
674
|
64 { ngx_string("real_ip_recursive"),
|
|
65 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
66 ngx_conf_set_flag_slot,
|
|
67 NGX_HTTP_LOC_CONF_OFFSET,
|
|
68 offsetof(ngx_http_realip_loc_conf_t, recursive),
|
|
69 NULL },
|
|
70
|
122
|
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;
|
412
|
112 ngx_uint_t i, hash;
|
674
|
113 ngx_addr_t addr;
|
412
|
114 ngx_list_part_t *part;
|
|
115 ngx_table_elt_t *header;
|
396
|
116 ngx_connection_t *c;
|
|
117 ngx_http_realip_ctx_t *ctx;
|
122
|
118 ngx_http_realip_loc_conf_t *rlcf;
|
|
119
|
396
|
120 ctx = ngx_http_get_module_ctx(r, ngx_http_realip_module);
|
|
121
|
|
122 if (ctx) {
|
272
|
123 return NGX_DECLINED;
|
122
|
124 }
|
|
125
|
|
126 rlcf = ngx_http_get_module_loc_conf(r, ngx_http_realip_module);
|
|
127
|
674
|
128 if (rlcf->from == NULL) {
|
272
|
129 return NGX_DECLINED;
|
122
|
130 }
|
|
131
|
412
|
132 switch (rlcf->type) {
|
|
133
|
|
134 case NGX_HTTP_REALIP_XREALIP:
|
|
135
|
122
|
136 if (r->headers_in.x_real_ip == NULL) {
|
272
|
137 return NGX_DECLINED;
|
122
|
138 }
|
|
139
|
|
140 len = r->headers_in.x_real_ip->value.len;
|
|
141 ip = r->headers_in.x_real_ip->value.data;
|
|
142
|
412
|
143 break;
|
|
144
|
|
145 case NGX_HTTP_REALIP_XFWD:
|
|
146
|
122
|
147 if (r->headers_in.x_forwarded_for == NULL) {
|
272
|
148 return NGX_DECLINED;
|
122
|
149 }
|
|
150
|
|
151 len = r->headers_in.x_forwarded_for->value.len;
|
|
152 ip = r->headers_in.x_forwarded_for->value.data;
|
|
153
|
412
|
154 break;
|
|
155
|
|
156 default: /* NGX_HTTP_REALIP_HEADER */
|
|
157
|
|
158 part = &r->headers_in.headers.part;
|
|
159 header = part->elts;
|
|
160
|
|
161 hash = rlcf->hash;
|
|
162 len = rlcf->header.len;
|
|
163 p = rlcf->header.data;
|
|
164
|
|
165 for (i = 0; /* void */ ; i++) {
|
|
166
|
|
167 if (i >= part->nelts) {
|
|
168 if (part->next == NULL) {
|
|
169 break;
|
|
170 }
|
|
171
|
|
172 part = part->next;
|
|
173 header = part->elts;
|
|
174 i = 0;
|
|
175 }
|
|
176
|
|
177 if (hash == header[i].hash
|
|
178 && len == header[i].key.len
|
|
179 && ngx_strncmp(p, header[i].lowcase_key, len) == 0)
|
|
180 {
|
|
181 len = header[i].value.len;
|
|
182 ip = header[i].value.data;
|
|
183
|
|
184 goto found;
|
|
185 }
|
|
186 }
|
|
187
|
|
188 return NGX_DECLINED;
|
122
|
189 }
|
|
190
|
412
|
191 found:
|
|
192
|
396
|
193 c = r->connection;
|
|
194
|
|
195 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "realip: \"%s\"", ip);
|
286
|
196
|
674
|
197 addr.sockaddr = c->sockaddr;
|
|
198 addr.socklen = c->socklen;
|
|
199 /* addr.name = c->addr_text; */
|
540
|
200
|
674
|
201 if (ngx_http_get_forwarded_addr(r, &addr, ip, len, rlcf->from,
|
|
202 rlcf->recursive)
|
|
203 == NGX_OK)
|
|
204 {
|
|
205 return ngx_http_realip_set_addr(r, &addr);
|
540
|
206 }
|
|
207
|
|
208 return NGX_DECLINED;
|
|
209 }
|
122
|
210
|
|
211
|
540
|
212 static ngx_int_t
|
674
|
213 ngx_http_realip_set_addr(ngx_http_request_t *r, ngx_addr_t *addr)
|
540
|
214 {
|
674
|
215 size_t len;
|
540
|
216 u_char *p;
|
674
|
217 u_char text[NGX_SOCKADDR_STRLEN];
|
540
|
218 ngx_connection_t *c;
|
|
219 ngx_pool_cleanup_t *cln;
|
|
220 ngx_http_realip_ctx_t *ctx;
|
286
|
221
|
540
|
222 cln = ngx_pool_cleanup_add(r->pool, sizeof(ngx_http_realip_ctx_t));
|
|
223 if (cln == NULL) {
|
|
224 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
225 }
|
286
|
226
|
540
|
227 ctx = cln->data;
|
|
228 ngx_http_set_ctx(r, ctx, ngx_http_realip_module);
|
|
229
|
|
230 c = r->connection;
|
|
231
|
674
|
232 len = ngx_sock_ntop(addr->sockaddr, text, NGX_SOCKADDR_STRLEN, 0);
|
|
233 if (len == 0) {
|
540
|
234 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
235 }
|
122
|
236
|
540
|
237 p = ngx_pnalloc(c->pool, len);
|
|
238 if (p == NULL) {
|
|
239 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
240 }
|
396
|
241
|
674
|
242 ngx_memcpy(p, text, len);
|
396
|
243
|
540
|
244 cln->handler = ngx_http_realip_cleanup;
|
286
|
245
|
540
|
246 ctx->connection = c;
|
|
247 ctx->sockaddr = c->sockaddr;
|
|
248 ctx->socklen = c->socklen;
|
|
249 ctx->addr_text = c->addr_text;
|
122
|
250
|
674
|
251 c->sockaddr = addr->sockaddr;
|
|
252 c->socklen = addr->socklen;
|
540
|
253 c->addr_text.len = len;
|
|
254 c->addr_text.data = p;
|
122
|
255
|
272
|
256 return NGX_DECLINED;
|
122
|
257 }
|
|
258
|
|
259
|
396
|
260 static void
|
|
261 ngx_http_realip_cleanup(void *data)
|
|
262 {
|
|
263 ngx_http_realip_ctx_t *ctx = data;
|
|
264
|
540
|
265 ngx_connection_t *c;
|
396
|
266
|
|
267 c = ctx->connection;
|
|
268
|
540
|
269 c->sockaddr = ctx->sockaddr;
|
|
270 c->socklen = ctx->socklen;
|
396
|
271 c->addr_text = ctx->addr_text;
|
|
272 }
|
|
273
|
|
274
|
122
|
275 static char *
|
|
276 ngx_http_realip_from(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
277 {
|
|
278 ngx_http_realip_loc_conf_t *rlcf = conf;
|
|
279
|
412
|
280 ngx_int_t rc;
|
|
281 ngx_str_t *value;
|
674
|
282 ngx_cidr_t *cidr;
|
122
|
283
|
540
|
284 value = cf->args->elts;
|
|
285
|
674
|
286 if (rlcf->from == NULL) {
|
|
287 rlcf->from = ngx_array_create(cf->pool, 2,
|
|
288 sizeof(ngx_cidr_t));
|
|
289 if (rlcf->from == NULL) {
|
|
290 return NGX_CONF_ERROR;
|
|
291 }
|
|
292 }
|
|
293
|
|
294 cidr = ngx_array_push(rlcf->from);
|
|
295 if (cidr == NULL) {
|
|
296 return NGX_CONF_ERROR;
|
|
297 }
|
|
298
|
540
|
299 #if (NGX_HAVE_UNIX_DOMAIN)
|
|
300
|
|
301 if (ngx_strcmp(value[1].data, "unix:") == 0) {
|
674
|
302 cidr->family = AF_UNIX;
|
540
|
303 return NGX_CONF_OK;
|
|
304 }
|
|
305
|
|
306 #endif
|
|
307
|
674
|
308 rc = ngx_ptocidr(&value[1], cidr);
|
326
|
309
|
|
310 if (rc == NGX_ERROR) {
|
122
|
311 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid parameter \"%V\"",
|
|
312 &value[1]);
|
|
313 return NGX_CONF_ERROR;
|
|
314 }
|
|
315
|
326
|
316 if (rc == NGX_DONE) {
|
|
317 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
318 "low address bits of %V are meaningless", &value[1]);
|
|
319 }
|
|
320
|
122
|
321 return NGX_CONF_OK;
|
|
322 }
|
|
323
|
|
324
|
412
|
325 static char *
|
|
326 ngx_http_realip(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
327 {
|
|
328 ngx_http_realip_loc_conf_t *rlcf = conf;
|
|
329
|
|
330 ngx_str_t *value;
|
|
331
|
|
332 value = cf->args->elts;
|
|
333
|
|
334 if (ngx_strcmp(value[1].data, "X-Real-IP") == 0) {
|
|
335 rlcf->type = NGX_HTTP_REALIP_XREALIP;
|
|
336 return NGX_CONF_OK;
|
|
337 }
|
|
338
|
|
339 if (ngx_strcmp(value[1].data, "X-Forwarded-For") == 0) {
|
|
340 rlcf->type = NGX_HTTP_REALIP_XFWD;
|
|
341 return NGX_CONF_OK;
|
|
342 }
|
|
343
|
|
344 rlcf->type = NGX_HTTP_REALIP_HEADER;
|
|
345 rlcf->hash = ngx_hash_strlow(value[1].data, value[1].data, value[1].len);
|
|
346 rlcf->header = value[1];
|
|
347
|
|
348 return NGX_CONF_OK;
|
|
349 }
|
|
350
|
|
351
|
122
|
352 static void *
|
|
353 ngx_http_realip_create_loc_conf(ngx_conf_t *cf)
|
|
354 {
|
|
355 ngx_http_realip_loc_conf_t *conf;
|
|
356
|
|
357 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_realip_loc_conf_t));
|
|
358 if (conf == NULL) {
|
496
|
359 return NULL;
|
122
|
360 }
|
|
361
|
|
362 /*
|
|
363 * set by ngx_pcalloc():
|
|
364 *
|
|
365 * conf->from = NULL;
|
412
|
366 * conf->hash = 0;
|
|
367 * conf->header = { 0, NULL };
|
122
|
368 */
|
|
369
|
412
|
370 conf->type = NGX_CONF_UNSET_UINT;
|
674
|
371 conf->recursive = NGX_CONF_UNSET;
|
122
|
372
|
|
373 return conf;
|
|
374 }
|
|
375
|
|
376
|
|
377 static char *
|
|
378 ngx_http_realip_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
379 {
|
|
380 ngx_http_realip_loc_conf_t *prev = parent;
|
|
381 ngx_http_realip_loc_conf_t *conf = child;
|
|
382
|
|
383 if (conf->from == NULL) {
|
|
384 conf->from = prev->from;
|
544
|
385 }
|
|
386
|
412
|
387 ngx_conf_merge_uint_value(conf->type, prev->type, NGX_HTTP_REALIP_XREALIP);
|
674
|
388 ngx_conf_merge_value(conf->recursive, prev->recursive, 0);
|
412
|
389
|
|
390 if (conf->header.len == 0) {
|
|
391 conf->hash = prev->hash;
|
|
392 conf->header = prev->header;
|
|
393 }
|
122
|
394
|
|
395 return NGX_CONF_OK;
|
|
396 }
|
|
397
|
|
398
|
|
399 static ngx_int_t
|
230
|
400 ngx_http_realip_init(ngx_conf_t *cf)
|
122
|
401 {
|
|
402 ngx_http_handler_pt *h;
|
|
403 ngx_http_core_main_conf_t *cmcf;
|
|
404
|
230
|
405 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
|
122
|
406
|
|
407 h = ngx_array_push(&cmcf->phases[NGX_HTTP_POST_READ_PHASE].handlers);
|
|
408 if (h == NULL) {
|
|
409 return NGX_ERROR;
|
|
410 }
|
|
411
|
|
412 *h = ngx_http_realip_handler;
|
|
413
|
130
|
414 h = ngx_array_push(&cmcf->phases[NGX_HTTP_PREACCESS_PHASE].handlers);
|
122
|
415 if (h == NULL) {
|
|
416 return NGX_ERROR;
|
|
417 }
|
|
418
|
|
419 *h = ngx_http_realip_handler;
|
|
420
|
|
421 return NGX_OK;
|
|
422 }
|