🚧 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,17 +27,24 @@ 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?;
debug! ("Waiting for local TCP client to connect to us");
let listener = TcpListener::bind ("127.0.0.1:30381").await?;
let (tcp_socket, _) = listener.accept ().await?;
let (mut local_recv, mut local_send) = tcp_socket.into_split ();
debug! ("Connecting to end server");
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 { if false {
debug! ("Waiting for local TCP client to connect to us");
let listener = TcpListener::bind ("127.0.0.1:30381").await?;
let (tcp_socket, _) = listener.accept ().await?;
let (mut local_recv, mut local_send) = tcp_socket.into_split ();
debug! ("Connecting to end server");
let (mut relay_send, mut relay_recv) = connection.open_bi ().await?;
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,50 +26,48 @@ 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"))??;
debug! ("Connecting to TCP end server");
let stream = TcpStream::connect ("127.0.0.1:30382").await?;
let (mut local_recv, mut local_send) = stream.into_split ();
debug! ("Relaying bytes...");
let uplink_task = tokio::spawn (async move {
// Uplink - local client to relay server
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 buf = vec! [0u8; 65_536];
loop {
let bytes_read = local_recv.read (&mut buf).await?;
let buf_slice = &buf [0..bytes_read];
relay_send.write_all (buf_slice).await?;
}
debug! ("Connecting to TCP end server"); debug! ("Uplink closed");
let stream = TcpStream::connect ("127.0.0.1:30382").await?; Ok::<_, anyhow::Error> (())
let (mut local_recv, mut local_send) = stream.into_split (); });
let downlink_task = tokio::spawn (async move {
// Downlink - Relay server to local client
debug! ("Relaying bytes..."); let mut buf = vec! [0u8; 65_536];
while let Some (bytes_read) = relay_recv.read (&mut buf).await? {
let buf_slice = &buf [0..bytes_read];
local_send.write_all (buf_slice).await?;
}
let uplink_task = tokio::spawn (async move { debug! ("Downlink closed");
// Uplink - local client to relay server
let mut buf = vec! [0u8; 65_536];
loop {
let bytes_read = local_recv.read (&mut buf).await?;
let buf_slice = &buf [0..bytes_read];
relay_send.write_all (buf_slice).await?;
}
debug! ("Uplink closed");
Ok::<_, anyhow::Error> (())
});
let downlink_task = tokio::spawn (async move { Ok::<_, anyhow::Error> (())
// Downlink - Relay server to local client });
let mut buf = vec! [0u8; 65_536]; uplink_task.await??;
while let Some (bytes_read) = relay_recv.read (&mut buf).await? { downlink_task.await??;
let buf_slice = &buf [0..bytes_read];
local_send.write_all (buf_slice).await?;
}
debug! ("Downlink closed");
Ok::<_, anyhow::Error> (())
});
uplink_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 => {
let (mut send, mut recv) = bi_stream?; while let Some (bi_stream) = bi_streams.next ().await {
let (mut send, mut recv) = bi_stream?;
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> (())
});
}
// Each new 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::{