Skip to content

Commit

Permalink
refactor: optimize code (yujincheng08#1569)
Browse files Browse the repository at this point in the history
fix blockVideoComment
fix bangumiPlayUrlHook for hk, tw, thai, cn

重构 精简代码
修复 屏蔽视频评论区
修复 港澳台泰大陆区的番剧观看
  • Loading branch information
CodePwn2021 authored Nov 19, 2024
1 parent 233a877 commit 05e0848
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 445 deletions.
5 changes: 5 additions & 0 deletions app/src/main/java/me/iacn/biliroaming/BiliBiliPackage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ class BiliBiliPackage constructor(private val mClassLoader: ClassLoader, mContex
val searchVideoCardClass by Weak { "com.bapis.bilibili.polymer.app.search.v1.SearchVideoCard" from mClassLoader }
val playSpeedManager by Weak { mHookInfo.playSpeedManager from mClassLoader }

// for v8.17.0+
val useNewMossFunc = instance.viewMossClass?.declaredMethods?.any {
it.name == "executeRelatesFeed"
} ?: false

val ids: Map<String, Int> by lazy {
mHookInfo.mapIds.idsMap
}
Expand Down
128 changes: 46 additions & 82 deletions app/src/main/java/me/iacn/biliroaming/hook/BangumiPlayUrlHook.kt
Original file line number Diff line number Diff line change
Expand Up @@ -197,80 +197,7 @@ class BangumiPlayUrlHook(classLoader: ClassLoader) : BaseHook(classLoader) {
"com.bapis.bilibili.pgc.gateway.player.v2.PlayURLMoss".findClassOrNull(mClassLoader)?.run {
var isDownload = false
hookBeforeMethod(
"playView",
"com.bapis.bilibili.pgc.gateway.player.v2.PlayViewReq"
) { param ->
val request = param.args[0]
// if getDownload == 1 -> flv download
// if getDownload == 2 -> dash download
// if qn == 0, we are querying available quality
// else we are downloading
// if fnval == 0 -> flv download
// thus fix download will set qn = 0 and set fnval to max
isDownload = sPrefs.getBoolean("allow_download", false)
&& request.callMethodAs<Int>("getDownload") >= 1
if (isDownload) {
if (!sPrefs.getBoolean("fix_download", false)
|| request.callMethodAs<Int>("getFnval") <= 1
) {
request.callMethod("setFnval", MAX_FNVAL)
request.callMethod("setFourk", true)
}
request.callMethod("setDownload", 0)
} else if (halfScreenQuality == 1 || fullScreenQuality != 0) {
request.callMethod("setFnval", MAX_FNVAL)
request.callMethod("setFourk", true)
if (halfScreenQuality == 1 && qnApplied.compareAndSet(false, true)) {
defaultQn?.let { request.callMethod("setQn", it) }
}
}
}
hookAfterMethod(
"playView",
"com.bapis.bilibili.pgc.gateway.player.v2.PlayViewReq"
) { param ->
// th:
// com.bilibili.lib.moss.api.BusinessException: 抱歉您所使用的平台不可观看!
// com.bilibili.lib.moss.api.BusinessException: 啥都木有
// connection err <- should skip because of cache:
// throwable: com.bilibili.lib.moss.api.NetworkException
if (instance.networkExceptionClass?.isInstance(param.throwable) == true)
return@hookAfterMethod
val request = param.args[0]
val response =
param.result ?: "com.bapis.bilibili.pgc.gateway.player.v2.PlayViewReply"
.on(mClassLoader).new()
if (needProxy(response)) {
try {
val serializedRequest = request.callMethodAs<ByteArray>("toByteArray")
val req = PlayViewReq.parseFrom(serializedRequest)
val seasonId = req.seasonId.toString().takeIf { it != "0" }
?: lastSeasonInfo["season_id"] ?: "0"
val (thaiSeason, thaiEp) = getSeasonLazy(seasonId, req.epId)
val content = getPlayUrl(reconstructQuery(req, response, thaiEp))
content?.let {
Log.toast("已从代理服务器获取播放地址\n如加载缓慢或黑屏,可去漫游设置中测速并设置 UPOS")
param.result = reconstructResponse(
req, response, it, isDownload, thaiSeason, thaiEp
)
}
?: throw CustomServerException(mapOf("未知错误" to "请检查哔哩漫游设置中解析服务器设置。"))
} catch (e: CustomServerException) {
param.result = showPlayerError(
response,
"请求解析服务器发生错误(点此查看更多)\n${e.message}"
)
Log.toast("请求解析服务器发生错误: ${e.message}", alsoLog = true)
}
} else if (isDownload) {
param.result = fixDownloadProto(response)
} else if (blockBangumiPageAds) {
param.result = purifyViewInfo(response)
}
}
// v8.17.0+
hookBeforeMethod(
"executePlayView",
if (instance.useNewMossFunc) "executePlayView" else "playView",
"com.bapis.bilibili.pgc.gateway.player.v2.PlayViewReq"
) { param ->
val request = param.args[0]
Expand Down Expand Up @@ -299,7 +226,7 @@ class BangumiPlayUrlHook(classLoader: ClassLoader) : BaseHook(classLoader) {
}
}
hookAfterMethod(
"executePlayView",
if (instance.useNewMossFunc) "executePlayView" else "playView",
"com.bapis.bilibili.pgc.gateway.player.v2.PlayViewReq"
) { param ->
// th:
Expand Down Expand Up @@ -344,7 +271,10 @@ class BangumiPlayUrlHook(classLoader: ClassLoader) : BaseHook(classLoader) {
}
instance.playerMossClass?.run {
var isDownload = false
hookBeforeMethod("playViewUnite", instance.playViewUniteReqClass) { param ->
hookBeforeMethod(
if (instance.useNewMossFunc) "executePlayViewUnite" else "playViewUnite",
instance.playViewUniteReqClass
) { param ->
val request = param.args[0]
val vod = request.callMethod("getVod") ?: return@hookBeforeMethod
isDownload = sPrefs.getBoolean("allow_download", false)
Expand All @@ -371,7 +301,10 @@ class BangumiPlayUrlHook(classLoader: ClassLoader) : BaseHook(classLoader) {
}
}
}
hookAfterMethod("playViewUnite", instance.playViewUniteReqClass) { param ->
hookAfterMethod(
if (instance.useNewMossFunc) "executePlayViewUnite" else "playViewUnite",
instance.playViewUniteReqClass
) { param ->
if (instance.networkExceptionClass?.isInstance(param.throwable) == true)
return@hookAfterMethod
val request = param.args[0]
Expand All @@ -380,9 +313,24 @@ class BangumiPlayUrlHook(classLoader: ClassLoader) : BaseHook(classLoader) {
.on(mClassLoader).new()
val supplementAny = response.callMethod("getSupplement")
val typeUrl = supplementAny?.callMethodAs<String>("getTypeUrl")

// Only handle pgc video
if (param.result != null && typeUrl != PGC_ANY_MODEL_TYPE_URL)
///////////////////
// newPlayUnite:
// request.vod.cid, response.play_arc.cid need skip
val requestVod = request.callMethod("getVod")
val reqCid = requestVod?.callMethodAs<Long>("getCid")

val responsePlayArc = response.callMethod("getPlayArc")
val respCid = responsePlayArc?.callMethodAs<Long>("getCid")

val isThai = reqCid != 0.toLong() && reqCid != respCid
if (
param.result != null && typeUrl != PGC_ANY_MODEL_TYPE_URL && !isThai
) {
return@hookAfterMethod
}

val extraContent = request.callMethodAs<Map<String, String>>("getExtraContentMap")
val seasonId = extraContent.getOrDefault("season_id", "0")
val reqEpId = extraContent.getOrDefault("ep_id", "0").toLong()
Expand All @@ -391,7 +339,7 @@ class BangumiPlayUrlHook(classLoader: ClassLoader) : BaseHook(classLoader) {
val supplement = supplementAny?.callMethod("getValue")
?.callMethodAs<ByteArray>("toByteArray")
?.let { PlayViewReply.parseFrom(it) } ?: playViewReply {}
if (needProxyUnite(response, supplement)) {
if (needProxyUnite(response, supplement) || isThai) {
try {
val serializedRequest = request.callMethodAs<ByteArray>("toByteArray")
val req = PlayViewUniteReq.parseFrom(serializedRequest)
Expand Down Expand Up @@ -455,9 +403,24 @@ class BangumiPlayUrlHook(classLoader: ClassLoader) : BaseHook(classLoader) {
.on(mClassLoader).new()
val supplementAny = response.callMethod("getSupplement")
val typeUrl = supplementAny?.callMethodAs<String>("getTypeUrl")

// Only handle pgc video
if (originalResp != null && typeUrl != PGC_ANY_MODEL_TYPE_URL)
///////////////////
// newPlayUnite:
// request.vod.cid, response.play_arc.cid
val requestVod = request.callMethod("getVod")
val reqCid = requestVod?.callMethodAs<Long>("getCid")

val responsePlayArc = response.callMethod("getPlayArc")
val respCid = responsePlayArc?.callMethodAs<Long>("getCid")

val isThai = reqCid != 0.toLong() && reqCid != respCid
if (
param.result != null && typeUrl != PGC_ANY_MODEL_TYPE_URL && !isThai
) {
return@mossResponseHandlerReplaceProxy null
}

val extraContent =
request.callMethodAs<Map<String, String>>("getExtraContentMap")
val seasonId = extraContent.getOrDefault("season_id", "0")
Expand All @@ -467,7 +430,7 @@ class BangumiPlayUrlHook(classLoader: ClassLoader) : BaseHook(classLoader) {
val supplement = supplementAny?.callMethod("getValue")
?.callMethodAs<ByteArray>("toByteArray")
?.let { PlayViewReply.parseFrom(it) } ?: playViewReply {}
val newResponse = if (needProxyUnite(response, supplement)) {
val newResponse = if (needProxyUnite(response, supplement) || isThai) {
try {
val serializedRequest = request.callMethodAs<ByteArray>("toByteArray")
val req = PlayViewUniteReq.parseFrom(serializedRequest)
Expand Down Expand Up @@ -496,7 +459,8 @@ class BangumiPlayUrlHook(classLoader: ClassLoader) : BaseHook(classLoader) {
}
}
instance.playURLMossClass?.hookBeforeMethod(
"playView", instance.playViewReqClass
if (instance.useNewMossFunc) "executePlayView" else "playView",
instance.playViewReqClass
) { param ->
val request = param.args[0]
val isDownload = request.callMethodAs<Int>("getDownload") >= 1
Expand Down
48 changes: 6 additions & 42 deletions app/src/main/java/me/iacn/biliroaming/hook/BangumiSeasonHook.kt
Original file line number Diff line number Diff line change
Expand Up @@ -321,17 +321,10 @@ class BangumiSeasonHook(classLoader: ClassLoader) : BaseHook(classLoader) {
}
}

instance.viewMossClass?.hookAfterMethod("view", instance.viewReqClass) { param ->
param.result?.let { return@hookAfterMethod }
val serializedRequest = param.args[0].callMethodAs<ByteArray>("toByteArray")
val req = ViewReq.parseFrom(serializedRequest)
val reply = fixViewProto(req)
val serializedReply = reply?.toByteArray() ?: return@hookAfterMethod
param.result =
(param.method as Method).returnType.callStaticMethod("parseFrom", serializedReply)
}
// v8.17.0+
instance.viewMossClass?.hookAfterMethod("execView", instance.viewReqClass) { param ->
instance.viewMossClass?.hookAfterMethod(
if (instance.useNewMossFunc) "executeView" else "view",
instance.viewReqClass
) { param ->
param.result?.let { return@hookAfterMethod }
val serializedRequest = param.args[0].callMethodAs<ByteArray>("toByteArray")
val req = ViewReq.parseFrom(serializedRequest)
Expand All @@ -342,7 +335,8 @@ class BangumiSeasonHook(classLoader: ClassLoader) : BaseHook(classLoader) {
}

instance.viewUniteMossClass?.hookAfterMethod(
"view", "com.bapis.bilibili.app.viewunite.v1.ViewReq"
if (instance.useNewMossFunc) "executeView" else "view",
"com.bapis.bilibili.app.viewunite.v1.ViewReq"
) { param ->
if (instance.networkExceptionClass?.isInstance(param.throwable) == true) return@hookAfterMethod
val response = param.result
Expand Down Expand Up @@ -370,36 +364,6 @@ class BangumiSeasonHook(classLoader: ClassLoader) : BaseHook(classLoader) {

fixViewProto(response, supplement)
}
// v8.17.0+
instance.viewUniteMossClass?.hookAfterMethod(
"executeView", "com.bapis.bilibili.app.viewunite.v1.ViewReq"
) { param ->
if (instance.networkExceptionClass?.isInstance(param.throwable) == true) return@hookAfterMethod
val response = param.result
if (response == null) {
val req = param.args[0].callMethodAs<ByteArray>("toByteArray").let {
ViewUniteReq.parseFrom(it)
}
val av = (if (req.hasAid()) req.aid.takeIf { it != 0L } else if (req.hasBvid()) bv2av(req.bvid) else null)?.toString()
fixViewProto(req, av)?.toByteArray()?.let {
param.result =
"com.bapis.bilibili.app.viewunite.v1.ViewReply".from(mClassLoader)
?.callStaticMethod("parseFrom", it)
} ?: Log.toast("解锁失败!", force = true)
return@hookAfterMethod
}
val supplementAny = response.callMethod("getSupplement")
val typeUrl = supplementAny?.callMethodAs<String>("getTypeUrl")
// Only handle pgc video
if (param.result != null && typeUrl != PGC_ANY_MODEL_TYPE_URL) {
return@hookAfterMethod
}
val supplement =
supplementAny?.callMethod("getValue")?.callMethodAs<ByteArray>("toByteArray")
?.let { ViewPgcAny.parseFrom(it) } ?: viewPgcAny {}

fixViewProto(response, supplement)
}

val urlHook: Hooker = fun(param) {
val redirectUrl = param.thisObject.getObjectFieldAs<String?>("redirectUrl")
Expand Down
Loading

0 comments on commit 05e0848

Please sign in to comment.