Untitled

 avatar
user_2299906
plain_text
a year ago
4.3 kB
4
Indexable
Never
public class ProxyLoginExtension
    {

        string ProxyLogin_Ext_manifest = "{\r\n  \"version\": \"1.0.0\",\r\n  \"manifest_version\": 2,\r\n  \"name\": \"ChromeProxy\",\r\n  \"permissions\": [\r\n    \"proxy\",\r\n    \"<all_urls>\",\r\n    \"webRequest\",\r\n    \"webRequestBlocking\"\r\n  ],\r\n  \"background\": {\r\n    \"scripts\": [\r\n      \"background.js\"\r\n    ],\r\n    \"persistent\": true\r\n  },\r\n  \"minimum_chrome_version\": \"22.0.0\"\r\n}";
        string ProxyLogin_Ext_background = "var config = {\r\n\tmode: \"fixed_servers\",\r\n\trules: {\r\n\t\tsingleProxy: {\r\n\t\t\tscheme: \"http\",\r\n\t\t\thost: \"{host}\",\r\n\t\t\tport: {port}\r\n\t\t},\r\n\t\tbypassList: [\"localhost\"]\r\n\t}\r\n};\r\nvar authCre = {\r\n\tauthCredentials: {\r\n\t\tusername: \"{username}\",\r\n\t\tpassword: \"{password}\"\r\n\t}\r\n};\r\n\r\nchrome.proxy.settings.set({ value: config, scope: \"regular\" }, function () { });\r\nchrome.proxy.onProxyError.addListener(function (details) {\r\n\tconsole.log(details);\r\n});\r\nchrome.webRequest.onAuthRequired.addListener(\r\n\tfunction callbackFn(details) {\r\n\t\tconsole.log(details);\r\n\t\treturn authCre;\r\n\t},\r\n\t{ urls: [\"<all_urls>\"] },\r\n\t['blocking']\r\n);";
        public Match RegexMatch(string input, string pattern)
        {
            if (string.IsNullOrEmpty(input)) return Match.Empty;
            else return Regex.Match(input, pattern);
        }
        string trueproxy(string proxyurl)
        {
            if (!string.IsNullOrEmpty(proxyurl))
            {
                if (Regex.IsMatch(proxyurl, "(.*?):(.*?)@(\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})[.](\\d{1,3}):(\\d+)"))
                {
                    string user = RegexMatch(proxyurl, "(.*?):(.*?)@(\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})[.](\\d{1,3}):(\\d+)").Groups[1].Value;
                    string pass = RegexMatch(proxyurl, "(.*?):(.*?)@(\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})[.](\\d{1,3}):(\\d+)").Groups[2].Value;
                    string host = RegexMatch(proxyurl, "(.*?):(.*?)@(.*?):(\\d+)").Groups[3].Value;
                    string port = RegexMatch(proxyurl, "(.*?):(.*?)@(.*?):(\\d+)").Groups[4].Value;
                    proxyurl = host + ":" + port + "|" + user + "|" + pass;
                }
                else if (Regex.IsMatch(proxyurl, "(\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})[.](\\d{1,3}):(\\d+):(.*?):(.*?)"))
                {
                    string user = RegexMatch(proxyurl, "(\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})[.](\\d{1,3}):(\\d+):(.*?):(.*?)$").Groups[6].Value;
                    string pass = RegexMatch(proxyurl, "(\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})[.](\\d{1,3}):(\\d+):(.*?):(.*?)$").Groups[7].Value;
                    string host = RegexMatch(proxyurl, "(.*?):(.*?):(.*?):(.+)").Groups[1].Value;
                    string port = RegexMatch(proxyurl, "(.*?):(.*?):(.*?):(.+)").Groups[2].Value;
                    proxyurl = host + ":" + port + "|" + user + "|" + pass;
                }
            }
            return proxyurl;
        }
        public void GenerateExtension(string path, string proxyurl)
        {            
            string[] temp = trueproxy(proxyurl).Replace("|", ":").Split(':');
            if (temp.Length < 2) return;
            if (string.IsNullOrEmpty(temp[0]) || string.IsNullOrEmpty(temp[1])) return;
            string host = temp[0];
            string port = temp[1];
            string username = "";
            string password = "";
            if (temp.Length >= 4)
            {
                username = temp[2];
                password = temp[3];
            }
            string background_ = ProxyLogin_Ext_background
                .Replace("{host}", host)
                .Replace("{port}", port.ToString())
                .Replace("{username}", username ?? string.Empty)
                .Replace("{password}", password ?? string.Empty);

            if (Directory.Exists(path)) try { Directory.Delete(path); } catch { }
            Directory.CreateDirectory(path);

            File.WriteAllText(Path.Combine(path, "background.js"), background_);
            File.WriteAllText(Path.Combine(path, "manifest.json"), ProxyLogin_Ext_manifest);
        }



    }