Skip to content

Commit b075a7d

Browse files
committed
trust all certs
1 parent 7821e8f commit b075a7d

File tree

4 files changed

+145
-89
lines changed

4 files changed

+145
-89
lines changed

app/src/main/java/com/lizongying/mytv/MainActivity.kt

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import android.content.pm.PackageInfo
55
import android.content.pm.PackageManager
66
import android.content.pm.Signature
77
import android.content.pm.SigningInfo
8+
import android.graphics.Color
89
import android.os.Build
910
import android.os.Bundle
1011
import android.os.Handler
@@ -36,7 +37,7 @@ class MainActivity : FragmentActivity() {
3637
private lateinit var gestureDetector: GestureDetector
3738

3839
private val handler = Handler()
39-
private val delay: Long = 3000
40+
private val delay: Long = 4000
4041

4142
override fun onCreate(savedInstanceState: Bundle?) {
4243
super.onCreate(savedInstanceState)
@@ -170,6 +171,40 @@ class MainActivity : FragmentActivity() {
170171
}
171172
}
172173

174+
private fun showHelp() {
175+
val versionName = getPackageInfo().versionName
176+
177+
val textView = TextView(this)
178+
textView.text =
179+
"当前版本: $versionName\n获取最新: https://github.com/lizongying/my-tv/releases/"
180+
textView.setBackgroundColor(0xFF263238.toInt())
181+
textView.setPadding(20, 50, 20, 20)
182+
183+
val imageView = ImageView(this)
184+
val drawable = ContextCompat.getDrawable(this, R.drawable.appreciate)
185+
imageView.setImageDrawable(drawable)
186+
imageView.setBackgroundColor(Color.WHITE)
187+
188+
val linearLayout = LinearLayout(this)
189+
linearLayout.orientation = LinearLayout.VERTICAL
190+
linearLayout.addView(textView)
191+
linearLayout.addView(imageView)
192+
193+
val layoutParams = LinearLayout.LayoutParams(
194+
LinearLayout.LayoutParams.MATCH_PARENT,
195+
LinearLayout.LayoutParams.WRAP_CONTENT
196+
)
197+
imageView.layoutParams = layoutParams
198+
textView.layoutParams = layoutParams
199+
200+
val builder: AlertDialog.Builder = AlertDialog.Builder(this)
201+
builder
202+
.setView(linearLayout)
203+
204+
val dialog: AlertDialog = builder.create()
205+
dialog.show()
206+
}
207+
173208
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
174209
when (keyCode) {
175210
KeyEvent.KEYCODE_BACK -> {
@@ -192,35 +227,13 @@ class MainActivity : FragmentActivity() {
192227
return true
193228
}
194229

230+
KeyEvent.KEYCODE_SETTINGS -> {
231+
showHelp()
232+
return true
233+
}
234+
195235
KeyEvent.KEYCODE_MENU -> {
196-
val versionName = getPackageInfo().versionName
197-
198-
val textView = TextView(this)
199-
textView.text =
200-
"当前版本: $versionName\n获取最新: https://github.com/lizongying/my-tv/releases/"
201-
202-
val imageView = ImageView(this)
203-
val drawable = ContextCompat.getDrawable(this, R.drawable.appreciate)
204-
imageView.setImageDrawable(drawable)
205-
206-
val linearLayout = LinearLayout(this)
207-
linearLayout.orientation = LinearLayout.VERTICAL
208-
linearLayout.addView(textView)
209-
linearLayout.addView(imageView)
210-
211-
val layoutParams = LinearLayout.LayoutParams(
212-
LinearLayout.LayoutParams.MATCH_PARENT,
213-
LinearLayout.LayoutParams.WRAP_CONTENT
214-
)
215-
imageView.layoutParams = layoutParams
216-
textView.layoutParams = layoutParams
217-
218-
val builder: AlertDialog.Builder = AlertDialog.Builder(this)
219-
builder
220-
.setView(linearLayout)
221-
222-
val dialog: AlertDialog = builder.create()
223-
dialog.show()
236+
showHelp()
224237
return true
225238
}
226239

app/src/main/java/com/lizongying/mytv/Request.kt

Lines changed: 58 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,10 @@ class Request {
8787
ysp = YSP(context)
8888
}
8989

90+
var call: Call<LiveInfo>? = null
91+
9092
fun fetchVideo(tvModel: TVViewModel, cookie: String) {
93+
call?.cancel()
9194
if (::myRunnable.isInitialized) {
9295
handler.removeCallbacks(myRunnable)
9396
}
@@ -97,71 +100,71 @@ class Request {
97100
tvModel.seq = 0
98101
val data = ysp?.switch(tvModel)
99102
val request = data?.let { LiveInfoRequest(it) }
103+
call = request?.let { yspApiService.getLiveInfo("guid=${ysp?.getGuid()}; $cookie", it) }
100104

101-
request?.let { yspApiService.getLiveInfo(cookie, it) }
102-
?.enqueue(object : Callback<LiveInfo> {
103-
override fun onResponse(call: Call<LiveInfo>, response: Response<LiveInfo>) {
104-
if (response.isSuccessful) {
105-
val liveInfo = response.body()
106-
if (liveInfo?.data?.playurl != null) {
107-
val chanll = liveInfo.data.chanll
108-
val decodedBytes = Base64.decode(
109-
chanll.substring(9, chanll.length - 3),
110-
Base64.DEFAULT
111-
)
112-
val decodedString = String(decodedBytes)
113-
val regex = Regex("""des_key = "([^"]+).+var des_iv = "([^"]+)""")
114-
val matchResult = regex.find(decodedString)
115-
if (matchResult != null) {
116-
val (key, iv) = matchResult.destructured
117-
val keyBytes = Base64.decode(key, Base64.DEFAULT)
118-
val ivBytes = Base64.decode(iv, Base64.DEFAULT)
119-
val url = liveInfo.data.playurl + "&revoi=" + encryptTripleDES(
120-
keyBytes + byteArrayOf(0, 0, 0, 0, 0, 0, 0, 0),
121-
ivBytes
122-
).uppercase()
123-
Log.i(TAG, "$title url $url")
124-
tvModel.addVideoUrl(url)
125-
tvModel.allReady()
126-
tvModel.retryTimes = 0
127-
myRunnable = MyRunnable(tvModel)
128-
handler.post(myRunnable)
129-
} else {
130-
Log.e(TAG, "$title key error")
131-
if (tvModel.retryTimes < tvModel.retryMaxTimes) {
132-
tvModel.retryTimes++
133-
fetchVideo(tvModel, cookie)
134-
}
135-
}
105+
call?.enqueue(object : Callback<LiveInfo> {
106+
override fun onResponse(call: Call<LiveInfo>, response: Response<LiveInfo>) {
107+
if (response.isSuccessful) {
108+
val liveInfo = response.body()
109+
if (liveInfo?.data?.playurl != null) {
110+
val chanll = liveInfo.data.chanll
111+
val decodedBytes = Base64.decode(
112+
chanll.substring(9, chanll.length - 3),
113+
Base64.DEFAULT
114+
)
115+
val decodedString = String(decodedBytes)
116+
val regex = Regex("""des_key = "([^"]+).+var des_iv = "([^"]+)""")
117+
val matchResult = regex.find(decodedString)
118+
if (matchResult != null) {
119+
val (key, iv) = matchResult.destructured
120+
val keyBytes = Base64.decode(key, Base64.DEFAULT)
121+
val ivBytes = Base64.decode(iv, Base64.DEFAULT)
122+
val url = liveInfo.data.playurl + "&revoi=" + encryptTripleDES(
123+
keyBytes + byteArrayOf(0, 0, 0, 0, 0, 0, 0, 0),
124+
ivBytes
125+
).uppercase()
126+
Log.i(TAG, "$title url $url")
127+
tvModel.addVideoUrl(url)
128+
tvModel.allReady()
129+
tvModel.retryTimes = 0
130+
myRunnable = MyRunnable(tvModel)
131+
handler.post(myRunnable)
136132
} else {
137-
if (liveInfo?.data?.errinfo != null && liveInfo.data.errinfo == "应版权方要求,暂停提供直播信号,请点击观看其他精彩节目") {
138-
Log.e(TAG, "$title error ${liveInfo.data.errinfo}")
139-
tvModel.setErrInfo(liveInfo.data.errinfo)
140-
} else {
141-
Log.e(TAG, "$title url error $request $liveInfo")
142-
if (tvModel.retryTimes < tvModel.retryMaxTimes) {
143-
tvModel.retryTimes++
144-
fetchVideo(tvModel, cookie)
145-
}
133+
Log.e(TAG, "$title key error")
134+
if (tvModel.retryTimes < tvModel.retryMaxTimes) {
135+
tvModel.retryTimes++
136+
fetchVideo(tvModel, cookie)
146137
}
147138
}
148139
} else {
149-
Log.e(TAG, "$title status error")
150-
if (tvModel.retryTimes < tvModel.retryMaxTimes) {
151-
tvModel.retryTimes++
152-
fetchVideo(tvModel, cookie)
140+
if (liveInfo?.data?.errinfo != null && liveInfo.data.errinfo == "应版权方要求,暂停提供直播信号,请点击观看其他精彩节目") {
141+
Log.e(TAG, "$title error ${liveInfo.data.errinfo}")
142+
tvModel.setErrInfo(liveInfo.data.errinfo)
143+
} else {
144+
Log.e(TAG, "$title url error $request $liveInfo")
145+
if (tvModel.retryTimes < tvModel.retryMaxTimes) {
146+
tvModel.retryTimes++
147+
fetchVideo(tvModel, cookie)
148+
}
153149
}
154150
}
155-
}
156-
157-
override fun onFailure(call: Call<LiveInfo>, t: Throwable) {
158-
Log.e(TAG, "$title request error")
151+
} else {
152+
Log.e(TAG, "$title status error")
159153
if (tvModel.retryTimes < tvModel.retryMaxTimes) {
160154
tvModel.retryTimes++
161155
fetchVideo(tvModel, cookie)
162156
}
163157
}
164-
})
158+
}
159+
160+
override fun onFailure(call: Call<LiveInfo>, t: Throwable) {
161+
Log.e(TAG, "$title request error")
162+
if (tvModel.retryTimes < tvModel.retryMaxTimes) {
163+
tvModel.retryTimes++
164+
fetchVideo(tvModel, cookie)
165+
}
166+
}
167+
})
165168
}
166169

167170
fun fetchVideo(tvModel: TVViewModel) {
@@ -172,7 +175,7 @@ class Request {
172175
val token = response.body()?.data?.token
173176
Log.i(TAG, "info success $token")
174177
val cookie =
175-
"guid=1; vplatform=109; yspopenid=vu0-8lgGV2LW9QjDeuBFsX8yMnzs37Q3_HZF6XyVDpGR_I; vusession=$token"
178+
"vplatform=109; yspopenid=vu0-8lgGV2LW9QjDeuBFsX8yMnzs37Q3_HZF6XyVDpGR_I; vusession=$token"
176179
fetchVideo(tvModel, cookie)
177180
} else {
178181
Log.e(TAG, "info status error")
@@ -197,7 +200,7 @@ class Request {
197200
if (tvModel.needToken) {
198201
fetchVideo(tvModel)
199202
} else {
200-
val cookie = "guid=1; vplatform=109"
203+
val cookie = "vplatform=109"
201204
fetchVideo(tvModel, cookie)
202205
}
203206
}

app/src/main/java/com/lizongying/mytv/api/ApiClient.kt

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import okhttp3.OkHttpClient
55
import retrofit2.Retrofit
66
import retrofit2.converter.gson.GsonConverterFactory
77
import retrofit2.converter.protobuf.ProtoConverterFactory
8+
import javax.net.ssl.SSLContext
9+
import javax.net.ssl.TrustManager
10+
import javax.net.ssl.X509TrustManager
811

912

1013
class ApiClient {
@@ -13,9 +16,7 @@ class ApiClient {
1316
private val protoUrl = "https://capi.yangshipin.cn/"
1417
private val traceUrl = "https://btrace.yangshipin.cn/"
1518

16-
private var okHttpClient = OkHttpClient.Builder()
17-
.dns(DnsCache())
18-
.build()
19+
private var okHttpClient = getUnsafeOkHttpClient()
1920

2021
val yspApiService: YSPApiService by lazy {
2122
Retrofit.Builder()
@@ -48,4 +49,40 @@ class ApiClient {
4849
.addConverterFactory(GsonConverterFactory.create())
4950
.build().create(YSPBtraceService::class.java)
5051
}
52+
53+
private fun getUnsafeOkHttpClient(): OkHttpClient {
54+
try {
55+
val trustAllCerts: Array<TrustManager> = arrayOf(
56+
object : X509TrustManager {
57+
override fun checkClientTrusted(
58+
chain: Array<out java.security.cert.X509Certificate>?,
59+
authType: String?
60+
) {
61+
}
62+
63+
override fun checkServerTrusted(
64+
chain: Array<out java.security.cert.X509Certificate>?,
65+
authType: String?
66+
) {
67+
}
68+
69+
override fun getAcceptedIssuers(): Array<java.security.cert.X509Certificate> {
70+
return emptyArray()
71+
}
72+
}
73+
)
74+
75+
val sslContext = SSLContext.getInstance("SSL")
76+
sslContext.init(null, trustAllCerts, java.security.SecureRandom())
77+
78+
return OkHttpClient.Builder()
79+
.sslSocketFactory(sslContext.socketFactory, trustAllCerts[0] as X509TrustManager)
80+
.hostnameVerifier { _, _ -> true }
81+
.dns(DnsCache())
82+
.build()
83+
84+
} catch (e: Exception) {
85+
throw RuntimeException(e)
86+
}
87+
}
5188
}

app/src/main/java/com/lizongying/mytv/api/YSP.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,10 @@ class YSP(var context: Context) {
7171

7272
randStr = getRand()
7373

74-
guid = newGuid()
74+
if (tvModel.retryTimes > 0) {
75+
guid = newGuid()
76+
}
77+
7578
timeStr = getTimeStr()
7679

7780
// guid = "lq3oqitm_1e15dnzgjnb"
@@ -107,7 +110,7 @@ class YSP(var context: Context) {
107110
return guid
108111
}
109112

110-
fun newGuid(): String {
113+
private fun newGuid(): String {
111114
guid = generateGuid()
112115
with(sharedPref!!.edit()) {
113116
putString("guid", guid)

0 commit comments

Comments
 (0)