From 88df9a2497e215dffb2d576086bc30a0c3200ef6 Mon Sep 17 00:00:00 2001 From: winlin Date: Fri, 4 Oct 2019 19:03:34 +0800 Subject: [PATCH] Support go-oryx rtmplb with proxy protocol. 3.0.56 --- README.md | 1 + trunk/src/app/srs_app_rtmp_conn.cpp | 6 +++ trunk/src/core/srs_core.hpp | 2 +- trunk/src/kernel/srs_kernel_error.hpp | 1 + trunk/src/protocol/srs_protocol_utility.cpp | 1 - trunk/src/protocol/srs_rtmp_stack.cpp | 43 ++++++++++++++++++--- trunk/src/protocol/srs_rtmp_stack.hpp | 8 ++++ 7 files changed, 54 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 68e26bc14..290f2e2b5 100755 --- a/README.md +++ b/README.md @@ -158,6 +158,7 @@ Please select according to languages: ### V3 changes +* v3.0, 2019-10-04, Support go-oryx rtmplb with [proxy protocol](https://github.com/ossrs/go-oryx/wiki/RtmpProxy). 3.0.56 * v3.0, 2019-10-03, Fix [#775][bug #775], Support SO_REUSEPORT to improve edge performance. 3.0.54 * v3.0, 2019-10-03, Remove KAFKA. 3.0.53 * v3.0, 2019-05-14, Covert Kernel File reader/writer. 3.0.52 diff --git a/trunk/src/app/srs_app_rtmp_conn.cpp b/trunk/src/app/srs_app_rtmp_conn.cpp index 5ca3b54f2..e1dabc1c3 100644 --- a/trunk/src/app/srs_app_rtmp_conn.cpp +++ b/trunk/src/app/srs_app_rtmp_conn.cpp @@ -159,6 +159,12 @@ srs_error_t SrsRtmpConn::do_cycle() if ((err = rtmp->handshake()) != srs_success) { return srs_error_wrap(err, "rtmp handshake"); } + + uint32_t rip = rtmp->proxy_real_ip(); + if (rip > 0) { + srs_trace("RTMP proxy real client ip=%d.%d.%d.%d", + uint8_t(rip>>24), uint8_t(rip>>16), uint8_t(rip>>8), uint8_t(rip)); + } SrsRequest* req = info->req; if ((err = rtmp->connect_app(req)) != srs_success) { diff --git a/trunk/src/core/srs_core.hpp b/trunk/src/core/srs_core.hpp index cbc9f6061..5f020c54b 100644 --- a/trunk/src/core/srs_core.hpp +++ b/trunk/src/core/srs_core.hpp @@ -27,7 +27,7 @@ // The version config. #define VERSION_MAJOR 3 #define VERSION_MINOR 0 -#define VERSION_REVISION 55 +#define VERSION_REVISION 56 // The macros generated by configure script. #include diff --git a/trunk/src/kernel/srs_kernel_error.hpp b/trunk/src/kernel/srs_kernel_error.hpp index 88672030f..a3e56e19f 100644 --- a/trunk/src/kernel/srs_kernel_error.hpp +++ b/trunk/src/kernel/srs_kernel_error.hpp @@ -176,6 +176,7 @@ #define ERROR_RTMP_STREAM_NAME_EMPTY 2051 #define ERROR_HTTP_HIJACK 2052 #define ERROR_RTMP_MESSAGE_CREATE 2053 +#define ERROR_RTMP_PROXY_EXCEED 2054 // // The system control message, // It's not an error, but special control logic. diff --git a/trunk/src/protocol/srs_protocol_utility.cpp b/trunk/src/protocol/srs_protocol_utility.cpp index af2e0eab3..57a369dff 100644 --- a/trunk/src/protocol/srs_protocol_utility.cpp +++ b/trunk/src/protocol/srs_protocol_utility.cpp @@ -151,7 +151,6 @@ void srs_random_generate(char* bytes, int size) if (!_random_initialized) { srand(0); _random_initialized = true; - srs_trace("srand initialized the random."); } for (int i = 0; i < size; i++) { diff --git a/trunk/src/protocol/srs_rtmp_stack.cpp b/trunk/src/protocol/srs_rtmp_stack.cpp index af9850a21..5d2387aef 100644 --- a/trunk/src/protocol/srs_rtmp_stack.cpp +++ b/trunk/src/protocol/srs_rtmp_stack.cpp @@ -1659,9 +1659,15 @@ bool srs_client_type_is_publish(SrsRtmpConnType type) SrsHandshakeBytes::SrsHandshakeBytes() { c0c1 = s0s1s2 = c2 = NULL; + proxy_real_ip = 0; } SrsHandshakeBytes::~SrsHandshakeBytes() +{ + dispose(); +} + +void SrsHandshakeBytes::dispose() { srs_freepa(c0c1); srs_freepa(s0s1s2); @@ -1682,6 +1688,26 @@ srs_error_t SrsHandshakeBytes::read_c0c1(ISrsProtocolReadWriter* io) if ((err = io->read_fully(c0c1, 1537, &nsize)) != srs_success) { return srs_error_wrap(err, "read c0c1"); } + + // Whether RTMP proxy, @see https://github.com/ossrs/go-oryx/wiki/RtmpProxy + if (uint8_t(c0c1[0]) == 0xF3) { + uint16_t nn = uint16_t(c0c1[1])<<8 | uint16_t(c0c1[2]); + ssize_t nn_consumed = 3 + nn; + if (nn > 1024) { + return srs_error_new(ERROR_RTMP_PROXY_EXCEED, "proxy exceed max size, nn=%d", nn); + } + + // 4B client real IP. + if (nn >= 4) { + proxy_real_ip = uint32_t(c0c1[3])<<24 | uint32_t(c0c1[4])<<16 | uint32_t(c0c1[5])<<8 | uint32_t(c0c1[6]); + nn -= 4; + } + + memmove(c0c1, c0c1 + nn_consumed, 1537 - nn_consumed); + if ((err = io->read_fully(c0c1 + 1537 - nn_consumed, nn_consumed, &nsize)) != srs_success) { + return srs_error_wrap(err, "read c0c1"); + } + } return err; } @@ -1888,7 +1914,7 @@ srs_error_t SrsRtmpClient::handshake() } } - srs_freep(hs_bytes); + hs_bytes->dispose(); return err; } @@ -1904,7 +1930,7 @@ srs_error_t SrsRtmpClient::simple_handshake() return srs_error_wrap(err, "simple handshake"); } - srs_freep(hs_bytes); + hs_bytes->dispose(); return err; } @@ -1920,7 +1946,7 @@ srs_error_t SrsRtmpClient::complex_handshake() return srs_error_wrap(err, "complex handshake"); } - srs_freep(hs_bytes); + hs_bytes->dispose(); return err; } @@ -2193,6 +2219,11 @@ SrsRtmpServer::~SrsRtmpServer() srs_freep(hs_bytes); } +uint32_t SrsRtmpServer::proxy_real_ip() +{ + return hs_bytes->proxy_real_ip; +} + void SrsRtmpServer::set_auto_response(bool v) { protocol->set_auto_response(v); @@ -2284,9 +2315,9 @@ srs_error_t SrsRtmpServer::handshake() return srs_error_wrap(err, "complex handshake"); } } - - srs_freep(hs_bytes); - + + hs_bytes->dispose(); + return err; } diff --git a/trunk/src/protocol/srs_rtmp_stack.hpp b/trunk/src/protocol/srs_rtmp_stack.hpp index 0159485fa..db8dce3db 100644 --- a/trunk/src/protocol/srs_rtmp_stack.hpp +++ b/trunk/src/protocol/srs_rtmp_stack.hpp @@ -500,6 +500,8 @@ bool srs_client_type_is_publish(SrsRtmpConnType type); class SrsHandshakeBytes { public: + // For RTMP proxy, the real IP. + uint32_t proxy_real_ip; // [1+1536] char* c0c1; // [1+1536+1536] @@ -509,6 +511,8 @@ public: public: SrsHandshakeBytes(); virtual ~SrsHandshakeBytes(); +public: + virtual void dispose(); public: virtual srs_error_t read_c0c1(ISrsProtocolReadWriter* io); virtual srs_error_t read_s0s1s2(ISrsProtocolReadWriter* io); @@ -615,6 +619,10 @@ private: public: SrsRtmpServer(ISrsProtocolReadWriter* skt); virtual ~SrsRtmpServer(); +public: + // For RTMP proxy, the real IP. 0 if no proxy. + // @doc https://github.com/ossrs/go-oryx/wiki/RtmpProxy + virtual uint32_t proxy_real_ip(); // Protocol methods proxy public: // Set the auto response message when recv for protocol stack.