Native Android client for Spotify, built with Jetpack Compose
Kotlin ad-freeadblockandroidjetpack-composekotlinlyricsmusic-playermusic-videomyspotispotifyspotify-adblockerspotify-clientspotify-connectspotui
Network: client token kept across launches, no probe before a live source's request, audio sources start first
kota-zoc09cca2aa1b3
diff --git a/base/api-spotify/src/main/kotlin/app/myspoti/spotify/SpotifyApi.kt b/base/api-spotify/src/main/kotlin/app/myspoti/spotify/SpotifyApi.ktindex 348b0546..89f1e480 100644--- a/base/api-spotify/src/main/kotlin/app/myspoti/spotify/SpotifyApi.kt+++ b/base/api-spotify/src/main/kotlin/app/myspoti/spotify/SpotifyApi.kt@@ -38,9 +38,11 @@ class SpotifyApi(deviceId: String? = null,/** The browser this install presents to Spotify (see [SpotifyHttp.browserUserAgent]); null keeps the constant. */userAgent: String? = null,+ /** Keeps the client token across launches (see [app.myspoti.spotify.gql.ClientTokenStore]). */+ tokenStore: app.myspoti.spotify.gql.ClientTokenStore? = null,) : MyspotiApi {val bridge = Bridge(host, scope)- val http = SpotifyHttp(http, bridge, language, userAgent, deviceId ?: java.util.UUID.randomUUID().toString())+ val http = SpotifyHttp(http, bridge, language, userAgent, deviceId ?: java.util.UUID.randomUUID().toString(), tokenStore)@Volatile private var currentOps: GqlOps = ops/** The operation table in use; [updateOps] swaps it when Spotify ships a new build. */val opsInUse: GqlOps get() = currentOpsdiff --git a/base/api-spotify/src/main/kotlin/app/myspoti/spotify/gql/Gql.kt b/base/api-spotify/src/main/kotlin/app/myspoti/spotify/gql/Gql.ktindex 2e35e5e0..05e5896f 100644--- a/base/api-spotify/src/main/kotlin/app/myspoti/spotify/gql/Gql.kt+++ b/base/api-spotify/src/main/kotlin/app/myspoti/spotify/gql/Gql.kt@@ -66,6 +66,16 @@ class BridgeGql(private val bridge: Bridge, private val source: OpsSource) : Gqlclass HttpException(val code: Int, message: String) : Exception("HTTP $code: $message")+/**+ * Where the host keeps the client token across launches, per client device id (the token is+ * minted for one): it lasts about 14 days, and minting one at every launch cost a round trip+ * before the first fresh read, and showed Spotify a pattern no browser tab has.+ */+interface ClientTokenStore {+ fun load(deviceId: String): String?+ fun save(deviceId: String, text: String)+}+/*** Direct HTTP to Spotify with the Web Player's credentials: the web token from the bridge,* and a client token minted here and kept about 14 days.@@ -86,10 +96,12 @@ class SpotifyHttp(* one more device). The host passes an id kept across restarts; null keeps the old behaviour.*/private var deviceId: String = java.util.UUID.randomUUID().toString(),+ /** Keeps the client token across launches; null: minted once per launch, as before. */+ private val tokenStore: ClientTokenStore? = null,) {private data class WebToken(val access: String, val expiresAtMs: Long, val clientId: String, val appVersion: String, val anonymous: Boolean,val fetchedAtMs: Long = System.currentTimeMillis())- private data class ClientToken(val value: String, val expiresAtMs: Long)+ private data class ClientToken(val value: String, val expiresAtMs: Long, val clientId: String = "")private val lock = Mutex()private var web: WebToken? = null@@ -263,10 +275,25 @@ class SpotifyHttp(private suspend fun tokens(): Pair<WebToken, ClientToken> = lock.withLock {val now = System.currentTimeMillis()val w = webToken()- val c = client?.takeIf { it.expiresAtMs - now > 3_600_000 } ?: mintClientToken(w).also { client = it }+ fun fresh(t: ClientToken) = t.expiresAtMs - now > 3_600_000 && (t.clientId.isEmpty() || t.clientId == w.clientId)+ val c = client?.takeIf(::fresh)+ ?: storedClientToken()?.takeIf(::fresh)?.also { client = it }+ ?: mintClientToken(w).also { client = it; keepClientToken(it) }w to c}+ private fun storedClientToken(): ClientToken? = runCatching {+ val o = Bridge.json.parseToJsonElement(tokenStore?.load(deviceId) ?: return null).jsonObject+ ClientToken(o["token"]!!.jsonPrimitive.content, o["expiresAtMs"]!!.jsonPrimitive.long, o["clientId"]!!.jsonPrimitive.content)+ }.getOrNull()++ private fun keepClientToken(t: ClientToken) {+ val store = tokenStore ?: return+ runCatching {+ store.save(deviceId, buildJsonObject { put("token", t.value); put("expiresAtMs", t.expiresAtMs); put("clientId", t.clientId) }.toString())+ }+ }+private suspend fun mintClientToken(w: WebToken): ClientToken {val body = buildJsonObject {putJsonObject("client_data") {@@ -294,7 +321,7 @@ class SpotifyHttp(?: throw GqlException("clienttoken", "HTTP $code: ${text.take(200)}")val seconds = (granted["expires_after_seconds"] as? JsonPrimitive)?.long ?: 86_400val token = (granted["token"] as? JsonPrimitive)?.content ?: throw GqlException("clienttoken", "no token in the answer")- return ClientToken(token, System.currentTimeMillis() + seconds * 1000)+ return ClientToken(token, System.currentTimeMillis() + seconds * 1000, w.clientId)}/** Who a raw request may go to: the account's own hosts (with its credentials) or Spotify's public CDN. */diff --git a/base/api-spotify/src/test/kotlin/app/myspoti/spotify/gql/ClientTokenStoreTest.kt b/base/api-spotify/src/test/kotlin/app/myspoti/spotify/gql/ClientTokenStoreTest.ktnew file mode 100644index 00000000..7ce80cd1--- /dev/null+++ b/base/api-spotify/src/test/kotlin/app/myspoti/spotify/gql/ClientTokenStoreTest.kt@@ -0,0 +1,67 @@+/* SPDX-License-Identifier: GPL-3.0-only */+package app.myspoti.spotify.gql++import app.myspoti.spotify.bridge.Bridge+import app.myspoti.spotify.bridge.BridgeHost+import kotlinx.coroutines.CoroutineScope+import kotlinx.coroutines.Dispatchers+import kotlinx.coroutines.SupervisorJob+import kotlinx.coroutines.cancel+import kotlinx.coroutines.flow.MutableSharedFlow+import kotlinx.coroutines.runBlocking+import okhttp3.Interceptor+import okhttp3.OkHttpClient+import okhttp3.Protocol+import okhttp3.Response+import okhttp3.ResponseBody.Companion.toResponseBody+import org.junit.jupiter.api.Assertions.assertEquals+import org.junit.jupiter.api.Test++/** The client token is minted once and reused by the next launch (a new SpotifyHttp) from the host's store. */+class ClientTokenStoreTest {+ /** Answers the page's token.get as the bridge would. */+ private class TokenHost(val clientId: String) : BridgeHost {+ override val messages = MutableSharedFlow<String>(extraBufferCapacity = 64)+ override suspend fun evaluate(script: String) {+ val m = Regex("""Myspoti\.call\((\d+), "([^"]+)", (.*)\)\.catch""").find(script) ?: return+ val value = """{"accessToken":"web","expiresAtMs":${System.currentTimeMillis() + 3_600_000},"clientId":"$clientId","appVersion":"1.0","isAnonymous":false}"""+ messages.emit("""{"type":"result","id":${m.groupValues[1]},"ok":true,"value":$value}""")+ }+ }++ private class MemoryStore : ClientTokenStore {+ val saved = HashMap<String, String>()+ override fun load(deviceId: String) = saved[deviceId]+ override fun save(deviceId: String, text: String) { saved[deviceId] = text }+ }++ private var mints = 0+ private val sentTokens = mutableListOf<String>()+ private val http = OkHttpClient.Builder().addInterceptor(Interceptor { chain ->+ val r = chain.request()+ val body = if (r.url.host == "clienttoken.spotify.com") {+ mints+++ """{"granted_token":{"token":"ct$mints","expires_after_seconds":1209600}}"""+ } else { sentTokens += r.header("client-token").orEmpty(); "{}" }+ Response.Builder().request(r).protocol(Protocol.HTTP_1_1).code(200).message("OK").body(body.toResponseBody()).build()+ }).build()++ private fun launch(store: ClientTokenStore, scope: CoroutineScope, device: String = "dev1", clientId: String = "c1") =+ SpotifyHttp(http, Bridge(TokenHost(clientId), scope), deviceId = device, tokenStore = store)++ @Test fun aTokenKeptOnDiskIsNotMintedAgain() = runBlocking {+ val scope = CoroutineScope(SupervisorJob() + Dispatchers.Default)+ try {+ val store = MemoryStore()+ launch(store, scope).send("GET", "https://api-partner.spotify.com/x")+ launch(store, scope).send("GET", "https://api-partner.spotify.com/x")+ assertEquals(1, mints, "the second launch reuses the stored token")+ assertEquals(listOf("ct1", "ct1"), sentTokens)+ // Another device id, or a token minted for another client id, is minted afresh.+ launch(store, scope, device = "dev2").send("GET", "https://api-partner.spotify.com/x")+ assertEquals(2, mints)+ launch(store, scope, clientId = "c2").send("GET", "https://api-partner.spotify.com/x")+ assertEquals(3, mints)+ } finally { scope.cancel() }+ }+}diff --git a/base/app/src/main/java/com/music/spotui/extensions/Extensions.kt b/base/app/src/main/java/com/music/spotui/extensions/Extensions.ktindex 17475b6b..1d98839f 100644--- a/base/app/src/main/java/com/music/spotui/extensions/Extensions.kt+++ b/base/app/src/main/java/com/music/spotui/extensions/Extensions.kt@@ -178,7 +178,20 @@ object Extensions {File(dir, "inbox").takeIf { it.exists() }?.deleteRecursively()ExtensionUpdates.readCachedBlocks()// One after the other, their sources asked only once all are loaded: a cold start is busy enough.- _registry.value.extensions.filter { it.enabled }.forEach { start(it, probe = false) }+ // The audio sources first, then downloads and lyrics, then the rest: a song asked at launch+ // waits for the audio sources only, not for a slow extension of another kind before them.+ val on = _registry.value.extensions.filter { it.enabled }.sortedBy {+ when {+ Action.AUDIO_SOURCE.key in it.granted -> 0+ Action.DOWNLOAD_SOURCE.key in it.granted -> 1+ Action.LYRICS_PROVIDE.key in it.granted -> 2+ else -> 3+ }+ }+ val (audio, rest) = on.partition { Action.AUDIO_SOURCE.key in it.granted }+ audio.forEach { start(it, probe = false) }+ audioStarted.complete(Unit)+ rest.forEach { start(it, probe = false) }started.complete(Unit)refreshProvided()probeAll()@@ -746,18 +759,18 @@ object Extensions {return isReady && (providing(Hook.AUDIO_SOURCE).isNotEmpty() || startingAudioSource())}- /** The extensions on at launch are still being started. */- fun starting(): Boolean = !started.isCompleted+ /** The audio-source extensions on at launch are still being started. */+ fun starting(): Boolean = !audioStarted.isCompleted- /** Waits up to [ms] for the extensions on at launch to be started; true once they are. */- suspend fun awaitStarted(ms: Long): Boolean = started.isCompleted || withTimeoutOrNull(ms) { started.await() } != null+ /** Waits up to [ms] for the audio-source extensions on at launch to be started; true once they are. */+ suspend fun awaitStarted(ms: Long): Boolean = audioStarted.isCompleted || withTimeoutOrNull(ms) { audioStarted.await() } != null/** An audio-source extension is turned on (running or not yet): a session of theirs goes back to them. */fun audioSourceOn(): Boolean = isReady && _registry.value.extensions.any { it.enabled && Action.AUDIO_SOURCE.key in it.granted }/** The app is still starting the extensions on at launch, an audio source among them. */private fun startingAudioSource(): Boolean =- !started.isCompleted && _registry.value.extensions.any { it.enabled && Action.AUDIO_SOURCE.key in it.granted }+ !audioStarted.isCompleted && _registry.value.extensions.any { it.enabled && Action.AUDIO_SOURCE.key in it.granted }/*** Why no audio-source extension is asked for a song while some are on ("x skipped (inactive:@@ -886,11 +899,11 @@ object Extensions {attempts.update(attempt) { it.copy(fallback = "Spotify: no extension had it under a minute ago, not asked again") }return null}- // Picked right after launch: the extensions still loading are waited for, not skipped.- if (!started.isCompleted) {+ // Picked right after launch: the audio sources still loading are waited for, not skipped.+ if (!audioStarted.isCompleted) {val t0 = System.currentTimeMillis()- val loaded = withTimeoutOrNull(START_AUDIO_MS) { started.await() } != null- Logbook.log("audio: extensions not loaded yet, waited ${System.currentTimeMillis() - t0} ms${if (loaded) "" else " (still loading)"}")+ val loaded = withTimeoutOrNull(START_AUDIO_MS) { audioStarted.await() } != null+ Logbook.log("audio: audio sources not loaded yet, waited ${System.currentTimeMillis() - t0} ms${if (loaded) "" else " (still loading)"}")}// The same song as a download asks with: its ISRC, which an extension matches recordings by.val withIsrc = withIsrc(track)@@ -1174,16 +1187,22 @@ object Extensions {private fun probeAll() = runtimes.forEach { (id, rt) -> scope.launch { probe(id, rt) } }/**- * Before [hook] is asked: its extensions probed [STATUS_EVERY_MS] ago or more are probed again,- * together, within [STATUS_MS]. One never probed (its probe at launch is on its way) is not- * waited for: it is in use until a probe says otherwise.+ * Before [hook] is asked: its extensions probed [STATUS_EVERY_MS] ago or more are probed again.+ * Only those whose last probe said inactive are waited for (together, within [STATUS_MS]): one+ * back on may answer this request. A live one is probed behind the request instead of before+ * it, which held the first song after 5 min idle a whole `status` round trip (up to 5.5 s).+ * One never probed (its probe at launch is on its way) is not waited for: it is in use until a+ * probe says otherwise.*/private suspend fun probeStale(hook: Hook) {val due = _registry.value.extensions.mapNotNull { e ->runtimes[e.id]?.takeIf { e.enabled && it.provides(hook) && health.last(e.id) != null && health.stale(e.id, STATUS_EVERY_MS) }?.let { e.id to it }}if (due.isEmpty()) return- withTimeoutOrNull(STATUS_MS + 500) { kotlinx.coroutines.coroutineScope { due.map { (id, rt) -> async { probe(id, rt) } }.forEach { it.await() } } }+ val (wait, behind) = due.partition { (id, _) -> health.inactive(id) }+ behind.forEach { (id, rt) -> scope.launch { probe(id, rt) } }+ if (wait.isEmpty()) return+ withTimeoutOrNull(STATUS_MS + 500) { kotlinx.coroutines.coroutineScope { wait.map { (id, rt) -> async { probe(id, rt) } }.forEach { it.await() } } }}/** Every minute, the probes [STATUS_EVERY_MS] old are asked again while the app is in sight or plays. */@@ -1410,6 +1429,8 @@ object Extensions {private val startLocks = ConcurrentHashMap<String, Any>()/** The extensions on at launch have all been started (or failed to). */private val started = kotlinx.coroutines.CompletableDeferred<Unit>()+ /** The audio-source extensions on at launch have been started (they start first). */+ private val audioStarted = kotlinx.coroutines.CompletableDeferred<Unit>()/*** Starts [e] unless that very package already runs. One start of an extension at a time: andiff --git a/base/host-android/src/main/kotlin/app/myspoti/host/android/MyspotiRuntime.kt b/base/host-android/src/main/kotlin/app/myspoti/host/android/MyspotiRuntime.ktindex 4f6f06a5..6cd751de 100644--- a/base/host-android/src/main/kotlin/app/myspoti/host/android/MyspotiRuntime.kt+++ b/base/host-android/src/main/kotlin/app/myspoti/host/android/MyspotiRuntime.kt@@ -37,7 +37,8 @@ object MyspotiRuntime {val updater = app.myspoti.spotify.gql.GqlOpsUpdater(okhttp3.OkHttpClient(), java.io.File(context.filesDir, "spotify/gql-ops.json"))host = WebPlayerHost(context)AudioModeWatch.install(context.applicationContext)- spotify = SpotifyApi(host, scope, language = Locale.getDefault().language, ops = updater.load(), deviceId = clientDeviceId(context, host.accounts.list.value.active), userAgent = host.userAgent)+ spotify = SpotifyApi(host, scope, language = Locale.getDefault().language, ops = updater.load(), deviceId = clientDeviceId(context, host.accounts.list.value.active), userAgent = host.userAgent,+ tokenStore = clientTokens(context))spotify.bridge.logger = { line -> Logbook.log(line) }host.onPageLoaded = { scope.launch { spotify.onPageLoaded() } }host.onPageStarting = { spotify.onPageStarting() }@@ -176,6 +177,16 @@ object MyspotiRuntime {* pair, and a fresh id at every start made the app look like a brand-new client each time.*//** The client's device id for the account kept in [profile]: the first account keeps the one it always had. */+ /** The client token of each client device id, in the app's own files (spotify/client-token-<id>). */+ private fun clientTokens(context: Context) = object : app.myspoti.spotify.gql.ClientTokenStore {+ private fun file(id: String) = java.io.File(context.filesDir, "spotify/client-token-" + id.filter { it.isLetterOrDigit() || it == '-' })+ override fun load(deviceId: String): String? = runCatching { file(deviceId).readText() }.getOrNull()+ override fun save(deviceId: String, text: String) {+ val f = file(deviceId)+ runCatching { f.parentFile?.mkdirs(); val tmp = java.io.File(f.path + ".tmp"); tmp.writeText(text); tmp.renameTo(f) }+ }+ }+private fun clientDeviceId(context: Context, profile: String): String {val name = if (profile == AccountList.DEFAULT_PROFILE) "device-id" else "device-id-" + profile.filter { it.isLetterOrDigit() || it == '-' || it == '_' }val file = java.io.File(context.filesDir, "spotify/$name")