mirror of
https://github.com/EasyTier/EasyTier.git
synced 2026-09-02 09:09:17 +00:00
rewrite NameServerAddr
fix Url to NameServerAddr conversion fmt
This commit is contained in:
@@ -1,12 +1,12 @@
|
|||||||
use crate::dns::config::DNS_NODE_TTI;
|
use crate::dns::config::DNS_NODE_TTI;
|
||||||
use crate::dns::utils::addr::NameServerAddr;
|
use crate::dns::utils::addr::NameServerAddr;
|
||||||
use crate::utils::dirty::DirtyFlag;
|
|
||||||
use crate::dns::zone::{Zone, ZoneGroup};
|
use crate::dns::zone::{Zone, ZoneGroup};
|
||||||
use crate::proto::dns::DnsNodeMgrRpc;
|
use crate::proto::dns::DnsNodeMgrRpc;
|
||||||
use crate::proto::dns::{DnsSnapshot, HeartbeatRequest, HeartbeatResponse};
|
use crate::proto::dns::{DnsSnapshot, HeartbeatRequest, HeartbeatResponse};
|
||||||
use crate::proto::rpc_types;
|
use crate::proto::rpc_types;
|
||||||
use crate::proto::rpc_types::controller::BaseController;
|
use crate::proto::rpc_types::controller::BaseController;
|
||||||
use crate::proto::utils::TransientDigest;
|
use crate::proto::utils::TransientDigest;
|
||||||
|
use crate::utils::dirty::DirtyFlag;
|
||||||
use anyhow::Error;
|
use anyhow::Error;
|
||||||
use hickory_server::zone_handler::Catalog;
|
use hickory_server::zone_handler::Catalog;
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
@@ -91,9 +91,11 @@ impl DnsNodeMgr {
|
|||||||
zones.push(Zone::system());
|
zones.push(Zone::system());
|
||||||
|
|
||||||
for forward in zones.iter_mut().flat_map(|z| &mut z.forward) {
|
for forward in zones.iter_mut().flat_map(|z| &mut z.forward) {
|
||||||
forward
|
forward.name_servers.retain_mut(|ns| {
|
||||||
.name_servers
|
ns.connections
|
||||||
.retain(|ns| !local.contains(&ns.into()));
|
.retain(|c| !local.contains(&(ns.ip, c).into()));
|
||||||
|
!ns.connections.is_empty()
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
zones.into()
|
zones.into()
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
use crate::common::PeerId;
|
use crate::common::PeerId;
|
||||||
use crate::common::global_ctx::ArcGlobalCtx;
|
use crate::common::global_ctx::ArcGlobalCtx;
|
||||||
use crate::dns::config::{DNS_PEER_TTI, DnsExportConfig, DnsGlobalCtxExt};
|
use crate::dns::config::{DNS_PEER_TTI, DnsExportConfig, DnsGlobalCtxExt};
|
||||||
use crate::utils::dirty::DirtyFlag;
|
|
||||||
use crate::dns::zone::ZoneGroup;
|
use crate::dns::zone::ZoneGroup;
|
||||||
use crate::peer_center::instance::PeerCenterPeerManagerTrait;
|
use crate::peer_center::instance::PeerCenterPeerManagerTrait;
|
||||||
use crate::peers::peer_manager::PeerManager;
|
use crate::peers::peer_manager::PeerManager;
|
||||||
@@ -13,6 +12,7 @@ use crate::proto::dns::{
|
|||||||
use crate::proto::rpc_types;
|
use crate::proto::rpc_types;
|
||||||
use crate::proto::rpc_types::controller::BaseController;
|
use crate::proto::rpc_types::controller::BaseController;
|
||||||
use crate::proto::utils::TransientDigest;
|
use crate::proto::utils::TransientDigest;
|
||||||
|
use crate::utils::dirty::DirtyFlag;
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
use moka::future::Cache;
|
use moka::future::Cache;
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
|
|||||||
@@ -2,14 +2,14 @@ use crate::proto;
|
|||||||
use crate::proto::utils::RepeatedMessageModel;
|
use crate::proto::utils::RepeatedMessageModel;
|
||||||
use anyhow::{Error, anyhow};
|
use anyhow::{Error, anyhow};
|
||||||
use hickory_net::xfer::Protocol;
|
use hickory_net::xfer::Protocol;
|
||||||
use hickory_resolver::config::{ConnectionConfig, NameServerConfig};
|
use hickory_resolver::config::{ConnectionConfig, NameServerConfig, ProtocolConfig};
|
||||||
use serde::de::IntoDeserializer;
|
use serde::de::IntoDeserializer;
|
||||||
use serde::{Deserialize, de};
|
use serde::{Deserialize, de};
|
||||||
use serde_with::{DeserializeFromStr, SerializeDisplay};
|
use serde_with::{DeserializeFromStr, SerializeDisplay};
|
||||||
use std::fmt::{Display, Formatter};
|
use std::fmt::{Display, Formatter};
|
||||||
use std::net::{IpAddr, SocketAddr};
|
use std::net::{IpAddr, SocketAddr};
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use url::{Host, Url};
|
use url::Url;
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, SerializeDisplay, DeserializeFromStr)]
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, SerializeDisplay, DeserializeFromStr)]
|
||||||
pub struct NameServerAddr {
|
pub struct NameServerAddr {
|
||||||
@@ -29,22 +29,16 @@ impl From<NameServerAddr> for NameServerConfig {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<&NameServerConfig> for NameServerAddr {
|
impl From<(IpAddr, &ConnectionConfig)> for NameServerAddr {
|
||||||
fn from(value: &NameServerConfig) -> Self {
|
fn from(value: (IpAddr, &ConnectionConfig)) -> Self {
|
||||||
let connection = value.connections.first().unwrap();
|
let (ip, config) = value;
|
||||||
Self {
|
Self {
|
||||||
protocol: connection.protocol.to_protocol(),
|
protocol: config.protocol.to_protocol(),
|
||||||
addr: SocketAddr::new(value.ip, connection.port),
|
addr: SocketAddr::new(ip, config.port),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<NameServerConfig> for NameServerAddr {
|
|
||||||
fn from(value: NameServerConfig) -> Self {
|
|
||||||
(&value).into()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<SocketAddr> for NameServerAddr {
|
impl From<SocketAddr> for NameServerAddr {
|
||||||
fn from(value: SocketAddr) -> Self {
|
fn from(value: SocketAddr) -> Self {
|
||||||
Self {
|
Self {
|
||||||
@@ -70,23 +64,23 @@ impl TryFrom<&Url> for NameServerAddr {
|
|||||||
type Error = Error;
|
type Error = Error;
|
||||||
|
|
||||||
fn try_from(value: &Url) -> Result<Self, Self::Error> {
|
fn try_from(value: &Url) -> Result<Self, Self::Error> {
|
||||||
let protocol = Protocol::deserialize(value.scheme().into_deserializer()).map_err(
|
let protocol = match Protocol::deserialize(value.scheme().into_deserializer()).map_err(
|
||||||
|e: de::value::Error| anyhow!("invalid protocol '{}': {}", value.scheme(), e),
|
|e: de::value::Error| anyhow!("invalid protocol '{}': {}", value.scheme(), e),
|
||||||
)?;
|
)? {
|
||||||
let port = value
|
Protocol::Udp => ProtocolConfig::Udp,
|
||||||
.port()
|
Protocol::Tcp => ProtocolConfig::Tcp,
|
||||||
.or_else(|| matches!(protocol, Protocol::Udp | Protocol::Tcp).then_some(53))
|
p => return Err(anyhow!("unsupported protocol: {}", p)),
|
||||||
.ok_or_else(|| anyhow!("port not found"))?;
|
};
|
||||||
let ip = match value.host().ok_or(anyhow!("host not found"))? {
|
let host = value.host_str().ok_or(anyhow!("host not found"))?;
|
||||||
Host::Domain(_) => {
|
let port = value.port().unwrap_or(protocol.default_port());
|
||||||
return Err(anyhow!("unsupported host: {}", value.host_str().unwrap()));
|
let addr = if let Ok(addr) = IpAddr::from_str(host) {
|
||||||
}
|
SocketAddr::new(addr, port)
|
||||||
Host::Ipv4(ip) => ip.into(),
|
} else {
|
||||||
Host::Ipv6(ip) => ip.into(),
|
return Err(anyhow!("invalid address: {}", host));
|
||||||
};
|
};
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
protocol,
|
protocol: protocol.to_protocol(),
|
||||||
addr: SocketAddr::new(ip, port),
|
addr,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -127,3 +121,13 @@ impl Display for NameServerAddr {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub type NameServerAddrGroup = RepeatedMessageModel<NameServerAddr>;
|
pub type NameServerAddrGroup = RepeatedMessageModel<NameServerAddr>;
|
||||||
|
|
||||||
|
impl From<&NameServerConfig> for NameServerAddrGroup {
|
||||||
|
fn from(value: &NameServerConfig) -> Self {
|
||||||
|
value
|
||||||
|
.connections
|
||||||
|
.iter()
|
||||||
|
.map(|c| (value.ip, c).into())
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
use crate::common::dns::get_default_resolver_config;
|
use crate::common::dns::get_default_resolver_config;
|
||||||
use crate::dns::utils::addr::NameServerAddr;
|
use crate::dns::utils::addr::{NameServerAddr, NameServerAddrGroup};
|
||||||
use crate::dns::utils::zone_handler::ArcZoneHandler;
|
use crate::dns::utils::zone_handler::ArcZoneHandler;
|
||||||
use crate::proto;
|
use crate::proto;
|
||||||
use crate::proto::utils::RepeatedMessageModel;
|
use crate::proto::utils::RepeatedMessageModel;
|
||||||
@@ -128,7 +128,8 @@ impl From<Zone> for proto::dns::ZoneData {
|
|||||||
.forward
|
.forward
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.flat_map(|f| f.name_servers.into_iter())
|
.flat_map(|f| f.name_servers.into_iter())
|
||||||
.map(Into::<NameServerAddr>::into)
|
.map(|ns| (&ns).into())
|
||||||
|
.flat_map(NameServerAddrGroup::into_iter)
|
||||||
.map(Into::into)
|
.map(Into::into)
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
|
pub mod dirty;
|
||||||
pub mod panic;
|
pub mod panic;
|
||||||
pub mod string;
|
pub mod string;
|
||||||
pub mod task;
|
pub mod task;
|
||||||
pub mod dirty;
|
|
||||||
|
|
||||||
use std::net::{IpAddr, Ipv4Addr, SocketAddr, TcpListener};
|
use std::net::{IpAddr, Ipv4Addr, SocketAddr, TcpListener};
|
||||||
use std::sync::{Arc, Weak};
|
use std::sync::{Arc, Weak};
|
||||||
|
|||||||
Reference in New Issue
Block a user