domain-check-lib 1.0.2

A fast, robust library for checking domain availability using RDAP and WHOIS protocols
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
//! WHOIS protocol implementation for domain availability checking.
//!
//! This module provides WHOIS-based domain checking as a fallback when RDAP is not available.
//! WHOIS is the traditional protocol for domain registration data, though it provides
//! unstructured text responses that require parsing.

use crate::error::DomainCheckError;
use crate::types::{CheckMethod, DomainResult};
use std::time::{Duration, Instant};
use tokio::process::Command;

/// WHOIS client for checking domain availability using the system's whois command.
///
/// This client uses the system's `whois` command-line tool to query domain information.
/// It's designed as a fallback when RDAP is not available or fails.
#[derive(Clone)]
pub struct WhoisClient {
    /// Timeout for WHOIS requests
    timeout: Duration,
}

impl WhoisClient {
    /// Create a new WHOIS client with default settings.
    pub fn new() -> Self {
        Self {
            timeout: Duration::from_secs(5),
        }
    }

    /// Create a new WHOIS client with custom timeout.
    pub fn with_timeout(timeout: Duration) -> Self {
        Self { timeout }
    }

    /// Check domain availability using WHOIS.
    ///
    /// This method executes the system's `whois` command and parses the output
    /// to determine if a domain is available or taken.
    ///
    /// # Arguments
    ///
    /// * `domain` - The domain name to check (e.g., "example.com")
    ///
    /// # Returns
    ///
    /// A `DomainResult` with availability status. Note that WHOIS typically
    /// doesn't provide structured registration details like RDAP.
    ///
    /// # Errors
    ///
    /// Returns `DomainCheckError` if:
    /// - The `whois` command is not available on the system
    /// - The WHOIS query times out
    /// - The WHOIS response cannot be parsed
    pub async fn check_domain(&self, domain: &str) -> Result<DomainResult, DomainCheckError> {
        let start_time = Instant::now();

        // Execute WHOIS command with timeout
        let result = tokio::time::timeout(self.timeout, self.execute_whois_command(domain)).await;

        let check_duration = start_time.elapsed();

        match result {
            Ok(Ok(available)) => {
                Ok(DomainResult {
                    domain: domain.to_string(),
                    available: Some(available),
                    info: None, // WHOIS parsing for detailed info is complex and inconsistent
                    check_duration: Some(check_duration),
                    method_used: CheckMethod::Whois,
                    error_message: None,
                })
            }
            Ok(Err(e)) => Err(e),
            Err(_) => Err(DomainCheckError::timeout("WHOIS query", self.timeout)),
        }
    }

    /// Check domain availability using WHOIS with a specific server.
    ///
    /// This method uses `whois -h <server> <domain>` for a targeted query,
    /// falling back to bare `whois <domain>` if the `-h` flag fails.
    ///
    /// # Arguments
    ///
    /// * `domain` - The domain name to check (e.g., "example.com")
    /// * `server` - The WHOIS server hostname (e.g., "whois.verisign-grs.com")
    pub async fn check_domain_with_server(
        &self,
        domain: &str,
        server: &str,
    ) -> Result<DomainResult, DomainCheckError> {
        let start_time = Instant::now();

        let result = tokio::time::timeout(
            self.timeout,
            self.execute_whois_command_with_server(domain, server),
        )
        .await;

        let check_duration = start_time.elapsed();

        match result {
            Ok(Ok(available)) => Ok(DomainResult {
                domain: domain.to_string(),
                available: Some(available),
                info: None,
                check_duration: Some(check_duration),
                method_used: CheckMethod::Whois,
                error_message: None,
            }),
            Ok(Err(_)) => {
                // Targeted query failed, fall back to bare whois
                self.check_domain(domain).await
            }
            Err(_) => Err(DomainCheckError::timeout("WHOIS query", self.timeout)),
        }
    }

    /// Execute the system whois command and parse the result.
    async fn execute_whois_command(&self, domain: &str) -> Result<bool, DomainCheckError> {
        // First attempt
        let output = Command::new("whois")
            .arg(domain)
            .output()
            .await
            .map_err(|e| {
                DomainCheckError::whois(
                    domain,
                    format!(
                        "Failed to execute whois command: {}. Make sure 'whois' is installed.",
                        e
                    ),
                )
            })?;

        let output_text = String::from_utf8_lossy(&output.stdout).to_lowercase();

        // Check for rate limiting first
        if self.is_rate_limited(&output_text) {
            // Wait and retry once
            tokio::time::sleep(Duration::from_millis(1000)).await;

            let retry_output = Command::new("whois")
                .arg(domain)
                .output()
                .await
                .map_err(|e| {
                    DomainCheckError::whois(domain, format!("Failed to execute whois retry: {}", e))
                })?;

            let retry_text = String::from_utf8_lossy(&retry_output.stdout).to_lowercase();
            self.parse_whois_availability(&retry_text)
        } else {
            self.parse_whois_availability(&output_text)
        }
    }

    /// Execute whois command with a specific server (-h flag).
    async fn execute_whois_command_with_server(
        &self,
        domain: &str,
        server: &str,
    ) -> Result<bool, DomainCheckError> {
        let output = Command::new("whois")
            .arg("-h")
            .arg(server)
            .arg(domain)
            .output()
            .await
            .map_err(|e| {
                DomainCheckError::whois(
                    domain,
                    format!("Failed to execute whois -h {} command: {}", server, e),
                )
            })?;

        let output_text = String::from_utf8_lossy(&output.stdout).to_lowercase();

        if self.is_rate_limited(&output_text) {
            tokio::time::sleep(Duration::from_millis(1000)).await;

            let retry_output = Command::new("whois")
                .arg("-h")
                .arg(server)
                .arg(domain)
                .output()
                .await
                .map_err(|e| {
                    DomainCheckError::whois(domain, format!("Failed to execute whois retry: {}", e))
                })?;

            let retry_text = String::from_utf8_lossy(&retry_output.stdout).to_lowercase();
            self.parse_whois_availability(&retry_text)
        } else {
            self.parse_whois_availability(&output_text)
        }
    }

    /// Parse WHOIS output to determine domain availability.
    ///
    /// This function looks for common patterns in WHOIS responses that indicate
    /// whether a domain is available or taken. WHOIS responses vary significantly
    /// between registries, so this uses a comprehensive set of patterns.
    fn parse_whois_availability(&self, whois_output: &str) -> Result<bool, DomainCheckError> {
        let output_lower = whois_output.to_lowercase();

        // First check for invalid TLD or server errors
        let invalid_tld_patterns = [
            "no whois server is known",
            "no whois server",
            "invalid tld",
            "unknown tld",
            "tld not found",
            "no such tld",
            "bad tld",
            "invalid domain extension",
        ];

        for pattern in &invalid_tld_patterns {
            if output_lower.contains(pattern) {
                return Err(DomainCheckError::bootstrap(
                    "unknown",
                    "Invalid or unsupported TLD for WHOIS lookup",
                ));
            }
        }

        // Patterns that typically indicate domain availability
        let available_patterns = [
            "no match",
            "not found",
            "no data found",
            "no entries found",
            "domain not found",
            "domain available",
            "status: available",
            "status: free",
            "no information available",
            "not registered",
            "no matching record",
            "domain status: no object found",
            "the queried object does not exist",
            "object does not exist",
            "no matching entry",
            "domain name not found",
            "this domain name has not been registered",
            "no found",
        ];

        // Patterns that indicate the domain is definitely taken
        let taken_patterns = [
            "domain status:",
            "registrar:",
            "creation date:",
            "created:",
            "registry domain id:",
            "registrant:",
            "admin contact:",
            "tech contact:",
            "name server:",
            "nameservers:",
            "expiry date:",
            "expires:",
            "updated:",
            "last updated:",
        ];

        // Check for availability patterns first (more specific)
        for pattern in &available_patterns {
            if output_lower.contains(pattern) {
                return Ok(true);
            }
        }

        // Check for taken patterns
        let taken_pattern_count = taken_patterns
            .iter()
            .filter(|pattern| output_lower.contains(*pattern))
            .count();

        // If we found multiple "taken" indicators, the domain is likely taken
        if taken_pattern_count >= 2 {
            return Ok(false);
        }

        // If the output is very short and doesn't look like an error,
        // it might indicate availability. But skip this heuristic if the
        // response contains error-like words (e.g. rate limit messages).
        let trimmed = output_lower.trim();
        if trimmed.len() < 50
            && !trimmed.contains("exceeded")
            && !trimmed.contains("error")
            && !trimmed.contains("denied")
            && !trimmed.contains("refused")
        {
            return Ok(true);
        }

        // For truly ambiguous cases, return an error instead of guessing
        // This prevents false positives for invalid domains
        Err(DomainCheckError::whois(
            "unknown",
            "Unable to determine domain status from WHOIS response",
        ))
    }

    /// Check if the WHOIS output indicates rate limiting.
    fn is_rate_limited(&self, output: &str) -> bool {
        let output_lower = output.to_lowercase();
        let rate_limit_patterns = [
            "rate limit exceeded",
            "too many requests",
            "try again later",
            "quota exceeded",
            "limit exceeded",
            "queries exceeded",
            "number of allowed queries",
            "throttled",
            "blocked",
            "rate-limited",
            "too many requests from your ip",
        ];

        rate_limit_patterns
            .iter()
            .any(|pattern| output_lower.contains(pattern))
    }
}

impl Default for WhoisClient {
    fn default() -> Self {
        Self::new()
    }
}

/// Discover the authoritative WHOIS server for a TLD via IANA referral.
///
/// Uses the system `whois` command to query `whois.iana.org` for the TLD,
/// then parses the response for a `refer:` line containing the authoritative
/// WHOIS server hostname.
///
/// # Arguments
///
/// * `tld` - The TLD to look up (e.g., "com", "co", "museum")
///
/// # Returns
///
/// The WHOIS server hostname (e.g., "whois.verisign-grs.com"), or None if
/// no referral was found or the query failed.
pub async fn discover_whois_server(tld: &str) -> Option<String> {
    let result = tokio::time::timeout(Duration::from_secs(10), async {
        let output = Command::new("whois")
            .arg("-h")
            .arg("whois.iana.org")
            .arg(tld)
            .output()
            .await
            .ok()?;

        let response = String::from_utf8_lossy(&output.stdout);
        parse_iana_refer_response(&response)
    })
    .await;

    result.unwrap_or(None)
}

/// Parse an IANA WHOIS response for the authoritative WHOIS server.
///
/// The IANA WHOIS response may use either `refer:` or `whois:` to indicate
/// the authoritative WHOIS server for a TLD. We check both fields, preferring
/// `refer:` when present.
///
/// ```text
/// whois:        whois.verisign-grs.com
/// refer:        whois.verisign-grs.com
/// ```
fn parse_iana_refer_response(response: &str) -> Option<String> {
    let mut whois_server = None;

    for line in response.lines() {
        let line_trimmed = line.trim();
        if let Some(server) = line_trimmed.strip_prefix("refer:") {
            let server = server.trim();
            if !server.is_empty() {
                // `refer:` is the canonical field — return immediately
                return Some(server.to_string());
            }
        } else if let Some(server) = line_trimmed.strip_prefix("whois:") {
            let server = server.trim();
            if !server.is_empty() {
                whois_server = Some(server.to_string());
            }
        }
    }

    whois_server
}

/// Check if the system has a working whois command.
///
/// This function can be used to verify that WHOIS functionality is available
/// before attempting to use the WhoisClient.
///
/// # Returns
///
/// `true` if the whois command is available and working, `false` otherwise.
#[allow(dead_code)]
pub async fn is_whois_available() -> bool {
    match Command::new("whois").arg("--version").output().await {
        Ok(output) => output.status.success(),
        Err(_) => {
            // Try with a different flag that's more universal
            (Command::new("whois").arg("example.com").output().await).is_ok()
        }
    }
}

/// Get the version of the system's whois command.
///
/// This is useful for debugging and ensuring compatibility.
#[allow(dead_code)]
pub async fn get_whois_version() -> Result<String, DomainCheckError> {
    let output = Command::new("whois")
        .arg("--version")
        .output()
        .await
        .map_err(|e| {
            DomainCheckError::whois("version", format!("Failed to get whois version: {}", e))
        })?;

    if output.status.success() {
        Ok(String::from_utf8_lossy(&output.stdout).trim().to_string())
    } else {
        Ok("Unknown whois version".to_string())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // ── WhoisClient creation ────────────────────────────────────────────

    #[test]
    fn test_whois_client_new() {
        let client = WhoisClient::new();
        assert_eq!(client.timeout, Duration::from_secs(5));
    }

    #[test]
    fn test_whois_client_with_timeout() {
        let client = WhoisClient::with_timeout(Duration::from_secs(10));
        assert_eq!(client.timeout, Duration::from_secs(10));
    }

    #[test]
    fn test_whois_client_default() {
        let client = WhoisClient::default();
        assert_eq!(client.timeout, Duration::from_secs(5));
    }

    // ── parse_whois_availability: available patterns ────────────────────

    #[test]
    fn test_available_no_match() {
        let client = WhoisClient::new();
        assert!(client
            .parse_whois_availability("No match for domain")
            .unwrap());
    }

    #[test]
    fn test_available_not_found() {
        let client = WhoisClient::new();
        assert!(client
            .parse_whois_availability("Not found: example.com")
            .unwrap());
    }

    #[test]
    fn test_available_domain_not_found() {
        let client = WhoisClient::new();
        assert!(client.parse_whois_availability("Domain not found").unwrap());
    }

    #[test]
    fn test_available_no_data_found() {
        let client = WhoisClient::new();
        assert!(client
            .parse_whois_availability("No data found for this query")
            .unwrap());
    }

    #[test]
    fn test_available_no_entries_found() {
        let client = WhoisClient::new();
        assert!(client.parse_whois_availability("No entries found").unwrap());
    }

    #[test]
    fn test_available_domain_available() {
        let client = WhoisClient::new();
        assert!(client
            .parse_whois_availability("Domain available for registration")
            .unwrap());
    }

    #[test]
    fn test_available_status_free() {
        let client = WhoisClient::new();
        assert!(client.parse_whois_availability("Status: free").unwrap());
    }

    #[test]
    fn test_available_not_registered() {
        let client = WhoisClient::new();
        assert!(client
            .parse_whois_availability("This domain is not registered")
            .unwrap());
    }

    #[test]
    fn test_available_object_does_not_exist() {
        let client = WhoisClient::new();
        assert!(client
            .parse_whois_availability("The queried object does not exist")
            .unwrap());
    }

    #[test]
    fn test_available_no_found() {
        let client = WhoisClient::new();
        assert!(client.parse_whois_availability("No found").unwrap());
    }

    #[test]
    fn test_available_case_insensitive() {
        let client = WhoisClient::new();
        assert!(client
            .parse_whois_availability("NO MATCH FOR DOMAIN")
            .unwrap());
        assert!(client.parse_whois_availability("DOMAIN NOT FOUND").unwrap());
    }

    // ── parse_whois_availability: taken patterns ────────────────────────

    #[test]
    fn test_taken_multiple_indicators() {
        let client = WhoisClient::new();
        let taken = "Domain Status: clientTransferProhibited\nRegistrar: GoDaddy\nCreation Date: 2020-01-01";
        assert!(!client.parse_whois_availability(taken).unwrap());
    }

    #[test]
    fn test_taken_registrar_and_nameserver() {
        let client = WhoisClient::new();
        let taken = "Registrar: MarkMonitor Inc.\nName Server: ns1.google.com";
        assert!(!client.parse_whois_availability(taken).unwrap());
    }

    #[test]
    fn test_taken_created_and_expires() {
        let client = WhoisClient::new();
        let taken = "Created: 2015-01-01\nExpires: 2025-01-01";
        assert!(!client.parse_whois_availability(taken).unwrap());
    }

    #[test]
    fn test_taken_single_indicator_not_enough() {
        let client = WhoisClient::new();
        // Only one "taken" pattern — needs >= 2 to confirm taken
        let ambiguous = "Registrar: SomeRegistrar\nSome other random text that is long enough to exceed fifty characters";
        // Single taken indicator + long text = error (ambiguous)
        let result = client.parse_whois_availability(ambiguous);
        assert!(result.is_err());
    }

    // ── parse_whois_availability: invalid TLD patterns ──────────────────

    #[test]
    fn test_invalid_tld_no_whois_server() {
        let client = WhoisClient::new();
        let result = client.parse_whois_availability("No whois server is known for this TLD");
        assert!(result.is_err());
    }

    #[test]
    fn test_invalid_tld_unknown() {
        let client = WhoisClient::new();
        let result = client.parse_whois_availability("Unknown TLD: .fakext");
        assert!(result.is_err());
    }

    #[test]
    fn test_invalid_tld_bad() {
        let client = WhoisClient::new();
        let result = client.parse_whois_availability("Bad TLD specified in query");
        assert!(result.is_err());
    }

    // ── parse_whois_availability: short output = available ──────────────

    #[test]
    fn test_short_output_considered_available() {
        let client = WhoisClient::new();
        // < 50 chars, no patterns matched = available
        assert!(client.parse_whois_availability("Some short text").unwrap());
    }

    #[test]
    fn test_empty_output_considered_available() {
        let client = WhoisClient::new();
        assert!(client.parse_whois_availability("").unwrap());
    }

    // ── parse_whois_availability: ambiguous = error ─────────────────────

    #[test]
    fn test_ambiguous_output_returns_error() {
        let client = WhoisClient::new();
        // Long text, no available or taken patterns matched with >= 2 hits
        let ambiguous = "This is some random whois response that doesn't match any known pattern and is longer than fifty characters total";
        let result = client.parse_whois_availability(ambiguous);
        assert!(result.is_err());
        // Display renders as "WHOIS lookup failed" for generic errors
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("WHOIS lookup failed"));
    }

    // ── is_rate_limited ─────────────────────────────────────────────────

    #[test]
    fn test_rate_limited_patterns() {
        let client = WhoisClient::new();
        assert!(client.is_rate_limited("Rate limit exceeded. Try again later."));
        assert!(client.is_rate_limited("Too many requests from your IP."));
        assert!(client.is_rate_limited("Quota exceeded for this connection"));
        assert!(client.is_rate_limited("Request throttled, please wait"));
        assert!(client.is_rate_limited("Your IP has been blocked"));
        assert!(client.is_rate_limited("You have been rate-limited"));
    }

    #[test]
    fn test_rate_limited_case_insensitive() {
        let client = WhoisClient::new();
        assert!(client.is_rate_limited("RATE LIMIT EXCEEDED"));
        assert!(client.is_rate_limited("Too Many Requests"));
    }

    #[test]
    fn test_not_rate_limited() {
        let client = WhoisClient::new();
        assert!(!client.is_rate_limited("Normal whois response"));
        assert!(!client.is_rate_limited("Domain Status: active"));
        assert!(!client.is_rate_limited(""));
    }

    // ── parse_iana_refer_response ───────────────────────────────────────

    #[test]
    fn test_iana_refer_standard() {
        let response =
            "% IANA WHOIS server\n\nrefer:        whois.verisign-grs.com\n\ndomain:       COM\n";
        assert_eq!(
            parse_iana_refer_response(response),
            Some("whois.verisign-grs.com".to_string())
        );
    }

    #[test]
    fn test_iana_refer_none() {
        let response = "% IANA WHOIS server\ndomain: TEST\nstatus: ACTIVE\n";
        assert_eq!(parse_iana_refer_response(response), None);
    }

    #[test]
    fn test_iana_refer_empty_value() {
        let response = "refer:        \ndomain: COM\n";
        assert_eq!(parse_iana_refer_response(response), None);
    }

    #[test]
    fn test_iana_whois_field_fallback() {
        let response = "whois:        whois.verisign-grs.com\ndomain: COM\n";
        assert_eq!(
            parse_iana_refer_response(response),
            Some("whois.verisign-grs.com".to_string())
        );
    }

    #[test]
    fn test_iana_refer_takes_precedence_over_whois() {
        let response =
            "whois:        whois.old-server.com\nrefer:        whois.correct-server.com\n";
        assert_eq!(
            parse_iana_refer_response(response),
            Some("whois.correct-server.com".to_string())
        );
    }

    #[test]
    fn test_iana_empty_whois_field() {
        let response = "whois:        \ndomain: COM\n";
        assert_eq!(parse_iana_refer_response(response), None);
    }

    #[test]
    fn test_iana_empty_response() {
        assert_eq!(parse_iana_refer_response(""), None);
    }

    // ── Network-dependent test ──────────────────────────────────────────

    #[tokio::test]
    async fn test_whois_availability_check() {
        if is_whois_available().await {
            let client = WhoisClient::new();
            let result = client.check_domain("google.com").await;
            assert!(result.is_ok());
        }
    }
}