2023-12-15 13:04:32 +08:00
|
|
|
package com.lizongying.mytv.api
|
|
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
|
import android.content.SharedPreferences
|
|
|
|
|
import com.lizongying.mytv.Encryptor
|
|
|
|
|
import com.lizongying.mytv.MainActivity
|
2023-12-29 10:44:58 +08:00
|
|
|
import com.lizongying.mytv.Utils.getDateTimestamp
|
2023-12-15 13:04:32 +08:00
|
|
|
import com.lizongying.mytv.models.TVViewModel
|
|
|
|
|
import kotlin.math.floor
|
2023-12-26 11:21:03 +08:00
|
|
|
import kotlin.random.Random
|
2023-12-15 13:04:32 +08:00
|
|
|
|
|
|
|
|
class YSP(var context: Context) {
|
|
|
|
|
private var cnlid = ""
|
|
|
|
|
|
|
|
|
|
private var livepid = ""
|
|
|
|
|
|
|
|
|
|
private var stream = "2"
|
|
|
|
|
|
|
|
|
|
private var guid = ""
|
|
|
|
|
|
|
|
|
|
private var cKey = ""
|
|
|
|
|
|
|
|
|
|
private var adjust = 1
|
|
|
|
|
|
|
|
|
|
private val sphttps = "1"
|
|
|
|
|
|
|
|
|
|
private var platform = "5910204"
|
|
|
|
|
|
|
|
|
|
private val cmd = "2"
|
|
|
|
|
|
|
|
|
|
private val encryptVer = "8.1"
|
|
|
|
|
|
|
|
|
|
private val dtype = "1"
|
|
|
|
|
|
|
|
|
|
private val devid = "devid"
|
|
|
|
|
|
|
|
|
|
private val otype = "ojson"
|
|
|
|
|
|
|
|
|
|
private val appVer = "V1.0.0"
|
|
|
|
|
|
|
|
|
|
private val appVersion = "V1.0.0"
|
|
|
|
|
|
|
|
|
|
private var randStr = ""
|
|
|
|
|
|
|
|
|
|
private val channel = "ysp_tx"
|
|
|
|
|
|
|
|
|
|
private var defn = ""
|
|
|
|
|
|
|
|
|
|
private var timeStr = ""
|
|
|
|
|
|
|
|
|
|
private var signature = ""
|
|
|
|
|
|
|
|
|
|
private var encryptor: Encryptor? = null
|
|
|
|
|
private var sharedPref: SharedPreferences? = null
|
|
|
|
|
|
|
|
|
|
init {
|
|
|
|
|
if (context is MainActivity) {
|
|
|
|
|
encryptor = Encryptor()
|
|
|
|
|
encryptor!!.init(context)
|
|
|
|
|
|
|
|
|
|
sharedPref = (context as MainActivity).getPreferences(Context.MODE_PRIVATE)
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-18 13:23:55 +08:00
|
|
|
guid = getGuid()
|
2023-12-15 13:04:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun switch(tvModel: TVViewModel): String {
|
2023-12-20 14:55:39 +08:00
|
|
|
livepid = tvModel.pid.value!!
|
|
|
|
|
cnlid = tvModel.sid.value!!
|
2023-12-15 13:04:32 +08:00
|
|
|
defn = "fhd"
|
|
|
|
|
|
|
|
|
|
randStr = getRand()
|
2024-01-10 22:11:13 +08:00
|
|
|
|
2024-01-11 19:33:31 +08:00
|
|
|
if (tvModel.retryTimes > 0) {
|
|
|
|
|
guid = newGuid()
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-15 13:04:32 +08:00
|
|
|
timeStr = getTimeStr()
|
|
|
|
|
|
|
|
|
|
// guid = "lq3oqitm_1e15dnzgjnb"
|
|
|
|
|
// randStr = "BfcCPQp8Hq"
|
|
|
|
|
// timeStr = "1702166501"
|
|
|
|
|
|
|
|
|
|
cKey =
|
|
|
|
|
encryptor!!.encrypt(cnlid, timeStr, appVer, guid, platform)
|
|
|
|
|
signature = getSignature()
|
|
|
|
|
return """{"cnlid":"$cnlid","livepid":"$livepid","stream":"$stream","guid":"$guid","cKey":"$cKey","adjust":$adjust,"sphttps":"$sphttps","platform":"$platform","cmd":"$cmd","encryptVer":"$encryptVer","dtype":"$dtype","devid":"$devid","otype":"$otype","appVer":"$appVer","app_version":"$appVersion","rand_str":"$randStr","channel":"$channel","defn":"$defn","signature":"$signature"}"""
|
2024-01-01 22:36:03 +08:00
|
|
|
// return """{"cnlid":"2000203803","livepid":"600001801","stream":"2","guid":"lq1y36mb_ccfwmja9zan","cKey":"--01A9F5E89BB86A0C61F4025BEDE15309B6913A79FD1AF1EE7F5EC9C7605F377D1D2281488385C32DEB9E7D0DD3559CB700BD7AF44DD5C9DE0AE14D94B8214027B5D664C108AEE23532348DCC61B86F7C8FBB6CF14D588E6093A25E97DF6D66F4882AB28F17016472DD43D45EF076B7F505176A5E8DEDF2662E5F9AB12B69CB20BCE1579BE724091F3AF6826AE34B713906F3FE139C3783F80EECBD08416DC525E1","adjust":1,"sphttps":"1","platform":"5910204","cmd":"2","encryptVer":"8.1","dtype":"1","devid":"devid","otype":"ojson","appVer":"V1.0.0","app_version":"V1.0.0","rand_str":"IOS2soOw44","channel":"ysp_tx","defn":"fhd","signature":"6ca945f651817de8c6e6910457ceafd6"}"""
|
2023-12-15 13:04:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun getTimeStr(): String {
|
2023-12-29 10:44:58 +08:00
|
|
|
return getDateTimestamp().toString()
|
2023-12-15 13:04:32 +08:00
|
|
|
}
|
|
|
|
|
|
2023-12-23 23:34:53 +08:00
|
|
|
fun generateGuid(): String {
|
2023-12-15 13:04:32 +08:00
|
|
|
val timestamp = (System.currentTimeMillis()).toString(36)
|
2023-12-26 11:21:03 +08:00
|
|
|
val randomPart = Random.nextLong().toString(36).take(11)
|
|
|
|
|
return timestamp + "_" + "0".repeat(11 - randomPart.length) + randomPart
|
2023-12-15 13:04:32 +08:00
|
|
|
}
|
|
|
|
|
|
2023-12-23 23:34:53 +08:00
|
|
|
fun getGuid(): String {
|
2023-12-18 13:23:55 +08:00
|
|
|
var guid = sharedPref?.getString("guid", "")
|
|
|
|
|
if (guid == null || guid.length < 18) {
|
2023-12-15 13:04:32 +08:00
|
|
|
guid = generateGuid()
|
2023-12-18 13:23:55 +08:00
|
|
|
with(sharedPref!!.edit()) {
|
|
|
|
|
putString("guid", guid)
|
|
|
|
|
apply()
|
|
|
|
|
}
|
2023-12-15 13:04:32 +08:00
|
|
|
}
|
|
|
|
|
return guid
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-11 19:33:31 +08:00
|
|
|
private fun newGuid(): String {
|
2024-01-10 22:11:13 +08:00
|
|
|
guid = generateGuid()
|
|
|
|
|
with(sharedPref!!.edit()) {
|
|
|
|
|
putString("guid", guid)
|
|
|
|
|
apply()
|
|
|
|
|
}
|
|
|
|
|
return guid
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-23 23:34:53 +08:00
|
|
|
fun getRand(): String {
|
2023-12-15 13:04:32 +08:00
|
|
|
var n = ""
|
|
|
|
|
val e = "ABCDEFGHIJKlMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
|
|
|
|
|
val r = e.length
|
|
|
|
|
for (i in 0 until 10) {
|
|
|
|
|
n += e[floor(Math.random() * r).toInt()]
|
|
|
|
|
}
|
|
|
|
|
return n
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun getSignature(): String {
|
|
|
|
|
val e =
|
|
|
|
|
"adjust=${adjust}&appVer=${appVer}&app_version=$appVersion&cKey=$cKey&channel=$channel&cmd=$cmd&cnlid=$cnlid&defn=${defn}&devid=${devid}&dtype=${dtype}&encryptVer=${encryptVer}&guid=${guid}&livepid=${livepid}&otype=${otype}&platform=${platform}&rand_str=${randStr}&sphttps=${sphttps}&stream=${stream}".toByteArray()
|
|
|
|
|
|
|
|
|
|
val hashedData = encryptor?.hash(e) ?: return ""
|
|
|
|
|
return hashedData.let { it -> it.joinToString("") { "%02x".format(it) } }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
|
private const val TAG = "YSP"
|
|
|
|
|
}
|
|
|
|
|
}
|