Skip to content

Commit

Permalink
feat: Address TODO, fetch indexes only when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
Claudemirovsky committed Nov 9, 2023
1 parent 334aa75 commit b717852
Showing 1 changed file with 29 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@ import eu.kanade.tachiyomi.animesource.model.Track
import eu.kanade.tachiyomi.animesource.model.Video
import eu.kanade.tachiyomi.lib.cryptoaes.CryptoAES
import eu.kanade.tachiyomi.lib.playlistutils.PlaylistUtils
import okhttp3.Headers
import eu.kanade.tachiyomi.network.GET
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.jsonPrimitive
import okhttp3.OkHttpClient
import uy.kohesive.injekt.injectLazy
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.jsonPrimitive
import okhttp3.CacheControl
import okhttp3.Headers
import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.OkHttpClient
import uy.kohesive.injekt.injectLazy

class MegaCloudExtractor(private val client: OkHttpClient, private val headers: Headers) {
private val json: Json by injectLazy()
Expand All @@ -30,16 +34,26 @@ class MegaCloudExtractor(private val client: OkHttpClient, private val headers:
private val SOURCES_URL = arrayOf("/embed-2/ajax/e-1/getSources?id=", "/ajax/embed-6-v2/getSources?id=")
private val SOURCES_SPLITTER = arrayOf("/e-1/", "/embed-6-v2/")
private val SOURCES_KEY = arrayOf("1", "6")
private val INDEX_PAIRS_MAP = mutableMapOf("1" to emptyList<List<Int>>(), "6" to emptyList<List<Int>>())
private val MUTEX = Mutex()

private inline fun <reified R> runLocked(crossinline block: () -> R) = runBlocking(Dispatchers.IO) {
MUTEX.withLock { block() }
}
}

private fun cipherTextCleaner(data: String, type: String): Pair<String, String> {
// TODO: fetch the key only when needed, using a thread-safe map
// (Like ConcurrentMap?) or MUTEX hacks.
val indexPairs = noCacheClient.newCall(GET("https://raw.githubusercontent.com/Claudemirovsky/keys/e$type/key", cache = cacheControl))
.execute()
.use { it.body.string() }
.let { json.decodeFromString<List<List<Int>>>(it) }
private fun getIndexPairs(type: String) = runLocked {
INDEX_PAIRS_MAP[type].orEmpty().ifEmpty {
noCacheClient.newCall(GET("https://raw.githubusercontent.com/Claudemirovsky/keys/e$type/key", cache = cacheControl))
.execute()
.use { it.body.string() }
.let { json.decodeFromString<List<List<Int>>>(it) }
.also { INDEX_PAIRS_MAP[type] = it }
}
}

private fun cipherTextCleaner(data: String, type: String): Pair<String, String> {
val indexPairs = getIndexPairs(type)
val (password, ciphertext, _) = indexPairs.fold(Triple("", data, 0)) { previous, item ->
val start = item.first() + previous.third
val end = start + item.last()
Expand All @@ -56,6 +70,8 @@ class MegaCloudExtractor(private val client: OkHttpClient, private val headers:
if (attempts > 2) throw Exception("PLEASE NUKE ANIWATCH AND CLOUDFLARE")
val (ciphertext, password) = cipherTextCleaner(ciphered, type)
return CryptoAES.decrypt(ciphertext, password).ifEmpty {
// Update index pairs
runLocked { INDEX_PAIRS_MAP[type] = emptyList<List<Int>>() }
tryDecrypting(ciphered, type, attempts + 1)
}
}
Expand All @@ -67,7 +83,7 @@ class MegaCloudExtractor(private val client: OkHttpClient, private val headers:
val subs2 = video.tracks
?.filter { it.kind == "captions" }
?.map { Track(it.file, it.label) }
?: emptyList()
.orEmpty()
return playlistUtils.extractFromHls(
masterUrl,
videoNameGen = { "$name - $it - $type" },
Expand Down

0 comments on commit b717852

Please sign in to comment.