Cloak/internal/multiplex/switchboard.go

173 lines
4.1 KiB
Go
Raw Normal View History

2018-10-05 22:44:20 +00:00
package multiplex
import (
2018-10-28 22:51:00 +00:00
"errors"
2019-08-02 14:45:33 +00:00
log "github.com/sirupsen/logrus"
"math/rand"
2018-10-05 22:44:20 +00:00
"net"
2018-10-28 21:22:38 +00:00
"sync"
"sync/atomic"
2018-10-05 22:44:20 +00:00
)
2019-08-12 21:43:16 +00:00
const (
FIXED_CONN_MAPPING switchboardStrategy = iota
UNIFORM_SPREAD
)
type switchboardConfig struct {
Valve
2019-08-12 21:43:16 +00:00
strategy switchboardStrategy
}
2018-10-20 20:41:01 +00:00
// switchboard is responsible for keeping the reference of TLS connections between client and server
2018-10-05 22:44:20 +00:00
type switchboard struct {
session *Session
*switchboardConfig
2018-10-28 22:51:00 +00:00
connsM sync.RWMutex
conns map[uint32]net.Conn
nextConnId uint32
2019-08-05 13:33:20 +00:00
broken uint32
2018-11-07 21:16:13 +00:00
}
func makeSwitchboard(sesh *Session, config *switchboardConfig) *switchboard {
2018-11-07 21:16:13 +00:00
// rates are uint64 because in the usermanager we want the bandwidth to be atomically
// operated (so that the bandwidth can change on the fly).
2018-10-05 22:44:20 +00:00
sb := &switchboard{
session: sesh,
switchboardConfig: config,
conns: make(map[uint32]net.Conn),
2018-10-05 22:44:20 +00:00
}
return sb
}
2019-08-05 13:33:20 +00:00
var errNilOptimum = errors.New("The optimal connection is nil")
var errBrokenSwitchboard = errors.New("the switchboard is broken")
2018-10-28 22:51:00 +00:00
func (sb *switchboard) addConn(conn net.Conn) {
connId := atomic.AddUint32(&sb.nextConnId, 1) - 1
sb.connsM.Lock()
sb.conns[connId] = conn
sb.connsM.Unlock()
go sb.deplex(connId, conn)
2018-10-28 21:22:38 +00:00
}
2018-10-20 16:03:39 +00:00
func (sb *switchboard) removeConn(connId uint32) {
sb.connsM.Lock()
delete(sb.conns, connId)
remaining := len(sb.conns)
sb.connsM.Unlock()
if remaining == 0 {
atomic.StoreUint32(&sb.broken, 1)
sb.session.SetTerminalMsg("no underlying connection left")
sb.session.Close()
2018-10-20 16:03:39 +00:00
}
2018-10-28 21:22:38 +00:00
}
2018-10-20 16:03:39 +00:00
// a pointer to connId is passed here so that the switchboard can reassign it
2019-08-14 09:44:50 +00:00
func (sb *switchboard) send(data []byte, connId *uint32) (n int, err error) {
2019-08-31 14:33:23 +00:00
sb.Valve.txWait(len(data))
sb.connsM.RLock()
2019-08-07 12:59:27 +00:00
defer sb.connsM.RUnlock()
2019-08-12 21:43:16 +00:00
if sb.strategy == UNIFORM_SPREAD {
randConnId := rand.Intn(len(sb.conns))
conn, ok := sb.conns[uint32(randConnId)]
if !ok {
return 0, errBrokenSwitchboard
} else {
2019-08-14 09:44:50 +00:00
n, err = conn.Write(data)
sb.AddTx(int64(n))
return
2019-08-12 21:43:16 +00:00
}
} else {
var conn net.Conn
conn, ok := sb.conns[*connId]
if ok {
2019-08-14 09:44:50 +00:00
n, err = conn.Write(data)
sb.AddTx(int64(n))
return
2019-08-12 21:43:16 +00:00
} else {
// do not call assignRandomConn() here.
// we'll have to do connsM.RLock() after we get a new connId from assignRandomConn, in order to
// get the new conn through conns[newConnId]
// however between connsM.RUnlock() in assignRandomConn and our call to connsM.RLock(), things may happen.
// in particular if newConnId is removed between the RUnlock and RLock, conns[newConnId] will return
// a nil pointer. To prevent this we must get newConnId and the reference to conn itself in one single mutex
// protection
if atomic.LoadUint32(&sb.broken) == 1 || len(sb.conns) == 0 {
return 0, errBrokenSwitchboard
}
2019-08-31 14:32:57 +00:00
r := rand.Intn(len(sb.conns))
var c int
for newConnId := range sb.conns {
if r == c {
connId = &newConnId
conn, _ = sb.conns[newConnId]
n, err = conn.Write(data)
sb.AddTx(int64(n))
return
}
c++
2019-08-21 12:14:23 +00:00
}
2019-08-31 14:32:57 +00:00
return 0, errBrokenSwitchboard
}
2018-10-28 21:22:38 +00:00
}
2018-10-05 22:44:20 +00:00
}
2019-08-20 21:43:04 +00:00
// returns a random connId
func (sb *switchboard) assignRandomConn() (uint32, error) {
2019-08-06 20:04:08 +00:00
sb.connsM.RLock()
2019-08-07 12:59:27 +00:00
defer sb.connsM.RUnlock()
2019-08-06 20:04:08 +00:00
if atomic.LoadUint32(&sb.broken) == 1 || len(sb.conns) == 0 {
return 0, errBrokenSwitchboard
2018-10-28 21:22:38 +00:00
}
2019-08-31 14:32:57 +00:00
r := rand.Intn(len(sb.conns))
var c int
for connId := range sb.conns {
if r == c {
return connId, nil
}
c++
}
return 0, errBrokenSwitchboard
2018-10-28 21:22:38 +00:00
}
2018-12-30 01:15:46 +00:00
// actively triggered by session.Close()
2018-12-31 11:30:39 +00:00
func (sb *switchboard) closeAll() {
2019-08-05 13:33:20 +00:00
if atomic.SwapUint32(&sb.broken, 1) == 1 {
return
}
sb.connsM.RLock()
for key, conn := range sb.conns {
conn.Close()
delete(sb.conns, key)
2018-10-28 21:22:38 +00:00
}
sb.connsM.RUnlock()
2018-10-05 22:44:20 +00:00
}
2019-08-05 13:33:20 +00:00
// deplex function costantly reads from a TCP connection
func (sb *switchboard) deplex(connId uint32, conn net.Conn) {
2018-10-16 20:13:19 +00:00
buf := make([]byte, 20480)
2018-10-09 20:53:55 +00:00
for {
2019-08-11 23:22:15 +00:00
n, err := sb.session.UnitRead(conn, buf)
2018-11-07 21:16:13 +00:00
sb.rxWait(n)
sb.Valve.AddRx(int64(n))
2018-10-09 20:53:55 +00:00
if err != nil {
2019-08-31 14:32:57 +00:00
log.Debugf("a connection for session %v has closed: %v", sb.session.id, err)
go conn.Close()
sb.removeConn(connId)
2018-10-09 20:53:55 +00:00
return
}
2019-08-16 22:47:15 +00:00
err = sb.session.recvDataFromRemote(buf[:n])
if err != nil {
log.Error(err)
}
2018-10-05 22:44:20 +00:00
}
}