mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-12-05 05:50:04 +08:00
refactor(subscription): keep profile writes outside proxy fallback
This commit is contained in:
@@ -12,7 +12,9 @@ pub(crate) mod snapshot;
|
|||||||
mod verge;
|
mod verge;
|
||||||
|
|
||||||
pub(crate) use self::config::Config;
|
pub(crate) use self::config::Config;
|
||||||
pub(crate) use self::prfitem::{build_remote_profile, fetch_remote_refresh, read_profile_item_file};
|
pub(crate) use self::prfitem::{
|
||||||
|
build_remote_profile, download_subscription, prepare_remote_refresh, read_profile_item_file,
|
||||||
|
};
|
||||||
pub use self::{clash::*, encrypt::*, mixed_port::*, profiles::*, verge::*};
|
pub use self::{clash::*, encrypt::*, mixed_port::*, profiles::*, verge::*};
|
||||||
pub use clash_verge_profile::{PrfExtra, PrfItem, PrfOption, PrfSelected};
|
pub use clash_verge_profile::{PrfExtra, PrfItem, PrfOption, PrfSelected};
|
||||||
|
|
||||||
|
|||||||
@@ -117,7 +117,7 @@ async fn from_local(
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
struct DownloadedSubscription {
|
pub(crate) struct DownloadedSubscription {
|
||||||
url: Url,
|
url: Url,
|
||||||
suggested_name: Option<std::string::String>,
|
suggested_name: Option<std::string::String>,
|
||||||
extra: Option<PrfExtra>,
|
extra: Option<PrfExtra>,
|
||||||
@@ -172,12 +172,7 @@ pub(crate) async fn build_remote_profile(
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) async fn fetch_remote_refresh(url: &str, option: Option<&PrfOption>) -> Result<RemoteProfileRefresh> {
|
pub(crate) async fn download_subscription(url: &str, option: Option<&PrfOption>) -> Result<DownloadedSubscription> {
|
||||||
let downloaded = download_subscription(url, option).await?;
|
|
||||||
prepare_remote_refresh(downloaded, option).await
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn download_subscription(url: &str, option: Option<&PrfOption>) -> Result<DownloadedSubscription> {
|
|
||||||
let with_proxy = option.is_some_and(|o| o.with_proxy.unwrap_or(false));
|
let with_proxy = option.is_some_and(|o| o.with_proxy.unwrap_or(false));
|
||||||
let self_proxy = option.is_some_and(|o| o.self_proxy.unwrap_or(false));
|
let self_proxy = option.is_some_and(|o| o.self_proxy.unwrap_or(false));
|
||||||
let accept_invalid_certs = option.is_some_and(|o| o.danger_accept_invalid_certs.unwrap_or(false));
|
let accept_invalid_certs = option.is_some_and(|o| o.danger_accept_invalid_certs.unwrap_or(false));
|
||||||
@@ -233,7 +228,7 @@ async fn download_subscription(url: &str, option: Option<&PrfOption>) -> Result<
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn prepare_remote_refresh(
|
pub(crate) async fn prepare_remote_refresh(
|
||||||
downloaded: DownloadedSubscription,
|
downloaded: DownloadedSubscription,
|
||||||
option: Option<&PrfOption>,
|
option: Option<&PrfOption>,
|
||||||
) -> Result<RemoteProfileRefresh> {
|
) -> Result<RemoteProfileRefresh> {
|
||||||
@@ -417,14 +412,12 @@ mod tests {
|
|||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
let item: clash_verge_profile::PrfItem = build_remote_profile(&url, None, None, Some(&option)).await?;
|
let item: clash_verge_profile::PrfItem = build_remote_profile(&url, None, None, Some(&option)).await?;
|
||||||
let refresh = fetch_remote_refresh(
|
let refresh_option = PrfOption {
|
||||||
&url,
|
update_interval: None,
|
||||||
Some(&PrfOption {
|
..option
|
||||||
update_interval: None,
|
};
|
||||||
..option
|
let downloaded = download_subscription(&url, Some(&refresh_option)).await?;
|
||||||
}),
|
let refresh = prepare_remote_refresh(downloaded, Some(&refresh_option)).await?;
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
server.await??;
|
server.await??;
|
||||||
assert_eq!(item.itype.as_deref(), Some("remote"));
|
assert_eq!(item.itype.as_deref(), Some("remote"));
|
||||||
assert_eq!(item.name.as_deref(), Some("remote.yaml"));
|
assert_eq!(item.name.as_deref(), Some("remote.yaml"));
|
||||||
|
|||||||
@@ -315,8 +315,13 @@ pub(crate) async fn profiles_save_file_safe() -> Result<()> {
|
|||||||
with_profiles_edit(|profiles| async move { save_profiles(&profiles).await.map(|_| profiles) }).await
|
with_profiles_edit(|profiles| async move { save_profiles(&profiles).await.map(|_| profiles) }).await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) async fn apply_remote_refresh_safe(index: &str, refresh: RemoteProfileRefresh) -> Result<()> {
|
pub(crate) async fn apply_remote_refresh_safe(index: &str, url: &str, refresh: RemoteProfileRefresh) -> Result<()> {
|
||||||
with_profiles_edit(|mut profiles| async move {
|
with_profiles_edit(|mut profiles| async move {
|
||||||
|
let current = profiles.get_item(index)?;
|
||||||
|
anyhow::ensure!(
|
||||||
|
current.itype.as_deref() == Some("remote") && current.url.as_deref() == Some(url),
|
||||||
|
"profile source changed during refresh"
|
||||||
|
);
|
||||||
apply_remote_refresh(&mut profiles, index, refresh).await?;
|
apply_remote_refresh(&mut profiles, index, refresh).await?;
|
||||||
Ok(profiles)
|
Ok(profiles)
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
config::{Config, PrfOption, fetch_remote_refresh, profiles::apply_remote_refresh_safe},
|
config::{Config, PrfOption, download_subscription, prepare_remote_refresh, profiles::apply_remote_refresh_safe},
|
||||||
core::{
|
core::{
|
||||||
CoreManager, handle,
|
CoreManager, handle,
|
||||||
notify::{NoticeStatus, Refresh, announce},
|
notify::{NoticeStatus, Refresh, announce},
|
||||||
@@ -101,7 +101,7 @@ async fn perform_profile_update(
|
|||||||
}
|
}
|
||||||
tracing::Span::current().record("strategy", name);
|
tracing::Span::current().record("strategy", name);
|
||||||
let started = std::time::Instant::now();
|
let started = std::time::Instant::now();
|
||||||
let result = fetch_remote_refresh(url, options.as_ref()).await;
|
let result = download_subscription(url, options.as_ref()).await;
|
||||||
let elapsed_field = match strategy {
|
let elapsed_field = match strategy {
|
||||||
Strategy::Configured => "configured_elapsed",
|
Strategy::Configured => "configured_elapsed",
|
||||||
Strategy::Clash => "clash_elapsed",
|
Strategy::Clash => "clash_elapsed",
|
||||||
@@ -123,9 +123,17 @@ async fn perform_profile_update(
|
|||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
let last_err = match result {
|
let last_err = match result {
|
||||||
Ok((strategy, refresh)) => {
|
Ok((strategy, downloaded)) => {
|
||||||
// A persistence error must not replay a successful download with another transport.
|
// A persistence error must not replay a successful download with another transport.
|
||||||
apply_remote_refresh_safe(uid, refresh).await?;
|
let profiles = Config::profiles().await;
|
||||||
|
let snapshot = profiles.latest_arc();
|
||||||
|
let current = snapshot.get_item(uid)?;
|
||||||
|
anyhow::ensure!(
|
||||||
|
current.itype.as_deref() == Some("remote") && current.url.as_deref() == Some(url),
|
||||||
|
"profile source changed during refresh"
|
||||||
|
);
|
||||||
|
let refresh = prepare_remote_refresh(downloaded, merged_opt.as_ref()).await?;
|
||||||
|
apply_remote_refresh_safe(uid, url, refresh).await?;
|
||||||
if !matches!(strategy, Strategy::Configured) {
|
if !matches!(strategy, Strategy::Configured) {
|
||||||
handle::Handle::notice(NoticeStatus::UpdateWithClashProxy, profile_name.as_str());
|
handle::Handle::notice(NoticeStatus::UpdateWithClashProxy, profile_name.as_str());
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user