admin管理员组

文章数量:1336187

My goal is to configure a proxy with authorization for an Android emulator (used with Appium) launched from command line.

I tried configuring the Android emulator directly with a proxy via this command:

emulator -avd <device> -http-proxy 'http://<user>:<password>@<host>:<port>'

Or setting in Android Studio settings

But I face the ERR_CONNECTION_REFUSED issue for HTTPS traffic.

Most solutions on internet handle this without the proxy auth. So a solution is to remove authorization from proxy by setting up local upstream proxy to the actual proxy with auth.

In Node.js, I achieved this with the proxy-chain library using the following code:

const { anonymizeProxy } = require('proxy-chain');

(async () => {
    const proxy = 'http://<user>:<password>@<host>:<port>';
    const anonymizedProxy = await anonymizeProxy(proxy);
    console.log(anonymizedProxy);
})();

This setup worked perfectly, handling HTTP and HTTPS traffic without issues. I tried to replicate this behavior in Python using pproxy library with the following code:

import asyncio
import pproxy

async def main():
    local_server = ':1234'
    upstream_proxy = 'http://<user>:<password>@<host>:<port>'

    server = pproxy.Server(local_server)
    remote = pproxy.Connection(upstream_proxy)
    args = {'rserver': [remote]}

    await server.start_server(args)
    await asyncio.Event().wait()

asyncio.run(main())

It works for HTTPS as system proxy inside my browser, but for emulator I still see ERR_CONNECTION_REFUSED error for HTTPS requests.

I do not need any traffic inspection just forwarding to the real proxy.

Is it possible to achieve this in Python? Are there libraries similar to proxy-chain for Python? Or is there a way to properly configure the Android emulator to use an authenticated proxy? Any insights would be appreciated!

本文标签: Handling HTTPS Proxy Authentication in Android Emulator with PythonStack Overflow