#21 Fix cloudflare passing

pull/26/head
Koitharu 5 years ago
parent 012416c881
commit 4d1f5e22d3

@ -74,7 +74,7 @@ dependencies {
implementation 'androidx.lifecycle:lifecycle-process:2.3.1' implementation 'androidx.lifecycle:lifecycle-process:2.3.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4' implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0' implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'
implementation 'androidx.recyclerview:recyclerview:1.2.0-rc01' implementation 'androidx.recyclerview:recyclerview:1.2.0'
implementation 'androidx.viewpager2:viewpager2:1.1.0-alpha01' implementation 'androidx.viewpager2:viewpager2:1.1.0-alpha01'
implementation 'androidx.preference:preference-ktx:1.1.1' implementation 'androidx.preference:preference-ktx:1.1.1'
implementation 'androidx.work:work-runtime-ktx:2.5.0' implementation 'androidx.work:work-runtime-ktx:2.5.0'
@ -99,7 +99,7 @@ dependencies {
implementation 'com.davemorrissey.labs:subsampling-scale-image-view-androidx:3.10.0' implementation 'com.davemorrissey.labs:subsampling-scale-image-view-androidx:3.10.0'
implementation 'com.github.solkin:disk-lru-cache:1.2' implementation 'com.github.solkin:disk-lru-cache:1.2'
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.6' debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.7'
testImplementation 'junit:junit:4.13.2' testImplementation 'junit:junit:4.13.2'
testImplementation 'org.json:json:20201115' testImplementation 'org.json:json:20201115'

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

@ -7,7 +7,7 @@ import okhttp3.HttpUrl
import kotlin.coroutines.resume import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine import kotlin.coroutines.suspendCoroutine
class AndroidCookieJar : CookieJar { class CookieJar : CookieJar {
private val cookieManager = CookieManager.getInstance() private val cookieManager = CookieManager.getInstance()
@ -28,15 +28,6 @@ class AndroidCookieJar : CookieJar {
} }
} }
fun remove(url: String, vararg names: String) {
val cookies = cookieManager.getCookie(url) ?: return
val newCookies = cookies.split(";")
.filterNot { cookie ->
names.any { cookie.startsWith("$it=") }
}.joinToString(";")
cookieManager.setCookie(url, newCookies)
}
fun clearAsync() { fun clearAsync() {
cookieManager.removeAllCookies(null) cookieManager.removeAllCookies(null)
} }

@ -11,7 +11,7 @@ import java.util.concurrent.TimeUnit
val networkModule val networkModule
get() = module { get() = module {
single { AndroidCookieJar() } bind CookieJar::class single { CookieJar() } bind CookieJar::class
single(named(CacheUtils.QUALIFIER_HTTP)) { CacheUtils.createHttpCache(androidContext()) } single(named(CacheUtils.QUALIFIER_HTTP)) { CacheUtils.createHttpCache(androidContext()) }
single { single {
OkHttpClient.Builder().apply { OkHttpClient.Builder().apply {

@ -11,7 +11,7 @@ import org.koin.android.ext.android.get
import org.koin.android.ext.android.inject import org.koin.android.ext.android.inject
import org.koitharu.kotatsu.R import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.base.ui.BasePreferenceFragment import org.koitharu.kotatsu.base.ui.BasePreferenceFragment
import org.koitharu.kotatsu.core.network.AndroidCookieJar import org.koitharu.kotatsu.core.network.CookieJar
import org.koitharu.kotatsu.core.prefs.AppSettings import org.koitharu.kotatsu.core.prefs.AppSettings
import org.koitharu.kotatsu.local.data.Cache import org.koitharu.kotatsu.local.data.Cache
import org.koitharu.kotatsu.search.ui.MangaSuggestionsProvider import org.koitharu.kotatsu.search.ui.MangaSuggestionsProvider
@ -75,7 +75,7 @@ class HistorySettingsFragment : BasePreferenceFragment(R.string.history_and_cach
} }
AppSettings.KEY_COOKIES_CLEAR -> { AppSettings.KEY_COOKIES_CLEAR -> {
viewLifecycleScope.launch { viewLifecycleScope.launch {
val cookieJar = get<AndroidCookieJar>() val cookieJar = get<CookieJar>()
cookieJar.clear() cookieJar.clear()
Snackbar.make( Snackbar.make(
listView ?: return@launch, listView ?: return@launch,

Loading…
Cancel
Save