feat(en/kisskh): Decrypt encrypted subtitles (#2646)

This commit is contained in:
Secozzi 2023-12-14 15:35:24 +00:00 committed by GitHub
parent 99d4cc0188
commit 06cbdfed94
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 90 additions and 4 deletions

View File

@ -5,7 +5,7 @@ ext {
extName = 'KissKH' extName = 'KissKH'
pkgNameSuffix = 'en.kisskh' pkgNameSuffix = 'en.kisskh'
extClass = '.KissKH' extClass = '.KissKH'
extVersionCode = 2 extVersionCode = 3
libVersion = '13' libVersion = '13'
} }

View File

@ -8,7 +8,6 @@ import eu.kanade.tachiyomi.animesource.model.Track
import eu.kanade.tachiyomi.animesource.model.Video import eu.kanade.tachiyomi.animesource.model.Video
import eu.kanade.tachiyomi.animesource.online.AnimeHttpSource import eu.kanade.tachiyomi.animesource.online.AnimeHttpSource
import eu.kanade.tachiyomi.network.GET import eu.kanade.tachiyomi.network.GET
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonArray import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.JsonObject
@ -21,7 +20,6 @@ import okhttp3.Headers
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import okhttp3.Request import okhttp3.Request
import okhttp3.Response import okhttp3.Response
import uy.kohesive.injekt.api.get
class KissKH : AnimeHttpSource() { class KissKH : AnimeHttpSource() {
@ -111,6 +109,8 @@ class KissKH : AnimeHttpSource() {
return videosFromElement(response, id) return videosFromElement(response, id)
} }
private val subDecryptor by lazy { SubDecryptor(client, headers, baseUrl) }
private fun videosFromElement(response: Response, id: String): List<Video> { private fun videosFromElement(response: Response, id: String): List<Video> {
val jsonData = response.body.string() val jsonData = response.body.string()
val jObject = json.decodeFromString<JsonObject>(jsonData) val jObject = json.decodeFromString<JsonObject>(jsonData)
@ -122,10 +122,16 @@ class KissKH : AnimeHttpSource() {
try { try {
val suburl = item.jsonObject["src"]!!.jsonPrimitive.content val suburl = item.jsonObject["src"]!!.jsonPrimitive.content
val lang = item.jsonObject["label"]!!.jsonPrimitive.content val lang = item.jsonObject["label"]!!.jsonPrimitive.content
if (suburl.endsWith("txt")) {
subList.add(subDecryptor.getSubtitles(suburl, lang))
} else {
subList.add(Track(suburl, lang)) subList.add(Track(suburl, lang))
}
} catch (_: Error) {} } catch (_: Error) {}
} }
val videoUrl = jObject["Video"]!!.jsonPrimitive.content val videoUrl = jObject["Video"]!!.jsonPrimitive.content
videoList.add(Video(videoUrl, "FirstParty", videoUrl, subtitleTracks = subList, headers = Headers.headersOf("referer", "https://kisskh.me/", "origin", "https://kisskh.me"))) videoList.add(Video(videoUrl, "FirstParty", videoUrl, subtitleTracks = subList, headers = Headers.headersOf("referer", "https://kisskh.me/", "origin", "https://kisskh.me")))
return videoList.reversed() return videoList.reversed()

View File

@ -0,0 +1,80 @@
package eu.kanade.tachiyomi.animeextension.en.kisskh
import android.net.Uri
import android.util.Base64
import eu.kanade.tachiyomi.animesource.model.Track
import eu.kanade.tachiyomi.network.GET
import okhttp3.Headers
import okhttp3.OkHttpClient
import java.io.File
import javax.crypto.Cipher
import javax.crypto.spec.IvParameterSpec
import javax.crypto.spec.SecretKeySpec
class SubDecryptor(private val client: OkHttpClient, private val headers: Headers, private val baseurl: String) {
fun getSubtitles(subUrl: String, subLang: String): Track {
val subHeaders = headers.newBuilder().apply {
add("Accept", "application/json, text/plain, */*")
add("Origin", baseurl)
add("Referer", "$baseurl/")
}.build()
val subtitleData = client.newCall(
GET(subUrl, subHeaders),
).execute().use { it.body.string() }
val chunks = subtitleData.split(CHUNK_REGEX)
.filter(String::isNotBlank)
.map(String::trim)
val decrypted = chunks.mapIndexed { index, chunk ->
val parts = chunk.split("\n")
val text = parts.slice(1 until parts.size)
val d = text.map { decrypt(it) }.joinToString("\n")
arrayOf(index + 1, parts.first(), d).joinToString("\n")
}.joinToString("\n\n")
val file = File.createTempFile("subs", "srt")
.also(File::deleteOnExit)
file.writeText(decrypted)
val uri = Uri.fromFile(file)
return Track(uri.toString(), subLang)
}
companion object {
private val CHUNK_REGEX by lazy { Regex("^\\d+$", RegexOption.MULTILINE) }
private val KEY = intArrayOf(942683446, 876098358, 875967282, 943142451)
private val IV = intArrayOf(909653298, 909193779, 925905208, 892483379)
}
private fun getKey(words: IntArray): SecretKeySpec {
val keyBytes = words.toByteArray()
return SecretKeySpec(keyBytes, "AES")
}
private fun decrypt(data: String): String {
val key = getKey(KEY)
val iv = IvParameterSpec(IV.toByteArray())
val cipher = Cipher.getInstance("AES/CBC/PKCS5Padding")
cipher.init(Cipher.DECRYPT_MODE, key, iv)
val encryptedBytes = Base64.decode(data, Base64.DEFAULT)
return String(cipher.doFinal(encryptedBytes), Charsets.UTF_8)
}
private fun IntArray.toByteArray(): ByteArray {
return ByteArray(size * 4).also { bytes ->
forEachIndexed { index, value ->
bytes[index * 4] = (value shr 24).toByte()
bytes[index * 4 + 1] = (value shr 16).toByte()
bytes[index * 4 + 2] = (value shr 8).toByte()
bytes[index * 4 + 3] = value.toByte()
}
}
}
}