From 2223951fa16011d735ef4c4cda11eba13403f62d Mon Sep 17 00:00:00 2001
From: oSumAtrIX <johan.melkonyan1@web.de>
Date: Sun, 8 Oct 2023 02:56:23 +0200
Subject: [PATCH] fix(Boost for Reddit - Spoof client): Set user-agent for
 OAuth requests to fix login

---
 .../boostforreddit/api/SpoofClientPatch.kt    | 22 ++++++++++++++++++-
 .../LoginActivityOnCreateFingerprint.kt       | 14 ++++++++++++
 2 files changed, 35 insertions(+), 1 deletion(-)
 create mode 100644 src/main/kotlin/app/revanced/patches/reddit/customclients/boostforreddit/api/fingerprints/LoginActivityOnCreateFingerprint.kt

diff --git a/src/main/kotlin/app/revanced/patches/reddit/customclients/boostforreddit/api/SpoofClientPatch.kt b/src/main/kotlin/app/revanced/patches/reddit/customclients/boostforreddit/api/SpoofClientPatch.kt
index 137cb137..7d8ac71f 100644
--- a/src/main/kotlin/app/revanced/patches/reddit/customclients/boostforreddit/api/SpoofClientPatch.kt
+++ b/src/main/kotlin/app/revanced/patches/reddit/customclients/boostforreddit/api/SpoofClientPatch.kt
@@ -6,7 +6,9 @@ import app.revanced.patcher.fingerprint.method.impl.MethodFingerprintResult
 import app.revanced.patcher.patch.annotation.CompatiblePackage
 import app.revanced.patcher.patch.annotation.Patch
 import app.revanced.patches.reddit.customclients.AbstractSpoofClientPatch
+import app.revanced.patches.reddit.customclients.Constants.OAUTH_USER_AGENT
 import app.revanced.patches.reddit.customclients.boostforreddit.api.fingerprints.GetClientIdFingerprint
+import app.revanced.patches.reddit.customclients.boostforreddit.api.fingerprints.LoginActivityOnCreateFingerprint
 
 @Patch(
     name = "Spoof client",
@@ -15,7 +17,9 @@ import app.revanced.patches.reddit.customclients.boostforreddit.api.fingerprints
 )
 @Suppress("unused")
 object SpoofClientPatch : AbstractSpoofClientPatch(
-    "http://rubenmayayo.com", listOf(GetClientIdFingerprint)
+    "http://rubenmayayo.com",
+    clientIdFingerprints = listOf(GetClientIdFingerprint),
+    userAgentFingerprints = listOf(LoginActivityOnCreateFingerprint)
 ) {
     override fun List<MethodFingerprintResult>.patchClientId(context: BytecodeContext) {
         first().mutableMethod.addInstructions(
@@ -26,4 +30,20 @@ object SpoofClientPatch : AbstractSpoofClientPatch(
             """
         )
     }
+
+    override fun List<MethodFingerprintResult>.patchUserAgent(context: BytecodeContext) {
+        first().let { result ->
+            result.mutableMethod.apply {
+                val insertIndex = result.scanResult.patternScanResult!!.endIndex
+
+                addInstructions(
+                    insertIndex,
+                    """
+                        const-string v7, "$OAUTH_USER_AGENT"
+                        invoke-virtual {v4, v7}, Landroid/webkit/WebSettings;->setUserAgentString(Ljava/lang/String;)V
+                    """
+                )
+            }
+        }
+    }
 }
diff --git a/src/main/kotlin/app/revanced/patches/reddit/customclients/boostforreddit/api/fingerprints/LoginActivityOnCreateFingerprint.kt b/src/main/kotlin/app/revanced/patches/reddit/customclients/boostforreddit/api/fingerprints/LoginActivityOnCreateFingerprint.kt
new file mode 100644
index 00000000..b7bbe1e6
--- /dev/null
+++ b/src/main/kotlin/app/revanced/patches/reddit/customclients/boostforreddit/api/fingerprints/LoginActivityOnCreateFingerprint.kt
@@ -0,0 +1,14 @@
+package app.revanced.patches.reddit.customclients.boostforreddit.api.fingerprints
+
+import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
+import com.android.tools.smali.dexlib2.Opcode
+
+object LoginActivityOnCreateFingerprint : MethodFingerprint(
+    opcodes = listOf(
+        Opcode.MOVE_RESULT_OBJECT,
+        Opcode.CONST_4
+    ),
+    customFingerprint = { method, classDef ->
+        method.name == "onCreate" && classDef.type.endsWith("LoginActivity;")
+    }
+)
\ No newline at end of file