mirror of
https://mirror.suhoan.cn/https://github.com/EasyTier/EasyTier.git
synced 2025-12-12 04:37:23 +08:00
make magic dns domain check robust (#1288)
This commit is contained in:
@@ -68,7 +68,7 @@ pub struct Record {
|
||||
}
|
||||
|
||||
impl Record {
|
||||
fn name(&self) -> anyhow::Result<rr::Name> {
|
||||
pub fn name(&self) -> anyhow::Result<rr::Name> {
|
||||
let name = rr::Name::from_str(self.name.as_str())?;
|
||||
Ok(name)
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -84,11 +84,23 @@ async fn test_magic_dns_server_instance() {
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let routes = vec![Route {
|
||||
hostname: "test1".to_string(),
|
||||
ipv4_addr: Some(Ipv4Inet::from_str("8.8.8.8/24").unwrap().into()),
|
||||
..Default::default()
|
||||
}];
|
||||
let routes = vec![
|
||||
Route {
|
||||
hostname: "test1".to_string(),
|
||||
ipv4_addr: Some(Ipv4Inet::from_str("8.8.8.8/24").unwrap().into()),
|
||||
..Default::default()
|
||||
},
|
||||
Route {
|
||||
hostname: "中文".to_string(),
|
||||
ipv4_addr: Some(Ipv4Inet::from_str("8.8.8.8/24").unwrap().into()),
|
||||
..Default::default()
|
||||
},
|
||||
Route {
|
||||
hostname: ".invalid".to_string(),
|
||||
ipv4_addr: Some(Ipv4Inet::from_str("8.8.8.8/24").unwrap().into()),
|
||||
..Default::default()
|
||||
},
|
||||
];
|
||||
dns_server_inst
|
||||
.data
|
||||
.update_dns_records(routes.iter(), DEFAULT_ET_DNS_ZONE)
|
||||
@@ -96,6 +108,7 @@ async fn test_magic_dns_server_instance() {
|
||||
.unwrap();
|
||||
|
||||
check_dns_record(&fake_ip, "test1.et.net", "8.8.8.8").await;
|
||||
check_dns_record(&fake_ip, "中文.et.net", "8.8.8.8").await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
|
||||
Reference in New Issue
Block a user