Skip to content

Commit 4e2e775

Browse files
authored
feat(updater) support proxy (#891)
* add proxy support Signed-off-by: San Zhang <sanzhang@mail.com> * 1. change string to url for proxy type 2. add proxy option in js api Signed-off-by: San Zhang <sanzhang@mail.com> * fix fmt issue Signed-off-by: San Zhang <sanzhang@mail.com> * Update plugins/updater/guest-js/index.ts --------- Signed-off-by: San Zhang <sanzhang@mail.com>
1 parent 8a3db79 commit 4e2e775

3 files changed

Lines changed: 45 additions & 7 deletions

File tree

plugins/updater/guest-js/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ interface CheckOptions {
1414
* Timeout in seconds
1515
*/
1616
timeout?: number;
17+
/**
18+
* A proxy url to be used when checking and downloading updates.
19+
*/
20+
proxy?: string;
1721
/**
1822
* Target identifier for the running application. This is sent to the backend.
1923
*/

plugins/updater/src/commands.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use std::{
1111
sync::atomic::{AtomicBool, Ordering},
1212
time::Duration,
1313
};
14+
use url::Url;
1415

1516
#[derive(Debug, Serialize)]
1617
#[serde(tag = "event", content = "data")]
@@ -42,6 +43,7 @@ pub(crate) async fn check<R: Runtime>(
4243
pending: State<'_, PendingUpdate>,
4344
headers: Option<Vec<(String, String)>>,
4445
timeout: Option<u64>,
46+
proxy: Option<String>,
4547
target: Option<String>,
4648
) -> Result<Metadata> {
4749
let mut builder = app.updater_builder();
@@ -53,6 +55,10 @@ pub(crate) async fn check<R: Runtime>(
5355
if let Some(timeout) = timeout {
5456
builder = builder.timeout(Duration::from_secs(timeout));
5557
}
58+
if let Some(ref proxy) = proxy {
59+
let url = Url::parse(proxy.as_str())?;
60+
builder = builder.proxy(url);
61+
}
5662
if let Some(target) = target {
5763
builder = builder.target(target);
5864
}

plugins/updater/src/updater.rs

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use http::HeaderName;
1616
use minisign_verify::{PublicKey, Signature};
1717
use reqwest::{
1818
header::{HeaderMap, HeaderValue},
19-
Client, StatusCode,
19+
ClientBuilder, StatusCode,
2020
};
2121
use semver::Version;
2222
use serde::{de::Error as DeError, Deserialize, Deserializer, Serialize};
@@ -94,6 +94,7 @@ pub struct UpdaterBuilder {
9494
endpoints: Option<Vec<Url>>,
9595
headers: HeaderMap,
9696
timeout: Option<Duration>,
97+
proxy: Option<Url>,
9798
installer_args: Option<Vec<String>>,
9899
}
99100

@@ -113,6 +114,7 @@ impl UpdaterBuilder {
113114
endpoints: None,
114115
headers: Default::default(),
115116
timeout: None,
117+
proxy: None,
116118
installer_args: None,
117119
}
118120
}
@@ -160,6 +162,11 @@ impl UpdaterBuilder {
160162
self
161163
}
162164

165+
pub fn proxy(mut self, proxy: Url) -> Self {
166+
self.proxy.replace(proxy);
167+
self
168+
}
169+
163170
pub fn installer_args<I, S>(mut self, args: I) -> Self
164171
where
165172
I: IntoIterator<Item = S>,
@@ -201,6 +208,7 @@ impl UpdaterBuilder {
201208
current_version: self.current_version,
202209
version_comparator: self.version_comparator,
203210
timeout: self.timeout,
211+
proxy: self.proxy,
204212
endpoints,
205213
installer_args: self.installer_args.unwrap_or(self.config.installer_args),
206214
arch,
@@ -217,6 +225,7 @@ pub struct Updater {
217225
current_version: Version,
218226
version_comparator: Option<Box<dyn Fn(Version, RemoteRelease) -> bool + Send + Sync>>,
219227
timeout: Option<Duration>,
228+
proxy: Option<Url>,
220229
endpoints: Vec<Url>,
221230
#[allow(dead_code)]
222231
installer_args: Vec<String>,
@@ -271,11 +280,20 @@ impl Updater {
271280
.replace("{{arch}}", self.arch)
272281
.parse()?;
273282

274-
let mut request = Client::new().get(url).headers(headers.clone());
283+
let mut request = ClientBuilder::new();
275284
if let Some(timeout) = self.timeout {
276285
request = request.timeout(timeout);
277286
}
278-
let response = request.send().await;
287+
if let Some(ref proxy) = self.proxy {
288+
let proxy = reqwest::Proxy::all(proxy.as_str())?;
289+
request = request.proxy(proxy);
290+
}
291+
let response = request
292+
.build()?
293+
.get(url)
294+
.headers(headers.clone())
295+
.send()
296+
.await;
279297

280298
if let Ok(res) = response {
281299
if res.status().is_success() {
@@ -326,6 +344,7 @@ impl Updater {
326344
body: release.notes.clone(),
327345
signature: release.signature(&self.json_target)?.to_owned(),
328346
timeout: self.timeout,
347+
proxy: self.proxy.clone(),
329348
headers: self.headers.clone(),
330349
})
331350
} else {
@@ -360,6 +379,8 @@ pub struct Update {
360379
pub signature: String,
361380
/// Request timeout
362381
pub timeout: Option<Duration>,
382+
/// Request proxy
383+
pub proxy: Option<Url>,
363384
/// Request headers
364385
pub headers: HeaderMap,
365386
}
@@ -384,13 +405,20 @@ impl Update {
384405
HeaderValue::from_str("tauri-updater").unwrap(),
385406
);
386407

387-
let mut request = Client::new()
388-
.get(self.download_url.clone())
389-
.headers(headers);
408+
let mut request = ClientBuilder::new();
390409
if let Some(timeout) = self.timeout {
391410
request = request.timeout(timeout);
392411
}
393-
let response = request.send().await?;
412+
if let Some(ref proxy) = self.proxy {
413+
let proxy = reqwest::Proxy::all(proxy.as_str())?;
414+
request = request.proxy(proxy);
415+
}
416+
let response = request
417+
.build()?
418+
.get(self.download_url.clone())
419+
.headers(headers)
420+
.send()
421+
.await?;
394422

395423
if !response.status().is_success() {
396424
return Err(Error::Network(format!(

0 commit comments

Comments
 (0)