♻️ refactor: move all protocol stuff out of the client proxy
parent
d265aa3cd9
commit
b64042043b
|
@ -2,7 +2,6 @@ use structopt::StructOpt;
|
||||||
use tokio::net::TcpListener;
|
use tokio::net::TcpListener;
|
||||||
|
|
||||||
use quic_demo::prelude::*;
|
use quic_demo::prelude::*;
|
||||||
use protocol::Command;
|
|
||||||
|
|
||||||
#[derive (Debug, StructOpt)]
|
#[derive (Debug, StructOpt)]
|
||||||
struct Opt {
|
struct Opt {
|
||||||
|
@ -29,18 +28,15 @@ async fn main () -> anyhow::Result <()> {
|
||||||
|
|
||||||
trace! ("Connecting to relay server");
|
trace! ("Connecting to relay server");
|
||||||
|
|
||||||
|
let client_id = opt.client_id.unwrap_or (42);
|
||||||
|
|
||||||
let quinn::NewConnection {
|
let quinn::NewConnection {
|
||||||
connection,
|
connection,
|
||||||
..
|
..
|
||||||
} = endpoint.connect (&relay_addr, "localhost")?.await?;
|
} = protocol::p2_connect_to_p3 (&endpoint, &relay_addr, client_id).await?;
|
||||||
|
|
||||||
let (mut send, mut recv) = connection.open_bi ().await?;
|
|
||||||
|
|
||||||
let client_id = opt.client_id.unwrap_or (42);
|
|
||||||
let server_id = opt.server_id.unwrap_or (43);
|
let server_id = opt.server_id.unwrap_or (43);
|
||||||
|
|
||||||
protocol::p2_connect_to_p3 (&mut send, &mut recv, client_id).await?;
|
|
||||||
|
|
||||||
let listener = TcpListener::bind (("127.0.0.1", local_tcp_port)).await?;
|
let listener = TcpListener::bind (("127.0.0.1", local_tcp_port)).await?;
|
||||||
|
|
||||||
trace! ("Accepting local TCP connections from P1");
|
trace! ("Accepting local TCP connections from P1");
|
||||||
|
@ -52,47 +48,9 @@ async fn main () -> anyhow::Result <()> {
|
||||||
tokio::spawn (async move {
|
tokio::spawn (async move {
|
||||||
let (local_recv, local_send) = tcp_socket.into_split ();
|
let (local_recv, local_send) = tcp_socket.into_split ();
|
||||||
|
|
||||||
debug! ("Started PTTH connection");
|
debug! ("Starting PTTH connection");
|
||||||
|
|
||||||
let (mut relay_send, mut relay_recv) = connection.open_bi ().await?;
|
let (relay_send, relay_recv) = protocol::p2_connect_to_p5 (&connection, server_id).await?;
|
||||||
|
|
||||||
// Ask P3 if we can connect to P4
|
|
||||||
|
|
||||||
let req_buf = [
|
|
||||||
Command::CONNECT_P2_TO_P4.0,
|
|
||||||
server_id,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
];
|
|
||||||
relay_send.write_all (&req_buf).await?;
|
|
||||||
|
|
||||||
let mut resp_buf = [0; 4];
|
|
||||||
relay_recv.read_exact (&mut resp_buf).await?;
|
|
||||||
assert_eq! (resp_buf, [
|
|
||||||
Command::OKAY.0,
|
|
||||||
Command::CONNECT_P2_TO_P4.0,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
]);
|
|
||||||
|
|
||||||
// Ask P4 if we can connect to P5
|
|
||||||
|
|
||||||
let req_buf = [
|
|
||||||
Command::CONNECT_P2_TO_P5.0,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
];
|
|
||||||
relay_send.write_all (&req_buf).await?;
|
|
||||||
|
|
||||||
let mut resp_buf = [0; 4];
|
|
||||||
relay_recv.read_exact (&mut resp_buf).await?;
|
|
||||||
assert_eq! (resp_buf, [
|
|
||||||
Command::OKAY.0,
|
|
||||||
Command::CONNECT_P2_TO_P5.0,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
]);
|
|
||||||
|
|
||||||
trace! ("Relaying bytes...");
|
trace! ("Relaying bytes...");
|
||||||
|
|
||||||
|
|
|
@ -18,12 +18,15 @@ impl Command {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn p2_connect_to_p3 (
|
pub async fn p2_connect_to_p3 (
|
||||||
send: &mut SendStream,
|
endpoint: &quinn::Endpoint,
|
||||||
recv: &mut RecvStream,
|
relay_addr: &std::net::SocketAddr,
|
||||||
client_id: u8,
|
client_id: u8,
|
||||||
)
|
) -> anyhow::Result <quinn::NewConnection>
|
||||||
-> anyhow::Result <()>
|
|
||||||
{
|
{
|
||||||
|
let new_conn = endpoint.connect (relay_addr, "localhost")?.await?;
|
||||||
|
|
||||||
|
let (mut send, mut recv) = new_conn.connection.open_bi ().await?;
|
||||||
|
|
||||||
let req_buf = [
|
let req_buf = [
|
||||||
Command::CONNECT_P2_TO_P3.0,
|
Command::CONNECT_P2_TO_P3.0,
|
||||||
client_id,
|
client_id,
|
||||||
|
@ -44,5 +47,59 @@ pub async fn p2_connect_to_p3 (
|
||||||
bail! ("P2 didn't get OK response when connecting to P3");
|
bail! ("P2 didn't get OK response when connecting to P3");
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok (())
|
Ok (new_conn)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn p2_connect_to_p5 (
|
||||||
|
connection: &quinn::Connection,
|
||||||
|
server_id: u8,
|
||||||
|
) -> anyhow::Result <(SendStream, RecvStream)>
|
||||||
|
{
|
||||||
|
let (mut relay_send, mut relay_recv) = connection.open_bi ().await?;
|
||||||
|
|
||||||
|
// Ask P3 if we can connect to P4
|
||||||
|
|
||||||
|
let req_buf = [
|
||||||
|
Command::CONNECT_P2_TO_P4.0,
|
||||||
|
server_id,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
];
|
||||||
|
relay_send.write_all (&req_buf).await?;
|
||||||
|
|
||||||
|
let mut resp_buf = [0; 4];
|
||||||
|
relay_recv.read_exact (&mut resp_buf).await?;
|
||||||
|
let expected = [
|
||||||
|
Command::OKAY.0,
|
||||||
|
Command::CONNECT_P2_TO_P4.0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
];
|
||||||
|
if resp_buf != expected {
|
||||||
|
bail! ("P2 didn't get OK response when asking P3 to connect P2 to P4");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ask P4 if we can connect to P5
|
||||||
|
|
||||||
|
let req_buf = [
|
||||||
|
Command::CONNECT_P2_TO_P5.0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
];
|
||||||
|
relay_send.write_all (&req_buf).await?;
|
||||||
|
|
||||||
|
let mut resp_buf = [0; 4];
|
||||||
|
relay_recv.read_exact (&mut resp_buf).await?;
|
||||||
|
let expected = [
|
||||||
|
Command::OKAY.0,
|
||||||
|
Command::CONNECT_P2_TO_P5.0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
];
|
||||||
|
if resp_buf != expected {
|
||||||
|
bail! ("P2 didn't get OK response when asking P4 to connect P2 to P5");
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok ((relay_send, relay_recv))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue