Skip to content

Commit e87c453

Browse files
committed
Add proxy support in Rust lib and WryWebViewPanel
1 parent 9b0b2a4 commit e87c453

3 files changed

Lines changed: 67 additions & 13 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package io.github.kdroidfilter.webview.wry
2+
3+
sealed class ProxyConfig {
4+
abstract val host: String
5+
abstract val port: Int
6+
7+
data class Http(
8+
override val host: String,
9+
override val port: Int
10+
) : ProxyConfig() {
11+
override fun toProxy(): Proxy.Http = Proxy.Http(Address(host, port.toUShort()))
12+
}
13+
14+
data class Socks5(
15+
override val host: String,
16+
override val port: Int
17+
) : ProxyConfig() {
18+
override fun toProxy(): Proxy.Socks5 = Proxy.Socks5(Address(host, port.toUShort()))
19+
}
20+
21+
internal abstract fun toProxy(): Proxy
22+
}

wrywebview/src/main/kotlin/io/github/kdroidfilter/webview/wry/WryWebViewPanel.kt

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ import javax.swing.JPanel
99
import javax.swing.SwingUtilities
1010
import javax.swing.Timer
1111
import kotlin.concurrent.thread
12-
import kotlin.properties.Delegates
1312

1413

1514
class WryWebViewPanel(
1615
initialUrl: String,
1716
customUserAgent: String? = null,
17+
proxyConfig: ProxyConfig? = null,
1818
private val bridgeLogger: (String) -> Unit = { System.err.println(it) }
1919
) : JPanel() {
2020
private val host = SkikoInterop.createHost()
@@ -23,6 +23,7 @@ class WryWebViewPanel(
2323
private var parentIsWindow: Boolean = false
2424
private var pendingUrl: String = initialUrl
2525
private val customUserAgent: String? = customUserAgent?.trim()?.takeIf { it.isNotEmpty() }
26+
private val proxy: Proxy? = proxyConfig?.toProxy()
2627
private var pendingUrlWithHeaders: String? = null
2728
private var pendingHeaders: Map<String, String> = emptyMap()
2829
private var pendingHtml: String? = null
@@ -348,15 +349,16 @@ class WryWebViewPanel(
348349
return try {
349350
webviewId =
350351
if (userAgent == null) {
351-
NativeBindings.createWebview(handleSnapshot, width, height, initialUrl, handler)
352+
NativeBindings.createWebview(handleSnapshot, width, height, initialUrl, handler, proxy)
352353
} else {
353354
NativeBindings.createWebviewWithUserAgent(
354355
handleSnapshot,
355356
width,
356357
height,
357358
initialUrl,
358359
userAgent,
359-
handler
360+
handler,
361+
proxy
360362
)
361363
}
362364
updateBounds()
@@ -394,15 +396,16 @@ class WryWebViewPanel(
394396
thread(name = "wry-webview-create", isDaemon = true) {
395397
val createdId = try {
396398
if (userAgent == null) {
397-
NativeBindings.createWebview(handleSnapshot, width, height, initialUrl, handler)
399+
NativeBindings.createWebview(handleSnapshot, width, height, initialUrl, handler, proxy)
398400
} else {
399401
NativeBindings.createWebviewWithUserAgent(
400402
handleSnapshot,
401403
width,
402404
height,
403405
initialUrl,
404406
userAgent,
405-
handler
407+
handler,
408+
proxy
406409
)
407410
}
408411
} catch (e: RuntimeException) {
@@ -702,8 +705,8 @@ class WryWebViewPanel(
702705

703706
private object NativeBindings {
704707

705-
fun createWebview(parentHandle: ULong, width: Int, height: Int, url: String, handler: NavigationHandler): ULong {
706-
return io.github.kdroidfilter.webview.wry.createWebview(parentHandle, width, height, url, handler)
708+
fun createWebview(parentHandle: ULong, width: Int, height: Int, url: String, handler: NavigationHandler, proxy: Proxy?): ULong {
709+
return io.github.kdroidfilter.webview.wry.createWebview(parentHandle, width, height, url, handler, proxy)
707710
}
708711

709712
fun createWebviewWithUserAgent(
@@ -713,6 +716,7 @@ private object NativeBindings {
713716
url: String,
714717
userAgent: String,
715718
handler: NavigationHandler,
719+
proxy: Proxy?
716720
): ULong {
717721
return io.github.kdroidfilter.webview.wry.createWebviewWithUserAgent(
718722
parentHandle,
@@ -721,6 +725,7 @@ private object NativeBindings {
721725
url,
722726
userAgent,
723727
handler,
728+
proxy
724729
)
725730
}
726731

wrywebview/src/main/rust/lib.rs

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ use wry::cookie::time::OffsetDateTime;
1616
use wry::cookie::{Cookie, Expiration, SameSite};
1717
use wry::http::header::HeaderName;
1818
use wry::http::{HeaderMap, HeaderValue};
19-
use wry::WebViewBuilder;
20-
19+
use wry::{ProxyConfig, ProxyEndpoint, WebViewBuilder};
20+
use crate::Proxy::Http;
21+
use crate::Proxy::Socks5;
2122
pub use error::WebViewError;
2223

2324
use handle::{make_bounds, raw_window_handle_from, RawWindow};
@@ -68,6 +69,18 @@ pub struct WebViewCookie {
6869
pub is_http_only: Option<bool>,
6970
}
7071

72+
#[derive(uniffi::Enum)]
73+
pub enum Proxy {
74+
Http(Address),
75+
Socks5(Address),
76+
}
77+
78+
#[derive(Debug, Clone, uniffi::Record)]
79+
pub struct Address {
80+
pub host: String,
81+
pub port: u16
82+
}
83+
7184
fn header_map_from(headers: Vec<HttpHeader>) -> Result<HeaderMap, WebViewError> {
7285
let mut map = HeaderMap::new();
7386
for header in headers {
@@ -146,6 +159,13 @@ fn cookie_from_record(cookie: WebViewCookie) -> Result<Cookie<'static>, WebViewE
146159
Ok(builder.build())
147160
}
148161

162+
fn proxy_from_record(proxy: Proxy) -> ProxyConfig {
163+
match proxy {
164+
Http(addr) => ProxyConfig::Http(ProxyEndpoint { host: addr.host, port: addr.port.to_string() }),
165+
Socks5(addr) => ProxyConfig::Socks5(ProxyEndpoint { host: (addr.host), port: (addr.port.to_string()) })
166+
}
167+
}
168+
149169
use std::sync::atomic::AtomicBool;
150170

151171
static LOG_ENABLED: AtomicBool = AtomicBool::new(false);
@@ -216,6 +236,7 @@ fn create_webview_inner(
216236
url: String,
217237
user_agent: Option<String>,
218238
nav_handler: Option<Box<dyn NavigationHandler>>,
239+
proxy_config: Option<ProxyConfig>,
219240
) -> Result<u64, WebViewError> {
220241
let user_agent =
221242
user_agent.and_then(|ua| {
@@ -252,6 +273,10 @@ fn create_webview_inner(
252273
builder = builder.with_user_agent(ua);
253274
}
254275

276+
if let Some(proxy) = proxy_config {
277+
builder = builder.with_proxy_config(proxy)
278+
}
279+
255280
let webview = builder
256281
.with_navigation_handler(move |new_url| {
257282
if let Some(handler) = &nav_handler {
@@ -358,12 +383,13 @@ pub fn create_webview(
358383
width: i32,
359384
height: i32,
360385
url: String,
361-
nav_handler: Option<Box<dyn NavigationHandler>>
386+
nav_handler: Option<Box<dyn NavigationHandler>>,
387+
proxy: Option<Proxy>
362388
) -> Result<u64, WebViewError> {
363389
#[cfg(target_os = "linux")]
364390
{
365391
return run_on_gtk_thread(move || {
366-
create_webview_inner(parent_handle, width, height, url, None, nav_handler)
392+
create_webview_inner(parent_handle, width, height, url, None, nav_handler, proxy.map(proxy_from_record))
367393
});
368394
}
369395

@@ -378,12 +404,13 @@ pub fn create_webview_with_user_agent(
378404
height: i32,
379405
url: String,
380406
user_agent: Option<String>,
381-
nav_handler: Option<Box<dyn NavigationHandler>>
407+
nav_handler: Option<Box<dyn NavigationHandler>>,
408+
proxy: Option<Proxy>
382409
) -> Result<u64, WebViewError> {
383410
#[cfg(target_os = "linux")]
384411
{
385412
return run_on_gtk_thread(move || {
386-
create_webview_inner(parent_handle, width, height, url, user_agent, nav_handler)
413+
create_webview_inner(parent_handle, width, height, url, user_agent, nav_handler, proxy.map(proxy_from_record))
387414
});
388415
}
389416

0 commit comments

Comments
 (0)