♻️ refactor: server proxy is now using the protocol module
parent
2d57bb9618
commit
1634f7a00d
|
@ -53,7 +53,7 @@ async fn handle_bi_stream (
|
|||
protocol::P3ToP4Stream::NewPtthConnection {
|
||||
client_id,
|
||||
..
|
||||
} => handle_new_ptth_connection (relay_send, relay_recv, local_tcp_port).await?,
|
||||
} => handle_new_ptth_connection (relay_send, relay_recv, local_tcp_port, client_id).await?,
|
||||
}
|
||||
|
||||
Ok (())
|
||||
|
@ -63,31 +63,17 @@ async fn handle_new_ptth_connection (
|
|||
mut relay_send: quinn::SendStream,
|
||||
mut relay_recv: quinn::RecvStream,
|
||||
local_tcp_port: u16,
|
||||
_client_id: u8,
|
||||
) -> anyhow::Result <()>
|
||||
{
|
||||
// TODO: Authorize P2 to connect to us
|
||||
// TODO: Check authorization for P2 --> P4
|
||||
|
||||
let resp_buf = [
|
||||
Command::OKAY.0,
|
||||
Command::CONNECT_P2_TO_P4_STEP_2.0,
|
||||
0,
|
||||
0,
|
||||
];
|
||||
relay_send.write_all (&resp_buf).await?;
|
||||
protocol::p4_authorize_p2_connection (&mut relay_send).await?;
|
||||
protocol::p4_expect_p5_request (&mut relay_recv).await?;
|
||||
|
||||
let mut req_buf = [0, 0, 0, 0];
|
||||
relay_recv.read_exact (&mut req_buf).await?;
|
||||
assert_eq! (req_buf [0], Command::CONNECT_P2_TO_P5.0);
|
||||
// TODO: Check authorization for P1 --> P5
|
||||
|
||||
// TODO: Authorize P2 to connect to P5
|
||||
|
||||
let resp_buf = [
|
||||
Command::OKAY.0,
|
||||
Command::CONNECT_P2_TO_P5.0,
|
||||
0,
|
||||
0,
|
||||
];
|
||||
relay_send.write_all (&resp_buf).await?;
|
||||
protocol::p4_authorize_p1_connection (&mut relay_send).await?;
|
||||
|
||||
debug! ("Started PTTH connection");
|
||||
|
||||
|
|
|
@ -74,7 +74,7 @@ impl ConnectEvents {
|
|||
connects - disconnects
|
||||
}
|
||||
|
||||
fn active (&self) -> u64 {
|
||||
fn _active (&self) -> u64 {
|
||||
let connects = self.connects.load (Ordering::Relaxed);
|
||||
let disconnects = self.disconnects.load (Ordering::Relaxed);
|
||||
connects - disconnects
|
||||
|
|
|
@ -18,29 +18,6 @@ impl Command {
|
|||
pub const OKAY: Command = Command (20);
|
||||
}
|
||||
|
||||
pub enum P3ToP4Stream {
|
||||
NewPtthConnection {
|
||||
client_id: u8,
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn p4_accept_p3_stream (
|
||||
recv: &mut RecvStream,
|
||||
) -> Result <P3ToP4Stream>
|
||||
{
|
||||
let mut req_buf = [0, 0, 0, 0];
|
||||
recv.read_exact (&mut req_buf).await?;
|
||||
|
||||
let cmd_type = req_buf [0];
|
||||
|
||||
Ok (match Command (cmd_type) {
|
||||
Command::CONNECT_P2_TO_P4_STEP_2 => P3ToP4Stream::NewPtthConnection {
|
||||
client_id: req_buf [1],
|
||||
},
|
||||
_ => bail! ("Invalid command type while P2 was accepting a new bi stream from P3"),
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn p2_connect_to_p3 (
|
||||
endpoint: &quinn::Endpoint,
|
||||
relay_addr: &std::net::SocketAddr,
|
||||
|
@ -105,13 +82,80 @@ pub async fn p4_connect_to_p3 (
|
|||
Ok (new_conn)
|
||||
}
|
||||
|
||||
pub enum P3ToP4Stream {
|
||||
NewPtthConnection {
|
||||
client_id: u8,
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn p4_accept_p3_stream (
|
||||
recv: &mut RecvStream,
|
||||
) -> Result <P3ToP4Stream>
|
||||
{
|
||||
let mut buf = [0, 0, 0, 0];
|
||||
recv.read_exact (&mut buf).await?;
|
||||
|
||||
let cmd_type = buf [0];
|
||||
|
||||
Ok (match Command (cmd_type) {
|
||||
Command::CONNECT_P2_TO_P4_STEP_2 => P3ToP4Stream::NewPtthConnection {
|
||||
client_id: buf [1],
|
||||
},
|
||||
_ => bail! ("Invalid command type while P2 was accepting a new bi stream from P3"),
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn p4_authorize_p2_connection (
|
||||
send: &mut SendStream,
|
||||
) -> Result <()>
|
||||
{
|
||||
let buf = [
|
||||
Command::OKAY.0,
|
||||
Command::CONNECT_P2_TO_P4_STEP_2.0,
|
||||
0,
|
||||
0,
|
||||
];
|
||||
send.write_all (&buf).await?;
|
||||
|
||||
Ok (())
|
||||
}
|
||||
|
||||
pub async fn p4_authorize_p1_connection (
|
||||
send: &mut SendStream,
|
||||
) -> Result <()>
|
||||
{
|
||||
let buf = [
|
||||
Command::OKAY.0,
|
||||
Command::CONNECT_P2_TO_P5.0,
|
||||
0,
|
||||
0,
|
||||
];
|
||||
send.write_all (&buf).await?;
|
||||
|
||||
Ok (())
|
||||
}
|
||||
|
||||
pub async fn p4_expect_p5_request (
|
||||
recv: &mut RecvStream,
|
||||
) -> Result <()>
|
||||
{
|
||||
let mut buf = [0, 0, 0, 0];
|
||||
recv.read_exact (&mut buf).await?;
|
||||
let cmd_type = Command (buf [0]);
|
||||
if cmd_type != Command::CONNECT_P2_TO_P5 {
|
||||
bail! ("P4 expected CONNECT_P2_TO_P5 but P2 sent something different");
|
||||
}
|
||||
|
||||
Ok (())
|
||||
}
|
||||
|
||||
async fn expect_exact_response (
|
||||
recv: &mut RecvStream, expected: [u8; 4]
|
||||
) -> Result <()>
|
||||
{
|
||||
let mut resp_buf = [0, 0, 0, 0];
|
||||
recv.read_exact (&mut resp_buf).await?;
|
||||
if resp_buf != expected {
|
||||
let mut buf = [0, 0, 0, 0];
|
||||
recv.read_exact (&mut buf).await?;
|
||||
if buf != expected {
|
||||
bail! ("Didn't receive exact response");
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue