make magic dns domain check robust (#1288)

This commit is contained in:
Sijie.Sun
2025-08-24 18:24:42 +08:00
committed by GitHub
parent 0804fd6632
commit 3299a77da3
3 changed files with 25 additions and 25 deletions
@@ -70,20 +70,6 @@ pub(super) struct MagicDnsServerInstanceData {
}
impl MagicDnsServerInstanceData {
fn is_valid_subdomain_label(s: &str) -> bool {
let s = s.trim();
// 长度检查:1-63 个字符
if s.is_empty() || s.len() > 63 {
return false;
}
// 检查每个字符是否合法,并确保不以 '-' 开头或结尾
s.chars().all(|c| matches!(c, 'a'..='z' | '0'..='9' | '-'))
&& !s.starts_with('-')
&& !s.ends_with('-')
}
pub async fn update_dns_records<'a, T: Iterator<Item = &'a Route>>(
&self,
routes: T,
@@ -95,11 +81,6 @@ impl MagicDnsServerInstanceData {
continue;
}
// check host name valid for dns
if !Self::is_valid_subdomain_label(&route.hostname) {
continue;
}
let Some(ipv4_addr) = route.ipv4_addr.unwrap_or_default().address else {
continue;
};
@@ -111,6 +92,12 @@ impl MagicDnsServerInstanceData {
.ttl(Duration::from_secs(1))
.build()?;
// check record name valid for dns
if let Err(e) = record.name() {
tracing::error!("Invalid subdomain label: {}", e);
continue;
}
records.push(record);
}