How can i get ip and port under forward proxy

honwel nginx-forum at nginx.us
Thu Jun 6 01:10:10 UTC 2013


Ok,  code is as follow:

Code:
src/event/ngx_event_connect.c       In ngx_event_connect_peer()

....................................
rc = connect(s, pc->sockaddr, pc->socklen);

    if (rc == -1) {
        err = ngx_socket_errno;


        if (err != NGX_EINPROGRESS
#if (NGX_WIN32)
            /* Winsock returns WSAEWOULDBLOCK (NGX_EAGAIN) */
            && err != NGX_EAGAIN
#endif
            )
        {
            if (err == NGX_ECONNREFUSED
#if (NGX_LINUX)
                /*
                 * Linux returns EAGAIN instead of ECONNREFUSED
                 * for unix sockets if listen queue is full
                 */
                || err == NGX_EAGAIN
#endif
                || err == NGX_ECONNRESET
                || err == NGX_ENETDOWN
                || err == NGX_ENETUNREACH
                || err == NGX_EHOSTDOWN
                || err == NGX_EHOSTUNREACH)
            {
                level = NGX_LOG_ERR;
} else {
                level = NGX_LOG_CRIT;
            }

            ngx_log_error(level, c->log, err, "connect() to %V failed",
                          pc->name);

            ngx_close_connection(c);
            pc->connection = NULL;

            return NGX_DECLINED;
        }
    }

    pc->local_socket = s;                       /* save socket fd  after
connect()   */



Code:
src/http/ngx_http_upstream.c        In ngx_http_upstream_process_header()

    ssize_t            n;
    ngx_int_t          rc;
    ngx_connection_t  *c;

    ngx_peer_connection_t *pc;
    u_char            sa[NGX_SOCKADDRLEN];
    socklen_t         len;
    u_char            text[NGX_SOCKADDR_STRLEN];
    u_char            *p;
    struct sockaddr_in *sin;


    pc = &u->peer;

    c = u->peer.connection;

    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
                   "http upstream process header");

    c->log->action = "reading response header from upstream";

    if (c->read->timedout) {
        ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_TIMEOUT);
        return;
    }

    if (!u->request_sent && ngx_http_upstream_test_connect(c) != NGX_OK) {
        ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR);
        return;
    }
    .........................................

    /* getsockname by pc->local_socket which saved in
ngx_event_connect_peer()*/

    if (pc == NULL) {
        return;
    }
    if (pc->local_socket) {
        if (getsockname(pc->local_socket, (struct sockaddr *) &sa, &len) !=
-1) {
            if (pc->local_sockaddr != NULL) {
                ngx_memcpy(pc->local_sockaddr, &sa, len);

                ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                                "http upstream connect socket: %i",
                                pc->local_socket);
                ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                                "http upstream connect sa_family: %i",
                                pc->local_sockaddr->sa_family);
                ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                                "http upstream connect uri: %s",
r->uri.data);
                sin = (struct sockaddr_in *) sa;
                p = (u_char *) &sin->sin_addr;
                p = ngx_snprintf(text, NGX_SOCKADDR_STRLEN,
"%ud.%ud.%ud.%ud:%d",
                             p[0], p[1], p[2], p[3], ntohs(sin->sin_port));
                ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                                "http upstream connect ip: %s", &text);
            }
        }
    }


Code:  src/http/ngx_http_upstream.c

static ngx_int_t
ngx_http_upstream_laddr_variable(ngx_http_request_t *r,
    ngx_http_variable_value_t *v, uintptr_t data)
{

    ngx_peer_connection_t   *pc;
    struct sockaddr_in      *sin;
    u_char                  sa[NGX_SOCKADDRLEN];
    socklen_t               len;
#if (NGX_HAVE_INET6)
    ngx_uint_t              i;
    struct sockaddr_in6     *sin6;
#endif

    ngx_str_t               s;
    u_char                  addr[NGX_SOCKADDR_STRLEN];

    s.len = NGX_SOCKADDR_STRLEN;
    s.data = addr;

    if (r->upstream == NULL) {
        return NGX_ERROR;
    }

    pc = &r->upstream->peer;
    if (pc == NULL){
        return NGX_ERROR;
    }
    if (pc->local_sockaddr == NULL) {
        return NGX_ERROR;

    s.len = ngx_sock_ntop(pc->local_sockaddr, s.data, s.len, 1);

    s.data = ngx_pnalloc(r->pool, s.len);
    if (s.data == NULL) {
        return NGX_ERROR;
    }


    ngx_memcpy(s.data, addr, s.len);

    v->len = s.len;
    v->valid = 1;
    v->no_cacheable = 0;
    v->not_found = 0;
    v->data = s.data;

    return NGX_OK;
}

Posted at Nginx Forum: http://forum.nginx.org/read.php?2,239883,239903#msg-239903



More information about the nginx mailing list