rwebtransport 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (122) hide show
  1. package/Cargo.lock +666 -0
  2. package/Cargo.toml +31 -0
  3. package/LICENSE +202 -0
  4. package/NOTICE +41 -0
  5. package/README.md +550 -0
  6. package/crates/echo-server/Cargo.toml +20 -0
  7. package/crates/echo-server/src/main.rs +666 -0
  8. package/crates/native/Cargo.toml +23 -0
  9. package/crates/native/src/config.rs +129 -0
  10. package/crates/native/src/driver.rs +463 -0
  11. package/crates/native/src/lib.rs +821 -0
  12. package/crates/native/src/server.rs +718 -0
  13. package/crates/native/src/session.rs +1191 -0
  14. package/crates/native/src/tls.rs +96 -0
  15. package/crates/wtcore/Cargo.toml +15 -0
  16. package/crates/wtcore/src/h3.rs +226 -0
  17. package/crates/wtcore/src/lib.rs +9 -0
  18. package/dist/datagrams.d.ts +16 -0
  19. package/dist/errors.d.ts +10 -0
  20. package/dist/index.d.ts +7 -0
  21. package/dist/index.js +1512 -0
  22. package/dist/index.js.map +1 -0
  23. package/dist/index.mjs +1473 -0
  24. package/dist/index.mjs.map +1 -0
  25. package/dist/loader.d.ts +2 -0
  26. package/dist/native.d.ts +226 -0
  27. package/dist/server.d.ts +44 -0
  28. package/dist/streams.d.ts +14 -0
  29. package/dist/types.d.ts +24 -0
  30. package/dist/webtransport.d.ts +20 -0
  31. package/package.json +79 -0
  32. package/prebuilds/win32-x64/rwebtransport.node +0 -0
  33. package/scripts/bench.mjs +180 -0
  34. package/scripts/build.js +67 -0
  35. package/scripts/postinstall.js +67 -0
  36. package/scripts/setup-unix.sh +129 -0
  37. package/scripts/setup-windows.ps1 +229 -0
  38. package/vendor/quiche/AGENTS.md +75 -0
  39. package/vendor/quiche/COPYING +23 -0
  40. package/vendor/quiche/Cargo.lock +1212 -0
  41. package/vendor/quiche/Cargo.toml +215 -0
  42. package/vendor/quiche/README.md +466 -0
  43. package/vendor/quiche/examples/Makefile +46 -0
  44. package/vendor/quiche/examples/README.md +20 -0
  45. package/vendor/quiche/examples/cert-big.crt +90 -0
  46. package/vendor/quiche/examples/cert.crt +18 -0
  47. package/vendor/quiche/examples/cert.key +28 -0
  48. package/vendor/quiche/examples/client.c +343 -0
  49. package/vendor/quiche/examples/client.rs +281 -0
  50. package/vendor/quiche/examples/gen-certs.sh +15 -0
  51. package/vendor/quiche/examples/http3-client.c +473 -0
  52. package/vendor/quiche/examples/http3-client.rs +349 -0
  53. package/vendor/quiche/examples/http3-server.c +614 -0
  54. package/vendor/quiche/examples/http3-server.rs +680 -0
  55. package/vendor/quiche/examples/qpack-decode.rs +87 -0
  56. package/vendor/quiche/examples/qpack-encode.rs +88 -0
  57. package/vendor/quiche/examples/rootca.crt +21 -0
  58. package/vendor/quiche/examples/server.c +529 -0
  59. package/vendor/quiche/examples/server.rs +529 -0
  60. package/vendor/quiche/include/quiche.h +1282 -0
  61. package/vendor/quiche/quiche.svg +1 -0
  62. package/vendor/quiche/src/buffers.rs +105 -0
  63. package/vendor/quiche/src/build.rs +66 -0
  64. package/vendor/quiche/src/cid.rs +1079 -0
  65. package/vendor/quiche/src/crypto/boringssl.rs +373 -0
  66. package/vendor/quiche/src/crypto/mod.rs +671 -0
  67. package/vendor/quiche/src/dgram.rs +118 -0
  68. package/vendor/quiche/src/error.rs +263 -0
  69. package/vendor/quiche/src/ffi.rs +2290 -0
  70. package/vendor/quiche/src/flowcontrol.rs +246 -0
  71. package/vendor/quiche/src/frame.rs +2172 -0
  72. package/vendor/quiche/src/h3/AGENTS.md +54 -0
  73. package/vendor/quiche/src/h3/ffi.rs +442 -0
  74. package/vendor/quiche/src/h3/frame.rs +1404 -0
  75. package/vendor/quiche/src/h3/mod.rs +8223 -0
  76. package/vendor/quiche/src/h3/qpack/decoder.rs +288 -0
  77. package/vendor/quiche/src/h3/qpack/encoder.rs +235 -0
  78. package/vendor/quiche/src/h3/qpack/mod.rs +202 -0
  79. package/vendor/quiche/src/h3/qpack/static_table.rs +323 -0
  80. package/vendor/quiche/src/h3/stream.rs +1595 -0
  81. package/vendor/quiche/src/lib.rs +9556 -0
  82. package/vendor/quiche/src/minmax.rs +446 -0
  83. package/vendor/quiche/src/packet.rs +2334 -0
  84. package/vendor/quiche/src/path.rs +1374 -0
  85. package/vendor/quiche/src/pmtud.rs +652 -0
  86. package/vendor/quiche/src/rand.rs +64 -0
  87. package/vendor/quiche/src/range_buf.rs +186 -0
  88. package/vendor/quiche/src/ranges.rs +755 -0
  89. package/vendor/quiche/src/recovery/AGENTS.md +80 -0
  90. package/vendor/quiche/src/recovery/bandwidth.rs +565 -0
  91. package/vendor/quiche/src/recovery/bytes_in_flight.rs +179 -0
  92. package/vendor/quiche/src/recovery/congestion/cubic.rs +1038 -0
  93. package/vendor/quiche/src/recovery/congestion/delivery_rate.rs +514 -0
  94. package/vendor/quiche/src/recovery/congestion/hystart.rs +241 -0
  95. package/vendor/quiche/src/recovery/congestion/mod.rs +361 -0
  96. package/vendor/quiche/src/recovery/congestion/prr.rs +238 -0
  97. package/vendor/quiche/src/recovery/congestion/recovery.rs +1170 -0
  98. package/vendor/quiche/src/recovery/congestion/reno.rs +291 -0
  99. package/vendor/quiche/src/recovery/congestion/test_sender.rs +201 -0
  100. package/vendor/quiche/src/recovery/gcongestion/bbr/bandwidth_sampler.rs +1947 -0
  101. package/vendor/quiche/src/recovery/gcongestion/bbr/windowed_filter.rs +158 -0
  102. package/vendor/quiche/src/recovery/gcongestion/bbr.rs +37 -0
  103. package/vendor/quiche/src/recovery/gcongestion/bbr2/drain.rs +120 -0
  104. package/vendor/quiche/src/recovery/gcongestion/bbr2/mode.rs +318 -0
  105. package/vendor/quiche/src/recovery/gcongestion/bbr2/network_model.rs +784 -0
  106. package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_bw.rs +729 -0
  107. package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_rtt.rs +175 -0
  108. package/vendor/quiche/src/recovery/gcongestion/bbr2/startup.rs +190 -0
  109. package/vendor/quiche/src/recovery/gcongestion/bbr2.rs +855 -0
  110. package/vendor/quiche/src/recovery/gcongestion/mod.rs +275 -0
  111. package/vendor/quiche/src/recovery/gcongestion/pacer.rs +294 -0
  112. package/vendor/quiche/src/recovery/gcongestion/recovery.rs +1384 -0
  113. package/vendor/quiche/src/recovery/mod.rs +3086 -0
  114. package/vendor/quiche/src/recovery/rtt.rs +152 -0
  115. package/vendor/quiche/src/stream/mod.rs +2210 -0
  116. package/vendor/quiche/src/stream/recv_buf.rs +1236 -0
  117. package/vendor/quiche/src/stream/send_buf.rs +849 -0
  118. package/vendor/quiche/src/test_utils.rs +681 -0
  119. package/vendor/quiche/src/tests.rs +12922 -0
  120. package/vendor/quiche/src/tls/boringssl.rs +375 -0
  121. package/vendor/quiche/src/tls/mod.rs +1245 -0
  122. package/vendor/quiche/src/transport_params.rs +628 -0
@@ -0,0 +1,349 @@
1
+ // Copyright (C) 2019, Cloudflare, Inc.
2
+ // All rights reserved.
3
+ //
4
+ // Redistribution and use in source and binary forms, with or without
5
+ // modification, are permitted provided that the following conditions are
6
+ // met:
7
+ //
8
+ // * Redistributions of source code must retain the above copyright notice,
9
+ // this list of conditions and the following disclaimer.
10
+ //
11
+ // * Redistributions in binary form must reproduce the above copyright
12
+ // notice, this list of conditions and the following disclaimer in the
13
+ // documentation and/or other materials provided with the distribution.
14
+ //
15
+ // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16
+ // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
17
+ // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18
+ // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
19
+ // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20
+ // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21
+ // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22
+ // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23
+ // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24
+ // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
+ // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
+
27
+ #[macro_use]
28
+ extern crate log;
29
+
30
+ use quiche::h3::NameValue;
31
+
32
+ use ring::rand::*;
33
+
34
+ const MAX_DATAGRAM_SIZE: usize = 1350;
35
+
36
+ fn main() {
37
+ let mut buf = [0; 65535];
38
+ let mut out = [0; MAX_DATAGRAM_SIZE];
39
+
40
+ let mut args = std::env::args();
41
+
42
+ let cmd = &args.next().unwrap();
43
+
44
+ if args.len() != 1 {
45
+ println!("Usage: {cmd} URL");
46
+ println!("\nSee tools/apps/ for more complete implementations.");
47
+ return;
48
+ }
49
+
50
+ let url = url::Url::parse(&args.next().unwrap()).unwrap();
51
+
52
+ // Setup the event loop.
53
+ let mut poll = mio::Poll::new().unwrap();
54
+ let mut events = mio::Events::with_capacity(1024);
55
+
56
+ // Resolve server address.
57
+ let peer_addr = url.socket_addrs(|| None).unwrap()[0];
58
+
59
+ // Bind to INADDR_ANY or IN6ADDR_ANY depending on the IP family of the
60
+ // server address. This is needed on macOS and BSD variants that don't
61
+ // support binding to IN6ADDR_ANY for both v4 and v6.
62
+ let bind_addr = match peer_addr {
63
+ std::net::SocketAddr::V4(_) => "0.0.0.0:0",
64
+ std::net::SocketAddr::V6(_) => "[::]:0",
65
+ };
66
+
67
+ // Create the UDP socket backing the QUIC connection, and register it with
68
+ // the event loop.
69
+ let mut socket =
70
+ mio::net::UdpSocket::bind(bind_addr.parse().unwrap()).unwrap();
71
+ poll.registry()
72
+ .register(&mut socket, mio::Token(0), mio::Interest::READABLE)
73
+ .unwrap();
74
+
75
+ // Create the configuration for the QUIC connection.
76
+ let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION).unwrap();
77
+
78
+ // *CAUTION*: this should not be set to `false` in production!!!
79
+ config.verify_peer(false);
80
+
81
+ config
82
+ .set_application_protos(quiche::h3::APPLICATION_PROTOCOL)
83
+ .unwrap();
84
+
85
+ config.set_max_idle_timeout(5000);
86
+ config.set_max_recv_udp_payload_size(MAX_DATAGRAM_SIZE);
87
+ config.set_max_send_udp_payload_size(MAX_DATAGRAM_SIZE);
88
+ config.set_initial_max_data(10_000_000);
89
+ config.set_initial_max_stream_data_bidi_local(1_000_000);
90
+ config.set_initial_max_stream_data_bidi_remote(1_000_000);
91
+ config.set_initial_max_stream_data_uni(1_000_000);
92
+ config.set_initial_max_streams_bidi(100);
93
+ config.set_initial_max_streams_uni(100);
94
+ config.set_disable_active_migration(true);
95
+
96
+ let mut http3_conn = None;
97
+
98
+ // Generate a random source connection ID for the connection.
99
+ let mut scid = [0; quiche::MAX_CONN_ID_LEN];
100
+ SystemRandom::new().fill(&mut scid[..]).unwrap();
101
+
102
+ let scid = quiche::ConnectionId::from_ref(&scid);
103
+
104
+ // Get local address.
105
+ let local_addr = socket.local_addr().unwrap();
106
+
107
+ // Create a QUIC connection and initiate handshake.
108
+ let mut conn =
109
+ quiche::connect(url.domain(), &scid, local_addr, peer_addr, &mut config)
110
+ .unwrap();
111
+
112
+ info!(
113
+ "connecting to {:} from {:} with scid {}",
114
+ peer_addr,
115
+ socket.local_addr().unwrap(),
116
+ hex_dump(&scid)
117
+ );
118
+
119
+ let (write, send_info) = conn.send(&mut out).expect("initial send failed");
120
+
121
+ while let Err(e) = socket.send_to(&out[..write], send_info.to) {
122
+ if e.kind() == std::io::ErrorKind::WouldBlock {
123
+ debug!("send() would block");
124
+ continue;
125
+ }
126
+
127
+ panic!("send() failed: {e:?}");
128
+ }
129
+
130
+ debug!("written {write}");
131
+
132
+ let h3_config = quiche::h3::Config::new().unwrap();
133
+
134
+ // Prepare request.
135
+ let mut path = String::from(url.path());
136
+
137
+ if let Some(query) = url.query() {
138
+ path.push('?');
139
+ path.push_str(query);
140
+ }
141
+
142
+ let req = vec![
143
+ quiche::h3::Header::new(b":method", b"GET"),
144
+ quiche::h3::Header::new(b":scheme", url.scheme().as_bytes()),
145
+ quiche::h3::Header::new(
146
+ b":authority",
147
+ url.host_str().unwrap().as_bytes(),
148
+ ),
149
+ quiche::h3::Header::new(b":path", path.as_bytes()),
150
+ quiche::h3::Header::new(b"user-agent", b"quiche"),
151
+ ];
152
+
153
+ let req_start = std::time::Instant::now();
154
+
155
+ let mut req_sent = false;
156
+
157
+ loop {
158
+ poll.poll(&mut events, conn.timeout()).unwrap();
159
+
160
+ // Read incoming UDP packets from the socket and feed them to quiche,
161
+ // until there are no more packets to read.
162
+ 'read: loop {
163
+ // If the event loop reported no events, it means that the timeout
164
+ // has expired, so handle it without attempting to read packets. We
165
+ // will then proceed with the send loop.
166
+ if events.is_empty() {
167
+ debug!("timed out");
168
+
169
+ conn.on_timeout();
170
+
171
+ break 'read;
172
+ }
173
+
174
+ let (len, from) = match socket.recv_from(&mut buf) {
175
+ Ok(v) => v,
176
+
177
+ Err(e) => {
178
+ // There are no more UDP packets to read, so end the read
179
+ // loop.
180
+ if e.kind() == std::io::ErrorKind::WouldBlock {
181
+ debug!("recv() would block");
182
+ break 'read;
183
+ }
184
+
185
+ panic!("recv() failed: {e:?}");
186
+ },
187
+ };
188
+
189
+ debug!("got {len} bytes");
190
+
191
+ let recv_info = quiche::RecvInfo {
192
+ to: local_addr,
193
+ from,
194
+ };
195
+
196
+ // Process potentially coalesced packets.
197
+ let read = match conn.recv(&mut buf[..len], recv_info) {
198
+ Ok(v) => v,
199
+
200
+ Err(e) => {
201
+ error!("recv failed: {e:?}");
202
+ continue 'read;
203
+ },
204
+ };
205
+
206
+ debug!("processed {read} bytes");
207
+ }
208
+
209
+ debug!("done reading");
210
+
211
+ if conn.is_closed() {
212
+ info!("connection closed, {:?}", conn.stats());
213
+ break;
214
+ }
215
+
216
+ // Create a new HTTP/3 connection once the QUIC connection is established.
217
+ if conn.is_established() && http3_conn.is_none() {
218
+ http3_conn = Some(
219
+ quiche::h3::Connection::with_transport(&mut conn, &h3_config)
220
+ .expect("Unable to create HTTP/3 connection, check the server's uni stream limit and window size"),
221
+ );
222
+ }
223
+
224
+ // Send HTTP requests once the QUIC connection is established, and until
225
+ // all requests have been sent.
226
+ if let Some(h3_conn) = &mut http3_conn {
227
+ if !req_sent {
228
+ info!("sending HTTP request {req:?}");
229
+
230
+ h3_conn.send_request(&mut conn, &req, true).unwrap();
231
+
232
+ req_sent = true;
233
+ }
234
+ }
235
+
236
+ if let Some(http3_conn) = &mut http3_conn {
237
+ // Process HTTP/3 events.
238
+ loop {
239
+ match http3_conn.poll(&mut conn) {
240
+ Ok((stream_id, quiche::h3::Event::Headers { list, .. })) => {
241
+ info!(
242
+ "got response headers {:?} on stream id {}",
243
+ hdrs_to_strings(&list),
244
+ stream_id
245
+ );
246
+ },
247
+
248
+ Ok((stream_id, quiche::h3::Event::Data)) => {
249
+ while let Ok(read) =
250
+ http3_conn.recv_body(&mut conn, stream_id, &mut buf)
251
+ {
252
+ debug!(
253
+ "got {read} bytes of response data on stream {stream_id}"
254
+ );
255
+
256
+ print!("{}", unsafe {
257
+ std::str::from_utf8_unchecked(&buf[..read])
258
+ });
259
+ }
260
+ },
261
+
262
+ Ok((_stream_id, quiche::h3::Event::Finished)) => {
263
+ info!(
264
+ "response received in {:?}, closing...",
265
+ req_start.elapsed()
266
+ );
267
+
268
+ conn.close(true, 0x100, b"kthxbye").unwrap();
269
+ },
270
+
271
+ Ok((_stream_id, quiche::h3::Event::Reset(e))) => {
272
+ error!("request was reset by peer with {e}, closing...");
273
+
274
+ conn.close(true, 0x100, b"kthxbye").unwrap();
275
+ },
276
+
277
+ Ok((_, quiche::h3::Event::PriorityUpdate)) => unreachable!(),
278
+
279
+ Ok((goaway_id, quiche::h3::Event::GoAway)) => {
280
+ info!("GOAWAY id={goaway_id}");
281
+ },
282
+
283
+ Err(quiche::h3::Error::Done) => {
284
+ break;
285
+ },
286
+
287
+ Err(e) => {
288
+ error!("HTTP/3 processing failed: {e:?}");
289
+
290
+ break;
291
+ },
292
+ }
293
+ }
294
+ }
295
+
296
+ // Generate outgoing QUIC packets and send them on the UDP socket, until
297
+ // quiche reports that there are no more packets to be sent.
298
+ loop {
299
+ let (write, send_info) = match conn.send(&mut out) {
300
+ Ok(v) => v,
301
+
302
+ Err(quiche::Error::Done) => {
303
+ debug!("done writing");
304
+ break;
305
+ },
306
+
307
+ Err(e) => {
308
+ error!("send failed: {e:?}");
309
+
310
+ conn.close(false, 0x1, b"fail").ok();
311
+ break;
312
+ },
313
+ };
314
+
315
+ if let Err(e) = socket.send_to(&out[..write], send_info.to) {
316
+ if e.kind() == std::io::ErrorKind::WouldBlock {
317
+ debug!("send() would block");
318
+ break;
319
+ }
320
+
321
+ panic!("send() failed: {e:?}");
322
+ }
323
+
324
+ debug!("written {write}");
325
+ }
326
+
327
+ if conn.is_closed() {
328
+ info!("connection closed, {:?}", conn.stats());
329
+ break;
330
+ }
331
+ }
332
+ }
333
+
334
+ fn hex_dump(buf: &[u8]) -> String {
335
+ let vec: Vec<String> = buf.iter().map(|b| format!("{b:02x}")).collect();
336
+
337
+ vec.join("")
338
+ }
339
+
340
+ pub fn hdrs_to_strings(hdrs: &[quiche::h3::Header]) -> Vec<(String, String)> {
341
+ hdrs.iter()
342
+ .map(|h| {
343
+ let name = String::from_utf8_lossy(h.name()).to_string();
344
+ let value = String::from_utf8_lossy(h.value()).to_string();
345
+
346
+ (name, value)
347
+ })
348
+ .collect()
349
+ }