83 lines
3.1 KiB
Rust
83 lines
3.1 KiB
Rust
//! Upstream server address syntax.
|
|
use std::net::SocketAddr;
|
|
|
|
/// Protocol used to talk to an upstream.
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize)]
|
|
#[serde(rename_all = "lowercase")]
|
|
pub enum UpstreamProto {
|
|
Udp,
|
|
Tcp,
|
|
Tls,
|
|
Https,
|
|
}
|
|
|
|
/// Parsed upstream: `1.1.1.1`, `1.1.1.1:53`, `[2606:4700::1111]:53`, `tcp://...`,
|
|
/// `tls://1.1.1.1:853#cloudflare-dns.com`, `https://1.1.1.1/dns-query#cloudflare-dns.com`.
|
|
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize)]
|
|
pub struct UpstreamAddr {
|
|
pub proto: UpstreamProto,
|
|
pub addr: SocketAddr,
|
|
/// TLS server name (SNI / certificate name) for tls/https.
|
|
pub tls_name: Option<String>,
|
|
/// HTTP path for https.
|
|
pub path: String,
|
|
pub original: String,
|
|
}
|
|
|
|
impl UpstreamAddr {
|
|
pub fn parse(s: &str) -> Result<Self, String> {
|
|
let original = s.to_string();
|
|
let (proto, rest) = if let Some(r) = s.strip_prefix("udp://") {
|
|
(UpstreamProto::Udp, r)
|
|
} else if let Some(r) = s.strip_prefix("tcp://") {
|
|
(UpstreamProto::Tcp, r)
|
|
} else if let Some(r) = s.strip_prefix("tls://") {
|
|
(UpstreamProto::Tls, r)
|
|
} else if let Some(r) = s.strip_prefix("https://") {
|
|
(UpstreamProto::Https, r)
|
|
} else {
|
|
(UpstreamProto::Udp, s)
|
|
};
|
|
let (rest, tls_name) = match rest.split_once('#') {
|
|
Some((a, n)) => (a, Some(n.to_string())),
|
|
None => (rest, None),
|
|
};
|
|
let (hostport, path) = match rest.find('/') {
|
|
Some(i) => (&rest[..i], rest[i..].to_string()),
|
|
None => (rest, "/dns-query".to_string()),
|
|
};
|
|
let default_port = match proto {
|
|
UpstreamProto::Udp | UpstreamProto::Tcp => 53,
|
|
UpstreamProto::Tls => 853,
|
|
UpstreamProto::Https => 443,
|
|
};
|
|
let addr: SocketAddr = if let Ok(a) = hostport.parse() {
|
|
a
|
|
} else if let Ok(ip) = hostport.trim_start_matches('[').trim_end_matches(']').parse::<std::net::IpAddr>() {
|
|
SocketAddr::new(ip, default_port)
|
|
} else {
|
|
return Err(format!("invalid upstream '{s}': must be an IP address (optionally with port); use '#name' for the TLS name"));
|
|
};
|
|
if matches!(proto, UpstreamProto::Tls | UpstreamProto::Https) && tls_name.is_none() {
|
|
return Err(format!("upstream '{s}': TLS upstreams require '#<server-name>' for certificate validation"));
|
|
}
|
|
Ok(Self { proto, addr, tls_name, path, original })
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
#[test]
|
|
fn parse_forms() {
|
|
assert_eq!(UpstreamAddr::parse("1.1.1.1").unwrap().addr.port(), 53);
|
|
assert_eq!(UpstreamAddr::parse("[::1]:5353").unwrap().addr.port(), 5353);
|
|
let t = UpstreamAddr::parse("tls://1.1.1.1#cloudflare-dns.com").unwrap();
|
|
assert_eq!((t.proto, t.addr.port()), (UpstreamProto::Tls, 853));
|
|
let h = UpstreamAddr::parse("https://9.9.9.9/dns-query#dns.quad9.net").unwrap();
|
|
assert_eq!(h.path, "/dns-query");
|
|
assert!(UpstreamAddr::parse("tls://1.1.1.1").is_err());
|
|
assert!(UpstreamAddr::parse("dns.google").is_err());
|
|
}
|
|
}
|