Infinityscans, try to solve cloudflare turnstile in webview (#821)

solve cloudflare turnstile in webview
This commit is contained in:
Secozzi 2024-01-31 01:22:32 +00:00 committed by GitHub
parent 3e1163e92b
commit 59db21ae35
3 changed files with 80 additions and 10 deletions

View File

@ -1,7 +1,7 @@
ext {
extName = 'InfinityScans'
extClass = '.InfinityScans'
extVersionCode = 1
extVersionCode = 2
isNsfw = true
}

View File

@ -23,7 +23,6 @@ import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.Response
import org.jsoup.nodes.Element
import uy.kohesive.injekt.injectLazy
import java.io.IOException
class InfinityScans : HttpSource() {
@ -37,14 +36,7 @@ class InfinityScans : HttpSource() {
override val supportsLatest = true
override val client: OkHttpClient = network.cloudflareClient.newBuilder()
.addInterceptor { chain ->
val response = chain.proceed(chain.request())
if (response.code == 401) {
response.close()
throw IOException("Solve Captcha in WebView")
}
response
}
.addInterceptor(WebviewInterceptor(baseUrl))
.rateLimit(1)
.build()

View File

@ -0,0 +1,78 @@
package eu.kanade.tachiyomi.extension.en.infinityscans
import android.app.Application
import android.os.Handler
import android.os.Looper
import android.webkit.WebResourceRequest
import android.webkit.WebResourceResponse
import android.webkit.WebView
import android.webkit.WebViewClient
import okhttp3.Interceptor
import okhttp3.Response
import uy.kohesive.injekt.injectLazy
import java.io.IOException
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
class WebviewInterceptor(private val baseUrl: String) : Interceptor {
private val context: Application by injectLazy()
private val handler by lazy { Handler(Looper.getMainLooper()) }
override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request()
val origRes = chain.proceed(request)
if (origRes.code != 401) return origRes
origRes.close()
resolveInWebview()
// If webview failed
val response = chain.proceed(request)
if (response.code == 401) {
response.close()
throw IOException("Solve Captcha in WebView")
}
return response
}
private fun resolveInWebview() {
val latch = CountDownLatch(1)
var webView: WebView? = null
var hasSetCookies = false
handler.post {
val webview = WebView(context)
webView = webview
with(webview.settings) {
javaScriptEnabled = true
domStorageEnabled = true
databaseEnabled = true
useWideViewPort = false
loadWithOverviewMode = false
}
webview.webViewClient = object : WebViewClient() {
override fun shouldInterceptRequest(view: WebView?, request: WebResourceRequest?): WebResourceResponse? {
if (request?.url.toString().contains("$baseUrl/ajax/turnstile")) {
hasSetCookies = true
} else if (request?.url.toString().contains(baseUrl) && hasSetCookies) {
latch.countDown()
}
return super.shouldInterceptRequest(view, request)
}
}
webview.loadUrl("$baseUrl/")
}
latch.await(20, TimeUnit.SECONDS)
handler.post {
webView?.stopLoading()
webView?.destroy()
webView = null
}
}
}