reserve-core 0.5.1

Core lookup, catalog, and rate-limiting engine behind the reserve domain finder
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
//! Reading the published detail out of a registry record.

use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::lookup::outcome::scrub;

#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct Registration {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub registrar: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub registrar_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub created_at: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub updated_at: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub expires_at: Option<String>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub statuses: Vec<String>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub nameservers: Vec<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub has_dnssec: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub abuse_email: Option<String>,
}

impl Registration {
    #[must_use]
    pub fn is_empty(&self) -> bool {
        *self == Self::default()
    }
}

/// @docgen The text protocol is the only source that answers for many country zones, so dropping its record left those zones with no detail at all.
#[must_use]
pub(crate) fn parse_text(raw: &str) -> Registration {
    let mut record = Registration::default();

    for line in raw.lines() {
        let line = line.trim();
        if line.is_empty() || line.starts_with('%') || line.starts_with('#') {
            continue;
        }
        // @docgen One registry we already speak to keys its record in brackets and carries no colon, so its record read as empty.
        let Some((key, value)) = line
            .strip_prefix('[')
            .and_then(|rest| rest.split_once(']'))
            .or_else(|| line.split_once(':'))
        else {
            continue;
        };
        let value = scrub(value);
        if value.is_empty() {
            continue;
        }
        let key = key.trim().to_lowercase();
        let key = key.trim_end_matches('.').trim();
        // @docgen The same field arrives as `Registrar`, `Sponsoring Registrar`, and `Created On`, so the wrappers are peeled once here.
        let key = key.strip_prefix("sponsoring ").unwrap_or(key);
        let key = key.strip_suffix(" on").unwrap_or(key);

        match key {
            "registrar" | "sponsoring registrar" | "registrar name" => {
                record.registrar.get_or_insert(value);
            }
            "registrar iana id" | "registrar id" => {
                record.registrar_id.get_or_insert(value);
            }
            "creation date"
            | "created on"
            | "created"
            | "domain registration date"
            | "registered on"
            | "registered" => {
                record.created_at.get_or_insert(value);
            }
            "updated date"
            | "last updated"
            | "last modified"
            | "changed"
            | "domain last updated date" => {
                record.updated_at.get_or_insert(value);
            }
            "registry expiry date"
            | "registrar registration expiration date"
            | "expiration date"
            | "expires on"
            | "expiry date"
            | "expires"
            | "domain expiration date" => {
                record.expires_at.get_or_insert(value);
            }
            "domain status" | "status" | "state" => {
                // @docgen Registries append an explanatory link to the status, and only the words before it are the status.
                let status = value
                    .split_once(" http")
                    .map_or(value.as_str(), |(said, _)| said)
                    .trim()
                    .to_owned();
                if !status.is_empty() && !record.statuses.contains(&status) {
                    record.statuses.push(status);
                }
            }
            "name server" | "nameserver" | "nserver" => {
                // @docgen The singular form puts the glue address after the host, so only the first token is a name.
                let host = value
                    .split_whitespace()
                    .next()
                    .unwrap_or(&value)
                    .to_lowercase();
                if !host.is_empty() && !record.nameservers.contains(&host) {
                    record.nameservers.push(host);
                }
            }
            "name servers" | "nameservers" => {
                // @docgen The plural form lists further hosts rather than a glue address, so taking only the first dropped the rest.
                for host in value.split_whitespace() {
                    let host = host.to_lowercase();
                    if host.contains('.') && !record.nameservers.contains(&host) {
                        record.nameservers.push(host);
                    }
                }
            }
            "dnssec" => {
                let lowered = value.to_lowercase();
                record
                    .has_dnssec
                    .get_or_insert(!(lowered.starts_with("unsigned") || lowered == "no"));
            }
            "registrar abuse contact email" | "abuse contact email" => {
                record.abuse_email.get_or_insert(value);
            }
            _ => {}
        }
    }

    record
}

#[must_use]
pub(crate) fn parse(body: &Value) -> Registration {
    let mut record = Registration::default();

    if let Some(statuses) = body.get("status").and_then(Value::as_array) {
        record.statuses = statuses
            .iter()
            .filter_map(Value::as_str)
            .map(scrub)
            .collect();
    }

    if let Some(events) = body.get("events").and_then(Value::as_array) {
        for event in events {
            let action = event
                .get("eventAction")
                .and_then(Value::as_str)
                .unwrap_or_default()
                .to_lowercase();
            let date = event.get("eventDate").and_then(Value::as_str).map(scrub);
            match action.as_str() {
                "registration" => record.created_at = date,
                "expiration" => record.expires_at = date,
                "last changed" => record.updated_at = date,
                _ => {}
            }
        }
    }

    if let Some(nameservers) = body.get("nameservers").and_then(Value::as_array) {
        record.nameservers = nameservers
            .iter()
            .filter_map(|ns| ns.get("ldhName").and_then(Value::as_str))
            .map(|ns| scrub(&ns.to_lowercase()))
            .collect();
    }

    record.has_dnssec = body
        .get("secureDNS")
        .and_then(|dns| dns.get("delegationSigned"))
        .and_then(Value::as_bool);

    if let Some(entities) = body.get("entities").and_then(Value::as_array) {
        for entity in entities {
            if !has_role(entity, "registrar") {
                continue;
            }
            record.registrar = vcard(entity, "fn");
            record.registrar_id = entity
                .get("publicIds")
                .and_then(Value::as_array)
                .and_then(|ids| ids.first())
                .and_then(|id| id.get("identifier"))
                .and_then(Value::as_str)
                .map(scrub);
            record.abuse_email =
                entity
                    .get("entities")
                    .and_then(Value::as_array)
                    .and_then(|nested| {
                        nested
                            .iter()
                            .find(|inner| has_role(inner, "abuse"))
                            .and_then(|inner| vcard(inner, "email"))
                    });
        }
    }

    record
}

fn has_role(entity: &Value, role: &str) -> bool {
    entity
        .get("roles")
        .and_then(Value::as_array)
        .is_some_and(|roles| {
            roles
                .iter()
                .filter_map(Value::as_str)
                .any(|found| found.eq_ignore_ascii_case(role))
        })
}

/// @docgen A contact card is `["vcard", [[key, {}, "text", value], ...]]`, which is where the index juggling comes from.
fn vcard(entity: &Value, key: &str) -> Option<String> {
    let items = entity
        .get("vcardArray")
        .and_then(Value::as_array)?
        .get(1)?
        .as_array()?;
    for item in items {
        let Some(parts) = item.as_array() else {
            continue;
        };
        if parts.first().and_then(Value::as_str) == Some(key)
            && let Some(value) = parts.get(3).and_then(Value::as_str)
            && !value.is_empty()
        {
            return Some(scrub(value));
        }
    }
    None
}

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

    fn sample() -> Value {
        json!({
            "objectClassName": "domain",
            "ldhName": "apple.com",
            "status": ["client transfer prohibited"],
            "events": [
                {"eventAction": "registration", "eventDate": "1987-02-19T05:00:00Z"},
                {"eventAction": "expiration", "eventDate": "2027-02-20T05:00:00Z"},
                {"eventAction": "last changed", "eventDate": "2026-02-09T15:41:53Z"},
                {"eventAction": "last update of RDAP database", "eventDate": "2026-08-15T00:00:00Z"}
            ],
            "nameservers": [{"ldhName": "A.NS.APPLE.COM"}],
            "secureDNS": {"delegationSigned": false},
            "entities": [{
                "roles": ["registrar"],
                "publicIds": [{"identifier": "470"}],
                "vcardArray": ["vcard", [["version", {}, "text", "4.0"], ["fn", {}, "text", "COM LAUDE"]]],
                "entities": [{
                    "roles": ["abuse"],
                    "vcardArray": ["vcard", [["email", {}, "text", "abuse@example.com"]]]
                }]
            }]
        })
    }

    #[test]
    fn it_reads_the_published_detail() {
        let record = parse(&sample());
        assert_eq!(record.registrar.as_deref(), Some("COM LAUDE"));
        assert_eq!(record.registrar_id.as_deref(), Some("470"));
        assert_eq!(record.created_at.as_deref(), Some("1987-02-19T05:00:00Z"));
        assert_eq!(record.expires_at.as_deref(), Some("2027-02-20T05:00:00Z"));
        assert_eq!(record.nameservers, vec!["a.ns.apple.com"]);
        assert_eq!(record.has_dnssec, Some(false));
        assert_eq!(record.abuse_email.as_deref(), Some("abuse@example.com"));
    }

    #[test]
    fn the_last_changed_event_wins_over_a_database_stamp() {
        assert_eq!(
            parse(&sample()).updated_at.as_deref(),
            Some("2026-02-09T15:41:53Z")
        );
    }

    #[test]
    fn an_empty_body_yields_an_empty_record() {
        assert!(parse(&json!({})).is_empty());
        assert!(!parse(&sample()).is_empty());
    }

    #[test]
    fn a_missing_contact_card_is_not_an_error() {
        let body = json!({"entities": [{"roles": ["registrar"]}]});
        let record = parse(&body);
        assert!(record.registrar.is_none());
        assert!(record.abuse_email.is_none());
    }

    #[test]
    fn a_text_record_yields_the_same_detail_the_structured_one_does() {
        let reply = "\
Domain Name: example.com.bd
Registrar: BTCL
Creation Date: 2019-04-01T10:00:00Z
Updated Date: 2024-02-11T08:30:00Z
Expiry Date: 2027-04-01T10:00:00Z
Domain Status: clientTransferProhibited https://icann.org/epp
Name Server: ns1.btcl.net.bd
Name Server: NS2.BTCL.NET.BD
DNSSEC: unsigned
Registrar Abuse Contact Email: abuse@example.test
";
        let record = parse_text(reply);
        assert_eq!(record.registrar.as_deref(), Some("BTCL"));
        assert_eq!(record.created_at.as_deref(), Some("2019-04-01T10:00:00Z"));
        assert_eq!(record.updated_at.as_deref(), Some("2024-02-11T08:30:00Z"));
        assert_eq!(record.expires_at.as_deref(), Some("2027-04-01T10:00:00Z"));
        assert_eq!(record.statuses, vec!["clientTransferProhibited".to_owned()]);
        assert_eq!(
            parse_text("Status: Registered, success\n").statuses,
            vec!["Registered, success".to_owned()],
            "a status that is a phrase is not cut at its first space"
        );
        assert_eq!(
            record.nameservers,
            vec!["ns1.btcl.net.bd".to_owned(), "ns2.btcl.net.bd".to_owned()],
            "a host is one entry however the registry cased it"
        );
        assert_eq!(record.has_dnssec, Some(false));
        assert_eq!(record.abuse_email.as_deref(), Some("abuse@example.test"));
        assert!(!record.is_empty());
    }

    #[test]
    fn a_reply_with_nothing_in_it_stays_empty_rather_than_printing_a_bare_heading() {
        assert!(parse_text("").is_empty());
        assert!(parse_text("% this zone publishes no record\n").is_empty());
    }

    #[test]
    fn a_hostile_text_record_cannot_carry_control_bytes_into_the_detail_block() {
        let reply = "Registrar: Evil\u{1b}[2J Ltd\nName Server: ns1\u{202e}.test\n";
        let record = parse_text(reply);
        assert!(!record.registrar.unwrap_or_default().contains('\u{1b}'));
        assert!(!record.nameservers.join(" ").contains('\u{202e}'));
    }

    #[test]
    fn the_older_registrar_wording_fills_the_same_fields() {
        let reply = "\
Created On:2003-01-01
Last Updated On:2020-01-01
Sponsoring Registrar:Example Inc.
Sponsoring Registrar IANA ID:292
Expiration Date:2027-01-01
";
        let record = parse_text(reply);
        assert_eq!(record.created_at.as_deref(), Some("2003-01-01"));
        assert_eq!(record.updated_at.as_deref(), Some("2020-01-01"));
        assert_eq!(record.registrar.as_deref(), Some("Example Inc."));
        assert_eq!(record.registrar_id.as_deref(), Some("292"));
        assert_eq!(record.expires_at.as_deref(), Some("2027-01-01"));
    }

    #[test]
    fn a_record_keyed_in_brackets_is_read_rather_than_skipped() {
        let reply = "\
[Domain Name]                   EXAMPLE.JP
[Registrant]                    Example Company
[Name Server]                   ns1.example.jp
[Name Server]                   ns2.example.jp
[Created on]                    2001/05/21
[Last Updated]                  2026/06/01
";
        let record = parse_text(reply);
        assert_eq!(
            record.nameservers,
            vec!["ns1.example.jp".to_owned(), "ns2.example.jp".to_owned()]
        );
        assert_eq!(record.created_at.as_deref(), Some("2001/05/21"));
        assert_eq!(record.updated_at.as_deref(), Some("2026/06/01"));
        assert!(!record.is_empty());
    }

    #[test]
    fn a_plural_nameserver_line_keeps_every_host_it_lists() {
        let record = parse_text("Name Servers: ns1.example.bd ns2.example.bd ns3.example.bd\n");
        assert_eq!(
            record.nameservers,
            vec![
                "ns1.example.bd".to_owned(),
                "ns2.example.bd".to_owned(),
                "ns3.example.bd".to_owned()
            ]
        );

        let singular = parse_text("Name Server: ns1.example.bd 203.0.113.10\n");
        assert_eq!(
            singular.nameservers,
            vec!["ns1.example.bd".to_owned()],
            "the singular form puts a glue address after the host"
        );
    }
}