-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow use of custom port value in Alt-Svc header. #3272
Changes from 1 commit
ce8167c
8b56e7d
43ee0c6
23665f1
afbe993
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -92,7 +92,8 @@ type Server struct { | |
// See https://www.ietf.org/archive/id/draft-schinazi-masque-h3-datagram-02.html. | ||
EnableDatagrams bool | ||
|
||
port uint32 // used atomically | ||
port uint32 // used atomically | ||
customAltSvcPort uint32 // custom port used for Alt-Svc response header | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it should be fine just to export |
||
|
||
mutex sync.Mutex | ||
listeners map[*quic.EarlyListener]struct{} | ||
|
@@ -439,16 +440,22 @@ func (s *Server) SetQuicHeaders(hdr http.Header) error { | |
port := atomic.LoadUint32(&s.port) | ||
|
||
if port == 0 { | ||
// Extract port from s.Server.Addr | ||
_, portStr, err := net.SplitHostPort(s.Server.Addr) | ||
if err != nil { | ||
return err | ||
} | ||
portInt, err := net.LookupPort("tcp", portStr) | ||
if err != nil { | ||
return err | ||
if s.customAltSvcPort != 0 { | ||
// Use customAltSvcPort if set | ||
port = s.customAltSvcPort | ||
} else { | ||
// Extract port from s.Server.Addr | ||
_, portStr, err := net.SplitHostPort(s.Server.Addr) | ||
if err != nil { | ||
return err | ||
} | ||
portInt, err := net.LookupPort("tcp", portStr) | ||
if err != nil { | ||
return err | ||
} | ||
port = uint32(portInt) | ||
} | ||
port = uint32(portInt) | ||
|
||
atomic.StoreUint32(&s.port, port) | ||
} | ||
|
||
|
@@ -486,6 +493,17 @@ func ListenAndServeQUIC(addr, certFile, keyFile string, handler http.Handler) er | |
// http.DefaultServeMux is used when handler is nil. | ||
// The correct Alt-Svc headers for QUIC are set. | ||
func ListenAndServe(addr, certFile, keyFile string, handler http.Handler) error { | ||
return ListenAndServeWithCustomAltSvcPort(addr, certFile, keyFile, handler, 0) | ||
} | ||
|
||
// ListenAndServeWithCustomAltSvcPort listens on the given network address for both, TLS and QUIC | ||
// connetions in parallel. It returns if one of the two returns an error. | ||
// http.DefaultServeMux is used when handler is nil. | ||
// The correct Alt-Svc headers for QUIC are set. | ||
// customAltSvcPort is used to override the default port value in the Alt-Svc response header. | ||
// This is useful when a Layer 4 firewall is redirecting UDP traffic and clients must use | ||
// a port different from the port the QUIC server itself is listening on. | ||
func ListenAndServeWithCustomAltSvcPort(addr, certFile, keyFile string, handler http.Handler, customAltSvcPort uint32) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we need an extra function here. Users who need to change the port should just initialize an |
||
// Load certs | ||
var err error | ||
certs := make([]tls.Certificate, 1) | ||
|
@@ -530,7 +548,8 @@ func ListenAndServe(addr, certFile, keyFile string, handler http.Handler) error | |
} | ||
|
||
quicServer := &Server{ | ||
Server: httpServer, | ||
Server: httpServer, | ||
customAltSvcPort: customAltSvcPort, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another possible way to do this would be to just set |
||
} | ||
|
||
if handler == nil { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is important enough to warrant complicating the example.