mirror of
https://github.com/nginx/nginx.git
synced 2024-11-25 06:26:48 +08:00
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:
parent
19140c8c4f
commit
06176bce91
@ -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)
|
||||
{
|
||||
|
@ -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,
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user