🚧 P3 can now tell apart P2 and P4 peers

main
_ 2021-07-17 07:11:34 +00:00
parent 1024c15fbb
commit 7406587d30
4 changed files with 111 additions and 55 deletions

View File

@ -27,7 +27,6 @@ async fn main () -> anyhow::Result <()> {
let mut resp_buf = [0u8, 0, 0, 0]; let mut resp_buf = [0u8, 0, 0, 0];
recv.read_exact (&mut resp_buf).await?; recv.read_exact (&mut resp_buf).await?;
if false {
debug! ("Waiting for local TCP client to connect to us"); debug! ("Waiting for local TCP client to connect to us");
let listener = TcpListener::bind ("127.0.0.1:30381").await?; let listener = TcpListener::bind ("127.0.0.1:30381").await?;
@ -36,8 +35,16 @@ async fn main () -> anyhow::Result <()> {
debug! ("Connecting to end server"); debug! ("Connecting to end server");
let (mut relay_send, mut relay_recv) = connection.open_bi ().await?; let (mut send, mut recv) = connection.open_bi ().await?;
let req_buf = [1, 43, 0, 0, 1, 0, 0, 0];
send.write_all (&req_buf).await?;
let mut resp_buf = [0; 8];
recv.read_exact (&mut resp_buf).await?;
/*
if false {
debug! ("Relaying bytes..."); debug! ("Relaying bytes...");
let uplink_task = tokio::spawn (async move { let uplink_task = tokio::spawn (async move {
@ -72,6 +79,6 @@ async fn main () -> anyhow::Result <()> {
uplink_task.await??; uplink_task.await??;
downlink_task.await??; downlink_task.await??;
} }
*/
Ok (()) Ok (())
} }

View File

@ -26,7 +26,6 @@ async fn main () -> anyhow::Result <()> {
let mut resp_buf = [0u8, 0, 0, 0]; let mut resp_buf = [0u8, 0, 0, 0];
recv.read_exact (&mut resp_buf).await?; recv.read_exact (&mut resp_buf).await?;
if false {
debug! ("Waiting for relay server to forward a bi stream"); debug! ("Waiting for relay server to forward a bi stream");
let (mut relay_send, mut relay_recv) = bi_streams.next ().await.ok_or_else (|| anyhow::anyhow! ("Relay server didn't open a bi stream"))??; let (mut relay_send, mut relay_recv) = bi_streams.next ().await.ok_or_else (|| anyhow::anyhow! ("Relay server didn't open a bi stream"))??;
@ -69,7 +68,6 @@ async fn main () -> anyhow::Result <()> {
uplink_task.await??; uplink_task.await??;
downlink_task.await??; downlink_task.await??;
}
Ok (()) Ok (())
} }

View File

@ -44,15 +44,52 @@ async fn main () -> anyhow::Result <()> {
send.write_all (&resp_buf).await?; send.write_all (&resp_buf).await?;
match peer_type { match peer_type {
2 => while let Some (bi_stream) = bi_streams.next ().await { 2 => {
while let Some (bi_stream) = bi_streams.next ().await {
let (mut send, mut recv) = bi_stream?; let (mut send, mut recv) = bi_stream?;
// Each new tokio::spawn (async move {
let mut req_buf = [0u8; 4];
recv.read_exact (&mut req_buf).await?;
let cmd_type = req_buf [0];
match cmd_type {
1 => {
let server_id = req_buf [1];
debug! ("P2 {} wants to connect to P4 {}", peer_id, server_id);
},
_ => bail! ("Unknown command type from P2"),
}
Ok::<_, anyhow::Error> (())
});
}
debug! ("P2 {} disconnected", peer_id);
},
4 => {
let (tx, mut rx) = mpsc::channel (2);
let p4_state = P4State {
req_channel: tx,
};
{
let mut relay_state = relay_state.lock ().await;
relay_state.p4_server_proxies.insert (peer_id, p4_state);
}
while let Some (req) = rx.recv ().await {
debug! ("P4 {} got a request", peer_id);
}
debug! ("P4 {} disconnected", peer_id);
}, },
4 => (),
_ => bail! ("Unknown QUIC client type"), _ => bail! ("Unknown QUIC client type"),
} }
debug! ("Peer {} disconnected", peer_id);
Ok::<_, anyhow::Error> (()) Ok::<_, anyhow::Error> (())
}); });
} }
@ -107,13 +144,23 @@ async fn main () -> anyhow::Result <()> {
#[derive (Default)] #[derive (Default)]
struct RelayState { struct RelayState {
p4_server_proxies: HashMap <u8, P4State>,
}
struct P4State {
req_channel: mpsc::Sender <RequestP2ToP4>,
} }
impl RelayState { impl RelayState {
} }
enum RequestP2ToP4 {
Connect {
},
}
struct PtthNewConnection { struct PtthNewConnection {
client_send: quinn::SendStream, client_send: quinn::SendStream,
client_recv: quinn::RecvStream, client_recv: quinn::RecvStream,

View File

@ -1,4 +1,5 @@
pub use std::{ pub use std::{
collections::*,
sync::Arc, sync::Arc,
time::Duration, time::Duration,
}; };
@ -10,7 +11,10 @@ pub use tokio::{
AsyncReadExt, AsyncReadExt,
AsyncWriteExt, AsyncWriteExt,
}, },
sync::Mutex, sync::{
Mutex,
mpsc,
},
task::JoinHandle, task::JoinHandle,
}; };
pub use tracing::{ pub use tracing::{