Realip: port support in X-Real-IP and X-Forwarded-For.

Now, the module extracts optional port which may accompany an
IP address.  This custom extension is introduced, among other
things, in order to facilitate logging of original client ports.
Addresses with ports are expected to be in the RFC 3986 format,
that is, with IPv6 addresses in square brackets.  E.g.,
"X-Real-IP: [2001:0db8::1]:12345" sets client port ($remote_port)
to 12345.
This commit is contained in:
Dmitry Volyntsev 2016-05-23 18:44:23 +03:00
parent 19140c8c4f
commit 06176bce91
3 changed files with 84 additions and 1 deletions

View File

@ -525,6 +525,85 @@ ngx_parse_addr(ngx_pool_t *pool, ngx_addr_t *addr, u_char *text, size_t len)
}
ngx_int_t
ngx_parse_addr_port(ngx_pool_t *pool, ngx_addr_t *addr, u_char *text,
size_t len)
{
u_char *p, *last;
size_t plen;
ngx_int_t rc, port;
struct sockaddr_in *sin;
#if (NGX_HAVE_INET6)
struct sockaddr_in6 *sin6;
#endif
rc = ngx_parse_addr(pool, addr, text, len);
if (rc != NGX_DECLINED) {
return rc;
}
last = text + len;
#if (NGX_HAVE_INET6)
if (len && text[0] == '[') {
p = ngx_strlchr(text, last, ']');
if (p == NULL || p == last - 1 || *++p != ':') {
return NGX_DECLINED;
}
text++;
len -= 2;
} else
#endif
{
p = ngx_strlchr(text, last, ':');
if (p == NULL) {
return NGX_DECLINED;
}
}
p++;
plen = last - p;
port = ngx_atoi(p, plen);
if (port < 1 || port > 65535) {
return NGX_DECLINED;
}
len -= plen + 1;
rc = ngx_parse_addr(pool, addr, text, len);
if (rc != NGX_OK) {
return rc;
}
switch (addr->sockaddr->sa_family) {
#if (NGX_HAVE_INET6)
case AF_INET6:
sin6 = (struct sockaddr_in6 *) addr->sockaddr;
sin6->sin6_port = htons(port);
break;
#endif
default: /* AF_INET */
sin = (struct sockaddr_in *) addr->sockaddr;
sin->sin_port = htons(port);
break;
}
return NGX_OK;
}
ngx_int_t
ngx_parse_url(ngx_pool_t *pool, ngx_url_t *u)
{

View File

@ -115,6 +115,8 @@ size_t ngx_inet_ntop(int family, void *addr, u_char *text, size_t len);
ngx_int_t ngx_ptocidr(ngx_str_t *text, ngx_cidr_t *cidr);
ngx_int_t ngx_parse_addr(ngx_pool_t *pool, ngx_addr_t *addr, u_char *text,
size_t len);
ngx_int_t ngx_parse_addr_port(ngx_pool_t *pool, ngx_addr_t *addr,
u_char *text, size_t len);
ngx_int_t ngx_parse_url(ngx_pool_t *pool, ngx_url_t *u);
ngx_int_t ngx_inet_resolve_host(ngx_pool_t *pool, ngx_url_t *u);
ngx_int_t ngx_cmp_sockaddr(struct sockaddr *sa1, socklen_t slen1,

View File

@ -2910,7 +2910,9 @@ ngx_http_get_forwarded_addr_internal(ngx_http_request_t *r, ngx_addr_t *addr,
}
}
if (ngx_parse_addr(r->pool, &paddr, p, xfflen - (p - xff)) != NGX_OK) {
if (ngx_parse_addr_port(r->pool, &paddr, p, xfflen - (p - xff))
!= NGX_OK)
{
return NGX_DECLINED;
}