1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
use std::collections::HashMap;
use std::pin::Pin;
use std::sync::{Arc, Mutex};
use std::task::{Context, Poll};
use futures::{
future,
stream::{self, Stream, StreamExt},
};
use hickory_client::proto::{rr::RecordType, xfer::DnsResponse};
use tokio::task::JoinHandle;
use crate::{error::BlastDNSError, utils::format_ptr_query};
/// Core trait for DNS resolution.
/// Types only need to implement `resolve_full`, all other methods have default implementations.
pub trait DnsResolver: Send + Sync + Clone {
/// Resolve a hostname and return the full DNS response.
/// This is the only method that needs to be implemented by types.
fn resolve_full(
&self,
host: String,
record_type: RecordType,
) -> impl std::future::Future<Output = Result<DnsResponse, BlastDNSError>> + Send;
/// Get the concurrency limit for batch operations.
/// Default implementation returns 32.
fn get_concurrency(&self) -> usize {
32
}
/// Resolve a hostname and return only the record data strings.
/// Default implementation extracts strings from the full response.
fn resolve(
&self,
host: String,
record_type: RecordType,
) -> impl std::future::Future<Output = Result<Vec<String>, BlastDNSError>> + Send {
async move {
let response = self.resolve_full(host, record_type).await?;
let answers: Vec<String> = response
.answers()
.iter()
.map(|record| record.data().to_string())
.collect();
Ok(answers)
}
}
/// Resolve multiple record types for a single hostname in parallel, returning only successful rdata strings.
/// Default implementation calls resolve_multi_full and filters successful results.
fn resolve_multi(
&self,
host: String,
record_types: Vec<RecordType>,
) -> impl std::future::Future<Output = Result<HashMap<RecordType, Vec<String>>, BlastDNSError>> + Send
{
async move {
let full_results = self.resolve_multi_full(host, record_types).await?;
let simplified: HashMap<RecordType, Vec<String>> = full_results
.into_iter()
.filter_map(|(record_type, result)| {
result.ok().and_then(|response| {
let answers: Vec<String> = response
.answers()
.iter()
.map(|record| record.data().to_string())
.collect();
// Only include if there are actual answers
if answers.is_empty() {
None
} else {
Some((record_type, answers))
}
})
})
.collect();
Ok(simplified)
}
}
/// Resolve multiple record types for a single hostname in parallel, returning full responses.
/// Default implementation calls resolve_full for each record type.
fn resolve_multi_full(
&self,
host: String,
record_types: Vec<RecordType>,
) -> impl std::future::Future<
Output = Result<HashMap<RecordType, Result<DnsResponse, BlastDNSError>>, BlastDNSError>,
> + Send {
async move {
if record_types.is_empty() {
return Err(BlastDNSError::Configuration(
"at least one record type is required".into(),
));
}
let futures: Vec<_> = record_types
.iter()
.map(|&record_type| {
let host = host.clone();
async move {
let mut query_host = host;
// Auto-format PTR queries if an IP address is provided
if record_type == RecordType::PTR {
query_host = format_ptr_query(&query_host);
}
let result = self.resolve_full(query_host, record_type).await;
(record_type, result)
}
})
.collect();
let results = future::join_all(futures).await;
Ok(results.into_iter().collect())
}
}
/// Resolve a batch of hostnames with bounded concurrency, returning simplified tuples.
///
/// Returns (hostname, record_type, [rdata_strings]) where rdata_strings contain only
/// the record data (e.g., "93.184.216.34" for A records, "10 aspmx.l.google.com." for MX).
/// Only successful resolutions with non-empty answers are returned.
fn resolve_batch<I, E>(
self: Arc<Self>,
hosts: I,
record_type: RecordType,
) -> impl stream::Stream<Item = (String, String, Vec<String>)> + Unpin + Send + 'static
where
Self: Sized + 'static,
I: Iterator<Item = Result<String, E>> + Send + 'static,
E: std::error::Error + Send + 'static,
{
let record_type_string = record_type.to_string();
Box::pin(
self.resolve_batch_full(hosts, record_type, true, true)
.filter_map(move |(host, result)| {
let record_type_str = record_type_string.clone();
async move {
match result {
Ok(response) => {
let answers: Vec<String> = response
.answers()
.iter()
.map(|record| record.data().to_string())
.collect();
if answers.is_empty() {
None
} else {
Some((host, record_type_str, answers))
}
}
Err(_) => None,
}
}
}),
)
}
/// Resolve a batch of hostnames with bounded concurrency and stream the full results as they complete.
fn resolve_batch_full<I, E>(
self: Arc<Self>,
hosts: I,
record_type: RecordType,
skip_empty: bool,
skip_errors: bool,
) -> impl stream::Stream<Item = (String, Result<DnsResponse, BlastDNSError>)> + Unpin + Send + 'static
where
Self: Sized + 'static,
I: Iterator<Item = Result<String, E>> + Send + 'static,
E: std::error::Error + Send + 'static,
{
let client = Arc::clone(&self);
let concurrency = self.get_concurrency();
// Convert iterator to stream using spawn_blocking to avoid blocking Tokio
let host_stream = BlockingIteratorStream::new(hosts);
Box::pin(
host_stream
.filter_map(|result| async move {
match result {
Ok(host) => Some(host),
Err(e) => {
eprintln!("Iterator error: {}", e);
None
}
}
})
.map(move |host| {
let client = Arc::clone(&client);
let label = host.clone();
async move {
let result = client.resolve_full(host, record_type).await;
(label, result)
}
})
.buffer_unordered(concurrency * 2)
.filter_map(move |(host, result)| async move {
// Filter empty responses if skip_empty is true
if skip_empty {
match &result {
Ok(response) if response.answers().is_empty() => return None,
_ => {}
}
}
// Filter errors if skip_errors is true
if skip_errors && result.is_err() {
return None;
}
Some((host, result))
}),
)
}
}
/// Stream adapter that wraps an iterator and polls it via spawn_blocking
pub struct BlockingIteratorStream<I, T> {
iterator: Arc<Mutex<I>>,
pending: Option<JoinHandle<Option<T>>>,
}
impl<I, T> BlockingIteratorStream<I, T>
where
I: Iterator<Item = T> + Send + 'static,
T: Send + 'static,
{
pub fn new(iterator: I) -> Self {
Self {
iterator: Arc::new(Mutex::new(iterator)),
pending: None,
}
}
}
impl<I, T> Stream for BlockingIteratorStream<I, T>
where
I: Iterator<Item = T> + Send + 'static,
T: Send + 'static,
{
type Item = T;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
// If no pending task, spawn one
if self.pending.is_none() {
let iterator = Arc::clone(&self.iterator);
let handle = tokio::task::spawn_blocking(move || {
let mut iter = iterator.lock().unwrap();
iter.next()
});
self.pending = Some(handle);
}
// Poll the pending task
let handle = self.pending.as_mut().unwrap();
match Pin::new(handle).poll(cx) {
Poll::Ready(Ok(result)) => {
self.pending = None;
Poll::Ready(result)
}
Poll::Ready(Err(_)) => {
self.pending = None;
Poll::Ready(None)
}
Poll::Pending => Poll::Pending,
}
}
}