mirror of https://github.com/cbeuw/Cloak
Refactor switchboard
This commit is contained in:
parent
d46fa74924
commit
fac381dbca
|
|
@ -6,6 +6,8 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
@ -105,27 +107,30 @@ func (sesh *Session) delStream(id uint32) {
|
||||||
sesh.streamsM.Unlock()
|
sesh.streamsM.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
// either fetch an existing stream or instantiate a new stream and put it in the dict, and return it
|
func (sesh *Session) recvDataFromRemote(data []byte) {
|
||||||
func (sesh *Session) getStream(id uint32, closingFrame bool) *Stream {
|
frame, err := sesh.Deobfs(data)
|
||||||
// it would have been neater to use defer Unlock(), however it gives
|
if err != nil {
|
||||||
// non-negligable overhead and this function is performance critical
|
log.Debugf("Failed to decrypt a frame for session %v: %v", sesh.id, err)
|
||||||
|
}
|
||||||
|
|
||||||
sesh.streamsM.Lock()
|
sesh.streamsM.Lock()
|
||||||
defer sesh.streamsM.Unlock()
|
defer sesh.streamsM.Unlock()
|
||||||
stream := sesh.streams[id]
|
stream, existing := sesh.streams[frame.StreamID]
|
||||||
if stream != nil {
|
if existing {
|
||||||
return stream
|
stream.writeFrame(frame)
|
||||||
} else {
|
} else {
|
||||||
if closingFrame {
|
if frame.Closing == 1 {
|
||||||
// If the stream has been closed and the current frame is a closing frame,
|
// If the stream has been closed and the current frame is a closing frame, we do noop
|
||||||
// we return nil
|
return
|
||||||
return nil
|
|
||||||
} else {
|
} else {
|
||||||
stream = makeStream(id, sesh)
|
stream = makeStream(frame.StreamID, sesh)
|
||||||
sesh.streams[id] = stream
|
sesh.streams[frame.StreamID] = stream
|
||||||
sesh.acceptCh <- stream
|
sesh.acceptCh <- stream
|
||||||
return stream
|
stream.writeFrame(frame)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sesh *Session) SetTerminalMsg(msg string) {
|
func (sesh *Session) SetTerminalMsg(msg string) {
|
||||||
|
|
@ -156,6 +161,7 @@ func (sesh *Session) Close() error {
|
||||||
sesh.streamsM.Unlock()
|
sesh.streamsM.Unlock()
|
||||||
|
|
||||||
sesh.sb.closeAll()
|
sesh.sb.closeAll()
|
||||||
|
log.Debugf("session %v closed gracefully", sesh.id)
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -51,6 +51,8 @@ func makeStream(id uint32, sesh *Session) *Stream {
|
||||||
|
|
||||||
func (s *Stream) isClosed() bool { return atomic.LoadUint32(&s.closed) == 1 }
|
func (s *Stream) isClosed() bool { return atomic.LoadUint32(&s.closed) == 1 }
|
||||||
|
|
||||||
|
func (s *Stream) writeFrame(frame *Frame) { s.sorter.writeNewFrame(frame) }
|
||||||
|
|
||||||
func (s *Stream) Read(buf []byte) (n int, err error) {
|
func (s *Stream) Read(buf []byte) (n int, err error) {
|
||||||
if len(buf) == 0 {
|
if len(buf) == 0 {
|
||||||
if s.isClosed() {
|
if s.isClosed() {
|
||||||
|
|
@ -94,7 +96,7 @@ func (s *Stream) Write(in []byte) (n int, err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return i, err
|
return i, err
|
||||||
}
|
}
|
||||||
n, err = s.session.sb.send(s.obfsBuf[:i])
|
n, err = s.session.sb.Write(s.obfsBuf[:i])
|
||||||
return
|
return
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -137,7 +139,7 @@ func (s *Stream) Close() error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
_, err = s.session.sb.send(s.obfsBuf[:i])
|
_, err = s.session.sb.Write(s.obfsBuf[:i])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,8 @@ type switchboard struct {
|
||||||
optimum atomic.Value // *connEnclave
|
optimum atomic.Value // *connEnclave
|
||||||
cesM sync.RWMutex
|
cesM sync.RWMutex
|
||||||
ces []*connEnclave
|
ces []*connEnclave
|
||||||
|
|
||||||
|
broken uint32
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sb *switchboard) getOptimum() *connEnclave {
|
func (sb *switchboard) getOptimum() *connEnclave {
|
||||||
|
|
@ -48,9 +50,13 @@ func makeSwitchboard(sesh *Session, valve *Valve) *switchboard {
|
||||||
return sb
|
return sb
|
||||||
}
|
}
|
||||||
|
|
||||||
var errNilOptimum error = errors.New("The optimal connection is nil")
|
var errNilOptimum = errors.New("The optimal connection is nil")
|
||||||
|
var errBrokenSwitchboard = errors.New("the switchboard is broken")
|
||||||
|
|
||||||
func (sb *switchboard) send(data []byte) (int, error) {
|
func (sb *switchboard) Write(data []byte) (int, error) {
|
||||||
|
if atomic.LoadUint32(&sb.broken) == 1 {
|
||||||
|
return 0, errBrokenSwitchboard
|
||||||
|
}
|
||||||
ce := sb.getOptimum()
|
ce := sb.getOptimum()
|
||||||
if ce == nil {
|
if ce == nil {
|
||||||
return 0, errNilOptimum
|
return 0, errNilOptimum
|
||||||
|
|
@ -104,17 +110,20 @@ func (sb *switchboard) removeConn(closing *connEnclave) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(sb.ces) == 0 {
|
remaining := len(sb.ces)
|
||||||
sb.session.SetTerminalMsg("no underlying connection left")
|
|
||||||
sb.cesM.Unlock()
|
|
||||||
sb.session.Close()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
sb.cesM.Unlock()
|
sb.cesM.Unlock()
|
||||||
|
if remaining == 0 {
|
||||||
|
atomic.StoreUint32(&sb.broken, 1)
|
||||||
|
sb.session.SetTerminalMsg("no underlying connection left")
|
||||||
|
sb.session.Close()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// actively triggered by session.Close()
|
// actively triggered by session.Close()
|
||||||
func (sb *switchboard) closeAll() {
|
func (sb *switchboard) closeAll() {
|
||||||
|
if atomic.SwapUint32(&sb.broken, 1) == 1 {
|
||||||
|
return
|
||||||
|
}
|
||||||
sb.cesM.RLock()
|
sb.cesM.RLock()
|
||||||
for _, ce := range sb.ces {
|
for _, ce := range sb.ces {
|
||||||
ce.remoteConn.Close()
|
ce.remoteConn.Close()
|
||||||
|
|
@ -122,8 +131,7 @@ func (sb *switchboard) closeAll() {
|
||||||
sb.cesM.RUnlock()
|
sb.cesM.RUnlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
// deplex function costantly reads from a TCP connection, call Deobfs and distribute it
|
// deplex function costantly reads from a TCP connection
|
||||||
// to the corresponding stream
|
|
||||||
func (sb *switchboard) deplex(ce *connEnclave) {
|
func (sb *switchboard) deplex(ce *connEnclave) {
|
||||||
buf := make([]byte, 20480)
|
buf := make([]byte, 20480)
|
||||||
for {
|
for {
|
||||||
|
|
@ -137,18 +145,6 @@ func (sb *switchboard) deplex(ce *connEnclave) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
frame, err := sb.session.Deobfs(buf[:n])
|
sb.session.recvDataFromRemote(buf[:n])
|
||||||
if err != nil {
|
|
||||||
log.Debugf("Failed to decrypt a frame for session %v: %v", sb.session.id, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
stream := sb.session.getStream(frame.StreamID, frame.Closing == 1)
|
|
||||||
// if the frame is telling us to close a closed stream
|
|
||||||
// (this happens when ss-server and ss-local closes the stream
|
|
||||||
// simutaneously), we don't do anything
|
|
||||||
if stream != nil {
|
|
||||||
stream.sorter.writeNewFrame(frame)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue