Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ngx_proxy_protocol_read() for proxy_protocol #1640

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 19 additions & 76 deletions ngx_rtmp_proxy_protocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,10 @@ ngx_rtmp_proxy_protocol(ngx_rtmp_session_t *s)
static void
ngx_rtmp_proxy_protocol_recv(ngx_event_t *rev)
{
u_char buf[107], *p, *pp, *text;
size_t len;
u_char buf[NGX_PROXY_PROTOCOL_MAX_HEADER], *p, *text;
size_t len, size;
ssize_t n;
ngx_err_t err;
ngx_int_t i;
ngx_addr_t addr;
ngx_connection_t *c;
ngx_rtmp_session_t *s;
Expand Down Expand Up @@ -101,76 +100,29 @@ ngx_rtmp_proxy_protocol_recv(ngx_event_t *rev)
return;
}

p = buf;

if (n <= 8 && ngx_strncmp(p, "PROXY ", 6) != 0) {
goto bad_header;
}

n -= 6;
p += 6;

ngx_memzero(&addr, sizeof(ngx_addr_t));

if (n >= 7 && ngx_strncmp(p, "UNKNOWN", 7) == 0) {
n -= 7;
p += 7;
goto skip;
}

if (n < 5 || ngx_strncmp(p, "TCP", 3) != 0
|| (p[3] != '4' && p[3] != '6') || p[4] != ' ')
{
goto bad_header;
}

n -= 5;
p += 5;

pp = ngx_strlchr(p, p + n, ' ');

if (pp == NULL) {
goto bad_header;
}

if (ngx_parse_addr(s->connection->pool, &addr, p, pp - p) != NGX_OK) {
goto bad_header;
}

n -= pp - p;
p = pp;

skip:

for (i = 0; i + 1 < n; i++) {
if (p[i] == CR && p[i + 1] == LF) {
break;
p = ngx_proxy_protocol_read(c, buf, buf + n);
if (p) {
size = p - buf;
if (c->recv(c, buf, size) != (ssize_t) size) {
goto failed;
}
if (ngx_parse_addr(c->pool, &addr, c->proxy_protocol->src_addr.data,
c->proxy_protocol->src_addr.len) != NGX_OK) {
goto failed;
}
}

if (i + 1 >= n) {
goto bad_header;
}

n = p - buf + i + 2;

if (c->recv(c, buf, n) != n) {
goto failed;
}

if (addr.socklen) {
text = ngx_palloc(s->connection->pool, NGX_SOCKADDR_STRLEN);
ngx_inet_set_port(addr.sockaddr, c->proxy_protocol->src_port);

if (text == NULL) {
text = ngx_pnalloc(c->pool, NGX_SOCKADDR_STRLEN);
len = ngx_sock_ntop(addr.sockaddr, addr.socklen, text,
NGX_SOCKADDR_STRLEN, 0);
if (len == 0) {
goto failed;
}

len = ngx_sock_ntop(addr.sockaddr,
#if (nginx_version >= 1005003)
addr.socklen,
#endif
text, NGX_SOCKADDR_STRLEN, 0);
if (len == 0) {
if (text == NULL) {
goto failed;
}

Expand All @@ -179,19 +131,10 @@ ngx_rtmp_proxy_protocol_recv(ngx_event_t *rev)
c->addr_text.data = text;
c->addr_text.len = len;

ngx_log_debug1(NGX_LOG_DEBUG_RTMP, c->log, 0,
"proxy_protocol: remote_addr:'%V'", &c->addr_text);
ngx_rtmp_handshake(s);
return;
}

ngx_rtmp_handshake(s);

return;

bad_header:

ngx_log_error(NGX_LOG_INFO, c->log, 0, "proxy_protocol: bad header");

failed:

ngx_rtmp_finalize_session(s);
}