domain-status 0.2.1

Concurrent URL status checker that captures comprehensive metadata in SQLite.
Documentation
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
//! WHOIS/RDAP domain lookup using whois-service crate

mod cache;
mod parse;
mod types;

use anyhow::Result;
use std::path::Path;
use std::sync::Arc;
use std::time::Duration;

use whois_service::{LookupStatus, WhoisClient};

pub use types::WhoisResult;

use cache::WhoisCacheStore;
use parse::{convert_parsed_data, enrich_result_from_raw_text};

/// Process-wide WHOIS/RDAP client (IANA bootstrap once, in-process cache + coalescing).
pub(crate) type SharedWhoisClient = Arc<WhoisClient>;

/// Build the shared scan client. Failures are best-effort (ADR 0002).
pub(crate) async fn init_shared_client() -> Result<SharedWhoisClient> {
    WhoisClient::new()
        .await
        .map(Arc::new)
        .map_err(|e| anyhow::anyhow!("Failed to create WHOIS client: {e}"))
}

fn whois_result_is_usable(result: &WhoisResult) -> bool {
    result.registrar.as_deref().is_some_and(|s| !s.is_empty())
        || result
            .raw_text
            .as_deref()
            .is_some_and(|s| !s.trim().is_empty())
}

/// Vendor `LookupStatus` is the cache gate: rate-limit banners must not look like a 7-day hit.
fn lookup_status_is_cacheable(status: LookupStatus, domain: &str) -> bool {
    match status {
        LookupStatus::Found => true,
        LookupStatus::RateLimited => {
            log::warn!("WHOIS rate-limited for {domain}; not caching");
            false
        }
        LookupStatus::NotFound => {
            log::debug!("WHOIS not found for {domain}");
            false
        }
    }
}

/// Performs a WHOIS lookup for a domain
///
/// This function uses the `whois-service` crate which:
/// - Automatically tries RDAP first, then falls back to WHOIS
/// - Handles IANA bootstrap for TLD discovery
/// - Implements per-server rate limiting
/// - Provides structured parsing
///
/// # Arguments
///
/// * `domain` - The domain to look up (e.g., "example.com")
/// * `cache_dir` - Optional cache directory for storing WHOIS data
///
/// # Returns
///
/// Returns WHOIS information if available, or None if lookup fails
///
/// `None` is not limited to "domain not found". It also covers timeouts,
/// backend failures, and other best-effort lookup failures.
///
/// # Examples
///
/// ```no_run
/// use domain_status::lookup_whois;
///
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// if let Some(result) = lookup_whois("example.com", None).await? {
///     println!("{:?}", result.registrar);
/// }
/// # Ok(())
/// # }
/// ```
///
/// # Errors
/// Returns `Err` when the WHOIS client cannot be created or the lookup fails.
pub async fn lookup_whois(domain: &str, cache_dir: Option<&Path>) -> Result<Option<WhoisResult>> {
    lookup_whois_with_client(domain, cache_dir, None).await
}

/// Scan-path lookup using the process-wide client when one was built at init.
pub(crate) async fn lookup_whois_with_client(
    domain: &str,
    cache_dir: Option<&Path>,
    client: Option<&WhoisClient>,
) -> Result<Option<WhoisResult>> {
    let existing = client.cloned();
    lookup_whois_with_lookup(domain, cache_dir, move |lookup_domain| {
        let domain = lookup_domain.to_string();
        async move {
            let client = match existing {
                Some(client) => client,
                None => WhoisClient::new()
                    .await
                    .map_err(|e| anyhow::anyhow!("Failed to create WHOIS client: {e}"))?,
            };
            client.lookup(&domain).await.map_err(anyhow::Error::from)
        }
    })
    .await
}

async fn lookup_whois_with_lookup<F, Fut>(
    domain: &str,
    cache_dir: Option<&Path>,
    lookup: F,
) -> Result<Option<WhoisResult>>
where
    F: FnOnce(&str) -> Fut,
    Fut: std::future::Future<Output = Result<whois_service::WhoisResponse>>,
{
    let cache_path = cache_dir.map_or_else(
        || crate::cache_paths::whois_dir(&crate::cache_paths::resolve_cache_root(None)),
        std::path::Path::to_path_buf,
    );
    let cache = WhoisCacheStore::default();

    if let Some(cached) = cache.load(&cache_path, domain).await? {
        let result = enrich_result_from_raw_text(WhoisResult::from(cached.result));
        if whois_result_is_usable(&result) {
            log::debug!("WHOIS cache hit for {domain}");
            return Ok(Some(result));
        }
        log::warn!("Ignoring empty cached WHOIS for {domain}");
    }

    log::debug!("Starting WHOIS lookup for domain: {domain}");
    if let Ok(response) = tokio::time::timeout(
        Duration::from_secs(crate::config::WHOIS_TIMEOUT_SECS),
        lookup(domain),
    )
    .await
    {
        let response = match response {
            Ok(response) => response,
            Err(e) => {
                log::warn!("WHOIS lookup failed for {domain}: {e}");
                return Ok(None);
            }
        };
        if !lookup_status_is_cacheable(response.lookup_status, domain) {
            return Ok(None);
        }

        log::debug!("WHOIS lookup successful for {domain}");
        let result = convert_parsed_data(&response);
        if !whois_result_is_usable(&result) {
            log::warn!(
                "WHOIS lookup returned no usable fields for {domain}; not caching empty result"
            );
            return Ok(None);
        }

        cache.save(&cache_path, domain, &result).await?;

        Ok(Some(result))
    } else {
        log::warn!(
            "WHOIS lookup timed out for {} after {}s",
            domain,
            crate::config::WHOIS_TIMEOUT_SECS
        );
        Ok(None)
    }
}

/// Persist a WHOIS cache entry so tests can exercise `enable_whois` without the network.
///
/// `cache_dir` is the WHOIS cache directory (the `whois/` subdirectory of the scan cache root).
///
/// # Errors
///
/// Returns an error if the cache directory cannot be created or the entry cannot be written.
#[cfg(any(test, feature = "test-utils"))]
pub async fn seed_whois_cache(cache_dir: &Path, domain: &str, result: &WhoisResult) -> Result<()> {
    WhoisCacheStore::default()
        .save(cache_dir, domain, result)
        .await
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::atomic::{AtomicUsize, Ordering};
    use std::sync::Arc;
    use tempfile::TempDir;
    use whois_service::{ParsedWhoisData, WhoisResponse};

    fn fake_response() -> WhoisResponse {
        WhoisResponse {
            domain: "example.com".to_string(),
            whois_server: "whois.example.com".to_string(),
            raw_data: "Registrant Organization: Example Org\nRegistrant Country: US\nraw whois"
                .to_string(),
            parsed_data: Some(ParsedWhoisData {
                registrar: Some("Example Registrar".to_string()),
                creation_date: Some("2024-01-15T10:30:45Z".to_string()),
                expiration_date: None,
                updated_date: None,
                name_servers: vec!["ns1.example.com".to_string()],
                status: vec!["clientTransferProhibited".to_string()],
                registrant_name: Some("Example Org".to_string()),
                registrant_email: None,
                admin_email: None,
                tech_email: None,
                created_ago: None,
                updated_ago: None,
                expires_in: None,
            }),
            lookup_status: LookupStatus::Found,
            cached: false,
            query_time_ms: 25,
            parsing_analysis: None,
        }
    }

    #[tokio::test]
    async fn test_lookup_whois_caches_first_lookup_and_hits_cache_next_time() {
        let temp_dir = TempDir::new().expect("temp dir");
        let calls = Arc::new(AtomicUsize::new(0));

        let first = lookup_whois_with_lookup("example.com", Some(temp_dir.path()), {
            let calls = Arc::clone(&calls);
            move |_| {
                let calls = Arc::clone(&calls);
                async move {
                    calls.fetch_add(1, Ordering::SeqCst);
                    Ok(fake_response())
                }
            }
        })
        .await
        .expect("lookup should succeed");

        let second = lookup_whois_with_lookup("example.com", Some(temp_dir.path()), {
            let calls = Arc::clone(&calls);
            move |_| {
                let calls = Arc::clone(&calls);
                async move {
                    calls.fetch_add(1, Ordering::SeqCst);
                    Ok(fake_response())
                }
            }
        })
        .await
        .expect("cached lookup should succeed");

        assert_eq!(calls.load(Ordering::SeqCst), 1);
        let first = first.expect("first result");
        let second = second.expect("second result");
        assert_eq!(first.registrar.as_deref(), Some("Example Registrar"));
        assert_eq!(second.registrar.as_deref(), Some("Example Registrar"));
        assert_eq!(first.registrant_org.as_deref(), Some("Example Org"));
        assert_eq!(second.registrant_country.as_deref(), Some("US"));
    }

    #[tokio::test]
    async fn test_lookup_whois_returns_none_on_backend_error() {
        let temp_dir = TempDir::new().expect("temp dir");
        let result = lookup_whois_with_lookup("example.com", Some(temp_dir.path()), |_| async {
            Err(anyhow::anyhow!("backend failure"))
        })
        .await
        .expect("lookup wrapper should not fail");

        assert!(result.is_none());
    }

    fn empty_response() -> WhoisResponse {
        WhoisResponse {
            domain: "empty.example".to_string(),
            whois_server: "whois.example.com".to_string(),
            raw_data: String::new(),
            parsed_data: None,
            lookup_status: LookupStatus::Found,
            cached: false,
            query_time_ms: 1,
            parsing_analysis: None,
        }
    }

    #[tokio::test]
    async fn test_lookup_whois_does_not_cache_empty_payload() {
        let temp_dir = TempDir::new().expect("temp dir");
        let calls = Arc::new(AtomicUsize::new(0));

        let first = lookup_whois_with_lookup("empty.example", Some(temp_dir.path()), {
            let calls = Arc::clone(&calls);
            move |_| {
                let calls = Arc::clone(&calls);
                async move {
                    calls.fetch_add(1, Ordering::SeqCst);
                    Ok(empty_response())
                }
            }
        })
        .await
        .expect("lookup wrapper should not fail");
        assert!(first.is_none());

        let second = lookup_whois_with_lookup("empty.example", Some(temp_dir.path()), {
            let calls = Arc::clone(&calls);
            move |_| {
                let calls = Arc::clone(&calls);
                async move {
                    calls.fetch_add(1, Ordering::SeqCst);
                    Ok(empty_response())
                }
            }
        })
        .await
        .expect("second lookup should not use an empty cache entry");

        assert!(second.is_none());
        assert_eq!(calls.load(Ordering::SeqCst), 2);
    }

    fn rate_limited_response() -> WhoisResponse {
        WhoisResponse {
            domain: "throttled.example".to_string(),
            whois_server: "whois.example.com".to_string(),
            raw_data: "Number of allowed queries exceeded.".to_string(),
            parsed_data: Some(ParsedWhoisData {
                registrar: Some("Example Registrar".to_string()),
                ..Default::default()
            }),
            lookup_status: LookupStatus::RateLimited,
            cached: false,
            query_time_ms: 1,
            parsing_analysis: None,
        }
    }

    fn not_found_response() -> WhoisResponse {
        WhoisResponse {
            domain: "missing.example".to_string(),
            whois_server: "RDAP: https://rdap.example.com".to_string(),
            raw_data: "Domain not found".to_string(),
            parsed_data: None,
            lookup_status: LookupStatus::NotFound,
            cached: false,
            query_time_ms: 1,
            parsing_analysis: None,
        }
    }

    #[tokio::test]
    async fn test_lookup_whois_does_not_cache_rate_limited() {
        let temp_dir = TempDir::new().expect("temp dir");
        let calls = Arc::new(AtomicUsize::new(0));

        let first = lookup_whois_with_lookup("throttled.example", Some(temp_dir.path()), {
            let calls = Arc::clone(&calls);
            move |_| {
                let calls = Arc::clone(&calls);
                async move {
                    calls.fetch_add(1, Ordering::SeqCst);
                    Ok(rate_limited_response())
                }
            }
        })
        .await
        .expect("lookup wrapper should not fail");
        assert!(first.is_none());

        let second = lookup_whois_with_lookup("throttled.example", Some(temp_dir.path()), {
            let calls = Arc::clone(&calls);
            move |_| {
                let calls = Arc::clone(&calls);
                async move {
                    calls.fetch_add(1, Ordering::SeqCst);
                    Ok(rate_limited_response())
                }
            }
        })
        .await
        .expect("rate-limited result must not be disk-cached");

        assert!(second.is_none());
        assert_eq!(calls.load(Ordering::SeqCst), 2);
    }

    #[tokio::test]
    async fn test_lookup_whois_returns_none_on_not_found() {
        let temp_dir = TempDir::new().expect("temp dir");
        let calls = Arc::new(AtomicUsize::new(0));

        let first = lookup_whois_with_lookup("missing.example", Some(temp_dir.path()), {
            let calls = Arc::clone(&calls);
            move |_| {
                let calls = Arc::clone(&calls);
                async move {
                    calls.fetch_add(1, Ordering::SeqCst);
                    Ok(not_found_response())
                }
            }
        })
        .await
        .expect("lookup wrapper should not fail");
        assert!(first.is_none());

        let second = lookup_whois_with_lookup("missing.example", Some(temp_dir.path()), {
            let calls = Arc::clone(&calls);
            move |_| {
                let calls = Arc::clone(&calls);
                async move {
                    calls.fetch_add(1, Ordering::SeqCst);
                    Ok(not_found_response())
                }
            }
        })
        .await
        .expect("not-found result must not be disk-cached");

        assert!(second.is_none());
        assert_eq!(calls.load(Ordering::SeqCst), 2);
    }

    #[tokio::test(start_paused = true)]
    async fn test_lookup_whois_returns_none_on_timeout() {
        let temp_dir = TempDir::new().expect("temp dir");
        let lookup = lookup_whois_with_lookup("example.com", Some(temp_dir.path()), |_| async {
            tokio::time::sleep(Duration::from_secs(crate::config::WHOIS_TIMEOUT_SECS + 1)).await;
            Ok(fake_response())
        });

        tokio::pin!(lookup);
        tokio::time::advance(Duration::from_secs(crate::config::WHOIS_TIMEOUT_SECS + 1)).await;

        let result = lookup.await.expect("wrapper should not fail");
        assert!(result.is_none());
    }
}