mirror of
https://github.com/EasyTier/EasyTier.git
synced 2026-08-07 04:59:49 +00:00
utils: move to a mod
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
use crate::dns::utils::{DirtyFlag, DirtyState, NameServerAddr};
|
||||
use crate::dns::utils::addr::NameServerAddr;
|
||||
use crate::dns::utils::dirty::{DirtyFlag, DirtyState};
|
||||
use crate::dns::zone::{Zone, ZoneGroup};
|
||||
use crate::proto::dns::DnsClientMgrRpc;
|
||||
use crate::proto::dns::{DnsSnapshot, HeartbeatRequest, HeartbeatResponse};
|
||||
|
||||
@@ -2,7 +2,8 @@ use crate::common::global_ctx::GlobalCtx;
|
||||
use crate::dns::config::policy::DnsPolicyConfig;
|
||||
use crate::dns::config::zone::ZoneConfig;
|
||||
use crate::dns::config::{DNS_DEFAULT_ADDRESS, DNS_DEFAULT_TLD};
|
||||
use crate::dns::utils::{parse, NameServerAddrGroup};
|
||||
use crate::dns::utils::addr::NameServerAddrGroup;
|
||||
use crate::dns::utils::parse;
|
||||
use crate::proto::dns::GetExportConfigResponse;
|
||||
use derivative::Derivative;
|
||||
use gethostname::gethostname;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::dns::utils::NameServerAddr;
|
||||
use crate::dns::utils::addr::NameServerAddr;
|
||||
use hickory_proto::rr::LowerName;
|
||||
use hickory_proto::xfer::Protocol;
|
||||
use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4};
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use crate::dns::config::policy::{DnsExportPolicy, ZonePolicyConfig};
|
||||
use crate::dns::utils::NameServerAddrGroup;
|
||||
use crate::dns::utils::addr::NameServerAddrGroup;
|
||||
use crate::dns::zone::Zone;
|
||||
use crate::proto::dns::ZoneData;
|
||||
use derivative::Derivative;
|
||||
|
||||
@@ -3,5 +3,5 @@ mod client_mgr;
|
||||
pub mod config;
|
||||
mod peer_mgr;
|
||||
mod server;
|
||||
pub mod utils;
|
||||
mod utils;
|
||||
pub mod zone;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use crate::common::config::ConfigLoader;
|
||||
use crate::common::PeerId;
|
||||
use crate::dns::config::{DnsExportConfig, DnsGlobalCtxExt};
|
||||
use crate::dns::utils::{DirtyFlag, DirtyState};
|
||||
use crate::dns::utils::dirty::{DirtyFlag, DirtyState};
|
||||
use crate::dns::zone::ZoneGroup;
|
||||
use crate::peer_center::instance::PeerCenterPeerManagerTrait;
|
||||
use crate::peers::peer_manager::PeerManager;
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
use super::utils::NameServerAddr;
|
||||
use crate::dns::client_mgr::DnsClientMgr;
|
||||
use crate::dns::utils::addr::NameServerAddr;
|
||||
use crate::peers::peer_manager::PeerManager;
|
||||
use crate::proto::dns::DnsClientMgrRpcServer;
|
||||
use derivative::Derivative;
|
||||
use derive_more::{Deref, DerefMut, From, Into};
|
||||
use hickory_proto::rr::Record;
|
||||
@@ -18,8 +20,6 @@ use std::{sync::Arc, time::Duration};
|
||||
use tokio::net::{TcpListener, UdpSocket};
|
||||
use tokio::{sync::RwLock, task::JoinHandle};
|
||||
use tokio_util::sync::CancellationToken;
|
||||
use crate::peers::peer_manager::PeerManager;
|
||||
use crate::proto::dns::DnsClientMgrRpcServer;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct DynamicCatalog {
|
||||
|
||||
@@ -1,309 +0,0 @@
|
||||
use crate::dns::config::DNS_SUPPORTED_PROTOCOLS;
|
||||
use crate::proto;
|
||||
use crate::proto::utils::RepeatedMessageModel;
|
||||
use anyhow::{anyhow, Error};
|
||||
use derive_more::{Deref, DerefMut};
|
||||
use hickory_proto::rr::{LowerName, RecordType};
|
||||
use hickory_proto::xfer::Protocol;
|
||||
use hickory_resolver::config::{NameServerConfig, NameServerConfigGroup};
|
||||
use hickory_server::authority::{
|
||||
Authority, LookupControlFlow, LookupObject, LookupOptions, MessageRequest, UpdateResult,
|
||||
ZoneType,
|
||||
};
|
||||
use hickory_server::server::RequestInfo;
|
||||
use idna::AsciiDenyList;
|
||||
use itertools::Itertools;
|
||||
use serde_with::{DeserializeFromStr, SerializeDisplay};
|
||||
use std::fmt::{Display, Formatter};
|
||||
use std::net::{IpAddr, SocketAddr};
|
||||
use std::str::FromStr;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use tokio::sync::Notify;
|
||||
use url::Url;
|
||||
|
||||
pub fn sanitize(name: &str) -> String {
|
||||
let dot = name.ends_with('.');
|
||||
let mut name = idna::domain_to_ascii_cow(name.as_ref(), AsciiDenyList::EMPTY)
|
||||
.unwrap_or_default()
|
||||
.into_owned()
|
||||
.to_lowercase()
|
||||
.split('.')
|
||||
.map(|label| {
|
||||
label
|
||||
.chars()
|
||||
.map(|c| if c.is_ascii_alphanumeric() { c } else { '-' })
|
||||
.take(63)
|
||||
.collect::<String>()
|
||||
.trim_matches('-')
|
||||
.to_string()
|
||||
})
|
||||
.filter(|label| !label.is_empty())
|
||||
.collect_vec()
|
||||
.join(".");
|
||||
name.truncate(253);
|
||||
if dot {
|
||||
name.push('.');
|
||||
}
|
||||
name
|
||||
}
|
||||
|
||||
pub fn parse(name: &str) -> LowerName {
|
||||
if let Ok(name) = name.parse() {
|
||||
name
|
||||
} else {
|
||||
let sanitized = sanitize(name);
|
||||
tracing::debug!("invalid hostname: {}, sanitized to: {}", name, sanitized);
|
||||
sanitized.parse().unwrap_or_default()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, SerializeDisplay, DeserializeFromStr)]
|
||||
pub struct NameServerAddr {
|
||||
pub(super) protocol: Protocol,
|
||||
pub(super) addr: SocketAddr,
|
||||
}
|
||||
|
||||
impl From<NameServerAddr> for NameServerConfig {
|
||||
fn from(value: NameServerAddr) -> Self {
|
||||
Self::new(value.addr, value.protocol)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<NameServerConfig> for NameServerAddr {
|
||||
fn from(value: NameServerConfig) -> Self {
|
||||
Self {
|
||||
protocol: value.protocol,
|
||||
addr: value.socket_addr,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<SocketAddr> for NameServerAddr {
|
||||
fn from(value: SocketAddr) -> Self {
|
||||
Self {
|
||||
protocol: Protocol::Udp,
|
||||
addr: value,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<IpAddr> for NameServerAddr {
|
||||
fn from(value: IpAddr) -> Self {
|
||||
SocketAddr::new(value, 53).into()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<NameServerAddr> for Url {
|
||||
fn from(value: NameServerAddr) -> Self {
|
||||
Url::parse(&format!("{}://{}", value.protocol, value.addr)).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<&Url> for NameServerAddr {
|
||||
type Error = Error;
|
||||
|
||||
fn try_from(value: &Url) -> Result<Self, Self::Error> {
|
||||
let scheme = value.scheme();
|
||||
let protocol = *DNS_SUPPORTED_PROTOCOLS
|
||||
.iter()
|
||||
.find(|p| p.to_string() == scheme)
|
||||
.ok_or(anyhow!("unsupported scheme: {}", scheme))?;
|
||||
let addr = value.host_str().ok_or(anyhow!("host not found"))?;
|
||||
let addr = addr
|
||||
.trim_start_matches('[')
|
||||
.trim_end_matches(']')
|
||||
.parse::<IpAddr>()
|
||||
.map_err(|e| anyhow!("invalid ip address '{}': {}", addr, e))?;
|
||||
let port = if let Some(port) = value.port() {
|
||||
port
|
||||
} else {
|
||||
match protocol {
|
||||
Protocol::Udp | Protocol::Tcp => 53,
|
||||
_ => return Err(anyhow!("port not found")),
|
||||
}
|
||||
};
|
||||
|
||||
Ok(Self {
|
||||
protocol,
|
||||
addr: SocketAddr::new(addr, port),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl From<NameServerAddr> for proto::common::Url {
|
||||
fn from(value: NameServerAddr) -> Self {
|
||||
Url::from(value).into()
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<&proto::common::Url> for NameServerAddr {
|
||||
type Error = Error;
|
||||
|
||||
fn try_from(value: &proto::common::Url) -> Result<Self, Self::Error> {
|
||||
Self::try_from(&Url::try_from(value)?)
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for NameServerAddr {
|
||||
type Err = Error;
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
macro_rules! try_parse {
|
||||
($($t:ty),+) => {
|
||||
$( if let Ok(v) = s.parse::<$t>() { return Ok(v.into()); } )+
|
||||
};
|
||||
}
|
||||
|
||||
try_parse!(IpAddr, SocketAddr);
|
||||
|
||||
(&Url::parse(s)?).try_into()
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for NameServerAddr {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
f.write_str(Url::from(*self).as_str())
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) type NameServerAddrGroup = RepeatedMessageModel<NameServerAddr>;
|
||||
|
||||
impl From<NameServerAddrGroup> for NameServerConfigGroup {
|
||||
fn from(value: NameServerAddrGroup) -> Self {
|
||||
value.into_iter().map_into().collect_vec().into()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<NameServerConfigGroup> for NameServerAddrGroup {
|
||||
fn from(value: NameServerConfigGroup) -> Self {
|
||||
value
|
||||
.into_inner()
|
||||
.into_iter()
|
||||
.map_into()
|
||||
.collect_vec()
|
||||
.into()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deref, DerefMut)]
|
||||
pub struct ChainedAuthority<A>(pub(super) A)
|
||||
where
|
||||
A: Authority,
|
||||
A::Lookup: LookupObject + 'static;
|
||||
|
||||
impl<A> From<A> for ChainedAuthority<A>
|
||||
where
|
||||
A: Authority,
|
||||
A::Lookup: LookupObject + 'static,
|
||||
{
|
||||
fn from(value: A) -> Self {
|
||||
Self(value)
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl<A> Authority for ChainedAuthority<A>
|
||||
where
|
||||
A: Authority,
|
||||
A::Lookup: LookupObject + 'static,
|
||||
{
|
||||
type Lookup = A::Lookup;
|
||||
|
||||
#[inline]
|
||||
fn zone_type(&self) -> ZoneType {
|
||||
self.0.zone_type()
|
||||
}
|
||||
#[inline]
|
||||
fn is_axfr_allowed(&self) -> bool {
|
||||
self.0.is_axfr_allowed()
|
||||
}
|
||||
#[inline]
|
||||
async fn update(&self, update: &MessageRequest) -> UpdateResult<bool> {
|
||||
self.0.update(update).await
|
||||
}
|
||||
#[inline]
|
||||
fn origin(&self) -> &LowerName {
|
||||
self.0.origin()
|
||||
}
|
||||
#[inline]
|
||||
async fn lookup(
|
||||
&self,
|
||||
name: &LowerName,
|
||||
rtype: RecordType,
|
||||
lookup_options: LookupOptions,
|
||||
) -> LookupControlFlow<Self::Lookup> {
|
||||
self.0.lookup(name, rtype, lookup_options).await
|
||||
}
|
||||
#[inline]
|
||||
async fn consult(
|
||||
&self,
|
||||
name: &LowerName,
|
||||
rtype: RecordType,
|
||||
lookup_options: LookupOptions,
|
||||
last_result: LookupControlFlow<Box<dyn LookupObject>>,
|
||||
) -> LookupControlFlow<Box<dyn LookupObject>> {
|
||||
if let Some(Ok(l)) = last_result.map_result() {
|
||||
LookupControlFlow::Break(Ok(l))
|
||||
} else {
|
||||
self.0
|
||||
.lookup(name, rtype, lookup_options)
|
||||
.await
|
||||
.map(|l| Box::new(l) as _)
|
||||
}
|
||||
}
|
||||
#[inline]
|
||||
async fn search(
|
||||
&self,
|
||||
request_info: RequestInfo<'_>,
|
||||
lookup_options: LookupOptions,
|
||||
) -> LookupControlFlow<Self::Lookup> {
|
||||
self.0.search(request_info, lookup_options).await
|
||||
}
|
||||
#[inline]
|
||||
async fn get_nsec_records(
|
||||
&self,
|
||||
name: &LowerName,
|
||||
lookup_options: LookupOptions,
|
||||
) -> LookupControlFlow<Self::Lookup> {
|
||||
self.0.get_nsec_records(name, lookup_options).await
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Deref, DerefMut)]
|
||||
pub(super) struct DirtyState<T> {
|
||||
#[deref]
|
||||
#[deref_mut]
|
||||
flags: T,
|
||||
pub notify: Notify,
|
||||
}
|
||||
|
||||
impl<T: Default> Default for DirtyState<T> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
flags: T::default(),
|
||||
notify: Notify::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(super) struct DirtyFlag(AtomicBool);
|
||||
|
||||
impl DirtyFlag {
|
||||
pub fn new(value: bool) -> Self {
|
||||
Self(AtomicBool::new(value))
|
||||
}
|
||||
|
||||
pub fn mark(&self) {
|
||||
self.0.store(true, Ordering::Release);
|
||||
}
|
||||
|
||||
pub fn reset(&self) -> bool {
|
||||
self.0.swap(false, Ordering::Acquire)
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for DirtyFlag {
|
||||
fn default() -> Self {
|
||||
Self::new(true)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
use crate::dns::config::DNS_SUPPORTED_PROTOCOLS;
|
||||
use crate::proto;
|
||||
use crate::proto::utils::RepeatedMessageModel;
|
||||
use anyhow::{anyhow, Error};
|
||||
use hickory_proto::xfer::Protocol;
|
||||
use hickory_resolver::config::{NameServerConfig, NameServerConfigGroup};
|
||||
use itertools::Itertools;
|
||||
use serde_with::{DeserializeFromStr, SerializeDisplay};
|
||||
use std::fmt::{Display, Formatter};
|
||||
use std::net::{IpAddr, SocketAddr};
|
||||
use std::str::FromStr;
|
||||
use url::Url;
|
||||
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, SerializeDisplay, DeserializeFromStr)]
|
||||
pub struct NameServerAddr {
|
||||
pub protocol: Protocol,
|
||||
pub addr: SocketAddr,
|
||||
}
|
||||
|
||||
impl From<NameServerAddr> for NameServerConfig {
|
||||
fn from(value: NameServerAddr) -> Self {
|
||||
Self::new(value.addr, value.protocol)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<NameServerConfig> for NameServerAddr {
|
||||
fn from(value: NameServerConfig) -> Self {
|
||||
Self {
|
||||
protocol: value.protocol,
|
||||
addr: value.socket_addr,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<SocketAddr> for NameServerAddr {
|
||||
fn from(value: SocketAddr) -> Self {
|
||||
Self {
|
||||
protocol: Protocol::Udp,
|
||||
addr: value,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<IpAddr> for NameServerAddr {
|
||||
fn from(value: IpAddr) -> Self {
|
||||
SocketAddr::new(value, 53).into()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<NameServerAddr> for Url {
|
||||
fn from(value: NameServerAddr) -> Self {
|
||||
Url::parse(&format!("{}://{}", value.protocol, value.addr)).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<&Url> for NameServerAddr {
|
||||
type Error = Error;
|
||||
|
||||
fn try_from(value: &Url) -> Result<Self, Self::Error> {
|
||||
let scheme = value.scheme();
|
||||
let protocol = *DNS_SUPPORTED_PROTOCOLS
|
||||
.iter()
|
||||
.find(|p| p.to_string() == scheme)
|
||||
.ok_or(anyhow!("unsupported scheme: {}", scheme))?;
|
||||
let addr = value.host_str().ok_or(anyhow!("host not found"))?;
|
||||
let addr = addr
|
||||
.trim_start_matches('[')
|
||||
.trim_end_matches(']')
|
||||
.parse::<IpAddr>()
|
||||
.map_err(|e| anyhow!("invalid ip address '{}': {}", addr, e))?;
|
||||
let port = if let Some(port) = value.port() {
|
||||
port
|
||||
} else {
|
||||
match protocol {
|
||||
Protocol::Udp | Protocol::Tcp => 53,
|
||||
_ => return Err(anyhow!("port not found")),
|
||||
}
|
||||
};
|
||||
|
||||
Ok(Self {
|
||||
protocol,
|
||||
addr: SocketAddr::new(addr, port),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl From<NameServerAddr> for proto::common::Url {
|
||||
fn from(value: NameServerAddr) -> Self {
|
||||
Url::from(value).into()
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<&proto::common::Url> for NameServerAddr {
|
||||
type Error = Error;
|
||||
|
||||
fn try_from(value: &proto::common::Url) -> Result<Self, Self::Error> {
|
||||
Self::try_from(&Url::try_from(value)?)
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for NameServerAddr {
|
||||
type Err = Error;
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
macro_rules! try_parse {
|
||||
($($t:ty),+) => {
|
||||
$( if let Ok(v) = s.parse::<$t>() { return Ok(v.into()); } )+
|
||||
};
|
||||
}
|
||||
|
||||
try_parse!(IpAddr, SocketAddr);
|
||||
|
||||
(&Url::parse(s)?).try_into()
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for NameServerAddr {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
f.write_str(Url::from(*self).as_str())
|
||||
}
|
||||
}
|
||||
|
||||
pub type NameServerAddrGroup = RepeatedMessageModel<NameServerAddr>;
|
||||
|
||||
impl From<NameServerAddrGroup> for NameServerConfigGroup {
|
||||
fn from(value: NameServerAddrGroup) -> Self {
|
||||
value.into_iter().map_into().collect_vec().into()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<NameServerConfigGroup> for NameServerAddrGroup {
|
||||
fn from(value: NameServerConfigGroup) -> Self {
|
||||
value
|
||||
.into_inner()
|
||||
.into_iter()
|
||||
.map_into()
|
||||
.collect_vec()
|
||||
.into()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
use derive_more::{Deref, DerefMut, From};
|
||||
use hickory_proto::rr::{LowerName, RecordType};
|
||||
use hickory_server::authority::{
|
||||
Authority, LookupControlFlow, LookupObject, LookupOptions, MessageRequest, UpdateResult,
|
||||
ZoneType,
|
||||
};
|
||||
use hickory_server::server::RequestInfo;
|
||||
|
||||
#[derive(From, Deref, DerefMut)]
|
||||
pub struct ChainedAuthority<A>(A)
|
||||
where
|
||||
A: Authority,
|
||||
A::Lookup: LookupObject + 'static;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl<A> Authority for ChainedAuthority<A>
|
||||
where
|
||||
A: Authority,
|
||||
A::Lookup: LookupObject + 'static,
|
||||
{
|
||||
type Lookup = A::Lookup;
|
||||
|
||||
#[inline]
|
||||
fn zone_type(&self) -> ZoneType {
|
||||
self.0.zone_type()
|
||||
}
|
||||
#[inline]
|
||||
fn is_axfr_allowed(&self) -> bool {
|
||||
self.0.is_axfr_allowed()
|
||||
}
|
||||
#[inline]
|
||||
async fn update(&self, update: &MessageRequest) -> UpdateResult<bool> {
|
||||
self.0.update(update).await
|
||||
}
|
||||
#[inline]
|
||||
fn origin(&self) -> &LowerName {
|
||||
self.0.origin()
|
||||
}
|
||||
#[inline]
|
||||
async fn lookup(
|
||||
&self,
|
||||
name: &LowerName,
|
||||
rtype: RecordType,
|
||||
lookup_options: LookupOptions,
|
||||
) -> LookupControlFlow<Self::Lookup> {
|
||||
self.0.lookup(name, rtype, lookup_options).await
|
||||
}
|
||||
#[inline]
|
||||
async fn consult(
|
||||
&self,
|
||||
name: &LowerName,
|
||||
rtype: RecordType,
|
||||
lookup_options: LookupOptions,
|
||||
last_result: LookupControlFlow<Box<dyn LookupObject>>,
|
||||
) -> LookupControlFlow<Box<dyn LookupObject>> {
|
||||
if let Some(Ok(l)) = last_result.map_result() {
|
||||
LookupControlFlow::Break(Ok(l))
|
||||
} else {
|
||||
self.0
|
||||
.lookup(name, rtype, lookup_options)
|
||||
.await
|
||||
.map(|l| Box::new(l) as _)
|
||||
}
|
||||
}
|
||||
#[inline]
|
||||
async fn search(
|
||||
&self,
|
||||
request_info: RequestInfo<'_>,
|
||||
lookup_options: LookupOptions,
|
||||
) -> LookupControlFlow<Self::Lookup> {
|
||||
self.0.search(request_info, lookup_options).await
|
||||
}
|
||||
#[inline]
|
||||
async fn get_nsec_records(
|
||||
&self,
|
||||
name: &LowerName,
|
||||
lookup_options: LookupOptions,
|
||||
) -> LookupControlFlow<Self::Lookup> {
|
||||
self.0.get_nsec_records(name, lookup_options).await
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
use derivative::Derivative;
|
||||
use derive_more::{Deref, DerefMut};
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use tokio::sync::Notify;
|
||||
|
||||
#[derive(Debug, Default, Deref, DerefMut)]
|
||||
pub struct DirtyState<T> {
|
||||
#[deref]
|
||||
#[deref_mut]
|
||||
flags: T,
|
||||
pub notify: Notify,
|
||||
}
|
||||
|
||||
#[derive(Derivative, Debug)]
|
||||
#[derivative(Default)]
|
||||
pub struct DirtyFlag(#[derivative(Default(value = "AtomicBool::new(true)"))] AtomicBool);
|
||||
|
||||
impl DirtyFlag {
|
||||
pub fn new(value: bool) -> Self {
|
||||
Self(AtomicBool::new(value))
|
||||
}
|
||||
|
||||
pub fn mark(&self) {
|
||||
self.0.store(true, Ordering::Release);
|
||||
}
|
||||
|
||||
pub fn reset(&self) -> bool {
|
||||
self.0.swap(false, Ordering::Acquire)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
use hickory_proto::rr::LowerName;
|
||||
use idna::AsciiDenyList;
|
||||
use itertools::Itertools;
|
||||
|
||||
pub mod addr;
|
||||
pub mod authority;
|
||||
pub mod dirty;
|
||||
|
||||
pub fn sanitize(name: &str) -> String {
|
||||
let dot = name.ends_with('.');
|
||||
let mut name = idna::domain_to_ascii_cow(name.as_ref(), AsciiDenyList::EMPTY)
|
||||
.unwrap_or_default()
|
||||
.into_owned()
|
||||
.to_lowercase()
|
||||
.split('.')
|
||||
.map(|label| {
|
||||
label
|
||||
.chars()
|
||||
.map(|c| if c.is_ascii_alphanumeric() { c } else { '-' })
|
||||
.take(63)
|
||||
.collect::<String>()
|
||||
.trim_matches('-')
|
||||
.to_string()
|
||||
})
|
||||
.filter(|label| !label.is_empty())
|
||||
.collect_vec()
|
||||
.join(".");
|
||||
name.truncate(253);
|
||||
if dot {
|
||||
name.push('.');
|
||||
}
|
||||
name
|
||||
}
|
||||
|
||||
pub fn parse(name: &str) -> LowerName {
|
||||
if let Ok(name) = name.parse() {
|
||||
name
|
||||
} else {
|
||||
let sanitized = sanitize(name);
|
||||
tracing::debug!("invalid hostname: {}, sanitized to: {}", name, sanitized);
|
||||
sanitized.parse().unwrap_or_default()
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
use crate::common::dns::get_default_resolver_config;
|
||||
use crate::dns::utils::NameServerAddr;
|
||||
use crate::dns::utils::addr::NameServerAddr;
|
||||
use crate::proto;
|
||||
use crate::proto::utils::RepeatedMessageModel;
|
||||
use crate::utils::MapTryInto;
|
||||
|
||||
Reference in New Issue
Block a user