client now chooses the TCP port for the server to forward.

main
_ 2021-07-18 22:55:32 +00:00
parent 56a3e6299b
commit 12fdd4c842
3 changed files with 25 additions and 16 deletions

View File

@ -9,11 +9,13 @@ struct Opt {
#[structopt (long)]
relay_addr: Option <String>,
#[structopt (long)]
local_tcp_port: Option <u16>,
#[structopt (long)]
client_id: Option <PeerId>,
#[structopt (long)]
client_tcp_port: Option <u16>,
#[structopt (long)]
server_id: Option <PeerId>,
#[structopt (long)]
server_tcp_port: Option <u16>,
}
#[tokio::main]
@ -21,7 +23,8 @@ async fn main () -> anyhow::Result <()> {
tracing_subscriber::fmt::init ();
let opt = Opt::from_args ();
let local_tcp_port = opt.local_tcp_port.unwrap_or (30381);
let client_tcp_port = opt.client_tcp_port.unwrap_or (30381);
let server_tcp_port = opt.server_tcp_port.unwrap_or (30382);
let server_cert = tokio::fs::read ("quic_server.crt").await?;
let relay_addr = opt.relay_addr.unwrap_or_else (|| String::from ("127.0.0.1:30380")).parse ()?;
@ -38,7 +41,7 @@ async fn main () -> anyhow::Result <()> {
let server_id = opt.server_id.unwrap_or_else (|| "bogus_server".to_string ());
let listener = TcpListener::bind (("127.0.0.1", local_tcp_port)).await?;
let listener = TcpListener::bind (("127.0.0.1", client_tcp_port)).await?;
trace! ("Accepting local TCP connections from P1");
@ -52,7 +55,7 @@ async fn main () -> anyhow::Result <()> {
debug! ("Starting PTTH connection");
let (relay_send, relay_recv) = protocol::p2_connect_to_p5 (&connection, &server_id).await?;
let (relay_send, relay_recv) = protocol::p2_connect_to_p5 (&connection, &server_id, server_tcp_port).await?;
trace! ("Relaying bytes...");

View File

@ -9,8 +9,6 @@ struct Opt {
#[structopt (long)]
relay_addr: Option <String>,
#[structopt (long)]
local_tcp_port: Option <u16>,
#[structopt (long)]
server_id: Option <PeerId>,
}
@ -19,7 +17,6 @@ async fn main () -> anyhow::Result <()> {
tracing_subscriber::fmt::init ();
let opt = Opt::from_args ();
let local_tcp_port = opt.local_tcp_port.unwrap_or (30382);
let server_cert = tokio::fs::read ("quic_server.crt").await?;
let relay_addr = opt.relay_addr.unwrap_or_else (|| String::from ("127.0.0.1:30380")).parse ()?;
@ -39,21 +36,20 @@ async fn main () -> anyhow::Result <()> {
loop {
let (relay_send, relay_recv) = bi_streams.next ().await.ok_or_else (|| anyhow::anyhow! ("Relay server didn't open a bi stream"))??;
tokio::spawn (handle_bi_stream (relay_send, relay_recv, local_tcp_port));
tokio::spawn (handle_bi_stream (relay_send, relay_recv));
}
}
async fn handle_bi_stream (
relay_send: quinn::SendStream,
mut relay_recv: quinn::RecvStream,
local_tcp_port: u16,
) -> anyhow::Result <()>
{
match protocol::p4_accept_p3_stream (&mut relay_recv).await? {
protocol::P3ToP4Stream::NewPtthConnection {
client_id,
..
} => handle_new_ptth_connection (relay_send, relay_recv, local_tcp_port, client_id).await?,
} => handle_new_ptth_connection (relay_send, relay_recv, client_id).await?,
}
Ok (())
@ -62,14 +58,13 @@ async fn handle_bi_stream (
async fn handle_new_ptth_connection (
mut relay_send: quinn::SendStream,
mut relay_recv: quinn::RecvStream,
local_tcp_port: u16,
_client_id: String,
) -> anyhow::Result <()>
{
// TODO: Check authorization for P2 --> P4
protocol::p4_authorize_p2_connection (&mut relay_send).await?;
protocol::p4_expect_p5_request (&mut relay_recv).await?;
let p4_to_p5_req = protocol::p4_expect_p5_request (&mut relay_recv).await?;
// TODO: Check authorization for P1 --> P5
@ -77,7 +72,7 @@ async fn handle_new_ptth_connection (
debug! ("Started PTTH connection");
let stream = TcpStream::connect (("127.0.0.1", local_tcp_port)).await?;
let stream = TcpStream::connect (("127.0.0.1", p4_to_p5_req.port)).await?;
let (local_recv, local_send) = stream.into_split ();
trace! ("Relaying bytes...");

View File

@ -51,6 +51,7 @@ pub async fn p2_connect_to_p3 (
pub async fn p2_connect_to_p5 (
connection: &quinn::Connection,
server_id: &str,
server_port: u16,
) -> Result <(SendStream, RecvStream)>
{
let (mut send, mut recv) = connection.open_bi ().await?;
@ -70,6 +71,7 @@ pub async fn p2_connect_to_p5 (
let cmd_type = Command::CONNECT_P2_TO_P5.0;
send.write_all (&[cmd_type, 0, 0, 0]).await?;
send.write_all (&server_port.to_le_bytes ()).await?;
expect_exact_response (&mut recv, [Command::OKAY.0, cmd_type, 0, 0]).await
.context ("P2 didn't get OK response when asking P4 to connect P2 to P5")?;
@ -272,9 +274,13 @@ pub async fn p4_authorize_p1_connection (
Ok (())
}
pub struct P2ConnectToP5Request {
pub port: u16,
}
pub async fn p4_expect_p5_request (
recv: &mut RecvStream,
) -> Result <()>
) -> Result <P2ConnectToP5Request>
{
let mut buf = [0, 0, 0, 0];
recv.read_exact (&mut buf).await?;
@ -282,8 +288,13 @@ pub async fn p4_expect_p5_request (
if cmd_type != Command::CONNECT_P2_TO_P5 {
bail! ("P4 expected CONNECT_P2_TO_P5 but P2 sent something different");
}
let mut port_buf = [0, 0];
recv.read_exact (&mut port_buf).await;
let port = u16::from_le_bytes (port_buf);
Ok (())
Ok (P2ConnectToP5Request {
port,
})
}
async fn expect_exact_response (