From 65661822580c31eb10a44be45842e259c598374c Mon Sep 17 00:00:00 2001 From: thep0y <51874567+thep0y@users.noreply.github.com> Date: Wed, 11 Sep 2024 10:36:12 +0800 Subject: [PATCH] feat(bundler): add `TAURI_BUNDLER_TOOLS_GITHUB_MIRROR`to specify a GitHub mirror (#10866) closes #7338 --- .changes/bundler-github-mirror-from-env.md | 6 ++++++ crates/tauri-bundler/Cargo.toml | 1 + .../tauri-bundler/src/bundle/windows/util.rs | 21 +++++++++++++++++-- crates/tauri-bundler/src/error.rs | 3 +++ crates/tauri-cli/ENVIRONMENT_VARIABLES.md | 1 + 5 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 .changes/bundler-github-mirror-from-env.md diff --git a/.changes/bundler-github-mirror-from-env.md b/.changes/bundler-github-mirror-from-env.md new file mode 100644 index 000000000000..143af34edb32 --- /dev/null +++ b/.changes/bundler-github-mirror-from-env.md @@ -0,0 +1,6 @@ +--- +"tauri-bundler": patch:feat +"tauri-cli": patch:feat +--- + +Add `TAURI_BUNDLER_TOOLS_GITHUB_MIRROR` environment variable to specify a GitHub mirror to download files and tools used by tauri bundler. This is designed for areas like Mainland China where GitHub access can be unreliable. diff --git a/crates/tauri-bundler/Cargo.toml b/crates/tauri-bundler/Cargo.toml index 25883b9c3b2c..1b3e33235d38 100644 --- a/crates/tauri-bundler/Cargo.toml +++ b/crates/tauri-bundler/Cargo.toml @@ -42,6 +42,7 @@ sha1 = "0.10" sha2 = "0.10" zip = { version = "2.0", default-features = false, features = ["deflate"] } dunce = "1" +url = "2" [target."cfg(target_os = \"windows\")".dependencies] uuid = { version = "1", features = ["v4", "v5"] } diff --git a/crates/tauri-bundler/src/bundle/windows/util.rs b/crates/tauri-bundler/src/bundle/windows/util.rs index 80c3f64bae0a..a8b693d4e3d9 100644 --- a/crates/tauri-bundler/src/bundle/windows/util.rs +++ b/crates/tauri-bundler/src/bundle/windows/util.rs @@ -9,6 +9,8 @@ use std::{ }; use sha2::Digest; +use ureq::AgentBuilder; +use url::Url; use zip::ZipArchive; pub const WEBVIEW2_BOOTSTRAPPER_URL: &str = "https://go.microsoft.com/fwlink/p/?LinkId=2124703"; @@ -67,11 +69,26 @@ pub fn download_webview2_offline_installer(base_path: &Path, arch: &str) -> crat Ok(file_path) } +fn create_agent_and_url(url: &str) -> crate::Result<(ureq::Agent, String)> { + match std::env::var("TAURI_BUNDLER_TOOLS_GITHUB_MIRROR") { + Ok(cdn) if url.starts_with("https://github.com/") => { + let mut parsed_cdn = Url::parse(&cdn)?; + parsed_cdn.set_path(url); + Ok((AgentBuilder::new().build(), parsed_cdn.into())) + } + _ => Ok(( + AgentBuilder::new().try_proxy_from_env(true).build(), + url.to_owned(), + )), + } +} + pub fn download(url: &str) -> crate::Result> { log::info!(action = "Downloading"; "{}", url); - let agent = ureq::AgentBuilder::new().try_proxy_from_env(true).build(); - let response = agent.get(url).call().map_err(Box::new)?; + let (agent, final_url) = create_agent_and_url(url)?; + + let response = agent.get(&final_url).call().map_err(Box::new)?; let mut bytes = Vec::new(); response.into_reader().read_to_end(&mut bytes)?; Ok(bytes) diff --git a/crates/tauri-bundler/src/error.rs b/crates/tauri-bundler/src/error.rs index 35650a976a17..7dd6b32b0dcf 100644 --- a/crates/tauri-bundler/src/error.rs +++ b/crates/tauri-bundler/src/error.rs @@ -58,6 +58,9 @@ pub enum Error { #[cfg(windows)] #[error("`{0}`")] Glob(#[from] glob::GlobError), + /// Failed to parse the URL + #[error("`{0}`")] + UrlParse(#[from] url::ParseError), /// Failed to validate downloaded file hash. #[error("hash mismatch of downloaded file")] HashError, diff --git a/crates/tauri-cli/ENVIRONMENT_VARIABLES.md b/crates/tauri-cli/ENVIRONMENT_VARIABLES.md index 5ce3089eeecb..65ae17749836 100644 --- a/crates/tauri-cli/ENVIRONMENT_VARIABLES.md +++ b/crates/tauri-cli/ENVIRONMENT_VARIABLES.md @@ -15,6 +15,7 @@ These environment variables are inputs to the CLI which may have an equivalent C - `TAURI_CLI_NO_DEV_SERVER_WAIT` — Skip waiting for the frontend dev server to start before building the tauri application. - `TAURI_LINUX_AYATANA_APPINDICATOR` — Set this var to `true` or `1` to force usage of `libayatana-appindicator` for system tray on Linux. - `TAURI_BUNDLER_WIX_FIPS_COMPLIANT` — Specify the bundler's WiX `FipsCompliant` option. +- `TAURI_BUNDLER_TOOLS_GITHUB_MIRROR` - Specify a GitHub mirror to download files and tools used by tauri bundler. - `TAURI_SKIP_SIDECAR_SIGNATURE_CHECK` - Skip signing sidecars. - `TAURI_SIGNING_PRIVATE_KEY` — Private key used to sign your app bundles, can be either a string or a path to the file. - `TAURI_SIGNING_PRIVATE_KEY_PASSWORD` — The signing private key password, see `TAURI_SIGNING_PRIVATE_KEY`.