2018-05-29 08:56:36 +03:00
|
|
|
/*
|
|
|
|
NAME
|
|
|
|
rtmp.go
|
|
|
|
|
|
|
|
DESCRIPTION
|
2019-01-10 16:49:50 +03:00
|
|
|
RTMP command functionality.
|
2018-05-29 08:56:36 +03:00
|
|
|
|
2018-08-24 03:55:36 +03:00
|
|
|
AUTHORS
|
2018-05-29 08:56:36 +03:00
|
|
|
Saxon Nelson-Milton <saxon@ausocean.org>
|
2019-01-15 07:29:30 +03:00
|
|
|
Dan Kortschak <dan@ausocean.org>
|
2019-01-07 08:50:35 +03:00
|
|
|
Alan Noble <alan@ausocean.org>
|
2018-05-29 08:56:36 +03:00
|
|
|
|
|
|
|
LICENSE
|
2019-01-07 08:50:35 +03:00
|
|
|
rtmp.go is Copyright (C) 2017-2019 the Australian Ocean Lab (AusOcean)
|
2018-05-29 08:56:36 +03:00
|
|
|
|
|
|
|
It is free software: you can redistribute it and/or modify them
|
|
|
|
under the terms of the GNU General Public License as published by the
|
|
|
|
Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
2018-08-30 14:01:19 +03:00
|
|
|
along with revid in gpl.txt. If not, see http://www.gnu.org/licenses.
|
2018-08-23 23:44:37 +03:00
|
|
|
|
2018-08-30 14:01:19 +03:00
|
|
|
Derived from librtmp under the GNU Lesser General Public License 2.1
|
|
|
|
Copyright (C) 2005-2008 Team XBMC http://www.xbmc.org
|
|
|
|
Copyright (C) 2008-2009 Andrej Stepanchuk
|
|
|
|
Copyright (C) 2009-2010 Howard Chu
|
2018-05-29 08:56:36 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
package rtmp
|
|
|
|
|
|
|
|
import (
|
2018-09-06 05:56:35 +03:00
|
|
|
"bytes"
|
2018-09-14 14:58:19 +03:00
|
|
|
"encoding/binary"
|
2018-07-16 06:30:58 +03:00
|
|
|
"errors"
|
2018-07-24 15:00:50 +03:00
|
|
|
"math/rand"
|
2019-01-06 07:12:51 +03:00
|
|
|
"net"
|
2018-07-14 08:29:00 +03:00
|
|
|
"strconv"
|
2018-08-11 05:51:22 +03:00
|
|
|
"time"
|
2019-01-11 23:43:27 +03:00
|
|
|
|
|
|
|
"bitbucket.org/ausocean/av/rtmp/amf"
|
2018-05-29 08:56:36 +03:00
|
|
|
)
|
|
|
|
|
2018-08-08 10:36:16 +03:00
|
|
|
const (
|
2019-01-11 02:22:21 +03:00
|
|
|
pkg = "rtmp:"
|
|
|
|
signatureSize = 1536
|
|
|
|
fullHeaderSize = 12
|
2018-07-13 18:43:17 +03:00
|
|
|
)
|
|
|
|
|
2019-01-11 02:22:21 +03:00
|
|
|
// Link flags.
|
2019-01-10 16:49:50 +03:00
|
|
|
const (
|
2019-01-11 02:22:21 +03:00
|
|
|
linkAuth = 0x0001 // using auth param
|
|
|
|
linkLive = 0x0002 // stream is live
|
|
|
|
linkSWF = 0x0004 // do SWF verification - not implemented
|
|
|
|
linkPlaylist = 0x0008 // send playlist before play - not implemented
|
|
|
|
linkBufx = 0x0010 // toggle stream on BufferEmpty msg - not implemented
|
2019-01-10 16:49:50 +03:00
|
|
|
)
|
|
|
|
|
2019-01-11 02:22:21 +03:00
|
|
|
// Protocol features.
|
2019-01-10 16:49:50 +03:00
|
|
|
const (
|
2019-01-11 02:22:21 +03:00
|
|
|
featureHTTP = 0x01 // not implemented
|
|
|
|
featureEncode = 0x02 // not implemented
|
|
|
|
featureSSL = 0x04 // not implemented
|
|
|
|
featureMFP = 0x08 // not implemented
|
|
|
|
featureWrite = 0x10 // publish, not play
|
|
|
|
featureHTTP2 = 0x20 // server-side RTMPT - not implemented
|
2019-01-10 16:49:50 +03:00
|
|
|
)
|
|
|
|
|
2019-01-11 02:22:21 +03:00
|
|
|
// RTMP protocols.
|
2019-01-10 16:49:50 +03:00
|
|
|
const (
|
2019-01-11 02:22:21 +03:00
|
|
|
protoRTMP = 0
|
|
|
|
protoRTMPE = featureEncode
|
|
|
|
protoRTMPT = featureHTTP
|
|
|
|
protoRTMPS = featureSSL
|
|
|
|
protoRTMPTE = (featureHTTP | featureEncode)
|
|
|
|
protoRTMPTS = (featureHTTP | featureSSL)
|
|
|
|
protoRTMFP = featureMFP
|
2019-01-10 16:49:50 +03:00
|
|
|
)
|
|
|
|
|
2019-01-11 02:22:21 +03:00
|
|
|
// RTMP tokens (lexemes).
|
|
|
|
// NB: Underscores are deliberately preserved in const names where they exist in the corresponding tokens.
|
2019-01-10 16:49:50 +03:00
|
|
|
const (
|
2019-01-11 02:22:21 +03:00
|
|
|
av_checkbw = "_checkbw"
|
|
|
|
av_onbwcheck = "_onbwcheck"
|
|
|
|
av_onbwdone = "_onbwdone"
|
|
|
|
av_result = "_result"
|
|
|
|
avApp = "app"
|
|
|
|
avAudioCodecs = "audioCodecs"
|
|
|
|
avCapabilities = "capabilities"
|
|
|
|
avClose = "close"
|
|
|
|
avCode = "code"
|
|
|
|
avConnect = "connect"
|
|
|
|
avCreatestream = "createStream"
|
|
|
|
avDeletestream = "deleteStream"
|
|
|
|
avFCPublish = "FCPublish"
|
|
|
|
avFCUnpublish = "FCUnpublish"
|
|
|
|
avFlashver = "flashVer"
|
|
|
|
avFpad = "fpad"
|
|
|
|
avLevel = "level"
|
|
|
|
avLive = "live"
|
|
|
|
avNetConnectionConnectInvalidApp = "NetConnection.Connect.InvalidApp"
|
|
|
|
avNetStreamFailed = "NetStream.Failed"
|
|
|
|
avNetStreamPauseNotify = "NetStream.Pause.Notify"
|
|
|
|
avNetStreamPlayComplete = "NetStream.Play.Complete"
|
|
|
|
avNetStreamPlayFailed = "NetStream.Play.Failed"
|
|
|
|
avNetStreamPlayPublishNotify = "NetStream.Play.PublishNotify"
|
|
|
|
avNetStreamPlayStart = "NetStream.Play.Start"
|
|
|
|
avNetStreamPlayStop = "NetStream.Play.Stop"
|
|
|
|
avNetStreamPlayStreamNotFound = "NetStream.Play.StreamNotFound"
|
|
|
|
avNetStreamPlayUnpublishNotify = "NetStream.Play.UnpublishNotify"
|
|
|
|
avNetStreamPublish_Start = "NetStream.Publish.Start"
|
|
|
|
avNetStreamSeekNotify = "NetStream.Seek.Notify"
|
|
|
|
avNonprivate = "nonprivate"
|
|
|
|
avObjectEncoding = "objectEncoding"
|
|
|
|
avOnBWDone = "onBWDone"
|
|
|
|
avOnFCSubscribe = "onFCSubscribe"
|
|
|
|
avOnFCUnsubscribe = "onFCUnsubscribe"
|
|
|
|
avOnStatus = "onStatus"
|
|
|
|
avPageUrl = "pageUrl"
|
|
|
|
avPing = "ping"
|
|
|
|
avPlay = "play"
|
|
|
|
avPlaylist_ready = "playlist_ready"
|
|
|
|
avPublish = "publish"
|
|
|
|
avReleasestream = "releaseStream"
|
|
|
|
avSecureToken = "secureToken"
|
|
|
|
avSet_playlist = "set_playlist"
|
|
|
|
avSwfUrl = "swfUrl"
|
|
|
|
avTcUrl = "tcUrl"
|
|
|
|
avType = "type"
|
|
|
|
avVideoCodecs = "videoCodecs"
|
|
|
|
avVideoFunction = "videoFunction"
|
2018-07-26 10:36:44 +03:00
|
|
|
)
|
2018-07-16 06:30:58 +03:00
|
|
|
|
2019-01-07 10:30:42 +03:00
|
|
|
// RTMP protocol strings.
|
|
|
|
var rtmpProtocolStrings = [...]string{
|
|
|
|
"rtmp",
|
|
|
|
"rtmpt",
|
|
|
|
"rtmpe",
|
|
|
|
"rtmpte",
|
|
|
|
"rtmps",
|
|
|
|
"rtmpts",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"rtmfp",
|
|
|
|
}
|
|
|
|
|
|
|
|
// RTMP errors.
|
2019-01-06 01:50:32 +03:00
|
|
|
var (
|
2019-01-06 04:36:16 +03:00
|
|
|
errUnknownScheme = errors.New("rtmp: unknown scheme")
|
2019-01-13 01:42:41 +03:00
|
|
|
errInvalidURL = errors.New("rtmp: invalid URL")
|
2019-01-07 16:29:41 +03:00
|
|
|
errConnected = errors.New("rtmp: already connected")
|
2019-01-07 10:30:42 +03:00
|
|
|
errNotConnected = errors.New("rtmp: not connected")
|
2019-01-10 16:18:11 +03:00
|
|
|
errNotWritable = errors.New("rtmp: connection not writable")
|
2019-01-06 07:12:51 +03:00
|
|
|
errInvalidHeader = errors.New("rtmp: invalid header")
|
|
|
|
errInvalidBody = errors.New("rtmp: invalid body")
|
2019-01-11 08:30:00 +03:00
|
|
|
errInvalidFlvTag = errors.New("rtmp: invalid FLV tag")
|
2019-01-10 15:22:43 +03:00
|
|
|
errUnimplemented = errors.New("rtmp: unimplemented feature")
|
2019-01-06 01:50:32 +03:00
|
|
|
)
|
|
|
|
|
2019-01-07 10:30:42 +03:00
|
|
|
// connect establishes an RTMP connection.
|
2019-01-19 05:42:24 +03:00
|
|
|
func connect(c *Conn) error {
|
|
|
|
addr, err := net.ResolveTCPAddr("tcp4", c.link.host+":"+strconv.Itoa(int(c.link.port)))
|
2019-01-06 07:12:51 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-01-19 05:42:24 +03:00
|
|
|
c.link.conn, err = net.DialTCP("tcp4", nil, addr)
|
2019-01-06 07:12:51 +03:00
|
|
|
if err != nil {
|
2019-01-19 05:42:24 +03:00
|
|
|
c.log(WarnLevel, pkg+"dial failed", "error", err.Error())
|
2019-01-06 07:12:51 +03:00
|
|
|
return err
|
|
|
|
}
|
2019-01-19 05:42:24 +03:00
|
|
|
c.log(DebugLevel, pkg+"connected")
|
|
|
|
err = handshake(c)
|
2019-01-06 07:12:51 +03:00
|
|
|
if err != nil {
|
2019-01-19 05:42:24 +03:00
|
|
|
c.log(WarnLevel, pkg+"handshake failed", "error", err.Error())
|
2019-01-13 01:42:41 +03:00
|
|
|
return err
|
2018-07-22 16:31:50 +03:00
|
|
|
}
|
2019-01-19 05:42:24 +03:00
|
|
|
c.log(DebugLevel, pkg+"handshaked")
|
|
|
|
err = sendConnectPacket(c)
|
2019-01-06 07:12:51 +03:00
|
|
|
if err != nil {
|
2019-01-19 05:42:24 +03:00
|
|
|
c.log(WarnLevel, pkg+"sendConnect failed", "error", err.Error())
|
2019-01-13 01:42:41 +03:00
|
|
|
return err
|
2018-07-22 16:31:50 +03:00
|
|
|
}
|
2019-01-19 05:42:24 +03:00
|
|
|
c.log(DebugLevel, pkg+"negotiating")
|
|
|
|
for !c.isPlaying {
|
2019-01-10 05:19:52 +03:00
|
|
|
pkt := packet{}
|
2019-01-19 05:42:24 +03:00
|
|
|
err = pkt.readFrom(c)
|
2019-01-06 07:12:51 +03:00
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
2019-01-07 10:30:42 +03:00
|
|
|
|
2019-01-10 16:31:26 +03:00
|
|
|
switch pkt.packetType {
|
2019-01-11 02:22:21 +03:00
|
|
|
case packetTypeAudio, packetTypeVideo, packetTypeInfo:
|
2019-01-19 05:42:24 +03:00
|
|
|
c.log(WarnLevel, pkg+"got packet before play; ignoring", "type", pkt.packetType)
|
2019-01-10 16:31:26 +03:00
|
|
|
default:
|
2019-01-19 05:42:24 +03:00
|
|
|
err = handlePacket(c, &pkt)
|
2019-01-10 16:40:29 +03:00
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
2019-01-10 05:19:52 +03:00
|
|
|
}
|
2018-08-24 12:20:04 +03:00
|
|
|
}
|
2019-01-06 01:50:32 +03:00
|
|
|
|
2019-01-19 05:42:24 +03:00
|
|
|
if !c.isPlaying {
|
2019-01-10 05:19:52 +03:00
|
|
|
return err
|
2019-01-06 01:50:32 +03:00
|
|
|
}
|
|
|
|
return nil
|
2018-08-24 12:20:04 +03:00
|
|
|
}
|
|
|
|
|
2019-01-07 10:30:42 +03:00
|
|
|
// handlePacket handles a packet that the client has received.
|
2019-01-13 02:17:04 +03:00
|
|
|
// NB: Unsupported packet types are logged fatally.
|
2019-01-19 05:42:24 +03:00
|
|
|
func handlePacket(c *Conn, pkt *packet) error {
|
2019-01-13 02:17:04 +03:00
|
|
|
if pkt.bodySize < 4 {
|
|
|
|
return errInvalidBody
|
|
|
|
}
|
|
|
|
|
2019-01-07 08:50:35 +03:00
|
|
|
switch pkt.packetType {
|
2019-01-11 02:22:21 +03:00
|
|
|
case packetTypeChunkSize:
|
2019-01-19 05:42:24 +03:00
|
|
|
c.inChunkSize = amf.DecodeInt32(pkt.body[:4])
|
|
|
|
c.log(DebugLevel, pkg+"set inChunkSize", "size", int(c.inChunkSize))
|
2018-08-25 16:52:22 +03:00
|
|
|
|
2019-01-11 02:22:21 +03:00
|
|
|
case packetTypeBytesReadReport:
|
2019-01-19 05:42:24 +03:00
|
|
|
c.log(DebugLevel, pkg+"received packetTypeBytesReadReport")
|
2018-08-25 16:52:22 +03:00
|
|
|
|
2019-01-11 02:22:21 +03:00
|
|
|
case packetTypeServerBW:
|
2019-01-19 05:42:24 +03:00
|
|
|
c.serverBW = amf.DecodeInt32(pkt.body[:4])
|
|
|
|
c.log(DebugLevel, pkg+"set serverBW", "size", int(c.serverBW))
|
2018-08-24 12:20:04 +03:00
|
|
|
|
2019-01-11 02:22:21 +03:00
|
|
|
case packetTypeClientBW:
|
2019-01-19 05:42:24 +03:00
|
|
|
c.clientBW = amf.DecodeInt32(pkt.body[:4])
|
|
|
|
c.log(DebugLevel, pkg+"set clientBW", "size", int(c.clientBW))
|
2019-01-07 10:30:42 +03:00
|
|
|
if pkt.bodySize > 4 {
|
2019-01-19 05:42:24 +03:00
|
|
|
c.clientBW2 = pkt.body[4]
|
|
|
|
c.log(DebugLevel, pkg+"set clientBW2", "size", int(c.clientBW2))
|
2019-01-07 10:30:42 +03:00
|
|
|
} else {
|
2019-01-19 05:42:24 +03:00
|
|
|
c.clientBW2 = 0xff
|
2019-01-07 10:30:42 +03:00
|
|
|
}
|
2018-08-24 12:20:04 +03:00
|
|
|
|
2019-01-11 02:22:21 +03:00
|
|
|
case packetTypeInvoke:
|
2019-01-19 05:42:24 +03:00
|
|
|
err := handleInvoke(c, pkt.body[:pkt.bodySize])
|
2019-01-06 07:12:51 +03:00
|
|
|
if err != nil {
|
2019-01-19 05:42:24 +03:00
|
|
|
c.log(WarnLevel, pkg+"unexpected error from handleInvoke", "error", err.Error())
|
2019-01-10 05:19:52 +03:00
|
|
|
return err
|
2018-08-24 12:20:04 +03:00
|
|
|
}
|
2018-08-25 16:52:22 +03:00
|
|
|
|
2019-01-13 02:17:04 +03:00
|
|
|
case packetTypeControl, packetTypeAudio, packetTypeVideo, packetTypeFlashVideo, packetTypeFlexMessage, packetTypeInfo:
|
2019-01-19 05:42:24 +03:00
|
|
|
c.log(FatalLevel, pkg+"unsupported packet type "+strconv.Itoa(int(pkt.packetType)))
|
2018-08-24 12:20:04 +03:00
|
|
|
|
2018-08-25 16:52:22 +03:00
|
|
|
default:
|
2019-01-19 05:42:24 +03:00
|
|
|
c.log(WarnLevel, pkg+"unknown packet type", "type", pkt.packetType)
|
2018-08-24 12:20:04 +03:00
|
|
|
}
|
2019-01-10 05:19:52 +03:00
|
|
|
return nil
|
2018-08-24 12:20:04 +03:00
|
|
|
}
|
|
|
|
|
2019-01-19 05:42:24 +03:00
|
|
|
func sendConnectPacket(c *Conn) error {
|
2018-09-19 06:06:39 +03:00
|
|
|
var pbuf [4096]byte
|
2019-01-07 08:50:35 +03:00
|
|
|
pkt := packet{
|
2019-01-11 02:22:21 +03:00
|
|
|
channel: chanControl,
|
|
|
|
headerType: headerSizeLarge,
|
|
|
|
packetType: packetTypeInvoke,
|
2019-01-19 09:08:40 +03:00
|
|
|
buf: pbuf[:],
|
2019-01-11 02:22:21 +03:00
|
|
|
body: pbuf[fullHeaderSize:],
|
2018-09-19 06:06:39 +03:00
|
|
|
}
|
2019-01-07 08:50:35 +03:00
|
|
|
enc := pkt.body
|
2018-07-28 15:00:08 +03:00
|
|
|
|
2019-01-12 23:59:36 +03:00
|
|
|
enc, err := amf.EncodeString(enc, avConnect)
|
2019-01-12 06:48:50 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-01-06 07:49:58 +03:00
|
|
|
}
|
2019-01-19 05:42:24 +03:00
|
|
|
c.numInvokes += 1
|
|
|
|
enc, err = amf.EncodeNumber(enc, float64(c.numInvokes))
|
2019-01-12 06:48:50 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-01-06 07:49:58 +03:00
|
|
|
}
|
2019-01-13 02:17:04 +03:00
|
|
|
|
2019-01-12 01:26:57 +03:00
|
|
|
enc[0] = amf.TypeObject
|
2018-09-19 06:06:39 +03:00
|
|
|
enc = enc[1:]
|
2019-01-19 05:42:24 +03:00
|
|
|
enc, err = amf.EncodeNamedString(enc, avApp, c.link.app)
|
2019-01-12 06:48:50 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-07-26 10:36:44 +03:00
|
|
|
}
|
2019-01-13 02:17:04 +03:00
|
|
|
enc, err = amf.EncodeNamedString(enc, avType, avNonprivate)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-07-26 10:36:44 +03:00
|
|
|
}
|
2019-01-19 05:42:24 +03:00
|
|
|
enc, err = amf.EncodeNamedString(enc, avTcUrl, c.link.url)
|
2019-01-13 02:17:04 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-07-26 10:36:44 +03:00
|
|
|
}
|
2019-01-12 23:59:36 +03:00
|
|
|
enc, err = amf.EncodeInt24(enc, amf.TypeObjectEnd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-07-26 10:36:44 +03:00
|
|
|
}
|
|
|
|
|
2019-01-12 23:59:36 +03:00
|
|
|
// add auth string, if any
|
2019-01-19 05:42:24 +03:00
|
|
|
if c.link.auth != "" {
|
|
|
|
enc, err = amf.EncodeBoolean(enc, c.link.flags&linkAuth != 0)
|
2019-01-12 06:48:50 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-07-26 10:36:44 +03:00
|
|
|
}
|
2019-01-19 05:42:24 +03:00
|
|
|
enc, err = amf.EncodeString(enc, c.link.auth)
|
2019-01-12 06:48:50 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-07-26 10:36:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-11 02:22:21 +03:00
|
|
|
pkt.bodySize = uint32((len(pbuf) - fullHeaderSize) - len(enc))
|
2018-07-26 10:36:44 +03:00
|
|
|
|
2019-01-19 05:42:24 +03:00
|
|
|
return pkt.writeTo(c, true) // response expected
|
2018-07-25 04:26:59 +03:00
|
|
|
}
|
|
|
|
|
2019-01-19 05:42:24 +03:00
|
|
|
func sendCreateStream(c *Conn) error {
|
2018-08-24 04:13:34 +03:00
|
|
|
var pbuf [256]byte
|
2019-01-07 08:50:35 +03:00
|
|
|
pkt := packet{
|
2019-01-11 02:22:21 +03:00
|
|
|
channel: chanControl,
|
|
|
|
headerType: headerSizeMedium,
|
|
|
|
packetType: packetTypeInvoke,
|
2019-01-19 09:08:40 +03:00
|
|
|
buf: pbuf[:],
|
2019-01-11 02:22:21 +03:00
|
|
|
body: pbuf[fullHeaderSize:],
|
2018-09-19 06:29:04 +03:00
|
|
|
}
|
2019-01-07 08:50:35 +03:00
|
|
|
enc := pkt.body
|
2018-09-19 06:29:04 +03:00
|
|
|
|
2019-01-13 05:22:06 +03:00
|
|
|
enc, err := amf.EncodeString(enc, avCreatestream)
|
2019-01-12 06:48:50 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-01-06 07:49:58 +03:00
|
|
|
}
|
2019-01-19 05:42:24 +03:00
|
|
|
c.numInvokes++
|
|
|
|
enc, err = amf.EncodeNumber(enc, float64(c.numInvokes))
|
2019-01-12 06:48:50 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-01-06 07:49:58 +03:00
|
|
|
}
|
2019-01-12 01:26:57 +03:00
|
|
|
enc[0] = amf.TypeNull
|
2018-09-19 06:29:04 +03:00
|
|
|
enc = enc[1:]
|
2018-08-24 04:13:34 +03:00
|
|
|
|
2019-01-11 02:22:21 +03:00
|
|
|
pkt.bodySize = uint32((len(pbuf) - fullHeaderSize) - len(enc))
|
2018-08-24 04:13:34 +03:00
|
|
|
|
2019-01-19 05:42:24 +03:00
|
|
|
return pkt.writeTo(c, true) // response expected
|
2018-08-24 04:13:34 +03:00
|
|
|
}
|
|
|
|
|
2019-01-19 05:42:24 +03:00
|
|
|
func sendReleaseStream(c *Conn) error {
|
2018-08-24 12:20:04 +03:00
|
|
|
var pbuf [1024]byte
|
2019-01-07 08:50:35 +03:00
|
|
|
pkt := packet{
|
2019-01-11 02:22:21 +03:00
|
|
|
channel: chanControl,
|
|
|
|
headerType: headerSizeMedium,
|
|
|
|
packetType: packetTypeInvoke,
|
2019-01-19 09:08:40 +03:00
|
|
|
buf: pbuf[:],
|
2019-01-11 02:22:21 +03:00
|
|
|
body: pbuf[fullHeaderSize:],
|
2018-09-19 06:29:04 +03:00
|
|
|
}
|
2019-01-07 08:50:35 +03:00
|
|
|
enc := pkt.body
|
2018-09-19 06:29:04 +03:00
|
|
|
|
2019-01-13 05:22:06 +03:00
|
|
|
enc, err := amf.EncodeString(enc, avReleasestream)
|
2019-01-12 06:48:50 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-01-06 07:49:58 +03:00
|
|
|
}
|
2019-01-19 05:42:24 +03:00
|
|
|
c.numInvokes++
|
|
|
|
enc, err = amf.EncodeNumber(enc, float64(c.numInvokes))
|
2019-01-12 06:48:50 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-01-06 07:49:58 +03:00
|
|
|
}
|
2019-01-12 01:26:57 +03:00
|
|
|
enc[0] = amf.TypeNull
|
2018-09-19 06:29:04 +03:00
|
|
|
enc = enc[1:]
|
2019-01-19 05:42:24 +03:00
|
|
|
enc, err = amf.EncodeString(enc, c.link.playpath)
|
2019-01-12 06:48:50 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-08-24 04:13:34 +03:00
|
|
|
}
|
2019-01-11 02:22:21 +03:00
|
|
|
pkt.bodySize = uint32((len(pbuf) - fullHeaderSize) - len(enc))
|
2018-08-24 04:13:34 +03:00
|
|
|
|
2019-01-19 05:42:24 +03:00
|
|
|
return pkt.writeTo(c, false)
|
2018-08-24 12:20:04 +03:00
|
|
|
}
|
2018-08-24 04:13:34 +03:00
|
|
|
|
2019-01-19 05:42:24 +03:00
|
|
|
func sendFCPublish(c *Conn) error {
|
2018-08-24 12:20:04 +03:00
|
|
|
var pbuf [1024]byte
|
2019-01-07 08:50:35 +03:00
|
|
|
pkt := packet{
|
2019-01-11 02:22:21 +03:00
|
|
|
channel: chanControl,
|
|
|
|
headerType: headerSizeMedium,
|
|
|
|
packetType: packetTypeInvoke,
|
2019-01-19 09:08:40 +03:00
|
|
|
buf: pbuf[:],
|
2019-01-11 02:22:21 +03:00
|
|
|
body: pbuf[fullHeaderSize:],
|
2018-09-19 06:29:04 +03:00
|
|
|
}
|
2019-01-07 08:50:35 +03:00
|
|
|
enc := pkt.body
|
2018-09-19 06:29:04 +03:00
|
|
|
|
2019-01-13 05:22:06 +03:00
|
|
|
enc, err := amf.EncodeString(enc, avFCPublish)
|
2019-01-12 06:48:50 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-01-06 07:49:58 +03:00
|
|
|
}
|
2019-01-19 05:42:24 +03:00
|
|
|
c.numInvokes++
|
|
|
|
enc, err = amf.EncodeNumber(enc, float64(c.numInvokes))
|
2019-01-12 06:48:50 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-01-06 07:49:58 +03:00
|
|
|
}
|
2019-01-12 01:26:57 +03:00
|
|
|
enc[0] = amf.TypeNull
|
2018-09-19 06:29:04 +03:00
|
|
|
enc = enc[1:]
|
2019-01-19 05:42:24 +03:00
|
|
|
enc, err = amf.EncodeString(enc, c.link.playpath)
|
2019-01-12 06:48:50 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-08-24 04:13:34 +03:00
|
|
|
}
|
2018-09-19 06:29:04 +03:00
|
|
|
|
2019-01-11 02:22:21 +03:00
|
|
|
pkt.bodySize = uint32((len(pbuf) - fullHeaderSize) - len(enc))
|
2018-08-24 04:13:34 +03:00
|
|
|
|
2019-01-19 05:42:24 +03:00
|
|
|
return pkt.writeTo(c, false)
|
2018-08-24 04:13:34 +03:00
|
|
|
}
|
|
|
|
|
2019-01-19 05:42:24 +03:00
|
|
|
func sendFCUnpublish(c *Conn) error {
|
2018-09-06 04:56:52 +03:00
|
|
|
var pbuf [1024]byte
|
2019-01-07 08:50:35 +03:00
|
|
|
pkt := packet{
|
2019-01-11 02:22:21 +03:00
|
|
|
channel: chanControl,
|
|
|
|
headerType: headerSizeMedium,
|
|
|
|
packetType: packetTypeInvoke,
|
2019-01-19 09:08:40 +03:00
|
|
|
buf: pbuf[:],
|
2019-01-11 02:22:21 +03:00
|
|
|
body: pbuf[fullHeaderSize:],
|
2018-09-19 06:29:04 +03:00
|
|
|
}
|
2019-01-07 08:50:35 +03:00
|
|
|
enc := pkt.body
|
2018-09-19 06:29:04 +03:00
|
|
|
|
2019-01-13 05:22:06 +03:00
|
|
|
enc, err := amf.EncodeString(enc, avFCUnpublish)
|
2019-01-12 06:48:50 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-01-06 07:49:58 +03:00
|
|
|
}
|
2019-01-19 05:42:24 +03:00
|
|
|
c.numInvokes++
|
|
|
|
enc, err = amf.EncodeNumber(enc, float64(c.numInvokes))
|
2019-01-12 06:48:50 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-01-06 07:49:58 +03:00
|
|
|
}
|
2019-01-12 01:26:57 +03:00
|
|
|
enc[0] = amf.TypeNull
|
2018-09-19 06:29:04 +03:00
|
|
|
enc = enc[1:]
|
2019-01-19 05:42:24 +03:00
|
|
|
enc, err = amf.EncodeString(enc, c.link.playpath)
|
2019-01-12 06:48:50 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-09-06 04:56:52 +03:00
|
|
|
}
|
|
|
|
|
2019-01-11 02:22:21 +03:00
|
|
|
pkt.bodySize = uint32((len(pbuf) - fullHeaderSize) - len(enc))
|
2018-09-06 04:56:52 +03:00
|
|
|
|
2019-01-19 05:42:24 +03:00
|
|
|
return pkt.writeTo(c, false)
|
2018-08-25 17:43:08 +03:00
|
|
|
}
|
|
|
|
|
2019-01-19 05:42:24 +03:00
|
|
|
func sendPublish(c *Conn) error {
|
2018-08-24 12:20:04 +03:00
|
|
|
var pbuf [1024]byte
|
2019-01-07 08:50:35 +03:00
|
|
|
pkt := packet{
|
2019-01-11 02:22:21 +03:00
|
|
|
channel: chanSource,
|
|
|
|
headerType: headerSizeLarge,
|
|
|
|
packetType: packetTypeInvoke,
|
2019-01-19 09:08:40 +03:00
|
|
|
buf: pbuf[:],
|
2019-01-11 02:22:21 +03:00
|
|
|
body: pbuf[fullHeaderSize:],
|
2018-09-19 06:29:04 +03:00
|
|
|
}
|
2019-01-07 08:50:35 +03:00
|
|
|
enc := pkt.body
|
2018-09-19 06:29:04 +03:00
|
|
|
|
2019-01-13 05:22:06 +03:00
|
|
|
enc, err := amf.EncodeString(enc, avPublish)
|
2019-01-12 06:48:50 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-01-06 07:49:58 +03:00
|
|
|
}
|
2019-01-19 05:42:24 +03:00
|
|
|
c.numInvokes++
|
|
|
|
enc, err = amf.EncodeNumber(enc, float64(c.numInvokes))
|
2019-01-12 06:48:50 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-01-06 07:49:58 +03:00
|
|
|
}
|
2019-01-12 01:26:57 +03:00
|
|
|
enc[0] = amf.TypeNull
|
2018-09-19 06:29:04 +03:00
|
|
|
enc = enc[1:]
|
2019-01-19 05:42:24 +03:00
|
|
|
enc, err = amf.EncodeString(enc, c.link.playpath)
|
2019-01-12 06:48:50 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-08-24 04:13:34 +03:00
|
|
|
}
|
2019-01-12 06:48:50 +03:00
|
|
|
enc, err = amf.EncodeString(enc, avLive)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-07-22 16:31:50 +03:00
|
|
|
}
|
|
|
|
|
2019-01-11 02:22:21 +03:00
|
|
|
pkt.bodySize = uint32((len(pbuf) - fullHeaderSize) - len(enc))
|
2018-07-22 16:31:50 +03:00
|
|
|
|
2019-01-19 05:42:24 +03:00
|
|
|
return pkt.writeTo(c, true) // response expected
|
2018-07-22 16:31:50 +03:00
|
|
|
}
|
|
|
|
|
2019-01-19 05:42:24 +03:00
|
|
|
func sendDeleteStream(c *Conn, dStreamId float64) error {
|
2018-09-06 05:15:48 +03:00
|
|
|
var pbuf [256]byte
|
2019-01-07 08:50:35 +03:00
|
|
|
pkt := packet{
|
2019-01-11 02:22:21 +03:00
|
|
|
channel: chanControl,
|
|
|
|
headerType: headerSizeMedium,
|
|
|
|
packetType: packetTypeInvoke,
|
2019-01-19 09:08:40 +03:00
|
|
|
buf: pbuf[:],
|
2019-01-11 02:22:21 +03:00
|
|
|
body: pbuf[fullHeaderSize:],
|
2018-09-19 06:29:04 +03:00
|
|
|
}
|
2019-01-07 08:50:35 +03:00
|
|
|
enc := pkt.body
|
2018-09-19 06:29:04 +03:00
|
|
|
|
2019-01-13 05:22:06 +03:00
|
|
|
enc, err := amf.EncodeString(enc, avDeletestream)
|
2019-01-12 06:48:50 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-01-06 07:12:51 +03:00
|
|
|
}
|
2019-01-19 05:42:24 +03:00
|
|
|
c.numInvokes++
|
|
|
|
enc, err = amf.EncodeNumber(enc, float64(c.numInvokes))
|
2019-01-12 06:48:50 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-01-06 07:12:51 +03:00
|
|
|
}
|
2019-01-12 01:26:57 +03:00
|
|
|
enc[0] = amf.TypeNull
|
2018-09-19 06:29:04 +03:00
|
|
|
enc = enc[1:]
|
2019-01-12 06:48:50 +03:00
|
|
|
enc, err = amf.EncodeNumber(enc, dStreamId)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-01-06 07:12:51 +03:00
|
|
|
}
|
2019-01-11 02:22:21 +03:00
|
|
|
pkt.bodySize = uint32((len(pbuf) - fullHeaderSize) - len(enc))
|
2018-09-06 05:15:48 +03:00
|
|
|
|
2019-01-19 05:42:24 +03:00
|
|
|
return pkt.writeTo(c, false)
|
2018-09-06 05:15:48 +03:00
|
|
|
}
|
|
|
|
|
2019-01-07 10:30:42 +03:00
|
|
|
// sendBytesReceived tells the server how many bytes the client has received.
|
2019-01-19 05:42:24 +03:00
|
|
|
func sendBytesReceived(c *Conn) error {
|
2018-08-24 12:20:04 +03:00
|
|
|
var pbuf [256]byte
|
2019-01-07 08:50:35 +03:00
|
|
|
pkt := packet{
|
2019-01-11 02:22:21 +03:00
|
|
|
channel: chanBytesRead,
|
|
|
|
headerType: headerSizeMedium,
|
|
|
|
packetType: packetTypeBytesReadReport,
|
2019-01-19 09:08:40 +03:00
|
|
|
buf: pbuf[:],
|
2019-01-11 02:22:21 +03:00
|
|
|
body: pbuf[fullHeaderSize:],
|
2019-01-07 06:03:07 +03:00
|
|
|
}
|
2019-01-07 08:50:35 +03:00
|
|
|
enc := pkt.body
|
2019-01-07 06:03:07 +03:00
|
|
|
|
2019-01-19 05:42:24 +03:00
|
|
|
c.nBytesInSent = c.nBytesIn
|
2019-01-13 05:22:06 +03:00
|
|
|
|
2019-01-19 05:42:24 +03:00
|
|
|
enc, err := amf.EncodeInt32(enc, c.nBytesIn)
|
2019-01-12 06:48:50 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-01-06 07:49:58 +03:00
|
|
|
}
|
2019-01-07 08:50:35 +03:00
|
|
|
pkt.bodySize = 4
|
2018-08-12 12:07:39 +03:00
|
|
|
|
2019-01-19 05:42:24 +03:00
|
|
|
return pkt.writeTo(c, false)
|
2018-08-24 12:20:04 +03:00
|
|
|
}
|
2018-08-12 13:07:56 +03:00
|
|
|
|
2019-01-19 05:42:24 +03:00
|
|
|
func sendCheckBW(c *Conn) error {
|
2018-08-24 12:20:04 +03:00
|
|
|
var pbuf [256]byte
|
2019-01-07 08:50:35 +03:00
|
|
|
pkt := packet{
|
2019-01-11 02:22:21 +03:00
|
|
|
channel: chanControl,
|
|
|
|
headerType: headerSizeLarge,
|
|
|
|
packetType: packetTypeInvoke,
|
2019-01-19 09:08:40 +03:00
|
|
|
buf: pbuf[:],
|
2019-01-11 02:22:21 +03:00
|
|
|
body: pbuf[fullHeaderSize:],
|
2018-09-19 06:29:04 +03:00
|
|
|
}
|
2019-01-07 08:50:35 +03:00
|
|
|
enc := pkt.body
|
2018-09-19 06:29:04 +03:00
|
|
|
|
2019-01-13 05:22:06 +03:00
|
|
|
enc, err := amf.EncodeString(enc, av_checkbw)
|
2019-01-12 06:48:50 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-01-06 07:49:58 +03:00
|
|
|
}
|
2019-01-19 05:42:24 +03:00
|
|
|
c.numInvokes++
|
|
|
|
enc, err = amf.EncodeNumber(enc, float64(c.numInvokes))
|
2019-01-12 06:48:50 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-01-06 07:49:58 +03:00
|
|
|
}
|
2019-01-12 01:26:57 +03:00
|
|
|
enc[0] = amf.TypeNull
|
2018-09-19 06:29:04 +03:00
|
|
|
enc = enc[1:]
|
2018-08-12 12:07:39 +03:00
|
|
|
|
2019-01-11 02:22:21 +03:00
|
|
|
pkt.bodySize = uint32((len(pbuf) - fullHeaderSize) - len(enc))
|
2018-08-12 13:17:53 +03:00
|
|
|
|
2019-01-19 05:42:24 +03:00
|
|
|
return pkt.writeTo(c, false)
|
2018-08-12 13:17:53 +03:00
|
|
|
}
|
|
|
|
|
2019-01-07 10:30:42 +03:00
|
|
|
func eraseMethod(m []method, i int) []method {
|
2018-09-12 04:59:38 +03:00
|
|
|
copy(m[i:], m[i+1:])
|
2019-01-07 08:50:35 +03:00
|
|
|
m[len(m)-1] = method{}
|
2018-09-12 04:59:38 +03:00
|
|
|
return m[:len(m)-1]
|
2018-08-24 16:00:40 +03:00
|
|
|
}
|
|
|
|
|
2019-01-07 10:30:42 +03:00
|
|
|
// int handleInvoke handles a packet invoke request
|
2019-01-19 05:42:24 +03:00
|
|
|
// Side effects: c.isPlaying set to true upon avNetStreamPublish_Start
|
|
|
|
func handleInvoke(c *Conn, body []byte) error {
|
2018-09-19 07:35:42 +03:00
|
|
|
if body[0] != 0x02 {
|
2019-01-06 07:12:51 +03:00
|
|
|
return errInvalidBody
|
2018-08-14 11:52:37 +03:00
|
|
|
}
|
2019-01-12 01:26:57 +03:00
|
|
|
var obj amf.Object
|
2019-01-12 07:25:12 +03:00
|
|
|
_, err := amf.Decode(&obj, body, false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-08-14 11:52:37 +03:00
|
|
|
}
|
2018-08-14 10:06:42 +03:00
|
|
|
|
2019-01-13 01:42:41 +03:00
|
|
|
meth, err := obj.StringProperty("", 0)
|
2019-01-12 08:40:09 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-01-13 01:42:41 +03:00
|
|
|
txn, err := obj.NumberProperty("", 1)
|
2019-01-12 08:40:09 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-09-06 11:33:55 +03:00
|
|
|
|
2019-01-19 05:42:24 +03:00
|
|
|
c.log(DebugLevel, pkg+"invoking method "+meth)
|
2019-01-07 08:50:35 +03:00
|
|
|
switch meth {
|
2019-01-11 02:22:21 +03:00
|
|
|
case av_result:
|
2019-01-19 05:42:24 +03:00
|
|
|
if (c.link.protocol & featureWrite) == 0 {
|
2019-01-13 05:34:05 +03:00
|
|
|
return errNotWritable
|
|
|
|
}
|
2018-09-06 12:30:28 +03:00
|
|
|
var methodInvoked string
|
2019-01-19 05:42:24 +03:00
|
|
|
for i, m := range c.methodCalls {
|
2018-09-12 04:59:38 +03:00
|
|
|
if float64(m.num) == txn {
|
|
|
|
methodInvoked = m.name
|
2019-01-19 05:42:24 +03:00
|
|
|
c.methodCalls = eraseMethod(c.methodCalls, i)
|
2018-09-06 11:33:55 +03:00
|
|
|
break
|
2018-08-14 11:52:37 +03:00
|
|
|
}
|
2018-09-06 11:33:55 +03:00
|
|
|
}
|
2018-09-06 12:30:28 +03:00
|
|
|
if methodInvoked == "" {
|
2019-01-19 05:42:24 +03:00
|
|
|
c.log(WarnLevel, pkg+"received result without matching request", "id", txn)
|
2019-01-12 07:59:58 +03:00
|
|
|
return nil
|
2018-09-06 11:33:55 +03:00
|
|
|
}
|
2019-01-19 05:42:24 +03:00
|
|
|
c.log(DebugLevel, pkg+"received result for "+methodInvoked)
|
2019-01-07 16:29:41 +03:00
|
|
|
|
2018-09-06 12:30:28 +03:00
|
|
|
switch methodInvoked {
|
2019-01-11 02:22:21 +03:00
|
|
|
case avConnect:
|
2019-01-19 05:42:24 +03:00
|
|
|
err := sendReleaseStream(c)
|
2019-01-10 16:18:11 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-01-19 05:42:24 +03:00
|
|
|
err = sendFCPublish(c)
|
2019-01-10 16:18:11 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-01-19 05:42:24 +03:00
|
|
|
err = sendCreateStream(c)
|
2019-01-10 16:18:11 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-09-06 11:33:55 +03:00
|
|
|
}
|
2018-08-14 16:00:47 +03:00
|
|
|
|
2019-01-11 02:22:21 +03:00
|
|
|
case avCreatestream:
|
2019-01-13 01:42:41 +03:00
|
|
|
n, err := obj.NumberProperty("", 3)
|
2019-01-12 08:40:09 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-01-19 05:42:24 +03:00
|
|
|
c.streamID = int32(n)
|
|
|
|
err = sendPublish(c)
|
2019-01-10 16:18:11 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-08-14 11:52:37 +03:00
|
|
|
}
|
2018-09-06 11:33:55 +03:00
|
|
|
|
2019-01-13 02:17:04 +03:00
|
|
|
default:
|
2019-01-19 05:42:24 +03:00
|
|
|
c.log(FatalLevel, pkg+"unexpected method invoked"+methodInvoked)
|
2018-08-13 05:45:43 +03:00
|
|
|
}
|
2018-09-06 11:33:55 +03:00
|
|
|
|
2019-01-11 02:22:21 +03:00
|
|
|
case avOnBWDone:
|
2019-01-19 05:42:24 +03:00
|
|
|
err := sendCheckBW(c)
|
2019-01-13 05:34:05 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-08-14 16:00:47 +03:00
|
|
|
}
|
2018-08-28 12:10:10 +03:00
|
|
|
|
2019-01-11 02:22:21 +03:00
|
|
|
case avOnStatus:
|
2019-01-13 01:42:41 +03:00
|
|
|
obj2, err := obj.ObjectProperty("", 3)
|
2019-01-12 08:40:09 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-01-13 01:42:41 +03:00
|
|
|
code, err := obj2.StringProperty(avCode, -1)
|
2019-01-12 08:40:09 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-01-13 01:42:41 +03:00
|
|
|
level, err := obj2.StringProperty(avLevel, -1)
|
2019-01-12 08:40:09 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-01-19 05:42:24 +03:00
|
|
|
c.log(DebugLevel, pkg+"onStatus", "code", code, "level", level)
|
2018-09-06 11:33:55 +03:00
|
|
|
|
2019-01-13 02:17:04 +03:00
|
|
|
if code != avNetStreamPublish_Start {
|
2019-01-19 05:42:24 +03:00
|
|
|
c.log(ErrorLevel, pkg+"unexpected response "+code)
|
2019-01-13 02:17:04 +03:00
|
|
|
return errUnimplemented
|
|
|
|
}
|
2019-01-19 05:42:24 +03:00
|
|
|
c.log(DebugLevel, pkg+"playing")
|
|
|
|
c.isPlaying = true
|
|
|
|
for i, m := range c.methodCalls {
|
2019-01-13 02:17:04 +03:00
|
|
|
if m.name == avPublish {
|
2019-01-19 05:42:24 +03:00
|
|
|
c.methodCalls = eraseMethod(c.methodCalls, i)
|
2018-08-14 11:52:37 +03:00
|
|
|
}
|
2018-08-24 12:20:04 +03:00
|
|
|
}
|
2018-08-28 12:10:10 +03:00
|
|
|
|
|
|
|
default:
|
2019-01-19 05:42:24 +03:00
|
|
|
c.log(FatalLevel, pkg+"unsuppoted method "+meth)
|
2018-08-14 21:49:18 +03:00
|
|
|
}
|
2019-01-06 07:12:51 +03:00
|
|
|
return nil
|
2018-08-24 12:20:04 +03:00
|
|
|
}
|
2018-08-14 21:49:18 +03:00
|
|
|
|
2019-01-19 05:42:24 +03:00
|
|
|
func handshake(c *Conn) error {
|
2019-01-11 02:22:21 +03:00
|
|
|
var clientbuf [signatureSize + 1]byte
|
2018-09-19 08:27:13 +03:00
|
|
|
clientsig := clientbuf[1:]
|
|
|
|
|
2019-01-11 02:22:21 +03:00
|
|
|
var serversig [signatureSize]byte
|
|
|
|
clientbuf[0] = chanControl
|
2019-01-07 10:30:42 +03:00
|
|
|
binary.BigEndian.PutUint32(clientsig, uint32(time.Now().UnixNano()/1000000))
|
2018-09-19 08:27:13 +03:00
|
|
|
copy(clientsig[4:8], []byte{0, 0, 0, 0})
|
2018-06-27 21:02:16 +03:00
|
|
|
|
2019-01-11 02:22:21 +03:00
|
|
|
for i := 8; i < signatureSize; i++ {
|
2018-09-19 08:27:13 +03:00
|
|
|
clientsig[i] = byte(rand.Intn(256))
|
2018-08-24 12:20:04 +03:00
|
|
|
}
|
2018-07-10 13:36:49 +03:00
|
|
|
|
2019-01-19 05:42:24 +03:00
|
|
|
_, err := c.write(clientbuf[:])
|
2019-01-06 07:12:51 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-08-24 12:20:04 +03:00
|
|
|
}
|
2019-01-19 05:42:24 +03:00
|
|
|
c.log(DebugLevel, pkg+"handshake sent")
|
2018-07-12 22:51:17 +03:00
|
|
|
|
2018-09-19 08:27:13 +03:00
|
|
|
var typ [1]byte
|
2019-01-19 05:42:24 +03:00
|
|
|
_, err = c.read(typ[:])
|
2019-01-06 07:12:51 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-08-24 12:20:04 +03:00
|
|
|
}
|
2019-01-19 05:42:24 +03:00
|
|
|
c.log(DebugLevel, pkg+"handshake received")
|
2018-07-14 15:40:01 +03:00
|
|
|
|
2018-09-19 05:20:46 +03:00
|
|
|
if typ[0] != clientbuf[0] {
|
2019-01-19 05:42:24 +03:00
|
|
|
c.log(WarnLevel, pkg+"handshake type mismatch", "sent", clientbuf[0], "received", typ)
|
2018-08-24 12:20:04 +03:00
|
|
|
}
|
2019-01-19 05:42:24 +03:00
|
|
|
_, err = c.read(serversig[:])
|
2019-01-06 07:12:51 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-07-05 09:55:46 +03:00
|
|
|
}
|
|
|
|
|
2018-08-24 12:20:04 +03:00
|
|
|
// decode server response
|
2018-09-19 08:27:13 +03:00
|
|
|
suptime := binary.BigEndian.Uint32(serversig[:4])
|
2019-01-19 05:42:24 +03:00
|
|
|
c.log(DebugLevel, pkg+"server uptime", "uptime", suptime)
|
2018-08-11 07:18:21 +03:00
|
|
|
|
2018-08-24 12:20:04 +03:00
|
|
|
// 2nd part of handshake
|
2019-01-19 05:42:24 +03:00
|
|
|
_, err = c.write(serversig[:])
|
2019-01-06 07:12:51 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-08-11 07:18:21 +03:00
|
|
|
}
|
2018-08-24 12:20:04 +03:00
|
|
|
|
2019-01-19 05:42:24 +03:00
|
|
|
_, err = c.read(serversig[:])
|
2019-01-06 07:12:51 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-08-24 12:20:04 +03:00
|
|
|
}
|
|
|
|
|
2019-01-11 02:22:21 +03:00
|
|
|
if !bytes.Equal(serversig[:signatureSize], clientbuf[1:signatureSize+1]) {
|
2019-01-19 05:42:24 +03:00
|
|
|
c.log(WarnLevel, pkg+"signature mismatch", "serversig", serversig[:signatureSize], "clientsig", clientbuf[1:signatureSize+1])
|
2018-08-24 12:20:04 +03:00
|
|
|
}
|
2019-01-06 07:12:51 +03:00
|
|
|
return nil
|
2018-08-11 07:18:21 +03:00
|
|
|
}
|