Skip to content

Commit

Permalink
feat(sni_sniffer): add port filter
Browse files Browse the repository at this point in the history
  • Loading branch information
spacemeowx2 committed Aug 31, 2023
1 parent 582ee68 commit cfceeb7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
6 changes: 5 additions & 1 deletion rd-std/src/sniffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ impl Builder<Net> for DNSSnifferNet {
pub struct SNINetConfig {
#[serde(default)]
net: NetRef,
/// Ports to sniff.
/// If not set, only 443 port will be sniffed.
#[serde(default)]
ports: Option<Vec<u16>>,
}

impl Builder<Net> for SNISnifferNet {
Expand All @@ -42,7 +46,7 @@ impl Builder<Net> for SNISnifferNet {
type Item = Self;

fn build(config: Self::Config) -> Result<Self> {
Ok(SNISnifferNet::new(config.net.value_cloned()))
Ok(SNISnifferNet::new(config.net.value_cloned(), config.ports))
}
}

Expand Down
15 changes: 11 additions & 4 deletions rd-std/src/sniffer/sni_sniffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ const BUFFER_SIZE: usize = 1024;

pub struct SNISnifferNet {
net: Net,
ports: Option<Vec<u16>>,
}

impl SNISnifferNet {
pub fn new(net: Net) -> Self {
Self { net }
pub fn new(net: Net, ports: Option<Vec<u16>>) -> Self {
Self { net, ports }
}
}

Expand Down Expand Up @@ -58,8 +59,14 @@ impl rd_interface::TcpConnect for SNISnifferNet {
ctx: &mut rd_interface::Context,
addr: &Address,
) -> Result<rd_interface::TcpStream> {
let tcp = SnifferTcp::new(addr, ConnectSendParam::new(self.net.clone(), ctx));
Ok(tcp.into_dyn())
if match &self.ports {
Some(ports) => ports.contains(&addr.port()),
None => addr.port() == 443,
} {
Ok(SnifferTcp::new(addr, ConnectSendParam::new(self.net.clone(), ctx)).into_dyn())
} else {
self.net.tcp_connect(ctx, addr).await
}
}
}

Expand Down

0 comments on commit cfceeb7

Please sign in to comment.