- if let Ok(tls_stream) = self.acceptor.accept(tcp_stream).await {
- debug!("Completed TLS handshake with {}", peer_addr);
- Ok(Some(Connection::from(TlsConnectionMetadata::Server {
- local_addr: self.local_addrs.clone(),
- peer_addr,
- stream: tls_stream,
- })))
- } else {
- warn!("Could not encrypt connection with TLS from {}", peer_addr);
- Ok(None)
+ match self.acceptor.accept(tcp_stream).await {
+ Ok(tls_stream) => {
+ debug!("Completed TLS handshake with {}", peer_addr);
+ Ok(Connection::from(TlsConnectionMetadata::Server {
+ local_addr: self.local_addrs.clone(),
+ peer_addr,
+ stream: tls_stream,
+ }))
+ }
+
+ Err(e) => {
+ warn!("Could not encrypt connection with TLS from {}", peer_addr);
+ Err(anyhow::Error::new(e))
+ }
+ }
+ }
+}
+
+impl Stream for TlsServer {
+ type Item = Connection;
+
+ fn poll_next(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
+ match futures::executor::block_on(self.listener.incoming().next()) {
+ Some(Ok(tcp_stream)) => {
+ let peer_addr = tcp_stream.peer_addr().expect("Could not retrieve peer IP address");
+ debug!("Received connection attempt from {}", peer_addr);
+
+ match futures::executor::block_on(self.acceptor.accept(tcp_stream)) {
+ Ok(tls_stream) => {
+ debug!("Completed TLS handshake with {}", peer_addr);
+ Poll::Ready(Some(Connection::from(TlsConnectionMetadata::Server {
+ local_addr: self.local_addrs.clone(),
+ peer_addr,
+ stream: tls_stream,
+ })))
+ }
+
+ Err(_e) => {
+ warn!("Could not encrypt connection with TLS from {}", peer_addr);
+ Poll::Pending
+ }
+ }
+ },
+
+ Some(Err(e)) => {
+ error!("Encountered error when trying to accept new connection {}", e);
+ Poll::Pending
+ }
+
+ None => Poll::Ready(None)