diff --git a/src/init.rs b/src/init.rs index 70fe5fa..bf478f7 100644 --- a/src/init.rs +++ b/src/init.rs @@ -222,11 +222,17 @@ impl Launcher { None }; - tokio::spawn( - server - .clone() - .listen(ip, Duration::new(1, 0), tls_cert, chain, key), - ); + let (tcp_socket, udp_socket, tls_socket) = Server::bind(ip, chain.is_some()).await?; + + tokio::spawn(server.clone().listen( + Duration::new(1, 0), + tls_cert, + chain, + key, + tcp_socket, + udp_socket, + tls_socket + )); } return Ok(ztauthority); diff --git a/src/server.rs b/src/server.rs index 22d6205..3e5b260 100644 --- a/src/server.rs +++ b/src/server.rs @@ -2,6 +2,7 @@ use std::{ net::{IpAddr, SocketAddr}, time::Duration, }; +use anyhow::Context; use tracing::info; use openssl::{ @@ -23,28 +24,42 @@ impl Server { Self(zt) } + pub async fn bind(ip: IpAddr, use_dot: bool) -> Result<(TcpListener, UdpSocket, Option), anyhow::Error> { + let sa = SocketAddr::new(ip, 53); + + let tcp = TcpListener::bind(sa).await.with_context(|| "Failed to bind TCP port 53")?; + let udp = UdpSocket::bind(sa).await.with_context(|| "Failed to bind UDP port 53")?; + + + let tls = match use_dot { + true => TcpListener::bind(SocketAddr::new(ip, 853)).await.with_context(|| "Failed to bind TCP port 853 (DoT)").ok(), + false => None + }; + + return Ok((tcp, udp, tls)); + } + // listener routine for TCP and UDP. pub async fn listen( self, - ip: IpAddr, tcp_timeout: Duration, certs: Option, cert_chain: Option>, key: Option>, + tcp: TcpListener, + udp: UdpSocket, + dot: Option, ) -> Result<(), anyhow::Error> { - let sa = SocketAddr::new(ip, 53); - let tcp = TcpListener::bind(sa).await?; - let udp = UdpSocket::bind(sa).await?; - let mut sf = ServerFuture::new(init_catalog(self.0).await?); - if let (Some(certs), Some(key)) = (certs.clone(), key.clone()) { - info!("Configuring DoT Listener"); - let tls = TcpListener::bind(SocketAddr::new(ip, 853)).await?; + if let Some(dot) = dot { + if let (Some(certs), Some(key)) = (certs.clone(), key.clone()) { + info!("Configuring DoT Listener"); - match sf.register_tls_listener(tls, tcp_timeout, ((certs, cert_chain), key)) { - Ok(_) => {} - Err(e) => tracing::error!("Cannot start DoT listener: {}", e), + match sf.register_tls_listener(dot, tcp_timeout, ((certs, cert_chain), key)) { + Ok(_) => {} + Err(e) => tracing::error!("Cannot start DoT listener: {}", e), + } } } diff --git a/tests/service/mod.rs b/tests/service/mod.rs index 9051ea1..afe845c 100644 --- a/tests/service/mod.rs +++ b/tests/service/mod.rs @@ -239,8 +239,23 @@ impl Service { for ip in listen_ips.clone() { let server = Server::new(ztauthority.to_owned()); + + let (tcp_socket, udp_socket, tls_socket) = match Server::bind(ip.ip(), false).await { + Ok(x) => x, + Err(e) => { + panic!("Could not bind port. {}", e); + } + }; info!("Serving {}", ip.clone()); - tokio::spawn(server.listen(ip.ip(), Duration::new(1, 0), None, None, None)); + tokio::spawn(server.listen( + Duration::new(1, 0), + None, + None, + None, + tcp_socket, + udp_socket, + tls_socket + )); } listen_ips