mirror of
https://github.com/EasyTier/EasyTier.git
synced 2026-08-31 08:19:14 +00:00
rpc: let peers generate their own dedicated zones
This commit is contained in:
@@ -2,7 +2,7 @@ use std::net::{Ipv4Addr, Ipv6Addr};
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use super::config::{DnsConfig, DNS_SERVER_RPC_ADDR};
|
||||
use super::config::{DnsConfig, DnsGlobalCtxExt, DNS_SERVER_RPC_ADDR};
|
||||
use crate::common::config::ConfigLoader;
|
||||
use crate::common::PeerId;
|
||||
use crate::peers::peer_manager::PeerManager;
|
||||
@@ -16,7 +16,6 @@ use crate::proto::rpc_types::controller::BaseController;
|
||||
use crate::tunnel::tcp::TcpTunnelConnector;
|
||||
use dashmap::DashMap;
|
||||
use derivative::Derivative;
|
||||
use hickory_proto::rr::Name;
|
||||
use tokio::sync::Mutex;
|
||||
use url::Url;
|
||||
|
||||
@@ -86,27 +85,6 @@ impl DnsClient {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn create_dedicated_zone(
|
||||
origin: &str,
|
||||
ipv4: Option<Ipv4Addr>,
|
||||
ipv6: Vec<Ipv6Addr>,
|
||||
) -> Option<ZoneConfigPb> {
|
||||
let mut records = Vec::new();
|
||||
|
||||
if let Some(ipv4) = ipv4 {
|
||||
records.push(format!("@ IN A {}", ipv4));
|
||||
}
|
||||
for ipv6 in ipv6 {
|
||||
records.push(format!("@ IN AAAA {}", ipv6));
|
||||
}
|
||||
(!records.is_empty()).then_some(ZoneConfigPb {
|
||||
origin: origin.to_string(),
|
||||
records,
|
||||
|
||||
..Default::default()
|
||||
})
|
||||
}
|
||||
|
||||
async fn build_snapshot(&self) -> DnsSnapshot {
|
||||
let global_ctx = self.peer_mgr.get_global_ctx_ref();
|
||||
|
||||
@@ -116,17 +94,7 @@ impl DnsClient {
|
||||
|
||||
// 1. Local zones
|
||||
zones.extend(config.zones.iter().map(Into::into));
|
||||
|
||||
// 1.5. Specialized zone for self (domain.tld -> self_ip)
|
||||
if let Ok(origin) = Name::from(config.get_name()).append_domain(&*config.domain) {
|
||||
let ipv4 = global_ctx.get_ipv4().map(|ip| ip.address());
|
||||
let ipv6 = global_ctx.get_ipv6().map(|ip| ip.address());
|
||||
let ipv6 = ipv6.map(|a| vec![a]).unwrap_or_default();
|
||||
if let Some(zone) = Self::create_dedicated_zone(origin.to_string().as_str(), ipv4, ipv6)
|
||||
{
|
||||
zones.push(zone);
|
||||
}
|
||||
}
|
||||
zones.extend(global_ctx.dns_self_zone().as_ref().map(Into::into));
|
||||
|
||||
// 2. Peer zones
|
||||
for ref_multi in self.peer_configs.iter() {
|
||||
@@ -135,29 +103,6 @@ impl DnsClient {
|
||||
zones.extend(config.zones.clone());
|
||||
}
|
||||
|
||||
// 3. Specialized zones for peers (domain.tld -> peer_ip)
|
||||
let routes = self.peer_mgr.list_routes().await;
|
||||
for route in routes.iter() {
|
||||
let peer_id = route.peer_id;
|
||||
if let Some(peer_config) = self.peer_configs.get(&peer_id) {
|
||||
let origin = format!("{}.{}", peer_config.name, peer_config.domain);
|
||||
let ipv4 = route
|
||||
.ipv4_addr
|
||||
.map(|ip| ip.address)
|
||||
.flatten()
|
||||
.map(Into::into);
|
||||
let ipv6 = route
|
||||
.ipv6_addr
|
||||
.map(|ip| ip.address)
|
||||
.flatten()
|
||||
.map(Into::into);
|
||||
let ipv6 = ipv6.map(|a| vec![a]).unwrap_or_default();
|
||||
if let Some(zone) = Self::create_dedicated_zone(origin.as_str(), ipv4, ipv6) {
|
||||
zones.push(zone);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
DnsSnapshot {
|
||||
zones,
|
||||
addresses: self
|
||||
|
||||
+76
-10
@@ -1,11 +1,14 @@
|
||||
use crate::dns::utils::{parse, sanitize, NameServerAddr};
|
||||
use crate::common::config::ConfigLoader;
|
||||
use crate::common::global_ctx::GlobalCtx;
|
||||
use crate::dns::utils::{parse, NameServerAddr};
|
||||
use crate::proto::dns::{DnsConfigPb, ZoneConfigPb};
|
||||
use derive_more::{Deref, DerefMut};
|
||||
use gethostname::gethostname;
|
||||
use hickory_proto::rr::{IntoName, LowerName, Name};
|
||||
use hickory_proto::rr::{LowerName, Name};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4};
|
||||
use std::iter;
|
||||
use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4};
|
||||
use std::str::FromStr;
|
||||
use std::sync::LazyLock;
|
||||
use url::Url;
|
||||
@@ -44,17 +47,49 @@ impl DnsConfig {
|
||||
self.name = parse(name);
|
||||
}
|
||||
|
||||
pub fn export(&self) -> DnsConfigPb {
|
||||
pub fn get_fqdn(&self) -> LowerName {
|
||||
Name::from(self.get_name())
|
||||
.append_domain(&self.domain)
|
||||
.unwrap()
|
||||
.into()
|
||||
}
|
||||
|
||||
pub fn set_fqdn(&mut self, fqdn: &str) {
|
||||
let mut fqdn = Name::from(parse(fqdn));
|
||||
fqdn.set_fqdn(true);
|
||||
self.name = Name::from_labels(iter::once(fqdn.iter().next().unwrap_or_default()))
|
||||
.unwrap_or_default()
|
||||
.into();
|
||||
self.domain = fqdn.base_name().into();
|
||||
}
|
||||
}
|
||||
|
||||
pub trait DnsGlobalCtxExt {
|
||||
fn dns_self_zone(&self) -> Option<ZoneConfig>;
|
||||
fn dns_export_config(&self) -> DnsConfigPb;
|
||||
}
|
||||
|
||||
impl DnsGlobalCtxExt for GlobalCtx {
|
||||
fn dns_self_zone(&self) -> Option<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)
|
||||
}
|
||||
|
||||
fn dns_export_config(&self) -> DnsConfigPb {
|
||||
let config = self.config.get_dns();
|
||||
let zone = self.dns_self_zone();
|
||||
let zones = config.zones.iter().chain(zone.iter());
|
||||
|
||||
DnsConfigPb {
|
||||
zones: self
|
||||
.zones
|
||||
.iter()
|
||||
zones: zones
|
||||
.filter(|z| z.policy.export.is_some()) // TODO: check policies of parent zones
|
||||
.map(Into::into)
|
||||
.collect(),
|
||||
|
||||
name: self.get_name().to_string(),
|
||||
domain: self.domain.to_string(),
|
||||
fqdn: config.get_fqdn().to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -88,6 +123,37 @@ pub struct ZoneConfig {
|
||||
pub policy: ZonePolicyConfig,
|
||||
}
|
||||
|
||||
impl ZoneConfig {
|
||||
fn dedicated(
|
||||
id: Option<Uuid>,
|
||||
origin: LowerName,
|
||||
ipv4: Option<Ipv4Addr>,
|
||||
ipv6: Vec<Ipv6Addr>,
|
||||
) -> Option<Self> {
|
||||
let mut records = Vec::new();
|
||||
|
||||
if let Some(ipv4) = ipv4 {
|
||||
records.push(format!("@ IN A {}", ipv4));
|
||||
}
|
||||
for ipv6 in ipv6 {
|
||||
records.push(format!("@ IN AAAA {}", ipv6));
|
||||
}
|
||||
|
||||
let policy = ZonePolicyConfig {
|
||||
export: Some(DnsExportPolicy::default()),
|
||||
};
|
||||
|
||||
(!records.is_empty()).then_some(Self {
|
||||
id: id.unwrap_or(Uuid::new_v4()),
|
||||
origin,
|
||||
records,
|
||||
policy,
|
||||
|
||||
..Default::default()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&ZoneConfig> for ZoneConfigPb {
|
||||
fn from(value: &ZoneConfig) -> Self {
|
||||
Self {
|
||||
|
||||
Reference in New Issue
Block a user