👕 refactor: break apart the relay's main fn so we can see which ports it bound
parent
605c15468a
commit
3f0272ed09
|
@ -1,6 +1,7 @@
|
||||||
use tokio::sync::watch;
|
use tokio::sync::watch;
|
||||||
|
|
||||||
use ptth_quic::prelude::*;
|
use ptth_quic::prelude::*;
|
||||||
|
use ptth_quic::executable_relay_server as relay;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main () -> anyhow::Result <()> {
|
async fn main () -> anyhow::Result <()> {
|
||||||
|
@ -8,7 +9,7 @@ async fn main () -> anyhow::Result <()> {
|
||||||
|
|
||||||
tracing_subscriber::fmt::init ();
|
tracing_subscriber::fmt::init ();
|
||||||
|
|
||||||
let opt = ptth_quic::executable_relay_server::Opt::from_args ();
|
let opt = relay::Opt::from_args ();
|
||||||
|
|
||||||
let (running_tx, mut running_rx) = watch::channel (true);
|
let (running_tx, mut running_rx) = watch::channel (true);
|
||||||
|
|
||||||
|
@ -17,8 +18,15 @@ async fn main () -> anyhow::Result <()> {
|
||||||
})?;
|
})?;
|
||||||
trace! ("Set Ctrl+C handler");
|
trace! ("Set Ctrl+C handler");
|
||||||
|
|
||||||
|
let app = relay::App::new (opt)?;
|
||||||
|
println! ("Base64 cert: {}", base64::encode (app.server_cert ()));
|
||||||
|
println! ("Listening on {}", app.listen_addr ());
|
||||||
|
|
||||||
|
tokio::fs::create_dir_all ("ptth_quic_output").await?;
|
||||||
|
tokio::fs::write ("ptth_quic_output/quic_server.crt", app.server_cert ()).await?;
|
||||||
|
|
||||||
tokio::select! {
|
tokio::select! {
|
||||||
val = ptth_quic::executable_relay_server::main (opt) => {
|
val = app.run () => {
|
||||||
|
|
||||||
},
|
},
|
||||||
val = running_rx.changed () => {
|
val = running_rx.changed () => {
|
||||||
|
|
|
@ -18,22 +18,50 @@ use crate::prelude::*;
|
||||||
use protocol::PeerId;
|
use protocol::PeerId;
|
||||||
|
|
||||||
#[derive (Debug, StructOpt)]
|
#[derive (Debug, StructOpt)]
|
||||||
pub (crate) struct Opt {
|
pub struct Opt {
|
||||||
#[structopt (long)]
|
#[structopt (long)]
|
||||||
pub (crate) listen_addr: Option <String>,
|
pub (crate) listen_addr: Option <String>,
|
||||||
#[structopt (long)]
|
#[structopt (long)]
|
||||||
pub (crate) tcp_listen_port: Option <u16>,
|
pub (crate) tcp_listen_port: Option <u16>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub (crate) async fn main (opt: Opt) -> anyhow::Result <()>
|
pub struct App {
|
||||||
{
|
endpoint: quinn::Endpoint,
|
||||||
|
listen_addr: SocketAddr,
|
||||||
|
server_cert: Vec <u8>,
|
||||||
|
tcp_listen_port: Option <u16>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl App {
|
||||||
|
pub fn new (opt: Opt) -> anyhow::Result <Self> {
|
||||||
let listen_addr = opt.listen_addr.unwrap_or_else (|| String::from ("0.0.0.0:30380")).parse ()?;
|
let listen_addr = opt.listen_addr.unwrap_or_else (|| String::from ("0.0.0.0:30380")).parse ()?;
|
||||||
let (endpoint, server_cert) = make_server_endpoint (listen_addr)?;
|
let (endpoint, server_cert) = make_server_endpoint (listen_addr)?;
|
||||||
println! ("Base64 cert: {}", base64::encode (&server_cert));
|
|
||||||
println! ("Listening on {}", listen_addr);
|
|
||||||
|
|
||||||
tokio::fs::create_dir_all ("ptth_quic_output").await?;
|
let listen_addr = endpoint.local_addr ()?;
|
||||||
tokio::fs::write ("ptth_quic_output/quic_server.crt", &server_cert).await?;
|
|
||||||
|
Ok (Self {
|
||||||
|
endpoint,
|
||||||
|
listen_addr,
|
||||||
|
server_cert,
|
||||||
|
tcp_listen_port: opt.tcp_listen_port,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn listen_addr (&self) -> SocketAddr {
|
||||||
|
self.listen_addr
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn server_cert (&self) -> &[u8] {
|
||||||
|
&self.server_cert
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn run (self) -> anyhow::Result <()> {
|
||||||
|
let Self {
|
||||||
|
endpoint,
|
||||||
|
listen_addr,
|
||||||
|
server_cert,
|
||||||
|
tcp_listen_port,
|
||||||
|
} = self;
|
||||||
|
|
||||||
let relay_state = RelayState::default ();
|
let relay_state = RelayState::default ();
|
||||||
if let Err (e) = relay_state.reload_config ().await {
|
if let Err (e) = relay_state.reload_config ().await {
|
||||||
|
@ -134,7 +162,7 @@ pub (crate) async fn main (opt: Opt) -> anyhow::Result <()>
|
||||||
|
|
||||||
debug! ("Serving HTTP on {:?}", http_addr);
|
debug! ("Serving HTTP on {:?}", http_addr);
|
||||||
|
|
||||||
if let Some (tcp_listen_port) = opt.tcp_listen_port {
|
if let Some (tcp_listen_port) = tcp_listen_port {
|
||||||
tokio::spawn (async move {
|
tokio::spawn (async move {
|
||||||
let cfg = udp_over_tcp::server::Config {
|
let cfg = udp_over_tcp::server::Config {
|
||||||
tcp_port: tcp_listen_port,
|
tcp_port: tcp_listen_port,
|
||||||
|
@ -175,6 +203,7 @@ pub (crate) async fn main (opt: Opt) -> anyhow::Result <()>
|
||||||
|
|
||||||
Ok (())
|
Ok (())
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async fn handle_http (_req: Request <Body>, relay_state: Arc <RelayState>)
|
async fn handle_http (_req: Request <Body>, relay_state: Arc <RelayState>)
|
||||||
-> anyhow::Result <Response <Body>>
|
-> anyhow::Result <Response <Body>>
|
||||||
|
|
|
@ -9,12 +9,12 @@ async fn end_to_end_async () -> anyhow::Result <()> {
|
||||||
use crate::executable_relay_server as relay;
|
use crate::executable_relay_server as relay;
|
||||||
|
|
||||||
let relay_opt = relay::Opt {
|
let relay_opt = relay::Opt {
|
||||||
listen_addr: "127.0.0.1:30381".to_string ().into (),
|
listen_addr: "127.0.0.1:0".to_string ().into (),
|
||||||
tcp_listen_port: 8001.into (),
|
tcp_listen_port: None,
|
||||||
};
|
};
|
||||||
|
let relay_app = relay::App::new (relay_opt)?;
|
||||||
let task_relay = tokio::spawn (async move {
|
let task_relay = tokio::spawn (async move {
|
||||||
relay::main (relay_opt).await?;
|
relay_app.run ().await
|
||||||
Ok::<_, anyhow::Error> (())
|
|
||||||
});
|
});
|
||||||
|
|
||||||
Ok (())
|
Ok (())
|
||||||
|
|
Loading…
Reference in New Issue