diff --git a/internal/multiplex/switchboard.go b/internal/multiplex/switchboard.go index c4bb416..fc4edd1 100644 --- a/internal/multiplex/switchboard.go +++ b/internal/multiplex/switchboard.go @@ -58,10 +58,10 @@ func (sb *switchboard) removeConn(connId uint32) { // a pointer to connId is passed here so that the switchboard can reassign it func (sb *switchboard) send(data []byte, connId *uint32) (int, error) { - var conn net.Conn sb.connsM.RLock() + defer sb.connsM.RUnlock() + var conn net.Conn conn, ok := sb.conns[*connId] - sb.connsM.RUnlock() if ok { return conn.Write(data) } else { @@ -72,13 +72,11 @@ func (sb *switchboard) send(data []byte, connId *uint32) (int, error) { // 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 - sb.connsM.RLock() if atomic.LoadUint32(&sb.broken) == 1 || len(sb.conns) == 0 { return 0, errBrokenSwitchboard } newConnId := rand.Intn(len(sb.conns)) conn = sb.conns[uint32(newConnId)] - sb.connsM.RUnlock() return conn.Write(data) } @@ -86,11 +84,11 @@ func (sb *switchboard) send(data []byte, connId *uint32) (int, error) { func (sb *switchboard) assignRandomConn() (uint32, error) { sb.connsM.RLock() + defer sb.connsM.RUnlock() if atomic.LoadUint32(&sb.broken) == 1 || len(sb.conns) == 0 { return 0, errBrokenSwitchboard } connId := rand.Intn(len(sb.conns)) - sb.connsM.RUnlock() return uint32(connId), nil } diff --git a/internal/server/TLS.go b/internal/server/TLS.go index 7302141..58af276 100644 --- a/internal/server/TLS.go +++ b/internal/server/TLS.go @@ -161,12 +161,6 @@ func parseClientHello(data []byte) (ret *ClientHello, err error) { return } -func xor(a []byte, b []byte) { - for i := range a { - a[i] ^= b[i] - } -} - func composeServerHello(sessionId []byte, sharedSecret []byte, sessionKey []byte) ([]byte, error) { nonce := make([]byte, 12) rand.Read(nonce)