From baaaa00e8411296384977808b9f9b846366a5531 Mon Sep 17 00:00:00 2001 From: Claudemirovsky <63046606+Claudemirovsky@users.noreply.github.com> Date: Sun, 14 Jan 2024 08:31:43 -0300 Subject: [PATCH] fix(tr/mangatr): Fix popular/latest pages + fix http 403 (#244) * fix: Update requestPath - Fixes popular/latest tabs * fix: Fix HTTP 403 - Steal DDoSGuardInterceptor from all/akuma * chore: Bump version --- .../mangatr/src/DDoSGuardInterceptor.kt | 68 +++++++++++++++++++ .../overrides/fmreader/mangatr/src/MangaTR.kt | 9 +++ .../multisrc/fmreader/FMReaderGenerator.kt | 2 +- 3 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 multisrc/overrides/fmreader/mangatr/src/DDoSGuardInterceptor.kt diff --git a/multisrc/overrides/fmreader/mangatr/src/DDoSGuardInterceptor.kt b/multisrc/overrides/fmreader/mangatr/src/DDoSGuardInterceptor.kt new file mode 100644 index 0000000000..4e154b3cea --- /dev/null +++ b/multisrc/overrides/fmreader/mangatr/src/DDoSGuardInterceptor.kt @@ -0,0 +1,68 @@ +package eu.kanade.tachiyomi.extension.tr.mangatr + +import android.webkit.CookieManager +import eu.kanade.tachiyomi.network.GET +import okhttp3.Cookie +import okhttp3.HttpUrl +import okhttp3.Interceptor +import okhttp3.OkHttpClient +import okhttp3.Response + +// Taken from src/all/akuma. +class DDoSGuardInterceptor(private val client: OkHttpClient) : Interceptor { + + private val cookieManager by lazy { CookieManager.getInstance() } + + override fun intercept(chain: Interceptor.Chain): Response { + val originalRequest = chain.request() + val response = chain.proceed(originalRequest) + + // Check if DDos-GUARD is on + if (response.code !in ERROR_CODES || response.header("Server") !in SERVER_CHECK) { + return response + } + + val cookies = cookieManager.getCookie(originalRequest.url.toString()) + val oldCookie = if (cookies != null && cookies.isNotEmpty()) { + cookies.split(";").mapNotNull { Cookie.parse(originalRequest.url, it) } + } else { + emptyList() + } + val ddg2Cookie = oldCookie.firstOrNull { it.name == "__ddg2_" } + if (!ddg2Cookie?.value.isNullOrEmpty()) { + return response + } + + response.close() + + val newCookie = getNewCookie(originalRequest.url) + ?: return chain.proceed(originalRequest) + + val newCookieHeader = (oldCookie + newCookie) + .joinToString("; ") { "${it.name}=${it.value}" } + + val modifiedRequest = originalRequest.newBuilder() + .header("cookie", newCookieHeader) + .build() + + return chain.proceed(modifiedRequest) + } + + private fun getNewCookie(url: HttpUrl): Cookie? { + val wellKnown = client.newCall(GET(wellKnownUrl)) + .execute().body.string() + .substringAfter("'", "") + .substringBefore("'", "") + val checkUrl = "${url.scheme}://${url.host + wellKnown}" + val response = client.newCall(GET(checkUrl)).execute() + return response.header("set-cookie")?.let { + Cookie.parse(url, it) + } + } + + companion object { + private const val wellKnownUrl = "https://check.ddos-guard.net/check.js" + private val ERROR_CODES = listOf(403) + private val SERVER_CHECK = listOf("ddos-guard") + } +} diff --git a/multisrc/overrides/fmreader/mangatr/src/MangaTR.kt b/multisrc/overrides/fmreader/mangatr/src/MangaTR.kt index 1575cd24a5..905f73e107 100644 --- a/multisrc/overrides/fmreader/mangatr/src/MangaTR.kt +++ b/multisrc/overrides/fmreader/mangatr/src/MangaTR.kt @@ -19,6 +19,15 @@ class MangaTR : FMReader("Manga-TR", "https://manga-tr.com", "tr") { override fun headersBuilder() = super.headersBuilder() .add("Accept-Language", "en-US,en;q=0.5") + override val client by lazy { + super.client.newBuilder() + .addInterceptor(DDoSGuardInterceptor(super.client)) + .build() + } + + override val requestPath = "manga-list-sayfala.html" + + // ============================== Popular =============================== override fun popularMangaNextPageSelector() = "div.btn-group:not(div.btn-block) button.btn-info" // =============================== Search =============================== diff --git a/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/fmreader/FMReaderGenerator.kt b/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/fmreader/FMReaderGenerator.kt index 7b635ddc34..a15d45cdc3 100644 --- a/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/fmreader/FMReaderGenerator.kt +++ b/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/fmreader/FMReaderGenerator.kt @@ -14,7 +14,7 @@ class FMReaderGenerator : ThemeSourceGenerator { override val sources = listOf( SingleLang("Epik Manga", "https://www.epikmanga.com", "tr"), SingleLang("KissLove", "https://klz9.com", "ja", isNsfw = true, overrideVersionCode = 4), - SingleLang("Manga-TR", "https://manga-tr.com", "tr", className = "MangaTR", overrideVersionCode = 2), + SingleLang("Manga-TR", "https://manga-tr.com", "tr", className = "MangaTR", overrideVersionCode = 3), SingleLang("ManhuaRock", "https://manhuarock.net", "vi", overrideVersionCode = 1), SingleLang("Say Truyen", "https://saytruyenvip.com", "vi", overrideVersionCode = 3), SingleLang("WeLoveManga", "https://weloma.art", "ja", pkgName = "rawlh", isNsfw = true, overrideVersionCode = 5),