-
Notifications
You must be signed in to change notification settings - Fork 86
/
server.rs
207 lines (171 loc) · 6.2 KB
/
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
use std::{net::SocketAddr, path::PathBuf, sync::Arc};
use bytes::{Bytes, BytesMut};
use http::{Request, StatusCode};
use rustls::pki_types::{CertificateDer, PrivateKeyDer};
use structopt::StructOpt;
use tokio::{fs::File, io::AsyncReadExt};
use tracing::{error, info, trace_span};
use h3::{error::ErrorLevel, quic::BidiStream, server::RequestStream};
use h3_quinn::quinn::{self, crypto::rustls::QuicServerConfig};
#[derive(StructOpt, Debug)]
#[structopt(name = "server")]
struct Opt {
#[structopt(
name = "dir",
short,
long,
help = "Root directory of the files to serve. \
If omitted, server will respond OK."
)]
pub root: Option<PathBuf>,
#[structopt(
short,
long,
default_value = "[::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/server.cert",
help = "Certificate for TLS. If present, `--key` is mandatory."
)]
pub cert: PathBuf,
#[structopt(
long,
short,
default_value = "examples/server.key",
help = "Private key for the certificate."
)]
pub key: PathBuf,
}
static ALPN: &[u8] = b"h3";
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
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)
.with_max_level(tracing::Level::INFO)
.init();
// process cli arguments
let opt = Opt::from_args();
let root = if let Some(root) = opt.root {
if !root.is_dir() {
return Err(format!("{}: is not a readable directory", root.display()).into());
} else {
info!("serving {}", root.display());
Arc::new(Some(root))
}
} else {
Arc::new(None)
};
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;
tls_config.alpn_protocols = vec![ALPN.into()];
let server_config =
quinn::ServerConfig::with_crypto(Arc::new(QuicServerConfig::try_from(tls_config)?));
let endpoint = quinn::Endpoint::server(server_config, opt.listen)?;
info!("listening on {}", opt.listen);
// handle incoming connections and requests
while let Some(new_conn) = endpoint.accept().await {
trace_span!("New connection being attempted");
let root = root.clone();
tokio::spawn(async move {
match new_conn.await {
Ok(conn) => {
info!("new connection established");
let mut h3_conn = h3::server::Connection::new(h3_quinn::Connection::new(conn))
.await
.unwrap();
loop {
match h3_conn.accept().await {
Ok(Some((req, stream))) => {
info!("new request: {:#?}", req);
let root = root.clone();
tokio::spawn(async {
if let Err(e) = handle_request(req, stream, root).await {
error!("handling request failed: {}", e);
}
});
}
// 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,
}
}
}
}
}
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_request<T>(
req: Request<()>,
mut stream: RequestStream<T, Bytes>,
serve_root: Arc<Option<PathBuf>>,
) -> Result<(), Box<dyn std::error::Error>>
where
T: BidiStream<Bytes>,
{
let (status, to_serve) = match serve_root.as_deref() {
None => (StatusCode::OK, None),
Some(_) if req.uri().path().contains("..") => (StatusCode::NOT_FOUND, None),
Some(root) => {
let to_serve = root.join(req.uri().path().strip_prefix('/').unwrap_or(""));
match File::open(&to_serve).await {
Ok(file) => (StatusCode::OK, Some(file)),
Err(e) => {
error!("failed to open: \"{}\": {}", to_serve.to_string_lossy(), e);
(StatusCode::NOT_FOUND, None)
}
}
}
};
let resp = http::Response::builder().status(status).body(()).unwrap();
match stream.send_response(resp).await {
Ok(_) => {
info!("successfully respond to connection");
}
Err(err) => {
error!("unable to send response to connection peer: {:?}", err);
}
}
if let Some(mut file) = to_serve {
loop {
let mut buf = BytesMut::with_capacity(4096 * 10);
if file.read_buf(&mut buf).await? == 0 {
break;
}
stream.send_data(buf.freeze()).await?;
}
}
Ok(stream.finish().await?)
}