From f682d727b9f19e4a1003e508a98b730503569bc0 Mon Sep 17 00:00:00 2001 From: Chopper <156493704+choppeh@users.noreply.github.com> Date: Sat, 24 Aug 2024 23:16:37 -0300 Subject: [PATCH] DiskusScan: Fix loading content (#4738) * Fix loading content * Bump version --- src/pt/diskusscan/build.gradle | 2 +- .../extension/pt/diskusscan/DiskusScan.kt | 113 +++++++++++++++--- 2 files changed, 96 insertions(+), 19 deletions(-) diff --git a/src/pt/diskusscan/build.gradle b/src/pt/diskusscan/build.gradle index b1ad41c56..839075e03 100644 --- a/src/pt/diskusscan/build.gradle +++ b/src/pt/diskusscan/build.gradle @@ -3,7 +3,7 @@ ext { extClass = '.DiskusScan' themePkg = 'mangathemesia' baseUrl = 'https://diskusscan.com' - overrideVersionCode = 11 + overrideVersionCode = 12 } apply from: "$rootDir/common.gradle" diff --git a/src/pt/diskusscan/src/eu/kanade/tachiyomi/extension/pt/diskusscan/DiskusScan.kt b/src/pt/diskusscan/src/eu/kanade/tachiyomi/extension/pt/diskusscan/DiskusScan.kt index 3762875e8..e85c607d4 100644 --- a/src/pt/diskusscan/src/eu/kanade/tachiyomi/extension/pt/diskusscan/DiskusScan.kt +++ b/src/pt/diskusscan/src/eu/kanade/tachiyomi/extension/pt/diskusscan/DiskusScan.kt @@ -1,13 +1,26 @@ package eu.kanade.tachiyomi.extension.pt.diskusscan +import android.annotation.SuppressLint +import android.app.Application +import android.os.Build +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 androidx.annotation.RequiresApi import eu.kanade.tachiyomi.multisrc.mangathemesia.MangaThemesia import eu.kanade.tachiyomi.network.GET import eu.kanade.tachiyomi.network.interceptor.rateLimit -import eu.kanade.tachiyomi.source.model.Page import eu.kanade.tachiyomi.source.model.SManga +import okhttp3.Headers import okhttp3.Request +import uy.kohesive.injekt.Injekt +import uy.kohesive.injekt.api.get import java.text.SimpleDateFormat import java.util.Locale +import java.util.concurrent.CountDownLatch import java.util.concurrent.TimeUnit class DiskusScan : MangaThemesia( @@ -20,18 +33,93 @@ class DiskusScan : MangaThemesia( // Changed their theme from Madara to MangaThemesia. override val versionId = 2 + private var challengeHeaders: Headers? = null + override val client = super.client.newBuilder() .addInterceptor { chain -> - val request = chain.request() - val headers = request.headers.newBuilder() - .set("Accept-Encoding", "") - .build() - chain.proceed(request.newBuilder().headers(headers).build()) + if (challengeHeaders != null) { + val request = chain.request().newBuilder() + .headers(challengeHeaders!!) + .build() + + val response = chain.proceed(request) + + if (response.isSuccessful) { + return@addInterceptor response + } + } + + chain.proceed(resolveChallenge(chain.request())) } - .rateLimit(2, 1, TimeUnit.SECONDS) + .rateLimit(1, 2, TimeUnit.SECONDS) .build() + @SuppressLint("SetJavaScriptEnabled") + private fun resolveChallenge(origin: Request): Request { + val latch = CountDownLatch(1) + val headers = Headers.Builder() + + Handler(Looper.getMainLooper()).post { + val webView = WebView(Injekt.get()) + with(webView.settings) { + javaScriptEnabled = true + blockNetworkImage = true + } + webView.webViewClient = object : WebViewClient() { + override fun onPageFinished(view: WebView?, url: String) { + view?.apply { + evaluateJavascript("document.documentElement.outerHTML") { html -> + if ("challenge" in html) { + return@evaluateJavascript + } + latch.countDown() + } + } + } + + @RequiresApi(Build.VERSION_CODES.N) + override fun shouldInterceptRequest( + view: WebView?, + request: WebResourceRequest, + ): WebResourceResponse? { + val ignore = listOf(".css", ".js", ".php", ".ico", "google", "fonts") + val url = request.url.toString() + if (ignore.any { url.contains(it, ignoreCase = true) }) { + return emptyResource() + } + + if (request.isOriginRequest()) { + for ((key, value) in request.requestHeaders) { + headers[key] = value + } + } + return super.shouldInterceptRequest(view, request) + } + + private fun WebResourceRequest.isOriginRequest(): Boolean = + this.url.toString().equals(origin.url.toString(), true) + + private fun emptyResource() = WebResourceResponse(null, null, null) + } + webView.loadUrl(origin.url.toString()) + } + + latch.await(client.callTimeoutMillis.toLong(), TimeUnit.MILLISECONDS) + + challengeHeaders = origin.headers.newBuilder().let { + for ((key, value) in headers.build()) { + it[key] = value + } + it.build() + } + + return origin.newBuilder() + .headers(challengeHeaders!!) + .build() + } + override fun headersBuilder() = super.headersBuilder() + .set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7") .set("Accept-Language", "pt-BR,en-US;q=0.7,en;q=0.3") .set("Alt-Used", baseUrl.substringAfterLast("/")) .set("Sec-Fetch-Dest", "document") @@ -60,15 +148,4 @@ class DiskusScan : MangaThemesia( // ============================== Chapters ============================== override fun chapterListRequest(manga: SManga) = mangaDetailsRequest(manga) - - // =============================== Pages ================================ - override fun imageUrlRequest(page: Page): Request { - val newHeaders = super.imageUrlRequest(page).headers.newBuilder() - .set("Sec-Fetch-Dest", "image") - .set("Sec-Fetch-Mode", "no-cors") - .set("Sec-Fetch-Site", "cross-site") - .build() - - return GET(page.imageUrl!!, newHeaders) - } }