From a273be6d89d30e32156501beb979efbe5ee831c6 Mon Sep 17 00:00:00 2001 From: Blatzar <46196380+Blatzar@users.noreply.github.com> Date: Sun, 21 Aug 2022 20:51:01 +0200 Subject: [PATCH] Fix oversight when cookies are already stored in cloudflare --- .../cloudstream3/network/CloudflareKiller.kt | 38 +++++++++++-------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/com/lagradost/cloudstream3/network/CloudflareKiller.kt b/app/src/main/java/com/lagradost/cloudstream3/network/CloudflareKiller.kt index 7a759cd6..05836269 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/network/CloudflareKiller.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/network/CloudflareKiller.kt @@ -23,16 +23,21 @@ class CloudflareKiller : Interceptor { } } - val savedCookies: MutableMap = mutableMapOf() + val savedCookies: MutableMap> = mutableMapOf() override fun intercept(chain: Interceptor.Chain): Response = runBlocking { val request = chain.request() - if (savedCookies[request.url.host] == null) { + val cookies = savedCookies[request.url.host] + + if (cookies == null) { bypassCloudflare(request)?.let { Log.d(TAG, "Succeeded bypassing cloudflare: ${request.url}") return@runBlocking it } + } else { + return@runBlocking proceed(request, cookies) } + debugWarning({ true }) { "Failed cloudflare at: ${request.url}" } return@runBlocking chain.proceed(request) } @@ -49,11 +54,25 @@ class CloudflareKiller : Interceptor { // Not sure if this takes expiration into account return getWebViewCookie(request.url.toString())?.let { cookie -> cookie.contains("cf_clearance").also { solved -> - if (solved) savedCookies[request.url.host] = cookie + if (solved) savedCookies[request.url.host] = parseCookieMap(cookie) } } ?: false } + private suspend fun proceed(request: Request, cookies: Map): Response { + val userAgentMap = WebViewResolver.getWebViewUserAgent()?.let { + mapOf("user-agent" to it) + } ?: emptyMap() + + val headers = + getHeaders(request.headers.toMap() + userAgentMap, cookies + request.cookies) + return app.baseClient.newCall( + request.newBuilder() + .headers(headers) + .build() + ).await() + } + private suspend fun bypassCloudflare(request: Request): Response? { val url = request.url.toString() @@ -78,17 +97,6 @@ class CloudflareKiller : Interceptor { } val cookies = savedCookies[request.url.host] ?: return null - - val mappedCookies = parseCookieMap(cookies) - val userAgentMap = WebViewResolver.getWebViewUserAgent()?.let { - mapOf("user-agent" to it) - } ?: emptyMap() - - val headers = getHeaders(request.headers.toMap() + userAgentMap, mappedCookies + request.cookies) - return app.baseClient.newCall( - request.newBuilder() - .headers(headers) - .build() - ).await() + return proceed(request, cookies) } } \ No newline at end of file