2021-10-02 17:39:03 +00:00
|
|
|
use structopt::StructOpt;
|
2021-10-10 16:18:02 +00:00
|
|
|
use tokio::{
|
|
|
|
net::TcpStream,
|
|
|
|
sync::watch,
|
|
|
|
};
|
2021-10-02 17:39:03 +00:00
|
|
|
|
|
|
|
use crate::prelude::*;
|
|
|
|
use protocol::PeerId;
|
|
|
|
|
2021-10-10 15:19:48 +00:00
|
|
|
/// A partially-filled-out config that structopt can deal with
|
|
|
|
/// Try to turn this into a Config as soon as possible.
|
2021-10-02 17:39:03 +00:00
|
|
|
#[derive (Debug, StructOpt)]
|
|
|
|
struct Opt {
|
|
|
|
#[structopt (long)]
|
|
|
|
relay_addr: Option <String>,
|
|
|
|
#[structopt (long)]
|
|
|
|
server_id: Option <PeerId>,
|
|
|
|
#[structopt (long)]
|
|
|
|
debug_echo: bool,
|
|
|
|
#[structopt (long)]
|
|
|
|
cert_url: Option <String>,
|
2022-10-31 18:50:42 +00:00
|
|
|
#[structopt (long)]
|
|
|
|
use_udp_over_tcp: Option <bool>,
|
2021-10-02 17:39:03 +00:00
|
|
|
}
|
|
|
|
|
2021-10-10 16:18:02 +00:00
|
|
|
pub async fn main (args: &[OsString], shutdown_rx: Option <watch::Receiver <bool>>) -> anyhow::Result <()> {
|
2022-10-09 15:30:57 +00:00
|
|
|
trace! ("executable_end_server::main");
|
2021-10-10 15:19:48 +00:00
|
|
|
let opt = Opt::from_iter (args);
|
|
|
|
let conf = opt.into_config ().await?;
|
2021-10-02 17:39:03 +00:00
|
|
|
|
2022-10-31 18:50:42 +00:00
|
|
|
let end_server = Arc::new (P4EndServer::connect (&conf)?);
|
|
|
|
|
|
|
|
let conf = if conf.use_udp_over_tcp {
|
|
|
|
let udp_sock = UdpSocket::bind (SocketAddrV4::new (Ipv4Addr::UNSPECIFIED, 0)).await?;
|
|
|
|
udp_sock.connect ((Ipv4Addr::LOCALHOST, end_server.endpoint.local_addr ()?.port ())).await?;
|
|
|
|
|
|
|
|
let udp_local_server_port = udp_sock.local_addr ()?.port ();
|
|
|
|
|
|
|
|
let tcp_sock = TcpSocket::new_v4 ()?;
|
|
|
|
let tcp_conn = tcp_sock.connect (conf.relay_addr).await?;
|
|
|
|
|
|
|
|
tokio::spawn (async move {
|
|
|
|
udp_over_tcp::client::main_with_sockets (udp_sock, tcp_conn).await
|
|
|
|
});
|
|
|
|
|
|
|
|
Config {
|
|
|
|
debug_echo: conf.debug_echo,
|
|
|
|
id: conf.id,
|
|
|
|
relay_addr: SocketAddr::V4 (SocketAddrV4::new (Ipv4Addr::LOCALHOST, udp_local_server_port)),
|
|
|
|
relay_cert: conf.relay_cert,
|
|
|
|
use_udp_over_tcp: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
conf
|
|
|
|
};
|
2021-10-10 16:18:02 +00:00
|
|
|
|
|
|
|
let run_task = {
|
|
|
|
let end_server = Arc::clone (&end_server);
|
|
|
|
tokio::spawn (async move {
|
2022-10-31 18:50:42 +00:00
|
|
|
end_server.run (conf).await?;
|
2021-10-10 16:18:02 +00:00
|
|
|
Ok::<_, anyhow::Error> (())
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some (mut shutdown_rx) = shutdown_rx {
|
|
|
|
while ! *shutdown_rx.borrow () {
|
|
|
|
shutdown_rx.changed ().await?;
|
|
|
|
}
|
|
|
|
end_server.shut_down ()?;
|
|
|
|
}
|
|
|
|
|
|
|
|
run_task.await??;
|
|
|
|
|
|
|
|
trace! ("P4 end server shut down gracefully.");
|
2021-10-10 14:36:01 +00:00
|
|
|
|
2021-10-10 15:19:48 +00:00
|
|
|
Ok (())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A filled-out config for constructing an end server
|
|
|
|
#[derive (Clone)]
|
|
|
|
pub struct Config {
|
|
|
|
pub debug_echo: bool,
|
|
|
|
pub id: String,
|
|
|
|
pub relay_addr: SocketAddr,
|
|
|
|
pub relay_cert: Vec <u8>,
|
2022-10-31 18:50:42 +00:00
|
|
|
pub use_udp_over_tcp: bool,
|
2021-10-10 15:19:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Opt {
|
|
|
|
/// Converts self into a Config that the server can use.
|
|
|
|
/// Performs I/O to load the relay cert from disk or from HTTP.
|
|
|
|
/// Fails if arguments can't be parsed or if I/O fails.
|
2021-10-02 17:39:03 +00:00
|
|
|
|
2021-10-10 15:19:48 +00:00
|
|
|
pub async fn into_config (self) -> anyhow::Result <Config> {
|
|
|
|
let id = self.server_id.clone ().unwrap_or_else (|| "bogus_server".to_string ());
|
2021-10-02 17:39:03 +00:00
|
|
|
|
2021-10-10 15:19:48 +00:00
|
|
|
let relay_addr: SocketAddr = self.relay_addr.clone ().unwrap_or_else (|| String::from ("127.0.0.1:30380")).parse ()?;
|
|
|
|
|
|
|
|
// Do I/O after all parsing is done.
|
|
|
|
// We don't want to waste a network request only to come back and error
|
|
|
|
// out on like "127.oooo.1" not parsing into a relay address.
|
|
|
|
|
|
|
|
let relay_cert: Vec <u8> = match self.cert_url.as_ref () {
|
|
|
|
Some (url) => reqwest::get (url).await?.bytes ().await?.into_iter ().collect (),
|
2021-10-10 16:23:21 +00:00
|
|
|
None => tokio::fs::read ("ptth_quic_output/quic_server.crt").await?,
|
2021-10-10 15:19:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok (Config {
|
|
|
|
debug_echo: self.debug_echo,
|
|
|
|
id,
|
|
|
|
relay_addr,
|
|
|
|
relay_cert,
|
2022-10-31 18:50:42 +00:00
|
|
|
use_udp_over_tcp: self.use_udp_over_tcp.unwrap_or (false),
|
2021-10-10 15:19:48 +00:00
|
|
|
})
|
2021-10-02 17:39:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-10 14:49:02 +00:00
|
|
|
pub struct P4EndServer {
|
2021-10-10 16:18:02 +00:00
|
|
|
endpoint: quinn::Endpoint,
|
|
|
|
shutdown_tx: watch::Sender <bool>,
|
|
|
|
shutdown_rx: watch::Receiver <bool>,
|
2021-10-10 14:49:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl P4EndServer {
|
2022-10-31 18:50:42 +00:00
|
|
|
pub fn connect (conf: &Config) -> anyhow::Result <Self> {
|
2021-10-10 15:19:48 +00:00
|
|
|
trace! ("P4 end server making its QUIC endpoint");
|
|
|
|
let endpoint = make_client_endpoint ("0.0.0.0:0".parse ()?, &[&conf.relay_cert])?;
|
|
|
|
|
2021-10-10 16:18:02 +00:00
|
|
|
let (shutdown_tx, shutdown_rx) = watch::channel (false);
|
2021-10-10 15:19:48 +00:00
|
|
|
|
|
|
|
Ok (P4EndServer {
|
2021-10-10 16:18:02 +00:00
|
|
|
endpoint,
|
|
|
|
shutdown_tx,
|
|
|
|
shutdown_rx,
|
2021-10-10 15:19:48 +00:00
|
|
|
})
|
2021-10-10 14:49:02 +00:00
|
|
|
}
|
|
|
|
|
2022-10-31 18:50:42 +00:00
|
|
|
pub async fn run (&self, conf: Config) -> anyhow::Result <()> {
|
|
|
|
let conf = Arc::new (conf);
|
|
|
|
|
2021-10-10 16:18:02 +00:00
|
|
|
trace! ("P4 end server connecting to P3 relay server");
|
|
|
|
let quinn::NewConnection {
|
|
|
|
mut bi_streams,
|
|
|
|
..
|
|
|
|
} = protocol::p4_connect_to_p3 (
|
|
|
|
&self.endpoint,
|
2022-10-31 18:50:42 +00:00
|
|
|
conf.relay_addr,
|
|
|
|
&conf.id
|
2021-10-10 16:18:02 +00:00
|
|
|
).await?;
|
|
|
|
|
|
|
|
debug! ("Connected to relay server");
|
|
|
|
|
2021-10-10 15:19:48 +00:00
|
|
|
trace! ("Accepting bi streams from P3");
|
|
|
|
|
2021-10-10 16:18:02 +00:00
|
|
|
let mut shutdown_rx = self.shutdown_rx.clone ();
|
|
|
|
|
2021-10-10 15:19:48 +00:00
|
|
|
loop {
|
2021-10-10 16:18:02 +00:00
|
|
|
tokio::select! {
|
|
|
|
_ = shutdown_rx.changed () => {
|
|
|
|
if *shutdown_rx.borrow () {
|
|
|
|
trace! ("P4 incoming bi streams task caught graceful shutdown");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
stream_opt = bi_streams.next () => {
|
|
|
|
let (relay_send, relay_recv) = stream_opt.ok_or_else (|| anyhow::anyhow! ("P4 ran out of incoming streams. Maybe P3 shut down or disconnected?"))??;
|
|
|
|
|
2022-10-31 18:50:42 +00:00
|
|
|
tokio::spawn (handle_bi_stream (Arc::clone (&conf), relay_send, relay_recv));
|
2021-10-10 16:18:02 +00:00
|
|
|
}
|
|
|
|
};
|
2021-10-10 15:19:48 +00:00
|
|
|
}
|
2021-10-10 16:18:02 +00:00
|
|
|
|
|
|
|
Ok (())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn shut_down (&self) -> anyhow::Result <()> {
|
|
|
|
trace! ("P4 end server shutting down...");
|
|
|
|
Ok (self.shutdown_tx.send (true)?)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn shutting_down (&self) -> bool {
|
|
|
|
*self.shutdown_rx.borrow ()
|
2021-10-10 14:49:02 +00:00
|
|
|
}
|
2021-10-10 14:36:01 +00:00
|
|
|
}
|
|
|
|
|
2021-10-02 17:39:03 +00:00
|
|
|
async fn handle_bi_stream (
|
2021-10-10 15:19:48 +00:00
|
|
|
conf: Arc <Config>,
|
2021-10-02 17:39:03 +00:00
|
|
|
relay_send: quinn::SendStream,
|
|
|
|
mut relay_recv: quinn::RecvStream,
|
|
|
|
) -> anyhow::Result <()>
|
|
|
|
{
|
|
|
|
match protocol::p4_accept_p3_stream (&mut relay_recv).await? {
|
|
|
|
protocol::P3ToP4Stream::NewPtthConnection {
|
|
|
|
client_id,
|
|
|
|
..
|
2021-10-10 15:19:48 +00:00
|
|
|
} => handle_new_ptth_connection (conf, relay_send, relay_recv, client_id).await?,
|
2021-10-02 17:39:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok (())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn handle_new_ptth_connection (
|
2021-10-10 15:19:48 +00:00
|
|
|
conf: Arc <Config>,
|
2021-10-02 17:39:03 +00:00
|
|
|
mut relay_send: quinn::SendStream,
|
|
|
|
mut relay_recv: quinn::RecvStream,
|
|
|
|
_client_id: String,
|
|
|
|
) -> anyhow::Result <()>
|
|
|
|
{
|
|
|
|
// TODO: Check authorization for P2 --> P4
|
|
|
|
|
|
|
|
protocol::p4_authorize_p2_connection (&mut relay_send).await?;
|
|
|
|
let p4_to_p5_req = protocol::p4_expect_p5_request (&mut relay_recv).await?;
|
|
|
|
|
|
|
|
// TODO: Check authorization for P1 --> P5
|
|
|
|
|
|
|
|
protocol::p4_authorize_p1_connection (&mut relay_send).await?;
|
|
|
|
|
|
|
|
debug! ("Started PTTH connection");
|
|
|
|
|
2021-10-10 15:19:48 +00:00
|
|
|
if conf.debug_echo {
|
2021-10-02 17:39:03 +00:00
|
|
|
relay_send.write (b"Connected to P4=P5 debug echo server\n").await?;
|
|
|
|
debug! ("Relaying bytes using internal debug echo server (P4=P5)");
|
|
|
|
tokio::io::copy (&mut relay_recv, &mut relay_send).await?;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
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...");
|
|
|
|
|
|
|
|
let ptth_conn = crate::connection::NewConnection {
|
|
|
|
local_send,
|
|
|
|
local_recv,
|
|
|
|
relay_send,
|
|
|
|
relay_recv,
|
|
|
|
}.build ();
|
|
|
|
|
|
|
|
ptth_conn.wait_for_close ().await?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok (())
|
|
|
|
}
|