mirror of
https://github.com/EasyTier/EasyTier.git
synced 2026-08-07 21:19:51 +00:00
fix magic-dns feature gate
This commit is contained in:
@@ -1,3 +1,12 @@
|
||||
use super::env_parser;
|
||||
use crate::{
|
||||
common::stun::StunInfoCollector,
|
||||
proto::{
|
||||
acl::Acl,
|
||||
common::{CompressionAlgoPb, PortForwardConfigPb, SecureModeConfig, SocketType},
|
||||
},
|
||||
tunnel::generate_digest_from_str,
|
||||
};
|
||||
use anyhow::Context;
|
||||
use base64::{prelude::BASE64_STANDARD, Engine as _};
|
||||
use cfg_if::cfg_if;
|
||||
@@ -13,23 +22,15 @@ use std::{
|
||||
use strum::{Display, EnumString, VariantArray};
|
||||
use tokio::io::AsyncReadExt as _;
|
||||
|
||||
use crate::{
|
||||
common::stun::StunInfoCollector,
|
||||
dns::config::DnsConfig,
|
||||
proto::{
|
||||
acl::Acl,
|
||||
common::{CompressionAlgoPb, PortForwardConfigPb, SecureModeConfig, SocketType},
|
||||
},
|
||||
tunnel::generate_digest_from_str,
|
||||
};
|
||||
|
||||
use super::env_parser;
|
||||
|
||||
pub type Flags = crate::proto::common::FlagsInConfig;
|
||||
|
||||
pub fn gen_default_flags() -> Flags {
|
||||
#[allow(deprecated)]
|
||||
Flags {
|
||||
#[allow(deprecated)]
|
||||
quic_listen_port: u32::MAX,
|
||||
#[allow(deprecated)]
|
||||
tld_dns_zone: "".to_string(),
|
||||
|
||||
default_protocol: "tcp".to_string(),
|
||||
dev_name: "".to_string(),
|
||||
enable_encryption: true,
|
||||
@@ -64,9 +65,6 @@ pub fn gen_default_flags() -> Flags {
|
||||
multi_thread_count: 2,
|
||||
encryption_algorithm: EncryptionAlgorithm::default().to_string(),
|
||||
disable_sym_hole_punching: false,
|
||||
tld_dns_zone: "".to_string(),
|
||||
|
||||
quic_listen_port: u32::MAX,
|
||||
need_p2p: false,
|
||||
instance_recv_bps_limit: u64::MAX,
|
||||
}
|
||||
@@ -119,8 +117,17 @@ impl Default for EncryptionAlgorithm {
|
||||
}
|
||||
}
|
||||
|
||||
cfg_if! {
|
||||
if #[cfg(feature = "magic-dns")] {
|
||||
use crate::dns::config::{DnsConfig, DnsConfigLoaderExt};
|
||||
} else {
|
||||
#[auto_impl::auto_impl(Box, &)]
|
||||
pub trait DnsConfigLoaderExt {}
|
||||
}
|
||||
}
|
||||
|
||||
#[auto_impl::auto_impl(Box, &)]
|
||||
pub trait ConfigLoader: Send + Sync {
|
||||
pub trait ConfigLoader: Send + Sync + DnsConfigLoaderExt {
|
||||
fn get_id(&self) -> uuid::Uuid;
|
||||
fn set_id(&self, id: uuid::Uuid);
|
||||
|
||||
@@ -203,15 +210,9 @@ pub trait ConfigLoader: Send + Sync {
|
||||
}
|
||||
fn set_credential_file(&self, _path: Option<std::path::PathBuf>) {}
|
||||
|
||||
fn get_dns(&self) -> DnsConfig;
|
||||
fn set_dns(&self, dns: DnsConfig);
|
||||
|
||||
fn get_hostname(&self) -> String;
|
||||
fn set_hostname(&self, hostname: &str);
|
||||
|
||||
fn get_fqdn(&self) -> String;
|
||||
fn set_fqdn(&self, fqdn: &str);
|
||||
|
||||
fn dump(&self) -> String;
|
||||
}
|
||||
|
||||
@@ -450,6 +451,7 @@ struct Config {
|
||||
peer: Option<Vec<PeerConfig>>,
|
||||
proxy_network: Option<Vec<ProxyNetworkConfig>>,
|
||||
|
||||
#[cfg(feature = "magic-dns")]
|
||||
dns: DnsConfig,
|
||||
|
||||
vpn_portal_config: Option<VpnPortalConfig>,
|
||||
@@ -535,6 +537,28 @@ impl TomlConfigLoader {
|
||||
}
|
||||
}
|
||||
|
||||
impl DnsConfigLoaderExt for TomlConfigLoader {
|
||||
cfg_if! {
|
||||
if #[cfg(feature = "magic-dns")] {
|
||||
fn get_dns(&self) -> DnsConfig {
|
||||
self.config.lock().unwrap().dns.clone()
|
||||
}
|
||||
|
||||
fn set_dns(&self, dns: DnsConfig) {
|
||||
self.config.lock().unwrap().dns = dns;
|
||||
}
|
||||
|
||||
fn get_fqdn(&self) -> String {
|
||||
self.config.lock().unwrap().dns.get_fqdn().to_string()
|
||||
}
|
||||
|
||||
fn set_fqdn(&self, fqdn: &str) {
|
||||
self.config.lock().unwrap().dns.set_fqdn(fqdn);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ConfigLoader for TomlConfigLoader {
|
||||
fn get_id(&self) -> uuid::Uuid {
|
||||
let mut locked_config = self.config.lock().unwrap();
|
||||
@@ -849,14 +873,6 @@ impl ConfigLoader for TomlConfigLoader {
|
||||
self.config.lock().unwrap().credential_file = path;
|
||||
}
|
||||
|
||||
fn get_dns(&self) -> DnsConfig {
|
||||
self.config.lock().unwrap().dns.clone()
|
||||
}
|
||||
|
||||
fn set_dns(&self, dns: DnsConfig) {
|
||||
self.config.lock().unwrap().dns = dns;
|
||||
}
|
||||
|
||||
fn get_hostname(&self) -> String {
|
||||
self.config.lock().unwrap().dns.get_name().to_string()
|
||||
}
|
||||
@@ -865,14 +881,6 @@ impl ConfigLoader for TomlConfigLoader {
|
||||
self.config.lock().unwrap().dns.set_name(hostname);
|
||||
}
|
||||
|
||||
fn get_fqdn(&self) -> String {
|
||||
self.config.lock().unwrap().dns.get_fqdn().to_string()
|
||||
}
|
||||
|
||||
fn set_fqdn(&self, fqdn: &str) {
|
||||
self.config.lock().unwrap().dns.set_fqdn(fqdn);
|
||||
}
|
||||
|
||||
fn dump(&self) -> String {
|
||||
let default_flags_json = serde_json::to_string(&gen_default_flags()).unwrap();
|
||||
let default_flags_hashmap =
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use std::{
|
||||
collections::{hash_map::DefaultHasher, HashMap},
|
||||
hash::Hasher,
|
||||
iter,
|
||||
net::{IpAddr, SocketAddr},
|
||||
sync::{Arc, Mutex},
|
||||
time::{SystemTime, UNIX_EPOCH},
|
||||
@@ -30,11 +31,16 @@ use crate::{
|
||||
},
|
||||
tunnel::matches_protocol,
|
||||
};
|
||||
#[cfg(feature = "magic-dns")]
|
||||
use crate::dns::{
|
||||
config::{DnsExportConfig, DnsGlobalCtxExt},
|
||||
server::DnsServer,
|
||||
};
|
||||
use crossbeam::atomic::AtomicCell;
|
||||
use hmac::{Hmac, Mac};
|
||||
use itertools::Itertools;
|
||||
use parking_lot::RwLock;
|
||||
use sha2::Sha256;
|
||||
use crate::dns::server::DnsServer;
|
||||
use socket2::Protocol;
|
||||
|
||||
pub type NetworkIdentity = crate::common::config::NetworkIdentity;
|
||||
@@ -204,7 +210,8 @@ pub struct GlobalCtx {
|
||||
|
||||
hostname: Mutex<String>,
|
||||
|
||||
dns: RwLock<Option<Arc<DnsServer>>>,
|
||||
#[cfg(feature = "magic-dns")]
|
||||
dns_server: RwLock<Option<Arc<DnsServer>>>,
|
||||
|
||||
stun_info_collection: Mutex<Arc<dyn StunInfoCollectorTrait>>,
|
||||
|
||||
@@ -239,7 +246,7 @@ impl std::fmt::Debug for GlobalCtx {
|
||||
}
|
||||
}
|
||||
|
||||
pub type ArcGlobalCtx = std::sync::Arc<GlobalCtx>;
|
||||
pub type ArcGlobalCtx = Arc<GlobalCtx>;
|
||||
|
||||
impl GlobalCtx {
|
||||
fn derive_feature_flags(flags: &Flags, current: Option<PeerFeatureFlag>) -> PeerFeatureFlag {
|
||||
@@ -302,7 +309,8 @@ impl GlobalCtx {
|
||||
stun_info_collector.clone(),
|
||||
)))),
|
||||
|
||||
dns: RwLock::new(None),
|
||||
#[cfg(feature = "magic-dns")]
|
||||
dns_server: RwLock::new(None),
|
||||
|
||||
hostname: Mutex::new(hostname),
|
||||
|
||||
@@ -422,14 +430,6 @@ impl GlobalCtx {
|
||||
self.ip_collector.lock().unwrap().as_ref().unwrap().clone()
|
||||
}
|
||||
|
||||
pub fn get_dns(&self) -> Option<Arc<DnsServer>> {
|
||||
self.dns.read().clone()
|
||||
}
|
||||
|
||||
pub fn set_dns(&self, dns: Option<Arc<DnsServer>>) {
|
||||
*self.dns.write() = dns;
|
||||
}
|
||||
|
||||
pub fn get_hostname(&self) -> String {
|
||||
return self.hostname.lock().unwrap().clone();
|
||||
}
|
||||
@@ -680,6 +680,47 @@ impl GlobalCtx {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "magic-dns")]
|
||||
impl DnsGlobalCtxExt for GlobalCtx {
|
||||
fn dns_server(&self) -> Option<Arc<DnsServer>> {
|
||||
self.dns_server.read().clone()
|
||||
}
|
||||
|
||||
fn set_dns_server(&self, dns: Option<Arc<DnsServer>>) {
|
||||
*self.dns_server.write() = dns;
|
||||
}
|
||||
|
||||
fn dns_self_zone(&self) -> crate::dns::config::zone::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();
|
||||
|
||||
crate::dns::config::zone::ZoneConfig::dedicated(
|
||||
Some(self.get_id()),
|
||||
fqdn.clone(),
|
||||
ipv4,
|
||||
ipv6,
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
fn dns_export_config(&self) -> DnsExportConfig {
|
||||
DnsExportConfig {
|
||||
zones: self
|
||||
.dns_iter_zones()
|
||||
.filter(|z| z.policy.export.is_some()) // TODO: check policies of parent zones
|
||||
.map_into()
|
||||
.collect(),
|
||||
fqdn: self.config.get_dns().get_fqdn().to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
fn dns_iter_zones(&self) -> impl Iterator<Item = crate::dns::config::zone::ZoneConfig> {
|
||||
iter::once(self.dns_self_zone()).chain(self.config.get_dns().zones)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod tests {
|
||||
use crate::{
|
||||
|
||||
Reference in New Issue
Block a user