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

218 lines
8.1 KiB
Kotlin
Raw Normal View History

2021-09-01 12:17:53 +00:00
package com.lagradost.cloudstream3.utils
2021-09-03 09:13:34 +00:00
import com.lagradost.cloudstream3.mvvm.logError
import com.lagradost.cloudstream3.network.get
import com.lagradost.cloudstream3.network.text
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 {
private val ENCRYPTION_DETECTION_REGEX = Regex("#EXT-X-KEY:METHOD=([^,]+),")
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+(.*)))""")
2021-09-01 12:17:53 +00:00
private val TS_EXTENSION_REGEX = Regex("""(.*\.ts.*)""")
2021-09-04 14:14:24 +00:00
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()
private fun getDecrypter(secretKey: ByteArray, data: ByteArray, iv: ByteArray = "".toByteArray()): ByteArray {
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
}
2021-09-01 12:17:53 +00:00
return result.getOrNull(0)
}
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
}
fun m3u8Generation(m3u8: M3u8Stream, returnThis: Boolean): List<M3u8Stream> {
2021-09-01 12:17:53 +00:00
val generate = sequence {
val m3u8Parent = getParentLink(m3u8.streamUrl)
val response = get(m3u8.streamUrl, headers = m3u8.headers).text
2021-09-01 12:17:53 +00:00
for (match in QUALITY_REGEX.findAll(response)) {
2021-09-04 14:52:28 +00:00
var (quality, m3u8Link, m3u8Link2) = match.destructured
2021-10-03 00:09:13 +00:00
if (m3u8Link.isEmpty()) m3u8Link = m3u8Link2
2021-09-01 12:17:53 +00:00
if (absoluteExtensionDetermination(m3u8Link) == "m3u8") {
2021-09-04 14:14:24 +00:00
if (isNotCompleteUrl(m3u8Link)) {
2021-09-01 12:17:53 +00:00
m3u8Link = "$m3u8Parent/$m3u8Link"
}
2021-09-04 14:52:28 +00:00
if (quality.isEmpty()) {
println(m3u8.streamUrl)
}
2021-09-01 12:17:53 +00:00
yieldAll(
m3u8Generation(
M3u8Stream(
m3u8Link,
quality.toIntOrNull(),
m3u8.headers
), false
2021-09-01 12:17:53 +00:00
)
)
}
yield(
M3u8Stream(
m3u8Link,
2021-09-01 21:36:41 +00:00
quality.toIntOrNull(),
2021-09-01 12:17:53 +00:00
m3u8.headers
)
)
}
if (returnThis) {
yield(
M3u8Stream(
m3u8.streamUrl,
0,
m3u8.headers
)
)
}
2021-09-01 12:17:53 +00:00
}
return generate.toList()
}
data class HlsDownloadData(
val bytes: ByteArray,
val currentIndex: Int,
val totalTs: Int,
val errored: Boolean = false
)
2021-09-01 21:30:21 +00:00
fun hlsYield(qualities: List<M3u8Stream>, startIndex: Int = 0): Iterator<HlsDownloadData> {
2021-09-04 14:14:24 +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) {
val m3u8Response = get(secondSelection.streamUrl, headers = headers).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()
val encryptionKeyResponse = get(encryptionUri, headers = headers)
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 = get(url, headers = headers)
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
}
}