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.
This commit is contained in:
SonoPG 2025-02-01 13:31:18 +01:00 committed by Draff
parent a0e3441507
commit 701b46e29a
No known key found for this signature in database
GPG Key ID: E8A89F3211677653
3 changed files with 41 additions and 2 deletions

View File

@ -2,4 +2,4 @@ plugins {
id("lib-multisrc") id("lib-multisrc")
} }
baseVersionCode = 2 baseVersionCode = 3

View File

@ -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)
}
}

View File

@ -28,7 +28,11 @@ abstract class MangaWorld(
) : ParsedHttpSource() { ) : ParsedHttpSource() {
override val supportsLatest = true 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 { companion object {
protected val CHAPTER_NUMBER_REGEX by lazy { Regex("""(?i)capitolo\s([0-9]+)""") } protected val CHAPTER_NUMBER_REGEX by lazy { Regex("""(?i)capitolo\s([0-9]+)""") }