refactor(subscription): pass the proxy directly to each download

This commit is contained in:
Tunglies
2026-12-03 19:38:23 +08:00
parent c742a9a17c
commit 83a520ea19
3 changed files with 72 additions and 77 deletions
+1 -1
View File
@@ -13,7 +13,7 @@ mod verge;
pub(crate) use self::config::Config;
pub(crate) use self::prfitem::{
build_remote_profile, download_subscription, prepare_remote_refresh, read_profile_item_file,
build_remote_profile, download_subscription, prepare_remote_refresh, read_profile_item_file, subscription_proxy,
};
pub use self::{clash::*, encrypt::*, mixed_port::*, profiles::*, verge::*};
pub use clash_verge_profile::{PrfExtra, PrfItem, PrfOption, PrfSelected};
+16 -19
View File
@@ -140,7 +140,7 @@ pub(crate) async fn build_remote_profile(
desc: Option<&String>,
option: Option<&PrfOption>,
) -> Result<PrfItem> {
let mut downloaded = download_subscription(url, option).await?;
let mut downloaded = download_subscription(url, option, subscription_proxy(option)).await?;
let uid: String = help::get_uid("R").into();
let file = format!("{uid}.yaml").into();
let name = name
@@ -172,31 +172,28 @@ pub(crate) async fn build_remote_profile(
})
}
pub(crate) 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 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 user_agent = option.and_then(|o| o.user_agent.clone());
let timeout = option.and_then(|o| o.timeout_seconds).unwrap_or(20);
let proxy_type = if self_proxy {
pub(crate) fn subscription_proxy(options: Option<&PrfOption>) -> ProxyType {
if options.and_then(|options| options.self_proxy) == Some(true) {
ProxyType::Localhost
} else if with_proxy {
} else if options.and_then(|options| options.with_proxy) == Some(true) {
ProxyType::System
} else {
ProxyType::None
};
}
}
pub(crate) async fn download_subscription(
url: &str,
option: Option<&PrfOption>,
proxy: ProxyType,
) -> Result<DownloadedSubscription> {
let accept_invalid_certs = option.is_some_and(|o| o.danger_accept_invalid_certs.unwrap_or(false));
let user_agent = option.and_then(|o| o.user_agent.clone());
let timeout = option.and_then(|o| o.timeout_seconds).unwrap_or(20);
let url = fix_dirty_url(url)?;
let resp = match NetworkManager::new()
.get(
url.as_str(),
proxy_type,
Some(timeout),
user_agent,
accept_invalid_certs,
)
.get(url.as_str(), proxy, Some(timeout), user_agent, accept_invalid_certs)
.await
{
Ok(r) => r,
@@ -416,7 +413,7 @@ mod tests {
update_interval: None,
..option
};
let downloaded = download_subscription(&url, Some(&refresh_option)).await?;
let downloaded = download_subscription(&url, Some(&refresh_option), ProxyType::None).await?;
let refresh = prepare_remote_refresh(downloaded, Some(&refresh_option)).await?;
server.await??;
assert_eq!(item.itype.as_deref(), Some("remote"));
+55 -57
View File
@@ -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,