seer-cli 0.52.1

Interactive CLI for Seer domain name utilities
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
//! Interface-agnostic result payload: one enum over every formatter-backed
//! seer-core result type, shared by the CLI and REPL (every single-shot
//! command's result, and `copy`) and the TUI (raw view / `y` copy).
//! Serialization reuses seer-core's formatters so copied text matches
//! `seer --format …` exactly.
use seer_core::output::{get_formatter, OutputFormat, YamlFormatter};

/// Serializes as the wrapped result itself (untagged), so `--quiet` JSON is
/// exactly the core type's.
#[derive(Debug, Clone, serde::Serialize)]
#[serde(untagged)]
pub enum Payload {
    Overview(Box<seer_core::LookupResult>),
    Whois(Box<seer_core::WhoisResponse>),
    Rdap(Box<seer_core::RdapResponse>),
    /// `dig` for one record type — and the TUI's DNS lens: the whole
    /// response (status, flags, CNAME chain, authority, wildcard probe).
    Dig(Box<seer_core::DnsQueryResult>),
    /// `dig` for several record types, one result per type in the order
    /// asked — a JSON array where [`Payload::Dig`] is an object.
    DigMany(Vec<seer_core::DnsQueryResult>),
    /// `dig +trace`: the delegation walk from the root servers.
    Trace(Box<seer_core::DnsTrace>),
    Ssl(Box<seer_core::SslReport>),
    Status(Box<seer_core::StatusResponse>),
    Prop(Box<seer_core::PropagationResult>),
    Reverse(Vec<seer_core::DnsRecord>),
    Avail(Box<seer_core::AvailabilityResult>),
    Tld(Box<seer_core::TldInfo>),
    Dnssec(Box<seer_core::DnssecReport>),
    Compare(Box<seer_core::DnsComparison>),
    Diff(Box<seer_core::DomainDiff>),
    Watch(Box<seer_core::WatchReport>),
    History(Vec<seer_core::HistoryEntry>),
    Subdomains(Box<seer_core::SubdomainResult>),
    /// `subdomains --diff`: the fresh enumeration against the stored baseline.
    SubdomainBaselineDiff(Box<seer_core::SubdomainBaselineDiff>),
    Info(Box<seer_core::DomainInfo>),
    Drift(Box<seer_core::DriftReport>),
    Posture(Box<seer_core::EmailPosture>),
    Headers(Box<seer_core::HeaderReport>),
    Takeover(Box<seer_core::TakeoverReport>),
    Caa(Box<seer_core::CaaPolicy>),
    Confusables(Box<seer_core::ConfusableReport>),
    Delegation(Box<seer_core::dns::DelegationReport>),
    /// `subdomains --resolve` output (live/dead + dangling-CNAME classes).
    SubdomainClassification(Box<seer_core::SubdomainClassification>),
    /// Unlike the others this has no `OutputFormatter` method — doctor reports
    /// render through `crate::render_doctor_report`, the same helper the CLI
    /// and REPL print with. Carried here anyway so `copy` after `doctor`
    /// copies the doctor report instead of silently copying whatever ran
    /// before it.
    Doctor(Box<seer_core::doctor::DoctorReport>),
}

impl Payload {
    /// Short lowercase label for user-facing messages ("copied whois …").
    pub fn kind(&self) -> &'static str {
        match self {
            Payload::Overview(_) => "lookup",
            Payload::Whois(_) => "whois",
            Payload::Rdap(_) => "rdap",
            Payload::Dig(_) | Payload::DigMany(_) => "dig",
            Payload::Trace(_) => "trace",
            Payload::Ssl(_) => "ssl",
            Payload::Status(_) => "status",
            Payload::Prop(_) => "propagation",
            Payload::Reverse(_) => "reverse",
            Payload::Avail(_) => "availability",
            Payload::Tld(_) => "tld",
            Payload::Dnssec(_) => "dnssec",
            Payload::Compare(_) => "compare",
            Payload::Diff(_) => "diff",
            Payload::Watch(_) => "watch",
            Payload::History(_) => "history",
            Payload::Subdomains(_) => "subdomains",
            Payload::SubdomainBaselineDiff(_) => "subdomain diff",
            Payload::Info(_) => "info",
            Payload::Drift(_) => "drift",
            Payload::Posture(_) => "posture",
            Payload::Headers(_) => "headers",
            Payload::Takeover(_) => "takeover",
            Payload::Caa(_) => "caa",
            Payload::Confusables(_) => "confusables",
            Payload::Delegation(_) => "delegation",
            Payload::SubdomainClassification(_) => "subdomain classification",
            Payload::Doctor(_) => "doctor",
        }
    }

    /// The `+short` form of a dig result — bare values, one per line (see
    /// [`seer_core::output::dig_short`]), empty when there are none — or
    /// `None` for a payload that has no short form. Several types print
    /// their values one after another, as dig does for several queries.
    pub fn short(&self) -> Option<String> {
        use seer_core::output::{dig_short, dig_trace_short};
        Some(match self {
            Payload::Dig(result) => dig_short(result),
            Payload::DigMany(results) => results
                .iter()
                .map(dig_short)
                .filter(|lines| !lines.is_empty())
                .collect::<Vec<_>>()
                .join("\n"),
            Payload::Trace(trace) => dig_trace_short(trace),
            _ => return None,
        })
    }
}

/// `value` as one JSON (pretty) or YAML document, or `None` for the prose
/// formats — for results with no formatter method of their own.
pub fn structured<T: serde::Serialize + ?Sized>(value: &T, format: OutputFormat) -> Option<String> {
    match format {
        OutputFormat::Json => Some(
            serde_json::to_string_pretty(value)
                .unwrap_or_else(|e| format!("{{\"error\":\"{}\"}}", e)),
        ),
        OutputFormat::Yaml => Some(YamlFormatter::new().to_yaml_value(value)),
        OutputFormat::Human | OutputFormat::Markdown => None,
    }
}

pub fn serialize(data: &Payload, format: OutputFormat) -> String {
    let fmt = get_formatter(format);
    match data {
        Payload::Overview(r) => fmt.format_lookup(r),
        Payload::Whois(w) => fmt.format_whois(w),
        Payload::Rdap(r) => fmt.format_rdap(r),
        Payload::Dig(result) => fmt.format_dig(result),
        // One document: the array of results, as `-q` prints it — or one
        // block per type, each with its own header. A human block opens
        // with a blank line of its own; Markdown needs one between a block's
        // closing note and the next heading.
        Payload::DigMany(results) => structured(results, format).unwrap_or_else(|| {
            results
                .iter()
                .map(|result| fmt.format_dig(result))
                .collect::<Vec<_>>()
                .join(if format == OutputFormat::Markdown {
                    "\n\n"
                } else {
                    "\n"
                })
        }),
        Payload::Trace(trace) => fmt.format_dns_trace(trace),
        Payload::Ssl(s) => fmt.format_ssl(s),
        Payload::Status(s) => fmt.format_status(s),
        Payload::Prop(p) => fmt.format_propagation(p),
        Payload::Reverse(records) => fmt.format_dns(records),
        Payload::Avail(a) => fmt.format_availability(a),
        Payload::Tld(t) => fmt.format_tld(t),
        Payload::Dnssec(r) => fmt.format_dnssec(r),
        Payload::Compare(c) => fmt.format_dns_comparison(c),
        Payload::Diff(d) => fmt.format_diff(d),
        Payload::Watch(w) => fmt.format_watch(w),
        // The entries as data; the prose listing is `manage::history_listing`.
        Payload::History(entries) => structured(entries, format)
            .unwrap_or_else(|| "history (raw view not applicable)".to_string()),
        Payload::Subdomains(s) => fmt.format_subdomains(s),
        Payload::SubdomainBaselineDiff(d) => fmt.format_subdomain_baseline_diff(d),
        Payload::Info(i) => fmt.format_domain_info(i),
        Payload::Drift(d) => fmt.format_drift(d),
        Payload::Posture(p) => fmt.format_posture(p),
        Payload::Headers(h) => fmt.format_headers(h),
        Payload::Takeover(t) => fmt.format_takeover(t),
        Payload::Caa(c) => fmt.format_caa(c),
        Payload::Confusables(c) => fmt.format_confusables(c),
        Payload::Delegation(d) => fmt.format_delegation(d),
        Payload::SubdomainClassification(c) => fmt.format_subdomain_classification(c),
        // No formatter method exists for doctor reports; reuse the shared
        // renderer so copied text matches what `seer doctor --format …` prints.
        Payload::Doctor(r) => crate::render_doctor_report(r, format),
    }
}

/// dig and trace results for the CLI's tests.
#[cfg(test)]
pub(crate) mod fixtures {
    use seer_core::dns::{RecordData, RecordType, TraceHop};
    use seer_core::{DnsQueryResult, DnsRecord, DnsStatus, DnsTrace};

    pub fn a(name: &str, address: &str) -> DnsRecord {
        DnsRecord {
            name: name.into(),
            record_type: RecordType::A,
            ttl: 300,
            data: RecordData::A {
                address: address.into(),
            },
        }
    }

    pub fn cname(name: &str, target: &str) -> DnsRecord {
        DnsRecord {
            name: name.into(),
            record_type: RecordType::CNAME,
            ttl: 300,
            data: RecordData::CNAME {
                target: target.into(),
            },
        }
    }

    pub fn mx(name: &str, exchange: &str) -> DnsRecord {
        DnsRecord {
            name: name.into(),
            record_type: RecordType::MX,
            ttl: 300,
            data: RecordData::MX {
                preference: 10,
                exchange: exchange.into(),
            },
        }
    }

    /// A NOERROR answer from the default upstream.
    pub fn dig(record_type: RecordType, answers: Vec<DnsRecord>) -> DnsQueryResult {
        DnsQueryResult {
            name: "www.seer.test".into(),
            record_type,
            server: None,
            answered_locally: false,
            status: DnsStatus::NoError,
            flags: vec!["qr".into(), "rd".into(), "ra".into()],
            answers,
            failed_types: Vec::new(),
            authority: vec![],
            wildcard: None,
            query_time_ms: 12,
        }
    }

    /// A negative or failed answer: `status`, the response header, and no
    /// records.
    pub fn dig_status(record_type: RecordType, status: DnsStatus) -> DnsQueryResult {
        DnsQueryResult {
            status,
            ..dig(record_type, vec![])
        }
    }

    /// A two-hop trace ending at the authoritative answer, or at `error`.
    pub fn trace(answers: Vec<DnsRecord>, error: Option<&str>) -> DnsTrace {
        let hop = |zone: &str, server: &str, answers: Vec<DnsRecord>| TraceHop {
            zone: zone.into(),
            server: server.into(),
            address: "192.0.2.53".into(),
            query_time_ms: 20,
            status: DnsStatus::NoError,
            authoritative: zone != ".",
            referral_zone: (zone == ".").then(|| "seer.test.".into()),
            referral: if zone == "." {
                vec!["ns1.seer.test.".into()]
            } else {
                vec![]
            },
            answers,
            failed_servers: vec![],
        };
        DnsTrace {
            name: "www.seer.test".into(),
            record_type: RecordType::A,
            hops: vec![
                hop(".", "a.root-servers.net.", vec![]),
                hop("seer.test.", "ns1.seer.test.", answers.clone()),
            ],
            status: DnsStatus::NoError,
            answers,
            error: error.map(Into::into),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::fixtures;
    use super::*;
    use seer_core::dns::{RecordData, RecordType};
    use seer_core::{DnsRecord, DnsStatus};

    fn chained() -> seer_core::DnsQueryResult {
        fixtures::dig(
            RecordType::A,
            vec![
                fixtures::cname("www.seer.test", "edge.cdn.test."),
                fixtures::a("edge.cdn.test", "192.0.2.7"),
            ],
        )
    }

    fn mx() -> seer_core::DnsQueryResult {
        fixtures::dig(
            RecordType::MX,
            vec![fixtures::mx("www.seer.test", "mail.seer.test.")],
        )
    }

    /// One type is the result object itself, several an array of them —
    /// the shape `-q` and `--format json` print.
    #[test]
    fn dig_payloads_serialize_as_an_object_or_an_array() {
        let one = Payload::Dig(Box::new(chained()));
        let value = serde_json::to_value(&one).unwrap();
        assert_eq!(value, serde_json::to_value(chained()).unwrap());
        assert_eq!(value["status"], "NOERROR");
        assert_eq!(value["answers"][1]["name"], "edge.cdn.test");

        let many = Payload::DigMany(vec![chained(), mx()]);
        let value = serde_json::to_value(&many).unwrap();
        assert_eq!(value, serde_json::to_value([chained(), mx()]).unwrap());

        let json = serialize(&many, OutputFormat::Json);
        let parsed: Vec<seer_core::DnsQueryResult> =
            serde_json::from_str(&json).expect("--format json is one array document");
        assert_eq!(parsed, vec![chained(), mx()]);
        let json = serialize(&one, OutputFormat::Json);
        let parsed: seer_core::DnsQueryResult =
            serde_json::from_str(&json).expect("one type is one object");
        assert_eq!(parsed, chained());

        let yaml = serialize(&many, OutputFormat::Yaml);
        assert!(
            yaml.trim_start().starts_with("- "),
            "a YAML sequence: {yaml}"
        );
        assert!(yaml.contains("mail.seer.test."), "{yaml}");

        let trace = fixtures::trace(vec![fixtures::a("www.seer.test", "192.0.2.7")], None);
        let value = serde_json::to_value(Payload::Trace(Box::new(trace.clone()))).unwrap();
        assert_eq!(value, serde_json::to_value(&trace).unwrap());
    }

    /// Human and Markdown render one block per type, in the order asked.
    #[test]
    fn dig_many_renders_a_block_per_type() {
        let many = Payload::DigMany(vec![chained(), mx()]);
        let human = serialize(&many, OutputFormat::Human);
        let (a, mx_block) = (
            human.find("DNS A Records: www.seer.test").expect("A block"),
            human
                .find("DNS MX Records: www.seer.test")
                .expect("MX block"),
        );
        assert!(a < mx_block, "{human}");
        assert!(human.contains("edge.cdn.test") && human.contains("mail.seer.test."));

        let markdown = serialize(&many, OutputFormat::Markdown);
        assert_eq!(markdown.matches("## DNS ").count(), 2, "{markdown}");
        assert!(
            markdown.starts_with("## DNS A Records: www.seer.test"),
            "{markdown}"
        );
        assert!(
            markdown.contains(".\n\n## DNS MX Records: www.seer.test"),
            "a blank line before each later block's heading: {markdown}"
        );

        let trace = fixtures::trace(vec![fixtures::a("www.seer.test", "192.0.2.7")], None);
        let human = serialize(&Payload::Trace(Box::new(trace)), OutputFormat::Human);
        assert!(human.contains("DNS Trace: www.seer.test A"), "{human}");
        assert!(human.contains("a.root-servers.net."), "{human}");
    }

    /// `+short`: CNAME targets then values, per type in order; types
    /// without answers add nothing; non-dig payloads have no short form.
    #[test]
    fn short_form_covers_the_dig_payloads_only() {
        assert_eq!(
            Payload::Dig(Box::new(chained())).short().as_deref(),
            Some("edge.cdn.test.\n192.0.2.7")
        );
        let nxdomain = fixtures::dig_status(RecordType::AAAA, DnsStatus::NxDomain);
        assert_eq!(
            Payload::Dig(Box::new(nxdomain.clone())).short().as_deref(),
            Some("")
        );
        assert_eq!(
            Payload::DigMany(vec![chained(), nxdomain, mx()])
                .short()
                .as_deref(),
            Some("edge.cdn.test.\n192.0.2.7\n10 mail.seer.test.")
        );
        let trace = fixtures::trace(vec![fixtures::a("www.seer.test", "192.0.2.7")], None);
        assert_eq!(
            Payload::Trace(Box::new(trace)).short().as_deref(),
            Some("192.0.2.7")
        );
        let stopped = fixtures::trace(vec![], Some("every server failed"));
        assert_eq!(
            Payload::Trace(Box::new(stopped)).short().as_deref(),
            Some("")
        );
        assert_eq!(Payload::Reverse(vec![]).short(), None);
    }

    fn ptr_records() -> Vec<DnsRecord> {
        vec![DnsRecord {
            name: "7.2.0.192.in-addr.arpa".into(),
            record_type: RecordType::PTR,
            ttl: 300,
            data: RecordData::PTR {
                target: "host.seer.test.".into(),
            },
        }]
    }

    /// `reverse` stays a record list: `resolve` output through `format_dns`.
    #[test]
    fn serializes_reverse_as_a_json_list() {
        let out = serialize(&Payload::Reverse(ptr_records()), OutputFormat::Json);
        assert!(out.contains("host.seer.test."));
        assert!(out.trim_start().starts_with('['));
    }

    #[test]
    fn serializes_reverse_as_markdown() {
        let out = serialize(&Payload::Reverse(ptr_records()), OutputFormat::Markdown);
        assert!(out.contains("host.seer.test."));
        assert!(
            out.contains('#') || out.contains('|'),
            "expected markdown structure"
        );
    }

    #[test]
    fn serializes_new_caa_variant() {
        let policy = seer_core::CaaPolicy {
            records: vec![],
            effective_domain: None,
            has_policy: false,
            issuer_match: None,
            iodef: vec![],
            wildcard_note: None,
            note: "no CAA policy found".into(),
        };
        let data = Payload::Caa(Box::new(policy));
        let out = serialize(&data, OutputFormat::Markdown);
        assert!(!out.is_empty());
    }

    /// `-q` prints the payload's JSON, which must be exactly the wrapped
    /// result's — no enum tag, no Box wrapper.
    #[test]
    fn serde_form_is_the_wrapped_result() {
        let records = ptr_records();
        let drift = seer_core::DriftReport::empty("example.com");
        assert_eq!(
            serde_json::to_string(&Payload::Reverse(records.clone())).unwrap(),
            serde_json::to_string(&records).unwrap()
        );
        assert_eq!(
            serde_json::to_string(&Payload::Drift(Box::new(drift.clone()))).unwrap(),
            serde_json::to_string(&drift).unwrap()
        );
    }

    #[test]
    fn kind_labels_are_lowercase() {
        assert_eq!(Payload::Reverse(vec![]).kind(), "reverse");
        assert_eq!(Payload::Dig(Box::new(chained())).kind(), "dig");
        assert_eq!(Payload::DigMany(vec![]).kind(), "dig");
        let trace = fixtures::trace(vec![], None);
        assert_eq!(Payload::Trace(Box::new(trace)).kind(), "trace");
    }
}