From c2a7b903a48a2f35f1e3135ffe0a590db1ce5dd5 Mon Sep 17 00:00:00 2001 From: Artem Chepurnoy Date: Tue, 2 Jul 2024 21:55:16 +0300 Subject: [PATCH] fix: Use Windows user agent on Android to please Cloudflare bot detection rules #324 --- .../keyguard/platform/util/PlatformUserAgent.kt | 10 ++++++++++ .../artemchep/keyguard/provider/bitwarden/api/login.kt | 7 ++++--- .../com/artemchep/keyguard/di/GlobalModuleJvm.kt | 5 +++-- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/common/src/commonMain/kotlin/com/artemchep/keyguard/platform/util/PlatformUserAgent.kt b/common/src/commonMain/kotlin/com/artemchep/keyguard/platform/util/PlatformUserAgent.kt index 2c32f560..a32a5abc 100644 --- a/common/src/commonMain/kotlin/com/artemchep/keyguard/platform/util/PlatformUserAgent.kt +++ b/common/src/commonMain/kotlin/com/artemchep/keyguard/platform/util/PlatformUserAgent.kt @@ -3,3 +3,13 @@ package com.artemchep.keyguard.platform.util import com.artemchep.keyguard.platform.Platform expect val Platform.userAgent: String + +// Taken from: +// https://releases.electronjs.org/releases/stable +private const val CHROME_VERSION = "126.0.6478.114" + +// Seems like desktop clients always use the Windows user-agents for +// privacy reasons. +val Platform.defaultUserAgent: String + get() = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/$CHROME_VERSION Safari/537.36" + diff --git a/common/src/commonMain/kotlin/com/artemchep/keyguard/provider/bitwarden/api/login.kt b/common/src/commonMain/kotlin/com/artemchep/keyguard/provider/bitwarden/api/login.kt index 31b32d1a..da05f114 100644 --- a/common/src/commonMain/kotlin/com/artemchep/keyguard/provider/bitwarden/api/login.kt +++ b/common/src/commonMain/kotlin/com/artemchep/keyguard/provider/bitwarden/api/login.kt @@ -11,6 +11,7 @@ import com.artemchep.keyguard.common.util.int import com.artemchep.keyguard.core.store.bitwarden.BitwardenToken import com.artemchep.keyguard.platform.CurrentPlatform import com.artemchep.keyguard.platform.Platform +import com.artemchep.keyguard.platform.util.defaultUserAgent import com.artemchep.keyguard.platform.util.userAgent import com.artemchep.keyguard.provider.bitwarden.ServerEnv import com.artemchep.keyguard.provider.bitwarden.ServerTwoFactorToken @@ -76,7 +77,7 @@ data class BitwardenPersona( clientVersion = CLIENT_VERSION, deviceType = "8", deviceName = "linux", - userAgent = userAgent, + userAgent = defaultUserAgent, ) private fun Platform.Desktop.MacOS.bitwardenPersona( @@ -86,7 +87,7 @@ data class BitwardenPersona( clientVersion = CLIENT_VERSION, deviceType = "7", deviceName = "macos", - userAgent = userAgent, + userAgent = defaultUserAgent, ) private fun Platform.Desktop.Windows.bitwardenPersona( @@ -96,7 +97,7 @@ data class BitwardenPersona( clientVersion = CLIENT_VERSION, deviceType = "6", deviceName = "windows", - userAgent = userAgent, + userAgent = defaultUserAgent, ) } } diff --git a/common/src/jvmMain/kotlin/com/artemchep/keyguard/di/GlobalModuleJvm.kt b/common/src/jvmMain/kotlin/com/artemchep/keyguard/di/GlobalModuleJvm.kt index 27158632..650fb1f9 100644 --- a/common/src/jvmMain/kotlin/com/artemchep/keyguard/di/GlobalModuleJvm.kt +++ b/common/src/jvmMain/kotlin/com/artemchep/keyguard/di/GlobalModuleJvm.kt @@ -361,7 +361,7 @@ import com.artemchep.keyguard.crypto.CryptoGeneratorJvm import com.artemchep.keyguard.crypto.FileEncryptorImpl import com.artemchep.keyguard.platform.CurrentPlatform import com.artemchep.keyguard.platform.util.isRelease -import com.artemchep.keyguard.platform.util.userAgent +import com.artemchep.keyguard.provider.bitwarden.api.BitwardenPersona import com.artemchep.keyguard.provider.bitwarden.usecase.CipherUrlCheckImpl import com.artemchep.keyguard.provider.bitwarden.usecase.CipherUrlDuplicateCheckImpl import io.ktor.client.HttpClient @@ -1295,7 +1295,8 @@ fun globalModuleJvm() = DI.Module( val okHttpClient: OkHttpClient = instance() HttpClient(OkHttp) { install(UserAgent) { - agent = CurrentPlatform.userAgent + agent = BitwardenPersona.of(CurrentPlatform) + .userAgent } engine { preconfigured = okHttpClient