diff --git a/src/en/infinityscans/build.gradle b/src/en/infinityscans/build.gradle index e0189e226..977e9767a 100644 --- a/src/en/infinityscans/build.gradle +++ b/src/en/infinityscans/build.gradle @@ -1,7 +1,7 @@ ext { extName = 'InfinityScans' extClass = '.InfinityScans' - extVersionCode = 1 + extVersionCode = 2 isNsfw = true } diff --git a/src/en/infinityscans/src/eu/kanade/tachiyomi/extension/en/infinityscans/InfinityScans.kt b/src/en/infinityscans/src/eu/kanade/tachiyomi/extension/en/infinityscans/InfinityScans.kt index 792bad4eb..f745fb99e 100644 --- a/src/en/infinityscans/src/eu/kanade/tachiyomi/extension/en/infinityscans/InfinityScans.kt +++ b/src/en/infinityscans/src/eu/kanade/tachiyomi/extension/en/infinityscans/InfinityScans.kt @@ -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() diff --git a/src/en/infinityscans/src/eu/kanade/tachiyomi/extension/en/infinityscans/WebviewInterceptor.kt b/src/en/infinityscans/src/eu/kanade/tachiyomi/extension/en/infinityscans/WebviewInterceptor.kt new file mode 100644 index 000000000..570f7bee3 --- /dev/null +++ b/src/en/infinityscans/src/eu/kanade/tachiyomi/extension/en/infinityscans/WebviewInterceptor.kt @@ -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 + } + } +}