Search code examples
c#selenium-webdriverselenium-chromedriversocks5

Setting Socks5 proxy with credentials for ChromeDriver C#


There are a few similar topics available, but neither of them helps me to solve the issue.

Basically, everything is simple. I have just a socks5 proxy with credentials. That's it.

        var host = "ip:port";
        var user = "user";
        var pass = "pass";
        var website = "https://website";

        // with HttpClient
        var proxy = new WebProxy($"socks5://{host}")
        {
            Credentials = new NetworkCredential(user, pass)
        };
        var handler = new HttpClientHandler
        {
            Proxy = proxy,
            UseProxy = true
        };
        var client = new HttpClient(handler);
        var content = await client.GetStringAsync(website); // <<< works well

        // With WebDriver
        var driverService = ChromeDriverService.CreateDefaultService();
        var options = new ChromeOptions();
        
        options.AddArguments($"--proxy-server=socks5://{user}:{pass}@{host}");

        var driver = new ChromeDriver(driverService, options);

        var networkAuthenticationHandler = new NetworkAuthenticationHandler
        {
            UriMatcher = uri => true, // this never even gets called!
            Credentials = new PasswordCredentials(user, pass)
        };

        var networkInterceptor = driver.Manage().Network;
        networkInterceptor.AddAuthenticationHandler(networkAuthenticationHandler);

        await networkInterceptor.StartMonitoring();

        driver.Navigate().GoToUrl(website);
        // ERR_NO_SUPPORTED_PROXIES error

Neither way works, even manual definition like this:

/*options.Proxy = new Proxy
{
    Kind = ProxyKind.Manual,
    IsAutoDetect = false,
    SocksVersion = 5,
    SocksUserName = user,
    SocksPassword = pass,
    SocksProxy = "socks5://" + host,
    HttpProxy = "socks5://" + host,
    SslProxy = "socks5://" + host,
};*/

The main issue that the UriMatcher is not even called! And Chrome logs show nothing specific. (Except empty "proxy" in "capabilities" section on Response InitSession).


Solution

  • Due to crbug#40829748, Chromium (and therefore Google-Chrome as well) doesn't support socks5 proxies with authentication.

    As a workaround, you can try one of

    1. use a middleware proxy server (socks5->middleware->Chrome) similar to seleniumwire (Python)
    2. Use Firefox instead of Chrome
    3. Use a proxy with another authentication protocol of available

    also see github.com/SeleniumHQ/selenium/issues/13820