2021-07-17 23:58:11 +00:00
|
|
|
use structopt::StructOpt;
|
2021-10-10 16:56:12 +00:00
|
|
|
use tokio::net::{
|
|
|
|
TcpListener,
|
|
|
|
TcpStream,
|
|
|
|
};
|
2021-07-17 02:39:08 +00:00
|
|
|
|
2021-07-17 01:10:30 +00:00
|
|
|
use quic_demo::prelude::*;
|
2021-07-18 20:32:49 +00:00
|
|
|
use protocol::PeerId;
|
2021-07-17 01:10:30 +00:00
|
|
|
|
2021-07-17 23:58:11 +00:00
|
|
|
#[derive (Debug, StructOpt)]
|
|
|
|
struct Opt {
|
|
|
|
#[structopt (long)]
|
|
|
|
relay_addr: Option <String>,
|
|
|
|
#[structopt (long)]
|
2021-07-18 22:22:48 +00:00
|
|
|
client_id: Option <PeerId>,
|
2021-07-17 23:58:11 +00:00
|
|
|
#[structopt (long)]
|
2021-07-18 20:32:49 +00:00
|
|
|
server_id: Option <PeerId>,
|
2021-07-18 22:55:32 +00:00
|
|
|
#[structopt (long)]
|
2021-07-19 00:01:46 +00:00
|
|
|
client_tcp_port: Option <u16>,
|
|
|
|
#[structopt (long)]
|
2021-07-18 22:55:32 +00:00
|
|
|
server_tcp_port: Option <u16>,
|
2021-07-17 23:58:11 +00:00
|
|
|
}
|
|
|
|
|
2021-07-17 01:10:30 +00:00
|
|
|
#[tokio::main]
|
|
|
|
async fn main () -> anyhow::Result <()> {
|
|
|
|
tracing_subscriber::fmt::init ();
|
|
|
|
|
2021-07-17 23:58:11 +00:00
|
|
|
let opt = Opt::from_args ();
|
2021-10-10 16:36:06 +00:00
|
|
|
let conf = opt.into_config ().await?;
|
2021-07-17 23:58:11 +00:00
|
|
|
|
2021-10-10 16:48:25 +00:00
|
|
|
let client = P2Client::connect (conf)?;
|
|
|
|
client.run ().await?;
|
2021-07-17 01:10:30 +00:00
|
|
|
|
2021-10-10 16:48:25 +00:00
|
|
|
Ok (())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct P2Client {
|
|
|
|
endpoint: quinn::Endpoint,
|
|
|
|
conf: Arc <Config>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl P2Client {
|
|
|
|
pub fn connect (conf: Config) -> anyhow::Result <Self> {
|
|
|
|
let endpoint = make_client_endpoint ("0.0.0.0:0".parse ()?, &[&conf.relay_cert])?;
|
|
|
|
let conf = Arc::new (conf);
|
|
|
|
|
|
|
|
Ok (Self {
|
|
|
|
endpoint,
|
|
|
|
conf,
|
|
|
|
})
|
|
|
|
}
|
2021-07-19 00:01:46 +00:00
|
|
|
|
2021-10-10 16:48:25 +00:00
|
|
|
pub async fn run (&self) -> anyhow::Result <()> {
|
|
|
|
debug! ("P2 client connecting to P3 relay server");
|
|
|
|
|
|
|
|
let conf = Arc::clone (&self.conf);
|
|
|
|
|
|
|
|
let quinn::NewConnection {
|
|
|
|
connection,
|
|
|
|
..
|
|
|
|
} = protocol::p2_connect_to_p3 (&self.endpoint, &conf.relay_addr, &conf.client_id).await?;
|
|
|
|
|
|
|
|
let listener = TcpListener::bind (("127.0.0.1", conf.client_tcp_port)).await?;
|
|
|
|
|
|
|
|
debug! ("Accepting local TCP connections from P1 at {}", conf.client_tcp_port);
|
|
|
|
|
|
|
|
// End of per-port stuff
|
|
|
|
// Beginning of per-connection stuff
|
|
|
|
|
2021-10-10 16:56:12 +00:00
|
|
|
let task_tcp_server = {
|
|
|
|
let connection = connection.clone ();
|
2021-10-10 16:48:25 +00:00
|
|
|
|
2021-10-10 16:56:12 +00:00
|
|
|
tokio::spawn (async move {
|
|
|
|
Self::run_tcp_server (listener, connection, &*conf).await
|
|
|
|
})
|
|
|
|
};
|
2021-09-10 20:36:03 +00:00
|
|
|
|
2021-10-10 16:48:25 +00:00
|
|
|
task_tcp_server.await??;
|
|
|
|
|
|
|
|
Ok (())
|
|
|
|
}
|
2021-10-10 16:56:12 +00:00
|
|
|
|
|
|
|
/// Runs a TCP listen to forward a single TCP port
|
|
|
|
|
|
|
|
async fn run_tcp_server (
|
|
|
|
listener: TcpListener,
|
|
|
|
connection: quinn::Connection,
|
|
|
|
conf: &Config,
|
|
|
|
) -> anyhow::Result <()> {
|
|
|
|
let running = true;
|
|
|
|
while running {
|
2021-10-10 16:58:12 +00:00
|
|
|
let (tcp_stream, _) = listener.accept ().await?;
|
2021-10-10 16:56:12 +00:00
|
|
|
let connection = connection.clone ();
|
|
|
|
let server_id = conf.server_id.clone ();
|
|
|
|
let server_tcp_port = conf.server_tcp_port;
|
|
|
|
|
|
|
|
tokio::spawn (async move {
|
2021-10-10 16:58:12 +00:00
|
|
|
Self::run_tcp_stream (&connection, tcp_stream, &server_id, server_tcp_port).await?;
|
2021-10-10 16:56:12 +00:00
|
|
|
|
|
|
|
Ok::<_, anyhow::Error> (())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok (())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Forwards a single TCP stream
|
|
|
|
|
|
|
|
async fn run_tcp_stream (
|
2021-10-10 16:58:12 +00:00
|
|
|
connection: &quinn::Connection,
|
|
|
|
tcp_stream: TcpStream,
|
2021-10-10 16:56:12 +00:00
|
|
|
server_id: &str,
|
|
|
|
server_tcp_port: u16,
|
|
|
|
) -> anyhow::Result <()> {
|
2021-10-10 16:58:12 +00:00
|
|
|
let (local_recv, local_send) = tcp_stream.into_split ();
|
2021-10-10 16:56:12 +00:00
|
|
|
|
|
|
|
debug! ("Starting PTTH connection");
|
|
|
|
|
2021-10-10 16:58:12 +00:00
|
|
|
let (relay_send, relay_recv) = protocol::p2_connect_to_p5 (connection, server_id, server_tcp_port).await?;
|
2021-10-10 16:56:12 +00:00
|
|
|
|
|
|
|
trace! ("Relaying bytes...");
|
|
|
|
|
|
|
|
let ptth_conn = quic_demo::connection::NewConnection {
|
|
|
|
local_send,
|
|
|
|
local_recv,
|
|
|
|
relay_send,
|
|
|
|
relay_recv,
|
|
|
|
}.build ();
|
|
|
|
|
|
|
|
ptth_conn.wait_for_close ().await?;
|
|
|
|
|
|
|
|
debug! ("Ended PTTH connection");
|
|
|
|
|
|
|
|
Ok (())
|
|
|
|
}
|
2021-07-17 20:02:16 +00:00
|
|
|
}
|
2021-10-10 16:33:14 +00:00
|
|
|
|
|
|
|
/// A filled-out config for constructing a P2 client
|
|
|
|
#[derive (Clone)]
|
|
|
|
pub struct Config {
|
|
|
|
client_tcp_port: u16,
|
|
|
|
server_tcp_port: u16,
|
|
|
|
client_id: String,
|
|
|
|
server_id: String,
|
|
|
|
relay_addr: SocketAddr,
|
|
|
|
relay_cert: Vec <u8>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Opt {
|
|
|
|
pub async fn into_config (self) -> anyhow::Result <Config> {
|
|
|
|
let client_tcp_port = self.client_tcp_port.unwrap_or (30381);
|
|
|
|
let server_tcp_port = self.server_tcp_port.unwrap_or (30382);
|
|
|
|
let client_id = self.client_id.unwrap_or_else (|| "bogus_client".to_string ());
|
|
|
|
let server_id = self.server_id.unwrap_or_else (|| "bogus_server".to_string ());
|
|
|
|
let relay_addr = self.relay_addr.unwrap_or_else (|| String::from ("127.0.0.1:30380")).parse ()?;
|
|
|
|
|
|
|
|
// Begin I/O
|
|
|
|
|
|
|
|
let relay_cert = tokio::fs::read ("ptth_quic_output/quic_server.crt").await?;
|
|
|
|
|
|
|
|
Ok (Config {
|
|
|
|
client_tcp_port,
|
|
|
|
server_tcp_port,
|
|
|
|
client_id,
|
|
|
|
server_id,
|
|
|
|
relay_addr,
|
|
|
|
relay_cert,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|