Smartproxy / Selenium-proxy-authentication

Example of username and password proxy authentication for use in Selenium
https://smartproxy.com/
MIT License
65 stars 13 forks source link

Proxy #5

Closed bansaripanseriya closed 1 month ago

bansaripanseriya commented 3 months ago

How to use proxy if I have only endpoint and api_key of proxy?

sakanamk commented 2 months ago

Hey,

If you only have an API key for your proxy, you'll need to modify main.py and extension.py files. This is based on the assumption that the proxy provider accepts API keys in this manner.

In the main.py script, simply replace username and password with api_key:

# main.py

from webdriver_manager.chrome import ChromeDriverManager
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from extension import proxies

api_key = 'API key'
endpoint = 'endpoint'
port = 'port'
website = 'https://www.ipinfo.io/ip'

chrome_options = webdriver.ChromeOptions()

proxies_extension = proxies(api_key, endpoint, port)

chrome_options.add_extension(proxies_extension)
chrome_options.add_argument("--headless=new")

chrome = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)
chrome.get(website)

In the extension.py file also replace username and password with api_key in the proxies function parameters and the substitutions at the end of background_js. The Chrome extension's authCredentials object requires a username and password , but the workaround I've found is to just use a placeholder like user for the username and use the API key as the password:

# extension.py

import zipfile

def proxies(api_key, endpoint, port):
    manifest_json = """
    {
        "version": "1.0.0",
        "manifest_version": 2,
        "name": "Proxies",
        "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: "user",
                password: "%s"
            }
        };
    }

    chrome.webRequest.onAuthRequired.addListener(
                callbackFn,
                {urls: ["<all_urls>"]},
                ['blocking']
    );
    """ % (endpoint, port, api_key)

    extension = 'proxies_extension.zip'

    with zipfile.ZipFile(extension, 'w') as zp:
        zp.writestr("manifest.json", manifest_json)
        zp.writestr("background.js", background_js)

    return extension

I hope this helps!