♻️ refactor

main
_ 2021-07-18 20:11:11 +00:00
parent 75a4515a70
commit 927ddecf97
2 changed files with 76 additions and 31 deletions

View File

@ -170,46 +170,34 @@ async fn handle_quic_connection (
let (mut send, mut recv) = conn.bi_streams.next ().await.ok_or_else (|| anyhow::anyhow! ("QUIC client didn't identify itself"))??; let (mut send, mut recv) = conn.bi_streams.next ().await.ok_or_else (|| anyhow::anyhow! ("QUIC client didn't identify itself"))??;
let mut req_buf = [0, 0, 0, 0]; let peer = protocol::p3_accept_peer (&mut recv).await?;
recv.read_exact (&mut req_buf).await?;
let peer_type = req_buf [0]; match peer {
let peer_id = req_buf [1]; protocol::P3Peer::P2ClientProxy (peer) => {
// TODO: Check authorization for P2 peers
match peer_type {
4 => debug! ("Server-side proxy (P4) connected, ID {}", peer_id), protocol::p3_authorize_p2_peer (&mut send).await?;
2 => debug! ("Client-side proxy (P2) connected, ID {}", peer_id), handle_p2_connection (relay_state, conn, peer).await?;
_ => bail! ("Unknown QUIC client type"), },
protocol::P3Peer::P4ServerProxy (peer) => {
// TODO: Check authorization for P2 peers
protocol::p3_authorize_p4_peer (&mut send).await?;
handle_p4_connection (relay_state, conn, peer).await?;
},
} }
let resp_buf = [
Command::OKAY.0,
peer_type,
0,
0,
];
send.write_all (&resp_buf).await?;
match peer_type {
2 => {
handle_p2_connection (relay_state, peer_id, conn).await?;
},
4 => {
handle_p4_connection (relay_state, peer_id, conn).await?;
},
_ => bail! ("Unknown QUIC client type"),
}
debug! ("Peer {} disconnected", peer_id);
Ok::<_, anyhow::Error> (()) Ok::<_, anyhow::Error> (())
} }
async fn handle_p2_connection ( async fn handle_p2_connection (
relay_state: Arc <RelayState>, relay_state: Arc <RelayState>,
client_id: u8,
conn: quinn::NewConnection, conn: quinn::NewConnection,
peer: protocol::P2ClientProxy,
) -> anyhow::Result <()> ) -> anyhow::Result <()>
{ {
let client_id = peer.id;
let quinn::NewConnection { let quinn::NewConnection {
mut bi_streams, mut bi_streams,
.. ..
@ -283,10 +271,11 @@ async fn handle_request_p2_to_p4 (
async fn handle_p4_connection ( async fn handle_p4_connection (
relay_state: Arc <RelayState>, relay_state: Arc <RelayState>,
server_id: u8,
conn: quinn::NewConnection, conn: quinn::NewConnection,
peer: protocol::P4ServerProxy,
) -> anyhow::Result <()> ) -> anyhow::Result <()>
{ {
let server_id = peer.id;
let quinn::NewConnection { let quinn::NewConnection {
connection, connection,
.. ..

View File

@ -64,6 +64,62 @@ pub async fn p2_connect_to_p5 (
Ok ((send, recv)) Ok ((send, recv))
} }
pub enum P3Peer {
P2ClientProxy (P2ClientProxy),
P4ServerProxy (P4ServerProxy),
}
pub struct P2ClientProxy {
pub id: u8,
}
pub struct P4ServerProxy {
pub id: u8,
}
pub async fn p3_accept_peer (
recv: &mut RecvStream,
) -> Result <P3Peer>
{
let mut buf = [0, 0, 0, 0];
recv.read_exact (&mut buf).await?;
let command = Command (buf [0]);
let id = buf [1];
Ok (match command {
Command::CONNECT_P2_TO_P3 => {
debug! ("Client-side proxy (P2) connected, ID {}", id);
P3Peer::P2ClientProxy (P2ClientProxy {
id,
})
},
Command::CONNECT_P4_TO_P3 => {
debug! ("Server-side proxy (P4) connected, ID {}", id);
P3Peer::P4ServerProxy (P4ServerProxy {
id,
})
},
_ => bail! ("Unknown QUIC client type"),
})
}
pub async fn p3_authorize_p2_peer (
send: &mut SendStream,
) -> Result <()>
{
send.write_all (&[Command::OKAY.0, Command::CONNECT_P2_TO_P3.0, 0, 0]).await?;
Ok (())
}
pub async fn p3_authorize_p4_peer (
send: &mut SendStream,
) -> Result <()>
{
send.write_all (&[Command::OKAY.0, Command::CONNECT_P4_TO_P3.0, 0, 0]).await?;
Ok (())
}
pub async fn p3_connect_p2_to_p4 ( pub async fn p3_connect_p2_to_p4 (
connection: &quinn::Connection, connection: &quinn::Connection,
client_id: u8, client_id: u8,
@ -179,7 +235,7 @@ async fn expect_exact_response (
let mut buf = [0, 0, 0, 0]; let mut buf = [0, 0, 0, 0];
recv.read_exact (&mut buf).await?; recv.read_exact (&mut buf).await?;
if buf != expected { if buf != expected {
bail! ("Didn't receive exact response"); bail! ("Didn't receive exact response, got {:?}", buf);
} }
Ok (()) Ok (())