parent
45cff438ce
commit
821cbc1d59
89 changed files with 668 additions and 557 deletions
|
@ -1,73 +1,53 @@
|
|||
package eu.kanade.tachiyomi.lib.chillxextractor
|
||||
|
||||
import android.util.Log
|
||||
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 eu.kanade.tachiyomi.network.GET
|
||||
import eu.kanade.tachiyomi.util.parseAs
|
||||
import kotlinx.serialization.ExperimentalSerializationApi
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.json.Json
|
||||
import okhttp3.Headers
|
||||
import okhttp3.OkHttpClient
|
||||
import uy.kohesive.injekt.injectLazy
|
||||
|
||||
class ChillxExtractor(private val client: OkHttpClient, private val headers: Headers) {
|
||||
private val json: Json by injectLazy()
|
||||
private val playlistUtils by lazy { PlaylistUtils(client, headers) }
|
||||
private val webViewResolver by lazy { WebViewResolver(client, headers) }
|
||||
|
||||
companion object {
|
||||
private val REGEX_MASTER_JS = Regex("""\s*=\s*'([^']+)""")
|
||||
private val REGEX_SOURCES = Regex("""sources:\s*\[\{"file":"([^"]+)""")
|
||||
private val REGEX_FILE = Regex("""file: ?"([^"]+)"""")
|
||||
private val REGEX_SOURCE = Regex("""source = ?"([^"]+)"""")
|
||||
private val REGEX_SUBS = Regex("""\{"file":"([^"]+)","label":"([^"]+)","kind":"captions","default":\w+\}""")
|
||||
private const val KEY_SOURCE = "https://raw.githubusercontent.com/Rowdy-Avocado/multi-keys/keys/index.html"
|
||||
}
|
||||
|
||||
fun videoFromUrl(url: String, referer: String, prefix: String = "Chillx - "): List<Video> {
|
||||
val newHeaders = headers.newBuilder()
|
||||
.set("Referer", "$referer/")
|
||||
.set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8")
|
||||
.set("Accept-Language", "en-US,en;q=0.5")
|
||||
.build()
|
||||
fun videoFromUrl(url: String, prefix: String = "Chillx - "): List<Video> {
|
||||
val data = webViewResolver.getDecryptedData(url) ?: return emptyList()
|
||||
|
||||
val body = client.newCall(GET(url, newHeaders)).execute().body.string()
|
||||
|
||||
val master = REGEX_MASTER_JS.find(body)?.groupValues?.get(1) ?: return emptyList()
|
||||
val aesJson = json.decodeFromString<CryptoInfo>(master)
|
||||
val key = fetchKey() ?: throw ErrorLoadingException("Unable to get key")
|
||||
val decryptedScript = CryptoAES.decryptWithSalt(aesJson.ciphertext, aesJson.salt, key)
|
||||
.replace("\\n", "\n")
|
||||
.replace("\\", "")
|
||||
|
||||
val masterUrl = REGEX_SOURCES.find(decryptedScript)?.groupValues?.get(1)
|
||||
?: REGEX_FILE.find(decryptedScript)?.groupValues?.get(1)
|
||||
?: REGEX_SOURCE.find(decryptedScript)?.groupValues?.get(1)
|
||||
val masterUrl = REGEX_SOURCES.find(data)?.groupValues?.get(1)
|
||||
?: REGEX_FILE.find(data)?.groupValues?.get(1)
|
||||
?: REGEX_SOURCE.find(data)?.groupValues?.get(1)
|
||||
?: return emptyList()
|
||||
|
||||
val subtitleList = buildList {
|
||||
val subtitles = REGEX_SUBS.findAll(decryptedScript)
|
||||
val subtitles = REGEX_SUBS.findAll(data)
|
||||
subtitles.forEach {
|
||||
Log.d("ChillxExtractor", "Found subtitle: ${it.groupValues}")
|
||||
add(Track(it.groupValues[1], decodeUnicodeEscape(it.groupValues[2])))
|
||||
}
|
||||
}
|
||||
|
||||
return playlistUtils.extractFromHls(
|
||||
val videoList = playlistUtils.extractFromHls(
|
||||
playlistUrl = masterUrl,
|
||||
referer = url,
|
||||
videoNameGen = { "$prefix$it" },
|
||||
subtitleList = subtitleList,
|
||||
)
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalSerializationApi::class)
|
||||
private fun fetchKey(): String? {
|
||||
return client.newCall(GET(KEY_SOURCE)).execute().parseAs<KeysData>().keys.firstOrNull()
|
||||
return videoList.map {
|
||||
Video(
|
||||
url = it.url,
|
||||
quality = it.quality,
|
||||
videoUrl = it.videoUrl,
|
||||
audioTracks = it.audioTracks,
|
||||
subtitleTracks = playlistUtils.fixSubtitles(it.subtitleTracks),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private fun decodeUnicodeEscape(input: String): String {
|
||||
|
@ -76,16 +56,4 @@ class ChillxExtractor(private val client: OkHttpClient, private val headers: Hea
|
|||
it.groupValues[1].toInt(16).toChar().toString()
|
||||
}
|
||||
}
|
||||
|
||||
@Serializable
|
||||
data class CryptoInfo(
|
||||
@SerialName("ct") val ciphertext: String,
|
||||
@SerialName("s") val salt: String,
|
||||
)
|
||||
|
||||
@Serializable
|
||||
data class KeysData(
|
||||
@SerialName("chillx") val keys: List<String>
|
||||
)
|
||||
}
|
||||
class ErrorLoadingException(message: String) : Exception(message)
|
||||
|
|
|
@ -0,0 +1,124 @@
|
|||
package eu.kanade.tachiyomi.lib.chillxextractor
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.app.Application
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.webkit.JavascriptInterface
|
||||
import android.webkit.WebResourceRequest
|
||||
import android.webkit.WebResourceResponse
|
||||
import android.webkit.WebView
|
||||
import android.webkit.WebViewClient
|
||||
import eu.kanade.tachiyomi.network.GET
|
||||
import okhttp3.Headers
|
||||
import okhttp3.OkHttpClient
|
||||
import uy.kohesive.injekt.injectLazy
|
||||
import java.io.ByteArrayInputStream
|
||||
import java.util.concurrent.CountDownLatch
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
class WebViewResolver(
|
||||
private val client: OkHttpClient,
|
||||
private val globalHeaders: Headers,
|
||||
) {
|
||||
private val context: Application by injectLazy()
|
||||
private val handler by lazy { Handler(Looper.getMainLooper()) }
|
||||
|
||||
class JsInterface(private val latch: CountDownLatch) {
|
||||
var result: String? = null
|
||||
|
||||
@JavascriptInterface
|
||||
fun passPayload(payload: String) {
|
||||
result = payload
|
||||
latch.countDown()
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressLint("SetJavaScriptEnabled")
|
||||
fun getDecryptedData(embedUrl: String): String? {
|
||||
val latch = CountDownLatch(1)
|
||||
var webView: WebView? = null
|
||||
val jsi = JsInterface(latch)
|
||||
val interfaceName = randomString()
|
||||
|
||||
handler.post {
|
||||
val webview = WebView(context)
|
||||
webView = webview
|
||||
|
||||
with(webview.settings) {
|
||||
javaScriptEnabled = true
|
||||
domStorageEnabled = true
|
||||
databaseEnabled = true
|
||||
useWideViewPort = false
|
||||
loadWithOverviewMode = false
|
||||
userAgentString = globalHeaders["User-Agent"]
|
||||
}
|
||||
|
||||
webview.addJavascriptInterface(jsi, interfaceName)
|
||||
webview.webViewClient = object : WebViewClient() {
|
||||
override fun shouldInterceptRequest(
|
||||
view: WebView?,
|
||||
request: WebResourceRequest?
|
||||
): WebResourceResponse? {
|
||||
if (request?.url.toString().contains("assets/js/library")) {
|
||||
return patchScript(request!!.url.toString(), interfaceName)
|
||||
?: super.shouldInterceptRequest(view, request)
|
||||
}
|
||||
|
||||
return super.shouldInterceptRequest(view, request)
|
||||
}
|
||||
}
|
||||
|
||||
webView?.loadUrl(embedUrl)
|
||||
}
|
||||
|
||||
latch.await(TIMEOUT_SEC, TimeUnit.SECONDS)
|
||||
|
||||
handler.post {
|
||||
webView?.stopLoading()
|
||||
webView?.destroy()
|
||||
webView = null
|
||||
}
|
||||
|
||||
return jsi.result
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val TIMEOUT_SEC: Long = 30
|
||||
}
|
||||
|
||||
private fun randomString(length: Int = 10): String {
|
||||
val charPool = ('a'..'z') + ('A'..'Z')
|
||||
return List(length) { charPool.random() }.joinToString("")
|
||||
}
|
||||
|
||||
private fun patchScript(scriptUrl: String, interfaceName: String): WebResourceResponse? {
|
||||
val scriptBody = client.newCall(GET(scriptUrl)).execute().body.string()
|
||||
|
||||
val oldFunc = randomString()
|
||||
val newBody = buildString {
|
||||
append(
|
||||
"""
|
||||
const $oldFunc = Function;
|
||||
window.Function = function (...args) {
|
||||
if (args.length == 1) {
|
||||
window.$interfaceName.passPayload(args[0]);
|
||||
}
|
||||
return $oldFunc(...args);
|
||||
};
|
||||
""".trimIndent()
|
||||
)
|
||||
append(scriptBody)
|
||||
}
|
||||
|
||||
|
||||
return WebResourceResponse(
|
||||
"application/javascript",
|
||||
"utf-8",
|
||||
200,
|
||||
"ok",
|
||||
mapOf("server" to "cloudflare"),
|
||||
ByteArrayInputStream(newBody.toByteArray()),
|
||||
)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue