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
use anyhow::{anyhow, Result};
use async_trait::async_trait;
use futures_util::StreamExt;
use quinn::{Endpoint, Incoming, ServerConfig};
use std::{error::Error, fs, net::SocketAddr, net::ToSocketAddrs, sync::Arc};
use url::Url;
pub struct QuicMessage {
pub server: QuicServer,
pub client: QuicClient,
}
impl QuicMessage {
pub fn new(client: QuicClient, server: QuicServer) -> QuicMessage {
QuicMessage { server, client }
}
}
#[async_trait]
pub trait QuicSocket {
fn new(addr: Option<SocketAddr>) -> Self;
async fn send(&mut self, payload: Vec<u8>) -> Result<()>;
async fn recv(&mut self) -> Result<std::vec::Vec<u8>>;
}
pub struct QuicServer {
pub endpoint: Endpoint,
pub incoming: Incoming,
}
pub struct QuicClient {
pub endpoint: Endpoint,
}
#[async_trait]
impl QuicSocket for QuicServer {
fn new(addr: Option<SocketAddr>) -> QuicServer {
let server_config = configure_server().unwrap();
let (endpoint, incoming) = quinn::Endpoint::server(server_config, addr.unwrap()).unwrap();
QuicServer { endpoint, incoming }
}
async fn send(&mut self, payload: Vec<u8>) -> Result<()> {
while let Some(conn) = self.incoming.next().await {
let quinn::NewConnection { mut bi_streams, .. } = conn.await?;
while let Some(stream) = bi_streams.next().await {
let stream = match stream {
Err(quinn::ConnectionError::ApplicationClosed { .. }) => {
return Ok(());
}
Err(e) => {
return Err(anyhow::Error::new(e));
}
Ok(s) => s,
};
tokio::spawn(handle_send(stream, payload.clone()));
break;
}
break;
}
Ok(())
}
async fn recv(&mut self) -> Result<std::vec::Vec<u8>> {
let mut ret = None;
while let Some(conn) = self.incoming.next().await {
ret = Some(tokio::spawn(handle_connection(conn)).await);
break;
}
Ok(ret.unwrap().unwrap().unwrap())
}
}
#[async_trait]
impl QuicSocket for QuicClient {
fn new(_addr: Option<SocketAddr>) -> Self {
let ca = "cert.der".to_string();
let mut roots = rustls::RootCertStore::empty();
roots
.add(&rustls::Certificate(fs::read(&ca).unwrap()))
.unwrap();
let mut client_crypto = rustls::ClientConfig::builder()
.with_safe_defaults()
.with_root_certificates(roots)
.with_no_client_auth();
client_crypto.alpn_protocols = ALPN_QUIC_HTTP.iter().map(|&x| x.into()).collect();
let mut endpoint = quinn::Endpoint::client("[::]:0".parse().unwrap()).unwrap();
endpoint.set_default_client_config(quinn::ClientConfig::new(Arc::new(client_crypto)));
QuicClient { endpoint }
}
async fn send(&mut self, payload: Vec<u8>) -> Result<()> {
let remote_url = Url::parse("http://127.0.0.1:4442").unwrap();
let host = Some("localhost".to_string());
let remote = (
remote_url.host_str().unwrap(),
remote_url.port().unwrap_or(4433),
)
.to_socket_addrs()?
.next()
.ok_or_else(|| anyhow!("couldn't resolve to an address"))
.unwrap();
let host = host
.as_ref()
.map_or_else(|| remote_url.host_str(), |x| Some(x))
.ok_or_else(|| anyhow!("no hostname specified"))?;
let new_conn = self
.endpoint
.connect(remote, host)?
.await
.map_err(|e| anyhow!("failed to connect: {}", e))?;
let quinn::NewConnection {
connection: conn, ..
} = new_conn;
let (mut send, _) = conn
.open_bi()
.await
.map_err(|e| anyhow!("failed to open stream: {}", e))?;
send.write_all(&payload)
.await
.map_err(|e| anyhow!("failed to send request: {}", e))?;
send.finish()
.await
.map_err(|e| anyhow!("failed to shutdown stream: {}", e))?;
conn.close(0u32.into(), b"done");
self.endpoint.wait_idle().await;
Ok(())
}
async fn recv(&mut self) -> Result<std::vec::Vec<u8>> {
let remote_url = Url::parse("http://127.0.0.1:4442").unwrap();
let host = Some("localhost".to_string());
let remote = (
remote_url.host_str().unwrap(),
remote_url.port().unwrap_or(4433),
)
.to_socket_addrs()?
.next()
.ok_or_else(|| anyhow!("couldn't resolve to an address"))
.unwrap();
let host = host
.as_ref()
.map_or_else(|| remote_url.host_str(), |x| Some(x))
.ok_or_else(|| anyhow!("no hostname specified"))?;
let new_conn = self
.endpoint
.connect(remote, host)?
.await
.map_err(|e| anyhow!("failed to connect: {}", e))?;
let quinn::NewConnection {
connection: conn, ..
} = new_conn;
let (_, recv) = conn
.open_bi()
.await
.map_err(|e| anyhow!("failed to open stream: {}", e))?;
let recv_bytes = recv
.read_to_end(usize::max_value())
.await
.map_err(|e| anyhow!("failed to read response: {}", e))?;
Ok(recv_bytes)
}
}
#[allow(clippy::field_reassign_with_default)]
fn configure_server() -> Result<ServerConfig, Box<dyn Error>> {
let cert_chain = fs::read("./cert.der")?;
let key = fs::read("./key.der")?;
let priv_key = rustls::PrivateKey(key);
let cert = vec![rustls::Certificate(cert_chain.clone())];
let mut server_crypto = rustls::ServerConfig::builder()
.with_safe_defaults()
.with_no_client_auth()
.with_single_cert(cert, priv_key)?;
server_crypto.alpn_protocols = ALPN_QUIC_HTTP.iter().map(|&x| x.into()).collect();
let mut server_config = quinn::ServerConfig::with_crypto(Arc::new(server_crypto));
Arc::get_mut(&mut server_config.transport)
.unwrap()
.max_concurrent_uni_streams(0_u8.into());
Ok(server_config)
}
#[allow(unused)]
#[allow(clippy::field_reassign_with_default)]
pub fn gen_certificates() -> Result<(), Box<dyn Error>> {
let cert = rcgen::generate_simple_self_signed(vec!["localhost".into()]).unwrap();
let cert_der = cert.serialize_der().unwrap();
fs::write("./cert.der".to_string(), &cert_der).unwrap();
let priv_key = cert.serialize_private_key_der();
fs::write("./key.der".to_string(), &priv_key).unwrap();
let key = rustls::PrivateKey(priv_key);
let cert = vec![rustls::Certificate(cert_der.clone())];
let mut server_crypto = rustls::ServerConfig::builder()
.with_safe_defaults()
.with_no_client_auth()
.with_single_cert(cert, key)?;
server_crypto.alpn_protocols = ALPN_QUIC_HTTP.iter().map(|&x| x.into()).collect();
Ok(())
}
#[allow(unused)]
pub const ALPN_QUIC_HTTP: &[&[u8]] = &[b"hq-29"];
async fn handle_connection(conn: quinn::Connecting) -> Result<std::vec::Vec<u8>> {
Ok(handle_spawn(conn).await.unwrap())
}
async fn handle_spawn(conn: quinn::Connecting) -> Result<std::vec::Vec<u8>> {
let quinn::NewConnection {
connection: _,
mut bi_streams,
..
} = conn.await?;
let mut req = None;
while let Some(stream) = bi_streams.next().await {
let stream = match stream {
Err(quinn::ConnectionError::ApplicationClosed { .. }) => {
return Ok(vec![]);
}
Err(e) => {
return Err(anyhow::Error::new(e));
}
Ok(s) => s,
};
req = Some(tokio::spawn(handle_request(stream)).await?);
break;
}
Ok(req.unwrap())
}
async fn handle_request((_, recv): (quinn::SendStream, quinn::RecvStream)) -> std::vec::Vec<u8> {
let req = recv
.read_to_end(64 * 1024)
.await
.map_err(|e| anyhow!("failed reading request: {}", e))
.unwrap();
req
}
async fn handle_send(
(mut send, _): (quinn::SendStream, quinn::RecvStream),
payload: Vec<u8>,
) -> Result<()> {
send.write_all(&payload)
.await
.map_err(|e| anyhow!("failed to send response: {}", e))?;
send.finish()
.await
.map_err(|e| anyhow!("failed to shutdown stream: {}", e))?;
Ok(())
}