seer-core 0.39.0

Core library for Seer domain name utilities
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
//! Parser for .ee domains (EIS / Estonian Internet Foundation format).
//!
//! EIS uses an indented-section format: each section begins with a header
//! ending in `:` at column 0, followed by indented `key:    value` lines.
//! The same key name (`name:`, `email:`, `changed:`) appears under multiple
//! sections (Domain / Registrant / Registrar / contacts), so the generic
//! regex parser cannot disambiguate without sectioning.
//!
//! Example EIS response:
//! ```text
//! Domain:
//! name:       example.ee
//! status:     ok (paid and in zone)
//! registered: 2012-01-27 10:00:15 +02:00
//! changed:    2025-12-05 08:05:19 +02:00
//! expire:     2027-01-28
//!
//! Registrant:
//! name:       Example Holder AS
//! org id:     10421629
//! country:    EE
//! email:      ...
//!
//! Registrar:
//! name:       Zone Media OÜ
//! url:        http://www.zone.ee
//!
//! Name servers:
//! nserver:    ns1.example.com
//! nserver:    ns2.example.com
//!
//! DNSSEC:
//! dnskey:     257 3 13 ...
//! ```
//!
//! "Domain not found" responses use the bare phrase and are matched by the
//! generic `AVAILABILITY_PATTERNS`, so this parser does not special-case them.

use chrono::{DateTime, Utc};
use once_cell::sync::Lazy;
use regex::Regex;

use super::{push_bounded, RegistryParser, MAX_NAMESERVERS, MAX_STATUSES};
use crate::whois::parser::WhoisResponse;

static KEY_VALUE: Lazy<Regex> = Lazy::new(|| {
    // `key:   value` lines. EIS fields are flush-left, with multiple spaces
    // padding before the value. Keys may contain spaces (e.g. `org id`).
    Regex::new(r"^([a-z][a-z0-9 ]*):\s*(.+?)\s*$").expect("Invalid EIS key/value regex")
});

/// Known section headers. We can't generically match `Foo:` as a section
/// because empty-value fields like `outzone:` would be ambiguous. Whitelist
/// the section names EIS actually uses; anything else keeps the current
/// section so we don't lose data on unexpected lines.
fn parse_section_header(line: &str) -> Option<Section> {
    let trimmed = line.trim_end();
    // Must end with `:` and have no leading whitespace.
    let stripped = trimmed.strip_suffix(':')?;
    if stripped.is_empty() || stripped.starts_with(char::is_whitespace) {
        return None;
    }
    Section::from_header(stripped)
}

#[derive(Clone, Copy, PartialEq, Eq)]
enum Section {
    None,
    Domain,
    Registrant,
    Admin,
    Tech,
    Registrar,
    Nameservers,
    Dnssec,
}

impl Section {
    /// Returns Some(section) only for the known EIS section names. Unknown
    /// headers leave the section unchanged so a stray colon-ending line
    /// doesn't drop following fields on the floor.
    fn from_header(name: &str) -> Option<Self> {
        let n = name.trim().to_ascii_lowercase();
        Some(match n.as_str() {
            "domain" => Section::Domain,
            "registrant" => Section::Registrant,
            "administrative contact" => Section::Admin,
            "technical contact" => Section::Tech,
            "registrar" => Section::Registrar,
            "name servers" => Section::Nameservers,
            "dnssec" => Section::Dnssec,
            _ => return None,
        })
    }
}

/// Parser for .ee domains using the EIS section-based format.
#[derive(Debug, Clone, Default)]
pub struct EisParser;

impl EisParser {
    pub fn new() -> Self {
        Self
    }

    /// EIS dates appear in two shapes: `YYYY-MM-DD HH:MM:SS ±HH:MM` (full
    /// timestamps for `registered:` and `changed:`) and bare `YYYY-MM-DD`
    /// (for `expire:`). Try both.
    fn parse_date(raw: &str) -> Option<DateTime<Utc>> {
        let s = raw.trim();
        if let Ok(dt) = DateTime::parse_from_str(s, "%Y-%m-%d %H:%M:%S %:z") {
            return Some(dt.with_timezone(&Utc));
        }
        if let Ok(d) = chrono::NaiveDate::parse_from_str(s, "%Y-%m-%d") {
            return Some(d.and_hms_opt(0, 0, 0)?.and_utc());
        }
        None
    }

    /// `email:` lines in EIS may carry a redaction placeholder; preserve only
    /// real-looking email values.
    fn is_real_value(value: &str) -> bool {
        let lower = value.to_ascii_lowercase();
        !lower.contains("not disclosed") && !lower.contains("redacted")
    }
}

impl RegistryParser for EisParser {
    fn supported_tlds(&self) -> &[&str] {
        &["ee"]
    }

    fn parse(&self, domain: &str, server: &str, raw: &str) -> WhoisResponse {
        let mut current = Section::None;

        let mut status: Vec<String> = Vec::new();
        let mut nameservers: Vec<String> = Vec::new();
        let mut registrar: Option<String> = None;
        let mut registrant: Option<String> = None;
        let mut organization: Option<String> = None;
        let mut registrant_country: Option<String> = None;
        let mut registrant_email: Option<String> = None;
        let mut registrant_phone: Option<String> = None;
        let mut admin_name: Option<String> = None;
        let mut admin_email: Option<String> = None;
        let mut tech_name: Option<String> = None;
        let mut tech_email: Option<String> = None;
        let mut creation_date: Option<DateTime<Utc>> = None;
        let mut expiration_date: Option<DateTime<Utc>> = None;
        let mut updated_date: Option<DateTime<Utc>> = None;
        let mut dnssec: Option<String> = None;

        for line in raw.lines() {
            // Skip preamble/footer chatter lines that don't match either shape.
            if let Some(section) = parse_section_header(line) {
                current = section;
                continue;
            }

            let Some(caps) = KEY_VALUE.captures(line) else {
                continue;
            };
            let key = caps[1].to_ascii_lowercase();
            let value = caps[2].trim().to_string();
            if value.is_empty() || !Self::is_real_value(&value) {
                continue;
            }

            match (current, key.as_str()) {
                (Section::Domain, "status") => {
                    // EIS statuses look like `ok (paid and in zone)`; keep the
                    // short form for downstream comparisons but preserve raw
                    // in the list too if it adds info.
                    let short = value
                        .split_whitespace()
                        .next()
                        .unwrap_or(&value)
                        .to_string();
                    push_bounded(&mut status, short, MAX_STATUSES);
                }
                (Section::Domain, "registered") if creation_date.is_none() => {
                    creation_date = Self::parse_date(&value);
                }
                (Section::Domain, "changed") if updated_date.is_none() => {
                    updated_date = Self::parse_date(&value);
                }
                (Section::Domain, "expire") if expiration_date.is_none() => {
                    expiration_date = Self::parse_date(&value);
                }
                (Section::Registrant, "name") if registrant.is_none() => {
                    registrant = Some(value);
                }
                (Section::Registrant, "org id") if organization.is_none() => {
                    organization = Some(value);
                }
                (Section::Registrant, "country") if registrant_country.is_none() => {
                    registrant_country = Some(value);
                }
                (Section::Registrant, "email") if registrant_email.is_none() => {
                    registrant_email = Some(value);
                }
                (Section::Registrant, "phone") if registrant_phone.is_none() => {
                    registrant_phone = Some(value);
                }
                (Section::Admin, "name") if admin_name.is_none() => {
                    admin_name = Some(value);
                }
                (Section::Admin, "email") if admin_email.is_none() => {
                    admin_email = Some(value);
                }
                (Section::Tech, "name") if tech_name.is_none() => {
                    tech_name = Some(value);
                }
                (Section::Tech, "email") if tech_email.is_none() => {
                    tech_email = Some(value);
                }
                (Section::Registrar, "name") if registrar.is_none() => {
                    registrar = Some(value);
                }
                (Section::Nameservers, "nserver") => {
                    // EIS sometimes appends a glue IP after the host.
                    let ns = value
                        .split_whitespace()
                        .next()
                        .unwrap_or(&value)
                        .to_ascii_lowercase();
                    push_bounded(&mut nameservers, ns, MAX_NAMESERVERS);
                }
                (Section::Dnssec, "dnskey") if dnssec.is_none() => {
                    dnssec = Some("signedDelegation".to_string());
                }
                _ => {}
            }
        }

        WhoisResponse {
            domain: domain.to_string(),
            registrar,
            registrant,
            organization,
            registrant_email,
            registrant_phone,
            registrant_address: None,
            registrant_country,
            admin_name,
            admin_organization: None,
            admin_email,
            admin_phone: None,
            tech_name,
            tech_organization: None,
            tech_email,
            tech_phone: None,
            creation_date,
            expiration_date,
            updated_date,
            nameservers,
            status,
            dnssec,
            whois_server: server.to_string(),
            raw_response: raw.to_string(),
        }
    }
}

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

    const SAMPLE: &str = "Estonia .ee Top Level Domain WHOIS server\n\
\n\
Domain:\n\
name:       eestienergia.ee\n\
status:     ok (paid and in zone)\n\
registered: 2012-01-27 10:00:15 +02:00\n\
changed:    2025-12-05 08:05:19 +02:00\n\
expire:     2027-01-28\n\
\n\
Registrant:\n\
name:       Eesti Energia AS\n\
org id:     10421629\n\
country:    EE\n\
email:      Not Disclosed - Visit www.internet.ee for webbased WHOIS\n\
phone:      Not Disclosed - Visit www.internet.ee for webbased WHOIS\n\
changed:    2025-12-05 08:05:19 +02:00\n\
\n\
Administrative contact:\n\
name:       Not Disclosed - Visit www.internet.ee for webbased WHOIS\n\
email:      Not Disclosed - Visit www.internet.ee for webbased WHOIS\n\
\n\
Technical contact:\n\
name:       Not Disclosed - Visit www.internet.ee for webbased WHOIS\n\
email:      Not Disclosed - Visit www.internet.ee for webbased WHOIS\n\
\n\
Registrar:\n\
name:       Zone Media OÜ\n\
url:        http://www.zone.ee\n\
phone:      +372 6886886\n\
changed:    2020-07-01 13:55:58 +03:00\n\
\n\
Name servers:\n\
nserver:   leonidas.ns.cloudflare.com\n\
nserver:   sara.ns.cloudflare.com\n\
changed:   2023-04-20 16:05:04 +03:00\n\
\n\
DNSSEC:\n\
dnskey:    257 3 13 mdsswUyr3DPW132mOi8V9xESWE8jTo0dxCjjnopKl\n\
changed:   2023-05-08 09:20:15 +03:00\n";

    fn parse(raw: &str) -> WhoisResponse {
        EisParser::new().parse("eestienergia.ee", "whois.tld.ee", raw)
    }

    #[test]
    fn extracts_registrar_from_registrar_section_not_domain_section() {
        // Regression: the generic parser previously returned
        // `name:       Zone Media OÜ` (the whole line) because it had no
        // section context. We must return the actual value.
        let r = parse(SAMPLE);
        assert_eq!(r.registrar.as_deref(), Some("Zone Media OÜ"));
    }

    #[test]
    fn extracts_registrant_separately_from_registrar() {
        let r = parse(SAMPLE);
        assert_eq!(r.registrant.as_deref(), Some("Eesti Energia AS"));
        assert_eq!(r.organization.as_deref(), Some("10421629"));
        assert_eq!(r.registrant_country.as_deref(), Some("EE"));
    }

    #[test]
    fn redacted_emails_are_dropped() {
        let r = parse(SAMPLE);
        assert!(
            r.registrant_email.is_none(),
            "redacted email should be skipped, got {:?}",
            r.registrant_email
        );
    }

    #[test]
    fn extracts_dates() {
        let r = parse(SAMPLE);
        let created = r.creation_date.expect("creation date");
        assert_eq!(created.year(), 2012);
        assert_eq!(created.month(), 1);
        let expires = r.expiration_date.expect("expiration date");
        assert_eq!(expires.year(), 2027);
        let updated = r.updated_date.expect("updated date");
        assert_eq!(updated.year(), 2025);
    }

    #[test]
    fn extracts_nameservers_without_glue() {
        let r = parse(SAMPLE);
        assert_eq!(r.nameservers.len(), 2);
        assert!(r
            .nameservers
            .contains(&"leonidas.ns.cloudflare.com".to_string()));
        assert!(r
            .nameservers
            .contains(&"sara.ns.cloudflare.com".to_string()));
    }

    #[test]
    fn dnssec_signed_when_dnskey_present() {
        let r = parse(SAMPLE);
        assert_eq!(r.dnssec.as_deref(), Some("signedDelegation"));
    }

    #[test]
    fn status_short_form() {
        let r = parse(SAMPLE);
        // Generic parser used to capture only `ok` — keep that short form so
        // downstream callers that compare on `"ok"` keep working.
        assert!(r.status.contains(&"ok".to_string()));
    }

    #[test]
    fn has_core_data_for_registered() {
        let r = parse(SAMPLE);
        assert!(r.has_core_data(), "registrar+dates+ns means core present");
    }

    #[test]
    fn supported_tlds() {
        assert_eq!(EisParser::new().supported_tlds(), &["ee"]);
    }

    #[test]
    fn nameservers_are_capped_against_hostile_body() {
        // A hostile WHOIS body with far more distinct nameserver lines than any
        // real domain. The per-registry parser must apply the same cap as the
        // generic parser (MAX_NAMESERVERS) so the O(n) `Vec::contains` dedup
        // can't be driven into O(n^2) CPU.
        let mut body = String::from("Name servers:\n");
        for i in 0..100 {
            body.push_str(&format!("nserver:    ns{i}.example.com\n"));
        }
        let r = parse(&body);
        assert!(
            r.nameservers.len() <= MAX_NAMESERVERS,
            "nameservers must be capped at {}, got {}",
            MAX_NAMESERVERS,
            r.nameservers.len()
        );
    }
}