Skip to content

Unofficial YouTube Music / Spotify Premium client for Android, built in Liquid Glass. Audio effects, Spotify Connect, listening history, 8 languages. Rust core on librespot.

Keep the gateway's query hashes out of the binary

Lelonio280499ed2998
diff --git a/.gitignore b/.gitignore
index 9ccf420..fc4b5e8 100644
--- a/.gitignore
+++ b/.gitignore
@@ -26,3 +26,7 @@ keystore.properties
.idea/
*.iml
.DS_Store
+
+# Network captures taken while reverse-engineering. They hold session
+# headers, so they stay on the machine that made them.
+*.har
diff --git a/app/src/main/java/dev/lelonio/square/SquareApplication.kt b/app/src/main/java/dev/lelonio/square/SquareApplication.kt
index 6d2b254..cb2bc82 100644
--- a/app/src/main/java/dev/lelonio/square/SquareApplication.kt
+++ b/app/src/main/java/dev/lelonio/square/SquareApplication.kt
@@ -33,6 +33,11 @@ class SquareApplication : Application() {
dev.lelonio.square.data.QualityStore(this)
}
+ /** The identifiers Spotify's gateway wants; kept fresh from the repository. */
+ val pathfinderKeys: dev.lelonio.square.data.PathfinderKeys by lazy {
+ dev.lelonio.square.data.PathfinderKeys(this)
+ }
+
/** How long one track dissolves into the next. */
val crossfade: dev.lelonio.square.data.CrossfadeStore by lazy {
dev.lelonio.square.data.CrossfadeStore(this)
diff --git a/app/src/main/java/dev/lelonio/square/data/PathfinderKeys.kt b/app/src/main/java/dev/lelonio/square/data/PathfinderKeys.kt
new file mode 100644
index 0000000..17697ef
--- /dev/null
+++ b/app/src/main/java/dev/lelonio/square/data/PathfinderKeys.kt
@@ -0,0 +1,87 @@
+package dev.lelonio.square.data
+
+import android.content.Context
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.withContext
+import okhttp3.OkHttpClient
+import okhttp3.Request
+import org.json.JSONObject
+
+/**
+ * The identifiers Spotify's GraphQL gateway wants, kept where they can be
+ * changed without an app release.
+ *
+ * A persisted query is addressed by the sha256 of a query Spotify already
+ * knows, and it retires those hashes whenever its web client is rebuilt. Built
+ * into the binary, that means the personalised home stops working one day and
+ * stays broken until everyone installs a new version, for the sake of a
+ * sixty-four character string. So the strings live in a file in this app's own
+ * repository, and the binary carries only the last ones known to work.
+ *
+ * The same goes for the client version the gateway is told about, which ages
+ * for the same reason and is fixed the same way.
+ *
+ * Nothing here is a secret and nothing here is trusted: the file names queries,
+ * and a wrong value costs the home page and nothing else.
+ */
+class PathfinderKeys(context: Context) {
+
+ private val prefs = context.applicationContext
+ .getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE)
+ private val client = OkHttpClient()
+
+ /** The hash for the `home` query, freshest first. */
+ val home: String get() = prefs.getString(KEY_HOME, null) ?: DEFAULT_HOME
+
+ /** The web client version the gateway is told about. */
+ val appVersion: String get() = prefs.getString(KEY_VERSION, null) ?: DEFAULT_VERSION
+
+ /**
+ * Reads the file, at most once a day.
+ *
+ * Failure is silent and cheap: what is already known stays, and that is
+ * either yesterday's copy or what shipped in the binary. This runs before
+ * the home page is asked for, and the page is drawn either way.
+ */
+ suspend fun refresh() = withContext(Dispatchers.IO) {
+ val last = prefs.getLong(KEY_CHECKED, 0)
+ if (System.currentTimeMillis() - last < INTERVAL_MS) return@withContext
+
+ runCatching {
+ val request = Request.Builder().url(URL).build()
+ client.newCall(request).execute().use { response ->
+ if (!response.isSuccessful) error("HTTP ${response.code}")
+ val body = JSONObject(response.body?.string().orEmpty())
+ val edit = prefs.edit().putLong(KEY_CHECKED, System.currentTimeMillis())
+ body.optString("home").takeIf { it.length == HASH_LENGTH }
+ ?.let { edit.putString(KEY_HOME, it) }
+ body.optString("appVersion").takeIf { it.isNotEmpty() }
+ ?.let { edit.putString(KEY_VERSION, it) }
+ edit.apply()
+ }
+ }.onFailure { android.util.Log.i(TAG, "keeping the known query hashes: $it") }
+ }
+
+ private companion object {
+ const val TAG = "PathfinderKeys"
+ const val FILE_NAME = "square_pathfinder"
+ const val KEY_HOME = "home"
+ const val KEY_VERSION = "app_version"
+ const val KEY_CHECKED = "checked_at"
+
+ /** Once a day: these change with Spotify's releases, not with ours. */
+ const val INTERVAL_MS = 24 * 60 * 60 * 1000L
+
+ /** A sha256 in hex, and a way to notice a file that says something else. */
+ const val HASH_LENGTH = 64
+
+ const val URL =
+ "https://raw.githubusercontent.com/${dev.lelonio.square.update.Updater.REPO}" +
+ "/master/pathfinder.json"
+
+ /** What was true when this version was built; see the note above. */
+ const val DEFAULT_HOME =
+ "76243c78b0e20ecdbe41b794dec8cbe73f75e585b0a7201b8d2e84578412847a"
+ const val DEFAULT_VERSION = "1.2.97.155.g5dd0dcaf-development"
+ }
+}
diff --git a/app/src/main/java/dev/lelonio/square/nativecore/NativeBridge.kt b/app/src/main/java/dev/lelonio/square/nativecore/NativeBridge.kt
index 509f831..bad41d9 100644
--- a/app/src/main/java/dev/lelonio/square/nativecore/NativeBridge.kt
+++ b/app/src/main/java/dev/lelonio/square/nativecore/NativeBridge.kt
@@ -226,8 +226,19 @@ object NativeBridge {
* hashes change when its web client is rebuilt. Callers must be able to
* carry on with nothing. Blocking.
*/
- fun homeFeed(timeZone: String, language: String): String =
- nativeHomeFeed(timeZone, language)
+ fun homeFeed(
+ timeZone: String,
+ language: String,
+ /**
+ * The persisted query's hash and the web client version to claim.
+ *
+ * Passed in rather than built in: Spotify retires both on its own
+ * schedule, and a string that ages should not need a release of this
+ * app to replace. See PathfinderKeys.
+ */
+ hash: String,
+ appVersion: String,
+ ): String = nativeHomeFeed(timeZone, language, hash, appVersion)
/** This device's own Connect id, which is what says "here" rather than "there". */
fun deviceId(): String = runCatching { nativeDeviceId() }.getOrDefault("")
@@ -335,7 +346,12 @@ object NativeBridge {
private external fun nativePublishContext(positionMs: Int): Boolean
private external fun nativeResumeHere(contextUri: String, trackUri: String, positionMs: Int)
private external fun nativeTakeOver()
- private external fun nativeHomeFeed(timeZone: String, language: String): String
+ private external fun nativeHomeFeed(
+ timeZone: String,
+ language: String,
+ hash: String,
+ appVersion: String,
+ ): String
private external fun nativeDeviceId(): String
private external fun nativeRemoteState(): String
private external fun nativeRemoteDevices(): String
diff --git a/app/src/main/java/dev/lelonio/square/ui/MainViewModel.kt b/app/src/main/java/dev/lelonio/square/ui/MainViewModel.kt
index 64b5d1c..1543c30 100644
--- a/app/src/main/java/dev/lelonio/square/ui/MainViewModel.kt
+++ b/app/src/main/java/dev/lelonio/square/ui/MainViewModel.kt
@@ -1119,12 +1119,16 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {
val homeShelves: StateFlow<List<HomeShelf>> = _homeShelves.asStateFlow()
private fun loadHomeShelves() = viewModelScope.launch {
+ val keys = container.pathfinderKeys
+ keys.refresh()
val shelves = withContext(Dispatchers.IO) {
runCatching {
SpotifyHome.parse(
NativeBridge.homeFeed(
java.util.TimeZone.getDefault().id,
java.util.Locale.getDefault().language,
+ keys.home,
+ keys.appVersion,
),
)
}
diff --git a/native/src/ffi.rs b/native/src/ffi.rs
index 12a0441..97465a9 100644
--- a/native/src/ffi.rs
+++ b/native/src/ffi.rs
@@ -498,11 +498,15 @@ pub extern "system" fn Java_dev_lelonio_square_nativecore_NativeBridge_nativeHom
_class: JClass,
time_zone: JString,
language: JString,
+ hash: JString,
+ app_version: JString,
) -> jstring {
let zone = read_string(&mut env, &time_zone).unwrap_or_default();
let language = read_string(&mut env, &language).unwrap_or_default();
+ let hash = read_string(&mut env, &hash).unwrap_or_default();
+ let app_version = read_string(&mut env, &app_version).unwrap_or_default();
guard_string(&mut env, "HomeFeed", || {
- crate::pathfinder::home(&zone, &language)
+ crate::pathfinder::home(&zone, &language, &hash, &app_version)
})
}
diff --git a/native/src/pathfinder.rs b/native/src/pathfinder.rs
index be121e0..3c53170 100644
--- a/native/src/pathfinder.rs
+++ b/native/src/pathfinder.rs
@@ -36,21 +36,22 @@ const APP_PLATFORM: &str = "WebPlayer";
/// Sent because the gateway is choosy about who is asking: with the platform
/// alone the home query answered `GenericError`, which is what it says to a
/// caller it does not recognise rather than to one it refuses. Like the query
-/// hashes, this ages: see the note at the top of the file.
+/// hashes, this ages, and like them the caller can pass a newer one; see
+/// PathfinderKeys on the Kotlin side.
const APP_VERSION: &str = "1.2.97.155.g5dd0dcaf-development";
/// One persisted query, by name and hash. See the note at the top of the file.
struct Query {
name: &'static str,
- hash: &'static str,
+ hash: String,
}
-/// The personalised home: the shelves, in the order the account is meant to see
-/// them.
-const HOME: Query = Query {
- name: "home",
- hash: "76243c78b0e20ecdbe41b794dec8cbe73f75e585b0a7201b8d2e84578412847a",
-};
+/// The hash that was current when this was written.
+///
+/// Used only when the caller has nothing better. Since these are retired by
+/// Spotify rather than by this app, the newest one is fetched at runtime and
+/// this is the floor rather than the answer.
+const HOME_HASH: &str = "76243c78b0e20ecdbe41b794dec8cbe73f75e585b0a7201b8d2e84578412847a";
/// Runs one persisted query and returns the raw JSON body.
async fn query(
@@ -58,6 +59,7 @@ async fn query(
query: &Query,
variables: String,
language: &str,
+ app_version: &str,
) -> engine::EngineResult<String> {
let body = format!(
concat!(
@@ -95,7 +97,10 @@ async fn query(
if let Ok(value) = HeaderValue::from_str(language) {
headers.insert(http::header::ACCEPT_LANGUAGE, value);
}
- headers.insert("spotify-app-version", HeaderValue::from_static(APP_VERSION));
+ let version = if app_version.is_empty() { APP_VERSION } else { app_version };
+ if let Ok(value) = HeaderValue::from_str(version) {
+ headers.insert("spotify-app-version", value);
+ }
headers.insert(
http::header::ORIGIN,
HeaderValue::from_static("https://open.spotify.com"),
@@ -142,7 +147,12 @@ fn visitor_id() -> String {
}
/// The account's home page, as Spotify's own client receives it.
-pub fn home(time_zone: &str, language: &str) -> engine::EngineResult<String> {
+pub fn home(
+ time_zone: &str,
+ language: &str,
+ hash: &str,
+ app_version: &str,
+) -> engine::EngineResult<String> {
let handle = engine::runtime_handle()?;
let session = engine::with_session(|session| session.clone())?;
@@ -167,5 +177,15 @@ pub fn home(time_zone: &str, language: &str) -> engine::EngineResult<String> {
zone, visitor_id()
);
- handle.block_on(async move { query(&session, &HOME, variables, &language).await })
+ let home = Query {
+ name: "home",
+ hash: if hash.is_empty() {
+ HOME_HASH.to_string()
+ } else {
+ hash.to_string()
+ },
+ };
+ let app_version = app_version.to_string();
+
+ handle.block_on(async move { query(&session, &home, variables, &language, &app_version).await })
}
diff --git a/pathfinder.json b/pathfinder.json
new file mode 100644
index 0000000..a225259
--- /dev/null
+++ b/pathfinder.json
@@ -0,0 +1,5 @@
+{
+ "_comment": "Identifiers Spotify's GraphQL gateway wants from a caller. Read by Square at runtime so a change here needs no new version of the app: see app/src/main/java/dev/lelonio/square/data/PathfinderKeys.kt. Both values are retired by Spotify whenever it rebuilds its web client, and both are readable from a network capture of open.spotify.com.",
+ "home": "76243c78b0e20ecdbe41b794dec8cbe73f75e585b0a7201b8d2e84578412847a",
+ "appVersion": "1.2.97.155.g5dd0dcaf-development"
+}