Infinityscans, try to solve cloudflare turnstile in webview (#821)
solve cloudflare turnstile in webview
This commit is contained in:
parent
5f8099de05
commit
220a70b423
|
@ -1,7 +1,7 @@
|
||||||
ext {
|
ext {
|
||||||
extName = 'InfinityScans'
|
extName = 'InfinityScans'
|
||||||
extClass = '.InfinityScans'
|
extClass = '.InfinityScans'
|
||||||
extVersionCode = 1
|
extVersionCode = 2
|
||||||
isNsfw = true
|
isNsfw = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -23,7 +23,6 @@ import okhttp3.RequestBody.Companion.toRequestBody
|
||||||
import okhttp3.Response
|
import okhttp3.Response
|
||||||
import org.jsoup.nodes.Element
|
import org.jsoup.nodes.Element
|
||||||
import uy.kohesive.injekt.injectLazy
|
import uy.kohesive.injekt.injectLazy
|
||||||
import java.io.IOException
|
|
||||||
|
|
||||||
class InfinityScans : HttpSource() {
|
class InfinityScans : HttpSource() {
|
||||||
|
|
||||||
|
@ -37,14 +36,7 @@ class InfinityScans : HttpSource() {
|
||||||
override val supportsLatest = true
|
override val supportsLatest = true
|
||||||
|
|
||||||
override val client: OkHttpClient = network.cloudflareClient.newBuilder()
|
override val client: OkHttpClient = network.cloudflareClient.newBuilder()
|
||||||
.addInterceptor { chain ->
|
.addInterceptor(WebviewInterceptor(baseUrl))
|
||||||
val response = chain.proceed(chain.request())
|
|
||||||
if (response.code == 401) {
|
|
||||||
response.close()
|
|
||||||
throw IOException("Solve Captcha in WebView")
|
|
||||||
}
|
|
||||||
response
|
|
||||||
}
|
|
||||||
.rateLimit(1)
|
.rateLimit(1)
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue