Skip to content

Commit

Permalink
support quic-go v0.35.1
Browse files Browse the repository at this point in the history
  • Loading branch information
imroc committed Jun 16, 2023
1 parent 5aa91a6 commit dd9c751
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
8 changes: 6 additions & 2 deletions internal/http3/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,12 @@ func newClient(hostname string, tlsConf *tls.Config, opts *roundTripperOpts, con
tlsConf = tlsConf.Clone()
}
if tlsConf.ServerName == "" {
host, _, _ := net.SplitHostPort(hostname)
tlsConf.ServerName = host
sni, _, err := net.SplitHostPort(hostname)
if err != nil {
// It's ok if net.SplitHostPort returns an error - it could be a hostname/IP address without a port.
sni = hostname
}
tlsConf.ServerName = sni
}
// Replace existing ALPNs by H3
tlsConf.NextProtos = []string{versionToALPN(conf.Versions[0])}
Expand Down
23 changes: 13 additions & 10 deletions internal/http3/roundtrip.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ import (
"golang.org/x/net/http/httpguts"
)

// declare this as a variable, such that we can it mock it in the tests
var quicDialer = quic.DialEarly

type roundTripCloser interface {
RoundTripOpt(*http.Request, RoundTripOpt) (*http.Response, error)
HandshakeComplete() bool
Expand Down Expand Up @@ -74,7 +71,7 @@ type RoundTripper struct {

newClient func(hostname string, tlsConf *tls.Config, opts *roundTripperOpts, conf *quic.Config, dialer dialFunc, opt *transport.Options) (roundTripCloser, error) // so we can mock it in tests
clients map[string]*roundTripCloserWithCount
udpConn *net.UDPConn
transport *quic.Transport
}

// RoundTripOpt are options for the Transport.RoundTripOpt method.
Expand Down Expand Up @@ -206,11 +203,12 @@ func (r *RoundTripper) getClient(hostname string, onlyCached bool) (rtc *roundTr
}
dial := r.Dial
if dial == nil {
if r.udpConn == nil {
r.udpConn, err = net.ListenUDP("udp", nil)
if r.transport == nil {
udpConn, err := net.ListenUDP("udp", nil)
if err != nil {
return nil, false, err
}
r.transport = &quic.Transport{Conn: udpConn}
}
dial = r.makeDialer()
}
Expand Down Expand Up @@ -259,9 +257,14 @@ func (r *RoundTripper) Close() error {
}
}
r.clients = nil
if r.udpConn != nil {
r.udpConn.Close()
r.udpConn = nil
if r.transport != nil {
if err := r.transport.Close(); err != nil {
return err
}
if err := r.transport.Conn.Close(); err != nil {
return err
}
r.transport = nil
}
return nil
}
Expand Down Expand Up @@ -301,7 +304,7 @@ func (r *RoundTripper) makeDialer() func(ctx context.Context, addr string, tlsCf
if err != nil {
return nil, err
}
return quicDialer(ctx, r.udpConn, udpAddr, tlsCfg, cfg)
return r.transport.DialEarly(ctx, udpAddr, tlsCfg, cfg)
}
}

Expand Down

0 comments on commit dd9c751

Please sign in to comment.