store DnsServer in GlobalCtx, load routes from DnsServer in ProxyCidrsMonitor

This commit is contained in:
Luna Yao
2026-02-23 03:29:00 +01:00
parent 3fc030de48
commit 86871b14d5
5 changed files with 29 additions and 3 deletions
+14
View File
@@ -32,7 +32,9 @@ use crate::{
};
use crossbeam::atomic::AtomicCell;
use hmac::{Hmac, Mac};
use parking_lot::RwLock;
use sha2::Sha256;
use crate::dns::server::DnsServer;
use socket2::Protocol;
pub type NetworkIdentity = crate::common::config::NetworkIdentity;
@@ -202,6 +204,8 @@ pub struct GlobalCtx {
hostname: Mutex<String>,
dns: RwLock<Option<Arc<DnsServer>>>,
stun_info_collection: Mutex<Arc<dyn StunInfoCollectorTrait>>,
running_listeners: Mutex<Vec<url::Url>>,
@@ -298,6 +302,8 @@ impl GlobalCtx {
stun_info_collector.clone(),
)))),
dns: RwLock::new(None),
hostname: Mutex::new(hostname),
stun_info_collection: Mutex::new(stun_info_collector),
@@ -416,6 +422,14 @@ 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();
}
+1 -1
View File
@@ -2,6 +2,6 @@ mod node;
mod node_mgr;
pub mod config;
mod peer_mgr;
mod server;
pub mod server;
mod utils;
pub mod zone;
+4
View File
@@ -68,12 +68,16 @@ impl DnsNode {
let server = Arc::new(DnsServer::new(self.peer_mgr.clone(), rpc));
self.peer_mgr
.get_global_ctx_ref()
.set_dns(Some(server.clone()));
tokio::join!(
self.peer_mgr
.add_nic_packet_process_pipeline(Box::new(server.clone())),
server.run()
);
self.peer_mgr.get_global_ctx_ref().set_dns(None);
let _ = self
.peer_mgr
.remove_nic_packet_process_pipeline(server.id())
+2 -2
View File
@@ -177,8 +177,8 @@ impl DnsServer {
}
}
pub fn routes(&self) -> HashSet<IpAddr> {
self.addresses.read().iter().map(|a| a.addr.ip()).collect()
pub fn addresses(&self) -> HashSet<SocketAddr> {
self.addresses.read().iter().map(|a| a.addr).collect()
}
async fn reload_listeners(
@@ -1,4 +1,5 @@
use std::collections::BTreeSet;
use std::net::IpAddr;
use std::sync::{Arc, Weak};
use std::time::Instant;
@@ -54,6 +55,13 @@ impl ProxyCidrsMonitor {
proxy_cidrs = routes.into_iter().collect();
}
if let Some(dns) = global_ctx.get_dns() {
proxy_cidrs.extend(dns.addresses().into_iter().filter_map(|a| match a.ip() {
IpAddr::V4(ip) => Some(cidr::Ipv4Cidr::new_host(ip)),
_ => None,
}))
}
// Calculate diff
if cur_proxy_cidrs == &proxy_cidrs {
return (proxy_cidrs, Vec::new(), Vec::new());