From 701b46e29acdaafd156017b07227a2a3a9af2c2f Mon Sep 17 00:00:00 2001 From: SonoPG <38359416+PGgamer2@users.noreply.github.com> Date: Sat, 1 Feb 2025 13:31:18 +0100 Subject: [PATCH] MangaWorld: Fix MWCookie requirement (#7319) * Create ShittyRedirectionInterceptor.kt * Add redirection interceptor * Update version code * Don't be lazy * Avoid NullPointerExceptions when searching for MWCookie * Update MangaWorld.kt with new name for interceptor * Rename class and store regex somewhere else * Check for content-type instead of header and return res correctly * Use .contains to not fail on longer types * Revert "vary" method to check for the JS challenge The "text/html" method breaks way too easily, the response.headers["vary"] just works fine. --- lib-multisrc/mangaworld/build.gradle.kts | 2 +- .../mangaworld/CookieRedirectInterceptor.kt | 35 +++++++++++++++++++ .../multisrc/mangaworld/MangaWorld.kt | 6 +++- 3 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 lib-multisrc/mangaworld/src/eu/kanade/tachiyomi/multisrc/mangaworld/CookieRedirectInterceptor.kt diff --git a/lib-multisrc/mangaworld/build.gradle.kts b/lib-multisrc/mangaworld/build.gradle.kts index 9dce2478c..e2f11e9c1 100644 --- a/lib-multisrc/mangaworld/build.gradle.kts +++ b/lib-multisrc/mangaworld/build.gradle.kts @@ -2,4 +2,4 @@ plugins { id("lib-multisrc") } -baseVersionCode = 2 +baseVersionCode = 3 diff --git a/lib-multisrc/mangaworld/src/eu/kanade/tachiyomi/multisrc/mangaworld/CookieRedirectInterceptor.kt b/lib-multisrc/mangaworld/src/eu/kanade/tachiyomi/multisrc/mangaworld/CookieRedirectInterceptor.kt new file mode 100644 index 000000000..8aeb1cb8c --- /dev/null +++ b/lib-multisrc/mangaworld/src/eu/kanade/tachiyomi/multisrc/mangaworld/CookieRedirectInterceptor.kt @@ -0,0 +1,35 @@ +package eu.kanade.tachiyomi.multisrc.mangaworld + +import eu.kanade.tachiyomi.network.GET +import okhttp3.Cookie +import okhttp3.Interceptor +import okhttp3.OkHttpClient +import okhttp3.Request +import okhttp3.Response +import okhttp3.ResponseBody.Companion.toResponseBody + +class CookieRedirectInterceptor(private val client: OkHttpClient) : Interceptor { + private val cookieRegex = Regex("""document\.cookie="(MWCookie[^"]+)""") + + override fun intercept(chain: Interceptor.Chain): Response { + val request = chain.request() + val response = chain.proceed(request) + // ignore requests that already have completed the JS challenge + if (response.headers["vary"] != null) return response + + val content = response.body.string() + val results = cookieRegex.find(content) + ?: return response.newBuilder().body(content.toResponseBody(response.body.contentType())).build() + val (cookieString) = results.destructured + return chain.proceed(loadCookie(request, cookieString)) + } + + private fun loadCookie(request: Request, cookieString: String): Request { + val cookie = Cookie.parse(request.url, cookieString)!! + client.cookieJar.saveFromResponse(request.url, listOf(cookie)) + val headers = request.headers.newBuilder() + .add("Cookie", cookie.toString()) + .build() + return GET(request.url, headers) + } +} diff --git a/lib-multisrc/mangaworld/src/eu/kanade/tachiyomi/multisrc/mangaworld/MangaWorld.kt b/lib-multisrc/mangaworld/src/eu/kanade/tachiyomi/multisrc/mangaworld/MangaWorld.kt index ab358e55f..e6610e652 100644 --- a/lib-multisrc/mangaworld/src/eu/kanade/tachiyomi/multisrc/mangaworld/MangaWorld.kt +++ b/lib-multisrc/mangaworld/src/eu/kanade/tachiyomi/multisrc/mangaworld/MangaWorld.kt @@ -28,7 +28,11 @@ abstract class MangaWorld( ) : ParsedHttpSource() { override val supportsLatest = true - override val client: OkHttpClient = network.cloudflareClient + + // CookieRedirectInterceptor extracts MWCookie from the page's JS code, applies it and then redirects to the page + override val client: OkHttpClient = network.cloudflareClient.newBuilder() + .addInterceptor(CookieRedirectInterceptor(network.cloudflareClient)) + .build() companion object { protected val CHAPTER_NUMBER_REGEX by lazy { Regex("""(?i)capitolo\s([0-9]+)""") }