Browse Source

webtransport: simplify listener constructor

pull/1828/head
Marten Seemann 2 years ago
parent
commit
ead3e4f981
  1. 35
      p2p/transport/webtransport/listener.go
  2. 2
      p2p/transport/webtransport/transport.go

35
p2p/transport/webtransport/listener.go

@ -9,13 +9,11 @@ import (
"net/http" "net/http"
"time" "time"
"github.com/libp2p/go-libp2p/core/connmgr"
"github.com/libp2p/go-libp2p/core/network" "github.com/libp2p/go-libp2p/core/network"
tpt "github.com/libp2p/go-libp2p/core/transport" tpt "github.com/libp2p/go-libp2p/core/transport"
"github.com/libp2p/go-libp2p/p2p/security/noise" "github.com/libp2p/go-libp2p/p2p/security/noise"
"github.com/libp2p/go-libp2p/p2p/security/noise/pb" "github.com/libp2p/go-libp2p/p2p/security/noise/pb"
"github.com/lucas-clemente/quic-go"
"github.com/lucas-clemente/quic-go/http3" "github.com/lucas-clemente/quic-go/http3"
"github.com/marten-seemann/webtransport-go" "github.com/marten-seemann/webtransport-go"
ma "github.com/multiformats/go-multiaddr" ma "github.com/multiformats/go-multiaddr"
@ -28,15 +26,10 @@ const queueLen = 16
const handshakeTimeout = 10 * time.Second const handshakeTimeout = 10 * time.Second
type listener struct { type listener struct {
transport tpt.Transport transport *transport
noise *noise.Transport
certManager *certManager
tlsConf *tls.Config tlsConf *tls.Config
isStaticTLSConf bool isStaticTLSConf bool
rcmgr network.ResourceManager
gater connmgr.ConnectionGater
server webtransport.Server server webtransport.Server
ctx context.Context ctx context.Context
@ -52,7 +45,7 @@ type listener struct {
var _ tpt.Listener = &listener{} var _ tpt.Listener = &listener{}
func newListener(laddr ma.Multiaddr, transport tpt.Transport, noise *noise.Transport, certManager *certManager, tlsConf *tls.Config, quicConf *quic.Config, gater connmgr.ConnectionGater, rcmgr network.ResourceManager) (tpt.Listener, error) { func newListener(laddr ma.Multiaddr, t *transport, tlsConf *tls.Config) (tpt.Listener, error) {
network, addr, err := manet.DialArgs(laddr) network, addr, err := manet.DialArgs(laddr)
if err != nil { if err != nil {
return nil, err return nil, err
@ -72,24 +65,20 @@ func newListener(laddr ma.Multiaddr, transport tpt.Transport, noise *noise.Trans
isStaticTLSConf := tlsConf != nil isStaticTLSConf := tlsConf != nil
if tlsConf == nil { if tlsConf == nil {
tlsConf = &tls.Config{GetConfigForClient: func(*tls.ClientHelloInfo) (*tls.Config, error) { tlsConf = &tls.Config{GetConfigForClient: func(*tls.ClientHelloInfo) (*tls.Config, error) {
return certManager.GetConfig(), nil return t.certManager.GetConfig(), nil
}} }}
} }
ln := &listener{ ln := &listener{
transport: transport, transport: t,
noise: noise,
certManager: certManager,
tlsConf: tlsConf, tlsConf: tlsConf,
isStaticTLSConf: isStaticTLSConf, isStaticTLSConf: isStaticTLSConf,
rcmgr: rcmgr,
gater: gater,
queue: make(chan tpt.CapableConn, queueLen), queue: make(chan tpt.CapableConn, queueLen),
serverClosed: make(chan struct{}), serverClosed: make(chan struct{}),
addr: udpConn.LocalAddr(), addr: udpConn.LocalAddr(),
multiaddr: localMultiaddr, multiaddr: localMultiaddr,
server: webtransport.Server{ server: webtransport.Server{
H3: http3.Server{ H3: http3.Server{
QuicConfig: quicConf, QuicConfig: t.quicConfig,
TLSConfig: tlsConf, TLSConfig: tlsConf,
}, },
CheckOrigin: func(r *http.Request) bool { return true }, CheckOrigin: func(r *http.Request) bool { return true },
@ -123,12 +112,12 @@ func (l *listener) httpHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
return return
} }
if l.gater != nil && !l.gater.InterceptAccept(&connMultiaddrs{local: l.multiaddr, remote: remoteMultiaddr}) { if l.transport.gater != nil && !l.transport.gater.InterceptAccept(&connMultiaddrs{local: l.multiaddr, remote: remoteMultiaddr}) {
w.WriteHeader(http.StatusForbidden) w.WriteHeader(http.StatusForbidden)
return return
} }
connScope, err := l.rcmgr.OpenConnection(network.DirInbound, false, remoteMultiaddr) connScope, err := l.transport.rcmgr.OpenConnection(network.DirInbound, false, remoteMultiaddr)
if err != nil { if err != nil {
log.Debugw("resource manager blocked incoming connection", "addr", r.RemoteAddr, "error", err) log.Debugw("resource manager blocked incoming connection", "addr", r.RemoteAddr, "error", err)
w.WriteHeader(http.StatusServiceUnavailable) w.WriteHeader(http.StatusServiceUnavailable)
@ -154,7 +143,7 @@ func (l *listener) httpHandler(w http.ResponseWriter, r *http.Request) {
} }
cancel() cancel()
if l.gater != nil && !l.gater.InterceptSecured(network.DirInbound, sconn.RemotePeer(), sconn) { if l.transport.gater != nil && !l.transport.gater.InterceptSecured(network.DirInbound, sconn.RemotePeer(), sconn) {
// TODO: can we close with a specific error here? // TODO: can we close with a specific error here?
sess.Close() sess.Close()
connScope.Done() connScope.Done()
@ -202,10 +191,10 @@ func (l *listener) handshake(ctx context.Context, sess *webtransport.Session) (*
} }
var earlyData [][]byte var earlyData [][]byte
if !l.isStaticTLSConf { if !l.isStaticTLSConf {
earlyData = l.certManager.SerializedCertHashes() earlyData = l.transport.certManager.SerializedCertHashes()
} }
n, err := l.noise.WithSessionOptions(noise.EarlyData( n, err := l.transport.noise.WithSessionOptions(noise.EarlyData(
nil, nil,
newEarlyDataSender(&pb.NoiseExtensions{WebtransportCerthashes: earlyData}), newEarlyDataSender(&pb.NoiseExtensions{WebtransportCerthashes: earlyData}),
)) ))
@ -228,10 +217,10 @@ func (l *listener) Addr() net.Addr {
} }
func (l *listener) Multiaddr() ma.Multiaddr { func (l *listener) Multiaddr() ma.Multiaddr {
if l.certManager == nil { if l.transport.certManager == nil {
return l.multiaddr return l.multiaddr
} }
return l.multiaddr.Encapsulate(l.certManager.AddrComponent()) return l.multiaddr.Encapsulate(l.transport.certManager.AddrComponent())
} }
func (l *listener) Close() error { func (l *listener) Close() error {

2
p2p/transport/webtransport/transport.go

@ -294,7 +294,7 @@ func (t *transport) Listen(laddr ma.Multiaddr) (tpt.Listener, error) {
return nil, t.listenOnceErr return nil, t.listenOnceErr
} }
} }
return newListener(laddr, t, t.noise, t.certManager, t.staticTLSConf, t.quicConfig, t.gater, t.rcmgr) return newListener(laddr, t, t.staticTLSConf)
} }
func (t *transport) Protocols() []int { func (t *transport) Protocols() []int {

Loading…
Cancel
Save