From 21549fcb4a5bdb7281b477594b9071b535087f86 Mon Sep 17 00:00:00 2001 From: Glonee Date: Thu, 1 Jun 2023 13:45:56 +0800 Subject: [PATCH] http3: set tls.Config.ServerName for outgoing requests, if unset (#3867) * fix #3865 * add test case * Update http3/client, client_test.go Co-authored-by: Marten Seemann --------- Co-authored-by: Marten Seemann --- http3/client.go | 9 +++++++++ http3/client_test.go | 16 ++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/http3/client.go b/http3/client.go index 9c94a31e..c54de5ea 100644 --- a/http3/client.go +++ b/http3/client.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "io" + "net" "net/http" "strconv" "sync" @@ -92,6 +93,14 @@ func newClient(hostname string, tlsConf *tls.Config, opts *roundTripperOpts, con } else { tlsConf = tlsConf.Clone() } + if tlsConf.ServerName == "" { + 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])} diff --git a/http3/client_test.go b/http3/client_test.go index d0f68295..2bc00884 100644 --- a/http3/client_test.go +++ b/http3/client_test.go @@ -90,6 +90,22 @@ var _ = Describe("Client", func() { Expect(dialAddrCalled).To(BeTrue()) }) + It("sets the ServerName in the tls.Config, if not set", func() { + const host = "foo.bar" + dialCalled := false + dialFunc := func(ctx context.Context, addr string, tlsCfg *tls.Config, cfg *quic.Config) (quic.EarlyConnection, error) { + Expect(tlsCfg.ServerName).To(Equal(host)) + dialCalled = true + return nil, errors.New("test done") + } + client, err := newClient(host, nil, &roundTripperOpts{}, nil, dialFunc) + Expect(err).ToNot(HaveOccurred()) + req, err := http.NewRequest("GET", "https://foo.bar", nil) + Expect(err).ToNot(HaveOccurred()) + client.RoundTripOpt(req, RoundTripOpt{}) + Expect(dialCalled).To(BeTrue()) + }) + It("uses the TLS config and QUIC config", func() { tlsConf := &tls.Config{ ServerName: "foo.bar",