Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6639a57

Browse files
time-riveramrbashirlucasfernog
authored
feat(core): add support to setting a webview proxy, closes #4263 (#8441)
* feat(wry): support proxy in wry runtime wry has been supported http/socks5 proxy in [#1006](tauri-apps/wry#1006), which has been merged in [commit 3cc4d79](tauri-apps/wry@3cc4d79). This patch aims to support its feature. Signed-off-by: lin fu <[email protected]> * Apply suggestions from code review * Apply suggestions from code review * Update core/tauri-runtime-wry/src/lib.rs * Update core/tauri/src/window/mod.rs * add macos-proxy flag * add change file * delete file * update change file * use macos-14 runner to test core --------- Signed-off-by: lin fu <[email protected]> Co-authored-by: Amr Bashir <[email protected]> Co-authored-by: Lucas Nogueira <[email protected]> Co-authored-by: Lucas Nogueira <[email protected]>
1 parent a093682 commit 6639a57

14 files changed

Lines changed: 114 additions & 4 deletions

File tree

.changes/webview-proxy.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
'tauri': 'minor:feat'
3+
'tauri-utils': 'minor:feat'
4+
'tauri-runtime-wry': 'minor'
5+
'tauri-runtime': 'minor'
6+
---
7+
8+
Added the `WindowConfig::proxy_url` `WebviewBuilder::proxy_url() / WebviewWindowBuilder::proxy_url()` options when creating a webview.

.github/workflows/test-core.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ jobs:
4646
command: 'test'
4747
}
4848
- {
49-
target: x86_64-apple-darwin,
50-
os: macos-latest,
49+
target: aarch64-apple-darwin,
50+
os: macos-14,
5151
toolchain: '1.70.0',
5252
cross: false,
5353
command: 'test'

core/tauri-config-schema/schema.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,14 @@
574574
"string",
575575
"null"
576576
]
577+
},
578+
"proxyUrl": {
579+
"description": "The proxy URL for the WebView for all network requests.\n\nMust be either a `http://` or a `socks5://` URL.\n\n## Platform-specific\n\n- **macOS**: Requires the `macos-proxy` feature flag and only compiles for macOS 14+.",
580+
"type": [
581+
"string",
582+
"null"
583+
],
584+
"format": "uri"
577585
}
578586
},
579587
"additionalProperties": false

core/tauri-runtime-wry/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,4 @@ macos-private-api = [
5050
objc-exception = [ "wry/objc-exception" ]
5151
linux-protocol-body = [ "wry/linux-body", "webkit2gtk/v2_40" ]
5252
tracing = [ "dep:tracing", "wry/tracing" ]
53+
macos-proxy = [ "wry/mac-proxy" ]

core/tauri-runtime-wry/src/lib.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ use tauri_utils::TitleBarStyle;
6363
use tauri_utils::{
6464
config::WindowConfig, debug_eprintln, ProgressBarState, ProgressBarStatus, Theme,
6565
};
66-
use wry::{FileDropEvent as WryFileDropEvent, Url, WebContext, WebView, WebViewBuilder};
66+
use wry::{
67+
FileDropEvent as WryFileDropEvent, ProxyConfig, ProxyEndpoint, Url, WebContext, WebView,
68+
WebViewBuilder,
69+
};
6770

6871
pub use tao;
6972
pub use tao::window::{Window, WindowBuilder as TaoWindowBuilder, WindowId as TaoWindowId};
@@ -3111,6 +3114,23 @@ pub fn center_window(window: &Window, window_size: TaoPhysicalSize<u32>) -> Resu
31113114
}
31123115
}
31133116

3117+
fn parse_proxy_url(url: &Url) -> Result<ProxyConfig> {
3118+
let host = url.host().map(|h| h.to_string()).unwrap_or_default();
3119+
let port = url.port().map(|p| p.to_string()).unwrap_or_default();
3120+
3121+
if url.scheme() == "http" {
3122+
let config = ProxyConfig::Http(ProxyEndpoint { host, port });
3123+
3124+
Ok(config)
3125+
} else if url.scheme() == "socks5" {
3126+
let config = ProxyConfig::Socks5(ProxyEndpoint { host, port });
3127+
3128+
Ok(config)
3129+
} else {
3130+
Err(Error::InvalidProxyUrl)
3131+
}
3132+
}
3133+
31143134
fn create_window<T: UserEvent, F: Fn(RawWindow) + Send + 'static>(
31153135
window_id: WindowId,
31163136
webview_id: u32,
@@ -3405,6 +3425,12 @@ fn create_webview<T: UserEvent>(
34053425
webview_builder = webview_builder.with_user_agent(&user_agent);
34063426
}
34073427

3428+
if let Some(proxy_url) = webview_attributes.proxy_url {
3429+
let config = parse_proxy_url(&proxy_url)?;
3430+
3431+
webview_builder = webview_builder.with_proxy_config(config);
3432+
}
3433+
34083434
#[cfg(windows)]
34093435
{
34103436
if let Some(additional_browser_args) = webview_attributes.additional_browser_args {

core/tauri-runtime/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ pub enum Error {
122122
Infallible(#[from] std::convert::Infallible),
123123
#[error("the event loop has been closed")]
124124
EventLoopClosed,
125+
#[error("Invalid proxy url")]
126+
InvalidProxyUrl,
125127
#[error("window not found")]
126128
WindowNotFound,
127129
}

core/tauri-runtime/src/webview.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ pub struct WebviewAttributes {
211211
pub transparent: bool,
212212
pub bounds: Option<(Position, Size)>,
213213
pub auto_resize: bool,
214+
pub proxy_url: Option<Url>,
214215
}
215216

216217
impl From<&WindowConfig> for WebviewAttributes {
@@ -234,6 +235,9 @@ impl From<&WindowConfig> for WebviewAttributes {
234235
if let Some(effects) = &config.window_effects {
235236
builder = builder.window_effects(effects.clone());
236237
}
238+
if let Some(url) = &config.proxy_url {
239+
builder = builder.proxy_url(url.to_owned());
240+
}
237241
builder
238242
}
239243
}
@@ -255,6 +259,7 @@ impl WebviewAttributes {
255259
transparent: false,
256260
bounds: None,
257261
auto_resize: false,
262+
proxy_url: None,
258263
}
259264
}
260265

@@ -338,6 +343,13 @@ impl WebviewAttributes {
338343
self.auto_resize = true;
339344
self
340345
}
346+
347+
/// Enable proxy for the WebView
348+
#[must_use]
349+
pub fn proxy_url(mut self, url: Url) -> Self {
350+
self.proxy_url = Some(url);
351+
self
352+
}
341353
}
342354

343355
/// IPC handler.

core/tauri-utils/src/config.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,6 +1267,14 @@ pub struct WindowConfig {
12671267
/// - **Linux**: This makes the new window transient for parent, see <https://docs.gtk.org/gtk3/method.Window.set_transient_for.html>
12681268
/// - **macOS**: This adds the window as a child of parent, see <https://developer.apple.com/documentation/appkit/nswindow/1419152-addchildwindow?language=objc>
12691269
pub parent: Option<String>,
1270+
/// The proxy URL for the WebView for all network requests.
1271+
///
1272+
/// Must be either a `http://` or a `socks5://` URL.
1273+
///
1274+
/// ## Platform-specific
1275+
///
1276+
/// - **macOS**: Requires the `macos-proxy` feature flag and only compiles for macOS 14+.
1277+
pub proxy_url: Option<Url>,
12701278
}
12711279

12721280
impl Default for WindowConfig {
@@ -1311,6 +1319,7 @@ impl Default for WindowConfig {
13111319
window_effects: None,
13121320
incognito: false,
13131321
parent: None,
1322+
proxy_url: None,
13141323
}
13151324
}
13161325
}
@@ -2306,6 +2315,7 @@ mod build {
23062315
let minimizable = self.minimizable;
23072316
let closable = self.closable;
23082317
let title = str_lit(&self.title);
2318+
let proxy_url = opt_str_lit(self.proxy_url.as_ref());
23092319
let fullscreen = self.fullscreen;
23102320
let focus = self.focus;
23112321
let transparent = self.transparent;
@@ -2349,6 +2359,7 @@ mod build {
23492359
minimizable,
23502360
closable,
23512361
title,
2362+
proxy_url,
23522363
fullscreen,
23532364
focus,
23542365
transparent,

core/tauri/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ config-json5 = [ "tauri-macros/config-json5" ]
163163
config-toml = [ "tauri-macros/config-toml" ]
164164
icon-ico = [ "infer", "ico" ]
165165
icon-png = [ "infer", "png" ]
166+
macos-proxy = [ "tauri-runtime-wry/macos-proxy" ]
166167

167168
[[example]]
168169
name = "commands"

core/tauri/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
//! - **config-toml**: Adds support to TOML format for the configuration `Tauri.toml`.
3636
//! - **icon-ico**: Adds support to set `.ico` window icons. Enables [`Icon::File`] and [`Icon::Raw`] variants.
3737
//! - **icon-png**: Adds support to set `.png` window icons. Enables [`Icon::File`] and [`Icon::Raw`] variants.
38+
//! - **macos-proxy**: Adds support for [`WebviewBuilder::proxy_url`] on macOS. Requires macOS 14+.
3839
//!
3940
//! ## Cargo allowlist features
4041
//!

0 commit comments

Comments
 (0)