seer-core 0.43.1

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
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
//! Upstream nameserver specification parsing.
//!
//! Every surface that accepts a custom nameserver (CLI `--server`/positional
//! nameservers, REPL `@server`, config `nameserver`, Python/REST/MCP) passes
//! an opaque string into [`crate::dns::DnsResolver`]'s custom-nameserver
//! path. This module is the single parser for that string, so all surfaces
//! get every transport for free:
//!
//! - **UDP** (classic DNS, port 53): `8.8.8.8`, `dns.google`,
//!   `9.9.9.9:5353`, `[2001:4860:4860::8888]:53`
//! - **DoT** (DNS over TLS, port 853): `tls://1.1.1.1`,
//!   `tls://dns.quad9.net:853`
//! - **DoH** (DNS over HTTPS, port 443, path `/dns-query`):
//!   `https://cloudflare-dns.com/dns-query`, `https://dns.google:443`
//!
//! Bare IPs/hostnames keep their historical UDP behavior byte-for-byte; the
//! `host:port` and bracketed-IPv6 forms are additive. Unbracketed IPv6
//! literals are always parsed as a plain address — bracket them
//! (`[2001:db8::1]:5353`) to attach a port.

use std::net::{IpAddr, Ipv6Addr};

use crate::error::{Result, SeerError};

/// Default port for plain (UDP) DNS.
const UDP_PORT: u16 = 53;
/// Default port for DNS over TLS (RFC 7858).
const TLS_PORT: u16 = 853;
/// Default port for DNS over HTTPS (RFC 8484).
const HTTPS_PORT: u16 = 443;
/// Conventional DoH query path used by every major public resolver.
const DEFAULT_DOH_PATH: &str = "/dns-query";

/// Transport protocol for an upstream nameserver.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NameserverProtocol {
    /// Classic DNS over UDP (with no scheme prefix).
    Udp,
    /// DNS over TLS (`tls://`), RFC 7858.
    Tls,
    /// DNS over HTTPS (`https://`), RFC 8484.
    Https,
}

/// A parsed upstream nameserver specification.
///
/// Produced by [`NameserverSpec::parse`] from the opaque nameserver strings
/// accepted everywhere in seer (CLI flags, REPL `@server`, `config.toml`).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NameserverSpec {
    /// Transport to speak to the upstream server.
    pub protocol: NameserverProtocol,
    /// Hostname or IP literal (IPv6 stored unbracketed).
    pub host: String,
    /// Target port (defaulted per protocol: 53 / 853 / 443).
    pub port: u16,
    /// DoH query path. Always `Some` for [`NameserverProtocol::Https`]
    /// (defaulting to `/dns-query`), always `None` otherwise.
    pub path: Option<String>,
}

impl NameserverSpec {
    /// Parses a nameserver specification string.
    ///
    /// Accepted forms:
    /// - `IP`, `hostname`, `host:port`, `[v6]`, `[v6]:port` → UDP (port 53)
    /// - `tls://host[:port]` → DoT (port 853)
    /// - `https://host[:port][/path]` → DoH (port 443, path `/dns-query`)
    ///
    /// # Errors
    /// Returns [`SeerError::InvalidInput`] for empty input, embedded
    /// whitespace, unknown schemes, malformed brackets, bad ports, a path on
    /// a `tls://` spec, or credentials in an `https://` spec.
    pub fn parse(input: &str) -> Result<Self> {
        let spec = input.trim();
        if spec.is_empty() {
            return Err(SeerError::InvalidInput(
                "nameserver must not be empty".to_string(),
            ));
        }
        if spec.chars().any(|c| c.is_whitespace() || c.is_control()) {
            return Err(SeerError::InvalidInput(format!(
                "nameserver must not contain whitespace: {spec:?}"
            )));
        }

        if let Some((scheme, rest)) = spec.split_once("://") {
            match scheme.to_ascii_lowercase().as_str() {
                "tls" => Self::parse_tls(rest),
                "https" => Self::parse_https(rest),
                other => Err(SeerError::InvalidInput(format!(
                    "unsupported nameserver scheme '{other}://' — use a bare IP/hostname (UDP), \
                     tls://host[:port] (DNS over TLS), or https://host[/path] (DNS over HTTPS)"
                ))),
            }
        } else {
            let (host, port) = parse_host_port(spec, UDP_PORT)?;
            Ok(Self {
                protocol: NameserverProtocol::Udp,
                host,
                port,
                path: None,
            })
        }
    }

    /// Parses the remainder of a `tls://` spec (`host[:port]`, no path).
    fn parse_tls(rest: &str) -> Result<Self> {
        if rest.contains('/') {
            return Err(SeerError::InvalidInput(format!(
                "tls:// nameservers do not take a path: tls://{rest} \
                 (paths are a DNS-over-HTTPS concept — use https:// for DoH)"
            )));
        }
        let (host, port) = parse_host_port(rest, TLS_PORT)?;
        Ok(Self {
            protocol: NameserverProtocol::Tls,
            host,
            port,
            path: None,
        })
    }

    /// Parses the remainder of an `https://` spec (`host[:port][/path]`).
    fn parse_https(rest: &str) -> Result<Self> {
        let (authority, path) = match rest.find('/') {
            Some(i) => (&rest[..i], rest[i..].to_string()),
            None => (rest, DEFAULT_DOH_PATH.to_string()),
        };
        if authority.contains('@') {
            return Err(SeerError::InvalidInput(format!(
                "credentials are not supported in DoH nameserver URLs: https://{rest}"
            )));
        }
        let (host, port) = parse_host_port(authority, HTTPS_PORT)?;
        Ok(Self {
            protocol: NameserverProtocol::Https,
            host,
            port,
            path: Some(path),
        })
    }

    /// The TLS server name presented for certificate verification (DoT/DoH).
    ///
    /// For hostname specs this is the hostname. For IP-literal specs it is
    /// the IP itself: rustls accepts IP-address server names and verifies
    /// them against the certificate's IP SANs, which the major public
    /// resolvers (1.1.1.1, 8.8.8.8, 9.9.9.9, …) all carry.
    pub fn tls_name(&self) -> &str {
        &self.host
    }
}

impl std::fmt::Display for NameserverSpec {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let host = if self.host.contains(':') {
            format!("[{}]", self.host)
        } else {
            self.host.clone()
        };
        match self.protocol {
            NameserverProtocol::Udp => write!(f, "{}:{}", host, self.port),
            NameserverProtocol::Tls => write!(f, "tls://{}:{}", host, self.port),
            NameserverProtocol::Https => write!(
                f,
                "https://{}:{}{}",
                host,
                self.port,
                self.path.as_deref().unwrap_or(DEFAULT_DOH_PATH)
            ),
        }
    }
}

/// Splits `host[:port]` (with bracketed-IPv6 support) into its parts,
/// applying `default_port` when no port is given.
///
/// Unbracketed strings that parse as an [`IpAddr`] are returned whole — an
/// IPv6 literal's trailing hextet is never misread as a port. Hostnames
/// cannot legally contain `:`, so for everything else a single trailing
/// `:port` is split off and validated.
fn parse_host_port(s: &str, default_port: u16) -> Result<(String, u16)> {
    if s.is_empty() {
        return Err(SeerError::InvalidInput(
            "nameserver host must not be empty".to_string(),
        ));
    }

    // Bracketed IPv6: [addr] or [addr]:port
    if let Some(rest) = s.strip_prefix('[') {
        let Some((addr, after)) = rest.split_once(']') else {
            return Err(SeerError::InvalidInput(format!(
                "unterminated '[' in nameserver address: {s}"
            )));
        };
        if addr.parse::<Ipv6Addr>().is_err() {
            return Err(SeerError::InvalidInput(format!(
                "invalid IPv6 literal in nameserver address: [{addr}]"
            )));
        }
        let port = match after {
            "" => default_port,
            _ => match after.strip_prefix(':') {
                Some(p) => parse_port(p)?,
                None => {
                    return Err(SeerError::InvalidInput(format!(
                        "expected ':port' after ']' in nameserver address: {s}"
                    )));
                }
            },
        };
        return Ok((addr.to_string(), port));
    }

    // A full IP literal (IPv4, or unbracketed IPv6 — which may contain many
    // colons) never has a port suffix.
    if s.parse::<IpAddr>().is_ok() {
        return Ok((s.to_string(), default_port));
    }

    // host:port — hostnames cannot contain ':'.
    if let Some((host, port)) = s.rsplit_once(':') {
        if host.contains(':') {
            return Err(SeerError::InvalidInput(format!(
                "invalid nameserver address {s}: bracket IPv6 literals to add \
                 a port (e.g. [2001:db8::1]:53)"
            )));
        }
        if host.is_empty() {
            return Err(SeerError::InvalidInput(format!(
                "nameserver host must not be empty: {s}"
            )));
        }
        return Ok((host.to_string(), parse_port(port)?));
    }

    Ok((s.to_string(), default_port))
}

/// Parses a decimal port number, rejecting 0, signs, and non-digits.
fn parse_port(s: &str) -> Result<u16> {
    // Reject sign characters explicitly: u16::from_str accepts a leading '+'.
    if s.is_empty() || !s.bytes().all(|b| b.is_ascii_digit()) {
        return Err(SeerError::InvalidInput(format!(
            "invalid nameserver port: {s:?} (expected 1-65535)"
        )));
    }
    match s.parse::<u16>() {
        Ok(p) if p != 0 => Ok(p),
        _ => Err(SeerError::InvalidInput(format!(
            "invalid nameserver port: {s} (expected 1-65535)"
        ))),
    }
}

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

    fn parse(s: &str) -> NameserverSpec {
        NameserverSpec::parse(s).unwrap_or_else(|e| panic!("{s:?} must parse: {e}"))
    }

    fn parse_err(s: &str) -> String {
        match NameserverSpec::parse(s) {
            Err(SeerError::InvalidInput(msg)) => msg,
            Err(other) => panic!("{s:?} must fail with InvalidInput, got: {other:?}"),
            Ok(spec) => panic!("{s:?} must fail to parse, got: {spec:?}"),
        }
    }

    // --- UDP forms (historical behavior + additive host:port) ---------

    #[test]
    fn udp_bare_ipv4() {
        let spec = parse("8.8.8.8");
        assert_eq!(spec.protocol, NameserverProtocol::Udp);
        assert_eq!(spec.host, "8.8.8.8");
        assert_eq!(spec.port, 53);
        assert_eq!(spec.path, None);
    }

    #[test]
    fn udp_ipv4_with_port() {
        let spec = parse("9.9.9.9:5353");
        assert_eq!(spec.protocol, NameserverProtocol::Udp);
        assert_eq!(spec.host, "9.9.9.9");
        assert_eq!(spec.port, 5353);
    }

    #[test]
    fn udp_hostname() {
        let spec = parse("dns.google");
        assert_eq!(spec.protocol, NameserverProtocol::Udp);
        assert_eq!(spec.host, "dns.google");
        assert_eq!(spec.port, 53);
    }

    #[test]
    fn udp_hostname_with_port() {
        let spec = parse("dns.google:5353");
        assert_eq!(spec.host, "dns.google");
        assert_eq!(spec.port, 5353);
    }

    #[test]
    fn udp_unbracketed_ipv6_is_whole_address() {
        // The trailing hextet must NOT be misread as a port.
        let spec = parse("2606:4700:4700::1111");
        assert_eq!(spec.protocol, NameserverProtocol::Udp);
        assert_eq!(spec.host, "2606:4700:4700::1111");
        assert_eq!(spec.port, 53);
    }

    #[test]
    fn udp_bracketed_ipv6_without_port() {
        let spec = parse("[2606:4700:4700::1111]");
        assert_eq!(spec.host, "2606:4700:4700::1111");
        assert_eq!(spec.port, 53);
    }

    #[test]
    fn udp_bracketed_ipv6_with_port() {
        let spec = parse("[2001:4860:4860::8888]:5353");
        assert_eq!(spec.protocol, NameserverProtocol::Udp);
        assert_eq!(spec.host, "2001:4860:4860::8888");
        assert_eq!(spec.port, 5353);
    }

    #[test]
    fn udp_input_is_trimmed() {
        let spec = parse("  8.8.8.8  ");
        assert_eq!(spec.host, "8.8.8.8");
    }

    // --- DoT forms -----------------------------------------------------

    #[test]
    fn tls_ip_default_port() {
        let spec = parse("tls://1.1.1.1");
        assert_eq!(spec.protocol, NameserverProtocol::Tls);
        assert_eq!(spec.host, "1.1.1.1");
        assert_eq!(spec.port, 853);
        assert_eq!(spec.path, None);
        assert_eq!(spec.tls_name(), "1.1.1.1");
    }

    #[test]
    fn tls_hostname_with_port() {
        let spec = parse("tls://dns.quad9.net:8853");
        assert_eq!(spec.protocol, NameserverProtocol::Tls);
        assert_eq!(spec.host, "dns.quad9.net");
        assert_eq!(spec.port, 8853);
        assert_eq!(spec.tls_name(), "dns.quad9.net");
    }

    #[test]
    fn tls_bracketed_ipv6_with_port() {
        let spec = parse("tls://[2606:4700:4700::1111]:853");
        assert_eq!(spec.host, "2606:4700:4700::1111");
        assert_eq!(spec.port, 853);
    }

    #[test]
    fn tls_scheme_is_case_insensitive() {
        let spec = parse("TLS://1.1.1.1");
        assert_eq!(spec.protocol, NameserverProtocol::Tls);
    }

    #[test]
    fn tls_rejects_path() {
        let msg = parse_err("tls://dns.quad9.net/dns-query");
        assert!(msg.contains("do not take a path"), "got: {msg}");
    }

    #[test]
    fn tls_rejects_empty_host() {
        parse_err("tls://");
    }

    // --- DoH forms -----------------------------------------------------

    #[test]
    fn https_hostname_defaults_port_and_path() {
        let spec = parse("https://cloudflare-dns.com");
        assert_eq!(spec.protocol, NameserverProtocol::Https);
        assert_eq!(spec.host, "cloudflare-dns.com");
        assert_eq!(spec.port, 443);
        assert_eq!(spec.path.as_deref(), Some("/dns-query"));
    }

    #[test]
    fn https_explicit_path() {
        let spec = parse("https://dns.google/resolve");
        assert_eq!(spec.host, "dns.google");
        assert_eq!(spec.path.as_deref(), Some("/resolve"));
    }

    #[test]
    fn https_port_and_path() {
        let spec = parse("https://doh.example.net:8443/custom/query");
        assert_eq!(spec.host, "doh.example.net");
        assert_eq!(spec.port, 8443);
        assert_eq!(spec.path.as_deref(), Some("/custom/query"));
    }

    #[test]
    fn https_ip_literal_host() {
        let spec = parse("https://8.8.8.8/dns-query");
        assert_eq!(spec.host, "8.8.8.8");
        assert_eq!(spec.tls_name(), "8.8.8.8");
    }

    #[test]
    fn https_bracketed_ipv6_with_port_and_path() {
        let spec = parse("https://[2606:4700:4700::1111]:443/dns-query");
        assert_eq!(spec.host, "2606:4700:4700::1111");
        assert_eq!(spec.port, 443);
        assert_eq!(spec.path.as_deref(), Some("/dns-query"));
    }

    #[test]
    fn https_rejects_credentials() {
        let msg = parse_err("https://user:pass@dns.example.net/dns-query");
        assert!(msg.contains("credentials"), "got: {msg}");
    }

    // --- Garbage and scheme typos ---------------------------------------

    #[test]
    fn rejects_empty_and_blank() {
        parse_err("");
        parse_err("   ");
    }

    #[test]
    fn rejects_embedded_whitespace() {
        parse_err("8.8.8.8 example.com");
        parse_err("tls://dns\t.quad9.net");
    }

    #[test]
    fn rejects_unknown_schemes() {
        for bad in [
            "http://dns.google",
            "udp://8.8.8.8",
            "tcp://8.8.8.8",
            "quic://1.1.1.1",
        ] {
            let msg = parse_err(bad);
            assert!(
                msg.contains("unsupported nameserver scheme"),
                "{bad}: {msg}"
            );
        }
    }

    #[test]
    fn rejects_scheme_typos_as_bad_ports() {
        // "tls:/1.1.1.1" has no "://" so it falls into the UDP host:port
        // path, where the "/1.1.1.1" suffix is an invalid port.
        parse_err("tls:/1.1.1.1");
    }

    #[test]
    fn rejects_bad_ports() {
        parse_err("8.8.8.8:0");
        parse_err("8.8.8.8:65536");
        parse_err("8.8.8.8:abc");
        parse_err("8.8.8.8:+53"); // u16::from_str accepts '+'; the parser must not
        parse_err("dns.google:");
        parse_err("tls://dns.quad9.net:");
    }

    #[test]
    fn rejects_malformed_brackets() {
        parse_err("[2001:db8::1"); // unterminated
        parse_err("[not-an-ip]");
        parse_err("[2001:db8::1]junk"); // garbage after bracket
        parse_err("[]:53"); // empty literal
    }

    #[test]
    fn rejects_unbracketed_ipv6_plus_port_shape() {
        // Not a valid IPv6 literal and the "host" still contains colons —
        // must point the user at the bracketed form.
        let msg = parse_err("2001:db8::zz:53");
        assert!(msg.contains("bracket IPv6"), "got: {msg}");
    }

    #[test]
    fn rejects_empty_host_with_port() {
        parse_err(":53");
    }

    // --- Display ---------------------------------------------------------

    #[test]
    fn display_round_trips_reasonably() {
        assert_eq!(parse("8.8.8.8").to_string(), "8.8.8.8:53");
        assert_eq!(parse("tls://1.1.1.1").to_string(), "tls://1.1.1.1:853");
        assert_eq!(
            parse("https://dns.google").to_string(),
            "https://dns.google:443/dns-query"
        );
        assert_eq!(
            parse("[2001:db8::1]:5353").to_string(),
            "[2001:db8::1]:5353"
        );
    }
}