mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-11-20 02:42:12 +08:00
* refactor(config): adopt tristr for config strings Wire the tristr git dependency (pinned rev) and switch the config and enhance modules from smartstring::alias::String to tristr::alias::String. Drop the smartstring-era per-field specta overrides except the three webdav fields required by serialize_with and the ipc::JsonValue bridge: tristr maps to Primitive::str natively and the generated bindings stay byte-identical (gen-contract --check passes). Call sites in not-yet-migrated modules (cmd/core/feat/utils) are bridged with explicit tristr::alias::String paths and as_str().into() conversions; each bridge is absorbed when its module migrates. * refactor(cmd): adopt tristr for command strings Switch the cmd module from smartstring::alias::String to tristr::alias::String and drop its smartstring-era per-field specta overrides (ProfileSelection/ProfileCreate/ProfilePatch, RuntimeLogs); bindings stay byte-identical. Calls into not-yet-migrated feat/core functions keep smartstring-typed locals via full paths until those modules migrate. * refactor(core): adopt tristr for core strings Switch the core module (validate, timer, sysopt, backup, hotkey, tray, manager, notify) from smartstring::alias::String to tristr::alias::String and drop the smartstring-era specta overrides on ValidationOutcome and the notify payload structs; bindings stay byte-identical. WebDAV calls from the not-yet-migrated feat module are bridged with as_str().into(). * refactor(feat): adopt tristr for feature strings Switch the feat module (backup, icon, dns, profile, clash) from smartstring::alias::String to tristr::alias::String, drop its smartstring-era specta overrides (IconInfo, backup listings, DnsOverrideOutcome), and absorb the temporary smartstring-typed bridge locals the cmd module used to call into feat. * refactor(utils): adopt tristr for utility strings Switch utils/network.rs and utils/resolve/scheme.rs from smartstring::alias::String to tristr::alias::String and absorb the temporary user_agent and profile-name bridge conversions. * refactor(deps): drop smartstring in favor of tristr Remove the smartstring workspace and member dependencies and its Cargo.lock entry; tristr is the only string representation left. * build(deps): bump tristr to fuzz-harness fix rev Pin 3738d8c renames the stale vstr path dependency in the upstream fuzz manifest so its seven targets build again; library code is unchanged from 3756ed1a.
85 lines
2.8 KiB
Rust
85 lines
2.8 KiB
Rust
use crate::core::notify::{Refresh, announce};
|
|
use anyhow::{Result, ensure};
|
|
use clash_verge_draft::DraftTransaction;
|
|
use serde::Serialize;
|
|
use tristr::alias::String;
|
|
|
|
use crate::{
|
|
config::{
|
|
Config,
|
|
dns::{ProfileDnsSettings, dns_override_source},
|
|
profiles::PROFILE_WRITE_LOCK,
|
|
},
|
|
constants::files::DNS_CONFIG,
|
|
core::CoreManager,
|
|
utils::dirs,
|
|
};
|
|
|
|
#[derive(Serialize, specta::Type)]
|
|
#[serde(tag = "status", rename_all = "snake_case")]
|
|
pub enum DnsOverrideOutcome {
|
|
Applied,
|
|
ConfirmationRequired { source: String },
|
|
}
|
|
|
|
pub async fn set_dns_override(
|
|
profile_uid: String,
|
|
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();
|
|
ensure!(
|
|
profiles.current.as_ref() == Some(&profile_uid),
|
|
"The active profile changed; retry the DNS setting"
|
|
);
|
|
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.profile_dns_settings.insert(
|
|
profile_uid.clone(),
|
|
ProfileDnsSettings {
|
|
enabled,
|
|
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
|
|
.profile_dns_settings
|
|
.insert(profile_uid, ProfileDnsSettings::default());
|
|
});
|
|
if let Some(source) = state.source {
|
|
outcome = DnsOverrideOutcome::ConfirmationRequired { source };
|
|
}
|
|
}
|
|
|
|
transaction.commit();
|
|
// Core and memory state remain applied even when persistence fails.
|
|
let result = verge.data_arc().save_file().await;
|
|
announce(Refresh::Verge);
|
|
announce(Refresh::Clash);
|
|
result?;
|
|
Ok(outcome)
|
|
}
|