Mercurial > hg > nginx
comparison src/http/modules/ngx_http_realip_module.c @ 2257:74d270c8821e
real_ip_header supports any header
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Thu, 25 Sep 2008 19:51:56 +0000 |
parents | 2300ab9b069e |
children | 2e91aecb9e57 |
comparison
equal
deleted
inserted
replaced
2256:8c17cfe63d2c | 2257:74d270c8821e |
---|---|
5 | 5 |
6 | 6 |
7 #include <ngx_config.h> | 7 #include <ngx_config.h> |
8 #include <ngx_core.h> | 8 #include <ngx_core.h> |
9 #include <ngx_http.h> | 9 #include <ngx_http.h> |
10 | |
11 | |
12 #define NGX_HTTP_REALIP_XREALIP 0 | |
13 #define NGX_HTTP_REALIP_XFWD 1 | |
14 #define NGX_HTTP_REALIP_HEADER 2 | |
10 | 15 |
11 | 16 |
12 /* AF_INET only */ | 17 /* AF_INET only */ |
13 | 18 |
14 typedef struct { | 19 typedef struct { |
17 } ngx_http_realip_from_t; | 22 } ngx_http_realip_from_t; |
18 | 23 |
19 | 24 |
20 typedef struct { | 25 typedef struct { |
21 ngx_array_t *from; /* array of ngx_http_realip_from_t */ | 26 ngx_array_t *from; /* array of ngx_http_realip_from_t */ |
22 ngx_uint_t xfwd; | 27 ngx_uint_t type; |
28 ngx_uint_t hash; | |
29 ngx_str_t header; | |
23 } ngx_http_realip_loc_conf_t; | 30 } ngx_http_realip_loc_conf_t; |
24 | 31 |
25 | 32 |
26 typedef struct { | 33 typedef struct { |
27 ngx_connection_t *connection; | 34 ngx_connection_t *connection; |
32 | 39 |
33 static ngx_int_t ngx_http_realip_handler(ngx_http_request_t *r); | 40 static ngx_int_t ngx_http_realip_handler(ngx_http_request_t *r); |
34 static void ngx_http_realip_cleanup(void *data); | 41 static void ngx_http_realip_cleanup(void *data); |
35 static char *ngx_http_realip_from(ngx_conf_t *cf, ngx_command_t *cmd, | 42 static char *ngx_http_realip_from(ngx_conf_t *cf, ngx_command_t *cmd, |
36 void *conf); | 43 void *conf); |
44 static char *ngx_http_realip(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); | |
37 static void *ngx_http_realip_create_loc_conf(ngx_conf_t *cf); | 45 static void *ngx_http_realip_create_loc_conf(ngx_conf_t *cf); |
38 static char *ngx_http_realip_merge_loc_conf(ngx_conf_t *cf, | 46 static char *ngx_http_realip_merge_loc_conf(ngx_conf_t *cf, |
39 void *parent, void *child); | 47 void *parent, void *child); |
40 static ngx_int_t ngx_http_realip_init(ngx_conf_t *cf); | 48 static ngx_int_t ngx_http_realip_init(ngx_conf_t *cf); |
41 | |
42 | |
43 static ngx_conf_enum_t ngx_http_realip_header[] = { | |
44 { ngx_string("X-Forwarded-For"), 1 }, | |
45 { ngx_string("X-Real-IP"), 0 }, | |
46 { ngx_null_string, 0 } | |
47 }; | |
48 | 49 |
49 | 50 |
50 static ngx_command_t ngx_http_realip_commands[] = { | 51 static ngx_command_t ngx_http_realip_commands[] = { |
51 | 52 |
52 { ngx_string("set_real_ip_from"), | 53 { ngx_string("set_real_ip_from"), |
56 0, | 57 0, |
57 NULL }, | 58 NULL }, |
58 | 59 |
59 { ngx_string("real_ip_header"), | 60 { ngx_string("real_ip_header"), |
60 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | 61 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, |
61 ngx_conf_set_enum_slot, | 62 ngx_http_realip, |
62 NGX_HTTP_LOC_CONF_OFFSET, | 63 NGX_HTTP_LOC_CONF_OFFSET, |
63 offsetof(ngx_http_realip_loc_conf_t, xfwd), | 64 0, |
64 &ngx_http_realip_header }, | 65 NULL }, |
65 | 66 |
66 ngx_null_command | 67 ngx_null_command |
67 }; | 68 }; |
68 | 69 |
69 | 70 |
103 ngx_http_realip_handler(ngx_http_request_t *r) | 104 ngx_http_realip_handler(ngx_http_request_t *r) |
104 { | 105 { |
105 u_char *ip, *p; | 106 u_char *ip, *p; |
106 size_t len; | 107 size_t len; |
107 in_addr_t addr; | 108 in_addr_t addr; |
108 ngx_uint_t i; | 109 ngx_uint_t i, hash; |
110 ngx_list_part_t *part; | |
111 ngx_table_elt_t *header; | |
109 struct sockaddr_in *sin; | 112 struct sockaddr_in *sin; |
110 ngx_connection_t *c; | 113 ngx_connection_t *c; |
111 ngx_pool_cleanup_t *cln; | 114 ngx_pool_cleanup_t *cln; |
112 ngx_http_realip_ctx_t *ctx; | 115 ngx_http_realip_ctx_t *ctx; |
113 ngx_http_realip_from_t *from; | 116 ngx_http_realip_from_t *from; |
128 | 131 |
129 if (rlcf->from == NULL) { | 132 if (rlcf->from == NULL) { |
130 return NGX_DECLINED; | 133 return NGX_DECLINED; |
131 } | 134 } |
132 | 135 |
133 if (rlcf->xfwd == 0) { | 136 switch (rlcf->type) { |
137 | |
138 case NGX_HTTP_REALIP_XREALIP: | |
139 | |
134 if (r->headers_in.x_real_ip == NULL) { | 140 if (r->headers_in.x_real_ip == NULL) { |
135 return NGX_DECLINED; | 141 return NGX_DECLINED; |
136 } | 142 } |
137 | 143 |
138 len = r->headers_in.x_real_ip->value.len; | 144 len = r->headers_in.x_real_ip->value.len; |
139 ip = r->headers_in.x_real_ip->value.data; | 145 ip = r->headers_in.x_real_ip->value.data; |
140 | 146 |
141 } else { | 147 break; |
148 | |
149 case NGX_HTTP_REALIP_XFWD: | |
150 | |
142 if (r->headers_in.x_forwarded_for == NULL) { | 151 if (r->headers_in.x_forwarded_for == NULL) { |
143 return NGX_DECLINED; | 152 return NGX_DECLINED; |
144 } | 153 } |
145 | 154 |
146 len = r->headers_in.x_forwarded_for->value.len; | 155 len = r->headers_in.x_forwarded_for->value.len; |
152 len -= p - ip; | 161 len -= p - ip; |
153 ip = p; | 162 ip = p; |
154 break; | 163 break; |
155 } | 164 } |
156 } | 165 } |
157 } | 166 |
167 break; | |
168 | |
169 default: /* NGX_HTTP_REALIP_HEADER */ | |
170 | |
171 part = &r->headers_in.headers.part; | |
172 header = part->elts; | |
173 | |
174 hash = rlcf->hash; | |
175 len = rlcf->header.len; | |
176 p = rlcf->header.data; | |
177 | |
178 for (i = 0; /* void */ ; i++) { | |
179 | |
180 if (i >= part->nelts) { | |
181 if (part->next == NULL) { | |
182 break; | |
183 } | |
184 | |
185 part = part->next; | |
186 header = part->elts; | |
187 i = 0; | |
188 } | |
189 | |
190 if (hash == header[i].hash | |
191 && len == header[i].key.len | |
192 && ngx_strncmp(p, header[i].lowcase_key, len) == 0) | |
193 { | |
194 len = header[i].value.len; | |
195 ip = header[i].value.data; | |
196 | |
197 goto found; | |
198 } | |
199 } | |
200 | |
201 return NGX_DECLINED; | |
202 } | |
203 | |
204 found: | |
158 | 205 |
159 c = r->connection; | 206 c = r->connection; |
160 | 207 |
161 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "realip: \"%s\"", ip); | 208 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "realip: \"%s\"", ip); |
162 | 209 |
229 static char * | 276 static char * |
230 ngx_http_realip_from(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | 277 ngx_http_realip_from(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) |
231 { | 278 { |
232 ngx_http_realip_loc_conf_t *rlcf = conf; | 279 ngx_http_realip_loc_conf_t *rlcf = conf; |
233 | 280 |
234 ngx_int_t rc; | 281 ngx_int_t rc; |
235 ngx_str_t *value; | 282 ngx_str_t *value; |
236 ngx_inet_cidr_t in_cidr; | 283 ngx_inet_cidr_t in_cidr; |
237 ngx_http_realip_from_t *from; | 284 ngx_http_realip_from_t *from; |
238 | 285 |
239 if (rlcf->from == NULL) { | 286 if (rlcf->from == NULL) { |
240 rlcf->from = ngx_array_create(cf->pool, 2, | 287 rlcf->from = ngx_array_create(cf->pool, 2, |
241 sizeof(ngx_http_realip_from_t)); | 288 sizeof(ngx_http_realip_from_t)); |
242 if (rlcf->from == NULL) { | 289 if (rlcf->from == NULL) { |
269 | 316 |
270 return NGX_CONF_OK; | 317 return NGX_CONF_OK; |
271 } | 318 } |
272 | 319 |
273 | 320 |
321 static char * | |
322 ngx_http_realip(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
323 { | |
324 ngx_http_realip_loc_conf_t *rlcf = conf; | |
325 | |
326 ngx_str_t *value; | |
327 | |
328 value = cf->args->elts; | |
329 | |
330 if (ngx_strcmp(value[1].data, "X-Real-IP") == 0) { | |
331 rlcf->type = NGX_HTTP_REALIP_XREALIP; | |
332 return NGX_CONF_OK; | |
333 } | |
334 | |
335 if (ngx_strcmp(value[1].data, "X-Forwarded-For") == 0) { | |
336 rlcf->type = NGX_HTTP_REALIP_XFWD; | |
337 return NGX_CONF_OK; | |
338 } | |
339 | |
340 rlcf->type = NGX_HTTP_REALIP_HEADER; | |
341 rlcf->hash = ngx_hash_strlow(value[1].data, value[1].data, value[1].len); | |
342 rlcf->header = value[1]; | |
343 | |
344 return NGX_CONF_OK; | |
345 } | |
346 | |
347 | |
274 static void * | 348 static void * |
275 ngx_http_realip_create_loc_conf(ngx_conf_t *cf) | 349 ngx_http_realip_create_loc_conf(ngx_conf_t *cf) |
276 { | 350 { |
277 ngx_http_realip_loc_conf_t *conf; | 351 ngx_http_realip_loc_conf_t *conf; |
278 | 352 |
283 | 357 |
284 /* | 358 /* |
285 * set by ngx_pcalloc(): | 359 * set by ngx_pcalloc(): |
286 * | 360 * |
287 * conf->from = NULL; | 361 * conf->from = NULL; |
362 * conf->hash = 0; | |
363 * conf->header = { 0, NULL }; | |
288 */ | 364 */ |
289 | 365 |
290 conf->xfwd = NGX_CONF_UNSET_UINT; | 366 conf->type = NGX_CONF_UNSET_UINT; |
291 | 367 |
292 return conf; | 368 return conf; |
293 } | 369 } |
294 | 370 |
295 | 371 |
301 | 377 |
302 if (conf->from == NULL) { | 378 if (conf->from == NULL) { |
303 conf->from = prev->from; | 379 conf->from = prev->from; |
304 } | 380 } |
305 | 381 |
306 ngx_conf_merge_uint_value(conf->xfwd, prev->xfwd, 0); | 382 ngx_conf_merge_uint_value(conf->type, prev->type, NGX_HTTP_REALIP_XREALIP); |
383 | |
384 if (conf->header.len == 0) { | |
385 conf->hash = prev->hash; | |
386 conf->header = prev->header; | |
387 } | |
307 | 388 |
308 return NGX_CONF_OK; | 389 return NGX_CONF_OK; |
309 } | 390 } |
310 | 391 |
311 | 392 |