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
//! [`ThrottleTransport`]: stay inside a registry's query budget.
//!
//! Registries rate-limit aggressively, and the punishment is silence rather than
//! an error — most port 43 servers simply stop answering an address that queries
//! too fast, and some keep refusing for hours. Pacing is therefore not politeness
//! but the only way a bulk check finishes with real answers.
//!
//! The budget is tracked per host, because that is where the limit lives. Two
//! `.com` queries share a limit; a `.com` and a `.de` query do not.

use std::collections::HashMap;
use std::sync::Mutex;
use std::time::{Duration, Instant};

use crate::error::Result;
use crate::registry::Endpoint;
use crate::transport::{Query, RawResponse, Transport};

#[cfg(feature = "async")]
use crate::transport::{AsyncTransport, BoxFuture};

/// How closely together queries may be sent.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ThrottlePolicy {
    /// Minimum gap between two queries to the same host.
    per_host: Duration,
    /// Minimum gap between any two queries, whatever the host.
    global: Duration,
    /// Per-host overrides, for registries with a documented limit.
    overrides: HashMap<String, Duration>,
}

impl ThrottlePolicy {
    /// No pacing at all.
    pub fn unlimited() -> Self {
        ThrottlePolicy {
            per_host: Duration::ZERO,
            global: Duration::ZERO,
            overrides: HashMap::new(),
        }
    }

    /// A minimum gap between queries to the same host.
    pub fn per_host(gap: Duration) -> Self {
        ThrottlePolicy {
            per_host: gap,
            global: Duration::ZERO,
            overrides: HashMap::new(),
        }
    }

    /// Also apply a minimum gap between any two queries.
    pub fn with_global(mut self, gap: Duration) -> Self {
        self.global = gap;
        self
    }

    /// Override the gap for one host.
    pub fn with_host(mut self, host: impl Into<String>, gap: Duration) -> Self {
        self.overrides.insert(host.into().to_ascii_lowercase(), gap);
        self
    }

    /// The gap that applies to a host.
    pub fn gap_for(&self, host: &str) -> Duration {
        self.overrides
            .get(&host.to_ascii_lowercase())
            .copied()
            .unwrap_or(self.per_host)
    }

    /// Whether this policy would ever make a caller wait.
    pub fn is_unlimited(&self) -> bool {
        self.per_host.is_zero() && self.global.is_zero() && self.overrides.is_empty()
    }
}

impl Default for ThrottlePolicy {
    /// One second between queries to the same host, none between different hosts.
    ///
    /// Chosen because it is the slowest documented limit among the major
    /// registries that publish one, and because the cost of being wrong in this
    /// direction is a slower check, while the cost of being wrong in the other is
    /// an hour of refusals.
    fn default() -> Self {
        ThrottlePolicy::per_host(Duration::from_secs(1))
    }
}

/// The last time each host was queried.
#[derive(Debug, Default)]
struct LastSeen {
    per_host: HashMap<String, Instant>,
    any: Option<Instant>,
}

impl LastSeen {
    /// How long to wait before querying `host`, and record the intent to do so.
    ///
    /// Reserving the slot inside the lock is what makes this correct under
    /// concurrency: two threads that ask at the same moment get different waits
    /// rather than both being told zero.
    fn reserve(&mut self, host: &str, policy: &ThrottlePolicy) -> Duration {
        let now = Instant::now();

        // What is stored is when the previous query was *scheduled to be sent*, which
        // may be in the future — that is exactly what makes reservations stack. So the
        // wait is measured forward from `last + gap`. Measuring backward from `now`
        // instead, as an elapsed-time subtraction does, hands three concurrent callers
        // the same one-gap wait and defeats the whole mechanism.
        let wait_until = |last: Option<Instant>, gap: Duration| match last {
            Some(last) => (last + gap).saturating_duration_since(now),
            None => Duration::ZERO,
        };

        let wait = wait_until(self.per_host.get(host).copied(), policy.gap_for(host))
            .max(wait_until(self.any, policy.global));

        let scheduled = now + wait;
        self.per_host.insert(host.to_string(), scheduled);
        self.any = Some(scheduled);

        wait
    }
}

/// Paces queries so a registry keeps answering.
///
/// ```
/// # #[cfg(feature = "blocking")] {
/// use std::time::Duration;
/// use monovm_whois::transport::{ThrottlePolicy, ThrottleTransport, Whois43Transport};
///
/// let policy = ThrottlePolicy::per_host(Duration::from_millis(500))
///     // DENIC documents a stricter limit than most.
///     .with_host("whois.denic.de", Duration::from_secs(2));
/// let transport = ThrottleTransport::new(Whois43Transport::default(), policy);
/// # }
/// ```
#[derive(Debug)]
pub struct ThrottleTransport<T> {
    inner: T,
    policy: ThrottlePolicy,
    state: Mutex<LastSeen>,
}

impl<T> ThrottleTransport<T> {
    /// Wrap a transport.
    pub fn new(inner: T, policy: ThrottlePolicy) -> Self {
        ThrottleTransport {
            inner,
            policy,
            state: Mutex::new(LastSeen::default()),
        }
    }

    /// The policy in force.
    pub fn policy(&self) -> &ThrottlePolicy {
        &self.policy
    }

    /// The wrapped transport.
    pub fn inner(&self) -> &T {
        &self.inner
    }

    /// Unwrap, returning the transport.
    pub fn into_inner(self) -> T {
        self.inner
    }

    fn reserve(&self, endpoint: &Endpoint) -> Duration {
        if self.policy.is_unlimited() {
            return Duration::ZERO;
        }

        let host = throttle_key(endpoint);
        self.state
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
            .reserve(&host, &self.policy)
    }
}

impl<T: Transport> Transport for ThrottleTransport<T> {
    fn supports(&self, endpoint: &Endpoint) -> bool {
        self.inner.supports(endpoint)
    }

    fn fetch(&self, query: &Query) -> Result<RawResponse> {
        let wait = self.reserve(&query.endpoint);
        if !wait.is_zero() {
            std::thread::sleep(wait);
        }
        self.inner.fetch(query)
    }

    fn name(&self) -> String {
        format!("throttle({})", self.inner.name())
    }
}

/// The asynchronous counterpart of [`ThrottleTransport`].
#[cfg(feature = "async")]
#[derive(Debug)]
pub struct AsyncThrottleTransport<T> {
    inner: T,
    policy: ThrottlePolicy,
    state: Mutex<LastSeen>,
}

#[cfg(feature = "async")]
impl<T> AsyncThrottleTransport<T> {
    /// Wrap a transport.
    pub fn new(inner: T, policy: ThrottlePolicy) -> Self {
        AsyncThrottleTransport {
            inner,
            policy,
            state: Mutex::new(LastSeen::default()),
        }
    }

    /// The policy in force.
    pub fn policy(&self) -> &ThrottlePolicy {
        &self.policy
    }

    /// The wrapped transport.
    pub fn inner(&self) -> &T {
        &self.inner
    }

    /// Unwrap, returning the transport.
    pub fn into_inner(self) -> T {
        self.inner
    }
}

#[cfg(feature = "async")]
impl<T: AsyncTransport> AsyncTransport for AsyncThrottleTransport<T> {
    fn supports(&self, endpoint: &Endpoint) -> bool {
        self.inner.supports(endpoint)
    }

    fn fetch<'a>(&'a self, query: &'a Query) -> BoxFuture<'a, Result<RawResponse>> {
        Box::pin(async move {
            // The lock is a std `Mutex` and is released before the await: holding
            // one across an await point would serialise every task on it, which is
            // precisely what a per-host budget is meant to avoid.
            let wait = if self.policy.is_unlimited() {
                Duration::ZERO
            } else {
                let host = throttle_key(&query.endpoint);
                self.state
                    .lock()
                    .unwrap_or_else(|poisoned| poisoned.into_inner())
                    .reserve(&host, &self.policy)
            };

            if !wait.is_zero() {
                tokio::time::sleep(wait).await;
            }
            self.inner.fetch(query).await
        })
    }

    fn name(&self) -> String {
        format!("async-throttle({})", self.inner.name())
    }
}

/// The thing a budget is tracked against: a host name.
///
/// For RDAP that means the URL's authority rather than the whole URL, since two
/// paths on one server share its limit.
fn throttle_key(endpoint: &Endpoint) -> String {
    match endpoint {
        Endpoint::Whois(whois) => whois.host().to_ascii_lowercase(),
        Endpoint::Rdap(rdap) => rdap
            .base()
            .split_once("://")
            .map(|(_, rest)| rest)
            .unwrap_or_else(|| rdap.base())
            .split('/')
            .next()
            .unwrap_or_else(|| rdap.base())
            .to_ascii_lowercase(),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::domain::Tld;
    use crate::transport::mock::MockTransport;

    fn query(endpoint: Endpoint) -> Query {
        Query::new(endpoint, "example.com", Tld::parse("com").unwrap())
    }

    #[test]
    fn budgets_are_tracked_per_host_not_per_url() {
        assert_eq!(
            throttle_key(&Endpoint::whois("WHOIS.Example")),
            "whois.example"
        );
        assert_eq!(
            throttle_key(&Endpoint::rdap("https://rdap.example/com/v1/domain/")),
            "rdap.example"
        );
        // Two paths on one server share its limit.
        assert_eq!(
            throttle_key(&Endpoint::rdap("https://rdap.example/a/")),
            throttle_key(&Endpoint::rdap("https://rdap.example/b/"))
        );
    }

    #[test]
    fn per_host_overrides_beat_the_default_gap() {
        let policy = ThrottlePolicy::per_host(Duration::from_millis(100))
            .with_host("Slow.Example", Duration::from_secs(5));

        assert_eq!(policy.gap_for("slow.example"), Duration::from_secs(5));
        assert_eq!(policy.gap_for("SLOW.EXAMPLE"), Duration::from_secs(5));
        assert_eq!(policy.gap_for("other.example"), Duration::from_millis(100));
    }

    #[test]
    fn the_first_query_to_a_host_never_waits() {
        let mut state = LastSeen::default();
        let policy = ThrottlePolicy::per_host(Duration::from_secs(10));
        assert_eq!(state.reserve("a.example", &policy), Duration::ZERO);
        // A different host is a different budget.
        assert_eq!(state.reserve("b.example", &policy), Duration::ZERO);
    }

    #[test]
    fn a_second_query_to_the_same_host_waits() {
        let mut state = LastSeen::default();
        let policy = ThrottlePolicy::per_host(Duration::from_secs(10));

        state.reserve("a.example", &policy);
        let wait = state.reserve("a.example", &policy);
        assert!(wait > Duration::from_secs(9), "got {wait:?}");
    }

    #[test]
    fn reservations_stack_so_concurrent_callers_get_different_slots() {
        let mut state = LastSeen::default();
        let policy = ThrottlePolicy::per_host(Duration::from_secs(1));

        assert_eq!(state.reserve("a.example", &policy), Duration::ZERO);
        let second = state.reserve("a.example", &policy);
        let third = state.reserve("a.example", &policy);

        assert!(third > second, "{third:?} should be later than {second:?}");
        assert!(third > Duration::from_millis(1900), "got {third:?}");
    }

    #[test]
    fn a_global_gap_applies_across_hosts() {
        let mut state = LastSeen::default();
        let policy = ThrottlePolicy::unlimited().with_global(Duration::from_secs(5));

        state.reserve("a.example", &policy);
        let wait = state.reserve("b.example", &policy);
        assert!(wait > Duration::from_secs(4), "got {wait:?}");
    }

    #[test]
    fn an_unlimited_policy_never_waits() {
        let policy = ThrottlePolicy::unlimited();
        assert!(policy.is_unlimited());

        let transport = ThrottleTransport::new(MockTransport::answering("ok"), policy);
        let started = Instant::now();
        for _ in 0..5 {
            transport
                .fetch(&query(Endpoint::whois("a.example")))
                .unwrap();
        }
        assert!(started.elapsed() < Duration::from_millis(200));
    }

    #[test]
    fn pacing_actually_delays_the_second_call() {
        let transport = ThrottleTransport::new(
            MockTransport::answering("ok"),
            ThrottlePolicy::per_host(Duration::from_millis(120)),
        );

        let started = Instant::now();
        transport
            .fetch(&query(Endpoint::whois("a.example")))
            .unwrap();
        transport
            .fetch(&query(Endpoint::whois("a.example")))
            .unwrap();
        assert!(
            started.elapsed() >= Duration::from_millis(100),
            "got {:?}",
            started.elapsed()
        );
    }
}