diff --git a/src/main/kotlin/app/revanced/patches/music/misc/tracking/SanitizeUrlQueryBytecodePatch.kt b/src/main/kotlin/app/revanced/patches/music/misc/tracking/SanitizeUrlQueryBytecodePatch.kt deleted file mode 100644 index 259af32ce1..0000000000 --- a/src/main/kotlin/app/revanced/patches/music/misc/tracking/SanitizeUrlQueryBytecodePatch.kt +++ /dev/null @@ -1,15 +0,0 @@ -package app.revanced.patches.music.misc.tracking - -import app.revanced.patches.music.misc.tracking.fingerprints.ShareLinkFormatterFingerprint -import app.revanced.patches.music.utils.integrations.Constants.MISC_PATH -import app.revanced.patches.shared.tracking.BaseSanitizeUrlQueryPatch -import app.revanced.patches.shared.tracking.fingerprints.CopyTextEndpointFingerprint - -object SanitizeUrlQueryBytecodePatch : BaseSanitizeUrlQueryPatch( - "$MISC_PATH/SanitizeUrlQueryPatch;", - listOf( - CopyTextEndpointFingerprint, - ShareLinkFormatterFingerprint - ), - null -) diff --git a/src/main/kotlin/app/revanced/patches/music/misc/tracking/SanitizeUrlQueryPatch.kt b/src/main/kotlin/app/revanced/patches/music/misc/tracking/SanitizeUrlQueryPatch.kt index d7a1eccfd9..f473af5761 100644 --- a/src/main/kotlin/app/revanced/patches/music/misc/tracking/SanitizeUrlQueryPatch.kt +++ b/src/main/kotlin/app/revanced/patches/music/misc/tracking/SanitizeUrlQueryPatch.kt @@ -4,6 +4,7 @@ import app.revanced.patcher.data.ResourceContext import app.revanced.patches.music.utils.compatibility.Constants.COMPATIBLE_PACKAGE import app.revanced.patches.music.utils.settings.CategoryType import app.revanced.patches.music.utils.settings.SettingsPatch +import app.revanced.patches.shared.tracking.BaseSanitizeUrlQueryPatch import app.revanced.util.patch.BaseResourcePatch @Suppress("unused") @@ -11,7 +12,7 @@ object SanitizeUrlQueryPatch : BaseResourcePatch( name = "Sanitize sharing links", description = "Adds an option to remove tracking query parameters from URLs when sharing links.", dependencies = setOf( - SanitizeUrlQueryBytecodePatch::class, + BaseSanitizeUrlQueryPatch::class, SettingsPatch::class ), compatiblePackages = COMPATIBLE_PACKAGE diff --git a/src/main/kotlin/app/revanced/patches/shared/tracking/BaseSanitizeUrlQueryPatch.kt b/src/main/kotlin/app/revanced/patches/shared/tracking/BaseSanitizeUrlQueryPatch.kt index a8cc7da0fc..2ec29d35f7 100644 --- a/src/main/kotlin/app/revanced/patches/shared/tracking/BaseSanitizeUrlQueryPatch.kt +++ b/src/main/kotlin/app/revanced/patches/shared/tracking/BaseSanitizeUrlQueryPatch.kt @@ -3,39 +3,69 @@ package app.revanced.patches.shared.tracking import app.revanced.patcher.data.BytecodeContext import app.revanced.patcher.extensions.InstructionExtensions.addInstructions import app.revanced.patcher.extensions.InstructionExtensions.getInstruction -import app.revanced.patcher.fingerprint.MethodFingerprint +import app.revanced.patcher.extensions.InstructionExtensions.replaceInstruction import app.revanced.patcher.patch.BytecodePatch +import app.revanced.patches.shared.integrations.Constants.PATCHES_PATH +import app.revanced.patches.shared.tracking.fingerprints.CopyTextEndpointFingerprint +import app.revanced.patches.shared.tracking.fingerprints.ShareLinkFormatterFingerprint +import app.revanced.patches.shared.tracking.fingerprints.SystemShareLinkFormatterFingerprint import app.revanced.util.resultOrThrow +import com.android.tools.smali.dexlib2.Opcode +import com.android.tools.smali.dexlib2.iface.instruction.FiveRegisterInstruction +import com.android.tools.smali.dexlib2.iface.instruction.ReferenceInstruction import com.android.tools.smali.dexlib2.iface.instruction.TwoRegisterInstruction -abstract class BaseSanitizeUrlQueryPatch( - private val descriptor: String, - private val sharedFingerprints: List, - private val additionalFingerprints: List? = null -) : BytecodePatch( - buildSet { - addAll(sharedFingerprints) - additionalFingerprints?.let(::addAll) - } +object BaseSanitizeUrlQueryPatch : BytecodePatch( + setOf( + CopyTextEndpointFingerprint, + ShareLinkFormatterFingerprint, + SystemShareLinkFormatterFingerprint + ) ) { - private fun MethodFingerprint.invoke() { - resultOrThrow().let { + private const val INTEGRATIONS_CLASS_DESCRIPTOR = + "$PATCHES_PATH/SanitizeUrlQueryPatch;" + + override fun execute(context: BytecodeContext) { + CopyTextEndpointFingerprint.resultOrThrow().let { it.mutableMethod.apply { val targetIndex = it.scanResult.patternScanResult!!.startIndex val targetRegister = getInstruction(targetIndex).registerA addInstructions( targetIndex + 2, """ - invoke-static {v$targetRegister}, $descriptor->stripQueryParameters(Ljava/lang/String;)Ljava/lang/String; + invoke-static {v$targetRegister}, $INTEGRATIONS_CLASS_DESCRIPTOR->stripQueryParameters(Ljava/lang/String;)Ljava/lang/String; move-result-object v$targetRegister """ ) } } - } - override fun execute(context: BytecodeContext) { - for (fingerprint in sharedFingerprints) - fingerprint.invoke() + arrayOf( + ShareLinkFormatterFingerprint, + SystemShareLinkFormatterFingerprint + ).forEach { fingerprint -> + fingerprint.resultOrThrow().let { + it.mutableMethod.apply { + for ((index, instruction) in implementation!!.instructions.withIndex()) { + if (instruction.opcode != Opcode.INVOKE_VIRTUAL) + continue + + if ((instruction as ReferenceInstruction).reference.toString() != "Landroid/content/Intent;->putExtra(Ljava/lang/String;Ljava/lang/String;)Landroid/content/Intent;") + continue + + if (getInstruction(index + 1).opcode != Opcode.GOTO) + continue + + val invokeInstruction = instruction as FiveRegisterInstruction + + replaceInstruction( + index, + "invoke-static {v${invokeInstruction.registerC}, v${invokeInstruction.registerD}, v${invokeInstruction.registerE}}, " + + "$INTEGRATIONS_CLASS_DESCRIPTOR->stripQueryParameters(Landroid/content/Intent;Ljava/lang/String;Ljava/lang/String;)V" + ) + } + } + } + } } } \ No newline at end of file diff --git a/src/main/kotlin/app/revanced/patches/music/misc/tracking/fingerprints/ShareLinkFormatterFingerprint.kt b/src/main/kotlin/app/revanced/patches/shared/tracking/fingerprints/ShareLinkFormatterFingerprint.kt similarity index 83% rename from src/main/kotlin/app/revanced/patches/music/misc/tracking/fingerprints/ShareLinkFormatterFingerprint.kt rename to src/main/kotlin/app/revanced/patches/shared/tracking/fingerprints/ShareLinkFormatterFingerprint.kt index 151493d527..272c9b1f1f 100644 --- a/src/main/kotlin/app/revanced/patches/music/misc/tracking/fingerprints/ShareLinkFormatterFingerprint.kt +++ b/src/main/kotlin/app/revanced/patches/shared/tracking/fingerprints/ShareLinkFormatterFingerprint.kt @@ -1,4 +1,4 @@ -package app.revanced.patches.music.misc.tracking.fingerprints +package app.revanced.patches.shared.tracking.fingerprints import app.revanced.patcher.fingerprint.MethodFingerprint import com.android.tools.smali.dexlib2.Opcode @@ -6,16 +6,14 @@ import com.android.tools.smali.dexlib2.iface.instruction.ReferenceInstruction import com.android.tools.smali.dexlib2.iface.reference.FieldReference /** - * Sharing panel of YouTube Music + * Sharing panel */ internal object ShareLinkFormatterFingerprint : MethodFingerprint( - returnType = "V", - parameters = listOf("L", "Ljava/util/Map;"), opcodes = listOf( Opcode.IGET_OBJECT, Opcode.CHECK_CAST, Opcode.GOTO, - Opcode.CONST_STRING, + null, Opcode.INVOKE_VIRTUAL ), customFingerprint = custom@{ methodDef, _ -> diff --git a/src/main/kotlin/app/revanced/patches/youtube/misc/tracking/fingerprints/SystemShareLinkFormatterFingerprint.kt b/src/main/kotlin/app/revanced/patches/shared/tracking/fingerprints/SystemShareLinkFormatterFingerprint.kt similarity index 83% rename from src/main/kotlin/app/revanced/patches/youtube/misc/tracking/fingerprints/SystemShareLinkFormatterFingerprint.kt rename to src/main/kotlin/app/revanced/patches/shared/tracking/fingerprints/SystemShareLinkFormatterFingerprint.kt index 63df46e822..446cef57e2 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/misc/tracking/fingerprints/SystemShareLinkFormatterFingerprint.kt +++ b/src/main/kotlin/app/revanced/patches/shared/tracking/fingerprints/SystemShareLinkFormatterFingerprint.kt @@ -1,4 +1,4 @@ -package app.revanced.patches.youtube.misc.tracking.fingerprints +package app.revanced.patches.shared.tracking.fingerprints import app.revanced.patcher.fingerprint.MethodFingerprint diff --git a/src/main/kotlin/app/revanced/patches/youtube/misc/tracking/SanitizeUrlQueryBytecodePatch.kt b/src/main/kotlin/app/revanced/patches/youtube/misc/tracking/SanitizeUrlQueryBytecodePatch.kt deleted file mode 100644 index 6eee7aa601..0000000000 --- a/src/main/kotlin/app/revanced/patches/youtube/misc/tracking/SanitizeUrlQueryBytecodePatch.kt +++ /dev/null @@ -1,58 +0,0 @@ -package app.revanced.patches.youtube.misc.tracking - -import app.revanced.patcher.data.BytecodeContext -import app.revanced.patcher.extensions.InstructionExtensions.getInstruction -import app.revanced.patcher.extensions.InstructionExtensions.replaceInstruction -import app.revanced.patches.shared.tracking.BaseSanitizeUrlQueryPatch -import app.revanced.patches.shared.tracking.fingerprints.CopyTextEndpointFingerprint -import app.revanced.patches.youtube.misc.tracking.fingerprints.ShareLinkFormatterFingerprint -import app.revanced.patches.youtube.misc.tracking.fingerprints.SystemShareLinkFormatterFingerprint -import app.revanced.patches.youtube.utils.integrations.Constants.MISC_PATH -import app.revanced.util.resultOrThrow -import com.android.tools.smali.dexlib2.Opcode -import com.android.tools.smali.dexlib2.iface.instruction.FiveRegisterInstruction -import com.android.tools.smali.dexlib2.iface.instruction.ReferenceInstruction - -object SanitizeUrlQueryBytecodePatch : BaseSanitizeUrlQueryPatch( - "$MISC_PATH/SanitizeUrlQueryPatch;", - listOf(CopyTextEndpointFingerprint), - listOf( - ShareLinkFormatterFingerprint, - SystemShareLinkFormatterFingerprint - ) -) { - private const val INTEGRATIONS_CLASS_DESCRIPTOR = - "$MISC_PATH/SanitizeUrlQueryPatch;" - - override fun execute(context: BytecodeContext) { - super.execute(context) - - arrayOf( - ShareLinkFormatterFingerprint, - SystemShareLinkFormatterFingerprint - ).forEach { fingerprint -> - fingerprint.resultOrThrow().let { - it.mutableMethod.apply { - for ((index, instruction) in implementation!!.instructions.withIndex()) { - if (instruction.opcode != Opcode.INVOKE_VIRTUAL) - continue - - if ((instruction as ReferenceInstruction).reference.toString() != "Landroid/content/Intent;->putExtra(Ljava/lang/String;Ljava/lang/String;)Landroid/content/Intent;") - continue - - if (getInstruction(index + 1).opcode != Opcode.GOTO) - continue - - val invokeInstruction = instruction as FiveRegisterInstruction - - replaceInstruction( - index, - "invoke-static {v${invokeInstruction.registerC}, v${invokeInstruction.registerD}, v${invokeInstruction.registerE}}, " - + "$INTEGRATIONS_CLASS_DESCRIPTOR->stripQueryParameters(Landroid/content/Intent;Ljava/lang/String;Ljava/lang/String;)V" - ) - } - } - } - } - } -} diff --git a/src/main/kotlin/app/revanced/patches/youtube/misc/tracking/SanitizeUrlQueryPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/misc/tracking/SanitizeUrlQueryPatch.kt index 6abfd09c32..b951ddfc56 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/misc/tracking/SanitizeUrlQueryPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/misc/tracking/SanitizeUrlQueryPatch.kt @@ -1,6 +1,7 @@ package app.revanced.patches.youtube.misc.tracking import app.revanced.patcher.data.ResourceContext +import app.revanced.patches.shared.tracking.BaseSanitizeUrlQueryPatch import app.revanced.patches.youtube.utils.compatibility.Constants.COMPATIBLE_PACKAGE import app.revanced.patches.youtube.utils.settings.SettingsPatch import app.revanced.util.patch.BaseResourcePatch @@ -10,7 +11,7 @@ object SanitizeUrlQueryPatch : BaseResourcePatch( name = "Sanitize sharing links", description = "Adds an option to remove tracking query parameters from URLs when sharing links.", dependencies = setOf( - SanitizeUrlQueryBytecodePatch::class, + BaseSanitizeUrlQueryPatch::class, SettingsPatch::class ), compatiblePackages = COMPATIBLE_PACKAGE diff --git a/src/main/kotlin/app/revanced/patches/youtube/misc/tracking/fingerprints/ShareLinkFormatterFingerprint.kt b/src/main/kotlin/app/revanced/patches/youtube/misc/tracking/fingerprints/ShareLinkFormatterFingerprint.kt deleted file mode 100644 index 4b76887645..0000000000 --- a/src/main/kotlin/app/revanced/patches/youtube/misc/tracking/fingerprints/ShareLinkFormatterFingerprint.kt +++ /dev/null @@ -1,39 +0,0 @@ -package app.revanced.patches.youtube.misc.tracking.fingerprints - -import app.revanced.patcher.fingerprint.MethodFingerprint -import com.android.tools.smali.dexlib2.Opcode -import com.android.tools.smali.dexlib2.iface.instruction.ReferenceInstruction -import com.android.tools.smali.dexlib2.iface.reference.FieldReference - -/** - * Sharing panel of YouTube - * - * This fingerprint is quite complex to be compatible with all versions from YouTube v18.25.40 to the latest version. - * If you drop supporting the old version, please change the fingerprint to be more intuitive. - */ -internal object ShareLinkFormatterFingerprint : MethodFingerprint( - opcodes = listOf( - Opcode.IGET_OBJECT, - Opcode.CHECK_CAST, - Opcode.GOTO, - Opcode.MOVE_OBJECT, - Opcode.INVOKE_VIRTUAL - ), - customFingerprint = custom@{ methodDef, _ -> - if (methodDef.implementation == null) - return@custom false - - var count = 0 - for (instruction in methodDef.implementation!!.instructions) { - if (instruction.opcode != Opcode.SGET_OBJECT) - continue - - val objectInstruction = instruction as ReferenceInstruction - if ((objectInstruction.reference as FieldReference).name != "androidAppEndpoint") - continue - - count++ - } - count == 2 - } -) \ No newline at end of file