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
//! Talking to servers: the only part of the crate that performs I/O.
//!
//! A [`Transport`] turns a [`Query`] into a [`RawResponse`] and does nothing
//! else. It does not decide whether a domain is available, does not parse the
//! record, and does not choose which endpoint to ask — those live in
//! [`detect`](crate::detect), [`parser`](crate::parser) and
//! [`client`](crate::client) respectively.
//!
//! # Composing behaviour
//!
//! Retries, rate limiting and caching are each a transport that wraps another
//! transport, so a caller assembles the policy they want instead of configuring
//! flags on one large object:
//!
//! ```
//! # #[cfg(feature = "blocking")] {
//! use std::time::Duration;
//! use monovm_whois::cache::MemoryCache;
//! use monovm_whois::transport::{
//!     CachingTransport, RetryPolicy, RetryTransport, ThrottlePolicy, ThrottleTransport,
//!     Whois43Transport,
//! };
//!
//! let transport = Whois43Transport::default();
//! let transport = RetryTransport::new(transport, RetryPolicy::default());
//! let transport = ThrottleTransport::new(transport, ThrottlePolicy::per_host(Duration::from_secs(1)));
//! let transport = CachingTransport::new(transport, MemoryCache::with_ttl(Duration::from_secs(300)));
//! # }
//! ```
//!
//! Each layer only knows about the one below it, so the order is the caller's
//! choice and means something: caching outside throttling serves repeats without
//! waiting, caching inside it would wait first.

use std::fmt;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::time::Duration;

use crate::domain::Tld;
use crate::error::{Error, Result};
use crate::registry::Endpoint;

mod caching;
mod retry;
mod throttle;

#[cfg(any(test, feature = "mock"))]
mod mock;
// RDAP needs a runtime to be reachable from; without one there is nothing to compile
// it for.
#[cfg(all(feature = "rdap", any(feature = "blocking", feature = "async")))]
mod rdap;
mod whois43;

pub use caching::CachingTransport;
pub use retry::{RetryPolicy, RetryTransport};
pub use throttle::{ThrottlePolicy, ThrottleTransport};
pub use whois43::referral_host;

#[cfg(any(test, feature = "mock"))]
pub use mock::{MockTransport, Scripted};
#[cfg(all(feature = "rdap", feature = "blocking"))]
pub use rdap::RdapTransport;
#[cfg(feature = "blocking")]
pub use whois43::Whois43Transport;

#[cfg(feature = "async")]
pub use caching::AsyncCachingTransport;
#[cfg(all(feature = "async", feature = "rdap"))]
pub use rdap::AsyncRdapTransport;
#[cfg(feature = "async")]
pub use retry::AsyncRetryTransport;
#[cfg(feature = "async")]
pub use throttle::AsyncThrottleTransport;
#[cfg(feature = "async")]
pub use whois43::AsyncWhois43Transport;

/// A boxed future, as returned by [`AsyncTransport::fetch`].
///
/// Spelled out rather than pulled in from `async-trait` so that the trait stays
/// object-safe without adding a proc-macro dependency.
pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;

/// One question, addressed to one endpoint.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Query {
    /// Where to send it.
    pub endpoint: Endpoint,
    /// Exactly what to put on the wire, or into the URL path.
    ///
    /// Already in the form the registry wants — punycode or Unicode, and with
    /// any registry-specific query flags applied. A transport sends this
    /// verbatim; deciding what it should be is the client's job.
    pub wire_name: String,
    /// The suffix being asked about, carried for diagnostics and cache keys.
    pub tld: Tld,
}

impl Query {
    /// Build a query.
    pub fn new(endpoint: Endpoint, wire_name: impl Into<String>, tld: Tld) -> Self {
        Query {
            endpoint,
            wire_name: wire_name.into(),
            tld,
        }
    }

    /// A stable key identifying this exact question.
    pub fn cache_key(&self) -> String {
        format!("{}|{}", self.endpoint.address(), self.wire_name)
    }
}

/// Which protocol produced a response, and therefore how to read it.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ResponseKind {
    /// Line-oriented text from a port 43 server.
    WhoisText,
    /// A JSON document from an RDAP server.
    RdapJson,
}

/// What a server said, before any interpretation.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RawResponse {
    endpoint: Endpoint,
    kind: ResponseKind,
    text: String,
    elapsed: Duration,
    from_cache: bool,
}

impl RawResponse {
    /// Build a response.
    pub fn new(
        endpoint: Endpoint,
        kind: ResponseKind,
        text: impl Into<String>,
        elapsed: Duration,
    ) -> Self {
        RawResponse {
            endpoint,
            kind,
            text: text.into(),
            elapsed,
            from_cache: false,
        }
    }

    /// The endpoint that answered.
    pub fn endpoint(&self) -> &Endpoint {
        &self.endpoint
    }

    /// Which protocol produced this.
    pub fn kind(&self) -> ResponseKind {
        self.kind
    }

    /// The body, exactly as received.
    pub fn text(&self) -> &str {
        &self.text
    }

    /// How long the round trip took.
    pub fn elapsed(&self) -> Duration {
        self.elapsed
    }

    /// Whether this came from a cache rather than the network.
    pub fn is_cached(&self) -> bool {
        self.from_cache
    }

    /// Mark as served from a cache. Used by [`CachingTransport`].
    pub(crate) fn mark_cached(mut self) -> Self {
        self.from_cache = true;
        self
    }

    /// Whether the body has any non-whitespace content.
    pub fn is_blank(&self) -> bool {
        self.text.trim().is_empty()
    }
}

/// A synchronous conversation with a server.
///
/// The trait itself is always available, so a caller can implement it against a
/// private protocol even in a build with no bundled transports compiled in.
pub trait Transport: fmt::Debug + Send + Sync {
    /// Whether this transport can talk to that endpoint.
    ///
    /// Checked before [`fetch`](Transport::fetch) so a router can dispatch, and
    /// so a caller gets [`Error::NoEndpoint`] rather than a protocol error when
    /// nothing can serve a registry.
    fn supports(&self, endpoint: &Endpoint) -> bool;

    /// Ask the question and return the answer verbatim.
    ///
    /// # Errors
    ///
    /// Network faults as [`Error::Connect`], [`Error::Io`] or [`Error::Timeout`];
    /// an empty body as [`Error::EmptyResponse`]. Note that an HTTP 404 from RDAP
    /// is *not* an error: it is how RDAP says a domain does not exist, and the
    /// body is returned so the detector can read it.
    fn fetch(&self, query: &Query) -> Result<RawResponse>;

    /// A short name for diagnostics, e.g. `whois43` or `retry(whois43)`.
    fn name(&self) -> String;
}

/// An asynchronous conversation with a server.
///
/// Mirrors [`Transport`] exactly; the two are separate traits rather than one
/// generic trait because the sync and async paths have genuinely different
/// signatures, and unifying them would put a runtime dependency in the blocking
/// build.
pub trait AsyncTransport: fmt::Debug + Send + Sync {
    /// Whether this transport can talk to that endpoint.
    fn supports(&self, endpoint: &Endpoint) -> bool;

    /// Ask the question and return the answer verbatim.
    fn fetch<'a>(&'a self, query: &'a Query) -> BoxFuture<'a, Result<RawResponse>>;

    /// A short name for diagnostics.
    fn name(&self) -> String;
}

impl<T: Transport + ?Sized> Transport for Arc<T> {
    fn supports(&self, endpoint: &Endpoint) -> bool {
        (**self).supports(endpoint)
    }

    fn fetch(&self, query: &Query) -> Result<RawResponse> {
        (**self).fetch(query)
    }

    fn name(&self) -> String {
        (**self).name()
    }
}

impl<T: AsyncTransport + ?Sized> AsyncTransport for Arc<T> {
    fn supports(&self, endpoint: &Endpoint) -> bool {
        (**self).supports(endpoint)
    }

    fn fetch<'a>(&'a self, query: &'a Query) -> BoxFuture<'a, Result<RawResponse>> {
        (**self).fetch(query)
    }

    fn name(&self) -> String {
        (**self).name()
    }
}

/// Timeouts shared by every transport.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TransportConfig {
    /// How long to wait for the connection to be established.
    pub connect_timeout: Duration,
    /// How long to wait for data once connected.
    pub read_timeout: Duration,
    /// Cap on how many bytes to read from one response.
    ///
    /// A WHOIS record is a few kilobytes; a server that streams megabytes is
    /// either broken or hostile, and neither is worth buffering.
    pub max_response_bytes: usize,
}

impl TransportConfig {
    /// 5 s to connect, 10 s to read, 1 MiB cap.
    pub const DEFAULT: TransportConfig = TransportConfig {
        connect_timeout: Duration::from_secs(5),
        read_timeout: Duration::from_secs(10),
        max_response_bytes: 1024 * 1024,
    };

    /// Set the connect timeout.
    pub fn connect_timeout(mut self, timeout: Duration) -> Self {
        self.connect_timeout = timeout;
        self
    }

    /// Set the read timeout.
    pub fn read_timeout(mut self, timeout: Duration) -> Self {
        self.read_timeout = timeout;
        self
    }

    /// Set the response size cap.
    pub fn max_response_bytes(mut self, bytes: usize) -> Self {
        self.max_response_bytes = bytes;
        self
    }
}

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

/// Dispatches to whichever transport can serve an endpoint.
///
/// A registry may publish a port 43 host and an RDAP URL; the client hands both
/// to one router rather than working out which object to call.
///
/// ```
/// # #[cfg(all(feature = "blocking", feature = "rdap"))] {
/// use monovm_whois::registry::Endpoint;
/// use monovm_whois::transport::{RdapTransport, Router, Whois43Transport};
///
/// let router = Router::new()
///     .with(Whois43Transport::default())
///     .with(RdapTransport::new().unwrap());
///
/// assert!(router.handles(&Endpoint::whois("whois.nic.uk")));
/// assert!(router.handles(&Endpoint::rdap("https://rdap.example/")));
/// # }
/// ```
#[derive(Debug, Default, Clone)]
pub struct Router {
    transports: Vec<Arc<dyn Transport>>,
}

impl Router {
    /// An empty router, which supports nothing.
    pub fn new() -> Self {
        Router {
            transports: Vec::new(),
        }
    }

    /// Add a transport. Earlier ones are preferred where both could serve.
    pub fn with(mut self, transport: impl Transport + 'static) -> Self {
        self.transports.push(Arc::new(transport));
        self
    }

    /// Add an already-shared transport.
    pub fn with_shared(mut self, transport: Arc<dyn Transport>) -> Self {
        self.transports.push(transport);
        self
    }

    /// Whether any registered transport can serve this endpoint.
    pub fn handles(&self, endpoint: &Endpoint) -> bool {
        self.transports
            .iter()
            .any(|transport| transport.supports(endpoint))
    }

    /// How many transports are registered.
    pub fn len(&self) -> usize {
        self.transports.len()
    }

    /// Whether no transport is registered.
    pub fn is_empty(&self) -> bool {
        self.transports.is_empty()
    }
}

impl Transport for Router {
    fn supports(&self, endpoint: &Endpoint) -> bool {
        self.handles(endpoint)
    }

    fn fetch(&self, query: &Query) -> Result<RawResponse> {
        for transport in &self.transports {
            if transport.supports(&query.endpoint) {
                return transport.fetch(query);
            }
        }

        Err(Error::NoEndpoint {
            tld: query.tld.clone(),
            detail: format!(
                "no transport handles {}; router has [{}]",
                query.endpoint,
                self.transports
                    .iter()
                    .map(|t| t.name())
                    .collect::<Vec<_>>()
                    .join(", ")
            ),
        })
    }

    fn name(&self) -> String {
        format!(
            "router({})",
            self.transports
                .iter()
                .map(|t| t.name())
                .collect::<Vec<_>>()
                .join("+")
        )
    }
}

/// The asynchronous counterpart of [`Router`].
#[derive(Debug, Default, Clone)]
pub struct AsyncRouter {
    transports: Vec<Arc<dyn AsyncTransport>>,
}

impl AsyncRouter {
    /// An empty router, which supports nothing.
    pub fn new() -> Self {
        AsyncRouter {
            transports: Vec::new(),
        }
    }

    /// Add a transport. Earlier ones are preferred where both could serve.
    pub fn with(mut self, transport: impl AsyncTransport + 'static) -> Self {
        self.transports.push(Arc::new(transport));
        self
    }

    /// Add an already-shared transport.
    pub fn with_shared(mut self, transport: Arc<dyn AsyncTransport>) -> Self {
        self.transports.push(transport);
        self
    }

    /// Whether any registered transport can serve this endpoint.
    pub fn handles(&self, endpoint: &Endpoint) -> bool {
        self.transports
            .iter()
            .any(|transport| transport.supports(endpoint))
    }

    /// How many transports are registered.
    pub fn len(&self) -> usize {
        self.transports.len()
    }

    /// Whether no transport is registered.
    pub fn is_empty(&self) -> bool {
        self.transports.is_empty()
    }
}

impl AsyncTransport for AsyncRouter {
    fn supports(&self, endpoint: &Endpoint) -> bool {
        self.handles(endpoint)
    }

    fn fetch<'a>(&'a self, query: &'a Query) -> BoxFuture<'a, Result<RawResponse>> {
        Box::pin(async move {
            for transport in &self.transports {
                if transport.supports(&query.endpoint) {
                    return transport.fetch(query).await;
                }
            }

            Err(Error::NoEndpoint {
                tld: query.tld.clone(),
                detail: format!("no async transport handles {}", query.endpoint),
            })
        })
    }

    fn name(&self) -> String {
        format!(
            "async-router({})",
            self.transports
                .iter()
                .map(|t| t.name())
                .collect::<Vec<_>>()
                .join("+")
        )
    }
}

/// Decode a server response into text.
///
/// Most registries speak UTF-8; a few still emit Latin-1. Trying UTF-8 first and
/// falling back keeps accented registrant names readable instead of replacing
/// them with `U+FFFD`.
#[cfg(any(feature = "blocking", feature = "async", test))]
pub(crate) fn decode_bytes(bytes: &[u8]) -> String {
    match std::str::from_utf8(bytes) {
        Ok(text) => text.to_string(),
        Err(_) => bytes.iter().map(|&byte| byte as char).collect(),
    }
}

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

    #[test]
    fn decodes_utf8_and_falls_back_to_latin1() {
        assert_eq!(decode_bytes("Müller".as_bytes()), "Müller");
        // 0xFC is `ü` in Latin-1 and invalid on its own in UTF-8.
        assert_eq!(
            decode_bytes(&[b'M', 0xFC, b'l', b'l', b'e', b'r']),
            "Müller"
        );
        assert_eq!(decode_bytes(b""), "");
    }

    #[test]
    fn cache_key_separates_endpoint_from_name() {
        let query = Query::new(
            Endpoint::whois("whois.nic.uk"),
            "example.co.uk",
            Tld::parse("co.uk").unwrap(),
        );
        assert_eq!(query.cache_key(), "whois.nic.uk|example.co.uk");
    }

    #[test]
    fn blank_detection_ignores_whitespace() {
        let response = |text: &str| {
            RawResponse::new(
                Endpoint::whois("w.example"),
                ResponseKind::WhoisText,
                text,
                Duration::ZERO,
            )
        };
        assert!(response("").is_blank());
        assert!(response(" \r\n\t ").is_blank());
        assert!(!response("No match").is_blank());
    }
}