diff --git a/http3/server.go b/http3/server.go index b798abd42..ce5bc909f 100644 --- a/http3/server.go +++ b/http3/server.go @@ -92,7 +92,11 @@ type Server struct { // See https://www.ietf.org/archive/id/draft-schinazi-masque-h3-datagram-02.html. EnableDatagrams bool - port uint32 // used atomically + // The port to use in Alt-Svc response headers. + // If needed Port can be manually set when the Server is created. + // This is useful when a Layer 4 firewall is redirecting UDP traffic and clients must use + // a port different from the port the Server is listening on. + Port uint32 mutex sync.Mutex listeners map[*quic.EarlyListener]struct{} @@ -436,7 +440,7 @@ func (s *Server) CloseGracefully(timeout time.Duration) error { // The values that are set depend on the port information from s.Server.Addr, and currently look like this (if Addr has port 443): // Alt-Svc: quic=":443"; ma=2592000; v="33,32,31,30" func (s *Server) SetQuicHeaders(hdr http.Header) error { - port := atomic.LoadUint32(&s.port) + port := atomic.LoadUint32(&s.Port) if port == 0 { // Extract port from s.Server.Addr @@ -449,7 +453,7 @@ func (s *Server) SetQuicHeaders(hdr http.Header) error { return err } port = uint32(portInt) - atomic.StoreUint32(&s.port, port) + atomic.StoreUint32(&s.Port, port) } // This code assumes that we will use protocol.SupportedVersions if no quic.Config is passed. diff --git a/http3/server_test.go b/http3/server_test.go index 04896ad39..02e9c4166 100644 --- a/http3/server_test.go +++ b/http3/server_test.go @@ -591,6 +591,14 @@ var _ = Describe("Server", func() { Expect(s.SetQuicHeaders(hdr)).To(Succeed()) Expect(hdr).To(Equal(http.Header{"Alt-Svc": {`h3=":443"; ma=2592000,h3-29=":443"; ma=2592000`}})) }) + + It("uses s.Port if set to a non-zero value", func() { + s.Server.Addr = ":443" + s.Port = 8443 + hdr := http.Header{} + Expect(s.SetQuicHeaders(hdr)).To(Succeed()) + Expect(hdr).To(Equal(http.Header{"Alt-Svc": {`h3-29=":8443"; ma=2592000`}})) + }) }) It("errors when ListenAndServe is called with s.Server nil", func() {