Cloak/internal/multiplex/stream.go

189 lines
4.7 KiB
Go
Raw Normal View History

2018-10-05 22:44:20 +00:00
package multiplex
import (
"errors"
2019-08-14 09:04:27 +00:00
"io"
2019-07-23 10:06:49 +00:00
"net"
"time"
2019-08-02 14:45:33 +00:00
log "github.com/sirupsen/logrus"
2018-10-27 22:35:46 +00:00
"math"
prand "math/rand"
2018-10-05 22:44:20 +00:00
"sync"
2018-10-23 19:47:58 +00:00
"sync/atomic"
2018-10-05 22:44:20 +00:00
)
var ErrBrokenStream = errors.New("broken stream")
2018-10-05 22:44:20 +00:00
type Stream struct {
id uint32
session *Session
recvBuf recvBuffer
2019-08-05 12:32:53 +00:00
2018-10-27 22:35:46 +00:00
// atomic
nextSendSeq uint64
2018-10-07 17:09:45 +00:00
2018-11-23 23:57:35 +00:00
writingM sync.RWMutex
2019-08-20 21:43:04 +00:00
// atomic
2019-07-28 10:58:45 +00:00
closed uint32
2019-08-04 09:38:49 +00:00
obfsBuf []byte
// we assign each stream a fixed underlying TCP connection to utilise order guarantee provided by TCP itself
// so that frameSorter should have few to none ooo frames to deal with
// overall the streams in a session should be uniformly distributed across all connections
2019-08-20 21:43:04 +00:00
// This is not used in unordered connection mode
assignedConnId uint32
2018-10-05 22:44:20 +00:00
}
func makeStream(sesh *Session, id uint32, assignedConnId uint32) *Stream {
var recvBuf recvBuffer
2019-08-14 09:04:27 +00:00
if sesh.Unordered {
recvBuf = NewDatagramBuffer()
2019-08-14 09:04:27 +00:00
} else {
recvBuf = NewStreamBuffer()
2019-08-14 09:04:27 +00:00
}
2019-08-05 12:32:53 +00:00
2018-10-05 22:44:20 +00:00
stream := &Stream{
id: id,
session: sesh,
recvBuf: recvBuf,
obfsBuf: make([]byte, 17000),
assignedConnId: assignedConnId,
2018-10-05 22:44:20 +00:00
}
2019-08-05 12:32:53 +00:00
2018-10-05 22:44:20 +00:00
return stream
}
2019-07-28 10:58:45 +00:00
func (s *Stream) isClosed() bool { return atomic.LoadUint32(&s.closed) == 1 }
2019-08-30 19:43:04 +00:00
func (s *Stream) writeFrame(frame Frame) error {
return s.recvBuf.Write(frame)
}
2019-08-05 13:33:20 +00:00
2019-08-20 21:43:04 +00:00
// Read implements io.Read
2019-07-28 10:58:45 +00:00
func (s *Stream) Read(buf []byte) (n int, err error) {
2019-08-06 10:19:47 +00:00
//log.Tracef("attempting to read from stream %v", s.id)
2018-10-16 20:13:19 +00:00
if len(buf) == 0 {
2019-07-28 10:58:45 +00:00
if s.isClosed() {
return 0, ErrBrokenStream
2019-07-28 10:58:45 +00:00
} else {
2018-10-16 20:13:19 +00:00
return 0, nil
2018-10-05 22:44:20 +00:00
}
}
2019-08-14 09:04:27 +00:00
2019-08-30 19:39:23 +00:00
n, err = s.recvBuf.Read(buf)
if err == io.EOF {
return n, ErrBrokenStream
2018-10-16 20:13:19 +00:00
}
2019-08-30 19:39:23 +00:00
log.Tracef("%v read from stream %v with err %v", n, s.id, err)
return
2018-10-05 22:44:20 +00:00
}
2019-08-20 21:43:04 +00:00
// Write implements io.Write
2019-07-28 10:58:45 +00:00
func (s *Stream) Write(in []byte) (n int, err error) {
2018-11-07 21:16:13 +00:00
// RWMutex used here isn't really for RW.
// we use it to exploit the fact that RLock doesn't create contention.
// The use of RWMutex is so that the stream will not actively close
// in the middle of the execution of Write. This may cause the closing frame
// to be sent before the data frame and cause loss of packet.
2019-08-06 10:19:47 +00:00
//log.Tracef("attempting to write %v bytes to stream %v",len(in),s.id)
2019-07-28 10:58:45 +00:00
s.writingM.RLock()
2019-07-28 11:52:57 +00:00
defer s.writingM.RUnlock()
2019-07-28 10:58:45 +00:00
if s.isClosed() {
return 0, ErrBrokenStream
2018-10-05 22:44:20 +00:00
}
f := &Frame{
2019-07-28 10:58:45 +00:00
StreamID: s.id,
Seq: atomic.AddUint64(&s.nextSendSeq, 1) - 1,
2018-10-27 22:35:46 +00:00
Closing: 0,
Payload: in,
2018-10-05 22:44:20 +00:00
}
2019-08-04 09:38:49 +00:00
i, err := s.session.Obfs(f, s.obfsBuf)
2018-12-09 23:45:06 +00:00
if err != nil {
2019-08-04 09:38:49 +00:00
return i, err
2018-12-09 23:45:06 +00:00
}
n, err = s.session.sb.send(s.obfsBuf[:i], &s.assignedConnId)
2019-08-14 09:04:27 +00:00
log.Tracef("%v sent to remote through stream %v with err %v", len(in), s.id, err)
if err != nil {
return
}
return len(in), nil
2018-10-05 22:44:20 +00:00
}
2019-07-28 10:58:45 +00:00
// the necessary steps to mark the stream as closed and to release resources
func (s *Stream) _close() {
atomic.StoreUint32(&s.closed, 1)
2019-08-30 19:45:06 +00:00
_ = s.recvBuf.Close() // both datagramBuffer and streamBuffer won't return err on Close()
2019-07-28 10:58:45 +00:00
}
2018-11-07 21:16:13 +00:00
// only close locally. Used when the stream close is notified by the remote
2019-07-28 10:58:45 +00:00
func (s *Stream) passiveClose() {
s._close()
s.session.delStream(s.id)
2019-08-06 10:19:47 +00:00
log.Tracef("stream %v passively closed", s.id)
2018-10-27 22:35:46 +00:00
}
// active close. Close locally and tell the remote that this stream is being closed
2019-07-28 10:58:45 +00:00
func (s *Stream) Close() error {
2018-10-14 19:32:54 +00:00
2019-07-28 10:58:45 +00:00
s.writingM.Lock()
2019-07-28 11:52:57 +00:00
defer s.writingM.Unlock()
2019-07-28 10:58:45 +00:00
if s.isClosed() {
2018-12-29 00:02:59 +00:00
return errors.New("Already Closed")
}
2018-10-27 22:35:46 +00:00
2018-11-07 21:16:13 +00:00
// Notify remote that this stream is closed
2019-07-28 10:58:45 +00:00
prand.Seed(int64(s.id))
2018-10-27 22:35:46 +00:00
padLen := int(math.Floor(prand.Float64()*200 + 300))
pad := make([]byte, padLen)
prand.Read(pad)
f := &Frame{
2019-07-28 10:58:45 +00:00
StreamID: s.id,
Seq: atomic.AddUint64(&s.nextSendSeq, 1) - 1,
2018-10-27 22:35:46 +00:00
Closing: 1,
Payload: pad,
}
2019-08-04 09:38:49 +00:00
i, err := s.session.Obfs(f, s.obfsBuf)
if err != nil {
return err
}
_, err = s.session.sb.send(s.obfsBuf[:i], &s.assignedConnId)
2019-08-04 09:38:49 +00:00
if err != nil {
return err
}
2018-10-27 22:35:46 +00:00
2019-07-28 10:58:45 +00:00
s._close()
s.session.delStream(s.id)
2019-08-06 10:19:47 +00:00
log.Tracef("stream %v actively closed", s.id)
2018-10-05 22:44:20 +00:00
return nil
}
2018-10-23 19:47:58 +00:00
2018-11-23 23:57:35 +00:00
// Same as passiveClose() but no call to session.delStream.
2018-10-23 19:47:58 +00:00
// This is called in session.Close() to avoid mutex deadlock
2018-11-23 23:57:35 +00:00
// We don't notify the remote because session.Close() is always
// called when the session is passively closed
2019-07-28 10:58:45 +00:00
func (s *Stream) closeNoDelMap() {
2019-08-06 10:19:47 +00:00
log.Tracef("stream %v closed by session", s.id)
2019-07-28 10:58:45 +00:00
s._close()
2018-10-23 19:47:58 +00:00
}
2019-07-23 10:06:49 +00:00
// the following functions are purely for implementing net.Conn interface.
// they are not used
var errNotImplemented = errors.New("Not implemented")
2019-07-28 10:58:45 +00:00
func (s *Stream) LocalAddr() net.Addr { return s.session.addrs.Load().([]net.Addr)[0] }
func (s *Stream) RemoteAddr() net.Addr { return s.session.addrs.Load().([]net.Addr)[1] }
2019-07-23 10:06:49 +00:00
// TODO: implement the following
2019-07-28 10:58:45 +00:00
func (s *Stream) SetDeadline(t time.Time) error { return errNotImplemented }
func (s *Stream) SetReadDeadline(t time.Time) error { return errNotImplemented }
func (s *Stream) SetWriteDeadline(t time.Time) error { return errNotImplemented }