mirror of
https://github.com/EasyTier/EasyTier.git
synced 2026-09-02 09:09:17 +00:00
config: add DNS import/export policies
config: move ID generation to ZoneConfig config: serde fixes
This commit is contained in:
+68
-30
@@ -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<LowerName> =
|
||||
pub struct DnsConfig {
|
||||
#[serde(rename = "zone")]
|
||||
pub zones: Vec<ZoneConfig>,
|
||||
#[serde(flatten)]
|
||||
pub policies: HashMap<LowerName, DnsPolicyConfig>,
|
||||
name: LowerName,
|
||||
pub domain: LowerName,
|
||||
pub addresses: Vec<SocketAddr>,
|
||||
@@ -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<String>,
|
||||
#[serde(default)]
|
||||
pub forwarders: Vec<NameServerAddr>,
|
||||
#[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<Vec<String>>,
|
||||
pub blacklist: Option<Vec<String>>,
|
||||
}
|
||||
|
||||
#[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<P = FunctionalityPolicy> {
|
||||
#[serde(flatten)]
|
||||
#[deref]
|
||||
#[deref_mut]
|
||||
policy: P,
|
||||
pub recursive: bool, // TODO
|
||||
}
|
||||
|
||||
pub type ZoneExportPolicy = FunctionalityPolicy;
|
||||
pub type DnsExportPolicy = DnsPolicy<ZoneExportPolicy>;
|
||||
pub type DnsImportPolicy = DnsPolicy<FunctionalityPolicy>;
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Default)]
|
||||
#[serde(default)]
|
||||
pub struct DnsPolicyConfig {
|
||||
pub import: DnsImportPolicy,
|
||||
pub export: Option<DnsExportPolicy>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Default)]
|
||||
#[serde(default)]
|
||||
pub struct ZonePolicyConfig {
|
||||
#[serde(default)]
|
||||
pub export: Option<DnsExportPolicy>,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user