feat: add R2 CDN race_download function and remove update.hwdns.net proxy

This commit is contained in:
t8y2
2026-05-14 16:45:55 +08:00
parent 919c3d58c1
commit 6994b4549e
+53 -1
View File
@@ -22,7 +22,9 @@ pub mod transfer;
pub mod types;
pub mod update;
pub const GITHUB_PROXIES: &[&str] = &["https://update.hwdns.net/", "https://gh-proxy.org/", ""];
pub const R2_CDN_BASE: &str = "https://dl.dbxio.com/";
pub const GITHUB_PROXIES: &[&str] = &["https://gh-proxy.org/", ""];
pub async fn race_github_proxies(
client: &reqwest::Client,
@@ -57,3 +59,53 @@ pub async fn race_github_proxies(
Err(last_err) => Err(last_err),
}
}
pub async fn race_download(
client: &reqwest::Client,
github_url: &str,
r2_path: &str,
user_agent: &str,
) -> Result<reqwest::Response, String> {
use futures::future::select_ok;
use std::pin::Pin;
let mut futs: Vec<Pin<Box<dyn std::future::Future<Output = Result<reqwest::Response, String>> + Send>>> =
Vec::with_capacity(GITHUB_PROXIES.len() + 1);
// R2 CDN source
{
let url = format!("{R2_CDN_BASE}{r2_path}");
let client = client.clone();
let ua = user_agent.to_string();
futs.push(Box::pin(async move {
client
.get(&url)
.header(reqwest::header::USER_AGENT, ua)
.send()
.await
.and_then(|r| r.error_for_status())
.map_err(|e| format!("{e}"))
}));
}
// GitHub proxies + direct
for proxy in GITHUB_PROXIES {
let url = format!("{proxy}{github_url}");
let client = client.clone();
let ua = user_agent.to_string();
futs.push(Box::pin(async move {
client
.get(&url)
.header(reqwest::header::USER_AGENT, ua)
.send()
.await
.and_then(|r| r.error_for_status())
.map_err(|e| format!("{e}"))
}) as Pin<Box<dyn std::future::Future<Output = Result<reqwest::Response, String>> + Send>>);
}
match select_ok(futs).await {
Ok((resp, _)) => Ok(resp),
Err(last_err) => Err(last_err),
}
}