utils: add a parse function for DNS name

This commit is contained in:
Luna Yao
2026-04-06 11:54:01 +02:00
parent 75ad15c324
commit 6836d61127
+11
View File
@@ -6,6 +6,7 @@ use serde_with::{DeserializeFromStr, SerializeDisplay};
use std::fmt::{Display, Formatter}; use std::fmt::{Display, Formatter};
use std::net::{IpAddr, SocketAddr}; use std::net::{IpAddr, SocketAddr};
use std::str::FromStr; use std::str::FromStr;
use hickory_proto::rr::LowerName;
use url::Url; use url::Url;
pub fn sanitize(name: &str) -> String { pub fn sanitize(name: &str) -> String {
@@ -34,6 +35,16 @@ pub fn sanitize(name: &str) -> String {
name name
} }
pub fn parse(name: &str) -> LowerName {
if let Ok(name) = name.parse() {
name
} else {
let sanitized = sanitize(&name);
tracing::debug!("invalid hostname: {}, sanitized to: {}", name, sanitized);
sanitized.parse().unwrap_or_default()
}
}
static DNS_SUPPORTED_PROTOCOLS: [Protocol; 2] = [ static DNS_SUPPORTED_PROTOCOLS: [Protocol; 2] = [
Protocol::Udp, Protocol::Udp,
Protocol::Tcp, Protocol::Tcp,