change client ID from a `u8` to a UTF-8 string up to 128 bytes long

main
_ 2021-07-18 21:18:49 +00:00
parent 4728e7e35c
commit 5bfb99f383
4 changed files with 83 additions and 22 deletions

View File

@ -11,7 +11,7 @@ struct Opt {
#[structopt (long)]
local_tcp_port: Option <u16>,
#[structopt (long)]
client_id: Option <PeerId>,
client_id: Option <String>,
#[structopt (long)]
server_id: Option <PeerId>,
}
@ -29,12 +29,12 @@ async fn main () -> anyhow::Result <()> {
trace! ("Connecting to relay server");
let client_id = opt.client_id.unwrap_or (42);
let client_id = opt.client_id.unwrap_or_else (|| "bogus_client".to_string ());
let quinn::NewConnection {
connection,
..
} = protocol::p2_connect_to_p3 (&endpoint, &relay_addr, client_id).await?;
} = protocol::p2_connect_to_p3 (&endpoint, &relay_addr, &client_id).await?;
let server_id = opt.server_id.unwrap_or (43);

View File

@ -63,7 +63,7 @@ async fn handle_new_ptth_connection (
mut relay_send: quinn::SendStream,
mut relay_recv: quinn::RecvStream,
local_tcp_port: u16,
_client_id: PeerId,
_client_id: String,
) -> anyhow::Result <()>
{
// TODO: Check authorization for P2 --> P4

View File

@ -93,7 +93,7 @@ impl RelayState {
struct RequestP2ToP4 {
client_send: quinn::SendStream,
client_recv: quinn::RecvStream,
client_id: PeerId,
client_id: String,
}
struct PtthNewConnection {
@ -206,6 +206,7 @@ async fn handle_p2_connection (
while let Some (bi_stream) = bi_streams.next ().await {
let (send, mut recv) = bi_stream?;
let relay_state = Arc::clone (&relay_state);
let client_id = client_id.clone ();
tokio::spawn (async move {
debug! ("Request started for P2");
@ -228,7 +229,7 @@ async fn handle_p2_connection (
async fn handle_request_p2_to_p4 (
relay_state: Arc <RelayState>,
client_id: PeerId,
client_id: String,
server_id: PeerId,
mut client_send: quinn::SendStream,
client_recv: quinn::RecvStream,
@ -289,9 +290,9 @@ async fn handle_p4_connection (
client_id,
} = req;
debug! ("P4 {} got a request from P2 {}", server_id, req.client_id);
debug! ("P4 {} got a request from P2 {}", server_id, client_id);
let (server_send, server_recv) = protocol::p3_connect_p2_to_p4 (&connection, client_id).await?;
let (server_send, server_recv) = protocol::p3_connect_p2_to_p4 (&connection, &client_id).await?;
trace! ("Relaying bytes...");

View File

@ -1,3 +1,7 @@
use std::{
convert::TryFrom,
};
use anyhow::Result;
use quinn::{
SendStream,
@ -7,6 +11,7 @@ use quinn::{
use crate::prelude::*;
pub type PeerId = u8;
const MAX_ID_LENGTH: usize = 128;
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Command (pub u8);
@ -23,14 +28,19 @@ impl Command {
pub async fn p2_connect_to_p3 (
endpoint: &quinn::Endpoint,
relay_addr: &std::net::SocketAddr,
client_id: PeerId,
client_id: &str,
) -> Result <quinn::NewConnection>
{
if client_id.as_bytes ().len () > MAX_ID_LENGTH {
bail! ("Client ID is longer than MAX_ID_LENGTH");
}
let new_conn = endpoint.connect (relay_addr, "localhost")?.await?;
let (mut send, mut recv) = new_conn.connection.open_bi ().await?;
let cmd_type = Command::CONNECT_P2_TO_P3.0;
send.write_all (&[cmd_type, client_id, 0, 0]).await?;
send.write_all (&[cmd_type, 0, 0, 0]).await?;
send_lv_string (&mut send, client_id).await?;
expect_exact_response (&mut recv, [Command::OKAY.0, cmd_type, 0, 0]).await
.context ("P2 didn't get OK response when connecting to P3")?;
@ -72,7 +82,7 @@ pub enum P3Peer {
}
pub struct P2ClientProxy {
pub id: PeerId,
pub id: String,
}
pub struct P4ServerProxy {
@ -87,16 +97,18 @@ pub async fn p3_accept_peer (
recv.read_exact (&mut buf).await?;
let command = Command (buf [0]);
let id = buf [1];
Ok (match command {
Command::CONNECT_P2_TO_P3 => {
let id = recv_lv_string (recv, MAX_ID_LENGTH).await?;
debug! ("Client-side proxy (P2) connected, ID {}", id);
P3Peer::P2ClientProxy (P2ClientProxy {
id,
})
},
Command::CONNECT_P4_TO_P3 => {
let id = buf [1];
debug! ("Server-side proxy (P4) connected, ID {}", id);
P3Peer::P4ServerProxy (P4ServerProxy {
id,
@ -124,20 +136,20 @@ pub async fn p3_authorize_p4_peer (
pub async fn p3_connect_p2_to_p4 (
connection: &quinn::Connection,
client_id: PeerId,
client_id: &str,
) -> Result <(SendStream, RecvStream)>
{
if client_id.as_bytes ().len () > MAX_ID_LENGTH {
bail! ("Client ID is longer than MAX_ID_LENGTH");
}
let (mut send, mut recv) = connection.open_bi ().await?;
let cmd_type = Command::CONNECT_P2_TO_P4_STEP_2.0;
let buf = [
cmd_type,
client_id,
0,
0,
];
let buf = [cmd_type, 0, 0, 0];
send.write_all (&buf).await?;
send_lv_string (&mut send, client_id).await?;
expect_exact_response (&mut recv, [Command::OKAY.0, cmd_type, 0, 0]).await
.context ("P3 didn't get OK response when asking P4 to connect P2 to P4")?;
@ -196,7 +208,7 @@ pub async fn p4_connect_to_p3 (
pub enum P3ToP4Stream {
NewPtthConnection {
client_id: PeerId,
client_id: String,
}
}
@ -210,8 +222,12 @@ pub async fn p4_accept_p3_stream (
let cmd_type = buf [0];
Ok (match Command (cmd_type) {
Command::CONNECT_P2_TO_P4_STEP_2 => P3ToP4Stream::NewPtthConnection {
client_id: buf [1],
Command::CONNECT_P2_TO_P4_STEP_2 => {
let client_id = recv_lv_string (recv, MAX_ID_LENGTH).await?;
P3ToP4Stream::NewPtthConnection {
client_id,
}
},
_ => bail! ("Invalid command type while P4 was accepting a new bi stream from P3"),
})
@ -274,4 +290,48 @@ async fn expect_exact_response (
Ok (())
}
async fn send_lv_u16 (send: &mut SendStream, buf: &[u8]) -> Result <()>
{
if buf.len () >= 65_536 {
bail! ("Buffer is too long to send with u16 length prefix");
}
let len = u16::try_from (buf.len ())?;
send.write_all (&len.to_le_bytes ()).await?;
send.write_all (buf).await?;
Ok (())
}
async fn send_lv_string (send: &mut SendStream, s: &str) -> Result <()>
{
send_lv_u16 (send, s.as_bytes ()).await?;
Ok (())
}
async fn recv_lv_u16 (recv: &mut RecvStream, max_len: usize) -> Result <Vec <u8>>
{
let mut len_buf = [0, 0];
recv.read_exact (&mut len_buf).await?;
let len = u16::from_le_bytes (len_buf);
let len = usize::try_from (len)?;
if len > max_len {
bail! ("Buffer is longer than max_len");
}
// We could skip this allocation but who cares
let mut buf = vec! [0u8; len];
recv.read_exact (&mut buf).await?;
Ok (buf)
}
async fn recv_lv_string (recv: &mut RecvStream, max_len: usize) -> Result <String>
{
let buf = recv_lv_u16 (recv, max_len).await?;
let s = String::from_utf8 (buf)?;
Ok (s)
}