fix: 握手协议

This commit is contained in:
dexter
2022-10-29 16:55:04 +08:00
parent 9357e8799f
commit c25b86e483

View File

@@ -9,6 +9,9 @@ import (
"io" "io"
"math/rand" "math/rand"
"net" "net"
"time"
"m7s.live/engine/v4/util"
) )
const ( const (
@@ -70,20 +73,18 @@ func ReadBuf(r io.Reader, length int) (buf []byte) {
} }
func (nc *NetConnection) Handshake() error { func (nc *NetConnection) Handshake() error {
C0C1 := ReadBuf(nc.Reader, 1536+1) C0C1 := ReadBuf(nc.Reader, C1S1_SIZE+1)
if C0C1[0] != RTMP_HANDSHAKE_VERSION { if C0C1[0] != RTMP_HANDSHAKE_VERSION {
return errors.New("C0 Error") return errors.New("C0 Error")
} }
var C1 = C0C1[1:]
if len(C0C1[1:]) != 1536 { if len(C1) != C1S1_SIZE {
return errors.New("C1 Error") return errors.New("C1 Error")
} }
var ts int
util.GetBE(C1[4:8], &ts)
C1 := make([]byte, 1536) if ts == 0 {
copy(C1, C0C1[1:])
temp := C1[4] & 0xff
if temp == 0 {
return nc.simple_handshake(C1) return nc.simple_handshake(C1)
} }
@@ -91,10 +92,10 @@ func (nc *NetConnection) Handshake() error {
} }
func (client *NetConnection) ClientHandshake() error { func (client *NetConnection) ClientHandshake() error {
C0C1 := make([]byte, 1536+1) C0C1 := make([]byte, C1S1_SIZE+1)
C0C1[0] = RTMP_HANDSHAKE_VERSION C0C1[0] = RTMP_HANDSHAKE_VERSION
client.Write(C0C1) client.Write(C0C1)
S1C1 := ReadBuf(client.Reader, 1536+1536+1) S1C1 := ReadBuf(client.Reader, C1S1_SIZE+C1S1_SIZE+1)
if S1C1[0] != RTMP_HANDSHAKE_VERSION { if S1C1[0] != RTMP_HANDSHAKE_VERSION {
return errors.New("S1 C1 Error") return errors.New("S1 C1 Error")
} }
@@ -104,12 +105,15 @@ func (client *NetConnection) ClientHandshake() error {
} }
func (nc *NetConnection) simple_handshake(C1 []byte) error { func (nc *NetConnection) simple_handshake(C1 []byte) error {
S1 := make([]byte, 1536+1) S0S1 := make([]byte, C1S1_SIZE+1)
S1[0] = RTMP_HANDSHAKE_VERSION S0S1[0] = RTMP_HANDSHAKE_VERSION
buffer := net.Buffers{S1, C1} util.PutBE(S0S1[1:5], time.Now().Unix()&0xFFFFFFFF)
buffer.WriteTo(nc) copy(S0S1[5:], "Monibuca")
if C2 := ReadBuf(nc.Reader, 1536); bytes.Compare(C2, S1[1:]) != 0 { nc.Write(S0S1)
if C2 := ReadBuf(nc.Reader, C1S1_SIZE); bytes.Compare(C2[8:], S0S1[9:]) != 0 {
return errors.New("C2 Error") return errors.New("C2 Error")
} else {
nc.Write(C2) //S2
} }
return nil return nil
} }