feat(reddit): uri-parameter-reddit patch

This commit is contained in:
inotia00 2023-05-07 05:31:05 +09:00
parent 7f3f75dc44
commit 42b25fedf9
3 changed files with 105 additions and 0 deletions

View File

@ -0,0 +1,15 @@
package app.revanced.patches.reddit.misc.uriparameters.annotations
import app.revanced.patcher.annotation.Compatibility
import app.revanced.patcher.annotation.Package
@Compatibility(
[Package(
"com.reddit.frontpage", arrayOf(
"2023.12.0",
"2023.17.1"
)
)]
)
@Target(AnnotationTarget.CLASS)
internal annotation class UriParametersCompatibility

View File

@ -0,0 +1,22 @@
package app.revanced.patches.reddit.misc.uriparameters.fingerprints
import app.revanced.patcher.extensions.or
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import org.jf.dexlib2.AccessFlags
import org.jf.dexlib2.Opcode
object ShareLinkFactoryFingerprint : MethodFingerprint(
returnType = "L",
access = AccessFlags.PUBLIC or AccessFlags.FINAL,
opcodes = listOf(
Opcode.CONST_STRING,
Opcode.CONST_STRING,
Opcode.INVOKE_DIRECT,
Opcode.APUT_OBJECT,
Opcode.INVOKE_STATIC,
Opcode.MOVE_RESULT_OBJECT,
Opcode.INVOKE_STATIC, // returnType: Ljava/lang/String;
Opcode.MOVE_RESULT_OBJECT
),
customFingerprint = { it.definingClass.endsWith("ShareLinkFactory;") }
)

View File

@ -0,0 +1,68 @@
package app.revanced.patches.reddit.misc.uriparameters.patch
import app.revanced.extensions.toErrorResult
import app.revanced.patcher.annotation.Description
import app.revanced.patcher.annotation.Name
import app.revanced.patcher.annotation.Version
import app.revanced.patcher.data.BytecodeContext
import app.revanced.patcher.extensions.addInstructions
import app.revanced.patcher.extensions.instruction
import app.revanced.patcher.patch.BytecodePatch
import app.revanced.patcher.patch.PatchResult
import app.revanced.patcher.patch.PatchResultError
import app.revanced.patcher.patch.PatchResultSuccess
import app.revanced.patcher.patch.annotations.Patch
import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod
import app.revanced.patches.reddit.misc.uriparameters.annotations.UriParametersCompatibility
import app.revanced.patches.reddit.misc.uriparameters.fingerprints.ShareLinkFactoryFingerprint
import org.jf.dexlib2.builder.instruction.BuilderInstruction35c
import org.jf.dexlib2.iface.instruction.OneRegisterInstruction
@Patch
@Name("uri-parameter-reddit")
@Description("Remove uri parameter when sharing links in reddit.")
@UriParametersCompatibility
@Version("0.0.1")
class UriParametersPatch : BytecodePatch(
listOf(
ShareLinkFactoryFingerprint
)
) {
override fun execute(context: BytecodeContext): PatchResult {
ShareLinkFactoryFingerprint.result?.let {
it.mutableMethod.apply {
val startIndex = it.scanResult.patternScanResult!!.startIndex
val endIndex = it.scanResult.patternScanResult!!.endIndex
val parameter = (instruction(endIndex - 1) as BuilderInstruction35c).reference
if (!parameter.toString().endsWith("Ljava/lang/String;"))
return PatchResultError("Method signature parameter did not match: $parameter")
val matcherRegister = getRegister(startIndex)
val tempRegister = getRegister(startIndex + 1)
val uriRegister = getRegister(endIndex)
addInstructions(
endIndex + 1,
"""
const-string v$tempRegister, ".?utm_source=.+"
invoke-static {v$tempRegister}, Ljava/util/regex/Pattern;->compile(Ljava/lang/String;)Ljava/util/regex/Pattern;
move-result-object v$matcherRegister
invoke-virtual {v$matcherRegister, v$uriRegister}, Ljava/util/regex/Pattern;->matcher(Ljava/lang/CharSequence;)Ljava/util/regex/Matcher;
move-result-object v$matcherRegister
const-string v$tempRegister, ""
invoke-virtual {v$matcherRegister, v$tempRegister}, Ljava/util/regex/Matcher;->replaceAll(Ljava/lang/String;)Ljava/lang/String;
move-result-object v$uriRegister
"""
)
}
} ?: return ShareLinkFactoryFingerprint.toErrorResult()
return PatchResultSuccess()
}
private fun MutableMethod.getRegister(index: Int):Int {
return (instruction(index) as OneRegisterInstruction).registerA
}
}