tebeka / selenium

Selenium/Webdriver client for Go
MIT License
2.54k stars 409 forks source link

Chrome SOCKS5 Proxy Error: ERR_SOCKS_CONNECTION_FAILED #201

Open cyber opened 4 years ago

cyber commented 4 years ago

Here is my setup:

    service, err := selenium.NewChromeDriverService("./chromedriver", 8080)
    if err != nil {
        panic(err)
    }

    defer service.Stop()

    caps := selenium.Capabilities{"browserName": "chrome"}

    caps.AddProxy(selenium.Proxy{
        Type:          selenium.Manual,
        SOCKS: "proxy.com",
        SOCKSVersion: 5,
        SocksPort: 777,
        SOCKSUsername: "username",
        SOCKSPassword: "password",
    })

    wd, err := selenium.NewRemote(caps, fmt.Sprintf("http://localhost:%d/wd/hub", 8080))
    if err != nil {
        panic(err)
    }

This will open chrome, but trying to visit any website will yield this error: ERR_SOCKS_CONNECTION_FAILED

The same proxy will work in python requests (in this format: socks5://username:password@proxy.com:777)

Any thoughts?

cyber commented 4 years ago

Also, adding something like socks5:// to the beginning of the SOCKS field causes the proxy to not work at all, and chromedriver connects directly to the internet.

cyber commented 4 years ago

Looks like this is just a limitation of chrome-driver itself? Had to switch to python & use the temporary extension trick to get this working.

Manouchehri commented 4 years ago

@cyber What's the temporary extension trick?

cyber commented 4 years ago

@Manouchehri can't find the exact stackoverflow thread that I copied it from, but this worked a couple months ago:

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)
def get_chromedriver(use_proxy=False, user_agent=None):
    chrome_options = webdriver.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.add_extension(pluginfile)
    if user_agent:
        chrome_options.add_argument('--user-agent=%s' % user_agent)

    chrome_options.add_experimental_option('excludeSwitches', ['enable-logging'])
    driver = webdriver.Chrome(
        CHROMEDRIVER_PATH,
        options=chrome_options)
    return driver

note that this'll create the .zip file in the directory, so you might want to remove it after oh and another limitation is that you can't use extensions in headless mode

Saberr43 commented 3 years ago

Same issue here after updating to newest version of the chrome-standalone docker image. I was able to have my proxies accept IP authentication, which appears to fix the issue, but basic auth in socks5 proxies are not working.

kasnet commented 3 years ago

I have the same issue

ProDanceGrammer commented 1 year ago

Did anyone find a solution?

milahu commented 11 months ago

https://github.com/SeleniumHQ/selenium/issues/9176#issuecomment-779208791

Chrome doesn't support SOCKS authentication

this "should" work by proxy-chaining with gost or redsocks (via)

gost -L=socks5://127.0.0.1:8005 -F=socks5://$username:$password@$ipaddr:$port &

chromium \
  --proxy-server=socks5://127.0.0.1:8005 \
  "--proxy-bypass-list=<-loopback>" \
  "--host-resolver-rules=MAP * ~NOTFOUND, EXCLUDE 127.0.0.1"

for host-resolver-rules see also https://www.chromium.org/developers/design-documents/network-stack/socks-proxy/