From ab14585a81138e4e399aca7c89bf89c920130c05 Mon Sep 17 00:00:00 2001 From: ziyue <1213642868@qq.com> Date: Wed, 22 Dec 2021 22:06:19 +0800 Subject: [PATCH] =?UTF-8?q?Rtmp:=20rtmp=E5=AE=A2=E6=88=B7=E7=AB=AF?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=A4=8D=E6=9D=82=E6=8F=A1=E6=89=8B=EF=BC=8C?= =?UTF-8?q?=E5=85=BC=E5=AE=B9=E6=9F=90=E4=BA=9Brtmp=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Rtmp/Rtmp.h | 3 +++ src/Rtmp/RtmpProtocol.cpp | 20 +++++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/Rtmp/Rtmp.h b/src/Rtmp/Rtmp.h index 0d064e5f..e197efe8 100644 --- a/src/Rtmp/Rtmp.h +++ b/src/Rtmp/Rtmp.h @@ -112,6 +112,9 @@ public: bytes[i] = cdata[rand() % (sizeof(cdata) - 1)]; } } + + void create_complex_c0c1(); + }PACKED; class RtmpHeader { diff --git a/src/Rtmp/RtmpProtocol.cpp b/src/Rtmp/RtmpProtocol.cpp index 3b0c28c6..686b389b 100644 --- a/src/Rtmp/RtmpProtocol.cpp +++ b/src/Rtmp/RtmpProtocol.cpp @@ -290,6 +290,7 @@ void RtmpProtocol::startClientSession(const function &func) { char handshake_head = HANDSHAKE_PLAINTEXT; onSendRawData(obtainBuffer(&handshake_head, 1)); RtmpHandshake c1(0); + c1.create_complex_c0c1(); onSendRawData(obtainBuffer((char *) (&c1), sizeof(c1))); _next_step_func = [this, func](const char *data, size_t len) { //等待 S0+S1+S2 @@ -429,6 +430,22 @@ static u_int8_t FPKey[] = { 0x93, 0xB8, 0xE6, 0x36, 0xCF, 0xEB, 0x31, 0xAE }; // 62 +//发送复杂握手c0c1 +void RtmpHandshake::create_complex_c0c1() { + memcpy(zero, "\x80\x00\x07\x02", 4); + string str((char *) this, sizeof(*this)); + auto offset_value = rand() % (C1_SCHEMA_SIZE - C1_OFFSET_SIZE - C1_DIGEST_SIZE); + str.erase(8 + C1_SCHEMA_SIZE + C1_OFFSET_SIZE + offset_value, C1_DIGEST_SIZE); + + auto offset_ptr = (uint8_t *) str.data() + 8 + C1_SCHEMA_SIZE; + offset_ptr[0] = offset_ptr[1] = offset_ptr[2] = offset_value / 4; + offset_ptr[3] = offset_value - 3 * (offset_value / 4); + + auto digest_value = openssl_HMACsha256(FPKey, C1_FPKEY_SIZE, str.data(), str.size()); + str.insert(8 + C1_SCHEMA_SIZE + C1_OFFSET_SIZE + offset_value, digest_value); + memcpy(this, str.data(), sizeof(*this)); +} + void RtmpProtocol::check_C1_Digest(const string &digest,const string &data){ auto sha256 = openssl_HMACsha256(FPKey, C1_FPKEY_SIZE, data.data(), data.size()); if (sha256 != digest) { @@ -692,7 +709,8 @@ void RtmpProtocol::handle_chunk(RtmpPacket::Ptr packet) { case CONTROL_STREAM_BEGIN: { //开始播放 if (chunk_data.buffer.size() < 4) { - throw std::runtime_error("CONTROL_STREAM_BEGIN: Not enough data."); + WarnL << "CONTROL_STREAM_BEGIN: Not enough data:" << chunk_data.buffer.size(); + break; } uint32_t stream_index = load_be32(&chunk_data.buffer[0]); onStreamBegin(stream_index);