From 4e2e77580c14fc6ce1ff5bc423a4ee573299c07b Mon Sep 17 00:00:00 2001 From: Zhang San <52621825+zhangsan946@users.noreply.github.com> Date: Wed, 17 Jan 2024 22:47:26 +0800 Subject: [PATCH 1/2] feat(updater) support proxy (#891) * add proxy support Signed-off-by: San Zhang * 1. change string to url for proxy type 2. add proxy option in js api Signed-off-by: San Zhang * fix fmt issue Signed-off-by: San Zhang * Update plugins/updater/guest-js/index.ts --------- Signed-off-by: San Zhang --- plugins/updater/guest-js/index.ts | 4 +++ plugins/updater/src/commands.rs | 6 +++++ plugins/updater/src/updater.rs | 42 +++++++++++++++++++++++++------ 3 files changed, 45 insertions(+), 7 deletions(-) diff --git a/plugins/updater/guest-js/index.ts b/plugins/updater/guest-js/index.ts index 5d5cd7b877..411c989fee 100644 --- a/plugins/updater/guest-js/index.ts +++ b/plugins/updater/guest-js/index.ts @@ -14,6 +14,10 @@ interface CheckOptions { * Timeout in seconds */ timeout?: number; + /** + * A proxy url to be used when checking and downloading updates. + */ + proxy?: string; /** * Target identifier for the running application. This is sent to the backend. */ diff --git a/plugins/updater/src/commands.rs b/plugins/updater/src/commands.rs index 4e8fcdc818..24abc3dec8 100644 --- a/plugins/updater/src/commands.rs +++ b/plugins/updater/src/commands.rs @@ -11,6 +11,7 @@ use std::{ sync::atomic::{AtomicBool, Ordering}, time::Duration, }; +use url::Url; #[derive(Debug, Serialize)] #[serde(tag = "event", content = "data")] @@ -42,6 +43,7 @@ pub(crate) async fn check( pending: State<'_, PendingUpdate>, headers: Option>, timeout: Option, + proxy: Option, target: Option, ) -> Result { let mut builder = app.updater_builder(); @@ -53,6 +55,10 @@ pub(crate) async fn check( if let Some(timeout) = timeout { builder = builder.timeout(Duration::from_secs(timeout)); } + if let Some(ref proxy) = proxy { + let url = Url::parse(proxy.as_str())?; + builder = builder.proxy(url); + } if let Some(target) = target { builder = builder.target(target); } diff --git a/plugins/updater/src/updater.rs b/plugins/updater/src/updater.rs index 75cd38a809..5c804504cf 100644 --- a/plugins/updater/src/updater.rs +++ b/plugins/updater/src/updater.rs @@ -16,7 +16,7 @@ use http::HeaderName; use minisign_verify::{PublicKey, Signature}; use reqwest::{ header::{HeaderMap, HeaderValue}, - Client, StatusCode, + ClientBuilder, StatusCode, }; use semver::Version; use serde::{de::Error as DeError, Deserialize, Deserializer, Serialize}; @@ -94,6 +94,7 @@ pub struct UpdaterBuilder { endpoints: Option>, headers: HeaderMap, timeout: Option, + proxy: Option, installer_args: Option>, } @@ -113,6 +114,7 @@ impl UpdaterBuilder { endpoints: None, headers: Default::default(), timeout: None, + proxy: None, installer_args: None, } } @@ -160,6 +162,11 @@ impl UpdaterBuilder { self } + pub fn proxy(mut self, proxy: Url) -> Self { + self.proxy.replace(proxy); + self + } + pub fn installer_args(mut self, args: I) -> Self where I: IntoIterator, @@ -201,6 +208,7 @@ impl UpdaterBuilder { current_version: self.current_version, version_comparator: self.version_comparator, timeout: self.timeout, + proxy: self.proxy, endpoints, installer_args: self.installer_args.unwrap_or(self.config.installer_args), arch, @@ -217,6 +225,7 @@ pub struct Updater { current_version: Version, version_comparator: Option bool + Send + Sync>>, timeout: Option, + proxy: Option, endpoints: Vec, #[allow(dead_code)] installer_args: Vec, @@ -271,11 +280,20 @@ impl Updater { .replace("{{arch}}", self.arch) .parse()?; - let mut request = Client::new().get(url).headers(headers.clone()); + let mut request = ClientBuilder::new(); if let Some(timeout) = self.timeout { request = request.timeout(timeout); } - let response = request.send().await; + if let Some(ref proxy) = self.proxy { + let proxy = reqwest::Proxy::all(proxy.as_str())?; + request = request.proxy(proxy); + } + let response = request + .build()? + .get(url) + .headers(headers.clone()) + .send() + .await; if let Ok(res) = response { if res.status().is_success() { @@ -326,6 +344,7 @@ impl Updater { body: release.notes.clone(), signature: release.signature(&self.json_target)?.to_owned(), timeout: self.timeout, + proxy: self.proxy.clone(), headers: self.headers.clone(), }) } else { @@ -360,6 +379,8 @@ pub struct Update { pub signature: String, /// Request timeout pub timeout: Option, + /// Request proxy + pub proxy: Option, /// Request headers pub headers: HeaderMap, } @@ -384,13 +405,20 @@ impl Update { HeaderValue::from_str("tauri-updater").unwrap(), ); - let mut request = Client::new() - .get(self.download_url.clone()) - .headers(headers); + let mut request = ClientBuilder::new(); if let Some(timeout) = self.timeout { request = request.timeout(timeout); } - let response = request.send().await?; + if let Some(ref proxy) = self.proxy { + let proxy = reqwest::Proxy::all(proxy.as_str())?; + request = request.proxy(proxy); + } + let response = request + .build()? + .get(self.download_url.clone()) + .headers(headers) + .send() + .await?; if !response.status().is_success() { return Err(Error::Network(format!( From 8505a756b569d88757ec58e452bfe4814d8107bf Mon Sep 17 00:00:00 2001 From: Amr Bashir Date: Wed, 17 Jan 2024 17:51:20 +0200 Subject: [PATCH 2/2] chore: add change file for updater proxy change (#907) --- .changes/updater-proxy.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changes/updater-proxy.md diff --git a/.changes/updater-proxy.md b/.changes/updater-proxy.md new file mode 100644 index 0000000000..882963c355 --- /dev/null +++ b/.changes/updater-proxy.md @@ -0,0 +1,6 @@ +--- +"updater": patch +"updater-js": patch +--- + +Add support for specifying proxy to use for checking and downloading updates.