mirror of
https://mirror.suhoan.cn/https://github.com/EasyTier/EasyTier.git
synced 2025-12-12 20:57:26 +08:00
make magic dns domain check robust (#1288)
This commit is contained in:
@@ -68,7 +68,7 @@ pub struct Record {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl 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())?;
|
let name = rr::Name::from_str(self.name.as_str())?;
|
||||||
Ok(name)
|
Ok(name)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -70,20 +70,6 @@ pub(super) struct MagicDnsServerInstanceData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl 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>>(
|
pub async fn update_dns_records<'a, T: Iterator<Item = &'a Route>>(
|
||||||
&self,
|
&self,
|
||||||
routes: T,
|
routes: T,
|
||||||
@@ -95,11 +81,6 @@ impl MagicDnsServerInstanceData {
|
|||||||
continue;
|
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 {
|
let Some(ipv4_addr) = route.ipv4_addr.unwrap_or_default().address else {
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
@@ -111,6 +92,12 @@ impl MagicDnsServerInstanceData {
|
|||||||
.ttl(Duration::from_secs(1))
|
.ttl(Duration::from_secs(1))
|
||||||
.build()?;
|
.build()?;
|
||||||
|
|
||||||
|
// check record name valid for dns
|
||||||
|
if let Err(e) = record.name() {
|
||||||
|
tracing::error!("Invalid subdomain label: {}", e);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
records.push(record);
|
records.push(record);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -84,11 +84,23 @@ async fn test_magic_dns_server_instance() {
|
|||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let routes = vec![Route {
|
let routes = vec![
|
||||||
hostname: "test1".to_string(),
|
Route {
|
||||||
ipv4_addr: Some(Ipv4Inet::from_str("8.8.8.8/24").unwrap().into()),
|
hostname: "test1".to_string(),
|
||||||
..Default::default()
|
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
|
dns_server_inst
|
||||||
.data
|
.data
|
||||||
.update_dns_records(routes.iter(), DEFAULT_ET_DNS_ZONE)
|
.update_dns_records(routes.iter(), DEFAULT_ET_DNS_ZONE)
|
||||||
@@ -96,6 +108,7 @@ async fn test_magic_dns_server_instance() {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
check_dns_record(&fake_ip, "test1.et.net", "8.8.8.8").await;
|
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]
|
#[tokio::test]
|
||||||
|
|||||||
Reference in New Issue
Block a user