monovm-whois-rust 1.0.0

Domain WHOIS and RDAP lookups with availability detection, structured record parsing, referral chasing, caching and rate limiting.
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
//! The runtime-independent half of a lookup.
//!
//! Everything here is a pure function of data already in hand: which endpoints to
//! try and in what order, what to put on the wire, what a response means, and
//! whether a referral is worth following. The blocking and asynchronous clients
//! share all of it and differ only in how they wait for a socket — which keeps the
//! two from drifting apart, and makes the interesting decisions testable without a
//! network.

use crate::domain::Tld;
use crate::error::{Error, Result};
use crate::registry::{Endpoint, EndpointKind, Registry, Resolution};
use crate::transport::Query;

// Referral chasing, response interpretation and failure aggregation are steps in a
// client's lookup sequence, so they exist only when a client does. Endpoint ordering
// and the policy types above are useful on their own and are not gated.
#[cfg(any(feature = "blocking", feature = "async"))]
use crate::detect::{DetectionEngine, Evidence, Verdict};
#[cfg(any(feature = "blocking", feature = "async"))]
use crate::registry::WhoisEndpoint;
#[cfg(any(feature = "blocking", feature = "async"))]
use crate::transport::{referral_host, RawResponse};

/// Which protocol to try first when a registry publishes both.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Preference {
    /// Port 43 first, RDAP as a fallback.
    ///
    /// The default. Port 43 needs no TLS handshake, so it is the faster of the two,
    /// and the curated availability wording in this crate's data was written
    /// against those servers. RDAP still covers the registries that have no port 43
    /// service at all.
    #[default]
    Whois,

    /// RDAP first, port 43 as a fallback.
    ///
    /// Worth choosing when correctness matters more than latency: RDAP answers with
    /// a structured object and a real 404, so a verdict from it is
    /// [`Confidence::Definitive`](crate::detect::Confidence::Definitive) rather than
    /// a judgement about wording.
    Rdap,

    /// Whatever order the registry definition lists.
    RegistryOrder,

    /// Port 43 only.
    WhoisOnly,

    /// RDAP only.
    RdapOnly,
}

impl Preference {
    /// Order a registry's endpoints, dropping any this preference excludes.
    pub fn order(self, registry: &Registry) -> Vec<Endpoint> {
        let whois = || registry.endpoints_matching(EndpointKind::Whois).cloned();
        let rdap = || registry.endpoints_matching(EndpointKind::Rdap).cloned();

        match self {
            Preference::Whois => whois().chain(rdap()).collect(),
            Preference::Rdap => rdap().chain(whois()).collect(),
            Preference::RegistryOrder => registry.endpoints().to_vec(),
            Preference::WhoisOnly => whois().collect(),
            Preference::RdapOnly => rdap().collect(),
        }
    }
}

/// How far to chase a thin registry's referral.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ReferralPolicy {
    /// How many referrals to follow. `0` disables chasing.
    pub max_hops: u8,
    /// Whether to follow a referral even when the registry answer already looks
    /// complete.
    pub always: bool,
}

impl ReferralPolicy {
    /// Follow one referral, and only from a registry that answered thinly.
    ///
    /// One hop is enough for the gTLD structure that makes referrals necessary:
    /// registry points at registrar, and the registrar is authoritative. More hops
    /// mostly find loops.
    pub const DEFAULT: ReferralPolicy = ReferralPolicy {
        max_hops: 1,
        always: false,
    };

    /// Do not follow referrals.
    pub const NONE: ReferralPolicy = ReferralPolicy {
        max_hops: 0,
        always: false,
    };

    /// Follow up to `hops` referrals whenever one is offered.
    pub fn eager(hops: u8) -> Self {
        ReferralPolicy {
            max_hops: hops,
            always: true,
        }
    }

    /// Whether chasing is enabled at all.
    pub fn is_enabled(self) -> bool {
        self.max_hops > 0
    }
}

impl Default for ReferralPolicy {
    fn default() -> Self {
        ReferralPolicy::DEFAULT
    }
}

/// The endpoints to try for one name, in order.
#[derive(Debug, Clone)]
pub struct Plan {
    /// The registry match this plan came from.
    pub resolution: Resolution,
    /// Endpoints to attempt, best first.
    pub attempts: Vec<Endpoint>,
    /// What to send, in the form the registry wants.
    pub wire_name: String,
}

impl Plan {
    /// Work out how to look a resolved name up.
    ///
    /// # Errors
    ///
    /// [`Error::NoEndpoint`] when the preference excludes everything the registry
    /// publishes — asking for [`Preference::RdapOnly`] against a registry with only
    /// a port 43 service, say. The error names what was dropped, since otherwise
    /// this looks like an unsupported TLD.
    pub fn build(resolution: Resolution, preference: Preference) -> Result<Self> {
        let attempts = preference.order(&resolution.registry);

        if attempts.is_empty() {
            let available: Vec<String> = resolution
                .registry
                .endpoints()
                .iter()
                .map(Endpoint::to_string)
                .collect();

            return Err(Error::NoEndpoint {
                tld: resolution.tld.clone(),
                detail: if available.is_empty() {
                    "the registry definition lists no endpoint".to_string()
                } else {
                    format!(
                        "{preference:?} excludes every endpoint the registry has: {}",
                        available.join(", ")
                    )
                },
            });
        }

        let wire_name = resolution.wire_form();
        Ok(Plan {
            resolution,
            attempts,
            wire_name,
        })
    }

    /// The suffix being looked up.
    pub fn tld(&self) -> &Tld {
        &self.resolution.tld
    }

    /// A query for one of the planned endpoints.
    pub fn query(&self, endpoint: &Endpoint) -> Query {
        Query::new(
            endpoint.clone(),
            self.wire_name.clone(),
            self.resolution.tld.clone(),
        )
    }

    /// Every planned query, in order.
    pub fn queries(&self) -> Vec<Query> {
        self.attempts
            .iter()
            .map(|endpoint| self.query(endpoint))
            .collect()
    }
}

/// Whether to ask a registrar's server as well, and which one.
///
/// Returns `None` when there is nothing to follow: no referral in the record, the
/// policy says not to, the hop budget is spent, the referral points back somewhere
/// already visited, or the registry answer was already complete.
#[cfg(any(feature = "blocking", feature = "async"))]
pub fn next_referral(
    response: &RawResponse,
    registry: &Registry,
    policy: ReferralPolicy,
    visited: &[WhoisEndpoint],
) -> Option<WhoisEndpoint> {
    if !policy.is_enabled() || visited.len() > policy.max_hops as usize {
        return None;
    }

    // A referral costs a round trip. Spend it when the registry is known to answer
    // thinly, or when the caller asked for it unconditionally.
    if !policy.always && !registry.is_thin() {
        return None;
    }

    let host = referral_host(response.text())?;
    let endpoint = WhoisEndpoint::parse(&host).ok()?;

    // Do not loop back to a server already asked, including the one that just
    // answered — several registrars point at the registry that referred us.
    if visited
        .iter()
        .any(|seen| seen.host().eq_ignore_ascii_case(endpoint.host()))
    {
        return None;
    }

    Some(endpoint)
}

/// Read one response, in the light of which registry produced it.
#[cfg(any(feature = "blocking", feature = "async"))]
pub fn interpret(
    engine: &DetectionEngine,
    response: &RawResponse,
    resolution: &Resolution,
) -> Result<Verdict> {
    let evidence = Evidence::from_response(
        response,
        &resolution.tld,
        Some(resolution.registry.as_ref()),
    );
    engine.decide(&evidence)
}

/// Combine the failures from every attempted endpoint into one error.
///
/// A caller that tried three endpoints and got three different failures needs all
/// three to diagnose anything, and the last one is not usually the informative one.
#[cfg(any(feature = "blocking", feature = "async"))]
pub fn combined_failure(name: &str, mut failures: Vec<(Endpoint, Error)>) -> Error {
    // A definite answer that happens to arrive as an error — a refusal, a wrong
    // server — is more useful than a connection failure, so surface it if present.
    if let Some(index) = failures
        .iter()
        .position(|(_, error)| matches!(error, Error::Refused { .. }))
    {
        return failures.swap_remove(index).1;
    }

    let consulted = failures
        .iter()
        .map(|(endpoint, _)| endpoint.address())
        .collect::<Vec<_>>()
        .join(", ");
    let detail = failures
        .iter()
        .map(|(endpoint, error)| format!("{}: {error}", endpoint.address()))
        .collect::<Vec<_>>()
        .join("; ");

    Error::Inconclusive {
        domain: name.to_string(),
        consulted: if consulted.is_empty() {
            "no endpoint".to_string()
        } else {
            consulted
        },
        detail,
    }
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;
    use std::time::Duration;

    use super::*;
    use crate::domain::DomainName;
    use crate::registry::Registry;
    use crate::transport::ResponseKind;

    fn resolution(registry: Registry) -> Resolution {
        let tld = Tld::parse("com").unwrap();
        let name = DomainName::parse("example.com").unwrap();
        Resolution {
            queried: name.clone(),
            registrable: name,
            tld,
            registry: Arc::new(registry),
        }
    }

    fn both_protocols() -> Registry {
        Registry::builder([Tld::parse("com").unwrap()])
            .endpoint(Endpoint::whois("whois.verisign-grs.com"))
            .endpoint(Endpoint::rdap("https://rdap.verisign.com/com/v1/"))
            .build()
    }

    fn response(text: &str) -> RawResponse {
        RawResponse::new(
            Endpoint::whois("whois.verisign-grs.com"),
            ResponseKind::WhoisText,
            text,
            Duration::ZERO,
        )
    }

    // ------------------------------------------------------------- preference

    #[test]
    fn preference_orders_and_filters_endpoints() {
        let registry = both_protocols();

        let whois_first = Preference::Whois.order(&registry);
        assert!(whois_first[0].is_whois());
        assert!(whois_first[1].is_rdap());

        let rdap_first = Preference::Rdap.order(&registry);
        assert!(rdap_first[0].is_rdap());
        assert!(rdap_first[1].is_whois());

        assert_eq!(Preference::WhoisOnly.order(&registry).len(), 1);
        assert!(Preference::WhoisOnly.order(&registry)[0].is_whois());
        assert!(Preference::RdapOnly.order(&registry)[0].is_rdap());
        assert_eq!(
            Preference::RegistryOrder.order(&registry),
            registry.endpoints()
        );
    }

    #[test]
    fn the_default_preference_is_whois_first() {
        assert_eq!(Preference::default(), Preference::Whois);
    }

    // -------------------------------------------------------------------- plan

    #[test]
    fn a_plan_lists_the_endpoints_to_try() {
        let plan = Plan::build(resolution(both_protocols()), Preference::Whois).unwrap();

        assert_eq!(plan.attempts.len(), 2);
        assert_eq!(plan.wire_name, "example.com");
        assert_eq!(plan.tld().ascii(), "com");
        assert_eq!(plan.queries().len(), 2);
    }

    #[test]
    fn a_plan_that_excludes_everything_says_so() {
        let whois_only = Registry::builder([Tld::parse("com").unwrap()])
            .endpoint(Endpoint::whois("whois.example"))
            .build();

        let error = Plan::build(resolution(whois_only), Preference::RdapOnly).unwrap_err();
        match error {
            Error::NoEndpoint { detail, .. } => {
                assert!(detail.contains("RdapOnly"), "{detail}");
                assert!(detail.contains("whois.example"), "{detail}");
            }
            other => panic!("got {other:?}"),
        }
    }

    #[test]
    fn a_registry_with_no_endpoint_says_so() {
        let empty = Registry::builder([Tld::parse("com").unwrap()]).build();
        let error = Plan::build(resolution(empty), Preference::Whois).unwrap_err();

        match error {
            Error::NoEndpoint { detail, .. } => assert!(detail.contains("no endpoint"), "{detail}"),
            other => panic!("got {other:?}"),
        }
    }

    #[test]
    fn the_wire_name_follows_the_registrys_idn_preference() {
        let tld = Tld::parse("de").unwrap();
        let name = DomainName::parse("münchen.de").unwrap();
        let registry = Registry::builder([tld.clone()])
            .endpoint(Endpoint::whois("whois.denic.de"))
            .idn_form(crate::registry::IdnForm::Unicode)
            .build();

        let plan = Plan::build(
            Resolution {
                queried: name.clone(),
                registrable: name,
                tld,
                registry: Arc::new(registry),
            },
            Preference::Whois,
        )
        .unwrap();

        assert_eq!(plan.wire_name, "münchen.de");
    }

    // ---------------------------------------------------------------- referral
    //
    // Referral chasing and failure aggregation exist to serve a client, and are
    // compiled only when one is — so their tests are gated the same way.
    #[cfg(any(feature = "blocking", feature = "async"))]
    mod client_driven {
        use super::*;

        const THIN_ANSWER: &str = "\
Domain Name: EXAMPLE.COM
Registrar: Example Registrar, LLC
Registrar WHOIS Server: whois.example-registrar.com
";

        #[test]
        fn a_thin_registry_referral_is_followed() {
            let registry = Registry::builder([Tld::parse("com").unwrap()])
                .thin(true)
                .build();
            let visited = vec![WhoisEndpoint::new("whois.verisign-grs.com", 43)];

            let next = next_referral(
                &response(THIN_ANSWER),
                &registry,
                ReferralPolicy::DEFAULT,
                &visited,
            );
            assert_eq!(next.unwrap().host(), "whois.example-registrar.com");
        }

        #[test]
        fn a_thick_registry_referral_is_not_followed_by_default() {
            let thick = Registry::builder([Tld::parse("com").unwrap()]).build();

            assert!(
                next_referral(&response(THIN_ANSWER), &thick, ReferralPolicy::DEFAULT, &[])
                    .is_none()
            );
            // Unless the caller asks for it unconditionally.
            assert!(next_referral(
                &response(THIN_ANSWER),
                &thick,
                ReferralPolicy::eager(1),
                &[]
            )
            .is_some());
        }

        #[test]
        fn referral_chasing_can_be_switched_off() {
            let thin = Registry::builder([Tld::parse("com").unwrap()])
                .thin(true)
                .build();
            assert!(
                next_referral(&response(THIN_ANSWER), &thin, ReferralPolicy::NONE, &[]).is_none()
            );
            assert!(!ReferralPolicy::NONE.is_enabled());
        }

        #[test]
        fn the_hop_budget_is_respected() {
            let thin = Registry::builder([Tld::parse("com").unwrap()])
                .thin(true)
                .build();
            // Two servers already asked exceeds a one-hop budget.
            let visited = vec![
                WhoisEndpoint::new("whois.verisign-grs.com", 43),
                WhoisEndpoint::new("whois.first-registrar.com", 43),
            ];

            assert!(next_referral(
                &response(THIN_ANSWER),
                &thin,
                ReferralPolicy::DEFAULT,
                &visited
            )
            .is_none());
        }

        #[test]
        fn a_referral_loop_is_not_followed() {
            let thin = Registry::builder([Tld::parse("com").unwrap()])
                .thin(true)
                .build();
            let visited = vec![WhoisEndpoint::new("whois.example-registrar.com", 43)];

            assert!(
                next_referral(
                    &response(THIN_ANSWER),
                    &thin,
                    ReferralPolicy::DEFAULT,
                    &visited
                )
                .is_none(),
                "a referral back to a server already asked must not be followed"
            );
        }

        #[test]
        fn a_record_with_no_referral_ends_the_chain() {
            let thin = Registry::builder([Tld::parse("com").unwrap()])
                .thin(true)
                .build();
            let complete = response("Domain Name: EXAMPLE.DE\nStatus: connect\n");

            assert!(next_referral(&complete, &thin, ReferralPolicy::DEFAULT, &[]).is_none());
        }

        // ------------------------------------------------------- combined failure

        #[test]
        fn a_refusal_outranks_a_connection_failure() {
            let failures = vec![
                (
                    Endpoint::whois("a.example"),
                    Error::Connect {
                        server: "a.example".into(),
                        source: std::io::Error::other("no route"),
                    },
                ),
                (
                    Endpoint::rdap("https://b.example/"),
                    Error::Refused {
                        server: "b.example".into(),
                        reason: crate::error::Refusal::RateLimited,
                    },
                ),
            ];

            let combined = combined_failure("example.com", failures);
            assert!(
                matches!(combined, Error::Refused { .. }),
                "a definite refusal is more useful than a transport failure: got {combined:?}"
            );
        }

        #[test]
        fn transport_failures_are_reported_together() {
            let failures = vec![
                (
                    Endpoint::whois("a.example"),
                    Error::Timeout {
                        server: "a.example".into(),
                        elapsed: Duration::from_secs(5),
                    },
                ),
                (
                    Endpoint::rdap("https://b.example/"),
                    Error::Http {
                        url: "https://b.example/".into(),
                        status: 500,
                    },
                ),
            ];

            match combined_failure("example.com", failures) {
                Error::Inconclusive {
                    consulted, detail, ..
                } => {
                    assert!(consulted.contains("a.example"), "{consulted}");
                    assert!(consulted.contains("b.example"), "{consulted}");
                    assert!(detail.contains("500"), "{detail}");
                }
                other => panic!("got {other:?}"),
            }
        }

        #[test]
        fn no_failures_at_all_still_produces_a_usable_error() {
            match combined_failure("example.com", Vec::new()) {
                Error::Inconclusive { consulted, .. } => assert_eq!(consulted, "no endpoint"),
                other => panic!("got {other:?}"),
            }
        }
    }
}