forked from hyperium/h3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webtransport_server.rs
338 lines (289 loc) · 11.9 KB
/
webtransport_server.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
use anyhow::{Context, Result};
use bytes::{BufMut, Bytes, BytesMut};
use h3::{
error::ErrorLevel,
ext::Protocol,
quic::{self, RecvDatagramExt, SendDatagramExt, SendStreamUnframed},
server::Connection,
};
use h3_quinn::quinn::{self, crypto::rustls::QuicServerConfig};
use h3_webtransport::{
server::{self, WebTransportSession},
stream,
};
use http::Method;
use rustls::pki_types::{CertificateDer, PrivateKeyDer};
use std::{net::SocketAddr, path::PathBuf, sync::Arc, time::Duration};
use structopt::StructOpt;
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
use tokio::pin;
use tracing::{error, info, trace_span};
#[derive(StructOpt, Debug)]
#[structopt(name = "server")]
struct Opt {
#[structopt(
short,
long,
default_value = "127.0.0.1:4433",
help = "What address:port to listen for new connections"
)]
pub listen: SocketAddr,
#[structopt(flatten)]
pub certs: Certs,
}
#[derive(StructOpt, Debug)]
pub struct Certs {
#[structopt(
long,
short,
default_value = "examples/localhost.crt",
help = "Certificate for TLS. If present, `--key` is mandatory."
)]
pub cert: PathBuf,
#[structopt(
long,
short,
default_value = "examples/localhost.key",
help = "Private key for the certificate."
)]
pub key: PathBuf,
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// 0. Setup tracing
#[cfg(not(feature = "tree"))]
tracing_subscriber::fmt()
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
.with_span_events(tracing_subscriber::fmt::format::FmtSpan::FULL)
.with_writer(std::io::stderr)
.init();
#[cfg(feature = "tree")]
use tracing_subscriber::prelude::*;
#[cfg(feature = "tree")]
tracing_subscriber::registry()
.with(tracing_subscriber::EnvFilter::from_default_env())
.with(tracing_tree::HierarchicalLayer::new(4).with_bracketed_fields(true))
.init();
// process cli arguments
let opt = Opt::from_args();
tracing::info!("Opt: {opt:#?}");
let Certs { cert, key } = opt.certs;
// create quinn server endpoint and bind UDP socket
// both cert and key must be DER-encoded
let cert = CertificateDer::from(std::fs::read(cert)?);
let key = PrivateKeyDer::try_from(std::fs::read(key)?)?;
let mut tls_config = rustls::ServerConfig::builder()
.with_no_client_auth()
.with_single_cert(vec![cert], key)?;
tls_config.max_early_data_size = u32::MAX;
let alpn: Vec<Vec<u8>> = vec![
b"h3".to_vec(),
b"h3-32".to_vec(),
b"h3-31".to_vec(),
b"h3-30".to_vec(),
b"h3-29".to_vec(),
];
tls_config.alpn_protocols = alpn;
let mut server_config =
quinn::ServerConfig::with_crypto(Arc::new(QuicServerConfig::try_from(tls_config)?));
let mut transport_config = quinn::TransportConfig::default();
transport_config.keep_alive_interval(Some(Duration::from_secs(2)));
server_config.transport = Arc::new(transport_config);
let endpoint = quinn::Endpoint::server(server_config, opt.listen)?;
info!("listening on {}", opt.listen);
// 2. Accept new quic connections and spawn a new task to handle them
while let Some(new_conn) = endpoint.accept().await {
trace_span!("New connection being attempted");
tokio::spawn(async move {
match new_conn.await {
Ok(conn) => {
info!("new http3 established");
let h3_conn = h3::server::builder()
.enable_webtransport(true)
.enable_connect(true)
.enable_datagram(true)
.max_webtransport_sessions(1)
.send_grease(true)
.build(h3_quinn::Connection::new(conn))
.await
.unwrap();
// tracing::info!("Establishing WebTransport session");
// // 3. TODO: Conditionally, if the client indicated that this is a webtransport session, we should accept it here, else use regular h3.
// // if this is a webtransport session, then h3 needs to stop handing the datagrams, bidirectional streams, and unidirectional streams and give them
// // to the webtransport session.
tokio::spawn(async move {
if let Err(err) = handle_connection(h3_conn).await {
tracing::error!("Failed to handle connection: {err:?}");
}
});
// let mut session: WebTransportSession<_, Bytes> =
// WebTransportSession::accept(h3_conn).await.unwrap();
// tracing::info!("Finished establishing webtransport session");
// // 4. Get datagrams, bidirectional streams, and unidirectional streams and wait for client requests here.
// // h3_conn needs to hand over the datagrams, bidirectional streams, and unidirectional streams to the webtransport session.
// let result = handle.await;
}
Err(err) => {
error!("accepting connection failed: {:?}", err);
}
}
});
}
// shut down gracefully
// wait for connections to be closed before exiting
endpoint.wait_idle().await;
Ok(())
}
async fn handle_connection(mut conn: Connection<h3_quinn::Connection, Bytes>) -> Result<()> {
// 3. TODO: Conditionally, if the client indicated that this is a webtransport session, we should accept it here, else use regular h3.
// if this is a webtransport session, then h3 needs to stop handing the datagrams, bidirectional streams, and unidirectional streams and give them
// to the webtransport session.
loop {
match conn.accept().await {
Ok(Some((req, stream))) => {
info!("new request: {:#?}", req);
let ext = req.extensions();
match req.method() {
&Method::CONNECT if ext.get::<Protocol>() == Some(&Protocol::WEB_TRANSPORT) => {
tracing::info!("Peer wants to initiate a webtransport session");
tracing::info!("Handing over connection to WebTransport");
let session = WebTransportSession::accept(req, stream, conn).await?;
tracing::info!("Established webtransport session");
// 4. Get datagrams, bidirectional streams, and unidirectional streams and wait for client requests here.
// h3_conn needs to hand over the datagrams, bidirectional streams, and unidirectional streams to the webtransport session.
handle_session_and_echo_all_inbound_messages(session).await?;
return Ok(());
}
_ => {
tracing::info!(?req, "Received request");
}
}
}
// indicating no more streams to be received
Ok(None) => {
break;
}
Err(err) => {
error!("Error on accept {}", err);
match err.get_error_level() {
ErrorLevel::ConnectionError => break,
ErrorLevel::StreamError => continue,
}
}
}
}
Ok(())
}
macro_rules! log_result {
($expr:expr) => {
if let Err(err) = $expr {
tracing::error!("{err:?}");
}
};
}
async fn echo_stream<T, R>(send: T, recv: R) -> anyhow::Result<()>
where
T: AsyncWrite,
R: AsyncRead,
{
pin!(send);
pin!(recv);
tracing::info!("Got stream");
let mut buf = Vec::new();
recv.read_to_end(&mut buf).await?;
let message = Bytes::from(buf);
send_chunked(send, message).await?;
Ok(())
}
// Used to test that all chunks arrive properly as it is easy to write an impl which only reads and
// writes the first chunk.
async fn send_chunked(mut send: impl AsyncWrite + Unpin, data: Bytes) -> anyhow::Result<()> {
for chunk in data.chunks(4) {
tokio::time::sleep(Duration::from_millis(100)).await;
tracing::info!("Sending {chunk:?}");
send.write_all(chunk).await?;
}
Ok(())
}
async fn open_bidi_test<S>(mut stream: S) -> anyhow::Result<()>
where
S: Unpin + AsyncRead + AsyncWrite,
{
tracing::info!("Opening bidirectional stream");
stream
.write_all(b"Hello from a server initiated bidi stream")
.await
.context("Failed to respond")?;
let mut resp = Vec::new();
stream.shutdown().await?;
stream.read_to_end(&mut resp).await?;
tracing::info!("Got response from client: {resp:?}");
Ok(())
}
/// This method will echo all inbound datagrams, unidirectional and bidirectional streams.
#[tracing::instrument(level = "info", skip(session))]
async fn handle_session_and_echo_all_inbound_messages<C>(
session: WebTransportSession<C, Bytes>,
) -> anyhow::Result<()>
where
// Use trait bounds to ensure we only happen to use implementation that are only for the quinn
// backend.
C: 'static
+ Send
+ h3::quic::Connection<Bytes>
+ RecvDatagramExt<Buf = Bytes>
+ SendDatagramExt<Bytes>,
<C::SendStream as h3::quic::SendStream<Bytes>>::Error:
'static + std::error::Error + Send + Sync + Into<std::io::Error>,
<C::RecvStream as h3::quic::RecvStream>::Error:
'static + std::error::Error + Send + Sync + Into<std::io::Error>,
stream::BidiStream<C::BidiStream, Bytes>:
quic::BidiStream<Bytes> + Unpin + AsyncWrite + AsyncRead,
<stream::BidiStream<C::BidiStream, Bytes> as quic::BidiStream<Bytes>>::SendStream:
Unpin + AsyncWrite + Send + Sync,
<stream::BidiStream<C::BidiStream, Bytes> as quic::BidiStream<Bytes>>::RecvStream:
Unpin + AsyncRead + Send + Sync,
C::SendStream: Send + Unpin,
C::RecvStream: Send + Unpin,
C::BidiStream: Send + Unpin,
stream::SendStream<C::SendStream, Bytes>: AsyncWrite,
C::BidiStream: SendStreamUnframed<Bytes>,
C::SendStream: SendStreamUnframed<Bytes>,
{
let session_id = session.session_id();
// This will open a bidirectional stream and send a message to the client right after connecting!
let stream = session.open_bi(session_id).await?;
tokio::spawn(async move { log_result!(open_bidi_test(stream).await) });
loop {
tokio::select! {
datagram = session.accept_datagram() => {
let datagram = datagram?;
if let Some((_, datagram)) = datagram {
tracing::info!("Responding with {datagram:?}");
// Put something before to make sure encoding and decoding works and don't just
// pass through
let mut resp = BytesMut::from(&b"Response: "[..]);
resp.put(datagram);
session.send_datagram(resp.freeze())?;
tracing::info!("Finished sending datagram");
}
}
uni_stream = session.accept_uni() => {
let (id, stream) = uni_stream?.unwrap();
let send = session.open_uni(id).await?;
tokio::spawn( async move { log_result!(echo_stream(send, stream).await); });
}
stream = session.accept_bi() => {
if let Some(server::AcceptedBi::BidiStream(_, stream)) = stream? {
let (send, recv) = quic::BidiStream::split(stream);
tokio::spawn( async move { log_result!(echo_stream(send, recv).await); });
}
}
else => {
break
}
}
}
tracing::info!("Finished handling session");
Ok(())
}