Skip to content

Commit

Permalink
Merge branch 'v2' into refactor/updater/resources-table
Browse files Browse the repository at this point in the history
  • Loading branch information
amrbashir authored Jan 18, 2024
2 parents e88239d + 8505a75 commit 103b2c1
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 7 deletions.
6 changes: 6 additions & 0 deletions .changes/updater-proxy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"updater": patch
"updater-js": patch
---

Add support for specifying proxy to use for checking and downloading updates.
4 changes: 4 additions & 0 deletions plugins/updater/guest-js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
6 changes: 6 additions & 0 deletions plugins/updater/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use serde::Serialize;
use tauri::{ipc::Channel, AppHandle, Manager, ResourceId, Runtime};

use std::time::Duration;
use url::Url;

#[derive(Debug, Serialize)]
#[serde(tag = "event", content = "data")]
Expand Down Expand Up @@ -39,6 +40,7 @@ pub(crate) async fn check<R: Runtime>(
app: AppHandle<R>,
headers: Option<Vec<(String, String)>>,
timeout: Option<u64>,
proxy: Option<String>,
target: Option<String>,
) -> Result<Metadata> {
let mut builder = app.updater_builder();
Expand All @@ -50,6 +52,10 @@ pub(crate) async fn check<R: Runtime>(
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);
}
Expand Down
42 changes: 35 additions & 7 deletions plugins/updater/src/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -97,6 +97,7 @@ pub struct UpdaterBuilder {
endpoints: Option<Vec<Url>>,
headers: HeaderMap,
timeout: Option<Duration>,
proxy: Option<Url>,
installer_args: Option<Vec<String>>,
}

Expand All @@ -116,6 +117,7 @@ impl UpdaterBuilder {
endpoints: None,
headers: Default::default(),
timeout: None,
proxy: None,
installer_args: None,
}
}
Expand Down Expand Up @@ -163,6 +165,11 @@ impl UpdaterBuilder {
self
}

pub fn proxy(mut self, proxy: Url) -> Self {
self.proxy.replace(proxy);
self
}

pub fn installer_args<I, S>(mut self, args: I) -> Self
where
I: IntoIterator<Item = S>,
Expand Down Expand Up @@ -204,6 +211,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,
Expand All @@ -220,6 +228,7 @@ pub struct Updater {
current_version: Version,
version_comparator: Option<Box<dyn Fn(Version, RemoteRelease) -> bool + Send + Sync>>,
timeout: Option<Duration>,
proxy: Option<Url>,
endpoints: Vec<Url>,
#[allow(dead_code)]
installer_args: Vec<String>,
Expand Down Expand Up @@ -274,11 +283,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() {
Expand Down Expand Up @@ -329,6 +347,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 {
Expand Down Expand Up @@ -363,6 +382,8 @@ pub struct Update {
pub signature: String,
/// Request timeout
pub timeout: Option<Duration>,
/// Request proxy
pub proxy: Option<Url>,
/// Request headers
pub headers: HeaderMap,
}
Expand All @@ -389,13 +410,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!(
Expand Down

0 comments on commit 103b2c1

Please sign in to comment.