16
16
17
17
18
18
typedef struct {
19
- ngx_array_t * from ; /* array of ngx_in_cidr_t */
19
+ ngx_array_t * from ; /* array of ngx_cidr_t */
20
20
ngx_uint_t type ;
21
21
ngx_uint_t hash ;
22
22
ngx_str_t header ;
23
- #if (NGX_HAVE_UNIX_DOMAIN )
24
- ngx_uint_t unixsock ; /* unsigned unixsock:2; */
25
- #endif
23
+ ngx_flag_t recursive ;
26
24
} ngx_http_realip_loc_conf_t ;
27
25
28
26
@@ -35,8 +33,8 @@ typedef struct {
35
33
36
34
37
35
static ngx_int_t ngx_http_realip_handler (ngx_http_request_t * r );
38
- static ngx_int_t ngx_http_realip_set_addr (ngx_http_request_t * r , u_char * ip ,
39
- size_t len );
36
+ static ngx_int_t ngx_http_realip_set_addr (ngx_http_request_t * r ,
37
+ ngx_addr_t * addr );
40
38
static void ngx_http_realip_cleanup (void * data );
41
39
static char * ngx_http_realip_from (ngx_conf_t * cf , ngx_command_t * cmd ,
42
40
void * conf );
@@ -63,6 +61,13 @@ static ngx_command_t ngx_http_realip_commands[] = {
63
61
0 ,
64
62
NULL },
65
63
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
+
66
71
ngx_null_command
67
72
};
68
73
@@ -105,10 +110,9 @@ ngx_http_realip_handler(ngx_http_request_t *r)
105
110
u_char * ip , * p ;
106
111
size_t len ;
107
112
ngx_uint_t i , hash ;
113
+ ngx_addr_t addr ;
108
114
ngx_list_part_t * part ;
109
115
ngx_table_elt_t * header ;
110
- struct sockaddr_in * sin ;
111
- ngx_in_cidr_t * from ;
112
116
ngx_connection_t * c ;
113
117
ngx_http_realip_ctx_t * ctx ;
114
118
ngx_http_realip_loc_conf_t * rlcf ;
@@ -121,12 +125,7 @@ ngx_http_realip_handler(ngx_http_request_t *r)
121
125
122
126
rlcf = ngx_http_get_module_loc_conf (r , ngx_http_realip_module );
123
127
124
- if (rlcf -> from == NULL
125
- #if (NGX_HAVE_UNIX_DOMAIN )
126
- && !rlcf -> unixsock
127
- #endif
128
- )
129
- {
128
+ if (rlcf -> from == NULL ) {
130
129
return NGX_DECLINED ;
131
130
}
132
131
@@ -152,15 +151,6 @@ ngx_http_realip_handler(ngx_http_request_t *r)
152
151
len = r -> headers_in .x_forwarded_for -> value .len ;
153
152
ip = r -> headers_in .x_forwarded_for -> value .data ;
154
153
155
- for (p = ip + len - 1 ; p > ip ; p -- ) {
156
- if (* p == ' ' || * p == ',' ) {
157
- p ++ ;
158
- len -= p - ip ;
159
- ip = p ;
160
- break ;
161
- }
162
- }
163
-
164
154
break ;
165
155
166
156
default : /* NGX_HTTP_REALIP_HEADER */
@@ -204,42 +194,27 @@ ngx_http_realip_handler(ngx_http_request_t *r)
204
194
205
195
ngx_log_debug1 (NGX_LOG_DEBUG_HTTP , c -> log , 0 , "realip: \"%s\"" , ip );
206
196
207
- /* AF_INET only */
208
-
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 ++ ) {
197
+ addr .sockaddr = c -> sockaddr ;
198
+ addr .socklen = c -> socklen ;
199
+ /* addr.name = c->addr_text; */
214
200
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 );
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 );
229
206
}
230
207
231
- #endif
232
-
233
208
return NGX_DECLINED ;
234
209
}
235
210
236
211
237
212
static ngx_int_t
238
- ngx_http_realip_set_addr (ngx_http_request_t * r , u_char * ip , size_t len )
213
+ ngx_http_realip_set_addr (ngx_http_request_t * r , ngx_addr_t * addr )
239
214
{
215
+ size_t len ;
240
216
u_char * p ;
241
- ngx_int_t rc ;
242
- ngx_addr_t addr ;
217
+ u_char text [NGX_SOCKADDR_STRLEN ];
243
218
ngx_connection_t * c ;
244
219
ngx_pool_cleanup_t * cln ;
245
220
ngx_http_realip_ctx_t * ctx ;
@@ -254,23 +229,17 @@ ngx_http_realip_set_addr(ngx_http_request_t *r, u_char *ip, size_t len)
254
229
255
230
c = r -> connection ;
256
231
257
- rc = ngx_parse_addr (c -> pool , & addr , ip , len );
258
-
259
- switch (rc ) {
260
- case NGX_DECLINED :
261
- return NGX_DECLINED ;
262
- case NGX_ERROR :
232
+ len = ngx_sock_ntop (addr -> sockaddr , text , NGX_SOCKADDR_STRLEN , 0 );
233
+ if (len == 0 ) {
263
234
return NGX_HTTP_INTERNAL_SERVER_ERROR ;
264
- default : /* NGX_OK */
265
- break ;
266
235
}
267
236
268
237
p = ngx_pnalloc (c -> pool , len );
269
238
if (p == NULL ) {
270
239
return NGX_HTTP_INTERNAL_SERVER_ERROR ;
271
240
}
272
241
273
- ngx_memcpy (p , ip , len );
242
+ ngx_memcpy (p , text , len );
274
243
275
244
cln -> handler = ngx_http_realip_cleanup ;
276
245
@@ -279,8 +248,8 @@ ngx_http_realip_set_addr(ngx_http_request_t *r, u_char *ip, size_t len)
279
248
ctx -> socklen = c -> socklen ;
280
249
ctx -> addr_text = c -> addr_text ;
281
250
282
- c -> sockaddr = addr . sockaddr ;
283
- c -> socklen = addr . socklen ;
251
+ c -> sockaddr = addr -> sockaddr ;
252
+ c -> socklen = addr -> socklen ;
284
253
c -> addr_text .len = len ;
285
254
c -> addr_text .data = p ;
286
255
@@ -310,55 +279,45 @@ ngx_http_realip_from(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
310
279
311
280
ngx_int_t rc ;
312
281
ngx_str_t * value ;
313
- ngx_cidr_t cidr ;
314
- ngx_in_cidr_t * from ;
282
+ ngx_cidr_t * cidr ;
315
283
316
284
value = cf -> args -> elts ;
317
285
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
-
327
286
if (rlcf -> from == NULL ) {
328
287
rlcf -> from = ngx_array_create (cf -> pool , 2 ,
329
- sizeof (ngx_in_cidr_t ));
288
+ sizeof (ngx_cidr_t ));
330
289
if (rlcf -> from == NULL ) {
331
290
return NGX_CONF_ERROR ;
332
291
}
333
292
}
334
293
335
- from = ngx_array_push (rlcf -> from );
336
- if (from == NULL ) {
294
+ cidr = ngx_array_push (rlcf -> from );
295
+ if (cidr == NULL ) {
337
296
return NGX_CONF_ERROR ;
338
297
}
339
298
340
- rc = ngx_ptocidr (& value [1 ], & cidr );
299
+ #if (NGX_HAVE_UNIX_DOMAIN )
300
+
301
+ if (ngx_strcmp (value [1 ].data , "unix:" ) == 0 ) {
302
+ cidr -> family = AF_UNIX ;
303
+ return NGX_CONF_OK ;
304
+ }
305
+
306
+ #endif
307
+
308
+ rc = ngx_ptocidr (& value [1 ], cidr );
341
309
342
310
if (rc == NGX_ERROR ) {
343
311
ngx_conf_log_error (NGX_LOG_EMERG , cf , 0 , "invalid parameter \"%V\"" ,
344
312
& value [1 ]);
345
313
return NGX_CONF_ERROR ;
346
314
}
347
315
348
- if (cidr .family != AF_INET ) {
349
- ngx_conf_log_error (NGX_LOG_EMERG , cf , 0 ,
350
- "\"set_real_ip_from\" supports IPv4 only" );
351
- return NGX_CONF_ERROR ;
352
- }
353
-
354
316
if (rc == NGX_DONE ) {
355
317
ngx_conf_log_error (NGX_LOG_WARN , cf , 0 ,
356
318
"low address bits of %V are meaningless" , & value [1 ]);
357
319
}
358
320
359
- from -> mask = cidr .u .in .mask ;
360
- from -> addr = cidr .u .in .addr ;
361
-
362
321
return NGX_CONF_OK ;
363
322
}
364
323
@@ -409,9 +368,7 @@ ngx_http_realip_create_loc_conf(ngx_conf_t *cf)
409
368
*/
410
369
411
370
conf -> type = NGX_CONF_UNSET_UINT ;
412
- #if (NGX_HAVE_UNIX_DOMAIN )
413
- conf -> unixsock = 2 ;
414
- #endif
371
+ conf -> recursive = NGX_CONF_UNSET ;
415
372
416
373
return conf ;
417
374
}
@@ -427,13 +384,8 @@ ngx_http_realip_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
427
384
conf -> from = prev -> from ;
428
385
}
429
386
430
- #if (NGX_HAVE_UNIX_DOMAIN )
431
- if (conf -> unixsock == 2 ) {
432
- conf -> unixsock = (prev -> unixsock == 2 ) ? 0 : prev -> unixsock ;
433
- }
434
- #endif
435
-
436
387
ngx_conf_merge_uint_value (conf -> type , prev -> type , NGX_HTTP_REALIP_XREALIP );
388
+ ngx_conf_merge_value (conf -> recursive , prev -> recursive , 0 );
437
389
438
390
if (conf -> header .len == 0 ) {
439
391
conf -> hash = prev -> hash ;
0 commit comments