From 56f430bf10f2eda6d82c59de508307ec8dcf6552 Mon Sep 17 00:00:00 2001 From: Luna Yao <40349250+ZnqbuZ@users.noreply.github.com> Date: Tue, 31 Mar 2026 23:11:56 +0200 Subject: [PATCH] add ttl to ZoneData --- easytier/src/dns/config/dns.rs | 29 +++++++++++++++++------------ easytier/src/dns/config/zone.rs | 9 +++------ easytier/src/dns/peer_mgr.rs | 8 +++----- easytier/src/dns/zone.rs | 1 + easytier/src/proto/dns.proto | 5 +++-- easytier/src/proto/dns.rs | 2 ++ 6 files changed, 29 insertions(+), 25 deletions(-) diff --git a/easytier/src/dns/config/dns.rs b/easytier/src/dns/config/dns.rs index 8288b731..aa6a86ea 100644 --- a/easytier/src/dns/config/dns.rs +++ b/easytier/src/dns/config/dns.rs @@ -26,13 +26,13 @@ pub struct DnsConfig { #[derivative(Default(value = "DNS_DEFAULT_TLD.clone()"))] pub domain: LowerName, #[derivative(Default(value = "vec![DNS_DEFAULT_ADDRESS].into()"))] - #[serde(deserialize_with = "DnsConfig::validate_addresses")] + #[serde(deserialize_with = "DnsConfig::deserialize_addresses")] pub addresses: NameServerAddrGroup, pub listeners: NameServerAddrGroup, } impl DnsConfig { - pub fn validate_addresses<'de, D>(deserializer: D) -> Result + pub fn deserialize_addresses<'de, D>(deserializer: D) -> Result where D: Deserializer<'de>, { @@ -82,32 +82,37 @@ impl DnsConfig { pub type DnsExportConfig = GetExportConfigResponse; pub trait DnsGlobalCtxExt { - fn dns_self_zone(&self) -> Option; + fn dns_self_zone(&self) -> ZoneConfig; fn dns_export_config(&self) -> DnsExportConfig; + fn dns_iter_zones(&self) -> impl Iterator; } impl DnsGlobalCtxExt for GlobalCtx { - fn dns_self_zone(&self) -> Option { + fn dns_self_zone(&self) -> ZoneConfig { let fqdn = self.config.get_dns().get_fqdn(); let ipv4 = self.get_ipv4().map(|ip| ip.address()); let ipv6 = self.get_ipv6().map(|ip| ip.address()); let ipv6 = ipv6.map(|a| vec![a]).unwrap_or_default(); - ZoneConfig::dedicated(Some(self.get_id()), fqdn.clone(), ipv4, ipv6) + ZoneConfig::dedicated(Some(self.get_id()), fqdn.clone(), ipv4, ipv6).unwrap() } fn dns_export_config(&self) -> DnsExportConfig { - let config = self.config.get_dns(); - let zone = self.dns_self_zone(); - let zones = config.zones.iter().chain(zone.iter()); - DnsExportConfig { - zones: zones + zones: self + .dns_iter_zones() .filter(|z| z.policy.export.is_some()) // TODO: check policies of parent zones - .cloned() .map_into() .collect(), - fqdn: config.get_fqdn().to_string(), + fqdn: self.config.get_dns().get_fqdn().to_string(), } } + + fn dns_iter_zones(&self) -> impl Iterator { + self.config + .get_dns() + .zones + .into_iter() + .chain(iter::once(self.dns_self_zone())) + } } diff --git a/easytier/src/dns/config/zone.rs b/easytier/src/dns/config/zone.rs index 2bfe45e5..ec21c780 100644 --- a/easytier/src/dns/config/zone.rs +++ b/easytier/src/dns/config/zone.rs @@ -39,7 +39,7 @@ impl ZoneConfig { origin: LowerName, ipv4: Option, ipv6: Vec, - ) -> Option { + ) -> anyhow::Result { let mut records = Vec::new(); if let Some(ipv4) = ipv4 { @@ -53,10 +53,6 @@ impl ZoneConfig { export: Some(DnsExportPolicy::default()), }; - if records.is_empty() { - return None; - } - let config = ZoneConfigInner { id: id.unwrap_or_else(Uuid::new_v4), origin, @@ -65,7 +61,7 @@ impl ZoneConfig { ..Default::default() }; - config.try_into().ok() + config.try_into() } } @@ -90,6 +86,7 @@ impl From for ZoneData { Self { id: Some(value.id.into()), origin: value.origin.to_string(), + ttl: value.ttl, records: value.records, forwarders: value.forwarders.into(), } diff --git a/easytier/src/dns/peer_mgr.rs b/easytier/src/dns/peer_mgr.rs index 72e88a96..9380abc0 100644 --- a/easytier/src/dns/peer_mgr.rs +++ b/easytier/src/dns/peer_mgr.rs @@ -58,13 +58,10 @@ impl DnsPeerMgr { pub fn snapshot(&self) -> DnsSnapshot { let global_ctx = self.peer_mgr.get_global_ctx_ref(); - let config = global_ctx.config.get_dns(); - let zones = config - .zones - .into_iter() + let zones = global_ctx + .dns_iter_zones() .map_into() - .chain(global_ctx.dns_self_zone().into_iter().map_into()) .chain( self.peers .iter() @@ -72,6 +69,7 @@ impl DnsPeerMgr { ) .collect(); + let config = global_ctx.config.get_dns(); DnsSnapshot { zones, addresses: config.addresses.into(), diff --git a/easytier/src/dns/zone.rs b/easytier/src/dns/zone.rs index bb40e079..5ab04f93 100644 --- a/easytier/src/dns/zone.rs +++ b/easytier/src/dns/zone.rs @@ -160,6 +160,7 @@ impl From for proto::dns::ZoneData { Self { id: Some(value.id.into()), origin: value.origin.to_string(), + ttl: 0, records, forwarders, } diff --git a/easytier/src/proto/dns.proto b/easytier/src/proto/dns.proto index 02c602fb..fd0f8528 100644 --- a/easytier/src/proto/dns.proto +++ b/easytier/src/proto/dns.proto @@ -7,8 +7,9 @@ package dns; message ZoneData { common.UUID id = 1; string origin = 2; - repeated string records = 3; - repeated common.Url forwarders = 4; + uint32 ttl = 3; + repeated string records = 4; + repeated common.Url forwarders = 5; } message GetExportConfigRequest {} diff --git a/easytier/src/proto/dns.rs b/easytier/src/proto/dns.rs index 378d7483..d1526dd8 100644 --- a/easytier/src/proto/dns.rs +++ b/easytier/src/proto/dns.rs @@ -30,6 +30,8 @@ impl Display for ZoneData { } writeln!(f)?; + writeln!(f, "$TTL {}", self.ttl)?; + for record in &self.records { writeln!(f, "{}", record)?; }