|
| 1 | +package app.revanced.patches.tiktok.misc.share |
| 2 | + |
| 3 | +import app.revanced.patcher.extensions.InstructionExtensions.addInstructions |
| 4 | +import app.revanced.patcher.extensions.InstructionExtensions.addInstructionsWithLabels |
| 5 | +import app.revanced.patcher.extensions.InstructionExtensions.getInstruction |
| 6 | +import app.revanced.patcher.patch.bytecodePatch |
| 7 | +import app.revanced.patches.shared.PATCH_DESCRIPTION_SANITIZE_SHARING_LINKS |
| 8 | +import app.revanced.patches.shared.PATCH_NAME_SANITIZE_SHARING_LINKS |
| 9 | +import app.revanced.patches.tiktok.misc.extension.sharedExtensionPatch |
| 10 | +import app.revanced.util.findFreeRegister |
| 11 | +import app.revanced.util.getReference |
| 12 | +import app.revanced.util.indexOfFirstInstructionOrThrow |
| 13 | +import com.android.tools.smali.dexlib2.Opcode |
| 14 | +import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction |
| 15 | +import com.android.tools.smali.dexlib2.iface.instruction.ReferenceInstruction |
| 16 | +import com.android.tools.smali.dexlib2.iface.reference.MethodReference |
| 17 | + |
| 18 | +private const val EXTENSION_CLASS_DESCRIPTOR = |
| 19 | + "Lapp/revanced/extension/tiktok/share/ShareUrlSanitizer;" |
| 20 | + |
| 21 | +@Suppress("unused") |
| 22 | +val sanitizeShareUrlsPatch = bytecodePatch( |
| 23 | + name = PATCH_NAME_SANITIZE_SHARING_LINKS, |
| 24 | + description = PATCH_DESCRIPTION_SANITIZE_SHARING_LINKS, |
| 25 | +) { |
| 26 | + dependsOn(sharedExtensionPatch) |
| 27 | + |
| 28 | + compatibleWith( |
| 29 | + "com.ss.android.ugc.trill"("36.5.4"), |
| 30 | + "com.zhiliaoapp.musically"("36.5.4"), |
| 31 | + ) |
| 32 | + |
| 33 | + execute { |
| 34 | + urlShorteningFingerprint.method.apply { |
| 35 | + val invokeIndex = indexOfFirstInstructionOrThrow { |
| 36 | + val ref = getReference<MethodReference>() |
| 37 | + ref?.name == "LIZ" && ref.definingClass.startsWith("LX/") |
| 38 | + } |
| 39 | + |
| 40 | + val moveResultIndex = indexOfFirstInstructionOrThrow(invokeIndex, Opcode.MOVE_RESULT_OBJECT) |
| 41 | + val urlRegister = getInstruction<OneRegisterInstruction>(moveResultIndex).registerA |
| 42 | + |
| 43 | + // Resolve Observable wrapper classes at runtime |
| 44 | + val observableWrapperIndex = indexOfFirstInstructionOrThrow(Opcode.NEW_INSTANCE) |
| 45 | + val observableWrapperClass = getInstruction<ReferenceInstruction>(observableWrapperIndex) |
| 46 | + .reference.toString() |
| 47 | + |
| 48 | + val observableFactoryIndex = indexOfFirstInstructionOrThrow { |
| 49 | + val ref = getReference<MethodReference>() |
| 50 | + ref?.name == "LJ" && ref.definingClass.startsWith("LX/") |
| 51 | + } |
| 52 | + val observableFactoryRef = getInstruction<ReferenceInstruction>(observableFactoryIndex) |
| 53 | + .reference as MethodReference |
| 54 | + |
| 55 | + val observableFactoryClass = observableFactoryRef.definingClass |
| 56 | + val observableInterfaceType = observableFactoryRef.parameterTypes.first() |
| 57 | + val observableReturnType = observableFactoryRef.returnType |
| 58 | + |
| 59 | + val wrapperRegister = findFreeRegister(moveResultIndex + 1, urlRegister) |
| 60 | + |
| 61 | + // Check setting and conditionally sanitize share URL. |
| 62 | + addInstructionsWithLabels( |
| 63 | + moveResultIndex + 1, |
| 64 | + """ |
| 65 | + invoke-static {}, $EXTENSION_CLASS_DESCRIPTOR->shouldSanitize()Z |
| 66 | + move-result v$wrapperRegister |
| 67 | + if-eqz v$wrapperRegister, :skip_sanitization |
| 68 | +
|
| 69 | + invoke-static { p1 }, $EXTENSION_CLASS_DESCRIPTOR->sanitizeShareUrl(Ljava/lang/String;)Ljava/lang/String; |
| 70 | + move-result-object v$urlRegister |
| 71 | +
|
| 72 | + # Wrap sanitized URL and return early to bypass ShareExtService |
| 73 | + new-instance v$wrapperRegister, $observableWrapperClass |
| 74 | + invoke-direct { v$wrapperRegister, v$urlRegister }, $observableWrapperClass-><init>(Ljava/lang/String;)V |
| 75 | + invoke-static { v$wrapperRegister }, $observableFactoryClass->LJ($observableInterfaceType)$observableReturnType |
| 76 | + move-result-object v$urlRegister |
| 77 | + return-object v$urlRegister |
| 78 | +
|
| 79 | + :skip_sanitization |
| 80 | + nop |
| 81 | + """ |
| 82 | + ) |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments