server.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. package http3
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "net"
  8. "net/http"
  9. "runtime"
  10. "strconv"
  11. "strings"
  12. "sync"
  13. "time"
  14. tls "github.com/Psiphon-Labs/psiphon-tls"
  15. "github.com/Psiphon-Labs/quic-go"
  16. "github.com/Psiphon-Labs/quic-go/internal/protocol"
  17. "github.com/Psiphon-Labs/quic-go/internal/utils"
  18. "github.com/Psiphon-Labs/quic-go/quicvarint"
  19. "github.com/quic-go/qpack"
  20. )
  21. // allows mocking of quic.Listen and quic.ListenAddr
  22. var (
  23. quicListen = func(conn net.PacketConn, tlsConf *tls.Config, config *quic.Config) (QUICEarlyListener, error) {
  24. return quic.ListenEarly(conn, tlsConf, config)
  25. }
  26. quicListenAddr = func(addr string, tlsConf *tls.Config, config *quic.Config) (QUICEarlyListener, error) {
  27. return quic.ListenAddrEarly(addr, tlsConf, config)
  28. }
  29. errPanicked = errors.New("panicked")
  30. )
  31. // NextProtoH3 is the ALPN protocol negotiated during the TLS handshake, for QUIC v1 and v2.
  32. const NextProtoH3 = "h3"
  33. // StreamType is the stream type of a unidirectional stream.
  34. type StreamType uint64
  35. const (
  36. streamTypeControlStream = 0
  37. streamTypePushStream = 1
  38. streamTypeQPACKEncoderStream = 2
  39. streamTypeQPACKDecoderStream = 3
  40. )
  41. // A QUICEarlyListener listens for incoming QUIC connections.
  42. type QUICEarlyListener interface {
  43. Accept(context.Context) (quic.EarlyConnection, error)
  44. Addr() net.Addr
  45. io.Closer
  46. }
  47. var _ QUICEarlyListener = &quic.EarlyListener{}
  48. func versionToALPN(v protocol.VersionNumber) string {
  49. //nolint:exhaustive // These are all the versions we care about.
  50. switch v {
  51. case protocol.Version1, protocol.Version2:
  52. return NextProtoH3
  53. default:
  54. return ""
  55. }
  56. }
  57. // ConfigureTLSConfig creates a new tls.Config which can be used
  58. // to create a quic.Listener meant for serving http3. The created
  59. // tls.Config adds the functionality of detecting the used QUIC version
  60. // in order to set the correct ALPN value for the http3 connection.
  61. func ConfigureTLSConfig(tlsConf *tls.Config) *tls.Config {
  62. // The tls.Config used to setup the quic.Listener needs to have the GetConfigForClient callback set.
  63. // That way, we can get the QUIC version and set the correct ALPN value.
  64. return &tls.Config{
  65. GetConfigForClient: func(ch *tls.ClientHelloInfo) (*tls.Config, error) {
  66. // determine the ALPN from the QUIC version used
  67. proto := NextProtoH3
  68. val := ch.Context().Value(quic.QUICVersionContextKey)
  69. if v, ok := val.(quic.VersionNumber); ok {
  70. proto = versionToALPN(v)
  71. }
  72. config := tlsConf
  73. if tlsConf.GetConfigForClient != nil {
  74. getConfigForClient := tlsConf.GetConfigForClient
  75. var err error
  76. conf, err := getConfigForClient(ch)
  77. if err != nil {
  78. return nil, err
  79. }
  80. if conf != nil {
  81. config = conf
  82. }
  83. }
  84. if config == nil {
  85. return nil, nil
  86. }
  87. config = config.Clone()
  88. config.NextProtos = []string{proto}
  89. return config, nil
  90. },
  91. }
  92. }
  93. // contextKey is a value for use with context.WithValue. It's used as
  94. // a pointer so it fits in an interface{} without allocation.
  95. type contextKey struct {
  96. name string
  97. }
  98. func (k *contextKey) String() string { return "quic-go/http3 context value " + k.name }
  99. // ServerContextKey is a context key. It can be used in HTTP
  100. // handlers with Context.Value to access the server that
  101. // started the handler. The associated value will be of
  102. // type *http3.Server.
  103. var ServerContextKey = &contextKey{"http3-server"}
  104. // RemoteAddrContextKey is a context key. It can be used in
  105. // HTTP handlers with Context.Value to access the remote
  106. // address of the connection. The associated value will be of
  107. // type net.Addr.
  108. //
  109. // Use this value instead of [http.Request.RemoteAddr] if you
  110. // require access to the remote address of the connection rather
  111. // than its string representation.
  112. var RemoteAddrContextKey = &contextKey{"remote-addr"}
  113. type requestError struct {
  114. err error
  115. streamErr ErrCode
  116. connErr ErrCode
  117. }
  118. func newStreamError(code ErrCode, err error) requestError {
  119. return requestError{err: err, streamErr: code}
  120. }
  121. func newConnError(code ErrCode, err error) requestError {
  122. return requestError{err: err, connErr: code}
  123. }
  124. // listenerInfo contains info about specific listener added with addListener
  125. type listenerInfo struct {
  126. port int // 0 means that no info about port is available
  127. }
  128. // Server is a HTTP/3 server.
  129. type Server struct {
  130. // Addr optionally specifies the UDP address for the server to listen on,
  131. // in the form "host:port".
  132. //
  133. // When used by ListenAndServe and ListenAndServeTLS methods, if empty,
  134. // ":https" (port 443) is used. See net.Dial for details of the address
  135. // format.
  136. //
  137. // Otherwise, if Port is not set and underlying QUIC listeners do not
  138. // have valid port numbers, the port part is used in Alt-Svc headers set
  139. // with SetQuicHeaders.
  140. Addr string
  141. // Port is used in Alt-Svc response headers set with SetQuicHeaders. If
  142. // needed Port can be manually set when the Server is created.
  143. //
  144. // This is useful when a Layer 4 firewall is redirecting UDP traffic and
  145. // clients must use a port different from the port the Server is
  146. // listening on.
  147. Port int
  148. // TLSConfig provides a TLS configuration for use by server. It must be
  149. // set for ListenAndServe and Serve methods.
  150. TLSConfig *tls.Config
  151. // QuicConfig provides the parameters for QUIC connection created with
  152. // Serve. If nil, it uses reasonable default values.
  153. //
  154. // Configured versions are also used in Alt-Svc response header set with
  155. // SetQuicHeaders.
  156. QuicConfig *quic.Config
  157. // Handler is the HTTP request handler to use. If not set, defaults to
  158. // http.NotFound.
  159. Handler http.Handler
  160. // EnableDatagrams enables support for HTTP/3 datagrams.
  161. // If set to true, QuicConfig.EnableDatagram will be set.
  162. // See https://datatracker.ietf.org/doc/html/rfc9297.
  163. EnableDatagrams bool
  164. // MaxHeaderBytes controls the maximum number of bytes the server will
  165. // read parsing the request HEADERS frame. It does not limit the size of
  166. // the request body. If zero or negative, http.DefaultMaxHeaderBytes is
  167. // used.
  168. MaxHeaderBytes int
  169. // AdditionalSettings specifies additional HTTP/3 settings.
  170. // It is invalid to specify any settings defined by the HTTP/3 draft and the datagram draft.
  171. AdditionalSettings map[uint64]uint64
  172. // StreamHijacker, when set, is called for the first unknown frame parsed on a bidirectional stream.
  173. // It is called right after parsing the frame type.
  174. // If parsing the frame type fails, the error is passed to the callback.
  175. // In that case, the frame type will not be set.
  176. // Callers can either ignore the frame and return control of the stream back to HTTP/3
  177. // (by returning hijacked false).
  178. // Alternatively, callers can take over the QUIC stream (by returning hijacked true).
  179. StreamHijacker func(FrameType, quic.Connection, quic.Stream, error) (hijacked bool, err error)
  180. // UniStreamHijacker, when set, is called for unknown unidirectional stream of unknown stream type.
  181. // If parsing the stream type fails, the error is passed to the callback.
  182. // In that case, the stream type will not be set.
  183. UniStreamHijacker func(StreamType, quic.Connection, quic.ReceiveStream, error) (hijacked bool)
  184. // ConnContext optionally specifies a function that modifies
  185. // the context used for a new connection c. The provided ctx
  186. // has a ServerContextKey value.
  187. ConnContext func(ctx context.Context, c quic.Connection) context.Context
  188. mutex sync.RWMutex
  189. listeners map[*QUICEarlyListener]listenerInfo
  190. closed bool
  191. altSvcHeader string
  192. logger utils.Logger
  193. }
  194. // ListenAndServe listens on the UDP address s.Addr and calls s.Handler to handle HTTP/3 requests on incoming connections.
  195. //
  196. // If s.Addr is blank, ":https" is used.
  197. func (s *Server) ListenAndServe() error {
  198. return s.serveConn(s.TLSConfig, nil)
  199. }
  200. // ListenAndServeTLS listens on the UDP address s.Addr and calls s.Handler to handle HTTP/3 requests on incoming connections.
  201. //
  202. // If s.Addr is blank, ":https" is used.
  203. func (s *Server) ListenAndServeTLS(certFile, keyFile string) error {
  204. var err error
  205. certs := make([]tls.Certificate, 1)
  206. certs[0], err = tls.LoadX509KeyPair(certFile, keyFile)
  207. if err != nil {
  208. return err
  209. }
  210. // We currently only use the cert-related stuff from tls.Config,
  211. // so we don't need to make a full copy.
  212. config := &tls.Config{
  213. Certificates: certs,
  214. }
  215. return s.serveConn(config, nil)
  216. }
  217. // Serve an existing UDP connection.
  218. // It is possible to reuse the same connection for outgoing connections.
  219. // Closing the server does not close the connection.
  220. func (s *Server) Serve(conn net.PacketConn) error {
  221. return s.serveConn(s.TLSConfig, conn)
  222. }
  223. // ServeQUICConn serves a single QUIC connection.
  224. func (s *Server) ServeQUICConn(conn quic.Connection) error {
  225. s.mutex.Lock()
  226. if s.logger == nil {
  227. s.logger = utils.DefaultLogger.WithPrefix("server")
  228. }
  229. s.mutex.Unlock()
  230. return s.handleConn(conn)
  231. }
  232. // ServeListener serves an existing QUIC listener.
  233. // Make sure you use http3.ConfigureTLSConfig to configure a tls.Config
  234. // and use it to construct a http3-friendly QUIC listener.
  235. // Closing the server does close the listener.
  236. // ServeListener always returns a non-nil error. After Shutdown or Close, the returned error is http.ErrServerClosed.
  237. func (s *Server) ServeListener(ln QUICEarlyListener) error {
  238. if err := s.addListener(&ln); err != nil {
  239. return err
  240. }
  241. defer s.removeListener(&ln)
  242. for {
  243. conn, err := ln.Accept(context.Background())
  244. if err == quic.ErrServerClosed {
  245. return http.ErrServerClosed
  246. }
  247. if err != nil {
  248. return err
  249. }
  250. go func() {
  251. if err := s.handleConn(conn); err != nil {
  252. s.logger.Debugf("handling connection failed: %s", err)
  253. }
  254. }()
  255. }
  256. }
  257. var errServerWithoutTLSConfig = errors.New("use of http3.Server without TLSConfig")
  258. func (s *Server) serveConn(tlsConf *tls.Config, conn net.PacketConn) error {
  259. if tlsConf == nil {
  260. return errServerWithoutTLSConfig
  261. }
  262. s.mutex.Lock()
  263. closed := s.closed
  264. s.mutex.Unlock()
  265. if closed {
  266. return http.ErrServerClosed
  267. }
  268. baseConf := ConfigureTLSConfig(tlsConf)
  269. quicConf := s.QuicConfig
  270. if quicConf == nil {
  271. quicConf = &quic.Config{Allow0RTT: true}
  272. } else {
  273. quicConf = s.QuicConfig.Clone()
  274. }
  275. if s.EnableDatagrams {
  276. quicConf.EnableDatagrams = true
  277. }
  278. var ln QUICEarlyListener
  279. var err error
  280. if conn == nil {
  281. addr := s.Addr
  282. if addr == "" {
  283. addr = ":https"
  284. }
  285. ln, err = quicListenAddr(addr, baseConf, quicConf)
  286. } else {
  287. ln, err = quicListen(conn, baseConf, quicConf)
  288. }
  289. if err != nil {
  290. return err
  291. }
  292. return s.ServeListener(ln)
  293. }
  294. func extractPort(addr string) (int, error) {
  295. _, portStr, err := net.SplitHostPort(addr)
  296. if err != nil {
  297. return 0, err
  298. }
  299. portInt, err := net.LookupPort("tcp", portStr)
  300. if err != nil {
  301. return 0, err
  302. }
  303. return portInt, nil
  304. }
  305. func (s *Server) generateAltSvcHeader() {
  306. if len(s.listeners) == 0 {
  307. // Don't announce any ports since no one is listening for connections
  308. s.altSvcHeader = ""
  309. return
  310. }
  311. // This code assumes that we will use protocol.SupportedVersions if no quic.Config is passed.
  312. supportedVersions := protocol.SupportedVersions
  313. if s.QuicConfig != nil && len(s.QuicConfig.Versions) > 0 {
  314. supportedVersions = s.QuicConfig.Versions
  315. }
  316. // keep track of which have been seen so we don't yield duplicate values
  317. seen := make(map[string]struct{}, len(supportedVersions))
  318. var versionStrings []string
  319. for _, version := range supportedVersions {
  320. if v := versionToALPN(version); len(v) > 0 {
  321. if _, ok := seen[v]; !ok {
  322. versionStrings = append(versionStrings, v)
  323. seen[v] = struct{}{}
  324. }
  325. }
  326. }
  327. var altSvc []string
  328. addPort := func(port int) {
  329. for _, v := range versionStrings {
  330. altSvc = append(altSvc, fmt.Sprintf(`%s=":%d"; ma=2592000`, v, port))
  331. }
  332. }
  333. if s.Port != 0 {
  334. // if Port is specified, we must use it instead of the
  335. // listener addresses since there's a reason it's specified.
  336. addPort(s.Port)
  337. } else {
  338. // if we have some listeners assigned, try to find ports
  339. // which we can announce, otherwise nothing should be announced
  340. validPortsFound := false
  341. for _, info := range s.listeners {
  342. if info.port != 0 {
  343. addPort(info.port)
  344. validPortsFound = true
  345. }
  346. }
  347. if !validPortsFound {
  348. if port, err := extractPort(s.Addr); err == nil {
  349. addPort(port)
  350. }
  351. }
  352. }
  353. s.altSvcHeader = strings.Join(altSvc, ",")
  354. }
  355. // We store a pointer to interface in the map set. This is safe because we only
  356. // call trackListener via Serve and can track+defer untrack the same pointer to
  357. // local variable there. We never need to compare a Listener from another caller.
  358. func (s *Server) addListener(l *QUICEarlyListener) error {
  359. s.mutex.Lock()
  360. defer s.mutex.Unlock()
  361. if s.closed {
  362. return http.ErrServerClosed
  363. }
  364. if s.logger == nil {
  365. s.logger = utils.DefaultLogger.WithPrefix("server")
  366. }
  367. if s.listeners == nil {
  368. s.listeners = make(map[*QUICEarlyListener]listenerInfo)
  369. }
  370. laddr := (*l).Addr()
  371. if port, err := extractPort(laddr.String()); err == nil {
  372. s.listeners[l] = listenerInfo{port}
  373. } else {
  374. s.logger.Errorf("Unable to extract port from listener %s, will not be announced using SetQuicHeaders: %s", laddr, err)
  375. s.listeners[l] = listenerInfo{}
  376. }
  377. s.generateAltSvcHeader()
  378. return nil
  379. }
  380. func (s *Server) removeListener(l *QUICEarlyListener) {
  381. s.mutex.Lock()
  382. defer s.mutex.Unlock()
  383. delete(s.listeners, l)
  384. s.generateAltSvcHeader()
  385. }
  386. func (s *Server) handleConn(conn quic.Connection) error {
  387. decoder := qpack.NewDecoder(nil)
  388. // send a SETTINGS frame
  389. str, err := conn.OpenUniStream()
  390. if err != nil {
  391. return fmt.Errorf("opening the control stream failed: %w", err)
  392. }
  393. b := make([]byte, 0, 64)
  394. b = quicvarint.Append(b, streamTypeControlStream) // stream type
  395. b = (&settingsFrame{Datagram: s.EnableDatagrams, Other: s.AdditionalSettings}).Append(b)
  396. str.Write(b)
  397. go s.handleUnidirectionalStreams(conn)
  398. // Process all requests immediately.
  399. // It's the client's responsibility to decide which requests are eligible for 0-RTT.
  400. for {
  401. str, err := conn.AcceptStream(context.Background())
  402. if err != nil {
  403. var appErr *quic.ApplicationError
  404. if errors.As(err, &appErr) && appErr.ErrorCode == quic.ApplicationErrorCode(ErrCodeNoError) {
  405. return nil
  406. }
  407. return fmt.Errorf("accepting stream failed: %w", err)
  408. }
  409. go func() {
  410. rerr := s.handleRequest(conn, str, decoder, func() {
  411. conn.CloseWithError(quic.ApplicationErrorCode(ErrCodeFrameUnexpected), "")
  412. })
  413. if rerr.err == errHijacked {
  414. return
  415. }
  416. if rerr.err != nil || rerr.streamErr != 0 || rerr.connErr != 0 {
  417. s.logger.Debugf("Handling request failed: %s", err)
  418. if rerr.streamErr != 0 {
  419. str.CancelWrite(quic.StreamErrorCode(rerr.streamErr))
  420. }
  421. if rerr.connErr != 0 {
  422. var reason string
  423. if rerr.err != nil {
  424. reason = rerr.err.Error()
  425. }
  426. conn.CloseWithError(quic.ApplicationErrorCode(rerr.connErr), reason)
  427. }
  428. return
  429. }
  430. str.Close()
  431. }()
  432. }
  433. }
  434. func (s *Server) handleUnidirectionalStreams(conn quic.Connection) {
  435. for {
  436. str, err := conn.AcceptUniStream(context.Background())
  437. if err != nil {
  438. s.logger.Debugf("accepting unidirectional stream failed: %s", err)
  439. return
  440. }
  441. go func(str quic.ReceiveStream) {
  442. streamType, err := quicvarint.Read(quicvarint.NewReader(str))
  443. if err != nil {
  444. if s.UniStreamHijacker != nil && s.UniStreamHijacker(StreamType(streamType), conn, str, err) {
  445. return
  446. }
  447. s.logger.Debugf("reading stream type on stream %d failed: %s", str.StreamID(), err)
  448. return
  449. }
  450. // We're only interested in the control stream here.
  451. switch streamType {
  452. case streamTypeControlStream:
  453. case streamTypeQPACKEncoderStream, streamTypeQPACKDecoderStream:
  454. // Our QPACK implementation doesn't use the dynamic table yet.
  455. // TODO: check that only one stream of each type is opened.
  456. return
  457. case streamTypePushStream: // only the server can push
  458. conn.CloseWithError(quic.ApplicationErrorCode(ErrCodeStreamCreationError), "")
  459. return
  460. default:
  461. if s.UniStreamHijacker != nil && s.UniStreamHijacker(StreamType(streamType), conn, str, nil) {
  462. return
  463. }
  464. str.CancelRead(quic.StreamErrorCode(ErrCodeStreamCreationError))
  465. return
  466. }
  467. f, err := parseNextFrame(str, nil)
  468. if err != nil {
  469. conn.CloseWithError(quic.ApplicationErrorCode(ErrCodeFrameError), "")
  470. return
  471. }
  472. sf, ok := f.(*settingsFrame)
  473. if !ok {
  474. conn.CloseWithError(quic.ApplicationErrorCode(ErrCodeMissingSettings), "")
  475. return
  476. }
  477. if !sf.Datagram {
  478. return
  479. }
  480. // If datagram support was enabled on our side as well as on the client side,
  481. // we can expect it to have been negotiated both on the transport and on the HTTP/3 layer.
  482. // Note: ConnectionState() will block until the handshake is complete (relevant when using 0-RTT).
  483. if s.EnableDatagrams && !conn.ConnectionState().SupportsDatagrams {
  484. conn.CloseWithError(quic.ApplicationErrorCode(ErrCodeSettingsError), "missing QUIC Datagram support")
  485. }
  486. }(str)
  487. }
  488. }
  489. func (s *Server) maxHeaderBytes() uint64 {
  490. if s.MaxHeaderBytes <= 0 {
  491. return http.DefaultMaxHeaderBytes
  492. }
  493. return uint64(s.MaxHeaderBytes)
  494. }
  495. func (s *Server) handleRequest(conn quic.Connection, str quic.Stream, decoder *qpack.Decoder, onFrameError func()) requestError {
  496. var ufh unknownFrameHandlerFunc
  497. if s.StreamHijacker != nil {
  498. ufh = func(ft FrameType, e error) (processed bool, err error) { return s.StreamHijacker(ft, conn, str, e) }
  499. }
  500. frame, err := parseNextFrame(str, ufh)
  501. if err != nil {
  502. if err == errHijacked {
  503. return requestError{err: errHijacked}
  504. }
  505. return newStreamError(ErrCodeRequestIncomplete, err)
  506. }
  507. hf, ok := frame.(*headersFrame)
  508. if !ok {
  509. return newConnError(ErrCodeFrameUnexpected, errors.New("expected first frame to be a HEADERS frame"))
  510. }
  511. if hf.Length > s.maxHeaderBytes() {
  512. return newStreamError(ErrCodeFrameError, fmt.Errorf("HEADERS frame too large: %d bytes (max: %d)", hf.Length, s.maxHeaderBytes()))
  513. }
  514. headerBlock := make([]byte, hf.Length)
  515. if _, err := io.ReadFull(str, headerBlock); err != nil {
  516. return newStreamError(ErrCodeRequestIncomplete, err)
  517. }
  518. hfs, err := decoder.DecodeFull(headerBlock)
  519. if err != nil {
  520. // TODO: use the right error code
  521. return newConnError(ErrCodeGeneralProtocolError, err)
  522. }
  523. req, err := requestFromHeaders(hfs)
  524. if err != nil {
  525. return newStreamError(ErrCodeMessageError, err)
  526. }
  527. connState := conn.ConnectionState().TLS
  528. // [Psiphon]
  529. req.TLS = tls.UnsafeFromConnectionState(&connState)
  530. req.RemoteAddr = conn.RemoteAddr().String()
  531. // Check that the client doesn't send more data in DATA frames than indicated by the Content-Length header (if set).
  532. // See section 4.1.2 of RFC 9114.
  533. var httpStr Stream
  534. if _, ok := req.Header["Content-Length"]; ok && req.ContentLength >= 0 {
  535. httpStr = newLengthLimitedStream(newStream(str, onFrameError), req.ContentLength)
  536. } else {
  537. httpStr = newStream(str, onFrameError)
  538. }
  539. body := newRequestBody(httpStr)
  540. req.Body = body
  541. if s.logger.Debug() {
  542. s.logger.Infof("%s %s%s, on stream %d", req.Method, req.Host, req.RequestURI, str.StreamID())
  543. } else {
  544. s.logger.Infof("%s %s%s", req.Method, req.Host, req.RequestURI)
  545. }
  546. ctx := str.Context()
  547. ctx = context.WithValue(ctx, ServerContextKey, s)
  548. ctx = context.WithValue(ctx, http.LocalAddrContextKey, conn.LocalAddr())
  549. ctx = context.WithValue(ctx, RemoteAddrContextKey, conn.RemoteAddr())
  550. if s.ConnContext != nil {
  551. ctx = s.ConnContext(ctx, conn)
  552. if ctx == nil {
  553. panic("http3: ConnContext returned nil")
  554. }
  555. }
  556. req = req.WithContext(ctx)
  557. r := newResponseWriter(str, conn, s.logger)
  558. if req.Method == http.MethodHead {
  559. r.isHead = true
  560. }
  561. handler := s.Handler
  562. if handler == nil {
  563. handler = http.DefaultServeMux
  564. }
  565. var panicked bool
  566. func() {
  567. defer func() {
  568. if p := recover(); p != nil {
  569. panicked = true
  570. if p == http.ErrAbortHandler {
  571. return
  572. }
  573. // Copied from net/http/server.go
  574. const size = 64 << 10
  575. buf := make([]byte, size)
  576. buf = buf[:runtime.Stack(buf, false)]
  577. s.logger.Errorf("http: panic serving: %v\n%s", p, buf)
  578. }
  579. }()
  580. handler.ServeHTTP(r, req)
  581. }()
  582. if body.wasStreamHijacked() {
  583. return requestError{err: errHijacked}
  584. }
  585. // only write response when there is no panic
  586. if !panicked {
  587. // response not written to the client yet, set Content-Length
  588. if !r.written {
  589. if _, haveCL := r.header["Content-Length"]; !haveCL {
  590. r.header.Set("Content-Length", strconv.FormatInt(r.numWritten, 10))
  591. }
  592. }
  593. r.Flush()
  594. }
  595. // If the EOF was read by the handler, CancelRead() is a no-op.
  596. str.CancelRead(quic.StreamErrorCode(ErrCodeNoError))
  597. // abort the stream when there is a panic
  598. if panicked {
  599. return newStreamError(ErrCodeInternalError, errPanicked)
  600. }
  601. return requestError{}
  602. }
  603. // Close the server immediately, aborting requests and sending CONNECTION_CLOSE frames to connected clients.
  604. // Close in combination with ListenAndServe() (instead of Serve()) may race if it is called before a UDP socket is established.
  605. func (s *Server) Close() error {
  606. s.mutex.Lock()
  607. defer s.mutex.Unlock()
  608. s.closed = true
  609. var err error
  610. for ln := range s.listeners {
  611. if cerr := (*ln).Close(); cerr != nil && err == nil {
  612. err = cerr
  613. }
  614. }
  615. return err
  616. }
  617. // CloseGracefully shuts down the server gracefully. The server sends a GOAWAY frame first, then waits for either timeout to trigger, or for all running requests to complete.
  618. // CloseGracefully in combination with ListenAndServe() (instead of Serve()) may race if it is called before a UDP socket is established.
  619. func (s *Server) CloseGracefully(timeout time.Duration) error {
  620. // TODO: implement
  621. return nil
  622. }
  623. // ErrNoAltSvcPort is the error returned by SetQuicHeaders when no port was found
  624. // for Alt-Svc to announce. This can happen if listening on a PacketConn without a port
  625. // (UNIX socket, for example) and no port is specified in Server.Port or Server.Addr.
  626. var ErrNoAltSvcPort = errors.New("no port can be announced, specify it explicitly using Server.Port or Server.Addr")
  627. // SetQuicHeaders can be used to set the proper headers that announce that this server supports HTTP/3.
  628. // The values set by default advertise all of the ports the server is listening on, but can be
  629. // changed to a specific port by setting Server.Port before launching the serverr.
  630. // If no listener's Addr().String() returns an address with a valid port, Server.Addr will be used
  631. // to extract the port, if specified.
  632. // For example, a server launched using ListenAndServe on an address with port 443 would set:
  633. //
  634. // Alt-Svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000
  635. func (s *Server) SetQuicHeaders(hdr http.Header) error {
  636. s.mutex.RLock()
  637. defer s.mutex.RUnlock()
  638. if s.altSvcHeader == "" {
  639. return ErrNoAltSvcPort
  640. }
  641. // use the map directly to avoid constant canonicalization
  642. // since the key is already canonicalized
  643. hdr["Alt-Svc"] = append(hdr["Alt-Svc"], s.altSvcHeader)
  644. return nil
  645. }
  646. // ListenAndServeQUIC listens on the UDP network address addr and calls the
  647. // handler for HTTP/3 requests on incoming connections. http.DefaultServeMux is
  648. // used when handler is nil.
  649. func ListenAndServeQUIC(addr, certFile, keyFile string, handler http.Handler) error {
  650. server := &Server{
  651. Addr: addr,
  652. Handler: handler,
  653. }
  654. return server.ListenAndServeTLS(certFile, keyFile)
  655. }
  656. // ListenAndServe listens on the given network address for both TLS/TCP and QUIC
  657. // connections in parallel. It returns if one of the two returns an error.
  658. // http.DefaultServeMux is used when handler is nil.
  659. // The correct Alt-Svc headers for QUIC are set.
  660. func ListenAndServe(addr, certFile, keyFile string, handler http.Handler) error {
  661. // Load certs
  662. var err error
  663. certs := make([]tls.Certificate, 1)
  664. certs[0], err = tls.LoadX509KeyPair(certFile, keyFile)
  665. if err != nil {
  666. return err
  667. }
  668. // We currently only use the cert-related stuff from tls.Config,
  669. // so we don't need to make a full copy.
  670. config := &tls.Config{
  671. Certificates: certs,
  672. }
  673. if addr == "" {
  674. addr = ":https"
  675. }
  676. // Open the listeners
  677. udpAddr, err := net.ResolveUDPAddr("udp", addr)
  678. if err != nil {
  679. return err
  680. }
  681. udpConn, err := net.ListenUDP("udp", udpAddr)
  682. if err != nil {
  683. return err
  684. }
  685. defer udpConn.Close()
  686. if handler == nil {
  687. handler = http.DefaultServeMux
  688. }
  689. // Start the servers
  690. quicServer := &Server{
  691. TLSConfig: config,
  692. Handler: handler,
  693. }
  694. hErr := make(chan error, 1)
  695. qErr := make(chan error, 1)
  696. go func() {
  697. hErr <- http.ListenAndServeTLS(addr, certFile, keyFile, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  698. quicServer.SetQuicHeaders(w.Header())
  699. handler.ServeHTTP(w, r)
  700. }))
  701. }()
  702. go func() {
  703. qErr <- quicServer.Serve(udpConn)
  704. }()
  705. select {
  706. case err := <-hErr:
  707. quicServer.Close()
  708. return err
  709. case err := <-qErr:
  710. // Cannot close the HTTP server or wait for requests to complete properly :/
  711. return err
  712. }
  713. }