move sanitize and parse

fmt dns

dns utils

fmt

unit test

unit test
This commit is contained in:
Luna Yao
2026-04-19 00:34:55 +02:00
parent ae4e9b3513
commit 185eb1e4c2
3 changed files with 66 additions and 47 deletions
+1 -1
View File
@@ -3,8 +3,8 @@ use crate::dns::config::zone::ZoneConfig;
use crate::dns::config::{DNS_DEFAULT_ADDRESS, DNS_DEFAULT_DOMAIN};
use crate::dns::server::DnsServer;
use crate::dns::utils::addr::NameServerAddrGroup;
use crate::dns::utils::parse;
use crate::proto::dns::GetExportConfigResponse;
use crate::utils::dns::parse;
use derivative::Derivative;
use hickory_net::xfer::Protocol;
use hickory_proto::rr::{LowerName, Name};
-39
View File
@@ -1,42 +1,3 @@
use hickory_proto::rr::LowerName;
use idna::AsciiDenyList;
pub mod addr;
pub mod response;
pub mod zone_handler;
pub fn sanitize(name: &str) -> String {
let dot = name.ends_with('.');
let mut name = idna::domain_to_ascii_cow(name.as_ref(), AsciiDenyList::EMPTY)
.unwrap_or_default()
.into_owned()
.to_lowercase()
.split('.')
.map(|label| {
label
.chars()
.map(|c| if c.is_ascii_alphanumeric() { c } else { '-' })
.take(63)
.collect::<String>()
.trim_matches('-')
.to_string()
})
.filter(|label| !label.is_empty())
.collect::<Vec<_>>()
.join(".");
name.truncate(253);
if dot {
name.push('.');
}
name
}
pub fn parse(name: &str) -> LowerName {
if let Ok(name) = name.parse() {
name
} else {
let sanitized = sanitize(name);
tracing::debug!("invalid name: {}, sanitized to: {}", name, sanitized);
sanitized.parse().unwrap_or_default()
}
}