rwebtransport 0.1.0 → 0.1.2
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.
- package/Cargo.lock +666 -666
- package/Cargo.toml +31 -31
- package/LICENSE +202 -202
- package/NOTICE +41 -41
- package/README.md +550 -550
- package/crates/echo-server/Cargo.toml +20 -20
- package/crates/echo-server/src/main.rs +666 -666
- package/crates/native/Cargo.toml +23 -23
- package/crates/native/src/config.rs +129 -129
- package/crates/native/src/driver.rs +463 -463
- package/crates/native/src/lib.rs +821 -821
- package/crates/native/src/server.rs +718 -718
- package/crates/native/src/session.rs +1191 -1191
- package/crates/native/src/tls.rs +96 -96
- package/crates/wtcore/Cargo.toml +15 -15
- package/crates/wtcore/src/h3.rs +226 -226
- package/crates/wtcore/src/lib.rs +9 -9
- package/dist/index.js.map +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +80 -79
- package/prebuilds/darwin-arm64/rwebtransport.node +0 -0
- package/prebuilds/linux-arm64/rwebtransport.node +0 -0
- package/prebuilds/linux-x64/rwebtransport.node +0 -0
- package/prebuilds/win32-x64/rwebtransport.node +0 -0
- package/scripts/bench.mjs +180 -180
- package/scripts/build.js +67 -67
- package/scripts/postinstall.js +67 -67
- package/scripts/setup-unix.sh +129 -129
- package/scripts/setup-windows.ps1 +229 -229
- package/vendor/quiche/AGENTS.md +75 -75
- package/vendor/quiche/COPYING +23 -23
- package/vendor/quiche/Cargo.lock +1212 -1212
- package/vendor/quiche/Cargo.toml +215 -215
- package/vendor/quiche/README.md +466 -466
- package/vendor/quiche/examples/Makefile +46 -46
- package/vendor/quiche/examples/README.md +20 -20
- package/vendor/quiche/examples/cert-big.crt +90 -90
- package/vendor/quiche/examples/cert.crt +18 -18
- package/vendor/quiche/examples/cert.key +28 -28
- package/vendor/quiche/examples/client.c +343 -343
- package/vendor/quiche/examples/client.rs +281 -281
- package/vendor/quiche/examples/gen-certs.sh +15 -15
- package/vendor/quiche/examples/http3-client.c +473 -473
- package/vendor/quiche/examples/http3-client.rs +349 -349
- package/vendor/quiche/examples/http3-server.c +614 -614
- package/vendor/quiche/examples/http3-server.rs +680 -680
- package/vendor/quiche/examples/qpack-decode.rs +87 -87
- package/vendor/quiche/examples/qpack-encode.rs +88 -88
- package/vendor/quiche/examples/rootca.crt +21 -21
- package/vendor/quiche/examples/server.c +529 -529
- package/vendor/quiche/examples/server.rs +529 -529
- package/vendor/quiche/include/quiche.h +1282 -1282
- package/vendor/quiche/src/buffers.rs +105 -105
- package/vendor/quiche/src/build.rs +66 -66
- package/vendor/quiche/src/cid.rs +1079 -1079
- package/vendor/quiche/src/crypto/boringssl.rs +373 -373
- package/vendor/quiche/src/crypto/mod.rs +671 -671
- package/vendor/quiche/src/dgram.rs +118 -118
- package/vendor/quiche/src/error.rs +263 -263
- package/vendor/quiche/src/ffi.rs +2290 -2290
- package/vendor/quiche/src/flowcontrol.rs +246 -246
- package/vendor/quiche/src/frame.rs +2172 -2172
- package/vendor/quiche/src/h3/AGENTS.md +54 -54
- package/vendor/quiche/src/h3/ffi.rs +442 -442
- package/vendor/quiche/src/h3/frame.rs +1404 -1404
- package/vendor/quiche/src/h3/mod.rs +8223 -8223
- package/vendor/quiche/src/h3/qpack/decoder.rs +288 -288
- package/vendor/quiche/src/h3/qpack/encoder.rs +235 -235
- package/vendor/quiche/src/h3/qpack/mod.rs +202 -202
- package/vendor/quiche/src/h3/qpack/static_table.rs +323 -323
- package/vendor/quiche/src/h3/stream.rs +1595 -1595
- package/vendor/quiche/src/lib.rs +9556 -9556
- package/vendor/quiche/src/minmax.rs +446 -446
- package/vendor/quiche/src/packet.rs +2334 -2334
- package/vendor/quiche/src/path.rs +1374 -1374
- package/vendor/quiche/src/pmtud.rs +652 -652
- package/vendor/quiche/src/rand.rs +64 -64
- package/vendor/quiche/src/range_buf.rs +186 -186
- package/vendor/quiche/src/ranges.rs +755 -755
- package/vendor/quiche/src/recovery/AGENTS.md +80 -80
- package/vendor/quiche/src/recovery/bandwidth.rs +565 -565
- package/vendor/quiche/src/recovery/bytes_in_flight.rs +179 -179
- package/vendor/quiche/src/recovery/congestion/cubic.rs +1038 -1038
- package/vendor/quiche/src/recovery/congestion/delivery_rate.rs +514 -514
- package/vendor/quiche/src/recovery/congestion/hystart.rs +241 -241
- package/vendor/quiche/src/recovery/congestion/mod.rs +361 -361
- package/vendor/quiche/src/recovery/congestion/prr.rs +238 -238
- package/vendor/quiche/src/recovery/congestion/recovery.rs +1170 -1170
- package/vendor/quiche/src/recovery/congestion/reno.rs +291 -291
- package/vendor/quiche/src/recovery/congestion/test_sender.rs +201 -201
- package/vendor/quiche/src/recovery/gcongestion/bbr/bandwidth_sampler.rs +1947 -1947
- package/vendor/quiche/src/recovery/gcongestion/bbr/windowed_filter.rs +158 -158
- package/vendor/quiche/src/recovery/gcongestion/bbr.rs +37 -37
- package/vendor/quiche/src/recovery/gcongestion/bbr2/drain.rs +120 -120
- package/vendor/quiche/src/recovery/gcongestion/bbr2/mode.rs +318 -318
- package/vendor/quiche/src/recovery/gcongestion/bbr2/network_model.rs +784 -784
- package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_bw.rs +729 -729
- package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_rtt.rs +175 -175
- package/vendor/quiche/src/recovery/gcongestion/bbr2/startup.rs +190 -190
- package/vendor/quiche/src/recovery/gcongestion/bbr2.rs +855 -855
- package/vendor/quiche/src/recovery/gcongestion/mod.rs +275 -275
- package/vendor/quiche/src/recovery/gcongestion/pacer.rs +294 -294
- package/vendor/quiche/src/recovery/gcongestion/recovery.rs +1384 -1384
- package/vendor/quiche/src/recovery/mod.rs +3086 -3086
- package/vendor/quiche/src/recovery/rtt.rs +152 -152
- package/vendor/quiche/src/stream/mod.rs +2210 -2210
- package/vendor/quiche/src/stream/recv_buf.rs +1236 -1236
- package/vendor/quiche/src/stream/send_buf.rs +849 -849
- package/vendor/quiche/src/test_utils.rs +681 -681
- package/vendor/quiche/src/tests.rs +12922 -12922
- package/vendor/quiche/src/tls/boringssl.rs +375 -375
- package/vendor/quiche/src/tls/mod.rs +1245 -1245
- package/vendor/quiche/src/transport_params.rs +628 -628
|
@@ -1,281 +1,281 @@
|
|
|
1
|
-
// Copyright (C) 2018-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 ring::rand::*;
|
|
31
|
-
|
|
32
|
-
const MAX_DATAGRAM_SIZE: usize = 1350;
|
|
33
|
-
|
|
34
|
-
const HTTP_REQ_STREAM_ID: u64 = 4;
|
|
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(&[
|
|
83
|
-
b"hq-interop",
|
|
84
|
-
b"hq-29",
|
|
85
|
-
b"hq-28",
|
|
86
|
-
b"hq-27",
|
|
87
|
-
b"http/0.9",
|
|
88
|
-
])
|
|
89
|
-
.unwrap();
|
|
90
|
-
|
|
91
|
-
config.set_max_idle_timeout(5000);
|
|
92
|
-
config.set_max_recv_udp_payload_size(MAX_DATAGRAM_SIZE);
|
|
93
|
-
config.set_max_send_udp_payload_size(MAX_DATAGRAM_SIZE);
|
|
94
|
-
config.set_initial_max_data(10_000_000);
|
|
95
|
-
config.set_initial_max_stream_data_bidi_local(1_000_000);
|
|
96
|
-
config.set_initial_max_stream_data_bidi_remote(1_000_000);
|
|
97
|
-
config.set_initial_max_streams_bidi(100);
|
|
98
|
-
config.set_initial_max_streams_uni(100);
|
|
99
|
-
config.set_disable_active_migration(true);
|
|
100
|
-
|
|
101
|
-
// Generate a random source connection ID for the connection.
|
|
102
|
-
let mut scid = [0; quiche::MAX_CONN_ID_LEN];
|
|
103
|
-
SystemRandom::new().fill(&mut scid[..]).unwrap();
|
|
104
|
-
|
|
105
|
-
let scid = quiche::ConnectionId::from_ref(&scid);
|
|
106
|
-
|
|
107
|
-
// Get local address.
|
|
108
|
-
let local_addr = socket.local_addr().unwrap();
|
|
109
|
-
|
|
110
|
-
// Create a QUIC connection and initiate handshake.
|
|
111
|
-
let mut conn =
|
|
112
|
-
quiche::connect(url.domain(), &scid, local_addr, peer_addr, &mut config)
|
|
113
|
-
.unwrap();
|
|
114
|
-
|
|
115
|
-
info!(
|
|
116
|
-
"connecting to {:} from {:} with scid {}",
|
|
117
|
-
peer_addr,
|
|
118
|
-
socket.local_addr().unwrap(),
|
|
119
|
-
hex_dump(&scid)
|
|
120
|
-
);
|
|
121
|
-
|
|
122
|
-
let (write, send_info) = conn.send(&mut out).expect("initial send failed");
|
|
123
|
-
|
|
124
|
-
while let Err(e) = socket.send_to(&out[..write], send_info.to) {
|
|
125
|
-
if e.kind() == std::io::ErrorKind::WouldBlock {
|
|
126
|
-
debug!("send() would block");
|
|
127
|
-
continue;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
panic!("send() failed: {e:?}");
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
debug!("written {write}");
|
|
134
|
-
|
|
135
|
-
let req_start = std::time::Instant::now();
|
|
136
|
-
|
|
137
|
-
let mut req_sent = false;
|
|
138
|
-
|
|
139
|
-
loop {
|
|
140
|
-
poll.poll(&mut events, conn.timeout()).unwrap();
|
|
141
|
-
|
|
142
|
-
// Read incoming UDP packets from the socket and feed them to quiche,
|
|
143
|
-
// until there are no more packets to read.
|
|
144
|
-
'read: loop {
|
|
145
|
-
// If the event loop reported no events, it means that the timeout
|
|
146
|
-
// has expired, so handle it without attempting to read packets. We
|
|
147
|
-
// will then proceed with the send loop.
|
|
148
|
-
if events.is_empty() {
|
|
149
|
-
debug!("timed out");
|
|
150
|
-
|
|
151
|
-
conn.on_timeout();
|
|
152
|
-
break 'read;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
let (len, from) = match socket.recv_from(&mut buf) {
|
|
156
|
-
Ok(v) => v,
|
|
157
|
-
|
|
158
|
-
Err(e) => {
|
|
159
|
-
// There are no more UDP packets to read, so end the read
|
|
160
|
-
// loop.
|
|
161
|
-
if e.kind() == std::io::ErrorKind::WouldBlock {
|
|
162
|
-
debug!("recv() would block");
|
|
163
|
-
break 'read;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
panic!("recv() failed: {e:?}");
|
|
167
|
-
},
|
|
168
|
-
};
|
|
169
|
-
|
|
170
|
-
debug!("got {len} bytes");
|
|
171
|
-
|
|
172
|
-
let recv_info = quiche::RecvInfo {
|
|
173
|
-
to: socket.local_addr().unwrap(),
|
|
174
|
-
from,
|
|
175
|
-
};
|
|
176
|
-
|
|
177
|
-
// Process potentially coalesced packets.
|
|
178
|
-
let read = match conn.recv(&mut buf[..len], recv_info) {
|
|
179
|
-
Ok(v) => v,
|
|
180
|
-
|
|
181
|
-
Err(e) => {
|
|
182
|
-
error!("recv failed: {e:?}");
|
|
183
|
-
continue 'read;
|
|
184
|
-
},
|
|
185
|
-
};
|
|
186
|
-
|
|
187
|
-
debug!("processed {read} bytes");
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
debug!("done reading");
|
|
191
|
-
|
|
192
|
-
if conn.is_closed() {
|
|
193
|
-
info!("connection closed, {:?}", conn.stats());
|
|
194
|
-
break;
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
// Send an HTTP request as soon as the connection is established.
|
|
198
|
-
if conn.is_established() && !req_sent {
|
|
199
|
-
info!("sending HTTP request for {}", url.path());
|
|
200
|
-
|
|
201
|
-
let req = format!("GET {}\r\n", url.path());
|
|
202
|
-
conn.stream_send(HTTP_REQ_STREAM_ID, req.as_bytes(), true)
|
|
203
|
-
.unwrap();
|
|
204
|
-
|
|
205
|
-
req_sent = true;
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
// Process all readable streams.
|
|
209
|
-
for s in conn.readable() {
|
|
210
|
-
while let Ok((read, fin)) = conn.stream_recv(s, &mut buf) {
|
|
211
|
-
debug!("received {read} bytes");
|
|
212
|
-
|
|
213
|
-
let stream_buf = &buf[..read];
|
|
214
|
-
|
|
215
|
-
debug!(
|
|
216
|
-
"stream {} has {} bytes (fin? {})",
|
|
217
|
-
s,
|
|
218
|
-
stream_buf.len(),
|
|
219
|
-
fin
|
|
220
|
-
);
|
|
221
|
-
|
|
222
|
-
print!("{}", unsafe {
|
|
223
|
-
std::str::from_utf8_unchecked(stream_buf)
|
|
224
|
-
});
|
|
225
|
-
|
|
226
|
-
// The server reported that it has no more data to send, which
|
|
227
|
-
// we got the full response. Close the connection.
|
|
228
|
-
if s == HTTP_REQ_STREAM_ID && fin {
|
|
229
|
-
info!(
|
|
230
|
-
"response received in {:?}, closing...",
|
|
231
|
-
req_start.elapsed()
|
|
232
|
-
);
|
|
233
|
-
|
|
234
|
-
conn.close(true, 0x00, b"kthxbye").unwrap();
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
// Generate outgoing QUIC packets and send them on the UDP socket, until
|
|
240
|
-
// quiche reports that there are no more packets to be sent.
|
|
241
|
-
loop {
|
|
242
|
-
let (write, send_info) = match conn.send(&mut out) {
|
|
243
|
-
Ok(v) => v,
|
|
244
|
-
|
|
245
|
-
Err(quiche::Error::Done) => {
|
|
246
|
-
debug!("done writing");
|
|
247
|
-
break;
|
|
248
|
-
},
|
|
249
|
-
|
|
250
|
-
Err(e) => {
|
|
251
|
-
error!("send failed: {e:?}");
|
|
252
|
-
|
|
253
|
-
conn.close(false, 0x1, b"fail").ok();
|
|
254
|
-
break;
|
|
255
|
-
},
|
|
256
|
-
};
|
|
257
|
-
|
|
258
|
-
if let Err(e) = socket.send_to(&out[..write], send_info.to) {
|
|
259
|
-
if e.kind() == std::io::ErrorKind::WouldBlock {
|
|
260
|
-
debug!("send() would block");
|
|
261
|
-
break;
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
panic!("send() failed: {e:?}");
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
debug!("written {write}");
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
if conn.is_closed() {
|
|
271
|
-
info!("connection closed, {:?}", conn.stats());
|
|
272
|
-
break;
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
fn hex_dump(buf: &[u8]) -> String {
|
|
278
|
-
let vec: Vec<String> = buf.iter().map(|b| format!("{b:02x}")).collect();
|
|
279
|
-
|
|
280
|
-
vec.join("")
|
|
281
|
-
}
|
|
1
|
+
// Copyright (C) 2018-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 ring::rand::*;
|
|
31
|
+
|
|
32
|
+
const MAX_DATAGRAM_SIZE: usize = 1350;
|
|
33
|
+
|
|
34
|
+
const HTTP_REQ_STREAM_ID: u64 = 4;
|
|
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(&[
|
|
83
|
+
b"hq-interop",
|
|
84
|
+
b"hq-29",
|
|
85
|
+
b"hq-28",
|
|
86
|
+
b"hq-27",
|
|
87
|
+
b"http/0.9",
|
|
88
|
+
])
|
|
89
|
+
.unwrap();
|
|
90
|
+
|
|
91
|
+
config.set_max_idle_timeout(5000);
|
|
92
|
+
config.set_max_recv_udp_payload_size(MAX_DATAGRAM_SIZE);
|
|
93
|
+
config.set_max_send_udp_payload_size(MAX_DATAGRAM_SIZE);
|
|
94
|
+
config.set_initial_max_data(10_000_000);
|
|
95
|
+
config.set_initial_max_stream_data_bidi_local(1_000_000);
|
|
96
|
+
config.set_initial_max_stream_data_bidi_remote(1_000_000);
|
|
97
|
+
config.set_initial_max_streams_bidi(100);
|
|
98
|
+
config.set_initial_max_streams_uni(100);
|
|
99
|
+
config.set_disable_active_migration(true);
|
|
100
|
+
|
|
101
|
+
// Generate a random source connection ID for the connection.
|
|
102
|
+
let mut scid = [0; quiche::MAX_CONN_ID_LEN];
|
|
103
|
+
SystemRandom::new().fill(&mut scid[..]).unwrap();
|
|
104
|
+
|
|
105
|
+
let scid = quiche::ConnectionId::from_ref(&scid);
|
|
106
|
+
|
|
107
|
+
// Get local address.
|
|
108
|
+
let local_addr = socket.local_addr().unwrap();
|
|
109
|
+
|
|
110
|
+
// Create a QUIC connection and initiate handshake.
|
|
111
|
+
let mut conn =
|
|
112
|
+
quiche::connect(url.domain(), &scid, local_addr, peer_addr, &mut config)
|
|
113
|
+
.unwrap();
|
|
114
|
+
|
|
115
|
+
info!(
|
|
116
|
+
"connecting to {:} from {:} with scid {}",
|
|
117
|
+
peer_addr,
|
|
118
|
+
socket.local_addr().unwrap(),
|
|
119
|
+
hex_dump(&scid)
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
let (write, send_info) = conn.send(&mut out).expect("initial send failed");
|
|
123
|
+
|
|
124
|
+
while let Err(e) = socket.send_to(&out[..write], send_info.to) {
|
|
125
|
+
if e.kind() == std::io::ErrorKind::WouldBlock {
|
|
126
|
+
debug!("send() would block");
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
panic!("send() failed: {e:?}");
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
debug!("written {write}");
|
|
134
|
+
|
|
135
|
+
let req_start = std::time::Instant::now();
|
|
136
|
+
|
|
137
|
+
let mut req_sent = false;
|
|
138
|
+
|
|
139
|
+
loop {
|
|
140
|
+
poll.poll(&mut events, conn.timeout()).unwrap();
|
|
141
|
+
|
|
142
|
+
// Read incoming UDP packets from the socket and feed them to quiche,
|
|
143
|
+
// until there are no more packets to read.
|
|
144
|
+
'read: loop {
|
|
145
|
+
// If the event loop reported no events, it means that the timeout
|
|
146
|
+
// has expired, so handle it without attempting to read packets. We
|
|
147
|
+
// will then proceed with the send loop.
|
|
148
|
+
if events.is_empty() {
|
|
149
|
+
debug!("timed out");
|
|
150
|
+
|
|
151
|
+
conn.on_timeout();
|
|
152
|
+
break 'read;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
let (len, from) = match socket.recv_from(&mut buf) {
|
|
156
|
+
Ok(v) => v,
|
|
157
|
+
|
|
158
|
+
Err(e) => {
|
|
159
|
+
// There are no more UDP packets to read, so end the read
|
|
160
|
+
// loop.
|
|
161
|
+
if e.kind() == std::io::ErrorKind::WouldBlock {
|
|
162
|
+
debug!("recv() would block");
|
|
163
|
+
break 'read;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
panic!("recv() failed: {e:?}");
|
|
167
|
+
},
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
debug!("got {len} bytes");
|
|
171
|
+
|
|
172
|
+
let recv_info = quiche::RecvInfo {
|
|
173
|
+
to: socket.local_addr().unwrap(),
|
|
174
|
+
from,
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
// Process potentially coalesced packets.
|
|
178
|
+
let read = match conn.recv(&mut buf[..len], recv_info) {
|
|
179
|
+
Ok(v) => v,
|
|
180
|
+
|
|
181
|
+
Err(e) => {
|
|
182
|
+
error!("recv failed: {e:?}");
|
|
183
|
+
continue 'read;
|
|
184
|
+
},
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
debug!("processed {read} bytes");
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
debug!("done reading");
|
|
191
|
+
|
|
192
|
+
if conn.is_closed() {
|
|
193
|
+
info!("connection closed, {:?}", conn.stats());
|
|
194
|
+
break;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// Send an HTTP request as soon as the connection is established.
|
|
198
|
+
if conn.is_established() && !req_sent {
|
|
199
|
+
info!("sending HTTP request for {}", url.path());
|
|
200
|
+
|
|
201
|
+
let req = format!("GET {}\r\n", url.path());
|
|
202
|
+
conn.stream_send(HTTP_REQ_STREAM_ID, req.as_bytes(), true)
|
|
203
|
+
.unwrap();
|
|
204
|
+
|
|
205
|
+
req_sent = true;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// Process all readable streams.
|
|
209
|
+
for s in conn.readable() {
|
|
210
|
+
while let Ok((read, fin)) = conn.stream_recv(s, &mut buf) {
|
|
211
|
+
debug!("received {read} bytes");
|
|
212
|
+
|
|
213
|
+
let stream_buf = &buf[..read];
|
|
214
|
+
|
|
215
|
+
debug!(
|
|
216
|
+
"stream {} has {} bytes (fin? {})",
|
|
217
|
+
s,
|
|
218
|
+
stream_buf.len(),
|
|
219
|
+
fin
|
|
220
|
+
);
|
|
221
|
+
|
|
222
|
+
print!("{}", unsafe {
|
|
223
|
+
std::str::from_utf8_unchecked(stream_buf)
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
// The server reported that it has no more data to send, which
|
|
227
|
+
// we got the full response. Close the connection.
|
|
228
|
+
if s == HTTP_REQ_STREAM_ID && fin {
|
|
229
|
+
info!(
|
|
230
|
+
"response received in {:?}, closing...",
|
|
231
|
+
req_start.elapsed()
|
|
232
|
+
);
|
|
233
|
+
|
|
234
|
+
conn.close(true, 0x00, b"kthxbye").unwrap();
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// Generate outgoing QUIC packets and send them on the UDP socket, until
|
|
240
|
+
// quiche reports that there are no more packets to be sent.
|
|
241
|
+
loop {
|
|
242
|
+
let (write, send_info) = match conn.send(&mut out) {
|
|
243
|
+
Ok(v) => v,
|
|
244
|
+
|
|
245
|
+
Err(quiche::Error::Done) => {
|
|
246
|
+
debug!("done writing");
|
|
247
|
+
break;
|
|
248
|
+
},
|
|
249
|
+
|
|
250
|
+
Err(e) => {
|
|
251
|
+
error!("send failed: {e:?}");
|
|
252
|
+
|
|
253
|
+
conn.close(false, 0x1, b"fail").ok();
|
|
254
|
+
break;
|
|
255
|
+
},
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
if let Err(e) = socket.send_to(&out[..write], send_info.to) {
|
|
259
|
+
if e.kind() == std::io::ErrorKind::WouldBlock {
|
|
260
|
+
debug!("send() would block");
|
|
261
|
+
break;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
panic!("send() failed: {e:?}");
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
debug!("written {write}");
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
if conn.is_closed() {
|
|
271
|
+
info!("connection closed, {:?}", conn.stats());
|
|
272
|
+
break;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
fn hex_dump(buf: &[u8]) -> String {
|
|
278
|
+
let vec: Vec<String> = buf.iter().map(|b| format!("{b:02x}")).collect();
|
|
279
|
+
|
|
280
|
+
vec.join("")
|
|
281
|
+
}
|
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
#!/bin/bash
|
|
2
|
-
set -ex
|
|
3
|
-
cd $(dirname $0)
|
|
4
|
-
openssl req -new -x509 -batch -nodes -days 10000 -keyout rootca.key -out rootca.crt
|
|
5
|
-
openssl req -new -batch -nodes -sha256 -keyout cert.key -out cert.csr -subj '/C=GB/CN=quic.tech'
|
|
6
|
-
openssl x509 -req -days 10000 -in cert.csr -CA rootca.crt -CAkey rootca.key -CAcreateserial -out cert.crt
|
|
7
|
-
openssl verify -CAfile rootca.crt cert.crt
|
|
8
|
-
cp cert.crt cert-big.crt
|
|
9
|
-
cat cert.crt >> cert-big.crt
|
|
10
|
-
cat cert.crt >> cert-big.crt
|
|
11
|
-
cat cert.crt >> cert-big.crt
|
|
12
|
-
cat cert.crt >> cert-big.crt
|
|
13
|
-
rm cert.csr
|
|
14
|
-
rm rootca.key
|
|
15
|
-
rm rootca.srl
|
|
1
|
+
#!/bin/bash
|
|
2
|
+
set -ex
|
|
3
|
+
cd $(dirname $0)
|
|
4
|
+
openssl req -new -x509 -batch -nodes -days 10000 -keyout rootca.key -out rootca.crt
|
|
5
|
+
openssl req -new -batch -nodes -sha256 -keyout cert.key -out cert.csr -subj '/C=GB/CN=quic.tech'
|
|
6
|
+
openssl x509 -req -days 10000 -in cert.csr -CA rootca.crt -CAkey rootca.key -CAcreateserial -out cert.crt
|
|
7
|
+
openssl verify -CAfile rootca.crt cert.crt
|
|
8
|
+
cp cert.crt cert-big.crt
|
|
9
|
+
cat cert.crt >> cert-big.crt
|
|
10
|
+
cat cert.crt >> cert-big.crt
|
|
11
|
+
cat cert.crt >> cert-big.crt
|
|
12
|
+
cat cert.crt >> cert-big.crt
|
|
13
|
+
rm cert.csr
|
|
14
|
+
rm rootca.key
|
|
15
|
+
rm rootca.srl
|