From 3ecfb8529c2d6dd9dd8465fb2bd7fb3d5c1326be Mon Sep 17 00:00:00 2001 From: AlphaBoom <30779939+AlphaBoom@users.noreply.github.com> Date: Tue, 10 Dec 2024 22:28:10 +0800 Subject: [PATCH] PixivComic: Use salt from chapter page (#6561) --- src/ja/pixivcomic/build.gradle | 2 +- .../extension/ja/pixivcomic/PixivComic.kt | 15 ++++++++++----- .../extension/ja/pixivcomic/PixivComicUtil.kt | 8 ++++---- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/ja/pixivcomic/build.gradle b/src/ja/pixivcomic/build.gradle index af530e742..2e3069ab4 100644 --- a/src/ja/pixivcomic/build.gradle +++ b/src/ja/pixivcomic/build.gradle @@ -1,7 +1,7 @@ ext { extName = 'Pixiv Comic' extClass = '.PixivComic' - extVersionCode = 2 + extVersionCode = 3 } apply from: "$rootDir/common.gradle" diff --git a/src/ja/pixivcomic/src/eu/kanade/tachiyomi/extension/ja/pixivcomic/PixivComic.kt b/src/ja/pixivcomic/src/eu/kanade/tachiyomi/extension/ja/pixivcomic/PixivComic.kt index deeb2f7a4..ca183959f 100644 --- a/src/ja/pixivcomic/src/eu/kanade/tachiyomi/extension/ja/pixivcomic/PixivComic.kt +++ b/src/ja/pixivcomic/src/eu/kanade/tachiyomi/extension/ja/pixivcomic/PixivComic.kt @@ -8,8 +8,12 @@ import eu.kanade.tachiyomi.source.model.Page import eu.kanade.tachiyomi.source.model.SChapter import eu.kanade.tachiyomi.source.model.SManga import eu.kanade.tachiyomi.source.online.HttpSource +import eu.kanade.tachiyomi.util.asJsoup import kotlinx.serialization.decodeFromString import kotlinx.serialization.json.Json +import kotlinx.serialization.json.JsonElement +import kotlinx.serialization.json.jsonObject +import kotlinx.serialization.json.jsonPrimitive import okhttp3.HttpUrl import okhttp3.HttpUrl.Companion.toHttpUrl import okhttp3.Request @@ -39,10 +43,6 @@ class PixivComic : HttpSource() { randomString() } - private val timeAndHash by lazy { - getTimeAndHash() - } - override val client = network.cloudflareClient.newBuilder() .addInterceptor(ShuffledImageInterceptor(key)) .addNetworkInterceptor(::tagInterceptor) @@ -244,12 +244,17 @@ class PixivComic : HttpSource() { } override fun pageListRequest(chapter: SChapter): Request { + val doc = client.newCall(GET(getChapterUrl(chapter), headers)).execute().asJsoup() + val salt = doc.selectFirst("script#__NEXT_DATA__")!!.data().let { + json.decodeFromString(it).jsonObject["props"]!!.jsonObject["pageProps"]!! + .jsonObject["salt"]!!.jsonPrimitive.content + } val url = apiBuilder() .addPathSegment("episodes") .addPathSegment(chapter.url) .addPathSegment("read_v4") .build() - + val timeAndHash = getTimeAndHash(salt) val header = headers.newBuilder() .add("X-Client-Time", timeAndHash.first) .add("X-Client-Hash", timeAndHash.second) diff --git a/src/ja/pixivcomic/src/eu/kanade/tachiyomi/extension/ja/pixivcomic/PixivComicUtil.kt b/src/ja/pixivcomic/src/eu/kanade/tachiyomi/extension/ja/pixivcomic/PixivComicUtil.kt index e9a1e22f2..ac863ad32 100644 --- a/src/ja/pixivcomic/src/eu/kanade/tachiyomi/extension/ja/pixivcomic/PixivComicUtil.kt +++ b/src/ja/pixivcomic/src/eu/kanade/tachiyomi/extension/ja/pixivcomic/PixivComicUtil.kt @@ -1,5 +1,6 @@ package eu.kanade.tachiyomi.extension.ja.pixivcomic +import android.annotation.SuppressLint import android.os.Build import okhttp3.Interceptor import okhttp3.Response @@ -10,8 +11,6 @@ import java.util.Locale import java.util.TimeZone import kotlin.math.abs -private const val TIME_SALT = "M7w5HORvvX-VP4tRj2CFQOQFPocBqLvHTIbhTU36UCo" - private class NoSuchTagException(message: String) : Exception(message) internal fun tagInterceptor(chain: Interceptor.Chain): Response { @@ -38,7 +37,7 @@ internal fun randomString(): String { } @OptIn(ExperimentalUnsignedTypes::class) -internal fun getTimeAndHash(): Pair { +internal fun getTimeAndHash(salt: String): Pair { val timeFormatted = if (Build.VERSION.SDK_INT < 24) { SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.ENGLISH).format(Date()) .plus(getCurrentTimeZoneOffsetString()) @@ -46,7 +45,7 @@ internal fun getTimeAndHash(): Pair { SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX", Locale.ENGLISH).format(Date()) } - val saltedTimeArray = timeFormatted.plus(TIME_SALT).toByteArray() + val saltedTimeArray = timeFormatted.plus(salt).toByteArray() val saltedTimeHash = MessageDigest.getInstance("SHA-256") .digest(saltedTimeArray).toUByteArray() val hexadecimalTimeHash = saltedTimeHash.joinToString("") { @@ -63,6 +62,7 @@ internal fun getTimeAndHash(): Pair { /** * workaround to retrieve time zone offset for android with version lower than 24 */ +@SuppressLint("DefaultLocale") private fun getCurrentTimeZoneOffsetString(): String { val timeZone = TimeZone.getDefault() val offsetInMillis = timeZone.rawOffset