♻️ refactor: continue extracting end server struct

main
_ 2021-10-10 14:49:02 +00:00
parent ee31d105c9
commit 953254e550
1 changed files with 31 additions and 13 deletions

View File

@ -19,41 +19,59 @@ struct Opt {
pub async fn main (args: &[OsString]) -> anyhow::Result <()> { pub async fn main (args: &[OsString]) -> anyhow::Result <()> {
let opt = Arc::new (Opt::from_iter (args)); let opt = Arc::new (Opt::from_iter (args));
let server_cert: Vec <u8> = match opt.cert_url.as_ref () { let relay_cert: Vec <u8> = match opt.cert_url.as_ref () {
Some (url) => reqwest::get (url).await?.bytes ().await?.into_iter ().collect (), Some (url) => reqwest::get (url).await?.bytes ().await?.into_iter ().collect (),
None => tokio::fs::read ("quic_server.crt").await?, None => tokio::fs::read ("quic_server.crt").await?,
}; };
let relay_addr: SocketAddr = opt.relay_addr.clone ().unwrap_or_else (|| String::from ("127.0.0.1:30380")).parse ()?; let relay_addr: SocketAddr = opt.relay_addr.clone ().unwrap_or_else (|| String::from ("127.0.0.1:30380")).parse ()?;
let end_server = P4EndServer { let id = opt.server_id.clone ().unwrap_or_else (|| "bogus_server".to_string ());
relay_addr: relay_addr,
server_cert: server_cert,
};
let endpoint = make_client_endpoint ("0.0.0.0:0".parse ()?, &[&end_server.server_cert])?; let endpoint = make_client_endpoint ("0.0.0.0:0".parse ()?, &[&relay_cert])?;
trace! ("Connecting to relay server"); trace! ("Connecting to relay server");
let server_id = opt.server_id.clone ().unwrap_or_else (|| "bogus_server".to_string ());
let quinn::NewConnection { let quinn::NewConnection {
mut bi_streams, bi_streams,
.. ..
} = protocol::p4_connect_to_p3 (&endpoint, &end_server.relay_addr, &server_id).await?; } = protocol::p4_connect_to_p3 (&endpoint, &relay_addr, &id).await?;
let mut end_server = P4EndServer {
bi_streams,
id,
relay_addr,
relay_cert,
};
debug! ("Connected to relay server"); debug! ("Connected to relay server");
trace! ("Accepting bi streams from P3"); trace! ("Accepting bi streams from P3");
loop { loop {
let (relay_send, relay_recv) = bi_streams.next ().await.ok_or_else (|| anyhow::anyhow! ("Relay server didn't open a bi stream"))??; let (relay_send, relay_recv) = end_server.bi_streams.next ().await.ok_or_else (|| anyhow::anyhow! ("Relay server didn't open a bi stream"))??;
tokio::spawn (handle_bi_stream (Arc::clone (&opt), relay_send, relay_recv)); tokio::spawn (handle_bi_stream (Arc::clone (&opt), relay_send, relay_recv));
} }
} }
struct P4EndServer { pub struct P4EndServer {
bi_streams: quinn::IncomingBiStreams,
id: String,
relay_addr: SocketAddr, relay_addr: SocketAddr,
server_cert: Vec <u8>, relay_cert: Vec <u8>,
}
impl P4EndServer {
pub fn id (&self) -> &str {
&self.id
}
pub fn relay_addr (&self) -> SocketAddr {
self.relay_addr
}
pub fn relay_cert (&self) -> &[u8] {
&self.relay_cert
}
} }
async fn handle_bi_stream ( async fn handle_bi_stream (