Skip to content

Commit c0b54ea

Browse files
committed
misc: replaced HashMaps with DashMap and DashSet in dns plugin for increased performances
1 parent 923b0f4 commit c0b54ea

File tree

1 file changed

+12
-16
lines changed

1 file changed

+12
-16
lines changed

src/plugins/dns/mod.rs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use std::collections::HashMap;
21
use std::net::IpAddr;
32
use std::sync::Arc;
43
use std::time::Duration;
54

65
use async_trait::async_trait;
7-
use tokio::sync::Mutex;
6+
use dashmap::DashMap;
7+
use dashmap::DashSet;
88
use trust_dns_resolver::{AsyncResolver, TokioAsyncResolver, config::*};
99
use x509_parser::prelude::{FromDer, GeneralName, X509Certificate};
1010

@@ -27,17 +27,17 @@ super::manager::register_plugin! {
2727
pub(crate) struct DNS {
2828
resolver: Option<TokioAsyncResolver>,
2929
opts: options::Options,
30-
hits: Arc<Mutex<HashMap<IpAddr, usize>>>,
31-
domains: Arc<Mutex<HashMap<String, u8>>>,
30+
hits: Arc<DashMap<IpAddr, usize>>,
31+
domains: Arc<DashSet<String>>,
3232
}
3333

3434
impl DNS {
3535
pub fn new() -> Self {
3636
DNS {
3737
resolver: None,
3838
opts: options::Options::default(),
39-
hits: Arc::new(Mutex::new(HashMap::default())),
40-
domains: Arc::new(Mutex::new(HashMap::default())),
39+
hits: Arc::new(DashMap::new()),
40+
domains: Arc::new(DashSet::new()),
4141
}
4242
}
4343

@@ -46,14 +46,13 @@ impl DNS {
4646
// this filtering in order too many positives for an address and work around this behaviour.
4747
let mut filtered = vec![];
4848
for ip in &addresses {
49-
let mut hits = self.hits.lock().await;
50-
let curr_hits = if let Some(ip_hits) = hits.get_mut(ip) {
49+
let curr_hits = if let Some(mut ip_hits) = self.hits.get_mut(ip) {
5150
// this ip already has a counter, increment it
5251
*ip_hits += 1;
5352
*ip_hits
5453
} else {
5554
// first time we see this ip, create the counter for it
56-
hits.insert(ip.to_owned(), 1);
55+
self.hits.insert(ip.to_owned(), 1);
5756
1
5857
};
5958

@@ -121,7 +120,7 @@ impl DNS {
121120
// skip wildcard names and other domains
122121
if !tls_domain.contains('*') && tls_domain.ends_with(&check) {
123122
// skip domains that have already been processed
124-
if !self.domains.lock().await.contains_key(&tls_domain) {
123+
if !self.domains.contains(&tls_domain) {
125124
// try to resolve to ip
126125
if let Ok(response) =
127126
self.resolver.as_ref().unwrap().lookup_ip(&tls_domain).await
@@ -213,7 +212,7 @@ impl Plugin for DNS {
213212
) -> Result<Option<Vec<Loot>>, Error> {
214213
let subdomain = format!("{}.{}", creds.single(), &creds.target).to_lowercase();
215214
// skip domains that have already been processed
216-
if self.domains.lock().await.contains_key(&subdomain) {
215+
if self.domains.contains(&subdomain) {
217216
return Ok(None);
218217
}
219218

@@ -254,7 +253,7 @@ impl Plugin for DNS {
254253
let mut loot = vec![Loot::new("dns", &subdomain, loot_data)];
255254

256255
// keep track of domains we processed already
257-
self.domains.lock().await.insert(subdomain.to_owned(), 1);
256+
self.domains.insert(subdomain.to_owned());
258257

259258
if !self.opts.dns_no_https {
260259
let more_loot = self
@@ -263,10 +262,7 @@ impl Plugin for DNS {
263262

264263
// keep track of domains we processed already
265264
for item in more_loot.iter() {
266-
self.domains
267-
.lock()
268-
.await
269-
.insert(item.get_target().to_string(), 1);
265+
self.domains.insert(item.get_target().to_string());
270266
}
271267

272268
loot.extend(more_loot);

0 commit comments

Comments
 (0)