From a0d1c5c565a23142b042a107f39b89dc57678870 Mon Sep 17 00:00:00 2001 From: Alessandro Jean <14254807+alessandrojean@users.noreply.github.com> Date: Wed, 5 Oct 2022 23:39:52 -0300 Subject: [PATCH] Use URL fragment in MangaPlus. (#13722) --- src/all/mangaplus/build.gradle | 2 +- .../extension/all/mangaplus/MangaPlus.kt | 64 +++++-------------- 2 files changed, 18 insertions(+), 48 deletions(-) diff --git a/src/all/mangaplus/build.gradle b/src/all/mangaplus/build.gradle index bcfdf5ac8..4ad79686c 100644 --- a/src/all/mangaplus/build.gradle +++ b/src/all/mangaplus/build.gradle @@ -6,7 +6,7 @@ ext { extName = 'MANGA Plus by SHUEISHA' pkgNameSuffix = 'all.mangaplus' extClass = '.MangaPlusFactory' - extVersionCode = 37 + extVersionCode = 38 } apply from: "$rootDir/common.gradle" diff --git a/src/all/mangaplus/src/eu/kanade/tachiyomi/extension/all/mangaplus/MangaPlus.kt b/src/all/mangaplus/src/eu/kanade/tachiyomi/extension/all/mangaplus/MangaPlus.kt index 230bc2bf8..e3c4406eb 100644 --- a/src/all/mangaplus/src/eu/kanade/tachiyomi/extension/all/mangaplus/MangaPlus.kt +++ b/src/all/mangaplus/src/eu/kanade/tachiyomi/extension/all/mangaplus/MangaPlus.kt @@ -315,8 +315,7 @@ class MangaPlus( return result.success.mangaViewer!!.pages .mapNotNull(MangaPlusPage::mangaPage) .mapIndexed { i, page -> - val encryptionKey = if (page.encryptionKey == null) "" else - "&encryptionKey=${page.encryptionKey}" + val encryptionKey = if (page.encryptionKey == null) "" else "#${page.encryptionKey}" Page(i, referer, page.imageUrl + encryptionKey) } } @@ -342,16 +341,6 @@ class MangaPlus( entryValues = QUALITY_PREF_ENTRY_VALUES setDefaultValue(QUALITY_PREF_DEFAULT_VALUE) summary = "%s" - - setOnPreferenceChangeListener { _, newValue -> - val selected = newValue as String - val index = findIndexOfValue(selected) - val entry = entryValues[index] as String - - preferences.edit() - .putString("${QUALITY_PREF_KEY}_$lang", entry) - .commit() - } } val splitPref = SwitchPreferenceCompat(screen.context).apply { @@ -359,14 +348,6 @@ class MangaPlus( title = intl.splitDoublePages summary = intl.splitDoublePagesSummary setDefaultValue(SPLIT_PREF_DEFAULT_VALUE) - - setOnPreferenceChangeListener { _, newValue -> - val checkValue = newValue as Boolean - - preferences.edit() - .putBoolean("${SPLIT_PREF_KEY}_$lang", checkValue) - .commit() - } } screen.addPreference(qualityPref) @@ -374,25 +355,16 @@ class MangaPlus( } private fun imageIntercept(chain: Interceptor.Chain): Response { - var request = chain.request() - - if (request.url.queryParameter("encryptionKey") == null) - return chain.proceed(request) - - val encryptionKey = request.url.queryParameter("encryptionKey")!! - - // Change the url and remove the encryptionKey to avoid detection. - val newUrl = request.url.newBuilder() - .removeAllQueryParameters("encryptionKey") - .build() - request = request.newBuilder() - .url(newUrl) - .build() - + val request = chain.request() val response = chain.proceed(request) + val encryptionKey = request.url.fragment + + if (encryptionKey.isNullOrEmpty()) { + return response + } val contentType = response.header("Content-Type", "image/jpeg")!! - val image = decodeImage(encryptionKey, response.body!!.bytes()) + val image = response.body!!.bytes().decodeXorCipher(encryptionKey) val body = image.toResponseBody(contentType.toMediaTypeOrNull()) return response.newBuilder() @@ -400,17 +372,6 @@ class MangaPlus( .build() } - private fun decodeImage(encryptionKey: String, imageBytes: ByteArray): ByteArray { - val keyStream = encryptionKey - .chunked(2) - .map { it.toInt(16) } - - return imageBytes - .mapIndexed { i, byte -> byte.toInt() xor keyStream[i % keyStream.size] } - .map(Int::toByte) - .toByteArray() - } - private fun thumbnailIntercept(chain: Interceptor.Chain): Response { val request = chain.request() val response = chain.proceed(request) @@ -433,6 +394,15 @@ class MangaPlus( return response } + private fun ByteArray.decodeXorCipher(key: String): ByteArray { + val keyStream = key.chunked(2) + .map { it.toInt(16) } + + return mapIndexed { i, byte -> byte.toInt() xor keyStream[i % keyStream.size] } + .map(Int::toByte) + .toByteArray() + } + private fun Response.asMangaPlusResponse(): MangaPlusResponse = use { json.decodeFromString(body!!.string()) }