Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Android proxy #222

Merged
merged 11 commits into from
Nov 5, 2024
Merged

feat: Android proxy #222

merged 11 commits into from
Nov 5, 2024

Conversation

WcaleNieWolny
Copy link
Contributor

@WcaleNieWolny WcaleNieWolny commented Nov 2, 2024

These changes are already in main. I accidentally pushed them with this commit. I will not revert as there are no more changes I want to push and unless someone has some objections, I think the code can say as it.

I will briefly explain how to use proxyRequests.

  • First, it's important to mention that it works only for android
  • proxyRequests is a regex expression

Here is an example how to use proxyRequests:

  1. Set window.InAppBrowserProxyRequest
    const proxyFn = async (call: Request) => {
    const headersObj = call.headers as any as { entries: () => IterableIterator<[string, string]> }
    const headers = Object.fromEntries(headersObj.entries())
    headers['User-Agent'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36'
    if (call.method === 'GET') {
      const proxiedResponse = await CapacitorHttp.get({
        url: 'https://example.com',
        headers: headers
      })
      console.log(JSON.stringify(proxiedResponse))
      return new Response(proxiedResponse.data, { status: proxiedResponse.status, headers: proxiedResponse.headers })
    }
    
    return null
    }
    ((window as any).InAppBrowserProxyRequest)  = proxyFn
  2. Set proxyRequests. In my case, I want to proxy all requests
    InAppBrowser.openWebView({ 
      url: 'https://apple.com', 
      headers: {
        'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36'
      },
      proxyRequests: `.*` // match everything
    })

This proxy has some limitations:

  • Response supports ONLY text data. Trying to pass a binary stream will cause a failure
  • The proxy fn will not be called for POST requests
  • For PUT requests, the proxy fn will be called, but the Request will not include the body
  • 3xx status codes are not suppored
  • content-type header is mandatory
  • this proxy works for Android only

/claim #221

Copy link

socket-security bot commented Nov 4, 2024

New and removed dependencies detected. Learn more about Socket for GitHub ↗︎

Package New capabilities Transitives Size Publisher
npm/[email protected] None +1 2.62 MB eventualbuddha, lukastaegert, rich_harris, ...2 more

🚮 Removed packages: npm/[email protected]

View full report↗︎

@WcaleNieWolny WcaleNieWolny merged commit 9f2c76a into proxy_android_to_review Nov 5, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant