From 48bbd574a52c8bf6834b26facfe7384b830f944a Mon Sep 17 00:00:00 2001 From: oSumAtrIX Date: Sat, 11 Jun 2022 06:37:18 +0200 Subject: [PATCH] feat: `microg-patch` --- .../app/revanced/extensions/Extensions.kt | 6 +- .../annotations/MicroGPatchCompatibility.kt | 2 +- .../patch/bytecode/MicroGBytecodePatch.kt | 81 ++++++++----------- .../patch/resource/MicroGResourcePatch.kt | 64 +++++++-------- .../youtube/misc/microg/shared/Constants.kt | 1 + 5 files changed, 67 insertions(+), 87 deletions(-) diff --git a/src/main/kotlin/app/revanced/extensions/Extensions.kt b/src/main/kotlin/app/revanced/extensions/Extensions.kt index 7797d890c..71cd8930d 100644 --- a/src/main/kotlin/app/revanced/extensions/Extensions.kt +++ b/src/main/kotlin/app/revanced/extensions/Extensions.kt @@ -27,9 +27,9 @@ internal fun String.startsWithAny(vararg prefix: String): Boolean { return false } -internal fun String.containsAny(vararg others: String): Boolean { - for (other in others) - if (this.contains(other)) +internal fun String.equalsAny(vararg other: String): Boolean { + for (_other in other) + if (this == _other) return true return false diff --git a/src/main/kotlin/app/revanced/patches/youtube/misc/microg/annotations/MicroGPatchCompatibility.kt b/src/main/kotlin/app/revanced/patches/youtube/misc/microg/annotations/MicroGPatchCompatibility.kt index b5f9ef996..a9c940234 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/misc/microg/annotations/MicroGPatchCompatibility.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/misc/microg/annotations/MicroGPatchCompatibility.kt @@ -5,7 +5,7 @@ import app.revanced.patcher.annotation.Package @Compatibility( [Package( - "com.google.android.youtube", arrayOf("17.14.35", "17.19.36") + "com.google.android.youtube", arrayOf("17.14.35", "17.19.36", "17.20.37") )] ) @Target(AnnotationTarget.CLASS) diff --git a/src/main/kotlin/app/revanced/patches/youtube/misc/microg/patch/bytecode/MicroGBytecodePatch.kt b/src/main/kotlin/app/revanced/patches/youtube/misc/microg/patch/bytecode/MicroGBytecodePatch.kt index 4b23fe84a..632481241 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/misc/microg/patch/bytecode/MicroGBytecodePatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/misc/microg/patch/bytecode/MicroGBytecodePatch.kt @@ -1,24 +1,22 @@ package app.revanced.patches.youtube.misc.microg.patch.bytecode -import app.revanced.extensions.containsAny -import app.revanced.extensions.startsWithAny +import app.revanced.extensions.equalsAny import app.revanced.patcher.annotation.Description import app.revanced.patcher.annotation.Name import app.revanced.patcher.annotation.Version import app.revanced.patcher.data.implementation.BytecodeData import app.revanced.patcher.data.implementation.proxy import app.revanced.patcher.extensions.addInstructions -import app.revanced.patcher.extensions.or +import app.revanced.patcher.patch.annotations.Dependencies +import app.revanced.patcher.patch.annotations.Patch import app.revanced.patcher.patch.implementation.BytecodePatch import app.revanced.patcher.patch.implementation.misc.PatchResult import app.revanced.patcher.patch.implementation.misc.PatchResultSuccess -import app.revanced.patcher.signature.implementation.method.MethodSignature -import app.revanced.patcher.signature.implementation.method.annotation.DirectPatternScanMethod -import app.revanced.patcher.signature.implementation.method.annotation.MatchingMethod import app.revanced.patcher.util.proxy.mutableTypes.MutableClass import app.revanced.patcher.util.smali.toInstruction import app.revanced.patcher.util.smali.toInstructions import app.revanced.patches.youtube.misc.microg.annotations.MicroGPatchCompatibility +import app.revanced.patches.youtube.misc.microg.patch.resource.MicroGResourcePatch import app.revanced.patches.youtube.misc.microg.patch.resource.enum.StringReplaceMode import app.revanced.patches.youtube.misc.microg.shared.Constants.BASE_MICROG_PACKAGE_NAME import app.revanced.patches.youtube.misc.microg.shared.Constants.REVANCED_PACKAGE_NAME @@ -26,7 +24,6 @@ import app.revanced.patches.youtube.misc.microg.signatures.GooglePlayUtilitySign import app.revanced.patches.youtube.misc.microg.signatures.IntegrityCheckSignature import app.revanced.patches.youtube.misc.microg.signatures.PrimeSignature import app.revanced.patches.youtube.misc.microg.signatures.ServiceCheckSignature -import org.jf.dexlib2.AccessFlags import org.jf.dexlib2.Opcode import org.jf.dexlib2.builder.MutableMethodImplementation import org.jf.dexlib2.builder.instruction.BuilderInstruction21c @@ -34,8 +31,9 @@ import org.jf.dexlib2.iface.instruction.formats.Instruction21c import org.jf.dexlib2.iface.reference.StringReference import org.jf.dexlib2.immutable.reference.ImmutableStringReference -// @Patch TODO: finish patch -@Name("microg-bytecode-patch") +@Patch(excludeByDefault = true) +@Dependencies(dependencies = [MicroGResourcePatch::class]) +@Name("microg-patch") @Description("Patch to allow YouTube ReVanced to run without root and under a different package name.") @MicroGPatchCompatibility @Version("0.0.1") @@ -59,30 +57,33 @@ class MicroGBytecodePatch : BytecodePatch( val stringValue = ((instruction as Instruction21c).reference as StringReference).string - val replaceMode = - if (stringValue == "com.google" || stringValue == "com.google.android.gms" || - stringValue.startsWithAny( - "com.google.iid", - "com.google.android.gms.chimera", - "com.google.android.c2dm", - ) || stringValue.containsAny( - "com.google.android.gms.auth.accounts", - "com.google.android.gsf", - "content://com.google.settings" - ) - ) { - StringReplaceMode.REPLACE_WITH_MICROG - } else if (stringValue.startsWith("com.google.android.gms.chimera.container")) // https://github.com/TeamVanced/VancedMicroG/pull/139/file - StringReplaceMode.DO_NOT_REPLACE - else if (stringValue.startsWithAny( - "com.google.android.youtube.SuggestionsProvider", - "com.google.android.youtube.fileprovider" - ) - ) { - StringReplaceMode.REPLACE_WITH_REVANCED - } else { - StringReplaceMode.DO_NOT_REPLACE - } + val replaceMode = if (stringValue.equalsAny( + "com.google.android.gms", + "com.google.android.youtube.fileprovider", + "com.google.android.c2dm.intent.REGISTER", + "com.google.android.c2dm.permission.SEND", + "com.google.iid.TOKEN_REQUEST", + "com.google", + "com.google.android.gms.auth.accounts", + "com.google.android.youtube.SuggestionProvider", + "com.google.android.c2dm.intent.REGISTRATION", + "com.google.android.gsf.action.GET_GLS", + "com.google.android.gsf.login", + "content://com.google.settings/partner", + "content://com.google.android.gsf.gservices", + "content://com.google.android.gsf.gservices/prefix", + "com.google.android.c2dm.intent.RECEIVE" + ) + ) { + StringReplaceMode.REPLACE_WITH_MICROG + } else if (stringValue.equalsAny( + "com.google.android.youtube.SuggestionsProvider", "com.google.android.youtube.fileprovider" + ) + ) { + StringReplaceMode.REPLACE_WITH_REVANCED + } else { + StringReplaceMode.DO_NOT_REPLACE + } if (replaceMode != StringReplaceMode.DO_NOT_REPLACE) { if (proxiedClass == null) { @@ -110,20 +111,6 @@ class MicroGBytecodePatch : BytecodePatch( } } - - // replace string back - val implementation = - signatures.elementAt(2).result!!.findParentMethod(@Name("do-not-replace-method") @MatchingMethod( - "Llpe;", "c" - ) @DirectPatternScanMethod @MicroGPatchCompatibility @Version("0.0.1") object : MethodSignature( - "L", AccessFlags.PUBLIC or AccessFlags.STATIC, listOf("L"), null, listOf("com.google.android.gms") - ) {})!!.method.implementation!! - - implementation.replaceInstruction( - implementation.instructions.indexOfFirst { it.opcode == Opcode.CONST_STRING }, - "const-string v0, \"com.google.android.gms\"".toInstruction() - ) - return PatchResultSuccess() } diff --git a/src/main/kotlin/app/revanced/patches/youtube/misc/microg/patch/resource/MicroGResourcePatch.kt b/src/main/kotlin/app/revanced/patches/youtube/misc/microg/patch/resource/MicroGResourcePatch.kt index 074674f80..b99a62733 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/misc/microg/patch/resource/MicroGResourcePatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/misc/microg/patch/resource/MicroGResourcePatch.kt @@ -9,61 +9,53 @@ import app.revanced.patcher.patch.implementation.misc.PatchResult import app.revanced.patcher.patch.implementation.misc.PatchResultSuccess import app.revanced.patches.youtube.misc.microg.annotations.MicroGPatchCompatibility import app.revanced.patches.youtube.misc.microg.shared.Constants.BASE_MICROG_PACKAGE_NAME +import app.revanced.patches.youtube.misc.microg.shared.Constants.REVANCED_APP_NAME import app.revanced.patches.youtube.misc.microg.shared.Constants.REVANCED_PACKAGE_NAME -// @Patch TODO: finish patch @Name("microg-resource-patch") @Description("Resource patch to allow YouTube ReVanced to run without root and under a different package name.") @MicroGPatchCompatibility @Version("0.0.1") class MicroGResourcePatch : ResourcePatch() { override fun execute(data: ResourceData): PatchResult { + data.getXmlEditor("res/xml/settings_fragment.xml").use { + val settingsElementIntent = it.file.createElement("intent") + settingsElementIntent.setAttribute("android:targetPackage", "$BASE_MICROG_PACKAGE_NAME.android.gms") + settingsElementIntent.setAttribute("android:targetClass", "org.microg.gms.ui.SettingsActivity") + + val settingsElement = it.file.createElement("Preference") + settingsElement.setAttribute("android:title", "MicroG") + settingsElement.appendChild(settingsElementIntent) + + it.file.firstChild.appendChild(settingsElement) + } + val manifest = data.get("AndroidManifest.xml").readText() data.get("AndroidManifest.xml").writeText( manifest.replace( - "package=\"com.google.android.youtube\"", "package=\"$REVANCED_PACKAGE_NAME\"" + "package=\"com.google.android.youtube", "package=\"$REVANCED_PACKAGE_NAME" ).replace( - " android:label=\"@string/application_name\" ", " android:label=\"{APP_NAME}\" " + "android:label=\"@string/application_name", "android:label=\"$REVANCED_APP_NAME" ).replace( - "", - " \n " - ) - ) - - val replacement = arrayOf( - Pair( - "com.google.android.youtube.SuggestionProvider", "$REVANCED_PACKAGE_NAME.SuggestionProvider" - ), Pair( "com.google.android.youtube.fileprovider", "$REVANCED_PACKAGE_NAME.fileprovider" + ).replace( + "com.google.android.youtube.SuggestionProvider", "$REVANCED_PACKAGE_NAME.SuggestionProvider" + ).replace( + "com.google.android.youtube.permission.C2D_MESSAGE", "$REVANCED_PACKAGE_NAME.permission.C2D_MESSAGE" + ).replace( // TODO: might not be needed + "com.google.android.youtube.lifecycle-trojan", "$REVANCED_PACKAGE_NAME.lifecycle-trojan" + ).replace( // TODO: might not be needed + "com.google.android.youtube.photopicker_images", "$REVANCED_PACKAGE_NAME.photopicker_images" + ).replace( + "com.google.android.c2dm", "$BASE_MICROG_PACKAGE_NAME.android.c2dm" + ).replace( + "", "" ) ) - data.forEach { - if (it.extension != "xml") return@forEach - - // TODO: use a reader and only replace strings where needed instead of reading & writing the entire file - var content = it.readText() - replacement.filter { translation -> content.contains(translation.first) }.forEach { translation -> - content = content.replace(translation.first, translation.second) - } - it.writeText(content) - } - return PatchResultSuccess() } } \ No newline at end of file diff --git a/src/main/kotlin/app/revanced/patches/youtube/misc/microg/shared/Constants.kt b/src/main/kotlin/app/revanced/patches/youtube/misc/microg/shared/Constants.kt index 768101e0a..5d56903c9 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/misc/microg/shared/Constants.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/misc/microg/shared/Constants.kt @@ -3,4 +3,5 @@ package app.revanced.patches.youtube.misc.microg.shared object Constants { internal const val BASE_MICROG_PACKAGE_NAME = "com.mgoogle" internal const val REVANCED_PACKAGE_NAME = "app.revanced.android.youtube" + internal const val REVANCED_APP_NAME = "YouTube ReVanced" } \ No newline at end of file