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(); }
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(); }