Skip to content
This repository was archived by the owner on Apr 11, 2023. It is now read-only.

Commit 5d63699

Browse files
author
mdounin
committed
Merge of r4614, r4624-r4629, r4631: proxy recursive changes.
*) Added IPv6 and UNIX-domain socket support in "debug_connection" directive. *) New function ngx_http_get_forwarded_addr() to look up real client address. On input it takes an original address, string in the X-Forwarded-For format and its length, list of trusted proxies, and a flag indicating to perform the recursive search. On output it returns NGX_OK and the "deepest" valid address in a chain, or NGX_DECLINED. It supports AF_INET and AF_INET6. Additionally, original address and/or proxy may be specified as AF_UNIX. *) Realip: chains of trusted proxies and IPv6 support. The module now supports recursive search of client address through the chain of trusted proxies, controlled by the "real_ip_recursive" directive (closes git-mirror#2). It also gets full IPv6 support (closes #44) and canonical value of the $client_addr variable on address change. Example: real_ip_header X-Forwarded-For; set_real_ip_from 127.0.0.0/8; set_real_ip_from ::1; set_real_ip_from unix:; real_ip_recursive on; *) Geo: chains of trusted proxies and partial IPv6 support. The module now supports recursive search of client address through the chain of trusted proxies, controlled by the "proxy_recursive" directive in the "geo" block. It also gets partial IPv6 support: now proxies may be specified with IPv6 addresses. Example: geo $test { ... proxy 127.0.0.1; proxy ::1; proxy_recursive; } There's also a slight change in behavior. When original client address (as specified by the "geo" directive) is one of the trusted proxies, and the value of the X-Forwarded-For request header cannot not be parsed as a valid address, an original client address will be used for lookup. Previously, 255.255.255.255 was used in this case. *) Geoip: trusted proxies support and partial IPv6 support. The module now supports recursive search of client address through the chain of trusted proxies (closes #100), in the same scope as the geo module. Proxies are listed by the "geoip_proxy" directive, recursive search is enabled by the "geoip_proxy_recursive" directive. IPv6 is partially supported: proxies may be specified with IPv6 addresses. Example: geoip_country .../GeoIP.dat; geoip_proxy 127.0.0.1; geoip_proxy ::1; geoip_proxy 10.0.0.0/8; geoip_proxy_recursive on;
1 parent d9d688f commit 5d63699

File tree

8 files changed

+407
-231
lines changed

8 files changed

+407
-231
lines changed

src/event/ngx_event.c

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,38 +1064,34 @@ ngx_event_debug_connection(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
10641064

10651065
ngx_int_t rc;
10661066
ngx_str_t *value;
1067-
ngx_event_debug_t *dc;
10681067
struct hostent *h;
1069-
ngx_cidr_t cidr;
1068+
ngx_cidr_t *cidr;
10701069

10711070
value = cf->args->elts;
10721071

1073-
dc = ngx_array_push(&ecf->debug_connection);
1074-
if (dc == NULL) {
1072+
cidr = ngx_array_push(&ecf->debug_connection);
1073+
if (cidr == NULL) {
10751074
return NGX_CONF_ERROR;
10761075
}
10771076

1078-
rc = ngx_ptocidr(&value[1], &cidr);
1077+
#if (NGX_HAVE_UNIX_DOMAIN)
1078+
1079+
if (ngx_strcmp(value[1].data, "unix:") == 0) {
1080+
cidr->family = AF_UNIX;
1081+
return NGX_CONF_OK;
1082+
}
1083+
1084+
#endif
1085+
1086+
rc = ngx_ptocidr(&value[1], cidr);
10791087

10801088
if (rc == NGX_DONE) {
10811089
ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
10821090
"low address bits of %V are meaningless", &value[1]);
1083-
rc = NGX_OK;
1091+
return NGX_CONF_OK;
10841092
}
10851093

10861094
if (rc == NGX_OK) {
1087-
1088-
/* AF_INET only */
1089-
1090-
if (cidr.family != AF_INET) {
1091-
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1092-
"\"debug_connection\" supports IPv4 only");
1093-
return NGX_CONF_ERROR;
1094-
}
1095-
1096-
dc->mask = cidr.u.in.mask;
1097-
dc->addr = cidr.u.in.addr;
1098-
10991095
return NGX_CONF_OK;
11001096
}
11011097

@@ -1107,8 +1103,9 @@ ngx_event_debug_connection(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
11071103
return NGX_CONF_ERROR;
11081104
}
11091105

1110-
dc->mask = 0xffffffff;
1111-
dc->addr = *(in_addr_t *)(h->h_addr_list[0]);
1106+
cidr->family = AF_INET;
1107+
cidr->u.in.mask = 0xffffffff;
1108+
cidr->u.in.addr = *(in_addr_t *)(h->h_addr_list[0]);
11121109

11131110
#else
11141111

@@ -1142,7 +1139,7 @@ ngx_event_core_create_conf(ngx_cycle_t *cycle)
11421139
#if (NGX_DEBUG)
11431140

11441141
if (ngx_array_init(&ecf->debug_connection, cycle->pool, 4,
1145-
sizeof(ngx_event_debug_t)) == NGX_ERROR)
1142+
sizeof(ngx_cidr_t)) == NGX_ERROR)
11461143
{
11471144
return NULL;
11481145
}

src/event/ngx_event.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -221,12 +221,6 @@ struct ngx_event_aio_s {
221221
#endif
222222

223223

224-
typedef struct {
225-
in_addr_t mask;
226-
in_addr_t addr;
227-
} ngx_event_debug_t;
228-
229-
230224
typedef struct {
231225
ngx_int_t (*add)(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags);
232226
ngx_int_t (*del)(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags);

src/event/ngx_event_accept.c

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -286,17 +286,56 @@ ngx_event_accept(ngx_event_t *ev)
286286
#if (NGX_DEBUG)
287287
{
288288

289-
in_addr_t i;
290-
ngx_event_debug_t *dc;
291-
struct sockaddr_in *sin;
289+
struct sockaddr_in *sin;
290+
ngx_cidr_t *cidr;
291+
ngx_uint_t i;
292+
#if (NGX_HAVE_INET6)
293+
struct sockaddr_in6 *sin6;
294+
ngx_uint_t n;
295+
#endif
292296

293-
sin = (struct sockaddr_in *) sa;
294-
dc = ecf->debug_connection.elts;
297+
cidr = ecf->debug_connection.elts;
295298
for (i = 0; i < ecf->debug_connection.nelts; i++) {
296-
if ((sin->sin_addr.s_addr & dc[i].mask) == dc[i].addr) {
297-
log->log_level = NGX_LOG_DEBUG_CONNECTION|NGX_LOG_DEBUG_ALL;
299+
if (cidr[i].family != c->sockaddr->sa_family) {
300+
goto next;
301+
}
302+
303+
switch (cidr[i].family) {
304+
305+
#if (NGX_HAVE_INET6)
306+
case AF_INET6:
307+
sin6 = (struct sockaddr_in6 *) c->sockaddr;
308+
for (n = 0; n < 16; n++) {
309+
if ((sin6->sin6_addr.s6_addr[n]
310+
& cidr[i].u.in6.mask.s6_addr[n])
311+
!= cidr[i].u.in6.addr.s6_addr[n])
312+
{
313+
goto next;
314+
}
315+
}
316+
break;
317+
#endif
318+
319+
#if (NGX_HAVE_UNIX_DOMAIN)
320+
case AF_UNIX:
321+
break;
322+
#endif
323+
324+
default: /* AF_INET */
325+
sin = (struct sockaddr_in *) c->sockaddr;
326+
if ((sin->sin_addr.s_addr & cidr[i].u.in.mask)
327+
!= cidr[i].u.in.addr)
328+
{
329+
goto next;
330+
}
298331
break;
299332
}
333+
334+
log->log_level = NGX_LOG_DEBUG_CONNECTION|NGX_LOG_DEBUG_ALL;
335+
break;
336+
337+
next:
338+
continue;
300339
}
301340

302341
}

0 commit comments

Comments
 (0)