use std::net::SocketAddr;
use std::future::Future;
use std::pin::Pin;
use tokio::io::{AsyncRead, AsyncWrite};
pub struct IncomingConnection<IO> {
pub stream: IO,
pub client_addr: SocketAddr,
pub target_addr: Option<SocketAddr>,
}
pub trait CaptureSource {
type IO: AsyncRead + AsyncWrite + Unpin + Send + 'static;
#[allow(clippy::type_complexity)]
fn accept(&mut self) -> Pin<Box<dyn Future<Output = crate::error::Result<IncomingConnection<Self::IO>>> + Send + '_>>;
fn listen_addrs(&self) -> Vec<SocketAddr> {
vec![]
}
}
use tokio::net::TcpListener;
pub struct TcpCaptureSource {
listener: TcpListener,
}
impl TcpCaptureSource {
pub fn new(listener: TcpListener) -> Self {
Self { listener }
}
}
impl CaptureSource for TcpCaptureSource {
type IO = tokio::net::TcpStream;
fn accept(&mut self) -> Pin<Box<dyn Future<Output = crate::error::Result<IncomingConnection<Self::IO>>> + Send + '_>> {
Box::pin(async move {
let (stream, client_addr) = self.listener.accept().await?;
Ok(IncomingConnection {
stream,
client_addr,
target_addr: None, })
})
}
fn listen_addrs(&self) -> Vec<SocketAddr> {
if let Ok(addr) = self.listener.local_addr() {
vec![addr]
} else {
vec![]
}
}
}