mirror of
https://github.com/EasyTier/EasyTier.git
synced 2026-09-02 17:15:43 +00:00
rewrite resolver_conf, socket_addrs
comment dns utils res
This commit is contained in:
@@ -1,13 +1,11 @@
|
|||||||
use crate::utils::dns::get_default_resolver_config;
|
|
||||||
use crate::dns::utils::addr::{NameServerAddr, NameServerAddrGroup};
|
use crate::dns::utils::addr::{NameServerAddr, NameServerAddrGroup};
|
||||||
use crate::dns::utils::zone_handler::{ArcZoneHandler, ChainedZoneHandler};
|
use crate::dns::utils::zone_handler::{ArcZoneHandler, ChainedZoneHandler};
|
||||||
use crate::proto;
|
use crate::proto;
|
||||||
use crate::proto::utils::RepeatedMessageModel;
|
use crate::proto::utils::RepeatedMessageModel;
|
||||||
|
use crate::utils::dns::resolver_conf;
|
||||||
use hickory_net::runtime::TokioRuntimeProvider;
|
use hickory_net::runtime::TokioRuntimeProvider;
|
||||||
use hickory_proto::rr::{LowerName, RecordSet, RrKey};
|
use hickory_proto::rr::{LowerName, RecordSet, RrKey};
|
||||||
use hickory_proto::serialize::txt::Parser;
|
use hickory_proto::serialize::txt::Parser;
|
||||||
use hickory_resolver::config::ResolverOpts;
|
|
||||||
use hickory_resolver::system_conf::read_system_conf;
|
|
||||||
use hickory_server::store::forwarder::{ForwardConfig, ForwardZoneHandler};
|
use hickory_server::store::forwarder::{ForwardConfig, ForwardZoneHandler};
|
||||||
use hickory_server::store::in_memory::InMemoryZoneHandler;
|
use hickory_server::store::in_memory::InMemoryZoneHandler;
|
||||||
use hickory_server::zone_handler::{AxfrPolicy, ZoneType};
|
use hickory_server::zone_handler::{AxfrPolicy, ZoneType};
|
||||||
@@ -28,8 +26,7 @@ pub struct Zone {
|
|||||||
|
|
||||||
impl Zone {
|
impl Zone {
|
||||||
pub fn system() -> Self {
|
pub fn system() -> Self {
|
||||||
let (config, opts) =
|
let (config, opts) = resolver_conf();
|
||||||
read_system_conf().unwrap_or((get_default_resolver_config(), ResolverOpts::default()));
|
|
||||||
let forward = ForwardConfig {
|
let forward = ForwardConfig {
|
||||||
name_servers: config.name_servers().to_vec(),
|
name_servers: config.name_servers().to_vec(),
|
||||||
options: Some(opts),
|
options: Some(opts),
|
||||||
|
|||||||
+39
-40
@@ -1,11 +1,12 @@
|
|||||||
|
use std::fmt::Debug;
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use std::sync::atomic::AtomicBool;
|
use std::sync::atomic::AtomicBool;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
use hickory_net::runtime::TokioRuntimeProvider;
|
use hickory_net::runtime::TokioRuntimeProvider;
|
||||||
use hickory_proto::rr::{IntoName, RData};
|
|
||||||
use hickory_proto::rr::rdata::SRV;
|
use hickory_proto::rr::rdata::SRV;
|
||||||
|
use hickory_proto::rr::{IntoName, RData};
|
||||||
use hickory_resolver::config::{
|
use hickory_resolver::config::{
|
||||||
ConnectionConfig, LookupIpStrategy, NameServerConfig, ResolverConfig, ResolverOpts,
|
ConnectionConfig, LookupIpStrategy, NameServerConfig, ResolverConfig, ResolverOpts,
|
||||||
};
|
};
|
||||||
@@ -19,7 +20,7 @@ use tokio::net::lookup_host;
|
|||||||
|
|
||||||
use crate::common::error::Error;
|
use crate::common::error::Error;
|
||||||
|
|
||||||
pub fn get_default_resolver_config() -> ResolverConfig {
|
pub fn resolver_conf() -> (ResolverConfig, ResolverOpts) {
|
||||||
let mut config = ResolverConfig::default();
|
let mut config = ResolverConfig::default();
|
||||||
for server in ["223.5.5.5", "180.184.1.1"] {
|
for server in ["223.5.5.5", "180.184.1.1"] {
|
||||||
config.add_name_server(NameServerConfig::new(
|
config.add_name_server(NameServerConfig::new(
|
||||||
@@ -28,23 +29,27 @@ pub fn get_default_resolver_config() -> ResolverConfig {
|
|||||||
vec![ConnectionConfig::udp()],
|
vec![ConnectionConfig::udp()],
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
config
|
|
||||||
|
let mut opts = ResolverOpts::default();
|
||||||
|
|
||||||
|
if let Ok((system_config, system_opts)) = read_system_conf() {
|
||||||
|
for ns in system_config.name_servers() {
|
||||||
|
config.add_name_server(ns.clone());
|
||||||
|
}
|
||||||
|
opts = system_opts;
|
||||||
|
}
|
||||||
|
|
||||||
|
opts.ip_strategy = LookupIpStrategy::Ipv4AndIpv6;
|
||||||
|
|
||||||
|
(config, opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub static ALLOW_USE_SYSTEM_DNS_RESOLVER: AtomicBool = AtomicBool::new(true);
|
static ALLOW_USE_SYSTEM_DNS_RESOLVER: AtomicBool = AtomicBool::new(true);
|
||||||
|
|
||||||
pub static RESOLVER: Lazy<Arc<Resolver<TokioRuntimeProvider>>> = Lazy::new(|| {
|
static RESOLVER: Lazy<Arc<Resolver<TokioRuntimeProvider>>> = Lazy::new(|| {
|
||||||
let mut cfg = get_default_resolver_config();
|
let (config, opts) = resolver_conf();
|
||||||
let mut opt = ResolverOpts::default();
|
let builder = TokioResolver::builder_with_config(config, TokioRuntimeProvider::default())
|
||||||
if let Ok((sys_cfg, sys_opt)) = read_system_conf() {
|
.with_options(opts);
|
||||||
for ns in sys_cfg.name_servers() {
|
|
||||||
cfg.add_name_server(ns.clone());
|
|
||||||
}
|
|
||||||
opt = sys_opt;
|
|
||||||
}
|
|
||||||
opt.ip_strategy = LookupIpStrategy::Ipv4AndIpv6;
|
|
||||||
let builder =
|
|
||||||
TokioResolver::builder_with_config(cfg, TokioRuntimeProvider::default()).with_options(opt);
|
|
||||||
Arc::new(
|
Arc::new(
|
||||||
builder
|
builder
|
||||||
.build()
|
.build()
|
||||||
@@ -106,51 +111,45 @@ pub async fn socket_addrs(
|
|||||||
default_port_number: impl Fn() -> Option<u16>,
|
default_port_number: impl Fn() -> Option<u16>,
|
||||||
) -> Result<Vec<SocketAddr>, Error> {
|
) -> Result<Vec<SocketAddr>, Error> {
|
||||||
let host = url.host().ok_or(Error::InvalidUrl(url.to_string()))?;
|
let host = url.host().ok_or(Error::InvalidUrl(url.to_string()))?;
|
||||||
|
|
||||||
|
// see https://github.com/EasyTier/EasyTier/pull/947, https://github.com/EasyTier/EasyTier/pull/1700
|
||||||
let port = url
|
let port = url
|
||||||
.port()
|
.port()
|
||||||
.or_else(default_port_number)
|
.or_else(default_port_number)
|
||||||
.ok_or(Error::InvalidUrl(url.to_string()))?;
|
.ok_or(Error::InvalidUrl(url.to_string()))?;
|
||||||
// See https://github.com/EasyTier/EasyTier/pull/947
|
|
||||||
// here is for compatibility with old version
|
|
||||||
let port = match port {
|
|
||||||
0 => match url.scheme() {
|
|
||||||
"ws" => 80,
|
|
||||||
"wss" => 443,
|
|
||||||
_ => port,
|
|
||||||
},
|
|
||||||
_ => port,
|
|
||||||
};
|
|
||||||
|
|
||||||
// if host is an ip address, return it directly
|
// if host is an ip address, return it directly
|
||||||
match host {
|
if let Some(ip) = match host {
|
||||||
url::Host::Ipv4(ip) => return Ok(vec![SocketAddr::new(std::net::IpAddr::V4(ip), port)]),
|
url::Host::Ipv4(ip) => Some(ip.into()),
|
||||||
url::Host::Ipv6(ip) => return Ok(vec![SocketAddr::new(std::net::IpAddr::V6(ip), port)]),
|
url::Host::Ipv6(ip) => Some(ip.into()),
|
||||||
_ => {}
|
_ => None,
|
||||||
|
} {
|
||||||
|
return Ok(vec![SocketAddr::new(ip, port)]);
|
||||||
}
|
}
|
||||||
|
|
||||||
let host = host.to_string();
|
let host = host.to_string();
|
||||||
|
|
||||||
if ALLOW_USE_SYSTEM_DNS_RESOLVER.load(std::sync::atomic::Ordering::Relaxed) {
|
if ALLOW_USE_SYSTEM_DNS_RESOLVER.load(std::sync::atomic::Ordering::Relaxed) {
|
||||||
let socket_addr = format!("{}:{}", host, port);
|
match lookup_host(format!("{}:{}", host, port)).await {
|
||||||
match lookup_host(socket_addr).await {
|
Ok(addrs) => {
|
||||||
Ok(a) => {
|
let addrs = addrs.collect();
|
||||||
let a = a.collect();
|
tracing::debug!(?addrs, "system dns lookup done");
|
||||||
tracing::debug!(?a, "system dns lookup done");
|
return Ok(addrs);
|
||||||
return Ok(a);
|
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(error) => {
|
||||||
tracing::error!(?e, "system dns lookup failed");
|
tracing::error!(?error, "system dns lookup failed");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// use hickory_resolver
|
// use hickory_resolver
|
||||||
let ret = RESOLVER.lookup_ip(&host).await.with_context(|| {
|
let ips = RESOLVER.lookup_ip(&host).await.with_context(|| {
|
||||||
format!(
|
format!(
|
||||||
"hickory dns lookup_ip failed, host: {}, port: {}",
|
"hickory dns lookup_ip failed, host: {}, port: {}",
|
||||||
host, port
|
host, port
|
||||||
)
|
)
|
||||||
})?;
|
})?;
|
||||||
Ok(ret
|
Ok(ips
|
||||||
.iter()
|
.iter()
|
||||||
.map(|ip| SocketAddr::new(ip, port))
|
.map(|ip| SocketAddr::new(ip, port))
|
||||||
.collect::<Vec<_>>())
|
.collect::<Vec<_>>())
|
||||||
|
|||||||
Reference in New Issue
Block a user