ClericPy / ichrome

Chrome controller for Humans, based on Chrome Devtools Protocol(CDP) and python3.7+.
https://pypi.org/project/ichrome/
MIT License
224 stars 29 forks source link

How to change proxy server at any time? #143

Closed juhacz closed 6 months ago

juhacz commented 7 months ago

I have several dozen proxy servers with authorization (username + pass) at my disposal. I need to write a program that loops into a website protected by Cloudflare, after passing the Cloudflare security check and receiving a cookie, it will save it to the database. Looking through the examples and documentation of the class, it seems to me that there is no way to obtain this functionality at this point?

Specifically, in the loop before execution: tab.goto('https://www.example.com") change the proxy server to another one like proxyServer='http://user:pass@xx.xx.xx.xx:port

I found a class with similar functionality to yours, there is such an option, but unfortunately after several or dozens of loops, Chrome reports ERR_TIMED_OUT, so it doesn't really work either.

https://github.com/kaliiiiiiiii/Selenium-Driverless

Thanks for the answer.

ClericPy commented 7 months ago

issues about auth proxy: https://github.com/ClericPy/ichrome/issues/41 https://github.com/ClericPy/ichrome/issues/80 https://github.com/ClericPy/ichrome/issues/86 https://github.com/ClericPy/ichrome/issues/89

above all

One choice is to use gost to change the auth proxy into simple proxy. Or you can use Fetch to add Authorization header to pass the basic auth info, it's a little difficult. Or use playwright https://playwright.dev/python/docs/network#http-proxy

PS: use many proxies in the same browser, use the incognito_tab

juhacz commented 7 months ago

@ClericPy Thanks for reply.

I have configured my proxy server so that it does not require authentication. I'm trying to use it but it still doesn't work. Maybe the important information is that this is an ipv6 based proxy server, not ipv4.

The program from the main page of your repository displays the ipv4 of my computer, instead of the ipv6 address of the proxy server.

import asyncio
from ichrome import AsyncChromeDaemon

async def test():
    async with AsyncChromeDaemon() as cd:
        async with cd.incognito_tab(proxyServer='http://x.xx.xx.xxx:port') as tab:
            await tab.goto('https://api64.ipify.org', timeout=5)
            print(await tab.html)
asyncio.run(test())

Of course, everything is fine with the proxy server, because I use it without any problems, for example in selenium-wire or when manually configuring programs. Am I doing something wrong or does it have to be an ipv4 server?

ClericPy commented 7 months ago

There is a possibility that incognito mode is only effective on specific chrome versions. It is a laboratory function. It works on linux+Chromium, but it does not work on Windows. It can only be changed to the proxy parameter in AsyncChromeDaemon for each process to use. One proxy per daemon instead of one proxy per browser context for incognito mode

The proxyServer is developed with https://chromedevtools.github.io/devtools-protocol/tot/Target/#method-createBrowserContext

Target.createBrowserContext EXPERIMENTAL [#](https://chromedevtools.github.io/devtools-protocol/tot/Target/#method-createBrowserContext)
Creates a new empty BrowserContext. Similar to an incognito profile but you can have more than one.
PARAMETERS
disposeOnDetach
boolean
If specified, disposes this context when debugging session disconnects.
proxyServer
string
Proxy server, similar to the one passed to --proxy-server
proxyBypassList
string
Proxy bypass list, similar to the one passed to --proxy-bypass-list
originsWithUniversalNetworkAccess
array[ string ]
An optional list of origins to grant unlimited cross-origin access to. Parts of the URL other than those constituting origin are ignored.
RETURN OBJECT
browserContextId
[Browser.BrowserContextID](https://chromedevtools.github.io/devtools-protocol/tot/Browser/#type-BrowserContextID)
The id of the context created.

the proxy of AsyncChromeDaemon is --proxy-server for cmdline args

ClericPy commented 7 months ago

Maybe you can try playwright-python which is developed by MS team.

juhacz commented 7 months ago

In fact, the operation depends on the operating system and browser, on Chrome 120 and Windows 11 it does not work, on Debian and Chrome 119 it works. Thank you, I will try to choose the best method for my applications.