Skip to content

Commit

Permalink
webtransport: only add cert hashes if we already started listening
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed May 5, 2023
1 parent 410248e commit 4151cd5
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions p2p/transport/webtransport/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"io"
"sync"
"sync/atomic"
"time"

"github.com/libp2p/go-libp2p/core/connmgr"
Expand Down Expand Up @@ -68,12 +69,12 @@ type transport struct {
rcmgr network.ResourceManager
gater connmgr.ConnectionGater

listenOnce sync.Once
listenOnceErr error
certManager *certManager
certManagerReady chan struct{} // Closed when the certManager has been instantiated.
staticTLSConf *tls.Config
tlsClientConf *tls.Config
listenOnce sync.Once
listenOnceErr error
certManager *certManager
hasCertManager atomic.Bool // set to true once the certManager is initialized
staticTLSConf *tls.Config
tlsClientConf *tls.Config

noise *noise.Transport

Expand All @@ -98,14 +99,13 @@ func New(key ic.PrivKey, psk pnet.PSK, connManager *quicreuse.ConnManager, gater
return nil, err
}
t := &transport{
pid: id,
privKey: key,
rcmgr: rcmgr,
gater: gater,
clock: clock.New(),
connManager: connManager,
conns: map[uint64]*conn{},
certManagerReady: make(chan struct{}),
pid: id,
privKey: key,
rcmgr: rcmgr,
gater: gater,
clock: clock.New(),
connManager: connManager,
conns: map[uint64]*conn{},
}
for _, opt := range opts {
if err := opt(t); err != nil {
Expand Down Expand Up @@ -300,13 +300,12 @@ func (t *transport) Listen(laddr ma.Multiaddr) (tpt.Listener, error) {
if t.staticTLSConf == nil {
t.listenOnce.Do(func() {
t.certManager, t.listenOnceErr = newCertManager(t.privKey, t.clock)
close(t.certManagerReady)
t.hasCertManager.Store(true)
})
if t.listenOnceErr != nil {
return nil, t.listenOnceErr
}
} else {
close(t.certManagerReady)
return nil, errors.New("static TLS config not supported on WebTransport")
}
tlsConf := t.staticTLSConf.Clone()
Expand Down Expand Up @@ -405,9 +404,10 @@ func (t *transport) Resolve(_ context.Context, maddr ma.Multiaddr) ([]ma.Multiad
return []ma.Multiaddr{beforeQuicMA.Encapsulate(quicComponent).Encapsulate(sniComponent).Encapsulate(afterQuicMA)}, nil
}

// AddCertHashes adds the current certificate hashes to a multiaddress.
// If called before Listen, it's a no-op.
func (t *transport) AddCertHashes(m ma.Multiaddr) ma.Multiaddr {
<-t.certManagerReady
if t.certManager == nil {
if !t.hasCertManager.Load() {
return m
}
return m.Encapsulate(t.certManager.AddrComponent())
Expand Down

0 comments on commit 4151cd5

Please sign in to comment.