Coder Social home page Coder Social logo

gedysintraware / cordova-plugin-webview-proxy Goto Github PK

View Code? Open in Web Editor NEW
21.0 3.0 12.0 31 KB

Plugin to proxy requests through the webview

Home Page: https://www.npmjs.com/package/cordova-plugin-webview-proxy

License: MIT License

Objective-C 90.84% JavaScript 9.16%
cordova cordova-ios wkwebview cors cookies proxy

cordova-plugin-webview-proxy's Introduction

cordova-plugin-webview-proxy

Work in progress plugin.

Cordova Plugin to proxy http(s) requests on iOS without CORS and Cookie restrictions

With this plugin you can do requests to remote servers just like you would do normally. Cookies and CORS restrictions don't apply here because the requests is performed by native code.

You just need to change the URL:

const response = await fetch(window.WebviewProxy.convertProxyUrl(url));
console.debug(response);

To delete all Cookies use this:

window.WebviewProxy.clearCookie();

Make sure you are using a custom scheme with your iOS platform

This plugin uses the WKURLSchemeHandler provided by WKWebView. It requires the latest version of cordova-ios.

You enable the custom scheme by setting these preferences in config.xml

<preference name="scheme" value="app" />
<preference name="hostname" value="testapp"/>

Testing this plugin

This test app with custom pages and a simple backend is helpful for testing and developing this plugin.

cordova-plugin-webview-proxy's People

Contributors

btietze avatar jurihahn avatar mmrosekgi avatar niklasmerz avatar thorsten-wolf-neptune avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

cordova-plugin-webview-proxy's Issues

[BUG] - Upload files using multipart/form-data;

Describe the bug
When trying to use file upload using proxy, the request is getting to the server, but the file is not uploaded....
Like there is an empty input.

*To Reproduce
configurations:

  • Use IOS device version 14.x
  • wkWebview Cordova ios 6.0.2
  • cordova plugin webview proxy
  • scheme is something://www.somethig.com
  1. add file input
  2. use FormData to add the file.
  3. use xhr and post it with multipart/form-data using the proxy.

Expected behavior
The server should get the body of the request

[BUG] don't work on iOS 12 and 11

Describe the bug
Same code tested on iOS: 14 (work), 13 (work), 12 (DON'T work), 11 (DON'T work)
HTTP Requests do not work on iOS 12 and 11

To Reproduce
Steps to reproduce the behavior:
Start you App under iOS 11 or 12 and make HTTP Request

Expected behavior
HTTP Requests work on all iOS versions

[BUG] Possible memory leak causing web view reload when downloading large files (images and videos) through the proxy

When our app starts, it fetches a lot of images and videos, about 100-200 MB total, in one go. While this occurs (only when proxying requests using cordova-plugin-webview-proxy), native app memory usage goes up, then the web view crashes and reloads when it reaches a certain point (~120-160 MB).

Here is the native console log output that appears when this occurs:

2021-12-14 12:24:27.381203-0500 App[2436:466287] [Process] 0x102cc8c00 - [PID=2447] WebProcessProxy::didClose: (web process 0 crash)
2021-12-14 12:24:27.381296-0500 App[2436:466287] [Process] 0x102cc8c00 - [PID=2447] WebProcessProxy::processDidTerminateOrFailedToLaunch: reason=4
2021-12-14 12:24:27.381361-0500 App[2436:466287] [ProcessSuspension] 0x102cf81e0 - ProcessAssertion: Failed to acquire RBS Background assertion 'ConnectionTerminationWatchdog' for process because PID is invalid
2021-12-14 12:24:27.381517-0500 App[2436:466287] [Process] 0x15883c418 - [pageProxyID=11, webPageID=12, PID=2447] WebPageProxy::processDidTerminate: (pid 2447), reason 4
2021-12-14 12:24:27.385477-0500 App[2436:466287] [Loading] 0x15883c418 - [pageProxyID=11, webPageID=12, PID=2447] WebPageProxy::dispatchProcessDidTerminate: reason=4
2021-12-14 12:24:27.387126-0500 App[2436:467004] [ProcessSuspension] 0x102cf81e0 - ProcessAssertion: Failed to acquire RBS assertion 'ConnectionTerminationWatchdog' for process with PID=0, error: (null)
2021-12-14 12:24:27.392717-0500 App[2436:467004] [assertion] Error acquiring assertion: <Error Domain=RBSServiceErrorDomain Code=1 "target is not running or doesn't have entitlement com.apple.runningboard.assertions.webkit" UserInfo={NSLocalizedFailureReason=target is not running or doesn't have entitlement com.apple.runningboard.assertions.webkit}>
2021-12-14 12:24:27.392875-0500 App[2436:467004] [ProcessSuspension] 0x102cf8240 - ProcessAssertion: Failed to acquire RBS assertion 'WebProcess Background Assertion' for process with PID=2447, error: Error Domain=RBSServiceErrorDomain Code=1 "target is not running or doesn't have entitlement com.apple.runningboard.assertions.webkit" UserInfo={NSLocalizedFailureReason=target is not running or doesn't have entitlement com.apple.runningboard.assertions.webkit}

Seems like the proxy is leaking something, but I'm not sure what at the moment. Any ideas?

To Reproduce

  1. Fetch some amount of large files using the proxy
  2. Watch memory usage
  3. Memory constantly goes up until the web view reloads

Expected behavior
Not leaking / reloading.

Cookies aren't sent to iframes or inappbrowser windows that load from the same domain

Describe the bug
Cookies aren't sent to iframes or inappbrowser windows that load from the same domain

To Reproduce

  1. Make a request through this plugin that has a response that sets a cookie.
  2. Load an iframe or inappbrowser window from the same domain as the above request
  3. Observe no cookies are sent

Expected behavior
iframes and inappbrowser windows have access to cookies set by cordova requests to that domain

Add Android support

Is your feature request related to a problem? Please describe.
Sometimes I meet CORS issues on Android devices/simulators as well.

Describe the solution you'd like
It would be helpful to have the same proxy functionality on Android platform.

Describe alternatives you've considered
cordova-plugin-advanced-http can be treated as alternative. CORS restrictions do not apply for it. But it is full-featured HTTP client which functionality in many points exceeds required minimum. Single lightweight plugin for this particular task would be preferable.

UPDATE
Found this issue in cordova-android repo. It solved my problem completely.
Shortly: need to add <preference name="AndroidInsecureFileModeEnabled" value="true" /> to config.xml. It disables the CORS enforcement for Cordova 10.x.

[BUG] Memory leak

Dear Alls,
After I apply this plugin, the Cookies is store.
However, when i check memory leak via XCode Instruments tool, this tool detected memory leak as below:
100.00% [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:cookies forURL:httpResponse.URL mainDocumentURL:nil];
If you have any suggestion, please let me known.
Thanks

proxy not working... still able to see the request trough network tab on safari

Hi
I am trying to integrate this plugin to overcome a CORS error while developing my app.
Somehow the plugins send the request but I can see the request trough network tab on safari so... I ended up having the same issue as before...CORS restrictions.

I DO see the request that change from https_proxy to the actual url but at the end the request is sent trough the webview instead of been native.

Can you please help?

here you have an example of my implementation

 var url = window.cordova ? window.WebviewProxy.convertProxyUrl(config.services.host + config.services.logout2ndStep) : config.services.host + config.services.logout2ndStep;
                    $http({
                            method: 'POST',
                            url: url,
                            withCredentials: true
                        })

Remove all cookie if device is offline

if I need to logout and can not make request to server, I want just to delete cookie.
document.cookie is always empty. And it is not possible to just remove all cookies?

We need anything like window.WebviewProxy.clearCookie()

Requests time out after 60 seconds

Describe the bug
Requests time out after 60 seconds

To Reproduce
Make a request that takes longer than 60s to resolve

Expected behavior
I was able to get past this by setting my own setTimeoutInterval on the request, but this probably needs to be made configurable

window.CDV_ASSETS_URL is undefined, leading to nothing being proxied

Describe the bug

See my comment in #3 for more details.

Environment: in a new Cordova application (built with cordova create) and these plugins installed:

    <plugin name="cordova-plugin-webview-proxy" spec="0.0.3" />
    <engine name="ios" spec="^6.2.0" />

I'm launching the application on an iPhone running iOS 14.4 and opening the Safari Developer Tools.

Running window.WebviewProxy.convertProxyUrl('https://example.com')); returns https://example.com, with no new protocol prefixed. As a result, requests are not proxied through this plugin, and requests to external servers fail for CORS reasons.

Investigating further, I see that window.CDV_ASSETS_URL returns undefined, which results in this line being returned: https://github.com/GEDYSIntraWare/cordova-plugin-webview-proxy/blob/master/www/WebviewProxy.js#L8

Any idea why this might be happening? I can verify that the platform is on the latest version (running cordova.platformVersion === 6.2.0).

[BUG] lowest supported ios api level?

Hi all,

Thanks for the awesome plugin!

We have ios cordova core 6.2.
We use the plugin on ios 13 and ios 14 with more or less success.
We realized that the intercepting of request is not perfect on ios 12.
The body of the request is always nil on my iphone 6 ios 12.5.2. here:
https://github.com/GEDYSIntraWare/cordova-plugin-webview-proxy/blob/master/src/ios/WebviewProxy.m#L27

Can you confirm that this is a bug? or what is the lowest supported ios api level for the plugin?
Do you have any suggestions for poor developers who must support old ios12 for client demand?

I am not an objective c ninja, please let me know if I see the situation wrongly.

Regards,
Gabor

Request body (urlSchemeTask.request.HTTPBody) is nil

Hi @NiklasMerz ,
We have fully set up the cordova-ios 6.2.0 + the current plugin + converting all the urls to be routed through the webproxy.
Unfortunately the very first call fails and after debugging it I have noticed that the urlSchemeTask.request.HTTPBody is nil even though from Javascript the body is correctly set up, in plugin code there is no body (the body is a json "{"name":"t","password":"1"}") and the headers are correctly set and transmitted.

The call is correctly routed to our server because the response headers contain the server details but we are getting back a 400 Bad Request and since this is the login call no cookies are returned hence everything stops.

Few more (maybe important) details:

  1. The call is a POST to our DB server which is CouchDB (done automatically by PoouchDB client DB)
  2. The call is a HTTPS call
  3. In Javascript there is a middleware polyfill xhr-polyfill (but that has been working perfectly since this moment) but I did debug in Safari and XCode at the same time and all the request details have been set correctly (url, body, headers)
  4. After returning in Javascript there is an error "TypeError: undefined is not an object (evaluating 'xhr.readyState')" - but this might not be relevant I think

Logs

There seems to be a stopSchemeTask log but I suspect is due to the fact our server returns 400

[BUG] cordova-plugin-webview-proxy doesn't play nice with navigator.mediaDevices in a cordova ios app

Describe the bug
cordova-plugin-webview-proxy doesn't play nice with navigator.mediaDevices in a cordova ios app.
when cordova-plugin-webview-proxy is configured to work in an ios app, navigator.mediaDevices will always return undefined.
The source of this problem are the 2 entries in the config.xml:

<preference name="scheme" value="app" />
<preference name="hostname" value="testapp"/>

If i'll remove these 2 entries, navigator.mediaDevices will start working, but the cordova-plugin-webview-proxy will stop working

To Reproduce
Steps to reproduce the behavior: mentioned above

Expected behavior
I would expect cordova-plugin-webview-proxy not to impact other functionalities

Be able to set cookies on the client

Is your feature request related to a problem? Please describe.
If I am not mistaken (and sorry for the noise if I am),

[[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if(error && (self.stoppedTasks == nil || ![self.stoppedTasks containsObject:urlSchemeTask])) {
@try {
NSLog(@"WebviewProxy error: %@", error);
[urlSchemeTask didFailWithError:error];
return;
} @catch (NSException *exception) {
NSLog(@"WebViewProxy send error exception: %@", exception.debugDescription);
}
}
saves the server-set cookie. A cookie set on the client (document.cookie) won't be used in any request. It would be nice to set cookies on the client as well.

Describe the solution you'd like
Ideally, something like window.WebviewProxy.setCookie() with the same behavior as document.cookie should do the job.

Additional context
I am testing this in a Meteor/Cordova context where I had no luck with app-bound-domains. This issue explains the issue in conjunction with Meteor risetechnologies/cookieTest#2 (comment)


Also thanks a lot @NiklasMerz for open sourcing your approach, IMHO this is a slim and neat way to tackle ITP issues! ๐Ÿš€

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.