|
| 1 | + |
| 2 | +/* |
| 3 | + * Copyright (C) Roman Arutyunyan |
| 4 | + */ |
| 5 | + |
| 6 | + |
| 7 | +#include <ngx_config.h> |
| 8 | +#include <ngx_core.h> |
| 9 | +#include "ngx_rtmp_proxy_protocol.h" |
| 10 | + |
| 11 | + |
| 12 | +static void ngx_rtmp_proxy_protocol_recv(ngx_event_t *rev); |
| 13 | + |
| 14 | + |
| 15 | +void |
| 16 | +ngx_rtmp_proxy_protocol(ngx_rtmp_session_t *s) |
| 17 | +{ |
| 18 | + ngx_connection_t *c; |
| 19 | + |
| 20 | + c = s->connection; |
| 21 | + c->read->handler = ngx_rtmp_proxy_protocol_recv; |
| 22 | + |
| 23 | + ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0, |
| 24 | + "proxy_protocol: start"); |
| 25 | + |
| 26 | + ngx_rtmp_proxy_protocol_recv(c->read); |
| 27 | +} |
| 28 | + |
| 29 | + |
| 30 | +static void |
| 31 | +ngx_rtmp_proxy_protocol_recv(ngx_event_t *rev) |
| 32 | +{ |
| 33 | + u_char buf[107], *p, *pp, *text; |
| 34 | + size_t len; |
| 35 | + ssize_t n; |
| 36 | + ngx_err_t err; |
| 37 | + ngx_int_t i; |
| 38 | + ngx_addr_t addr; |
| 39 | + ngx_connection_t *c; |
| 40 | + ngx_rtmp_session_t *s; |
| 41 | + |
| 42 | + c = rev->data; |
| 43 | + s = c->data; |
| 44 | + |
| 45 | + if (c->destroyed) { |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + if (rev->timedout) { |
| 50 | + ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, |
| 51 | + "proxy_protocol: recv: client timed out"); |
| 52 | + c->timedout = 1; |
| 53 | + ngx_rtmp_finalize_session(s); |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + if (rev->timer_set) { |
| 58 | + ngx_del_timer(rev); |
| 59 | + } |
| 60 | + |
| 61 | + n = recv(c->fd, buf, sizeof(buf), MSG_PEEK); |
| 62 | + |
| 63 | + err = ngx_socket_errno; |
| 64 | + |
| 65 | + if (n == -1) { |
| 66 | + |
| 67 | + if (err == NGX_EAGAIN) { |
| 68 | + ngx_add_timer(rev, s->timeout); |
| 69 | + |
| 70 | + if (ngx_handle_read_event(c->read, 0) != NGX_OK) { |
| 71 | + ngx_rtmp_finalize_session(s); |
| 72 | + } |
| 73 | + |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + ngx_rtmp_finalize_session(s); |
| 78 | + |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + p = buf; |
| 83 | + |
| 84 | + if (n <= 8 && ngx_strncmp(p, "PROXY ", 6) != 0) { |
| 85 | + goto bad_header; |
| 86 | + } |
| 87 | + |
| 88 | + n -= 6; |
| 89 | + p += 6; |
| 90 | + |
| 91 | + ngx_memzero(&addr, sizeof(ngx_addr_t)); |
| 92 | + |
| 93 | + if (n >= 7 && ngx_strncmp(p, "UNKNOWN", 7) == 0) { |
| 94 | + n -= 7; |
| 95 | + p += 7; |
| 96 | + goto skip; |
| 97 | + } |
| 98 | + |
| 99 | + if (n < 5 || ngx_strncmp(p, "TCP", 3) != 0 |
| 100 | + || (p[3] != '4' && p[3] != '6') || p[4] != ' ') |
| 101 | + { |
| 102 | + goto bad_header; |
| 103 | + } |
| 104 | + |
| 105 | + n -= 5; |
| 106 | + p += 5; |
| 107 | + |
| 108 | + pp = ngx_strlchr(p, p + n, ' '); |
| 109 | + |
| 110 | + if (pp == NULL) { |
| 111 | + goto bad_header; |
| 112 | + } |
| 113 | + |
| 114 | + if (ngx_parse_addr(s->connection->pool, &addr, p, pp - p) != NGX_OK) { |
| 115 | + goto bad_header; |
| 116 | + } |
| 117 | + |
| 118 | + n -= pp - p; |
| 119 | + p = pp; |
| 120 | + |
| 121 | +skip: |
| 122 | + |
| 123 | + for (i = 0; i + 1 < n; i++) { |
| 124 | + if (p[i] == CR && p[i + 1] == LF) { |
| 125 | + break; |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + if (i + 1 == n) { |
| 130 | + goto bad_header; |
| 131 | + } |
| 132 | + |
| 133 | + n = p - buf + i + 2; |
| 134 | + |
| 135 | + if (c->recv(c, buf, n) != n) { |
| 136 | + goto failed; |
| 137 | + } |
| 138 | + |
| 139 | + if (addr.socklen) { |
| 140 | + text = ngx_palloc(s->connection->pool, NGX_SOCKADDR_STRLEN); |
| 141 | + |
| 142 | + if (text == NULL) { |
| 143 | + goto failed; |
| 144 | + } |
| 145 | + |
| 146 | + len = ngx_sock_ntop(addr.sockaddr, addr.socklen, text, |
| 147 | + NGX_SOCKADDR_STRLEN, 0); |
| 148 | + if (len == 0) { |
| 149 | + goto failed; |
| 150 | + } |
| 151 | + |
| 152 | + c->sockaddr = addr.sockaddr; |
| 153 | + c->socklen = addr.socklen; |
| 154 | + c->addr_text.data = text; |
| 155 | + c->addr_text.len = len; |
| 156 | + |
| 157 | + ngx_log_debug1(NGX_LOG_DEBUG_RTMP, c->log, 0, |
| 158 | + "proxy_protocol: remote_addr:'%V'", &c->addr_text); |
| 159 | + } |
| 160 | + |
| 161 | + ngx_rtmp_handshake(s); |
| 162 | + |
| 163 | + return; |
| 164 | + |
| 165 | +bad_header: |
| 166 | + |
| 167 | + ngx_log_error(NGX_LOG_INFO, c->log, 0, "proxy_protocol: bad header"); |
| 168 | + |
| 169 | +failed: |
| 170 | + |
| 171 | + ngx_rtmp_finalize_session(s); |
| 172 | +} |
0 commit comments