Send Kavita client info headers
Some checks failed
CI / Prepare job (push) Has been cancelled
CI / Build extensions (${{ matrix.chunk.number }}) (push) Has been cancelled
CI / Publish extension repo (push) Has been cancelled
Create Release on Merge / release (push) Has been cancelled

This commit is contained in:
2026-05-23 23:39:20 +09:00
parent b7f98dee7e
commit c85c009e17
2 changed files with 33 additions and 1 deletions

View File

@@ -2,7 +2,7 @@ ext {
kmkVersionCode = 1
extName = 'Kavita'
extClass = '.KavitaFactory'
extVersionCode = 29
extVersionCode = 30
isNsfw = false
}

View File

@@ -84,6 +84,7 @@ import java.io.IOException
import java.security.MessageDigest
import java.util.Calendar
import java.util.Locale
import java.util.UUID
import java.util.concurrent.ConcurrentHashMap
import kotlin.let
import kotlin.runCatching
@@ -122,6 +123,7 @@ class Kavita(private val suffix: String = "") : ConfigurableSource, UnmeteredSou
private val preferences: SharedPreferences by lazy {
Injekt.get<Application>().getSharedPreferences("source_$id", 0x0000)
}
private val application: Application by lazy { Injekt.get() }
override val name = "${KavitaInt.KAVITA_NAME} (${preferences.getString(KavitaConstants.customSourceNamePref, suffix)})"
override val lang = "all"
override val supportsLatest = true
@@ -2086,6 +2088,8 @@ class Kavita(private val suffix: String = "") : ConfigurableSource, UnmeteredSou
}
return Headers.Builder()
.add("User-Agent", "Tachiyomi Kavita v${AppInfo.getVersionName()}")
.add("X-Kavita-Client", kavitaClientHeader())
.add("X-Device-Id", deviceFingerprint())
.add("Content-Type", "application/json")
.add("Authorization", "Bearer $jwtToken")
}
@@ -2093,10 +2097,38 @@ class Kavita(private val suffix: String = "") : ConfigurableSource, UnmeteredSou
private fun setupLoginHeaders(): Headers.Builder {
return Headers.Builder()
.add("User-Agent", "Tachiyomi Kavita v${AppInfo.getVersionName()}")
.add("X-Kavita-Client", kavitaClientHeader())
.add("X-Device-Id", deviceFingerprint())
.add("Content-Type", "application/json")
.add("Authorization", "Bearer $jwtToken")
}
private fun kavitaClientHeader(): String {
val resources = application.resources
val configuration = resources.configuration
val metrics = resources.displayMetrics
val screenWidth = metrics.widthPixels.coerceAtLeast(0)
val screenHeight = metrics.heightPixels.coerceAtLeast(0)
val smallestWidthDp = configuration.smallestScreenWidthDp
val deviceType = when {
smallestWidthDp >= 600 -> "Tablet"
else -> "Mobile"
}
val orientation = if (screenWidth > screenHeight) "landscape" else "portrait"
return "web-app/${AppInfo.getVersionName()} (Kavita Extension/${AppInfo.getVersionName()}; Android; $deviceType; ${screenWidth}x$screenHeight; $orientation)"
}
private fun deviceFingerprint(): String {
val key = "kavita_extension_device_id"
val existing = preferences.getString(key, null)
if (!existing.isNullOrBlank()) return existing
val generated = UUID.randomUUID().toString()
preferences.edit().putString(key, generated).apply()
return generated
}
override fun setupPreferenceScreen(screen: PreferenceScreen) {
val opdsAddressPref = screen.editTextPreference(
ADDRESS_TITLE,