Skip to content

Commit

Permalink
Added the $realip_remote_port variable.
Browse files Browse the repository at this point in the history
  • Loading branch information
xeioex committed May 23, 2016
1 parent f56cf3d commit 97495b6
Showing 1 changed file with 71 additions and 4 deletions.
75 changes: 71 additions & 4 deletions src/http/modules/ngx_http_realip_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,14 @@ static char *ngx_http_realip_merge_loc_conf(ngx_conf_t *cf,
void *parent, void *child);
static ngx_int_t ngx_http_realip_add_variables(ngx_conf_t *cf);
static ngx_int_t ngx_http_realip_init(ngx_conf_t *cf);
static ngx_http_realip_ctx_t *ngx_http_realip_get_module_ctx(
ngx_http_request_t *r);


static ngx_int_t ngx_http_realip_remote_addr_variable(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_http_realip_remote_port_variable(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data);


static ngx_command_t ngx_http_realip_commands[] = {
Expand Down Expand Up @@ -115,6 +119,9 @@ static ngx_http_variable_t ngx_http_realip_vars[] = {
{ ngx_string("realip_remote_addr"), NULL,
ngx_http_realip_remote_addr_variable, 0, 0, 0 },

{ ngx_string("realip_remote_port"), NULL,
ngx_http_realip_remote_port_variable, 0, 0, 0 },

{ ngx_null_string, NULL, NULL, 0, 0, 0 }
};

Expand Down Expand Up @@ -475,11 +482,9 @@ ngx_http_realip_init(ngx_conf_t *cf)
}


static ngx_int_t
ngx_http_realip_remote_addr_variable(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data)
static ngx_http_realip_ctx_t *
ngx_http_realip_get_module_ctx(ngx_http_request_t *r)
{
ngx_str_t *addr_text;
ngx_pool_cleanup_t *cln;
ngx_http_realip_ctx_t *ctx;

Expand All @@ -500,6 +505,19 @@ ngx_http_realip_remote_addr_variable(ngx_http_request_t *r,
}
}

return ctx;
}


static ngx_int_t
ngx_http_realip_remote_addr_variable(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data)
{
ngx_str_t *addr_text;
ngx_http_realip_ctx_t *ctx;

ctx = ngx_http_realip_get_module_ctx(r);

addr_text = ctx ? &ctx->addr_text : &r->connection->addr_text;

v->len = addr_text->len;
Expand All @@ -510,3 +528,52 @@ ngx_http_realip_remote_addr_variable(ngx_http_request_t *r,

return NGX_OK;
}


static ngx_int_t
ngx_http_realip_remote_port_variable(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data)
{
ngx_uint_t port;
struct sockaddr *sa;
ngx_http_realip_ctx_t *ctx;

ctx = ngx_http_realip_get_module_ctx(r);

sa = ctx ? ctx->sockaddr : r->connection->sockaddr;

v->len = 0;
v->valid = 1;
v->no_cacheable = 0;
v->not_found = 0;

v->data = ngx_pnalloc(r->pool, sizeof("65535") - 1);
if (v->data == NULL) {
return NGX_ERROR;
}

switch (sa->sa_family) {

#if (NGX_HAVE_INET6)
case AF_INET6:
port = ntohs(((struct sockaddr_in6 *) sa)->sin6_port);
break;
#endif

#if (NGX_HAVE_UNIX_DOMAIN)
case AF_UNIX:
port = 0;
break;
#endif

default: /* AF_INET */
port = ntohs(((struct sockaddr_in *) sa)->sin_port);
break;
}

if (port > 0 && port < 65536) {
v->len = ngx_sprintf(v->data, "%ui", port) - v->data;
}

return NGX_OK;
}

0 comments on commit 97495b6

Please sign in to comment.