runtime fixes and multicast ignore

This commit is contained in:
2026-03-19 15:05:56 +07:00
parent 5f9cf382ea
commit 3b5bbf00ae
2 changed files with 37 additions and 46 deletions
+23 -43
View File
@@ -22,7 +22,6 @@ impl DnsHandler {
cache_path: format!("{}/hosts_cache.txt", cache_dir), cache_path: format!("{}/hosts_cache.txt", cache_dir),
} }
} }
pub async fn init(&mut self) -> anyhow::Result<()> { pub async fn init(&mut self) -> anyhow::Result<()> {
let path = std::path::PathBuf::from(&self.cache_path); let path = std::path::PathBuf::from(&self.cache_path);
@@ -41,60 +40,41 @@ impl DnsHandler {
if needs_update { if needs_update {
let p_clone = self.cache_path.clone(); let p_clone = self.cache_path.clone();
// Просто спавним асинхронную задачу
tokio::spawn(async move { tokio::spawn(async move {
tokio::time::sleep(Duration::from_secs(10)).await; tokio::time::sleep(Duration::from_secs(10)).await;
Self::download_blocklist(p_clone); // Теперь вызываем асинхронно через .await
if let Err(e) = Self::download_blocklist_async(p_clone).await {
netrunner_logger::error!("DNS: Background update failed: {}", e);
}
}); });
} }
Ok(()) Ok(())
} }
fn download_blocklist(cache_path: String) { // Делаем функцию асинхронной и убираем block_on
let runtime = crate::RUNTIME.get_or_init(|| { async fn download_blocklist_async(cache_path: String) -> anyhow::Result<()> {
tokio::runtime::Builder::new_multi_thread() info!("DNS: Starting background download to {}", cache_path);
.enable_all()
.build()
.expect("Failed to create Tokio runtime")
});
runtime.block_on(async move { let client = reqwest::Client::builder()
info!("DNS: Starting background download to {}", cache_path); .timeout(Duration::from_secs(30))
.build()?;
let client = reqwest::Client::builder() let resp = client
.timeout(Duration::from_secs(30)) .get("https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts")
.build(); .send()
.await?;
let client = match client { if resp.status().is_success() {
Ok(c) => c, let bytes = resp.bytes().await?;
Err(e) => { tokio::fs::write(&cache_path, bytes).await?;
netrunner_logger::error!("DNS: Failed to create client: {}", e); info!("DNS: Blocklist downloaded successfully to {}", cache_path);
return; } else {
} error!("DNS: Download failed with status {}", resp.status());
}; }
match client Ok(())
.get("https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts")
.send()
.await
{
Ok(resp) if resp.status().is_success() => {
if let Ok(bytes) = resp.bytes().await {
if let Err(e) = tokio::fs::write(&cache_path, bytes).await {
netrunner_logger::error!("DNS: File write error: {}", e);
} else {
info!("DNS: Blocklist downloaded successfully to {}", cache_path);
}
}
}
Ok(resp) => {
netrunner_logger::error!("DNS: Download failed with status {}", resp.status())
}
Err(e) => {
netrunner_logger::error!("DNS: Request error (check internet/TLS): {}", e)
}
}
});
} }
async fn load_from_file(&mut self) -> anyhow::Result<()> { async fn load_from_file(&mut self) -> anyhow::Result<()> {
+14 -3
View File
@@ -120,9 +120,20 @@ impl Engine {
break; break;
} }
if n >= 20 && buf[0] >> 4 == 4 { let first_byte = buf[0];
let dest_ip = &buf[16..20]; let version = first_byte >> 4;
if dest_ip[0] >= 224 && dest_ip[0] <= 239 {
if version == 4 {
// IPv4 Multicast: 224.0.0.0 - 239.255.255.255
if buf[12..16] == [0, 0, 0, 0] {
continue;
} // Маршрутизация
if buf[16] >= 224 && buf[16] <= 239 {
continue;
}
} else if version == 6 {
// IPv6 Multicast: ff00::/8 (первый байт 0xff)
if first_byte == 0xff {
continue; continue;
} }
} }