forked from quic-go/quic-go
implement DialNonFWSecure for the client
This commit is contained in:
31
session.go
31
session.go
@@ -38,6 +38,11 @@ var (
|
||||
// Once the callback has been called with isForwardSecure = true, it is guarantueed to not be called with isForwardSecure = false after that
|
||||
type cryptoChangeCallback func(session Session, isForwardSecure bool)
|
||||
|
||||
type handshakeEvent struct {
|
||||
encLevel protocol.EncryptionLevel
|
||||
err error
|
||||
}
|
||||
|
||||
type closeError struct {
|
||||
err error
|
||||
remote bool
|
||||
@@ -88,6 +93,9 @@ type session struct {
|
||||
// will be closed as soon as the handshake completes, and receive any error that might occur until then
|
||||
// it is used to block WaitUntilHandshakeComplete()
|
||||
handshakeCompleteChan chan error
|
||||
// handshakeChan receives handshake events and is closed as soon the handshake completes
|
||||
// the receiving end of this channel is passed to the creator of the session
|
||||
handshakeChan chan<- handshakeEvent
|
||||
|
||||
nextAckScheduledTime time.Time
|
||||
|
||||
@@ -139,6 +147,8 @@ func newSession(
|
||||
}
|
||||
aeadChanged := make(chan protocol.EncryptionLevel, 2)
|
||||
s.aeadChanged = aeadChanged
|
||||
handshakeChan := make(chan handshakeEvent, 1)
|
||||
s.handshakeChan = handshakeChan
|
||||
var err error
|
||||
s.cryptoSetup, err = handshake.NewCryptoSetup(connectionID, sourceAddr, v, sCfg, cryptoStream, s.connectionParameters, config.Versions, aeadChanged)
|
||||
if err != nil {
|
||||
@@ -156,10 +166,9 @@ func newClientSession(
|
||||
hostname string,
|
||||
v protocol.VersionNumber,
|
||||
connectionID protocol.ConnectionID,
|
||||
cryptoChangeCallback cryptoChangeCallback,
|
||||
config *Config,
|
||||
negotiatedVersions []protocol.VersionNumber,
|
||||
) (*session, error) {
|
||||
) (*session, <-chan handshakeEvent, error) {
|
||||
s := &session{
|
||||
conn: conn,
|
||||
connectionID: connectionID,
|
||||
@@ -167,7 +176,6 @@ func newClientSession(
|
||||
version: v,
|
||||
config: config,
|
||||
|
||||
cryptoChangeCallback: cryptoChangeCallback,
|
||||
connectionParameters: handshake.NewConnectionParamatersManager(protocol.PerspectiveClient, v),
|
||||
}
|
||||
|
||||
@@ -176,6 +184,8 @@ func newClientSession(
|
||||
|
||||
aeadChanged := make(chan protocol.EncryptionLevel, 2)
|
||||
s.aeadChanged = aeadChanged
|
||||
handshakeChan := make(chan handshakeEvent, 1)
|
||||
s.handshakeChan = handshakeChan
|
||||
cryptoStream, _ := s.OpenStream()
|
||||
var err error
|
||||
s.cryptoSetup, err = handshake.NewCryptoSetupClient(
|
||||
@@ -190,13 +200,13 @@ func newClientSession(
|
||||
negotiatedVersions,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
s.packer = newPacketPacker(connectionID, s.cryptoSetup, s.connectionParameters, s.streamFramer, s.perspective, s.version)
|
||||
s.unpacker = &packetUnpacker{aead: s.cryptoSetup, version: s.version}
|
||||
|
||||
return s, err
|
||||
return s, handshakeChan, err
|
||||
}
|
||||
|
||||
// setup is called from newSession and newClientSession and initializes values that are independent of the perspective
|
||||
@@ -275,16 +285,22 @@ runLoop:
|
||||
// begins with the public header and we never copy it.
|
||||
putPacketBuffer(p.publicHeader.Raw)
|
||||
case l, ok := <-aeadChanged:
|
||||
if !ok {
|
||||
if !ok { // the aeadChanged chan was closed. This means that the handshake is completed.
|
||||
s.handshakeComplete = true
|
||||
aeadChanged = nil // prevent this case from ever being selected again
|
||||
close(s.handshakeChan)
|
||||
close(s.handshakeCompleteChan)
|
||||
} else {
|
||||
if l == protocol.EncryptionForwardSecure {
|
||||
s.packer.SetForwardSecure()
|
||||
}
|
||||
s.tryDecryptingQueuedPackets()
|
||||
s.cryptoChangeCallback(s, l == protocol.EncryptionForwardSecure)
|
||||
// TODO: remove this, when removing the cryptoChangeCallback for the server
|
||||
if s.perspective == protocol.PerspectiveServer {
|
||||
s.cryptoChangeCallback(s, l == protocol.EncryptionForwardSecure)
|
||||
} else {
|
||||
s.handshakeChan <- handshakeEvent{encLevel: l}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -314,6 +330,7 @@ runLoop:
|
||||
// otherwise this chan will already be closed
|
||||
if !s.handshakeComplete {
|
||||
s.handshakeCompleteChan <- closeErr.err
|
||||
s.handshakeChan <- handshakeEvent{err: closeErr.err}
|
||||
}
|
||||
s.handleCloseError(closeErr)
|
||||
close(s.runClosed)
|
||||
|
||||
Reference in New Issue
Block a user