mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-11-23 04:42:08 +08:00
feat(dns): protect provider DNS from unconfirmed overrides (#7902)
Fixes #7292
This commit is contained in:
@@ -9,7 +9,7 @@ use crate::{
|
||||
};
|
||||
use anyhow::{Result, anyhow};
|
||||
use chrono::Utc;
|
||||
use clash_verge_logging::{Type, logging};
|
||||
use clash_verge_logging::{Type, logging, logging_error};
|
||||
use reqwest_dav::list_cmd::ListFile;
|
||||
use serde::Serialize;
|
||||
use smartstring::alias::String;
|
||||
@@ -85,6 +85,7 @@ async fn finalize_restored_verge_config(
|
||||
return Err(err);
|
||||
}
|
||||
}
|
||||
logging_error!(Type::Config, Config::sync_dns_override().await);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
use anyhow::Result;
|
||||
use clash_verge_draft::DraftTransaction;
|
||||
use serde::Serialize;
|
||||
use smartstring::alias::String;
|
||||
|
||||
use crate::{
|
||||
config::{Config, dns::dns_override_source, profiles::PROFILE_WRITE_LOCK},
|
||||
constants::files::DNS_CONFIG,
|
||||
core::{CoreManager, handle::Handle},
|
||||
utils::dirs,
|
||||
};
|
||||
|
||||
#[derive(Serialize)]
|
||||
#[serde(tag = "status", rename_all = "snake_case")]
|
||||
pub enum DnsOverrideOutcome {
|
||||
Applied,
|
||||
ConfirmationRequired { source: String },
|
||||
}
|
||||
|
||||
pub async fn set_dns_override(enabled: bool, confirmation: Option<String>) -> Result<DnsOverrideOutcome> {
|
||||
let _profile_write = PROFILE_WRITE_LOCK.lock().await;
|
||||
let _config_write = Config::lock_config_write().await;
|
||||
let profiles = Config::profiles().await.data_arc();
|
||||
let source = dns_override_source(
|
||||
profiles.current.as_deref().unwrap_or_default(),
|
||||
&profiles.current_mapping().await?,
|
||||
)?;
|
||||
if enabled && let Some(source) = source.as_ref().filter(|source| Some(*source) != confirmation.as_ref()) {
|
||||
return Ok(DnsOverrideOutcome::ConfirmationRequired { source: source.clone() });
|
||||
}
|
||||
|
||||
if enabled {
|
||||
let content = tokio::fs::read_to_string(dirs::app_home_dir()?.join(DNS_CONFIG)).await?;
|
||||
serde_yaml_ng::from_str::<serde_yaml_ng::Mapping>(&content)?;
|
||||
}
|
||||
|
||||
let verge = Config::verge().await;
|
||||
let transaction = DraftTransaction::begin(vec![&verge])?;
|
||||
verge.edit_draft(|draft| {
|
||||
draft.enable_dns_settings = Some(enabled);
|
||||
draft.dns_override_confirmation = if enabled { source } else { None };
|
||||
});
|
||||
CoreManager::global().update_config_checked().await?;
|
||||
|
||||
let state = Config::runtime().await.data_arc().dns_override.clone();
|
||||
let mut outcome = DnsOverrideOutcome::Applied;
|
||||
// A subscription update can replace the file while Mihomo is validating it.
|
||||
if enabled && let Some(state) = state.filter(|state| !state.enabled) {
|
||||
verge.edit_draft(|draft| {
|
||||
draft.enable_dns_settings = Some(false);
|
||||
draft.dns_override_confirmation = None;
|
||||
});
|
||||
if let Some(source) = state.source {
|
||||
outcome = DnsOverrideOutcome::ConfirmationRequired { source };
|
||||
}
|
||||
}
|
||||
|
||||
transaction.commit();
|
||||
Handle::refresh_verge();
|
||||
Handle::refresh_clash();
|
||||
verge.data_arc().save_file().await?;
|
||||
Ok(outcome)
|
||||
}
|
||||
@@ -2,6 +2,7 @@ mod backup;
|
||||
mod clash;
|
||||
mod config;
|
||||
mod core_upgrade;
|
||||
mod dns;
|
||||
mod icon;
|
||||
mod listener;
|
||||
mod profile;
|
||||
@@ -14,6 +15,7 @@ pub use backup::*;
|
||||
pub use clash::*;
|
||||
pub use config::*;
|
||||
pub use core_upgrade::*;
|
||||
pub use dns::*;
|
||||
pub use icon::*;
|
||||
pub use listener::*;
|
||||
pub use profile::*;
|
||||
|
||||
@@ -5,7 +5,7 @@ use crate::{
|
||||
utils::help::{mask_err, mask_url},
|
||||
};
|
||||
use anyhow::{Result, bail};
|
||||
use clash_verge_logging::{Type, logging};
|
||||
use clash_verge_logging::{Type, logging, logging_error};
|
||||
use smartstring::alias::String;
|
||||
|
||||
/// Toggle proxy profile
|
||||
@@ -200,6 +200,7 @@ pub async fn update_profile(uid: &String, option: Option<&PrfOption>, is_mannual
|
||||
logging!(debug, Type::Config, "[订阅更新] 更新内核配置");
|
||||
match CoreManager::global().update_config_with_force(is_mannual_trigger).await {
|
||||
Ok(outcome) if outcome.is_valid() => {
|
||||
logging_error!(Type::Config, Config::sync_dns_override().await);
|
||||
handle::Handle::refresh_clash();
|
||||
}
|
||||
Ok(outcome @ (ValidationOutcome::Skipped { .. } | ValidationOutcome::Busy)) if !is_mannual_trigger => {
|
||||
@@ -228,5 +229,9 @@ pub async fn update_profile(uid: &String, option: Option<&PrfOption>, is_mannual
|
||||
|
||||
/// 增强配置
|
||||
pub async fn enhance_profiles() -> Result<ValidationOutcome> {
|
||||
CoreManager::global().update_config_forced().await
|
||||
let outcome = CoreManager::global().update_config_forced().await?;
|
||||
if outcome.is_valid() {
|
||||
logging_error!(Type::Config, Config::sync_dns_override().await);
|
||||
}
|
||||
Ok(outcome)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user