selenium icon indicating copy to clipboard operation
selenium copied to clipboard

Chrome SOCKS5 Proxy Error: ERR_SOCKS_CONNECTION_FAILED

Open cyber opened this issue 5 years ago • 8 comments

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:[email protected]:777)

Any thoughts?

cyber avatar Jun 18 '20 01:06 cyber

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 avatar Jun 18 '20 01:06 cyber

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.

cyber avatar Sep 07 '20 22:09 cyber

@cyber What's the temporary extension trick?

Manouchehri avatar Sep 09 '20 20:09 Manouchehri

@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

cyber avatar Sep 10 '20 05:09 cyber

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.

Saberr43 avatar Nov 25 '20 00:11 Saberr43

I have the same issue

kasnet avatar Apr 23 '21 10:04 kasnet

Did anyone find a solution?

ProDanceGrammer avatar Jun 12 '23 18:06 ProDanceGrammer

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/

milahu avatar Dec 20 '23 13:12 milahu