mirror of
https://github.com/null2264/yokai.git
synced 2025-06-21 10:44:42 +00:00
Handle HTTP 403 responses with CloudflareInterceptor
also updating from handler in that class Co-Authored-By: jmir1 <43830312+jmir1@users.noreply.github.com> Co-Authored-By: arkon <4098258+arkon@users.noreply.github.com>
This commit is contained in:
parent
2230e69e8c
commit
7d3138ca67
1 changed files with 8 additions and 7 deletions
|
@ -2,11 +2,10 @@ package eu.kanade.tachiyomi.network.interceptor
|
|||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.Context
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.webkit.WebSettings
|
||||
import android.webkit.WebView
|
||||
import android.widget.Toast
|
||||
import androidx.core.content.ContextCompat
|
||||
import eu.kanade.tachiyomi.R
|
||||
import eu.kanade.tachiyomi.network.NetworkHelper
|
||||
import eu.kanade.tachiyomi.source.online.HttpSource
|
||||
|
@ -28,7 +27,7 @@ import java.util.concurrent.TimeUnit
|
|||
|
||||
class CloudflareInterceptor(private val context: Context) : Interceptor {
|
||||
|
||||
private val handler = Handler(Looper.getMainLooper())
|
||||
private val executor = ContextCompat.getMainExecutor(context)
|
||||
|
||||
private val networkHelper: NetworkHelper by injectLazy()
|
||||
|
||||
|
@ -57,7 +56,7 @@ class CloudflareInterceptor(private val context: Context) : Interceptor {
|
|||
val response = chain.proceed(originalRequest)
|
||||
|
||||
// Check if Cloudflare anti-bot is on
|
||||
if (response.code != 503 || response.header("Server") !in SERVER_CHECK) {
|
||||
if (response.code !in ERROR_CODES || response.header("Server") !in SERVER_CHECK) {
|
||||
return response
|
||||
}
|
||||
|
||||
|
@ -92,7 +91,7 @@ class CloudflareInterceptor(private val context: Context) : Interceptor {
|
|||
val headers = request.headers.toMultimap().mapValues { it.value.getOrNull(0) ?: "" }.toMutableMap()
|
||||
headers["X-Requested-With"] = WebViewUtil.REQUESTED_WITH
|
||||
|
||||
handler.post {
|
||||
executor.execute {
|
||||
val webview = WebView(context)
|
||||
webView = webview
|
||||
webview.setDefaultSettings()
|
||||
|
@ -128,7 +127,7 @@ class CloudflareInterceptor(private val context: Context) : Interceptor {
|
|||
isMainFrame: Boolean
|
||||
) {
|
||||
if (isMainFrame) {
|
||||
if (errorCode == 503) {
|
||||
if (errorCode in ERROR_CODES) {
|
||||
// Found the Cloudflare challenge page.
|
||||
challengeFound = true
|
||||
} else {
|
||||
|
@ -146,13 +145,14 @@ class CloudflareInterceptor(private val context: Context) : Interceptor {
|
|||
// around 4 seconds but it can take more due to slow networks or server issues.
|
||||
latch.await(12, TimeUnit.SECONDS)
|
||||
|
||||
handler.post {
|
||||
executor.execute {
|
||||
if (!cloudflareBypassed) {
|
||||
isWebViewOutdated = webView?.isOutdated() == true
|
||||
}
|
||||
|
||||
webView?.stopLoading()
|
||||
webView?.destroy()
|
||||
webView = null
|
||||
}
|
||||
|
||||
// Throw exception if we failed to bypass Cloudflare
|
||||
|
@ -167,6 +167,7 @@ class CloudflareInterceptor(private val context: Context) : Interceptor {
|
|||
}
|
||||
|
||||
companion object {
|
||||
private val ERROR_CODES = listOf(403, 503)
|
||||
private val SERVER_CHECK = arrayOf("cloudflare-nginx", "cloudflare")
|
||||
private val COOKIE_NAMES = listOf("cf_clearance")
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue