mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-12-05 05:50:04 +08:00
refactor(subscription): pass the proxy directly to each download
This commit is contained in:
@@ -1,11 +1,17 @@
|
||||
use crate::{
|
||||
config::{Config, PrfOption, download_subscription, prepare_remote_refresh, profiles::save_remote_refresh},
|
||||
config::{
|
||||
Config, PrfOption, download_subscription, prepare_remote_refresh, profiles::save_remote_refresh,
|
||||
subscription_proxy,
|
||||
},
|
||||
core::{
|
||||
CoreManager, handle,
|
||||
notify::{NoticeStatus, Refresh, announce},
|
||||
validate::ValidationOutcome,
|
||||
},
|
||||
utils::help::{mask_err, mask_url},
|
||||
utils::{
|
||||
help::{mask_err, mask_url},
|
||||
network::ProxyType,
|
||||
},
|
||||
};
|
||||
use anyhow::{Result, bail};
|
||||
use clash_verge_logging::{Type, logging};
|
||||
@@ -48,11 +54,13 @@ async fn should_update_profile(uid: &str, ignore_auto_update: bool) -> Result<Op
|
||||
async fn perform_profile_update(
|
||||
uid: &str,
|
||||
url: &str,
|
||||
opt: Option<&PrfOption>,
|
||||
option: Option<&PrfOption>,
|
||||
profile_options: Option<&PrfOption>,
|
||||
overrides: Option<&PrfOption>,
|
||||
is_manual_trigger: bool,
|
||||
) -> Result<()> {
|
||||
let merged_opt = PrfOption::merge(opt, option);
|
||||
let options = PrfOption::merge(profile_options, overrides);
|
||||
let options = options.as_ref();
|
||||
let configured_proxy = subscription_proxy(options);
|
||||
let profiles = Config::profiles().await;
|
||||
let profiles_arc = profiles.latest_arc();
|
||||
let profile_name = profiles_arc
|
||||
@@ -71,54 +79,44 @@ async fn perform_profile_update(
|
||||
let result = clash_verge_task::retry::try_strategies(
|
||||
Strategy::Configured,
|
||||
[Strategy::Clash, Strategy::System],
|
||||
|strategy| {
|
||||
let mut options = merged_opt.clone();
|
||||
async move {
|
||||
let (name, success, failure) = match strategy {
|
||||
Strategy::Configured => (
|
||||
"configured",
|
||||
"[订阅更新] 更新订阅配置成功",
|
||||
"[订阅更新] 直接更新失败: {},尝试使用Clash代理更新",
|
||||
),
|
||||
Strategy::Clash => (
|
||||
"clash",
|
||||
"[订阅更新] 使用 Clash代理 更新订阅配置成功",
|
||||
"[订阅更新] Clash代理更新失败: {},尝试使用系统代理更新",
|
||||
),
|
||||
Strategy::System => (
|
||||
"system",
|
||||
"[订阅更新] 使用 系统代理 更新订阅配置成功",
|
||||
"[订阅更新] 系统代理更新失败: {},所有重试均已失败",
|
||||
),
|
||||
};
|
||||
match strategy {
|
||||
Strategy::Configured => {}
|
||||
Strategy::Clash | Strategy::System => {
|
||||
let option = options.get_or_insert_with(PrfOption::default);
|
||||
option.self_proxy = Some(matches!(strategy, Strategy::Clash));
|
||||
option.with_proxy = Some(matches!(strategy, Strategy::System));
|
||||
}
|
||||
}
|
||||
tracing::Span::current().record("strategy", name);
|
||||
let started = std::time::Instant::now();
|
||||
let result = download_subscription(url, options.as_ref()).await;
|
||||
let elapsed_field = match strategy {
|
||||
Strategy::Configured => "configured_elapsed",
|
||||
Strategy::Clash => "clash_elapsed",
|
||||
Strategy::System => "system_elapsed",
|
||||
};
|
||||
tracing::Span::current().record(elapsed_field, tracing::field::debug(started.elapsed()));
|
||||
match &result {
|
||||
Ok(_) => logging!(info, Type::Config, "{success}"),
|
||||
Err(error) => logging!(
|
||||
warn,
|
||||
Type::Config,
|
||||
"{}",
|
||||
failure.replace("{}", &mask_err(&format!("{error:#}")))
|
||||
),
|
||||
}
|
||||
result
|
||||
|strategy| async move {
|
||||
let (name, proxy, elapsed_field, success, failure) = match strategy {
|
||||
Strategy::Configured => (
|
||||
"configured",
|
||||
configured_proxy,
|
||||
"configured_elapsed",
|
||||
"[订阅更新] 更新订阅配置成功",
|
||||
"[订阅更新] 直接更新失败: {},尝试使用Clash代理更新",
|
||||
),
|
||||
Strategy::Clash => (
|
||||
"clash",
|
||||
ProxyType::Localhost,
|
||||
"clash_elapsed",
|
||||
"[订阅更新] 使用 Clash代理 更新订阅配置成功",
|
||||
"[订阅更新] Clash代理更新失败: {},尝试使用系统代理更新",
|
||||
),
|
||||
Strategy::System => (
|
||||
"system",
|
||||
ProxyType::System,
|
||||
"system_elapsed",
|
||||
"[订阅更新] 使用 系统代理 更新订阅配置成功",
|
||||
"[订阅更新] 系统代理更新失败: {},所有重试均已失败",
|
||||
),
|
||||
};
|
||||
tracing::Span::current().record("strategy", name);
|
||||
let started = std::time::Instant::now();
|
||||
let result = download_subscription(url, options, proxy).await;
|
||||
tracing::Span::current().record(elapsed_field, tracing::field::debug(started.elapsed()));
|
||||
match &result {
|
||||
Ok(_) => logging!(info, Type::Config, "{success}"),
|
||||
Err(error) => logging!(
|
||||
warn,
|
||||
Type::Config,
|
||||
"{}",
|
||||
failure.replace("{}", &mask_err(&format!("{error:#}")))
|
||||
),
|
||||
}
|
||||
result
|
||||
},
|
||||
)
|
||||
.await;
|
||||
@@ -132,7 +130,7 @@ async fn perform_profile_update(
|
||||
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?;
|
||||
let refresh = prepare_remote_refresh(downloaded, options).await?;
|
||||
save_remote_refresh(uid, url, refresh).await?;
|
||||
if !matches!(strategy, Strategy::Configured) {
|
||||
handle::Handle::notice(NoticeStatus::UpdateWithClashProxy, profile_name.as_str());
|
||||
@@ -154,11 +152,11 @@ async fn perform_profile_update(
|
||||
|
||||
#[tracing::instrument(skip_all, level = "info", fields(uid = %uid, manual = is_manual_trigger))]
|
||||
pub async fn update_profile(uid: &str, option: Option<&PrfOption>, is_manual_trigger: bool) -> Result<()> {
|
||||
let url_opt = should_update_profile(uid, is_manual_trigger).await?;
|
||||
let subscription = should_update_profile(uid, is_manual_trigger).await?;
|
||||
|
||||
let profile_persisted = match url_opt {
|
||||
Some((url, opt)) => {
|
||||
perform_profile_update(uid, &url, opt.as_ref(), option, is_manual_trigger).await?;
|
||||
let profile_persisted = match subscription {
|
||||
Some((url, profile_options)) => {
|
||||
perform_profile_update(uid, &url, profile_options.as_ref(), option, is_manual_trigger).await?;
|
||||
true
|
||||
}
|
||||
None => false,
|
||||
|
||||
Reference in New Issue
Block a user