🚧 wip: PTTH_QUIC to quinn 0.9.x

main
(on company time) 2022-12-16 13:49:34 -06:00
parent c13d1f37bf
commit 996543cecc
9 changed files with 41 additions and 75 deletions

51
Cargo.lock generated
View File

@ -553,15 +553,6 @@ dependencies = [
"slab", "slab",
] ]
[[package]]
name = "fxhash"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c31b6d751ae2c7f11320402d34e41349dd1016f8d5d45e48c4312bc8625af50c"
dependencies = [
"byteorder",
]
[[package]] [[package]]
name = "generic-array" name = "generic-array"
version = "0.14.6" version = "0.14.6"
@ -1516,16 +1507,15 @@ checksum = "a993555f31e5a609f617c12db6250dedcac1b0a85076912c436e6fc9b2c8e6a3"
[[package]] [[package]]
name = "quinn" name = "quinn"
version = "0.8.5" version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b435e71d9bfa0d8889927231970c51fb89c58fa63bffcab117c9c7a41e5ef8f" checksum = "445cbfe2382fa023c4f2f3c7e1c95c03dcc1df2bf23cebcb2b13e1402c4394d1"
dependencies = [ dependencies = [
"bytes", "bytes",
"futures-channel", "pin-project-lite",
"futures-util",
"fxhash",
"quinn-proto", "quinn-proto",
"quinn-udp", "quinn-udp",
"rustc-hash",
"rustls", "rustls",
"thiserror", "thiserror",
"tokio", "tokio",
@ -1535,17 +1525,16 @@ dependencies = [
[[package]] [[package]]
name = "quinn-proto" name = "quinn-proto"
version = "0.8.4" version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fce546b9688f767a57530652488420d419a8b1f44a478b451c3d1ab6d992a55" checksum = "72ef4ced82a24bb281af338b9e8f94429b6eca01b4e66d899f40031f074e74c9"
dependencies = [ dependencies = [
"bytes", "bytes",
"fxhash",
"rand", "rand",
"ring", "ring",
"rustc-hash",
"rustls", "rustls",
"rustls-native-certs", "rustls-native-certs",
"rustls-pemfile 0.2.1",
"slab", "slab",
"thiserror", "thiserror",
"tinyvec", "tinyvec",
@ -1555,16 +1544,15 @@ dependencies = [
[[package]] [[package]]
name = "quinn-udp" name = "quinn-udp"
version = "0.1.4" version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b07946277141531aea269befd949ed16b2c85a780ba1043244eda0969e538e54" checksum = "641538578b21f5e5c8ea733b736895576d0fe329bb883b937db6f4d163dbaaf4"
dependencies = [ dependencies = [
"futures-util",
"libc", "libc",
"quinn-proto", "quinn-proto",
"socket2", "socket2",
"tokio",
"tracing", "tracing",
"windows-sys 0.42.0",
] ]
[[package]] [[package]]
@ -1688,7 +1676,7 @@ dependencies = [
"percent-encoding", "percent-encoding",
"pin-project-lite", "pin-project-lite",
"rustls", "rustls",
"rustls-pemfile 1.0.1", "rustls-pemfile",
"serde", "serde",
"serde_json", "serde_json",
"serde_urlencoded", "serde_urlencoded",
@ -1787,6 +1775,12 @@ dependencies = [
"walkdir", "walkdir",
] ]
[[package]]
name = "rustc-hash"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
[[package]] [[package]]
name = "rustls" name = "rustls"
version = "0.20.7" version = "0.20.7"
@ -1806,20 +1800,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0167bac7a9f490495f3c33013e7722b53cb087ecbe082fb0c6387c96f634ea50" checksum = "0167bac7a9f490495f3c33013e7722b53cb087ecbe082fb0c6387c96f634ea50"
dependencies = [ dependencies = [
"openssl-probe", "openssl-probe",
"rustls-pemfile 1.0.1", "rustls-pemfile",
"schannel", "schannel",
"security-framework", "security-framework",
] ]
[[package]]
name = "rustls-pemfile"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5eebeaeb360c87bfb72e84abdb3447159c0eaececf1bef2aecd65a8be949d1c9"
dependencies = [
"base64 0.13.1",
]
[[package]] [[package]]
name = "rustls-pemfile" name = "rustls-pemfile"
version = "1.0.1" version = "1.0.1"

View File

@ -13,7 +13,7 @@ base64 = "0.20.0"
ctrlc = "3.2.4" ctrlc = "3.2.4"
futures-util = "0.3.25" futures-util = "0.3.25"
hyper = { version = "0.14.23", features = ["http1", "server", "stream", "tcp"] } hyper = { version = "0.14.23", features = ["http1", "server", "stream", "tcp"] }
quinn = "0.8.5" quinn = "0.9.3"
rand = "0.8.5" rand = "0.8.5"
rcgen = "0.10.0" rcgen = "0.10.0"
rmp-serde = "1.1.1" rmp-serde = "1.1.1"

View File

@ -61,10 +61,7 @@ impl P2Client {
let conf = Arc::clone (&self.conf); let conf = Arc::clone (&self.conf);
let quinn::NewConnection { let connection = protocol::p2_connect_to_p3 (&self.endpoint, conf.relay_addr, &conf.client_id).await?;
connection,
..
} = protocol::p2_connect_to_p3 (&self.endpoint, conf.relay_addr, &conf.client_id).await?;
let client_tcp_port = conf.client_tcp_port; let client_tcp_port = conf.client_tcp_port;

View File

@ -140,10 +140,7 @@ impl P4EndServer {
let conf = Arc::new (conf); let conf = Arc::new (conf);
trace! ("P4 end server connecting to P3 relay server"); trace! ("P4 end server connecting to P3 relay server");
let quinn::NewConnection { let conn = protocol::p4_connect_to_p3 (
mut bi_streams,
..
} = protocol::p4_connect_to_p3 (
&self.endpoint, &self.endpoint,
conf.relay_addr, conf.relay_addr,
&conf.id &conf.id
@ -163,8 +160,8 @@ impl P4EndServer {
break; break;
} }
} }
stream_opt = bi_streams.next () => { stream_opt = conn.accept_bi () => {
let (relay_send, relay_recv) = stream_opt.ok_or_else (|| anyhow::anyhow! ("P4 ran out of incoming streams. Maybe P3 shut down or disconnected?"))??; let (relay_send, relay_recv) = stream_opt?;
tokio::spawn (handle_bi_stream (Arc::clone (&conf), relay_send, relay_recv)); tokio::spawn (handle_bi_stream (Arc::clone (&conf), relay_send, relay_recv));
} }

View File

@ -28,7 +28,7 @@ pub struct Opt {
pub async fn main (opt: Opt) -> anyhow::Result <()> pub async fn main (opt: Opt) -> anyhow::Result <()>
{ {
let listen_addr = opt.listen_addr.unwrap_or_else (|| String::from ("0.0.0.0:30380")).parse ()?; let listen_addr = opt.listen_addr.unwrap_or_else (|| String::from ("0.0.0.0:30380")).parse ()?;
let (mut incoming, server_cert) = make_server_endpoint (listen_addr)?; let (endpoint, server_cert) = make_server_endpoint (listen_addr)?;
println! ("Base64 cert: {}", base64::encode (&server_cert)); println! ("Base64 cert: {}", base64::encode (&server_cert));
tokio::fs::create_dir_all ("ptth_quic_output").await?; tokio::fs::create_dir_all ("ptth_quic_output").await?;
@ -57,7 +57,7 @@ pub async fn main (opt: Opt) -> anyhow::Result <()>
let task_quic_server = { let task_quic_server = {
let relay_state = Arc::clone (&relay_state); let relay_state = Arc::clone (&relay_state);
tokio::spawn (async move { tokio::spawn (async move {
while let Some (conn) = incoming.next ().await { while let Some (conn) = endpoint.accept ().await {
let relay_state = Arc::clone (&relay_state); let relay_state = Arc::clone (&relay_state);
// Each new peer QUIC connection gets its own task // Each new peer QUIC connection gets its own task
@ -286,13 +286,13 @@ async fn handle_quic_connection (
conn: quinn::Connecting, conn: quinn::Connecting,
) -> anyhow::Result <()> ) -> anyhow::Result <()>
{ {
let mut conn = conn.await?; let conn = conn.await?;
// Everyone who connects must identify themselves with the first // Everyone who connects must identify themselves with the first
// bi stream // bi stream
// TODO: Timeout // TODO: Timeout
let (mut send, mut recv) = conn.bi_streams.next ().await.ok_or_else (|| anyhow::anyhow! ("QUIC client didn't identify itself"))??; let (mut send, mut recv) = conn.accept_bi ().await?;
let peer = protocol::p3_accept_peer (&mut recv).await?; let peer = protocol::p3_accept_peer (&mut recv).await?;
@ -318,19 +318,13 @@ async fn handle_quic_connection (
async fn handle_p2_connection ( async fn handle_p2_connection (
relay_state: Arc <RelayState>, relay_state: Arc <RelayState>,
conn: quinn::NewConnection, conn: quinn::Connection,
peer: protocol::P2ClientProxy, peer: protocol::P2ClientProxy,
) -> anyhow::Result <()> ) -> anyhow::Result <()>
{ {
let client_id = peer.id; let client_id = peer.id;
let quinn::NewConnection { while let Ok ((send, mut recv)) = conn.accept_bi ().await {
mut bi_streams,
..
} = conn;
while let Some (bi_stream) = bi_streams.next ().await {
let (send, mut recv) = bi_stream?;
let relay_state = Arc::clone (&relay_state); let relay_state = Arc::clone (&relay_state);
let client_id = client_id.clone (); let client_id = client_id.clone ();
@ -442,15 +436,11 @@ async fn handle_direc_p2_to_p4 (
async fn handle_p4_connection ( async fn handle_p4_connection (
relay_state: Arc <RelayState>, relay_state: Arc <RelayState>,
conn: quinn::NewConnection, connection: quinn::Connection,
peer: protocol::P4ServerProxy, peer: protocol::P4ServerProxy,
) -> anyhow::Result <()> ) -> anyhow::Result <()>
{ {
let server_id = peer.id; let server_id = peer.id;
let quinn::NewConnection {
connection,
..
} = conn;
let (tx, mut rx) = mpsc::channel (2); let (tx, mut rx) = mpsc::channel (2);
let p4_state = P4State { let p4_state = P4State {

View File

@ -33,14 +33,14 @@ pub async fn p2_connect_to_p3 (
endpoint: &quinn::Endpoint, endpoint: &quinn::Endpoint,
relay_addr: std::net::SocketAddr, relay_addr: std::net::SocketAddr,
client_id: &str, client_id: &str,
) -> Result <quinn::NewConnection> ) -> Result <quinn::Connection>
{ {
if client_id.as_bytes ().len () > MAX_ID_LENGTH { if client_id.as_bytes ().len () > MAX_ID_LENGTH {
bail! ("Client ID is longer than MAX_ID_LENGTH"); bail! ("Client ID is longer than MAX_ID_LENGTH");
} }
let new_conn = endpoint.connect (relay_addr, "localhost")?.await?; let new_conn = endpoint.connect (relay_addr, "localhost")?.await?;
let (mut send, mut recv) = new_conn.connection.open_bi ().await?; let (mut send, mut recv) = new_conn.open_bi ().await?;
let cmd_type = Command::CONNECT_P2_TO_P3.0; let cmd_type = Command::CONNECT_P2_TO_P3.0;
send.write_all (&[cmd_type, 0, 0, 0]).await?; send.write_all (&[cmd_type, 0, 0, 0]).await?;
@ -251,14 +251,14 @@ pub async fn p4_connect_to_p3 (
endpoint: &quinn::Endpoint, endpoint: &quinn::Endpoint,
relay_addr: std::net::SocketAddr, relay_addr: std::net::SocketAddr,
server_id: &str, server_id: &str,
) -> Result <quinn::NewConnection> ) -> Result <quinn::Connection>
{ {
if server_id.as_bytes ().len () > MAX_ID_LENGTH { if server_id.as_bytes ().len () > MAX_ID_LENGTH {
bail! ("Server ID is longer than MAX_ID_LENGTH"); bail! ("Server ID is longer than MAX_ID_LENGTH");
} }
let new_conn = endpoint.connect (relay_addr, "localhost")?.await?; let new_conn = endpoint.connect (relay_addr, "localhost")?.await?;
let (mut send, mut recv) = new_conn.connection.open_bi ().await?; let (mut send, mut recv) = new_conn.open_bi ().await?;
let cmd_type = Command::CONNECT_P4_TO_P3.0; let cmd_type = Command::CONNECT_P4_TO_P3.0;
send.write_all (&[cmd_type, 0, 0, 0]).await?; send.write_all (&[cmd_type, 0, 0, 0]).await?;

View File

@ -8,7 +8,7 @@ use std::{
}; };
use quinn::{ use quinn::{
ClientConfig, Endpoint, Incoming, ClientConfig, Endpoint,
ServerConfig, TransportConfig, ServerConfig, TransportConfig,
}; };
@ -26,7 +26,7 @@ pub fn make_client_endpoint(
let mut transport = quinn::TransportConfig::default (); let mut transport = quinn::TransportConfig::default ();
transport.keep_alive_interval (Some (Duration::from_millis (5_000))); transport.keep_alive_interval (Some (Duration::from_millis (5_000)));
client_cfg.transport = Arc::new (transport); client_cfg.transport_config (Arc::new (transport));
let mut endpoint = Endpoint::client (bind_addr)?; let mut endpoint = Endpoint::client (bind_addr)?;
endpoint.set_default_client_config (client_cfg); endpoint.set_default_client_config (client_cfg);
@ -41,10 +41,10 @@ pub fn make_client_endpoint(
/// - a stream of incoming QUIC connections /// - a stream of incoming QUIC connections
/// - server certificate serialized into DER format /// - server certificate serialized into DER format
#[allow(unused)] #[allow(unused)]
pub fn make_server_endpoint(bind_addr: SocketAddr) -> anyhow::Result<(Incoming, Vec<u8>)> { pub fn make_server_endpoint(bind_addr: SocketAddr) -> anyhow::Result<(Endpoint, Vec<u8>)> {
let (server_config, server_cert) = configure_server()?; let (server_config, server_cert) = configure_server()?;
let (_endpoint, incoming) = Endpoint::server (server_config, bind_addr)?; let endpoint = Endpoint::server (server_config, bind_addr)?;
Ok((incoming, server_cert)) Ok((endpoint, server_cert))
} }
/// Builds default quinn client config and trusts given certificates. /// Builds default quinn client config and trusts given certificates.

View File

@ -12,7 +12,7 @@ anyhow = "1.0.38"
blake3 = "1.0.0" blake3 = "1.0.0"
fltk = "1.2.8" fltk = "1.2.8"
ptth_quic = { path = "../ptth_quic" } ptth_quic = { path = "../ptth_quic" }
quinn = "0.8.5" quinn = "0.9.3"
rand = "0.8.4" rand = "0.8.4"
rand_chacha = "0.3.1" rand_chacha = "0.3.1"
reqwest = "0.11.4" reqwest = "0.11.4"

View File

@ -235,10 +235,7 @@ fn main () -> anyhow::Result <()> {
let client_id = opt.client_id.unwrap_or_else (|| "bogus_client".to_string ()); let client_id = opt.client_id.unwrap_or_else (|| "bogus_client".to_string ());
let quinn::NewConnection { let connection = protocol::p2_connect_to_p3 (&endpoint, relay_addr, &client_id).await
connection,
..
} = protocol::p2_connect_to_p3 (&endpoint, relay_addr, &client_id).await
.context ("P2 can't connect to P3")?; .context ("P2 can't connect to P3")?;
Ok::<_, anyhow::Error> (connection) Ok::<_, anyhow::Error> (connection)