From 78b2afb785412f37c5835b50dad91b1fe7f4a385 Mon Sep 17 00:00:00 2001 From: Luna Yao <40349250+ZnqbuZ@users.noreply.github.com> Date: Tue, 17 Feb 2026 15:36:09 +0100 Subject: [PATCH] config: add DNS import/export policies config: move ID generation to ZoneConfig config: serde fixes --- easytier/Cargo.toml | 3 +- easytier/src/dns/config.rs | 98 +++++++++++++++++++-------- easytier/src/peers/peer_ospf_route.rs | 3 +- easytier/src/proto/dns.proto | 23 +++---- 4 files changed, 79 insertions(+), 48 deletions(-) diff --git a/easytier/Cargo.toml b/easytier/Cargo.toml index 204fadf4..0b2d8b6a 100644 --- a/easytier/Cargo.toml +++ b/easytier/Cargo.toml @@ -167,7 +167,8 @@ network-interface = "2.0" # for ospf route petgraph = "0.8.1" hashbrown = "0.15.3" -ordered_hash_map = "0.5.0" +ordered_hash_map = "0.5.0"# TODO: REPLACE THIS +indexmap = "2.13.0" # for wireguard boringtun = { package = "boringtun-easytier", version = "0.6.1", optional = true } diff --git a/easytier/src/dns/config.rs b/easytier/src/dns/config.rs index f0cbe0a0..f59fdc01 100644 --- a/easytier/src/dns/config.rs +++ b/easytier/src/dns/config.rs @@ -1,11 +1,14 @@ use crate::dns::utils::{sanitize, NameServerAddr}; -use crate::proto::dns::{DnsConfigKind, DnsConfigPb, ZoneConfigPb}; +use crate::proto::dns::{DnsConfigPb, ZoneConfigPb}; +use derive_more::{Deref, DerefMut}; use gethostname::gethostname; use hickory_proto::rr::LowerName; use serde::{Deserialize, Serialize}; +use std::collections::HashMap; use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4}; use std::str::FromStr; use std::sync::LazyLock; +use uuid::Uuid; pub const DNS_DEFAULT_ADDRESS: SocketAddr = SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(100, 100, 100, 101), 53)); @@ -17,6 +20,8 @@ pub static DNS_DEFAULT_TLD: LazyLock = pub struct DnsConfig { #[serde(rename = "zone")] pub zones: Vec, + #[serde(flatten)] + pub policies: HashMap, name: LowerName, pub domain: LowerName, pub addresses: Vec, @@ -43,34 +48,17 @@ impl DnsConfig { }; } - pub fn to_pb(&self, kind: DnsConfigKind) -> DnsConfigPb { - let pb = DnsConfigPb { - kind: kind.into(), + pub fn export(&self) -> DnsConfigPb { + DnsConfigPb { + zones: self + .zones + .iter() + .filter(|z| z.policy.export.is_some()) // TODO: check policies of parent zones + .map(Into::into) + .collect(), + name: self.get_name(), domain: self.domain.to_string(), - - ..Default::default() - }; - - match kind { - DnsConfigKind::Local => DnsConfigPb { - zones: self.zones.iter().map(Into::into).collect(), - addresses: self.addresses.clone().into_iter().map(Into::into).collect(), - listeners: self.listeners.iter().map(ToString::to_string).collect(), - - ..pb - }, - - DnsConfigKind::Remote => DnsConfigPb { - zones: self - .zones - .iter() - .filter(|z| z.broadcast) - .map(Into::into) - .collect(), - - ..pb - }, } } } @@ -78,19 +66,21 @@ impl DnsConfig { impl Default for DnsConfig { fn default() -> Self { Self { + zones: Vec::new(), + policies: HashMap::new(), name: LowerName::default(), domain: DNS_DEFAULT_TLD.clone(), addresses: vec![DNS_DEFAULT_ADDRESS], listeners: vec![], - zones: vec![], } } } #[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Default)] pub struct ZoneConfig { - #[serde(default)] - pub broadcast: bool, + #[serde(default = "Uuid::new_v4")] + #[serde(skip_serializing)] + id: Uuid, pub origin: LowerName, #[serde(default)] pub ttl: u32, @@ -98,11 +88,14 @@ pub struct ZoneConfig { pub records: Vec, #[serde(default)] pub forwarders: Vec, + #[serde(flatten)] + pub policy: ZonePolicyConfig, } impl From<&ZoneConfig> for ZoneConfigPb { fn from(value: &ZoneConfig) -> Self { Self { + id: Some(value.id.into()), origin: value.origin.to_string(), ttl: value.ttl, records: value.records.clone(), @@ -110,3 +103,48 @@ impl From<&ZoneConfig> for ZoneConfigPb { } } } + +#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Default)] +#[serde(default)] +pub struct AclPolicy { + pub whitelist: Option>, + pub blacklist: Option>, +} + +#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Default, Deref, DerefMut)] +#[serde(default)] +pub struct FunctionalityPolicy { + #[serde(flatten)] + #[deref] + #[deref_mut] + acl: AclPolicy, // TODO + pub disabled: bool, +} + +#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Default, Deref, DerefMut)] +#[serde(default)] +pub struct DnsPolicy

{ + #[serde(flatten)] + #[deref] + #[deref_mut] + policy: P, + pub recursive: bool, // TODO +} + +pub type ZoneExportPolicy = FunctionalityPolicy; +pub type DnsExportPolicy = DnsPolicy; +pub type DnsImportPolicy = DnsPolicy; + +#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Default)] +#[serde(default)] +pub struct DnsPolicyConfig { + pub import: DnsImportPolicy, + pub export: Option, +} + +#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Default)] +#[serde(default)] +pub struct ZonePolicyConfig { + #[serde(default)] + pub export: Option, +} diff --git a/easytier/src/peers/peer_ospf_route.rs b/easytier/src/peers/peer_ospf_route.rs index ccaeba29..04471b0f 100644 --- a/easytier/src/peers/peer_ospf_route.rs +++ b/easytier/src/peers/peer_ospf_route.rs @@ -40,7 +40,6 @@ use super::{ PeerPacketFilter, }; use crate::common::config::ConfigLoader; -use crate::proto::dns::DnsConfigKind; use crate::{ common::{ config::NetworkIdentity, @@ -261,7 +260,7 @@ impl RoutePeerInfo { ipv6_addr: global_ctx.get_ipv6().map(|x| x.into()), groups: global_ctx.get_acl_groups(my_peer_id), - dns: Some(global_ctx.config.get_dns().to_pb(DnsConfigKind::Remote)), + dns: Some(global_ctx.config.get_dns().export()), noise_static_pubkey, diff --git a/easytier/src/proto/dns.proto b/easytier/src/proto/dns.proto index b522f76c..02ce8614 100644 --- a/easytier/src/proto/dns.proto +++ b/easytier/src/proto/dns.proto @@ -4,23 +4,16 @@ import "common.proto"; package dns; -enum DnsConfigKind { - LOCAL = 0; - REMOTE = 1; -} - message DnsConfigPb { - DnsConfigKind kind = 1; - repeated ZoneConfigPb zones = 2; - string name = 3; - string domain = 4; - repeated common.SocketAddr addresses = 5; - repeated string listeners = 6; + repeated ZoneConfigPb zones = 1; + string name = 2; + string domain = 3; } message ZoneConfigPb { - string origin = 1; - uint32 ttl = 2; - repeated string records = 3; - repeated string forwarders = 4; + common.UUID id = 1; + string origin = 2; + uint32 ttl = 3; + repeated string records = 4; + repeated string forwarders = 5; }