From be292c9f7a3208718f8f624ffb7ee645f98a95a5 Mon Sep 17 00:00:00 2001 From: Daniel LaCosse <3759828+daniellacosse@users.noreply.github.com> Date: Thu, 7 Nov 2024 09:48:57 -0500 Subject: [PATCH 1/3] update mobileproxy webview android instructions --- x/mobileproxy/README.md | 46 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/x/mobileproxy/README.md b/x/mobileproxy/README.md index a45f0214..22b30dbb 100644 --- a/x/mobileproxy/README.md +++ b/x/mobileproxy/README.md @@ -551,9 +551,51 @@ Note that this may not fully work on Android, since it will only affect the JVM, ### Web View -We are working on instructions on how use the local proxy in a Webview. +#### Android -On Android, you will likely have to implement [WebViewClient.shouldInterceptRequest](https://developer.android.com/reference/android/webkit/WebViewClient#shouldInterceptRequest(android.webkit.WebView,%20android.webkit.WebResourceRequest)) to fulfill requests using an HTTP client that uses the local proxy. +On Android, you can create your own WebViewClient and override the [`shouldInterceptRequest`](https://developer.android.com/reference/android/webkit/WebViewClient#shouldInterceptRequest(android.webkit.WebView,%20android.webkit.WebResourceRequest)) method in order to fulfill requests with an HTTP client that uses the local proxy: + +```kotlin +import android.webkit.WebResourceRequest +import android.webkit.WebResourceResponse +import android.webkit.WebView +import android.webkit.WebViewClient +import java.net.HttpURLConnection +import java.net.InetSocketAddress +import java.net.Proxy +import java.net.URL + +class MyWebViewClient() : WebViewClient() { + override fun shouldInterceptRequest(view: WebView?, request: WebResourceRequest?): WebResourceResponse? { + return try { + val url = URL(request?.url.toString()) + val connection = url.openConnection( + Proxy( + Proxy.Type.HTTP, + InetSocketAddress(/* proxyHost */, /* proxyPort */) + ) + ) as HttpURLConnection + + WebResourceResponse( + connection.contentType, + connection.contentEncoding, + connection.inputStream + ) + } catch (e: Exception) { + // TODO: handle error + null + } + } +} +``` + +Then you simply inject that client into your activity's WebView like so: + +```kotlin +this.webView.webViewClient = MyWebViewClient() +``` + +#### iOS As of iOS 17, you can add a proxy configuration to a `WKWebView` via its [`WKWebsiteDataStore` property](https://developer.apple.com/documentation/webkit/wkwebviewconfiguration). From c16cae0ce53ab5869367e1067816c1df92fa5da5 Mon Sep 17 00:00:00 2001 From: Daniel LaCosse <3759828+daniellacosse@users.noreply.github.com> Date: Fri, 8 Nov 2024 11:22:34 -0500 Subject: [PATCH 2/3] update to proxy approach --- x/mobileproxy/README.md | 41 +++++++++-------------------------------- 1 file changed, 9 insertions(+), 32 deletions(-) diff --git a/x/mobileproxy/README.md b/x/mobileproxy/README.md index 22b30dbb..22ce9016 100644 --- a/x/mobileproxy/README.md +++ b/x/mobileproxy/README.md @@ -553,40 +553,17 @@ Note that this may not fully work on Android, since it will only affect the JVM, #### Android -On Android, you can create your own WebViewClient and override the [`shouldInterceptRequest`](https://developer.android.com/reference/android/webkit/WebViewClient#shouldInterceptRequest(android.webkit.WebView,%20android.webkit.WebResourceRequest)) method in order to fulfill requests with an HTTP client that uses the local proxy: +On Android, you can easily apply a proxy configuration to all the web views in your application with the [`androidx.webview`](https://developer.android.com/reference/androidx/webkit/ProxyController) library like so: ```kotlin -import android.webkit.WebResourceRequest -import android.webkit.WebResourceResponse -import android.webkit.WebView -import android.webkit.WebViewClient -import java.net.HttpURLConnection -import java.net.InetSocketAddress -import java.net.Proxy -import java.net.URL - -class MyWebViewClient() : WebViewClient() { - override fun shouldInterceptRequest(view: WebView?, request: WebResourceRequest?): WebResourceResponse? { - return try { - val url = URL(request?.url.toString()) - val connection = url.openConnection( - Proxy( - Proxy.Type.HTTP, - InetSocketAddress(/* proxyHost */, /* proxyPort */) - ) - ) as HttpURLConnection - - WebResourceResponse( - connection.contentType, - connection.contentEncoding, - connection.inputStream - ) - } catch (e: Exception) { - // TODO: handle error - null - } - } -} +ProxyController.getInstance() + .setProxyOverride( + ProxyConfig.Builder() + .addProxyRule(this.proxy!!.address()) + .build(), + {}, // execution context for the following callback + {} // callback to be called once the ProxyConfig is applied + ) ``` Then you simply inject that client into your activity's WebView like so: From fa2f18f51f989ee35b24fdcaec08eff8982d98ed Mon Sep 17 00:00:00 2001 From: Daniel LaCosse <3759828+daniellacosse@users.noreply.github.com> Date: Thu, 14 Nov 2024 11:27:24 -0500 Subject: [PATCH 3/3] Update README.md --- x/mobileproxy/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/mobileproxy/README.md b/x/mobileproxy/README.md index 22ce9016..67970baf 100644 --- a/x/mobileproxy/README.md +++ b/x/mobileproxy/README.md @@ -561,7 +561,7 @@ ProxyController.getInstance() ProxyConfig.Builder() .addProxyRule(this.proxy!!.address()) .build(), - {}, // execution context for the following callback + {}, // execution context for the following callback - do anything needed here once the proxy is applied, like refreshing web views {} // callback to be called once the ProxyConfig is applied ) ```