cloudstream/app/src/main/java/com/lagradost/cloudstream3/utils/M3u8Helper.kt

273 lines
9.5 KiB
Kotlin
Raw Normal View History

2021-09-01 12:17:53 +00:00
package com.lagradost.cloudstream3.utils
import com.lagradost.cloudstream3.app
2021-09-03 09:13:34 +00:00
import com.lagradost.cloudstream3.mvvm.logError
import kotlinx.coroutines.runBlocking
2021-09-01 12:17:53 +00:00
import javax.crypto.Cipher
import javax.crypto.spec.IvParameterSpec
import javax.crypto.spec.SecretKeySpec
2021-09-01 13:53:27 +00:00
import kotlin.math.pow
2021-09-01 12:17:53 +00:00
class M3u8Helper {
2022-04-18 23:20:28 +00:00
companion object {
private val generator = M3u8Helper()
suspend fun generateM3u8(
2022-04-18 23:20:28 +00:00
source: String,
streamUrl: String,
referer: String,
quality: Int? = null,
headers: Map<String, String> = mapOf(),
name: String = source
): List<ExtractorLink> {
return generator.m3u8Generation(
M3u8Stream(
streamUrl = streamUrl,
quality = quality,
headers = headers,
2022-05-21 18:28:58 +00:00
), null
2022-04-18 23:20:28 +00:00
)
.map { stream ->
ExtractorLink(
source,
name = name,
stream.streamUrl,
referer,
stream.quality ?: Qualities.Unknown.value,
true,
stream.headers,
)
}
}
}
2021-09-01 12:17:53 +00:00
private val ENCRYPTION_DETECTION_REGEX = Regex("#EXT-X-KEY:METHOD=([^,]+),")
2022-04-18 23:20:28 +00:00
private val ENCRYPTION_URL_IV_REGEX =
Regex("#EXT-X-KEY:METHOD=([^,]+),URI=\"([^\"]+)\"(?:,IV=(.*))?")
private val QUALITY_REGEX =
Regex("""#EXT-X-STREAM-INF:(?:(?:.*?(?:RESOLUTION=\d+x(\d+)).*?\s+(.*))|(?:.*?\s+(.*)))""")
2022-04-18 23:20:28 +00:00
private val TS_EXTENSION_REGEX =
Regex("""(.*\.ts.*|.*\.jpg.*)""") //.jpg here 'case vizcloud uses .jpg instead of .ts
2021-09-01 12:17:53 +00:00
2022-04-18 23:20:28 +00:00
private fun absoluteExtensionDetermination(url: String): String? {
2021-09-01 12:17:53 +00:00
val split = url.split("/")
val gg: String = split[split.size - 1].split("?")[0]
return if (gg.contains(".")) {
2021-09-01 21:05:53 +00:00
gg.split(".").ifEmpty { null }?.last()
2021-09-01 12:17:53 +00:00
} else null
}
2021-09-01 13:54:10 +00:00
private fun toBytes16Big(n: Int): ByteArray {
2021-09-01 13:53:27 +00:00
return ByteArray(16) {
val fixed = n / 256.0.pow((15 - it))
(maxOf(0, fixed.toInt()) % 256).toByte()
2021-09-01 12:17:53 +00:00
}
}
private val defaultIvGen = sequence {
var initial = 1
while (true) {
2021-09-01 13:53:27 +00:00
yield(toBytes16Big(initial))
2021-09-01 12:17:53 +00:00
++initial
}
}.iterator()
2022-04-18 23:20:28 +00:00
private fun getDecrypter(
secretKey: ByteArray,
data: ByteArray,
iv: ByteArray = "".toByteArray()
): ByteArray {
2021-09-01 12:17:53 +00:00
val ivKey = if (iv.isEmpty()) defaultIvGen.next() else iv
val c = Cipher.getInstance("AES/CBC/PKCS5Padding")
val skSpec = SecretKeySpec(secretKey, "AES")
val ivSpec = IvParameterSpec(ivKey)
c.init(Cipher.DECRYPT_MODE, skSpec, ivSpec)
return c.doFinal(data)
}
private fun isEncrypted(m3u8Data: String): Boolean {
val st = ENCRYPTION_DETECTION_REGEX.find(m3u8Data)
return st != null && (st.value.isNotEmpty() || st.destructured.component1() != "NONE")
}
2021-09-03 09:13:34 +00:00
data class M3u8Stream(
2021-09-01 12:17:53 +00:00
val streamUrl: String,
val quality: Int? = null,
val headers: Map<String, String> = mapOf()
)
private fun selectBest(qualities: List<M3u8Stream>): M3u8Stream? {
2021-09-01 21:30:21 +00:00
val result = qualities.sortedBy {
if (it.quality != null && it.quality <= 1080) it.quality else 0
2021-09-04 14:14:24 +00:00
}.filter {
listOf("m3u", "m3u8").contains(absoluteExtensionDetermination(it.streamUrl))
2021-09-04 14:52:28 +00:00
}
return result.lastOrNull()
2021-09-01 12:17:53 +00:00
}
private fun getParentLink(uri: String): String {
val split = uri.split("/").toMutableList()
split.removeLast()
return split.joinToString("/")
}
2021-09-04 14:14:24 +00:00
private fun isNotCompleteUrl(url: String): Boolean {
return !url.contains("https://") && !url.contains("http://")
2021-09-01 12:17:53 +00:00
}
suspend fun m3u8Generation(m3u8: M3u8Stream, returnThis: Boolean? = true): List<M3u8Stream> {
// return listOf(m3u8)
val list = mutableListOf<M3u8Stream>()
2021-09-01 12:17:53 +00:00
val m3u8Parent = getParentLink(m3u8.streamUrl)
val response = app.get(m3u8.streamUrl, headers = m3u8.headers, verify = false).text
// var hasAnyContent = false
for (match in QUALITY_REGEX.findAll(response)) {
// hasAnyContent = true
var (quality, m3u8Link, m3u8Link2) = match.destructured
if (m3u8Link.isEmpty()) m3u8Link = m3u8Link2
if (absoluteExtensionDetermination(m3u8Link) == "m3u8") {
if (isNotCompleteUrl(m3u8Link)) {
m3u8Link = "$m3u8Parent/$m3u8Link"
2021-09-01 12:17:53 +00:00
}
if (quality.isEmpty()) {
println(m3u8.streamUrl)
}
list += m3u8Generation(
2021-09-01 12:17:53 +00:00
M3u8Stream(
m3u8Link,
2021-09-01 21:36:41 +00:00
quality.toIntOrNull(),
2021-09-01 12:17:53 +00:00
m3u8.headers
), false
)
}
list += M3u8Stream(
m3u8Link,
quality.toIntOrNull(),
m3u8.headers
)
2021-09-01 12:17:53 +00:00
}
if (returnThis != false) {
list += M3u8Stream(
m3u8.streamUrl,
Qualities.Unknown.value,
m3u8.headers
)
}
return list
2021-09-01 12:17:53 +00:00
}
2021-09-01 12:17:53 +00:00
data class HlsDownloadData(
val bytes: ByteArray,
val currentIndex: Int,
val totalTs: Int,
val errored: Boolean = false
)
suspend fun hlsYield(
qualities: List<M3u8Stream>,
startIndex: Int = 0
): Iterator<HlsDownloadData> {
2022-04-18 23:20:28 +00:00
if (qualities.isEmpty()) return listOf(
HlsDownloadData(
byteArrayOf(),
1,
1,
true
)
).iterator()
2021-09-01 14:34:31 +00:00
var selected = selectBest(qualities)
if (selected == null) {
selected = qualities[0]
}
2021-09-01 12:17:53 +00:00
val headers = selected.headers
val streams = qualities.map { m3u8Generation(it, false) }.flatten()
2021-09-01 21:30:21 +00:00
//val sslVerification = if (headers.containsKey("ssl_verification")) headers["ssl_verification"].toBoolean() else true
2021-09-01 12:17:53 +00:00
val secondSelection = selectBest(streams.ifEmpty { listOf(selected) })
if (secondSelection != null) {
2022-04-18 23:20:28 +00:00
val m3u8Response =
runBlocking {
app.get(
secondSelection.streamUrl,
headers = headers,
verify = false
).text
}
2021-09-01 12:17:53 +00:00
2021-10-03 00:09:13 +00:00
var encryptionUri: String?
2021-09-01 12:17:53 +00:00
var encryptionIv = byteArrayOf()
2021-09-01 21:30:21 +00:00
var encryptionData = byteArrayOf()
2021-09-01 12:17:53 +00:00
val encryptionState = isEncrypted(m3u8Response)
2021-09-01 12:17:53 +00:00
if (encryptionState) {
2021-09-01 21:30:21 +00:00
val match =
ENCRYPTION_URL_IV_REGEX.find(m3u8Response)!!.destructured // its safe to assume that its not going to be null
2021-09-01 12:17:53 +00:00
encryptionUri = match.component2()
2021-09-04 14:14:24 +00:00
if (isNotCompleteUrl(encryptionUri)) {
2021-09-01 12:17:53 +00:00
encryptionUri = "${getParentLink(secondSelection.streamUrl)}/$encryptionUri"
}
encryptionIv = match.component3().toByteArray()
2022-04-18 23:20:28 +00:00
val encryptionKeyResponse =
runBlocking { app.get(encryptionUri, headers = headers, verify = false) }
encryptionData = encryptionKeyResponse.body?.bytes() ?: byteArrayOf()
2021-09-01 12:17:53 +00:00
}
val allTs = TS_EXTENSION_REGEX.findAll(m3u8Response)
2021-09-01 21:30:21 +00:00
val allTsList = allTs.toList()
2021-09-04 13:24:37 +00:00
val totalTs = allTsList.size
2021-09-01 12:17:53 +00:00
if (totalTs == 0) {
2021-09-04 14:14:24 +00:00
return listOf(HlsDownloadData(byteArrayOf(), 1, 1, true)).iterator()
2021-09-01 12:17:53 +00:00
}
var lastYield = 0
val relativeUrl = getParentLink(secondSelection.streamUrl)
var retries = 0
2021-09-01 21:30:21 +00:00
val tsByteGen = sequence {
2021-09-01 12:17:53 +00:00
loop@ for ((index, ts) in allTs.withIndex()) {
val url = if (
2021-09-04 14:14:24 +00:00
isNotCompleteUrl(ts.destructured.component1())
) "$relativeUrl/${ts.destructured.component1()}" else ts.destructured.component1()
2021-09-01 21:30:21 +00:00
val c = index + 1 + startIndex
2021-09-01 12:17:53 +00:00
while (lastYield != c) {
try {
val tsResponse =
runBlocking { app.get(url, headers = headers, verify = false) }
var tsData = tsResponse.body?.bytes() ?: byteArrayOf()
2021-09-01 12:17:53 +00:00
if (encryptionState) {
tsData = getDecrypter(encryptionData, tsData, encryptionIv)
yield(HlsDownloadData(tsData, c, totalTs))
lastYield = c
break
}
yield(HlsDownloadData(tsData, c, totalTs))
lastYield = c
} catch (e: Exception) {
2021-09-01 12:59:28 +00:00
logError(e)
2021-09-01 12:17:53 +00:00
if (retries == 3) {
yield(HlsDownloadData(byteArrayOf(), c, totalTs, true))
break@loop
}
++retries
Thread.sleep(2_000)
}
}
}
}
return tsByteGen.iterator()
}
2021-09-04 14:14:24 +00:00
return listOf(HlsDownloadData(byteArrayOf(), 1, 1, true)).iterator()
2021-09-01 12:17:53 +00:00
}
}