Skip to content

Commit

Permalink
fix(YouTube/Spoof streaming data) : app crashes when loading ads in S…
Browse files Browse the repository at this point in the history
…horts
  • Loading branch information
inotia00 committed Sep 1, 2024
1 parent c285c62 commit 3ca97f9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,18 @@ import app.revanced.patcher.extensions.InstructionExtensions.addInstructions
import app.revanced.patcher.extensions.InstructionExtensions.addInstructionsWithLabels
import app.revanced.patcher.extensions.InstructionExtensions.getInstruction
import app.revanced.patcher.extensions.InstructionExtensions.getInstructions
import app.revanced.patcher.extensions.InstructionExtensions.replaceInstruction
import app.revanced.patcher.patch.PatchException
import app.revanced.patcher.util.smali.ExternalLabel
import app.revanced.patches.youtube.utils.compatibility.Constants
import app.revanced.patches.youtube.utils.fix.streamingdata.fingerprints.BuildBrowseRequestFingerprint
import app.revanced.patches.youtube.utils.fix.streamingdata.fingerprints.BuildInitPlaybackRequestFingerprint
import app.revanced.patches.youtube.utils.fix.streamingdata.fingerprints.BuildMediaDataSourceFingerprint
import app.revanced.patches.youtube.utils.fix.streamingdata.fingerprints.BuildPlayerRequestURIFingerprint
import app.revanced.patches.youtube.utils.fix.streamingdata.fingerprints.BuildRequestFingerprint
import app.revanced.patches.youtube.utils.fix.streamingdata.fingerprints.CreateStreamingDataFingerprint
import app.revanced.patches.youtube.utils.fix.streamingdata.fingerprints.NerdsStatsVideoFormatBuilderFingerprint
import app.revanced.patches.youtube.utils.fix.streamingdata.fingerprints.ProtobufClassParseByteBufferFingerprint
import app.revanced.patches.youtube.utils.integrations.Constants.MISC_PATH
import app.revanced.patches.youtube.utils.settings.SettingsPatch
import app.revanced.patches.youtube.video.videoid.VideoIdPatch
import app.revanced.util.getReference
import app.revanced.util.patch.BaseBytecodePatch
import app.revanced.util.resultOrThrow
Expand All @@ -33,13 +32,13 @@ object SpoofStreamingDataPatch : BaseBytecodePatch(
dependencies = setOf(
SettingsPatch::class,
SpoofUserAgentPatch::class,
VideoIdPatch::class,
),
compatiblePackages = Constants.COMPATIBLE_PACKAGE,
fingerprints = setOf(
BuildBrowseRequestFingerprint,
BuildInitPlaybackRequestFingerprint,
BuildMediaDataSourceFingerprint,
BuildPlayerRequestURIFingerprint,
BuildRequestFingerprint,
CreateStreamingDataFingerprint,
ProtobufClassParseByteBufferFingerprint,

Expand All @@ -49,10 +48,6 @@ object SpoofStreamingDataPatch : BaseBytecodePatch(
) {
private const val INTEGRATIONS_CLASS_DESCRIPTOR =
"$MISC_PATH/SpoofStreamingDataPatch;"
private const val REQUEST_CLASS_DESCRIPTOR =
"Lorg/chromium/net/UrlRequest;"
private const val REQUEST_BUILDER_CLASS_DESCRIPTOR =
"Lorg/chromium/net/UrlRequest\$Builder;"

override fun execute(context: BytecodeContext) {

Expand All @@ -76,51 +71,16 @@ object SpoofStreamingDataPatch : BaseBytecodePatch(

// endregion

// region Block /get_watch requests to fall back to /player requests.
// region Copy request headers for streaming data fetch.

BuildPlayerRequestURIFingerprint.resultOrThrow().let {
it.mutableMethod.apply {
val invokeToStringIndex =
BuildPlayerRequestURIFingerprint.indexOfToStringInstruction(this)
val uriRegister =
getInstruction<FiveRegisterInstruction>(invokeToStringIndex).registerC

addInstructions(
invokeToStringIndex,
"""
invoke-static { v$uriRegister }, $INTEGRATIONS_CLASS_DESCRIPTOR->blockGetWatchRequest(Landroid/net/Uri;)Landroid/net/Uri;
move-result-object v$uriRegister
""",
)
}
}

// endregion

// region Fetch replacement streams.

BuildRequestFingerprint.resultOrThrow().let { result ->
BuildBrowseRequestFingerprint.resultOrThrow().let { result ->
result.mutableMethod.apply {
val buildRequestIndex =
BuildRequestFingerprint.indexOfBuildUrlRequestInstruction(this)
val requestBuilderRegister =
getInstruction<FiveRegisterInstruction>(buildRequestIndex).registerC

val newRequestBuilderIndex =
BuildRequestFingerprint.indexOfNewUrlRequestBuilderInstruction(this)
BuildBrowseRequestFingerprint.indexOfNewUrlRequestBuilderInstruction(this)
val urlRegister =
getInstruction<FiveRegisterInstruction>(newRequestBuilderIndex).registerD

// Replace "requestBuilder.build()" with integrations call.
replaceInstruction(
buildRequestIndex,
"invoke-static { v$requestBuilderRegister }, " +
"$INTEGRATIONS_CLASS_DESCRIPTOR->" +
"buildRequest($REQUEST_BUILDER_CLASS_DESCRIPTOR)" +
REQUEST_CLASS_DESCRIPTOR
)

val entrySetIndex = BuildRequestFingerprint.indexOfEntrySetInstruction(this)
val entrySetIndex = BuildBrowseRequestFingerprint.indexOfEntrySetInstruction(this)
val mapRegister = if (entrySetIndex < 0)
urlRegister + 1
else
Expand All @@ -129,7 +89,7 @@ object SpoofStreamingDataPatch : BaseBytecodePatch(
var smaliInstructions =
"invoke-static { v$urlRegister, v$mapRegister }, " +
"$INTEGRATIONS_CLASS_DESCRIPTOR->" +
"setHeader(Ljava/lang/String;Ljava/util/Map;)V"
"setFetchHeaders(Ljava/lang/String;Ljava/util/Map;)V"

if (entrySetIndex < 0) smaliInstructions = """
move-object/from16 v$mapRegister, p1
Expand Down Expand Up @@ -257,6 +217,9 @@ object SpoofStreamingDataPatch : BaseBytecodePatch(

// endregion

// Prefetch streaming data.
VideoIdPatch.hookPlayerResponseVideoId("$INTEGRATIONS_CLASS_DESCRIPTOR->fetchStreamingData(Ljava/lang/String;Z)V")

/**
* Add settings
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
package app.revanced.patches.youtube.utils.fix.streamingdata.fingerprints

import app.revanced.patcher.fingerprint.MethodFingerprint
import app.revanced.patches.youtube.utils.fix.streamingdata.fingerprints.BuildRequestFingerprint.indexOfBuildUrlRequestInstruction
import app.revanced.patches.youtube.utils.fix.streamingdata.fingerprints.BuildRequestFingerprint.indexOfEntrySetInstruction
import app.revanced.patches.youtube.utils.fix.streamingdata.fingerprints.BuildRequestFingerprint.indexOfNewUrlRequestBuilderInstruction
import app.revanced.patches.youtube.utils.fix.streamingdata.fingerprints.BuildRequestFingerprint.indexOfRequestFinishedListenerInstruction
import app.revanced.patches.youtube.utils.fix.streamingdata.fingerprints.BuildBrowseRequestFingerprint.indexOfEntrySetInstruction
import app.revanced.patches.youtube.utils.fix.streamingdata.fingerprints.BuildBrowseRequestFingerprint.indexOfNewUrlRequestBuilderInstruction
import app.revanced.patches.youtube.utils.fix.streamingdata.fingerprints.BuildBrowseRequestFingerprint.indexOfRequestFinishedListenerInstruction
import app.revanced.util.getReference
import app.revanced.util.indexOfFirstInstruction
import com.android.tools.smali.dexlib2.Opcode
import com.android.tools.smali.dexlib2.iface.Method
import com.android.tools.smali.dexlib2.iface.reference.MethodReference

internal object BuildRequestFingerprint : MethodFingerprint(
internal object BuildBrowseRequestFingerprint : MethodFingerprint(
customFingerprint = { methodDef, _ ->
methodDef.implementation != null &&
indexOfRequestFinishedListenerInstruction(methodDef) >= 0 &&
!methodDef.definingClass.startsWith("Lorg/") &&
indexOfNewUrlRequestBuilderInstruction(methodDef) >= 0 &&
indexOfBuildUrlRequestInstruction(methodDef) >= 0 &&
// YouTube 17.34.36 ~ YouTube 18.35.36
(indexOfEntrySetInstruction(methodDef) >= 0 ||
// YouTube 18.36.39 ~
Expand All @@ -36,12 +34,6 @@ internal object BuildRequestFingerprint : MethodFingerprint(
getReference<MethodReference>().toString() == "Lorg/chromium/net/CronetEngine;->newUrlRequestBuilder(Ljava/lang/String;Lorg/chromium/net/UrlRequest${'$'}Callback;Ljava/util/concurrent/Executor;)Lorg/chromium/net/UrlRequest${'$'}Builder;"
}

fun indexOfBuildUrlRequestInstruction(methodDef: Method) =
methodDef.indexOfFirstInstruction {
opcode == Opcode.INVOKE_VIRTUAL &&
getReference<MethodReference>().toString() == "Lorg/chromium/net/ExperimentalUrlRequest${'$'}Builder;->build()Lorg/chromium/net/ExperimentalUrlRequest;"
}

fun indexOfEntrySetInstruction(methodDef: Method) =
methodDef.indexOfFirstInstruction {
opcode == Opcode.INVOKE_INTERFACE &&
Expand Down

This file was deleted.

0 comments on commit 3ca97f9

Please sign in to comment.