fysh711426/UndetectedChromeDriver

Using proxy with auth

Closed this issue · 6 comments

Are there any simple solutions to use undetected chromedriver with authorized proxies?

Maybe try an extension.
#30

any new update for this sourcecode bro ?

Are there any simple solutions to use undetected chromedriver with authorized proxies?

try my solution:
`
options.AddArguments("--disable-extensions"); // don't use this if you want add extension into selenium

  private const string background_js = "\r\nvar config = {\r\n\tmode: \"fixed_servers\",\r\n    rules: {\r\n        singleProxy: {\r\n            scheme: \"http\",\r\n            host: \"{HOST}\",\r\n            port: parseInt({PORT})\r\n        },\r\n        bypassList: []\r\n\t}\r\n};\r\n\r\nchrome.proxy.settings.set({ value: config, scope: \"regular\" }, function() { });\r\n\r\nfunction callbackFn(details)\r\n{\r\n\treturn {\r\n\t\tauthCredentials:\r\n\t\t{\r\n\t\t\tusername: \"{USERNAME}\",\r\n\t\t\tpassword: \"{PASSWORD}\"\r\n\t\t}\r\n\t};\r\n}\r\n\r\nchrome.webRequest.onAuthRequired.addListener(\r\n\tcallbackFn,\r\n\r\n\t{ urls:[\"<all_urls>\"] },\r\n    ['blocking']\r\n);";

    private const string manifest_json = "\r\n{\r\n    \"version\": \"1.0.0\",\r\n    \"manifest_version\": 2,\r\n    \"name\": \"Chrome Proxy\",\r\n    \"permissions\": [\r\n        \"proxy\",\r\n        \"tabs\",\r\n        \"unlimitedStorage\",\r\n        \"storage\",\r\n        \"<all_urls>\",\r\n        \"webRequest\",\r\n        \"webRequestBlocking\"\r\n    ],\r\n    \"background\": {\r\n        \"scripts\": [\"background.js\"]\r\n\t},\r\n    \"minimum_chrome_version\":\"22.0.0\"\r\n}";
    private static string ReplaceTemplates(string str, string host, string port, string userName, string password)
    {
        return str.Replace("{HOST}", host).Replace("{PORT}", port.ToString()).Replace("{USERNAME}", userName)
            .Replace("{PASSWORD}", password);
    }



#region Create Plugin use for chrome
                string contents = ReplaceTemplates("\r\nvar config = {\r\n\tmode: \"fixed_servers\",\r\n    rules: {\r\n        singleProxy: {\r\n            scheme: \"http\",\r\n            host: \"{HOST}\",\r\n            port: parseInt({PORT})\r\n        },\r\n        bypassList: []\r\n\t}\r\n};\r\n\r\nchrome.proxy.settings.set({ value: config, scope: \"regular\" }, function() { });\r\n\r\nfunction callbackFn(details)\r\n{\r\n\treturn {\r\n\t\tauthCredentials:\r\n\t\t{\r\n\t\t\tusername: \"{USERNAME}\",\r\n\t\t\tpassword: \"{PASSWORD}\"\r\n\t\t}\r\n\t};\r\n}\r\n\r\nchrome.webRequest.onAuthRequired.addListener(\r\n\tcallbackFn,\r\n\r\n\t{ urls:[\"<all_urls>\"] },\r\n    ['blocking']\r\n);",
                    {ip}, {port}, {username}, {password});
                if (!Directory.Exists("Plugins"))
                {
                    Directory.CreateDirectory("Plugins");
                }
                string manifest = "Plugins/manifest.json";
                string background = "Plugins/background.js";
                if (File.Exists(manifest))
                {
                    File.Delete(manifest);
                }
                if (File.Exists(background))
                {
                    File.Delete(background);
                }
                File.WriteAllText(manifest, "\r\n{\r\n    \"version\": \"1.0.0\",\r\n    \"manifest_version\": 2,\r\n    \"name\": \"Chrome Proxy\",\r\n    \"permissions\": [\r\n        \"proxy\",\r\n        \"tabs\",\r\n        \"unlimitedStorage\",\r\n        \"storage\",\r\n        \"<all_urls>\",\r\n        \"webRequest\",\r\n        \"webRequestBlocking\"\r\n    ],\r\n    \"background\": {\r\n        \"scripts\": [\"background.js\"]\r\n\t},\r\n    \"minimum_chrome_version\":\"22.0.0\"\r\n}");
                File.WriteAllText(background, contents);

                var extension = $"{System.IO.Directory.GetCurrentDirectory()}\\Plugins";
                options.AddArgument($"--load-extension={extension}");
                #endregion

`

any new update for this sourcecode bro ?

Not yet, waiting for undetected-chromedriver update. 😭

@hathuchung96 thank you for the solution!
I've just changed this code a little. I use many proxies at the same time and I added the ability to create folder for each proxy, Here is my code.

    public static class UCProxy
    {
        private const string background_js = @"
var config = {
    mode: ""fixed_servers"",
    rules: {
        singleProxy: {
            scheme: 'http',
            host: '{HOST}',
            port: parseInt({PORT})
        },
        bypassList: []
    }
};
chrome.proxy.settings.set({ 
    value: config, 
    scope: 'regular' }, 
    function() { });
function callbackFn(details) {
    return {
        authCredentials: {
            username: '{USERNAME}',
            password: '{PASSWORD}'
        }
    };
}
chrome.webRequest.onAuthRequired.addListener(callbackFn, { urls:['<all_urls>'] }, ['blocking']);
";

        private const string 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""
}
";

        public static void AddProxy(this ChromiumOptions options, string host, string port, string username, string password, string folder_name)
        {
            if (!Directory.Exists($"extensions/{folder_name}"))
            {
                Directory.CreateDirectory($"extensions/{folder_name}");
            }
            string manifest = $"extensions/{folder_name}/manifest.json";
            string background = $"extensions/{folder_name}/background.js";
            if (File.Exists(manifest))
            {
                File.Delete(manifest);
            }
            if (File.Exists(background))
            {
                File.Delete(background);
            }

            string background_content = ReplaceTemplates(background_js, host, port, username, password);

            File.WriteAllText(manifest, manifest_json);
            File.WriteAllText(background, background_content);

            var extension = $"{Directory.GetCurrentDirectory()}\\extensions\\{folder_name}";
            options.AddArgument($"--load-extension={extension}");

        }

        private static string ReplaceTemplates(string str, string host, string port, string userName, string password)
        {
            return str.Replace("{HOST}", host).Replace("{PORT}", port.ToString()).Replace("{USERNAME}", userName)
                .Replace("{PASSWORD}", password);
        }
    }

and then create unique folder name for each proxy and pass it to the extension:

string folder_name = Guid.NewGuid().ToString();
options.AddProxy(host, port, username, password, folder_name);

It remains only to add the ability to clean up this garbage after completion of work.

The problem is solved. Thank you!