ultrafunkamsterdam / undetected-chromedriver

Custom Selenium Chromedriver | Zero-Config | Passes ALL bot mitigation systems (like Distil / Imperva/ Datadadome / CloudFlare IUAM)
https://github.com/UltrafunkAmsterdam/undetected-chromedriver
GNU General Public License v3.0
9.75k stars 1.15k forks source link

Solution for adding authenticated Proxies (...almost!) #498

Open ENUM1 opened 2 years ago

ENUM1 commented 2 years ago

I am looking for a solution to use authenticated proxies with UC. The following allows me to insert authenticated proxies by inserting a chrome extension. However, it only works for webdriver and not UC.

The alternatives are to use seleniumwire.undetected_chromedriver but the handling of their TLS fingerprinting makes it unsuitable. So, I'm hoping for a crowd solution to this.

All help is appreciated.

import os
import zipfile
import undetected_chromedriver.v2 as uc

PROXY_HOST = 'gb.smartproxy.com'  # rotating proxy
PROXY_PORT = 30000
PROXY_USER = 'user'
PROXY_PASS = 'pass'

manifest_json = """
{
    "version": "1.0.0",
    "manifest_version": 2,
    "name": "Chrome Proxy",
    "permissions": [
        "proxy",
        "tabs",
        "unlimitedStorage",
        "storage",
        "<all_urls>",
        "webRequest",
        "webRequestBlocking"
    ],
    "background": {
        "scripts": ["background.js"]
    },
    "minimum_chrome_version":"22.0.0"
}
"""

background_js = """
var config = {
        mode: "fixed_servers",
        rules: {
        singleProxy: {
            scheme: "http",
            host: "%s",
            port: parseInt(%s)
        },
        bypassList: ["localhost"]
        }
    };

chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});

function callbackFn(details) {
    return {
        authCredentials: {
            username: "%s",
            password: "%s"
        }
    };
}

chrome.webRequest.onAuthRequired.addListener(
            callbackFn,
            {urls: ["<all_urls>"]},
            ['blocking']
);
""" % (
    PROXY_HOST,
    PROXY_PORT,
    PROXY_USER,
    PROXY_PASS,
)

pluginfile = "proxy_auth_plugin.zip"

def get_chromedriver(use_proxy=False, user_agent=None):
    path = os.path.dirname(os.path.abspath(__file__))
    chrome_options = uc.ChromeOptions()
    if use_proxy:
        pluginfile = 'proxy_auth_plugin.zip'

    with zipfile.ZipFile(pluginfile, "w") as zp:
        zp.writestr("manifest.json", manifest_json)
        zp.writestr("background.js", background_js)
    chrome_options = uc.ChromeOptions()
    chrome_options.add_extension(pluginfile)
    if user_agent:
        chrome_options.add_argument('--user-agent=%s' % user_agent)
    driver = uc.Chrome(options=chrome_options)
    return driver

def main():
    driver = get_chromedriver(use_proxy=True)
    #driver.get('https://www.google.com/search?q=my+ip+address')
    driver.get('https://httpbin.org/ip')

if __name__ == '__main__':
    main()
taytanqbpass commented 2 years ago

yep I actually also need this. But I'm getting issues with seleniumwire.undetected_chromedriver.v2 when packing the app with pyinstaller. i get some errors as discussed in https://github.com/wkeeling/selenium-wire/issues/469

iamat8080 commented 2 years ago

option.add_argument(f"--load-extension={os.getcwd()}/Chrome/proxy")