예제 #1
0
 private void ClearSessionVariables()
 {
     _httpClient        = null;
     _cloudflareHandler = null;
     _siteUrl           = null;
     _cancellationToken = null;
     _captchaDetectResults.Clear();
 }
예제 #2
0
        /// <summary>
        /// Solves cloudflare challenge protecting a specific website.
        /// </summary>
        /// <param name="httpClient">HttpClient to use in challenge solving process.</param>
        /// <param name="cloudflareHandler">CloudflareHandler of the HttpClient</param>
        /// <param name="siteUrl">Uri of the website.</param>
        /// <param name="cancellationToken">CancellationToken to contol solving operation cancelling.</param>
        internal async Task <SolveResult> Solve(HttpClient httpClient, CloudflareHandler cloudflareHandler, Uri siteUrl, [Optional] CancellationToken cancellationToken)
        {
            this.cloudflareHandler = cloudflareHandler;
            this.httpClient        = httpClient.Clone(this.cloudflareHandler, false);
            this.siteUrl           = siteUrl;
            this.cancellationToken = cancellationToken;

            var result = await Solve();

            this.httpClient.Dispose();
            captchaDetectResults.Clear();

            return(result);
        }
예제 #3
0
        /// <summary>
        /// Detects cloudflare protection type for a specific website.
        /// </summary>
        /// <param name="httpClient">HttpClient to use in detection process.</param>
        /// <param name="httpClientHandler">HttpClientHandler of the HttpClient.</param>
        /// <param name="targetUri">The uri of the website.</param>
        /// <param name="requireHttps">Https is required.</param>
        public static async Task <DetectResult> Detect(HttpClient httpClient, HttpClientHandler httpClientHandler, Uri targetUri, bool requireHttps = false)
        {
            DetectResult result = default(DetectResult);

            //await _locker.LockAsync(async () =>
            //{
            var cloudflareHandler = new CloudflareHandler(httpClientHandler);

            result = await Detect(httpClient, cloudflareHandler, targetUri, requireHttps);

            //cloudflareHandler.Dispose();
            //});

            return(result);
        }
예제 #4
0
        /// <summary>
        /// Solves cloudflare challenge protecting a specific website.
        /// </summary>
        /// <param name="httpClient">HttpClient to use in challenge solving process.</param>
        /// <param name="httpClientHandler">HttpClientHandler of the HttpClient.</param>
        /// <param name="siteUrl">Uri of the website.</param>
        /// <param name="cancellationToken">CancellationToken to contol solving operation cancelling.</param>
        /// <param name="randomUserAgent">Use a new random user-agent.</param>
        public async Task <SolveResult> Solve(HttpClient httpClient, HttpClientHandler httpClientHandler, Uri siteUrl, [Optional] CancellationToken cancellationToken, bool randomUserAgent = false)
        {
            SolveResult result = default(SolveResult);

            await _locker.LockAsync(async() =>
            {
                userAgent             = randomUserAgent ? Utils.GetGenerateRandomUserAgent() : defaultUserAgent;
                var cloudflareHandler = new CloudflareHandler(httpClientHandler, userAgent);
                result = await Solve(httpClient, cloudflareHandler, siteUrl, cancellationToken);

                httpClient.DefaultRequestHeaders.UserAgent.Clear();
                httpClient.DefaultRequestHeaders.UserAgent.ParseAdd(result.UserAgent);
            });

            return(result);
        }
예제 #5
0
        /// <summary>
        /// Solves cloudflare challenge protecting a specific website.
        /// </summary>
        /// <param name="httpClient">HttpClient to use in challenge solving process.</param>
        /// <param name="httpClientHandler">HttpClientHandler of the HttpClient.</param>
        /// <param name="siteUrl">Uri of the website.</param>
        /// <param name="userAgent">The user-agent which will be used to solve the challenge.</param>
        /// <param name="cancellationToken">CancellationToken to contol solving operation cancelling.</param>
        public async Task <SolveResult> Solve(HttpClient httpClient, HttpClientHandler httpClientHandler, Uri siteUrl, string userAgent, [Optional] CancellationToken cancellationToken)
        {
            SolveResult result = default(SolveResult);

            await _locker.LockAsync(async() =>
            {
                this.userAgent        = userAgent;
                var cloudflareHandler = new CloudflareHandler(httpClientHandler, userAgent);
                result = await Solve(httpClient, cloudflareHandler, siteUrl, cancellationToken);

                httpClient.DefaultRequestHeaders.UserAgent.Clear();
                httpClient.DefaultRequestHeaders.UserAgent.ParseAdd(result.UserAgent);
            });

            return(result);
        }
예제 #6
0
        internal async Task <SolveResult> Solve(HttpClient httpClient, CloudflareHandler cloudflareHandler, Uri siteUrl, CancellationToken?cancellationToken = null)
        {
            var result = default(SolveResult);

            _httpClient        = httpClient.Clone(_cloudflareHandler);
            _siteUrl           = siteUrl;
            _cancellationToken = cancellationToken;

            result = await SolveWithJavascript(MaxTries - (CaptchaSolvingEnabled ? MaxCaptchaTries : 0));

            if (CaptchaSolvingEnabled)
            {
                result = await SolveWithCaptcha();
            }

            ClearSessionVariables();

            return(result);
        }
        internal static async Task <DetectResult> Detect(HttpClient httpClient, CloudflareHandler cloudflareHandler, Uri targetUri, bool requireHttps = false)
        {
            var _httpClient = httpClient.Clone(cloudflareHandler);

            if (!requireHttps)
            {
                targetUri = targetUri.ForceHttp();
            }

            var detectResult = await Detect(_httpClient, targetUri);

            if (detectResult.Protection.Equals(CloudflareProtection.Unknown) && !detectResult.SupportsHttp)
            {
                targetUri = targetUri.ForceHttps();

                detectResult = await Detect(_httpClient, targetUri);
            }

            return(detectResult);
        }
예제 #8
0
        /// <summary>
        /// Solves cloudflare challenge protecting a specific website.
        /// </summary>
        /// <param name="siteUrl">Uri of the website.</param>
        /// <param name="userAgent">The user-agent which will be used to solve the challenge.</param>
        /// <param name="proxy">Proxy to use while solving the challenge.</param>
        /// <param name="cancellationToken">CancellationToken to contol solving operation cancelling.</param>
        public async Task <SolveResult> Solve(Uri siteUrl, string userAgent, [Optional] IWebProxy proxy, [Optional] CancellationToken cancellationToken)
        {
            SolveResult result = default(SolveResult);

            await _locker.LockAsync(async() =>
            {
                this.userAgent    = userAgent;
                cloudflareHandler = new CloudflareHandler(userAgent);
                cloudflareHandler.HttpClientHandler.Proxy = proxy;
                httpClient             = new HttpClient(cloudflareHandler);
                this.siteUrl           = siteUrl;
                this.cancellationToken = cancellationToken;

                result = await Solve();

                httpClient.Dispose();
                captchaDetectResults.Clear();
            });

            return(result);
        }
예제 #9
0
        /// <summary>
        /// Solves cloudflare challenge protecting a specific website.
        /// </summary>
        /// <param name="siteUrl">Uri of the website.</param>
        /// <param name="proxy">Proxy to use while solving the challenge.</param>
        /// <param name="cancellationToken">CancellationToken to contol solving operation cancelling.</param>
        /// <param name="randomUserAgent">Use a new random user-agent.</param>
        public async Task <SolveResult> Solve(Uri siteUrl, [Optional] IWebProxy proxy, [Optional] CancellationToken cancellationToken, bool randomUserAgent = false)
        {
            SolveResult result = default(SolveResult);

            await _locker.LockAsync(async() =>
            {
                userAgent         = randomUserAgent ? Utils.GetGenerateRandomUserAgent() : defaultUserAgent;
                cloudflareHandler = new CloudflareHandler(userAgent);
                cloudflareHandler.HttpClientHandler.Proxy = proxy;
                httpClient             = new HttpClient(cloudflareHandler);
                this.siteUrl           = siteUrl;
                this.cancellationToken = cancellationToken;

                result = await Solve();

                httpClient.Dispose();
                captchaDetectResults.Clear();
            });

            return(result);
        }
예제 #10
0
 public async Task <SolveResult> Solve(HttpClient httpClient, HttpClientHandler httpClientHandler, Uri siteUrl, CancellationToken?cancellationToken = null)
 {
     _cloudflareHandler = new CloudflareHandler(httpClientHandler);
     return(await Solve(httpClient, _cloudflareHandler, siteUrl, cancellationToken));
 }
        public static async Task <DetectResult> Detect(HttpClient httpClient, HttpClientHandler httpClientHandler, Uri targetUri, bool requireHttps = false)
        {
            var cloudflareHandler = new CloudflareHandler(httpClientHandler);

            return(await Detect(httpClient, cloudflareHandler, targetUri, requireHttps));
        }