Rtmp: rtmp客户端支持复杂握手,兼容某些rtmp服务器

This commit is contained in:
ziyue 2021-12-22 22:06:19 +08:00
parent 2e9ff3ed3c
commit ab14585a81
2 changed files with 22 additions and 1 deletions

View File

@ -112,6 +112,9 @@ public:
bytes[i] = cdata[rand() % (sizeof(cdata) - 1)]; bytes[i] = cdata[rand() % (sizeof(cdata) - 1)];
} }
} }
void create_complex_c0c1();
}PACKED; }PACKED;
class RtmpHeader { class RtmpHeader {

View File

@ -290,6 +290,7 @@ void RtmpProtocol::startClientSession(const function<void()> &func) {
char handshake_head = HANDSHAKE_PLAINTEXT; char handshake_head = HANDSHAKE_PLAINTEXT;
onSendRawData(obtainBuffer(&handshake_head, 1)); onSendRawData(obtainBuffer(&handshake_head, 1));
RtmpHandshake c1(0); RtmpHandshake c1(0);
c1.create_complex_c0c1();
onSendRawData(obtainBuffer((char *) (&c1), sizeof(c1))); onSendRawData(obtainBuffer((char *) (&c1), sizeof(c1)));
_next_step_func = [this, func](const char *data, size_t len) { _next_step_func = [this, func](const char *data, size_t len) {
//等待 S0+S1+S2 //等待 S0+S1+S2
@ -429,6 +430,22 @@ static u_int8_t FPKey[] = {
0x93, 0xB8, 0xE6, 0x36, 0xCF, 0xEB, 0x31, 0xAE 0x93, 0xB8, 0xE6, 0x36, 0xCF, 0xEB, 0x31, 0xAE
}; // 62 }; // 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){ void RtmpProtocol::check_C1_Digest(const string &digest,const string &data){
auto sha256 = openssl_HMACsha256(FPKey, C1_FPKEY_SIZE, data.data(), data.size()); auto sha256 = openssl_HMACsha256(FPKey, C1_FPKEY_SIZE, data.data(), data.size());
if (sha256 != digest) { if (sha256 != digest) {
@ -692,7 +709,8 @@ void RtmpProtocol::handle_chunk(RtmpPacket::Ptr packet) {
case CONTROL_STREAM_BEGIN: { case CONTROL_STREAM_BEGIN: {
//开始播放 //开始播放
if (chunk_data.buffer.size() < 4) { 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]); uint32_t stream_index = load_be32(&chunk_data.buffer[0]);
onStreamBegin(stream_index); onStreamBegin(stream_index);