use hyper::{ Body, Request, Response, Server, service::{ make_service_fn, service_fn, }, StatusCode, }; use structopt::StructOpt; use tokio::{ net::UdpSocket, }; use crate::prelude::*; use protocol::PeerId; #[derive (Debug, StructOpt)] pub struct Opt { #[structopt (long)] pub (crate) listen_addr: Option , #[structopt (long)] pub (crate) tcp_listen_port: Option , } pub struct App { endpoint: quinn::Endpoint, listen_addr: SocketAddr, pub (crate) metrics: Arc >, server_cert: Vec , tcp_listener: Option , } #[derive (Default)] pub (crate) struct Metrics { pub (crate) connected_end_servers: usize, } impl App { pub async fn new (opt: Opt) -> anyhow::Result { let config = load_config ().await.ok (); 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 listen_addr = endpoint.local_addr ()?; let tcp_port = opt.tcp_listen_port.or (config.map (|cfg| cfg.tcp_listen_port).flatten ()); let tcp_listener = if let Some (tcp_port) = tcp_port { let cfg = udp_over_tcp::server::Config { tcp_port, udp_port: listen_addr.port (), }; Some (udp_over_tcp::server::Listener::new (cfg).await?) } else { None }; Ok (Self { endpoint, listen_addr, metrics: Default::default (), server_cert, tcp_listener, }) } pub fn listen_addr (&self) -> SocketAddr { self.listen_addr } pub fn server_cert (&self) -> &[u8] { &self.server_cert } pub fn tcp_listen_port (&self) -> anyhow::Result