config: let get_name return LowerName

This commit is contained in:
Luna Yao
2026-04-06 11:54:01 +02:00
parent 6836d61127
commit 9d48cefd05
+8 -14
View File
@@ -1,13 +1,14 @@
use crate::dns::utils::{sanitize, NameServerAddr}; use crate::dns::utils::{parse, sanitize, NameServerAddr};
use crate::proto::dns::{DnsConfigPb, ZoneConfigPb}; use crate::proto::dns::{DnsConfigPb, ZoneConfigPb};
use derive_more::{Deref, DerefMut}; use derive_more::{Deref, DerefMut};
use gethostname::gethostname; use gethostname::gethostname;
use hickory_proto::rr::LowerName; use hickory_proto::rr::{IntoName, LowerName, Name};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::collections::HashMap; use std::collections::HashMap;
use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4}; use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4};
use std::str::FromStr; use std::str::FromStr;
use std::sync::LazyLock; use std::sync::LazyLock;
use url::Url;
use uuid::Uuid; use uuid::Uuid;
pub const DNS_DEFAULT_ADDRESS: SocketAddr = pub const DNS_DEFAULT_ADDRESS: SocketAddr =
@@ -29,23 +30,16 @@ pub struct DnsConfig {
} }
impl DnsConfig { impl DnsConfig {
pub fn get_name(&self) -> String { pub fn get_name(&self) -> LowerName {
if self.name.is_empty() { if self.name.is_empty() {
gethostname().to_string_lossy().to_string() parse(gethostname().to_string_lossy().as_ref())
} else { } else {
self.name.to_string() self.name.clone()
} }
} }
pub fn set_name(&mut self, name: &str) { pub fn set_name(&mut self, name: &str) {
self.name = match LowerName::from_str(name) { self.name = parse(name);
Ok(name) => name,
Err(_) => {
let sanitized = sanitize(name);
tracing::debug!("invalid hostname: {}, sanitized to: {}", name, sanitized);
LowerName::from_str(&sanitized).unwrap_or_default()
}
};
} }
pub fn export(&self) -> DnsConfigPb { pub fn export(&self) -> DnsConfigPb {
@@ -57,7 +51,7 @@ impl DnsConfig {
.map(Into::into) .map(Into::into)
.collect(), .collect(),
name: self.get_name(), name: self.get_name().to_string(),
domain: self.domain.to_string(), domain: self.domain.to_string(),
} }
} }