V1.4.6: 点歌取址失败——修裸 Socket 通道两处缺陷 + 打通 JS 侧日志
现象: 曲库正常后点歌失败「详情=无法生成下载地址」,且远程日志里没有任何线索。
排查(本地复现源站协议):
- 用同一套签名从本机请求 i.php / music/do.php 全部正常,能拿到这首歌的真实地址;
换 App 注入的版本号(VER=2.0/VN=4.1.3.03281430) 也正常 → 源站没问题,是设备侧。
- 根因在 KtvJsBridge.rawRequest() 这条裸 Socket 通道(点歌取址正是走它):
1. 连接阶段没有超时: `Socket(host, port)` 不受 soTimeout 约束,遇路由黑洞按系统
TCP 超时(~2min)干等;7 个源叠加远超 JS_REQUEST_TIMEOUT_MS(180s) →
桥返回空 → 「无法生成下载地址」,而日志里连失败都没有(请求还没失败);
2. HTTPS 分支用 SSLSocketFactory.getDefault(),既不受
HttpsURLConnection.setDefaultSSLSocketFactory 影响,也不受
network_security_config 约束 → 老设备缺 ISRG 新根时握手必失败。
修复:
- rawRequest 连接改为显式 connect(timeout): Socket().connect(InetSocketAddress, t);
HTTPS 改用 TlsCompat.sslSocketFactory()(系统信任 ∪ 内置根证书)。
- TlsCompat 暴露 sslSocketFactory(),并按 API 分级: 裸通道始终用增强工厂,
HttpsURLConnection 默认工厂仅在 API<24 接管。
- 裸通道失败上报 RAW-ERR(原来只在 logcat)。
- 打通 JS 侧日志: wrapHtml 注入 console 桥,把 ktv_api.js 的 _log()/console.log
转发到 android.log → MiliLog("JS ...");点歌失败时可通过
「JS ...」逐步过程 + 「点歌失败 ... 详情=」汇总原因定位。
版本 1.4.5(25) 到 1.4.6(26);签名证书不变。ktv_api.js 未改动,KTV_BRIDGE_API 仍为 8。
This commit is contained in:
@@ -330,12 +330,32 @@ class KtvJsBridge(context: Context) {
|
||||
|
||||
private fun wrapHtml(script: String): String {
|
||||
val escaped = script.replace("</script", "<\\/script", ignoreCase = true)
|
||||
return "<!doctype html><html><head><meta charset=\"utf-8\"></head><body><script>$escaped</script></body></html>"
|
||||
// console.log 桥接:ktv_api.js 的 _log()/_log 细节(Try source / Token FAIL / do.php code=
|
||||
// 等)原本只进 WebView 控制台,远程日志里完全看不到,点歌失败无从定位。
|
||||
// 这里把它转发到 android.log → MiliLog,并保留原始 console 行为。
|
||||
val consoleBridge = """
|
||||
(function(){
|
||||
try {
|
||||
var orig = console.log;
|
||||
console.log = function(){
|
||||
try { android.log(Array.prototype.map.call(arguments, String).join(' ')); } catch(e) {}
|
||||
if (orig) { try { orig.apply(console, arguments); } catch(e) {} }
|
||||
};
|
||||
} catch(e) {}
|
||||
})();
|
||||
""".trimIndent()
|
||||
return "<!doctype html><html><head><meta charset=\"utf-8\"></head><body><script>$consoleBridge</script>" +
|
||||
"<script>$escaped</script></body></html>"
|
||||
}
|
||||
|
||||
inner class NativeBridge {
|
||||
@JavascriptInterface
|
||||
fun log(message: String) = Log.d(TAG, message)
|
||||
fun log(message: String) {
|
||||
Log.d(TAG, message)
|
||||
// 把 JS 侧日志接进远程日志:歌曲源解析(token/do.php/多源排序)的失败原因
|
||||
// 以前只走 console.log,现场完全看不到。加上前缀便于检索与过滤。
|
||||
runCatching { MiliLog.info("JS ${message.take(180)}") }
|
||||
}
|
||||
|
||||
@JavascriptInterface
|
||||
fun onUrl(requestId: String, url: String) {
|
||||
@@ -450,10 +470,22 @@ class KtvJsBridge(context: Context) {
|
||||
val isHttps = url.startsWith("https://")
|
||||
val uri = URL(url).toURI()
|
||||
val port = if (uri.port > 0) uri.port else if (isHttps) 443 else 80
|
||||
val connectTimeout = timeoutMs.coerceIn(1_000, 30_000)
|
||||
socket = if (isHttps) {
|
||||
(SSLSocketFactory.getDefault().createSocket(uri.host, port) as SSLSocket).apply { startHandshake() }
|
||||
// 用 TlsCompat 的工厂(系统信任 ∪ 内置根证书):裸 socket 走的是
|
||||
// SSLSocketFactory.getDefault(),不受 HttpsURLConnection 的默认工厂影响,
|
||||
// network_security_config 也管不到它 —— 老设备上会因缺 ISRG 新根握手失败。
|
||||
(TlsCompat.sslSocketFactory().createSocket() as SSLSocket).apply {
|
||||
connect(java.net.InetSocketAddress(uri.host, port), connectTimeout)
|
||||
startHandshake()
|
||||
}
|
||||
} else {
|
||||
Socket(uri.host, port)
|
||||
// ⚠️ 必须显式给连接超时:原写法 `Socket(host, port)` 的连接阶段**不受**
|
||||
// soTimeout 约束,遇到路由黑洞会按系统 TCP 超时(约 2 分钟)干等,
|
||||
// 多个源叠加会把 JS 的 3 分钟预算耗光,表现为「无法生成下载地址」。
|
||||
Socket().apply {
|
||||
connect(java.net.InetSocketAddress(uri.host, port), connectTimeout)
|
||||
}
|
||||
}
|
||||
socket.soTimeout = timeoutMs.coerceIn(1_000, 30_000)
|
||||
val path = if (uri.rawQuery != null) "${uri.rawPath}?${uri.rawQuery}" else uri.rawPath
|
||||
@@ -494,6 +526,11 @@ class KtvJsBridge(context: Context) {
|
||||
JSONObject().put("status", status).put("body", responseBody).toString()
|
||||
} catch (error: Throwable) {
|
||||
Log.w(TAG, "raw request failed: ${error.message}")
|
||||
// 裸 Socket 失败原因以前只在 logcat:点歌失败时远程日志里什么都看不到。
|
||||
MiliLog.warn(
|
||||
"RAW-ERR $method ${runCatching { java.net.URI(url).host }.getOrNull() ?: url} " +
|
||||
"${error.javaClass.simpleName}: ${error.message}",
|
||||
)
|
||||
JSONObject().put("status", 0).put("body", "").put("error", error.message.orEmpty()).toString()
|
||||
} finally {
|
||||
try { socket?.close() } catch (ignored: Exception) {}
|
||||
|
||||
Reference in New Issue
Block a user