Cloak/internal/multiplex/session.go

177 lines
4.1 KiB
Go
Raw Normal View History

2018-10-05 22:44:20 +00:00
package multiplex
import (
2018-10-23 19:47:58 +00:00
"errors"
2019-01-12 15:51:20 +00:00
//"log"
2018-10-05 22:44:20 +00:00
"net"
"sync"
2018-10-23 19:47:58 +00:00
"sync/atomic"
"time"
2018-10-05 22:44:20 +00:00
)
const (
2018-10-23 19:47:58 +00:00
acceptBacklog = 1024
2018-10-05 22:44:20 +00:00
)
2018-10-27 14:27:43 +00:00
var ErrBrokenSession = errors.New("broken session")
var errRepeatSessionClosing = errors.New("trying to close a closed session")
2018-10-05 22:44:20 +00:00
type Session struct {
id uint32
2018-10-05 22:44:20 +00:00
// Used in Stream.Write. Add multiplexing headers, encrypt and add TLS header
2018-12-09 23:45:06 +00:00
obfs Obfser
2018-10-05 22:44:20 +00:00
// Remove TLS header, decrypt and unmarshall multiplexing headers
2018-12-09 23:45:06 +00:00
deobfs Deobfser
2018-10-05 22:44:20 +00:00
// This is supposed to read one TLS message, the same as GoQuiet's ReadTillDrain
2018-11-07 21:16:13 +00:00
obfsedRead func(net.Conn, []byte) (int, error)
2018-10-05 22:44:20 +00:00
2018-10-27 22:35:46 +00:00
// atomic
2018-10-23 19:47:58 +00:00
nextStreamID uint32
2018-10-05 22:44:20 +00:00
streamsM sync.Mutex
2018-10-05 22:44:20 +00:00
streams map[uint32]*Stream
// Switchboard manages all connections to remote
sb *switchboard
addrs atomic.Value
2018-10-05 22:44:20 +00:00
// For accepting new streams
acceptCh chan *Stream
2018-10-23 19:47:58 +00:00
2019-07-28 22:27:59 +00:00
closed uint32
terminalMsg atomic.Value
2018-10-05 22:44:20 +00:00
}
2018-12-09 23:45:06 +00:00
func MakeSession(id uint32, valve *Valve, obfs Obfser, deobfs Deobfser, obfsedRead func(net.Conn, []byte) (int, error)) *Session {
2018-10-05 22:44:20 +00:00
sesh := &Session{
id: id,
2018-10-07 17:09:45 +00:00
obfs: obfs,
deobfs: deobfs,
2018-11-07 21:16:13 +00:00
obfsedRead: obfsedRead,
2018-10-09 20:53:55 +00:00
nextStreamID: 1,
2018-10-05 22:44:20 +00:00
streams: make(map[uint32]*Stream),
acceptCh: make(chan *Stream, acceptBacklog),
}
sesh.addrs.Store([]net.Addr{nil, nil})
2018-11-07 21:16:13 +00:00
sesh.sb = makeSwitchboard(sesh, valve)
2019-06-16 13:30:35 +00:00
go sesh.timeoutAfter(30 * time.Second)
2018-10-05 22:44:20 +00:00
return sesh
}
2018-10-07 17:09:45 +00:00
func (sesh *Session) AddConnection(conn net.Conn) {
2018-10-28 21:22:38 +00:00
sesh.sb.addConn(conn)
addrs := []net.Addr{conn.LocalAddr(), conn.RemoteAddr()}
sesh.addrs.Store(addrs)
2018-10-07 17:09:45 +00:00
}
2018-10-05 22:44:20 +00:00
func (sesh *Session) OpenStream() (*Stream, error) {
2019-07-28 22:27:59 +00:00
if sesh.IsClosed() {
2018-11-07 21:16:13 +00:00
return nil, ErrBrokenSession
}
id := atomic.AddUint32(&sesh.nextStreamID, 1) - 1
// Because atomic.AddUint32 returns the value after incrementation
2018-10-05 22:44:20 +00:00
stream := makeStream(id, sesh)
sesh.streamsM.Lock()
sesh.streams[id] = stream
sesh.streamsM.Unlock()
2019-01-12 15:51:20 +00:00
//log.Printf("Opening stream %v\n", id)
2018-10-05 22:44:20 +00:00
return stream, nil
}
2019-07-23 10:06:49 +00:00
func (sesh *Session) Accept() (net.Conn, error) {
2019-07-28 22:27:59 +00:00
if sesh.IsClosed() {
2018-10-27 14:27:43 +00:00
return nil, ErrBrokenSession
2018-10-23 19:47:58 +00:00
}
2019-07-28 22:27:59 +00:00
stream := <-sesh.acceptCh
if stream == nil {
return nil, ErrBrokenSession
}
return stream, nil
2018-10-05 22:44:20 +00:00
}
func (sesh *Session) delStream(id uint32) {
2018-10-16 20:13:19 +00:00
sesh.streamsM.Lock()
2018-10-05 22:44:20 +00:00
delete(sesh.streams, id)
if len(sesh.streams) == 0 {
go sesh.timeoutAfter(30 * time.Second)
}
2018-10-16 20:13:19 +00:00
sesh.streamsM.Unlock()
2018-10-05 22:44:20 +00:00
}
2018-12-31 11:30:39 +00:00
// either fetch an existing stream or instantiate a new stream and put it in the dict, and return it
func (sesh *Session) getStream(id uint32, closingFrame bool) *Stream {
2018-11-24 00:55:26 +00:00
// it would have been neater to use defer Unlock(), however it gives
// non-negligable overhead and this function is performance critical
sesh.streamsM.Lock()
2019-07-28 11:52:57 +00:00
defer sesh.streamsM.Unlock()
2018-11-24 00:55:26 +00:00
stream := sesh.streams[id]
if stream != nil {
return stream
} else {
if closingFrame {
2018-12-31 11:30:39 +00:00
// If the stream has been closed and the current frame is a closing frame,
// we return nil
2018-11-24 00:55:26 +00:00
return nil
} else {
stream = makeStream(id, sesh)
sesh.streams[id] = stream
sesh.acceptCh <- stream
2019-01-12 15:51:20 +00:00
//log.Printf("Adding stream %v\n", id)
2018-11-24 00:55:26 +00:00
return stream
}
}
}
func (sesh *Session) SetTerminalMsg(msg string) {
sesh.terminalMsg.Store(msg)
}
func (sesh *Session) TerminalMsg() string {
msg := sesh.terminalMsg.Load()
if msg != nil {
return msg.(string)
} else {
return ""
}
}
2018-10-23 19:47:58 +00:00
func (sesh *Session) Close() error {
2019-07-28 22:27:59 +00:00
atomic.StoreUint32(&sesh.closed, 1)
2018-10-23 19:47:58 +00:00
sesh.streamsM.Lock()
2019-07-28 22:27:59 +00:00
sesh.acceptCh <- nil
2018-10-23 19:47:58 +00:00
for id, stream := range sesh.streams {
// If we call stream.Close() here, streamsM will result in a deadlock
// because stream.Close calls sesh.delStream, which locks the mutex.
// so we need to implement a method of stream that closes the stream without calling
// sesh.delStream
go stream.closeNoDelMap()
delete(sesh.streams, id)
}
sesh.streamsM.Unlock()
2018-12-31 11:30:39 +00:00
sesh.sb.closeAll()
2018-10-23 19:47:58 +00:00
return nil
}
2019-01-20 12:13:29 +00:00
2019-07-28 22:27:59 +00:00
func (sesh *Session) IsClosed() bool {
return atomic.LoadUint32(&sesh.closed) == 1
2019-01-20 12:13:29 +00:00
}
func (sesh *Session) timeoutAfter(to time.Duration) {
time.Sleep(to)
sesh.streamsM.Lock()
2019-07-28 22:27:59 +00:00
if len(sesh.streams) == 0 && !sesh.IsClosed() {
sesh.streamsM.Unlock()
sesh.SetTerminalMsg("timeout")
sesh.Close()
} else {
sesh.streamsM.Unlock()
}
}
2019-07-23 10:06:49 +00:00
func (sesh *Session) Addr() net.Addr { return sesh.addrs.Load().([]net.Addr)[0] }