Files
clash-verge-rev/src-tauri/src/utils/resolve/scheme.rs
T

121 lines
4.0 KiB
Rust

use crate::core::notify::NoticeStatus;
use anyhow::Result;
use tauri::Url;
use tristr::alias::String;
use crate::{
config::{Config, PrfItem, build_remote_profile, profiles},
core::{CoreManager, handle, timer::Timer},
utils::help,
};
use clash_verge_logging::{Type, logging, logging_error};
use clash_verge_subscription::decode_subscription_link;
pub(super) async fn resolve_scheme(param: &str) -> Result<()> {
let param_str = if param.starts_with("[") && param.len() > 4 {
param
.get(2..param.len() - 2)
.ok_or_else(|| anyhow::anyhow!("Invalid string slice boundaries"))?
} else {
param
};
let masked_deep_link = help::mask_url(param_str);
logging!(debug, Type::Config, "received deep link: {masked_deep_link}");
let link_parsed = Url::parse(param_str)
.map_err(|e| anyhow::anyhow!("failed to parse deep link: {e:?}, param: {masked_deep_link}"))?;
let Some(link) = decode_subscription_link(&link_parsed) else {
logging!(
warn,
Type::Config,
"missing url parameter in deep link: {masked_deep_link}"
);
return Ok(());
};
let name = link.name.map(String::from);
import_subscription(&link.url, name.as_ref()).await;
Ok(())
}
async fn import_subscription(url: &str, name: Option<&String>) {
let had_current_profile = {
let profiles = Config::profiles().await;
profiles.latest_arc().current.is_some()
};
let Some(mut item) = fetch_profile_item(url, name).await else {
return;
};
let uid = item.uid.clone().unwrap_or_default();
if let Err(e) = profiles::append_profile(&mut item).await {
logging!(error, Type::Config, "failed to import subscription url: {:?}", e);
Config::profiles().await.discard();
handle::Handle::notice(NoticeStatus::ImportSubUrlError, e.to_string());
return;
}
if let Err(e) = profiles::save_committed_profiles().await {
logging!(error, Type::Config, "failed to save imported subscription: {e:#}");
handle::Handle::notice(NoticeStatus::ImportSubUrlError, e.to_string());
return;
}
logging_error!(Type::Timer, Timer::global().refresh().await);
handle::Handle::notice(
NoticeStatus::ImportSubUrlOk,
"", // 空 msg 传入,我们不希望导致 后端-前端-后端 死循环,这里只做提醒。
);
post_import_updates(&uid, had_current_profile).await;
}
async fn fetch_profile_item(url: &str, name: Option<&String>) -> Option<PrfItem> {
match build_remote_profile(url, name, None, None).await {
Ok(item) => Some(item),
Err(e) => {
logging!(error, Type::Config, "failed to parse profile from url: {:?}", e);
handle::Handle::notice(NoticeStatus::ImportSubUrlError, e.to_string());
None
}
}
}
async fn post_import_updates(uid: &str, had_current_profile: bool) {
handle::Handle::refresh_verge();
handle::Handle::notify_profile_changed(uid);
let should_update_core = if uid.is_empty() || had_current_profile {
false
} else {
let profiles = Config::profiles().await;
profiles.latest_arc().current.as_deref() == Some(uid)
};
if should_update_core {
refresh_core_config().await;
}
}
async fn refresh_core_config() {
logging!(
info,
Type::Config,
"Deep link import set current profile; refreshing core config"
);
match CoreManager::global().update_config_forced().await {
Ok(outcome) if outcome.is_valid() => handle::Handle::refresh_clash(),
Ok(outcome) => {
let message = outcome.to_string();
logging!(warn, Type::Config, "Apply config failed: {}", message);
handle::Handle::notice(NoticeStatus::ConfigValidateError, message);
}
Err(err) => {
logging!(error, Type::Config, "Apply config error: {err:#}");
handle::Handle::notice(NoticeStatus::UpdateFailed, format!("{err}"));
}
}
}