예제 #1
0
        public static void Sample_AntiCaptcha()
        {
            var target = new Uri("https://uam.hitmehard.fun/HIT");

            var cf = new CloudflareSolver(new AntiCaptchaProvider("YOUR_API_KEY"))
            {
                MaxTries        = 3, // Default value is 3
                MaxCaptchaTries = 1, // Default value is 1
                //ClearanceDelay = 3000  // Default value is the delay time determined in challenge code (not required in captcha)
            };

            var httpClientHandler = new HttpClientHandler();
            var httpClient        = new HttpClient(httpClientHandler);

            httpClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0");

            var result = cf.Solve(httpClient, httpClientHandler, target).Result;

            if (result.Success)
            {
                Console.WriteLine($"[Success] Protection bypassed: {result.DetectResult.Protection}");
            }
            else
            {
                Console.WriteLine($"[Failed] Details: {result.FailReason}");
                return;
            }

            // Once the protection has been bypassed we can use that httpClient to send the requests as usual
            var response = httpClient.GetAsync(target).Result;
            var html     = response.Content.ReadAsStringAsync().Result;

            Console.WriteLine($"Server response: {html}");
        }
        public static async Task WebClientSample()
        {
            var cf = new CloudflareSolver
            {
                MaxTries       = 3,
                ClearanceDelay = 3000
            };

            var result = await cf.Solve(target);

            if (!result.Success)
            {
                Console.WriteLine($"[Failed] Details: {result.FailReason}");
                return;
            }

            // Add session cookies, user-agent and proxy (if used) to the WebClient headers
            var client = new WebClient();

            client.Headers.Add(HttpRequestHeader.Cookie, result.Cookies.AsHeaderString());
            client.Headers.Add(HttpRequestHeader.UserAgent, result.UserAgent);

            // Once the protection has been bypassed we can use that WebClient to send the requests as usual
            var content = await client.DownloadStringTaskAsync(target);

            Console.WriteLine($"Server response: {content}");
        }
        public static async Task HttpWebRequestSample()
        {
            var cf = new CloudflareSolver
            {
                MaxTries       = 3,
                ClearanceDelay = 3000
            };

            var result = await cf.Solve(target);

            if (!result.Success)
            {
                Console.WriteLine($"[Failed] Details: {result.FailReason}");
                return;
            }

            // Add session cookies, user-agent and proxy (if used) to the HttpWebRequest headers
            var request = (HttpWebRequest)WebRequest.Create(target);

            request.Headers.Add(HttpRequestHeader.Cookie, result.Cookies.AsHeaderString());
            request.Headers.Add(HttpRequestHeader.UserAgent, result.UserAgent);

            // Once the protection has been bypassed we can use that HttpWebRequest to send the requests as usual
            var response = (HttpWebResponse)await request.GetResponseAsync();

            var content = await new StreamReader(response.GetResponseStream()).ReadToEndAsync();

            Console.WriteLine($"Server response: {content}");
        }
예제 #4
0
        public async Task <TModel> Get <TModel>(string path)
        {
            if (_cf != null)
            {
                var resultCloudflare = await _cf.Solve(_client, _handler, new Uri(_client.BaseAddress, path));

                if (!resultCloudflare.Success)
                {
                    HandleError(resultCloudflare);
                }
            }

            var result = await _client.GetAsync(path);

            if (!result.IsSuccessStatusCode)
            {
                HandleError(result);
            }
            var response = await result.Content.ReadAsStringAsync();

            return(JsonConvert.DeserializeObject <TModel>(response));
        }
        public async Task SolveWebsiteChallenge_uamhitmehardfun()
        {
            var cf = new CloudflareSolver
            {
                MaxTries       = 3,
                ClearanceDelay = 3000
            };

            var handler = new HttpClientHandler();
            var client  = new HttpClient(handler);

            var result = await cf.Solve(client, handler, uamhitmehardfunUri);

            Assert.IsTrue(result.Success);

            var content = await client.GetStringAsync(uamhitmehardfunUri);

            Assert.AreEqual("Dstat.cc is the best", content);
        }
        public async Task SolveWebsiteChallenge_soundpark()
        {
            var cf = new CloudflareSolver
            {
                MaxTries       = 3,
                ClearanceDelay = 3000
            };

            var handler = new HttpClientHandler();
            var client  = new HttpClient(handler);

            var result = await cf.Solve(client, handler, soundparkUri);

            Assert.IsTrue(result.Success);

            HttpResponseMessage response = await client.GetAsync(soundparkUri);

            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
        }
예제 #7
0
        public async Task SolveWebsiteChallenge_uamhitmehardfun_WebClient()
        {
            var cf = new CloudflareSolver
            {
                MaxTries       = 3,
                ClearanceDelay = 3000
            };

            var result = await cf.Solve(uamhitmehardfunUri);

            Assert.IsTrue(result.Success);

            var client = new WebClient();

            client.Headers.Add(HttpRequestHeader.Cookie, result.Cookies.AsHeaderString());
            client.Headers.Add(HttpRequestHeader.UserAgent, result.UserAgent);

            var content = await client.DownloadStringTaskAsync(uamhitmehardfunUri);

            Assert.AreEqual("Dstat.cc is the best", content);
        }
        public async Task SolveWebsiteChallenge_soundpark_WebClient()
        {
            var cf = new CloudflareSolver
            {
                MaxTries       = 3,
                ClearanceDelay = 3000
            };

            var result = await cf.Solve(soundparkUri);

            Assert.IsTrue(result.Success);

            var client = new WebClient();

            client.Headers.Add(HttpRequestHeader.Cookie, result.Cookies.AsHeaderString());
            client.Headers.Add(HttpRequestHeader.UserAgent, result.UserAgent);

            var content = await client.DownloadStringTaskAsync(soundparkUri);

            Assert.IsTrue(content.Contains("Music Torrent Tracker"));
        }
        public async Task SolveWebsiteChallenge_soundpark_HttpWebRequest()
        {
            var cf = new CloudflareSolver
            {
                MaxTries       = 3,
                ClearanceDelay = 3000
            };

            var result = await cf.Solve(soundparkUri);

            Assert.IsTrue(result.Success);

            var request = (HttpWebRequest)WebRequest.Create(soundparkUri);

            request.Headers.Add(HttpRequestHeader.Cookie, result.Cookies.AsHeaderString());
            request.Headers.Add(HttpRequestHeader.UserAgent, result.UserAgent);

            var response = (HttpWebResponse)await request.GetResponseAsync();

            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
        }
예제 #10
0
        public async Task SolveWebsiteChallenge_uamhitmehardfun_HttpWebRequest()
        {
            var cf = new CloudflareSolver
            {
                MaxTries       = 3,
                ClearanceDelay = 3000
            };

            var result = await cf.Solve(uamhitmehardfunUri);

            Assert.IsTrue(result.Success);

            var request = (HttpWebRequest)WebRequest.Create(uamhitmehardfunUri);

            request.Headers.Add(HttpRequestHeader.Cookie, result.Cookies.AsHeaderString());
            request.Headers.Add(HttpRequestHeader.UserAgent, result.UserAgent);

            var response = (HttpWebResponse)await request.GetResponseAsync();

            var content = await new StreamReader(response.GetResponseStream()).ReadToEndAsync();

            Assert.AreEqual("Dstat.cc is the best", content);
        }
        public async static Task Sample_AntiCaptcha()
        {
            var cf = new CloudflareSolver(new AntiCaptchaService("YOUR_API_KEY"))
            {
                MaxTries        = 5,
                MaxCaptchaTries = 2
            };

            var handler = new HttpClientHandler();
            var client  = new HttpClient(handler);

            var result = await cf.Solve(client, handler, target);

            if (!result.Success)
            {
                Console.WriteLine($"[Failed] Details: {result.FailReason}");
                return;
            }

            // Once the protection has been bypassed we can use that httpClient to send the requests as usual
            var content = await client.GetStringAsync(target);

            Console.WriteLine($"Server response: {content}");
        }
        public static async Task Sample()
        {
            var cf = new CloudflareSolver
            {
                MaxTries       = 3,
                ClearanceDelay = 3000
            };

            var handler = new HttpClientHandler();
            var client  = new HttpClient(handler);

            var result = await cf.Solve(client, handler, target);

            if (!result.Success)
            {
                Console.WriteLine($"[Failed] Details: {result.FailReason}");
                return;
            }

            // Once the protection has been bypassed we can use that HttpClient to send the requests as usual
            var content = await client.GetStringAsync(target);

            Console.WriteLine($"Server response: {content}");
        }
        public void SolveWebsiteChallenge_uamhitmehardfun_With2Captcha()
        {
            var target = new Uri("https://uam.hitmehard.fun/HIT");

            var cf = new CloudflareSolver(new TwoCaptchaProvider("YOUR_API_KEY"))
            {
                MaxTries        = 2,
                MaxCaptchaTries = 2
            };

            var httpClientHandler = new HttpClientHandler();
            var httpClient        = new HttpClient(httpClientHandler);

            httpClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0");

            var result = cf.Solve(httpClient, httpClientHandler, target).Result;

            Assert.IsTrue(result.Success);

            var response = httpClient.GetAsync(target).Result;
            var html     = response.Content.ReadAsStringAsync().Result;

            Assert.AreEqual("Dstat.cc is the best", html);
        }
        public void SolveWebsiteChallenge_uamhitmehardfun()
        {
            var uri = new Uri("https://uam.hitmehard.fun/HIT");

            var cf = new CloudflareSolver
            {
                MaxTries       = 3,
                ClearanceDelay = 3000
            };

            var httpClientHandler = new HttpClientHandler();
            var httpClient        = new HttpClient(httpClientHandler);

            httpClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0");

            var result = cf.Solve(httpClient, httpClientHandler, uri).Result;

            Assert.IsTrue(result.Success);

            var response = httpClient.GetAsync(uri).Result;
            var html     = response.Content.ReadAsStringAsync().Result;

            Assert.AreEqual("Dstat.cc is the best", html);
        }
        public async Task SolveWebsiteChallenge_soundpark_With2Captcha()
        {
            if (Settings.TwoCaptchaApiKey.Equals("YOUR_API_KEY"))
            {
                return;
            }

            var cf = new CloudflareSolver(new TwoCaptchaService(Settings.TwoCaptchaApiKey))
            {
                MaxTries        = 2,
                MaxCaptchaTries = 2
            };

            var handler = new HttpClientHandler();
            var client  = new HttpClient(handler);

            var result = await cf.Solve(client, handler, soundparkUri);

            Assert.IsTrue(result.Success);

            HttpResponseMessage response = await client.GetAsync(soundparkUri);

            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
        }
        public async Task SolveWebsiteChallenge_uamhitmehardfun_With2Captcha()
        {
            if (Settings.TwoCaptchaApiKey.Equals("YOUR_API_KEY"))
            {
                return;
            }

            var cf = new CloudflareSolver(new TwoCaptchaProvider(Settings.TwoCaptchaApiKey))
            {
                MaxTries        = 2,
                MaxCaptchaTries = 2
            };

            var handler = new HttpClientHandler();
            var client  = new HttpClient(handler);

            var result = await cf.Solve(client, handler, uamhitmehardfunUri);

            Assert.IsTrue(result.Success);

            var content = await client.GetStringAsync(uamhitmehardfunUri);

            Assert.AreEqual("Dstat.cc is the best", content);
        }
예제 #17
0
        /// <inheritdoc />
        public override void Process(BotData data)
        {
            base.Process(data);

            // If the clearance info is already set and we're not getting it fresh each time, skip
            if (data.UseProxies)
            {
                if (data.Proxy.Clearance != "" && data.Proxy.Cfduid != "" && !data.GlobalSettings.Proxies.AlwaysGetClearance)
                {
                    data.Log(new LogEntry("Skipping CF Bypass because there is already a valid cookie", Colors.White));
                    return;
                }
            }

            var localUrl = ReplaceValues(url, data);

            var timeout = data.GlobalSettings.General.RequestTimeout * 1000;

            // We initialize the solver basing on the captcha service available
            CloudflareSolver cf = null;

            switch (data.GlobalSettings.Captchas.CurrentService)
            {
            case BlockCaptcha.CaptchaService.AntiCaptcha:
                cf = new CloudflareSolver(new AntiCaptchaProvider(data.GlobalSettings.Captchas.AntiCapToken));
                break;

            case BlockCaptcha.CaptchaService.TwoCaptcha:
                cf = new CloudflareSolver(new TwoCaptchaProvider(data.GlobalSettings.Captchas.TwoCapToken));
                break;

            default:
                cf = new CloudflareSolver();
                break;
            }

            // Initialize the handler with the Proxy and the previous cookies
            HttpClientHandler handler = null;

            CookieContainer cookies = new CookieContainer();

            foreach (var cookie in data.Cookies)
            {
                cookies.Add(new Cookie(cookie.Key, cookie.Value));
            }

            if (data.UseProxies)
            {
                if (data.Proxy.Type != Extreme.Net.ProxyType.Http)
                {
                    throw new Exception($"The proxy type {data.Proxy.Type} is not supported by this block yet");
                }

                var proxy = new WebProxy(data.Proxy.Proxy, false);

                if (data.Proxy.Username != "")
                {
                    proxy.Credentials = new NetworkCredential(data.Proxy.Username, data.Proxy.Password);
                }

                handler = new HttpClientHandler()
                {
                    Proxy                  = proxy,
                    CookieContainer        = cookies,
                    AllowAutoRedirect      = true,
                    AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
                };
            }
            else
            {
                handler = new HttpClientHandler()
                {
                    CookieContainer        = cookies,
                    AllowAutoRedirect      = true,
                    AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
                };
            }

            // Initialize the HttpClient with the given handler, timeout, user-agent
            var httpClient = new HttpClient(handler);

            httpClient.Timeout = TimeSpan.FromMinutes(timeout);
            httpClient.DefaultRequestHeaders.Add("User-Agent", ReplaceValues(userAgent, data));

            var uri = new Uri(localUrl);

            // Solve the CF challenge
            var result = cf.Solve(httpClient, handler, uri).Result;

            if (result.Success)
            {
                data.Log(new LogEntry($"[Success] Protection bypassed: {result.DetectResult.Protection}", Colors.GreenYellow));
            }
            else
            {
                throw new Exception($"CF Bypass Failed: {result.FailReason}");
            }

            // Once the protection has been bypassed we can use that httpClient to send the requests as usual
            var response = httpClient.GetAsync(uri).Result;

            // Save the cookies
            var ck = cookies.GetCookies(uri);

            var clearance = "";
            var cfduid    = "";

            try
            {
                clearance = ck["cf_clearance"].Value;
                cfduid    = ck["__cfduid"].Value;
            }
            catch { }

            if (data.UseProxies)
            {
                data.Proxy.Clearance = clearance;
                data.Proxy.Cfduid    = cfduid;
            }

            data.Log(new LogEntry("Got Cloudflare clearance!", Colors.GreenYellow));
            data.Log(new LogEntry(clearance + Environment.NewLine + cfduid + Environment.NewLine, Colors.White));

            // Get code
            data.ResponseCode = ((int)response.StatusCode).ToString();
            data.Log(new LogEntry("Response code: " + data.ResponseCode, Colors.Cyan));

            // Get headers
            data.Log(new LogEntry("Received headers:", Colors.DeepPink));
            var headerList      = new List <KeyValuePair <string, string> >();
            var receivedHeaders = response.Headers.GetEnumerator();

            data.ResponseHeaders.Clear();

            while (receivedHeaders.MoveNext())
            {
                var header = receivedHeaders.Current;
                var value  = header.Value.GetEnumerator();
                if (value.MoveNext())
                {
                    data.ResponseHeaders.Add(header.Key, value.Current);
                    data.Log(new LogEntry(header.Key + ": " + value.Current, Colors.LightPink));
                }
            }

            // Get cookies
            data.Log(new LogEntry("Received cookies:", Colors.Goldenrod));
            foreach (Cookie cookie in ck)
            {
                if (data.Cookies.ContainsKey(cookie.Name))
                {
                    data.Cookies[cookie.Name] = cookie.Value;
                }
                else
                {
                    data.Cookies.Add(cookie.Name, cookie.Value);
                }
                data.Log(new LogEntry(cookie.Name + ": " + cookie.Value, Colors.LightGoldenrodYellow));
            }

            // Save the content
            data.ResponseSource = response.Content.ReadAsStringAsync().Result;
            data.Log(new LogEntry("Response Source:", Colors.Green));
            data.Log(new LogEntry(data.ResponseSource, Colors.GreenYellow));
        }
예제 #18
0
            protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
            {
                var baseUri = new Uri(request.RequestUri, "/");

                request.Headers.Add("User-Agent", UserAgent);

                HttpResponseMessage res = null;

                string cookie;

                using (CookieHeaderLock.GetReadLock())
                    cookie = Cookies;

                if (cookie != null)
                {
                    request.Headers.Add("Cookie", cookie);
                    res = await base.SendAsync(request, cancellationToken);

                    if (!CloudflareDetector.IsClearanceRequired(res))
                    {
                        return(res);
                    }

                    res.Dispose();
                    res = null;
                }

                if (!await semaphoreSlim.WaitAsync(0))
                {
                    await semaphoreSlim.WaitAsync();

                    semaphoreSlim.Release();

                    if (Cookies == null)
                    {
                        throw new BypassFailed();
                    }

                    using (CookieHeaderLock.GetReadLock())
                        cookie = Cookies;
                }
                else
                {
                    using (await CookieHeaderLock.GetWriteLockAsync())
                    {
                        var cf = new CloudflareSolver(UserAgent)
                        {
                            MaxTries       = 3,
                            ClearanceDelay = 5000,
                        };

                        try
                        {
                            var cfs = await cf.Solve(request.RequestUri).ConfigureAwait(false);

                            if (cfs.Success)
                            {
                                Cookies = cfs.Cookies.AsHeaderString();

                                return(await base.SendAsync(request, cancellationToken));
                            }
                        }
                        catch
                        {
                            // 2020-02-29
                            // New CF Challenge : __cf_chl_jschl_tk__
                            // https://github.com/RyuzakiH/CloudflareSolverRe/issues/14
                        }

                        try
                        {
                            BypassByBrowser frm = null;
                            try
                            {
                                frm = Application.Current.Dispatcher.Invoke(() => new BypassByBrowser(new UriBuilder(request.RequestUri)
                                {
                                    Path = null, Query = null
                                }.Uri)
                                {
                                    Owner = Application.Current.MainWindow,
                                });

                                var status = Application.Current.Dispatcher.Invoke(frm.ShowDialog) ?? false;

                                frm.Wait.Wait();

                                if (status)
                                {
                                    cookie = Cookies = frm.Cookies.GetCookieHeader(baseUri);
                                }
                                else
                                {
                                    throw new BypassFailed();
                                }
                            }
                            catch (BypassFailed)
                            {
                                throw;
                            }
                            finally
                            {
                                if (frm != null)
                                {
                                    Application.Current.Dispatcher.Invoke(frm.Close);
                                    Application.Current.Dispatcher.Invoke(frm.Dispose);
                                }
                            }
                        }
                        finally
                        {
                            semaphoreSlim.Release();
                        }
                    }
                }

                if (cookie != null)
                {
                    request.Headers.Add("Cookie", cookie);
                }

                res = await base.SendAsync(request, cancellationToken);

                if (!CloudflareDetector.IsClearanceRequired(res))
                {
                    return(res);
                }

                res.Dispose();
                res = null;

                throw new BypassFailed();
            }
예제 #19
0
        /// <inheritdoc />
        public override void Process(BotData data)
        {
            base.Process(data);

            // If the clearance info is already set and we're not getting it fresh each time, skip
            if (data.UseProxies)
            {
                if (data.Proxy.Clearance != "" && !data.GlobalSettings.Proxies.AlwaysGetClearance)
                {
                    data.Log(new LogEntry("Skipping CF Bypass because there is already a valid cookie", Colors.White));
                    data.Cookies["cf_clearance"] = data.Proxy.Clearance;
                    data.Cookies["__cfduid"]     = data.Proxy.Cfduid;
                    return;
                }
            }

            var localUrl = ReplaceValues(url, data);
            var uri      = new Uri(localUrl);

            // Initialize the captcha provider
            CaptchaService service = Captchas.GetService(data.GlobalSettings.Captchas);

            // Initialize the Cloudflare Solver
            CloudflareSolver cf = new CloudflareSolver(service, ReplaceValues(UserAgent, data))
            {
                ClearanceDelay  = 3000,
                MaxCaptchaTries = 1,
                MaxTries        = 3
            };

            // Create the cookie container
            CookieContainer cookies = new CookieContainer();

            foreach (var cookie in data.Cookies)
            {
                cookies.Add(new Cookie(cookie.Key, cookie.Value, "/", uri.Host));
            }

            // Initialize the http handler
            HttpClientHandler handler = new HttpClientHandler
            {
                AllowAutoRedirect      = AutoRedirect,
                CookieContainer        = cookies,
                AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
                SslProtocols           = SecurityProtocol.ToSslProtocols()
            };

            // Assign the proxy to the inner handler if necessary
            if (data.UseProxies)
            {
                if (data.Proxy.Type != Extreme.Net.ProxyType.Http)
                {
                    throw new Exception($"The proxy type {data.Proxy.Type} is not supported by this block yet");
                }

                handler.Proxy    = new WebProxy(data.Proxy.Proxy, false);
                handler.UseProxy = true;

                if (!string.IsNullOrEmpty(data.Proxy.Username))
                {
                    handler.DefaultProxyCredentials = new NetworkCredential(data.Proxy.Username, data.Proxy.Password);
                }
            }

            // Initialize the http client
            HttpClient http = new HttpClient(handler);

            http.Timeout = TimeSpan.FromSeconds(data.GlobalSettings.General.RequestTimeout);
            http.DefaultRequestHeaders.Add("User-Agent", ReplaceValues(UserAgent, data));

            SolveResult result = new SolveResult();

            try
            {
                result = cf.Solve(http, handler, uri, ReplaceValues(UserAgent, data)).Result;
            }
            catch (AggregateException ex)
            {
                // Join all the aggregate exception inner exception messages
                var message = string.Join(Environment.NewLine, ex.InnerExceptions.Select(e => e.Message));

                if (data.ConfigSettings.IgnoreResponseErrors)
                {
                    data.Log(new LogEntry(message, Colors.Tomato));
                    data.ResponseCode = message;
                    return;
                }
                throw new Exception(message);
            }
            catch (Exception ex)
            {
                if (data.ConfigSettings.IgnoreResponseErrors)
                {
                    data.Log(new LogEntry(ex.Message, Colors.Tomato));
                    data.ResponseSource = ex.Message;
                    return;
                }
                throw;
            }

            if (result.Success)
            {
                data.Log(new LogEntry($"[Success] Protection bypassed: {result.DetectResult.Protection}", Colors.GreenYellow));
            }
            else if (result.DetectResult.Protection == CloudflareProtection.Unknown)
            {
                data.Log(new LogEntry($"Unknown protection, skipping the bypass!", Colors.Tomato));
            }
            else
            {
                var message = $"CF Bypass Failed: {result.FailReason}";

                if (data.ConfigSettings.IgnoreResponseErrors)
                {
                    data.Log(new LogEntry(message, Colors.Tomato));
                    data.ResponseSource = message;
                    return;
                }
                throw new Exception(message);
            }

            // Now that we got the cookies, proceed with the normal request
            HttpResponseMessage response = null;

            try
            {
                response = http.GetAsync(uri).Result;
            }
            catch (Exception ex)
            {
                if (data.ConfigSettings.IgnoreResponseErrors)
                {
                    data.ResponseSource = ex.Message;
                    return;
                }
                throw new Exception(ex.Message);
            }
            finally
            {
                handler.Dispose();
                http.Dispose();
            }

            var responseString = response.Content.ReadAsStringAsync().Result;

            // Save the cloudflare cookies
            var clearance = "";
            var cfduid    = "";

            foreach (Cookie cookie in cookies.GetCookies(uri))
            {
                switch (cookie.Name)
                {
                case "cf_clearance":
                    clearance = cookie.Value;
                    break;

                case "__cfduid":
                    cfduid = cookie.Value;
                    break;
                }
            }

            // Save the cookies in the proxy
            if (data.UseProxies)
            {
                data.Proxy.Clearance = clearance;
                data.Proxy.Cfduid    = cfduid;
            }

            if (clearance != "")
            {
                data.Log(new LogEntry("Got Cloudflare clearance!", Colors.GreenYellow));
                data.Log(new LogEntry(clearance + Environment.NewLine + cfduid + Environment.NewLine, Colors.White));
            }

            // Get address
            data.Address = response.RequestMessage.RequestUri.AbsoluteUri;
            if (PrintResponseInfo)
            {
                data.Log(new LogEntry($"Address: {data.Address}", Colors.Cyan));
            }

            // Get code
            data.ResponseCode = ((int)response.StatusCode).ToString();
            if (PrintResponseInfo)
            {
                data.Log(new LogEntry($"Response code: {data.ResponseCode}", Colors.Cyan));
            }

            // Get headers
            if (PrintResponseInfo)
            {
                data.Log(new LogEntry("Received headers:", Colors.DeepPink));
            }
            data.ResponseHeaders.Clear();
            foreach (var header in response.Headers)
            {
                var h = new KeyValuePair <string, string>(header.Key, header.Value.First());
                data.ResponseHeaders.Add(h.Key, h.Value);
                if (PrintResponseInfo)
                {
                    data.Log(new LogEntry($"{h.Key}: {h.Value}", Colors.LightPink));
                }
            }

            // Add the Content-Length header if it was not sent by the server
            if (!data.ResponseHeaders.ContainsKey("Content-Length"))
            {
                if (data.ResponseHeaders.ContainsKey("Content-Encoding") && data.ResponseHeaders["Content-Encoding"].Contains("gzip"))
                {
                    data.ResponseHeaders["Content-Length"] = GZip.Zip(responseString).Length.ToString();
                }
                else
                {
                    data.ResponseHeaders["Content-Length"] = responseString.Length.ToString();
                }

                if (PrintResponseInfo)
                {
                    data.Log(new LogEntry($"Content-Length: {data.ResponseHeaders["Content-Length"]}", Colors.LightPink));
                }
            }

            // Get cookies
            if (PrintResponseInfo)
            {
                data.Log(new LogEntry("Received cookies:", Colors.Goldenrod));
            }
            foreach (Cookie cookie in cookies.GetCookies(uri))
            {
                data.Cookies[cookie.Name] = cookie.Value;
                if (PrintResponseInfo)
                {
                    data.Log(new LogEntry($"{cookie.Name}: {cookie.Value}", Colors.LightGoldenrodYellow));
                }
            }

            // Print source
            data.ResponseSource = responseString;
            if (PrintResponseInfo)
            {
                data.Log(new LogEntry("Response Source:", Colors.Green));
                data.Log(new LogEntry(data.ResponseSource, Colors.GreenYellow));
            }
        }
예제 #20
0
            protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
            {
                var baseUri = new Uri(request.RequestUri, "/");

                request.Headers.Add("User-Agent", UserAgent);

                HttpResponseMessage res = null;

                CookieInfomation ci;

                lock (Cookies)
                    if (!Cookies.TryGetValue(request.RequestUri.Host, out ci))
                    {
                        Cookies[request.RequestUri.Host] = (ci = new CookieInfomation());
                    }

                string cookie;

                using (ci.Lock.GetReadLock())
                    cookie = ci.Cookie;

                request.Headers.Add("Cookie", cookie);
                res = await base.SendAsync(request, cancellationToken);

                if (res == null)
                {
                    throw new NullReferenceException();
                }

                if (!CloudflareDetector.IsClearanceRequired(res))
                {
                    return(res);
                }

                res.Dispose();
                res = null;

                if (!await ci.SemaphoreSlim.WaitAsync(0))
                {
                    await ci.SemaphoreSlim.WaitAsync();

                    ci.SemaphoreSlim.Release();

                    if (ci.Cookie == null)
                    {
                        throw new BypassFailed();
                    }

                    using (ci.Lock.GetReadLock())
                        cookie = ci.Cookie;
                }
                else
                {
                    try
                    {
                        using (await ci.Lock.GetWriteLockAsync())
                        {
                            var cf = new CloudflareSolver(UserAgent)
                            {
                                MaxTries       = 3,
                                ClearanceDelay = 5000,
                            };

                            try
                            {
                                SolveResult cfs;
                                using (var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken))
                                {
                                    cts.CancelAfter(5000);

                                    cfs = await cf.Solve(request.RequestUri, null, cts.Token);
                                }

                                if (cfs.Success)
                                {
                                    ci.Cookie = cfs.Cookies.AsHeaderString();

                                    return(await base.SendAsync(request, cancellationToken));
                                }
                            }
                            catch (OperationCanceledException)
                            {
                            }
                            catch (Exception ex)
                            {
                                SentrySdk.CaptureException(ex);
                            }
                        }
                    }
                    finally
                    {
                        ci.SemaphoreSlim.Release();
                    }
                }

                if (cookie != null)
                {
                    request.Headers.Add("Cookie", cookie);
                }

                res = await base.SendAsync(request, cancellationToken);

                if (!CloudflareDetector.IsClearanceRequired(res))
                {
                    return(res);
                }

                res.Dispose();
                res = null;

                throw new BypassFailed();
            }