#21 Fix cloudflare passing

This commit is contained in:
Koitharu
2021-04-11 11:33:04 +03:00
parent 012416c881
commit 4d1f5e22d3
5 changed files with 15 additions and 23 deletions

View File

@@ -3,18 +3,16 @@ package org.koitharu.kotatsu.browser.cloudflare
import android.graphics.Bitmap
import android.webkit.WebView
import okhttp3.HttpUrl.Companion.toHttpUrl
import org.koitharu.kotatsu.core.network.AndroidCookieJar
import org.koitharu.kotatsu.core.network.CookieJar
import org.koitharu.kotatsu.core.network.WebViewClientCompat
class CloudFlareClient(
private val cookieJar: AndroidCookieJar,
private val cookieJar: CookieJar,
private val callback: CloudFlareCallback,
private val targetUrl: String
) : WebViewClientCompat() {
init {
cookieJar.remove(targetUrl, CF_UID, CF_CLEARANCE)
}
private val oldClearance = getCookieValue(CF_CLEARANCE)
override fun onPageStarted(view: WebView, url: String?, favicon: Bitmap?) {
super.onPageStarted(view, url, favicon)
@@ -32,16 +30,19 @@ class CloudFlareClient(
}
private fun checkClearance() {
val cookies = cookieJar.loadForRequest(targetUrl.toHttpUrl())
if (cookies.any { it.name == CF_CLEARANCE }) {
val clearance = getCookieValue(CF_CLEARANCE)
if (clearance != null && clearance != oldClearance) {
callback.onCheckPassed()
}
}
private fun getCookieValue(name: String): String? {
return cookieJar.loadForRequest(targetUrl.toHttpUrl())
.find { it.name == name }?.value
}
private companion object {
const val CF_UID = "__cfduid"
const val CF_CLEARANCE = "cf_clearance"
}
}