Skip to content

Commit

Permalink
feat(wry): support proxy in wry runtime
Browse files Browse the repository at this point in the history
wry has been supported http/socks5 proxy in
[tauri-apps#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 <river@vvl.me>
  • Loading branch information
time-river committed Dec 21, 2023
1 parent 2032228 commit a6bbe35
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 1 deletion.
7 changes: 7 additions & 0 deletions core/tauri-config-schema/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,13 @@
}
]
},
"proxy": {
"description": "The proxy URL used by the webview for all network requests.",
"type": [
"string",
"null"
]
},
"userAgent": {
"description": "The user agent for the webview",
"type": [
Expand Down
37 changes: 36 additions & 1 deletion core/tauri-runtime-wry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ use tauri_utils::TitleBarStyle;
use tauri_utils::{
config::WindowConfig, debug_eprintln, ProgressBarState, ProgressBarStatus, Theme,
};
use wry::{FileDropEvent as WryFileDropEvent, Url, WebContext, WebView, WebViewBuilder};
use wry::{
FileDropEvent as WryFileDropEvent, ProxyConfig, ProxyEndpoint, Url, WebContext, WebView,
WebViewBuilder,
};

pub use tao;
pub use tao::window::{Window, WindowBuilder as TaoWindowBuilder, WindowId};
Expand Down Expand Up @@ -2702,6 +2705,32 @@ pub fn center_window(window: &Window, window_size: TaoPhysicalSize<u32>) -> Resu
}
}

fn parse_proxy(url: &str) -> Result<ProxyConfig> {
let proxy = Url::parse(url).unwrap();

if proxy.scheme() == "http" && proxy.path() == "" {
let host = proxy.host().unwrap();
let port = proxy.port().unwrap();
let config = ProxyConfig::Http(ProxyEndpoint {
host: host.to_string(),
port: port.to_string(),
});

Ok(config)
} else if proxy.scheme() == "socks5" && proxy.path() == "" {
let host = proxy.host().unwrap();
let port = proxy.port().unwrap();
let config = ProxyConfig::Socks5(ProxyEndpoint {
host: host.to_string(),
port: port.to_string(),
});

Ok(config)
} else {
Err(Error::InvalidProxyUrl)
}
}

fn create_webview<T: UserEvent, F: Fn(RawWindow) + Send + 'static>(
window_id: WebviewId,
event_loop: &EventLoopWindowTarget<Message<T>>,
Expand Down Expand Up @@ -2860,6 +2889,12 @@ fn create_webview<T: UserEvent, F: Fn(RawWindow) + Send + 'static>(
webview_builder = webview_builder.with_user_agent(&user_agent);
}

if let Some(proxy) = webview_attributes.proxy {
let config = parse_proxy(&proxy).unwrap();

webview_builder = webview_builder.with_proxy_config(config);
}

#[cfg(windows)]
{
if let Some(additional_browser_args) = webview_attributes.additional_browser_args {
Expand Down
2 changes: 2 additions & 0 deletions core/tauri-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ pub enum Error {
Infallible(#[from] std::convert::Infallible),
#[error("the event loop has been closed")]
EventLoopClosed,
#[error("Invalid proxy url")]
InvalidProxyUrl,
}

/// Result type.
Expand Down
12 changes: 12 additions & 0 deletions core/tauri-runtime/src/webview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use std::{fmt, path::PathBuf};
#[derive(Debug, Clone)]
pub struct WebviewAttributes {
pub url: WindowUrl,
pub proxy: Option<String>,
pub user_agent: Option<String>,
pub initialization_scripts: Vec<String>,
pub data_directory: Option<PathBuf>,
Expand Down Expand Up @@ -50,6 +51,9 @@ impl From<&WindowConfig> for WebviewAttributes {
if let Some(effects) = &config.window_effects {
builder = builder.window_effects(effects.clone());
}
if let Some(proxy) = &config.proxy {
builder = builder.proxy_config(proxy);
}
builder
}
}
Expand All @@ -67,6 +71,7 @@ impl WebviewAttributes {
additional_browser_args: None,
window_effects: None,
incognito: false,
proxy: None,
}
}

Expand Down Expand Up @@ -135,6 +140,13 @@ impl WebviewAttributes {
self.incognito = incognito;
self
}

/// Enable proxy for the WebView
#[must_use]
pub fn proxy_config(mut self, proxy: &str) -> Self {
self.proxy = Some(proxy.to_string());
self
}
}

/// Do **NOT** implement this trait except for use in a custom [`Runtime`](crate::Runtime).
Expand Down
10 changes: 10 additions & 0 deletions core/tauri-utils/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,9 @@ pub struct WindowConfig {
/// The window webview URL.
#[serde(default)]
pub url: WindowUrl,
/// The proxy URL used by the webview for all network requests.
#[serde(default = "default_proxy")]
pub proxy: Option<String>,
/// The user agent for the webview
#[serde(alias = "user-agent")]
pub user_agent: Option<String>,
Expand Down Expand Up @@ -1160,6 +1163,7 @@ impl Default for WindowConfig {
Self {
label: default_window_label(),
url: WindowUrl::default(),
proxy: default_proxy(),
user_agent: None,
file_drop_enabled: true,
center: false,
Expand Down Expand Up @@ -1200,6 +1204,10 @@ impl Default for WindowConfig {
}
}

fn default_proxy() -> Option<String> {
None
}

fn default_window_label() -> String {
"main".to_string()
}
Expand Down Expand Up @@ -2370,6 +2378,7 @@ mod build {
let minimizable = self.minimizable;
let closable = self.closable;
let title = str_lit(&self.title);
let proxy = opt_str_lit(self.proxy.as_ref());
let fullscreen = self.fullscreen;
let focus = self.focus;
let transparent = self.transparent;
Expand Down Expand Up @@ -2412,6 +2421,7 @@ mod build {
minimizable,
closable,
title,
proxy,
fullscreen,
focus,
transparent,
Expand Down
7 changes: 7 additions & 0 deletions core/tauri/src/window/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,13 @@ impl<'a, R: Runtime> WindowBuilder<'a, R> {
self.webview_attributes.incognito = incognito;
self
}

/// Enable proxy for the WebView
#[must_use]
pub fn proxy_config(mut self, proxy: &str) -> Self {
self.webview_attributes.proxy = Some(proxy.to_string());
self
}
}

/// Key for a JS event listener.
Expand Down
2 changes: 2 additions & 0 deletions tooling/api/src/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1972,6 +1972,8 @@ interface WindowOptions {
* - local file path or route such as `/path/to/page.html` or `/users` is appended to the application URL (the devServer URL on development, or `tauri://localhost/` and `https://tauri.localhost/` on production).
*/
url?: string
/** The proxy URL. */
proxyUrl?: string
/** Show window in the center of the screen.. */
center?: boolean
/** The initial vertical position. Only applies if `y` is also set. */
Expand Down
7 changes: 7 additions & 0 deletions tooling/cli/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,13 @@
}
]
},
"proxy": {
"description": "The proxy URL used by the webview for all network requests.",
"type": [
"string",
"null"
]
},
"userAgent": {
"description": "The user agent for the webview",
"type": [
Expand Down

0 comments on commit a6bbe35

Please sign in to comment.