conn.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. package http3
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "log/slog"
  7. "net"
  8. "net/http"
  9. "net/http/httptrace"
  10. "sync"
  11. "sync/atomic"
  12. "time"
  13. "github.com/Psiphon-Labs/quic-go"
  14. "github.com/Psiphon-Labs/quic-go/internal/protocol"
  15. "github.com/Psiphon-Labs/quic-go/quicvarint"
  16. "github.com/quic-go/qpack"
  17. )
  18. // Connection is an HTTP/3 connection.
  19. // It has all methods from the quic.Connection expect for AcceptStream, AcceptUniStream,
  20. // SendDatagram and ReceiveDatagram.
  21. type Connection interface {
  22. OpenStream() (quic.Stream, error)
  23. OpenStreamSync(context.Context) (quic.Stream, error)
  24. OpenUniStream() (quic.SendStream, error)
  25. OpenUniStreamSync(context.Context) (quic.SendStream, error)
  26. LocalAddr() net.Addr
  27. RemoteAddr() net.Addr
  28. CloseWithError(quic.ApplicationErrorCode, string) error
  29. Context() context.Context
  30. ConnectionState() quic.ConnectionState
  31. // ReceivedSettings returns a channel that is closed once the client's SETTINGS frame was received.
  32. ReceivedSettings() <-chan struct{}
  33. // Settings returns the settings received on this connection.
  34. Settings() *Settings
  35. }
  36. type connection struct {
  37. quic.Connection
  38. ctx context.Context
  39. perspective protocol.Perspective
  40. logger *slog.Logger
  41. enableDatagrams bool
  42. decoder *qpack.Decoder
  43. streamMx sync.Mutex
  44. streams map[protocol.StreamID]*datagrammer
  45. settings *Settings
  46. receivedSettings chan struct{}
  47. idleTimeout time.Duration
  48. idleTimer *time.Timer
  49. }
  50. func newConnection(
  51. ctx context.Context,
  52. quicConn quic.Connection,
  53. enableDatagrams bool,
  54. perspective protocol.Perspective,
  55. logger *slog.Logger,
  56. idleTimeout time.Duration,
  57. ) *connection {
  58. c := &connection{
  59. ctx: ctx,
  60. Connection: quicConn,
  61. perspective: perspective,
  62. logger: logger,
  63. idleTimeout: idleTimeout,
  64. enableDatagrams: enableDatagrams,
  65. decoder: qpack.NewDecoder(func(hf qpack.HeaderField) {}),
  66. receivedSettings: make(chan struct{}),
  67. streams: make(map[protocol.StreamID]*datagrammer),
  68. }
  69. if idleTimeout > 0 {
  70. c.idleTimer = time.AfterFunc(idleTimeout, c.onIdleTimer)
  71. }
  72. return c
  73. }
  74. func (c *connection) onIdleTimer() {
  75. c.CloseWithError(quic.ApplicationErrorCode(ErrCodeNoError), "idle timeout")
  76. }
  77. func (c *connection) clearStream(id quic.StreamID) {
  78. c.streamMx.Lock()
  79. defer c.streamMx.Unlock()
  80. delete(c.streams, id)
  81. if c.idleTimeout > 0 && len(c.streams) == 0 {
  82. c.idleTimer.Reset(c.idleTimeout)
  83. }
  84. }
  85. func (c *connection) openRequestStream(
  86. ctx context.Context,
  87. requestWriter *requestWriter,
  88. reqDone chan<- struct{},
  89. disableCompression bool,
  90. maxHeaderBytes uint64,
  91. ) (*requestStream, error) {
  92. str, err := c.Connection.OpenStreamSync(ctx)
  93. if err != nil {
  94. return nil, err
  95. }
  96. datagrams := newDatagrammer(func(b []byte) error { return c.sendDatagram(str.StreamID(), b) })
  97. c.streamMx.Lock()
  98. c.streams[str.StreamID()] = datagrams
  99. c.streamMx.Unlock()
  100. qstr := newStateTrackingStream(str, c, datagrams)
  101. rsp := &http.Response{}
  102. hstr := newStream(qstr, c, datagrams, func(r io.Reader, l uint64) error {
  103. hdr, err := c.decodeTrailers(r, l, maxHeaderBytes)
  104. if err != nil {
  105. return err
  106. }
  107. rsp.Trailer = hdr
  108. return nil
  109. })
  110. trace := httptrace.ContextClientTrace(ctx)
  111. return newRequestStream(hstr, requestWriter, reqDone, c.decoder, disableCompression, maxHeaderBytes, rsp, trace), nil
  112. }
  113. func (c *connection) decodeTrailers(r io.Reader, l, maxHeaderBytes uint64) (http.Header, error) {
  114. if l > maxHeaderBytes {
  115. return nil, fmt.Errorf("HEADERS frame too large: %d bytes (max: %d)", l, maxHeaderBytes)
  116. }
  117. b := make([]byte, l)
  118. if _, err := io.ReadFull(r, b); err != nil {
  119. return nil, err
  120. }
  121. fields, err := c.decoder.DecodeFull(b)
  122. if err != nil {
  123. return nil, err
  124. }
  125. return parseTrailers(fields)
  126. }
  127. func (c *connection) acceptStream(ctx context.Context) (quic.Stream, *datagrammer, error) {
  128. str, err := c.AcceptStream(ctx)
  129. if err != nil {
  130. return nil, nil, err
  131. }
  132. datagrams := newDatagrammer(func(b []byte) error { return c.sendDatagram(str.StreamID(), b) })
  133. if c.perspective == protocol.PerspectiveServer {
  134. strID := str.StreamID()
  135. c.streamMx.Lock()
  136. c.streams[strID] = datagrams
  137. if c.idleTimeout > 0 {
  138. if len(c.streams) == 1 {
  139. c.idleTimer.Stop()
  140. }
  141. }
  142. c.streamMx.Unlock()
  143. str = newStateTrackingStream(str, c, datagrams)
  144. }
  145. return str, datagrams, nil
  146. }
  147. func (c *connection) CloseWithError(code quic.ApplicationErrorCode, msg string) error {
  148. if c.idleTimer != nil {
  149. c.idleTimer.Stop()
  150. }
  151. return c.Connection.CloseWithError(code, msg)
  152. }
  153. func (c *connection) handleUnidirectionalStreams(hijack func(StreamType, quic.ConnectionTracingID, quic.ReceiveStream, error) (hijacked bool)) {
  154. var (
  155. rcvdControlStr atomic.Bool
  156. rcvdQPACKEncoderStr atomic.Bool
  157. rcvdQPACKDecoderStr atomic.Bool
  158. )
  159. for {
  160. str, err := c.Connection.AcceptUniStream(context.Background())
  161. if err != nil {
  162. if c.logger != nil {
  163. c.logger.Debug("accepting unidirectional stream failed", "error", err)
  164. }
  165. return
  166. }
  167. go func(str quic.ReceiveStream) {
  168. streamType, err := quicvarint.Read(quicvarint.NewReader(str))
  169. if err != nil {
  170. id := c.Connection.Context().Value(quic.ConnectionTracingKey).(quic.ConnectionTracingID)
  171. if hijack != nil && hijack(StreamType(streamType), id, str, err) {
  172. return
  173. }
  174. if c.logger != nil {
  175. c.logger.Debug("reading stream type on stream failed", "stream ID", str.StreamID(), "error", err)
  176. }
  177. return
  178. }
  179. // We're only interested in the control stream here.
  180. switch streamType {
  181. case streamTypeControlStream:
  182. case streamTypeQPACKEncoderStream:
  183. if isFirst := rcvdQPACKEncoderStr.CompareAndSwap(false, true); !isFirst {
  184. c.Connection.CloseWithError(quic.ApplicationErrorCode(ErrCodeStreamCreationError), "duplicate QPACK encoder stream")
  185. }
  186. // Our QPACK implementation doesn't use the dynamic table yet.
  187. return
  188. case streamTypeQPACKDecoderStream:
  189. if isFirst := rcvdQPACKDecoderStr.CompareAndSwap(false, true); !isFirst {
  190. c.Connection.CloseWithError(quic.ApplicationErrorCode(ErrCodeStreamCreationError), "duplicate QPACK decoder stream")
  191. }
  192. // Our QPACK implementation doesn't use the dynamic table yet.
  193. return
  194. case streamTypePushStream:
  195. switch c.perspective {
  196. case protocol.PerspectiveClient:
  197. // we never increased the Push ID, so we don't expect any push streams
  198. c.Connection.CloseWithError(quic.ApplicationErrorCode(ErrCodeIDError), "")
  199. case protocol.PerspectiveServer:
  200. // only the server can push
  201. c.Connection.CloseWithError(quic.ApplicationErrorCode(ErrCodeStreamCreationError), "")
  202. }
  203. return
  204. default:
  205. if hijack != nil {
  206. if hijack(
  207. StreamType(streamType),
  208. c.Connection.Context().Value(quic.ConnectionTracingKey).(quic.ConnectionTracingID),
  209. str,
  210. nil,
  211. ) {
  212. return
  213. }
  214. }
  215. str.CancelRead(quic.StreamErrorCode(ErrCodeStreamCreationError))
  216. return
  217. }
  218. // Only a single control stream is allowed.
  219. if isFirstControlStr := rcvdControlStr.CompareAndSwap(false, true); !isFirstControlStr {
  220. c.Connection.CloseWithError(quic.ApplicationErrorCode(ErrCodeStreamCreationError), "duplicate control stream")
  221. return
  222. }
  223. fp := &frameParser{conn: c.Connection, r: str}
  224. f, err := fp.ParseNext()
  225. if err != nil {
  226. c.Connection.CloseWithError(quic.ApplicationErrorCode(ErrCodeFrameError), "")
  227. return
  228. }
  229. sf, ok := f.(*settingsFrame)
  230. if !ok {
  231. c.Connection.CloseWithError(quic.ApplicationErrorCode(ErrCodeMissingSettings), "")
  232. return
  233. }
  234. c.settings = &Settings{
  235. EnableDatagrams: sf.Datagram,
  236. EnableExtendedConnect: sf.ExtendedConnect,
  237. Other: sf.Other,
  238. }
  239. close(c.receivedSettings)
  240. if !sf.Datagram {
  241. return
  242. }
  243. // If datagram support was enabled on our side as well as on the server side,
  244. // we can expect it to have been negotiated both on the transport and on the HTTP/3 layer.
  245. // Note: ConnectionState() will block until the handshake is complete (relevant when using 0-RTT).
  246. if c.enableDatagrams && !c.Connection.ConnectionState().SupportsDatagrams {
  247. c.Connection.CloseWithError(quic.ApplicationErrorCode(ErrCodeSettingsError), "missing QUIC Datagram support")
  248. return
  249. }
  250. go func() {
  251. if err := c.receiveDatagrams(); err != nil {
  252. if c.logger != nil {
  253. c.logger.Debug("receiving datagrams failed", "error", err)
  254. }
  255. }
  256. }()
  257. }(str)
  258. }
  259. }
  260. func (c *connection) sendDatagram(streamID protocol.StreamID, b []byte) error {
  261. // TODO: this creates a lot of garbage and an additional copy
  262. data := make([]byte, 0, len(b)+8)
  263. data = quicvarint.Append(data, uint64(streamID/4))
  264. data = append(data, b...)
  265. return c.Connection.SendDatagram(data)
  266. }
  267. func (c *connection) receiveDatagrams() error {
  268. for {
  269. b, err := c.Connection.ReceiveDatagram(context.Background())
  270. if err != nil {
  271. return err
  272. }
  273. quarterStreamID, n, err := quicvarint.Parse(b)
  274. if err != nil {
  275. c.Connection.CloseWithError(quic.ApplicationErrorCode(ErrCodeDatagramError), "")
  276. return fmt.Errorf("could not read quarter stream id: %w", err)
  277. }
  278. if quarterStreamID > maxQuarterStreamID {
  279. c.Connection.CloseWithError(quic.ApplicationErrorCode(ErrCodeDatagramError), "")
  280. return fmt.Errorf("invalid quarter stream id: %w", err)
  281. }
  282. streamID := protocol.StreamID(4 * quarterStreamID)
  283. c.streamMx.Lock()
  284. dg, ok := c.streams[streamID]
  285. if !ok {
  286. c.streamMx.Unlock()
  287. return nil
  288. }
  289. c.streamMx.Unlock()
  290. dg.enqueue(b[n:])
  291. }
  292. }
  293. // ReceivedSettings returns a channel that is closed once the peer's SETTINGS frame was received.
  294. // Settings can be optained from the Settings method after the channel was closed.
  295. func (c *connection) ReceivedSettings() <-chan struct{} { return c.receivedSettings }
  296. // Settings returns the settings received on this connection.
  297. // It is only valid to call this function after the channel returned by ReceivedSettings was closed.
  298. func (c *connection) Settings() *Settings { return c.settings }
  299. // Context returns the context of the underlying QUIC connection.
  300. func (c *connection) Context() context.Context { return c.ctx }