Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions initiator.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"crypto/tls"
"fmt"
"net"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -161,19 +162,30 @@ func (i *Initiator) handleConnection(session *session, tlsConfig *tls.Config, di

connectionAttempt := 0

// pendingConn holds the raw TCP connection during TLS handshake so the
// stop-goroutine can close it to unblock a stuck Handshake() call.
var pendingConn net.Conn
var connMu sync.Mutex

for {
if !i.waitForInSessionTime(session) {
return
}

ctx, cancel := context.WithCancel(context.Background())

// We start a goroutine in order to be able to cancel the dialer mid-connection
// on receiving a stop signal to stop the initiator.
// We start a goroutine in order to be able to cancel the dialer
// mid-connection and to close the raw TCP socket if a TLS handshake
// is in progress when a stop signal is received.
go func() {
select {
case <-i.stopChan:
cancel()
connMu.Lock()
if pendingConn != nil {
pendingConn.Close()
}
connMu.Unlock()
case <-ctx.Done():
return
}
Expand All @@ -200,11 +212,26 @@ func (i *Initiator) handleConnection(session *session, tlsConfig *tls.Config, di
}
tlsConfig.ServerName = serverName
}

// Store the raw TCP connection so the stop-goroutine can close it
// to unblock Handshake() if stop is signaled during TLS negotiation.
connMu.Lock()
pendingConn = netConn
connMu.Unlock()

tlsConn := tls.Client(netConn, tlsConfig)
if err = tlsConn.Handshake(); err != nil {
connMu.Lock()
pendingConn = nil
connMu.Unlock()
session.log.OnEventf("Failed handshake: %v", err)
goto reconnect
}

connMu.Lock()
pendingConn = nil
connMu.Unlock()

netConn = tlsConn
}

Expand Down
Loading