|
| 1 | +package io.github.a13e300.myinjector |
| 2 | + |
| 3 | +import android.net.Uri |
| 4 | +import android.os.Bundle |
| 5 | +import io.github.a13e300.myinjector.arch.IHook |
| 6 | +import io.github.a13e300.myinjector.arch.createObfsTable |
| 7 | +import io.github.a13e300.myinjector.arch.hookAllBefore |
| 8 | +import io.github.a13e300.myinjector.arch.hookAllNop |
| 9 | +import io.github.a13e300.myinjector.arch.toObfsInfo |
| 10 | + |
| 11 | +class BiliHandler : IHook() { |
| 12 | + private fun hookShare() { |
| 13 | + val b23TvRegex = Regex("https?://b23\\.tv/[0-9a-zA-Z]+") |
| 14 | + val filterOutParams = listOf("share_session_id", "share_source", "bbid", "share_medium") |
| 15 | + runCatching { |
| 16 | + findClass("com.bilibili.lib.sharewrapper.ShareHelperV2").hookAllBefore("shareTo") { param -> |
| 17 | + val b = param.args[1] as Bundle |
| 18 | + val content = b.getString("params_content") ?: return@hookAllBefore |
| 19 | + val url = b.getString("params_target_url") ?: return@hookAllBefore |
| 20 | + val res = b23TvRegex.find(content) ?: return@hookAllBefore |
| 21 | + val sanitizedUrl = Uri.parse(url).run { |
| 22 | + val newUrl = buildUpon().clearQuery() |
| 23 | + queryParameterNames.forEach { |
| 24 | + if (it !in filterOutParams) { |
| 25 | + newUrl.appendQueryParameter(it, getQueryParameter(it)) |
| 26 | + } |
| 27 | + } |
| 28 | + newUrl.build().toString() |
| 29 | + } |
| 30 | + logD("url $url -> $sanitizedUrl") |
| 31 | + b.putString("params_content", content.replaceRange(res.range, sanitizedUrl)) |
| 32 | + } |
| 33 | + }.onFailure { |
| 34 | + logE("hookShare", it) |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + private fun hookAd() { |
| 39 | + runCatching { |
| 40 | + // 视频底部广告 |
| 41 | + val tbl = createObfsTable("bili", 1) { bridge -> |
| 42 | + val method = |
| 43 | + bridge.findMethod { |
| 44 | + matcher { |
| 45 | + usingEqStrings("DetailAdService", "-", "onCreateViews") |
| 46 | + } |
| 47 | + }.single().toObfsInfo() |
| 48 | + |
| 49 | + mutableMapOf("DetailAdService-onCreateViews" to method) |
| 50 | + } |
| 51 | + val adCreateView = tbl["DetailAdService-onCreateViews"]!! |
| 52 | + findClass(adCreateView.className).hookAllNop(adCreateView.memberName) |
| 53 | + }.onFailure { |
| 54 | + logE("hookAd", it) |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + override fun onHook() { |
| 59 | + hookShare() |
| 60 | + hookAd() |
| 61 | + } |
| 62 | +} |
0 commit comments