Skip to content

Native Android client for Spotify, built with Jetpack Compose

feat(extensions): support transformed media and concurrent sources

kota-zo7256d452c794
diff --git a/base/app/src/main/java/com/music/spotui/data/offline/OfflineCrypto.kt b/base/app/src/main/java/com/music/spotui/data/offline/OfflineCrypto.kt
index defd62df..8f123b22 100644
--- a/base/app/src/main/java/com/music/spotui/data/offline/OfflineCrypto.kt
+++ b/base/app/src/main/java/com/music/spotui/data/offline/OfflineCrypto.kt
@@ -146,6 +146,8 @@ object OfflineCrypto {
/** The song's first bytes read ahead ([StreamHead]), served before [other] goes on from their end. */
private var head: ByteArray? = null
private var headAt = 0
+ private var transformed: java.io.InputStream? = null
+ private var transformedLeft = C.LENGTH_UNSET.toLong()
override fun open(dataSpec: DataSpec): Long {
uri = dataSpec.uri
@@ -213,6 +215,26 @@ object OfflineCrypto {
active = net
val spec = dataSpec.buildUpon().setUri(Uri.parse(stream.url))
.setHttpRequestHeaders(dataSpec.httpRequestHeaders + stream.headers).build()
+ stream.transform?.let { transform ->
+ val aligned = dataSpec.position / transform.segmentBytes * transform.segmentBytes
+ val skip = dataSpec.position - aligned
+ val available = net.open(spec.buildUpon().setPosition(aligned).setLength(C.LENGTH_UNSET.toLong()).build())
+ val input = object : java.io.InputStream() {
+ override fun read(): Int {
+ val b = ByteArray(1)
+ return if (net.read(b,0,1) < 0) -1 else b[0].toInt() and 255
+ }
+ override fun read(b: ByteArray, off: Int, len: Int) = net.read(b,off,len)
+ }
+ val decoded = app.myspoti.extensions.PatternedBlowfish.open(input,transform,aligned)
+ var leftToSkip = skip
+ while (leftToSkip-- > 0) if (decoded.read() < 0) throw java.io.EOFException("transform: seek past end")
+ transformed = decoded
+ val rest = if (available == C.LENGTH_UNSET.toLong()) available else (available-skip).coerceAtLeast(0)
+ transformedLeft = if (dataSpec.length == C.LENGTH_UNSET.toLong()) rest else
+ if (rest == C.LENGTH_UNSET.toLong()) dataSpec.length else minOf(rest,dataSpec.length)
+ return transformedLeft
+ }
val ahead = if (dataSpec.position == 0L && dataSpec.length == C.LENGTH_UNSET.toLong())
StreamHead.take(dataSpec.uri.lastPathSegment.orEmpty(), stream.url) else null
if (ahead != null) {
@@ -245,6 +267,14 @@ object OfflineCrypto {
}
override fun read(buffer: ByteArray, offset: Int, length: Int): Int {
+ transformed?.let { decoded ->
+ if (length == 0) return 0
+ if (transformedLeft == 0L) return C.RESULT_END_OF_INPUT
+ val want = if (transformedLeft == C.LENGTH_UNSET.toLong()) length else minOf(length.toLong(),transformedLeft).toInt()
+ val n = decoded.read(buffer,offset,want)
+ if (n > 0 && transformedLeft != C.LENGTH_UNSET.toLong()) transformedLeft -= n
+ return n
+ }
head?.let { h ->
if (headAt < h.size) {
if (length == 0) return 0
@@ -280,6 +310,7 @@ object OfflineCrypto {
override fun close() {
head = null
+ transformed?.close(); transformed = null
if (usingOther) { usingOther = false; active.close(); return }
if (dash != null) { dash = null; if (uri != null) { uri = null; transferEnded() }; return }
raf?.close(); raf = null; cipher = null
diff --git a/base/app/src/main/java/com/music/spotui/data/offline/OfflineDownloader.kt b/base/app/src/main/java/com/music/spotui/data/offline/OfflineDownloader.kt
index ff2ced2a..8d4cd3bb 100644
--- a/base/app/src/main/java/com/music/spotui/data/offline/OfflineDownloader.kt
+++ b/base/app/src/main/java/com/music/spotui/data/offline/OfflineDownloader.kt
@@ -388,7 +388,19 @@ object OfflineDownloader {
DashFlacRemux.download("data:application/dash+xml;base64,${file.manifest}", out, file.headers)
}
else -> fetchWithProgress(r.trackId, file.url!!, out, file.headers)
- } && out.length() >= 10_000
+ } && out.length() >= 10_000 && runInterruptible(Dispatchers.IO) {
+ file.transform?.let { spec ->
+ val staged = File.createTempFile("download-transform-", ".partial", out.parentFile)
+ try {
+ out.inputStream().use { input -> staged.outputStream().use { output ->
+ app.myspoti.extensions.PatternedBlowfish.transform(input, output, spec)
+ } }
+ if (Thread.currentThread().isInterrupted) throw java.io.InterruptedIOException("transform cancelled")
+ java.nio.file.Files.move(staged.toPath(), out.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING)
+ } finally { staged.delete() }
+ }
+ true
+ }
} catch (e: CancellationException) {
throw e
} catch (e: Exception) {
diff --git a/base/extensions/src/main/kotlin/app/myspoti/extensions/ExtensionRuntime.kt b/base/extensions/src/main/kotlin/app/myspoti/extensions/ExtensionRuntime.kt
index dc682248..46c23eb4 100644
--- a/base/extensions/src/main/kotlin/app/myspoti/extensions/ExtensionRuntime.kt
+++ b/base/extensions/src/main/kotlin/app/myspoti/extensions/ExtensionRuntime.kt
@@ -64,6 +64,7 @@ class ExtensionRuntime(
grantedKeys: Set<String>,
private val limits: Limits = Limits(),
private val guard: SurfaceGuard = SurfaceGuard.app,
+ private val raceChild: Boolean = false,
) : AutoCloseable {
private val granted: Set<String> = Action.migrateGranted(grantedKeys)
private val declared: Set<Action> = manifest.actionSet()
@@ -322,6 +323,7 @@ class ExtensionRuntime(
}
def("http", 1) { a -> http(str(a, 0)) }
def("httpAll", 1) { a -> httpAll(str(a, 0)) }
+ def("sourceRace", 3) { a -> sourceRace(str(a,0),str(a,1),str(a,2)) }
def("storageGet", 1) { a -> need(Action.STORAGE); host.storage(id).get(str(a, 0)) }
def("storageSet", 2) { a ->
need(Action.STORAGE)
@@ -481,6 +483,61 @@ class ExtensionRuntime(
}
}
+ /** Independent source lanes: no shared Rhino scope, no recursive races or extra privileges. */
+ private fun sourceRace(hookName: String, query: String, sources: String): String? {
+ if (raceChild) throw error("sources.race: nested races are not allowed")
+ val hook = Hook.of(hookName)?.takeIf { it == Hook.AUDIO_SOURCE || it == Hook.DOWNLOAD_SOURCE }
+ ?: throw error("sources.race: audio-source or download-source only")
+ hook.action?.let(::need)
+ val input = ExtJson.parseToJsonElement(query) as? JsonObject ?: throw error("sources.race: query must be an object")
+ val lanes = (ExtJson.parseToJsonElement(sources) as? JsonArray)?.map {
+ (it as? JsonPrimitive)?.content ?: throw error("sources.race: invalid lane")
+ }?.distinct() ?: throw error("sources.race: lanes must be an array")
+ if (lanes.isEmpty() || lanes.size > 4 || lanes.any { !it.matches(Regex("[a-zA-Z0-9_-]{1,40}")) })
+ throw error("sources.race: one to four named lanes")
+ val until = System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(leftMs())
+ val sharedHost = object : Host by host {
+ override fun http(extensionId: String, request: HttpRequest): HttpResponse {
+ refusal(request)?.let { throw IllegalArgumentException(it) }
+ if (!allowRequest()) throw IllegalArgumentException("HTTP quota exceeded")
+ return host.http(extensionId, request)
+ }
+ override fun notify(extensionId: String, text: String) {
+ if (allowRequest(notifyTimes, MAX_NOTIFY_PER_HOUR, 3_600_000L)) host.notify(extensionId,text)
+ }
+ }
+ val children = java.util.concurrent.ConcurrentLinkedQueue<ExtensionRuntime>()
+ val completion = java.util.concurrent.ExecutorCompletionService<String?>(parallel())
+ val keys = granted.intersect(setOf("download.source", "audio.source", "network", "network.any", "storage", "notify"))
+ val jobs = lanes.map { lane -> completion.submit(java.util.concurrent.Callable {
+ val child = ExtensionRuntime(manifest,script,sharedHost,keys,limits,guard,raceChild=true)
+ children.add(child)
+ try {
+ if (Thread.currentThread().isInterrupted) return@Callable null
+ child.start()
+ val left = TimeUnit.NANOSECONDS.toMillis(until-System.nanoTime())
+ if (left <= 0 || Thread.currentThread().isInterrupted) return@Callable null
+ val out = child.call(hook,JsonObject(input + ("source" to JsonPrimitive(lane))).toString(),left)
+ ?: return@Callable null
+ if (hook == Hook.DOWNLOAD_SOURCE) {
+ val files = DownloadFile.parseList(out)
+ val lossless = (input["quality"] as? JsonPrimitive)?.content != "small"
+ if (files.isEmpty() || (lossless && files.any { !it.lossless })) null else out
+ } else if (AudioStream.parse(out) == null) null else out
+ } catch (_: Exception) { null } finally { child.close() }
+ }) }
+ try {
+ repeat(jobs.size) {
+ val result = completion.poll(leftMs(), TimeUnit.MILLISECONDS)?.get()
+ if (result != null) return result
+ }
+ return null
+ } finally {
+ jobs.forEach { it.cancel(true) }
+ children.forEach { it.close() }
+ }
+ }
+
private fun throttle(times: ArrayDeque<Long> = requestTimes, max: Int = limits.requestsPerMinute, what: String = "requests") {
if (!allowRequest(times, max)) throw error("more than $max $what in a minute")
}
diff --git a/base/extensions/src/main/kotlin/app/myspoti/extensions/Hooks.kt b/base/extensions/src/main/kotlin/app/myspoti/extensions/Hooks.kt
index 6b407cd6..eae21486 100644
--- a/base/extensions/src/main/kotlin/app/myspoti/extensions/Hooks.kt
+++ b/base/extensions/src/main/kotlin/app/myspoti/extensions/Hooks.kt
@@ -121,6 +121,7 @@ data class AudioStream(
* 44.1 kHz"), 80 characters at most.
*/
val source: String? = null,
+ val transform: FileTransform? = null,
) {
/** How long the app keeps this answer: [expiresInMs], at most [maxMs]. */
fun keepMs(maxMs: Long): Long = expiresInMs?.coerceIn(0, maxMs) ?: maxMs
@@ -159,6 +160,7 @@ data class AudioStream(
)
val manifest = (o["manifest"] as? JsonPrimitive)?.contentOrNull
if (manifest != null) {
+ if (o["transform"] != null) throw ExtensionException("transform: not for DASH")
if (manifest.length > MAX_MANIFEST) throw ExtensionException("audio-source: manifest over 1 MB")
val mpd = runCatching { String(java.util.Base64.getMimeDecoder().decode(manifest)) }.getOrNull()
if (mpd == null || !mpd.contains("<MPD")) throw ExtensionException("audio-source: manifest is not a base64 MPEG-DASH manifest")
@@ -167,7 +169,7 @@ data class AudioStream(
}
val url = (o["url"] as? JsonPrimitive)?.contentOrNull ?: return null
checkUrl("audio-source", url)
- return base.copy(url = url)
+ return base.copy(url = url, transform = parseFileTransform(o["transform"]))
}
}
}
@@ -183,6 +185,16 @@ object AudioQuery {
}
}
+private fun parseFileTransform(raw: kotlinx.serialization.json.JsonElement?): FileTransform? = raw?.let {
+ val t = it as? JsonObject ?: throw ExtensionException("transform: must be an object")
+ fun text(k: String) = (t[k] as? JsonPrimitive)?.contentOrNull
+ fun number(k: String, default: Int) = if (k !in t) default else
+ text(k)?.toIntOrNull() ?: throw ExtensionException("transform: invalid $k")
+ if (text("algorithm") != "blowfish-cbc-pattern") throw ExtensionException("transform: unsupported algorithm")
+ FileTransform(text("keyHex") ?: "", text("ivHex") ?: "", number("segmentBytes",2048),
+ number("every",3), number("offset",0)).checked()
+}
+
/** A public http(s) address, or the hook's error. */
internal fun checkUrl(hook: String, url: String) {
val scheme = url.substringBefore(':').lowercase()
@@ -234,6 +246,7 @@ data class DownloadFile(
val provider: String?,
/** The extension found this file by the song's ISRC (the exact recording). */
val isrcMatched: Boolean,
+ val transform: FileTransform? = null,
) {
val lossless get() = format == "flac" || format == "dash-flac"
@@ -275,6 +288,8 @@ data class DownloadFile(
url ?: return null
checkUrl("download-source", url)
}
+ val transform = parseFileTransform(o["transform"])
+ if (transform != null && format == "dash-flac") throw ExtensionException("transform: not for DASH")
return DownloadFile(
url = if (format == "dash-flac") null else url,
manifest = if (format == "dash-flac") manifest else null,
@@ -285,6 +300,7 @@ data class DownloadFile(
sizeHint = int("sizeHint")?.takeIf { it > 0 },
provider = str("provider")?.take(40)?.ifBlank { null },
isrcMatched = (o["isrcMatched"] as? JsonPrimitive)?.booleanOrNull ?: false,
+ transform = transform,
)
}
}
diff --git a/base/extensions/src/main/kotlin/app/myspoti/extensions/Host.kt b/base/extensions/src/main/kotlin/app/myspoti/extensions/Host.kt
index 3b877422..fe17b8ec 100644
--- a/base/extensions/src/main/kotlin/app/myspoti/extensions/Host.kt
+++ b/base/extensions/src/main/kotlin/app/myspoti/extensions/Host.kt
@@ -179,7 +179,6 @@ object BasicHttp {
var contentType: String? = null
request.headers.forEach { (k, v) ->
if (k.lowercase() in FORBIDDEN_HEADERS) return@forEach
- if (k.equals("User-Agent", true)) return@forEach
if (k.equals("Content-Type", true)) contentType = v
headers.set(k, v)
}
diff --git a/base/extensions/src/main/kotlin/app/myspoti/extensions/PatternedBlowfish.kt b/base/extensions/src/main/kotlin/app/myspoti/extensions/PatternedBlowfish.kt
new file mode 100644
index 00000000..e5abe651
--- /dev/null
+++ b/base/extensions/src/main/kotlin/app/myspoti/extensions/PatternedBlowfish.kt
@@ -0,0 +1,79 @@
+package app.myspoti.extensions
+
+import java.io.InputStream
+import java.io.OutputStream
+import java.io.InterruptedIOException
+import javax.crypto.Cipher
+import javax.crypto.spec.SecretKeySpec
+import javax.crypto.spec.IvParameterSpec
+
+/** Bounded, provider-neutral download transform. Keys are supplied by the extension. */
+data class FileTransform(
+ val keyHex: String,
+ val ivHex: String,
+ val segmentBytes: Int = 2048,
+ val every: Int = 3,
+ val offset: Int = 0,
+) {
+ fun checked(): FileTransform {
+ require(keyHex.length in 8..112 && keyHex.length % 2 == 0 && keyHex.matches(Regex("[0-9a-fA-F]+"))) { "transform: invalid Blowfish key" }
+ require(ivHex.matches(Regex("[0-9a-fA-F]{16}"))) { "transform: invalid CBC IV" }
+ require(segmentBytes in 8..65536 && segmentBytes % 8 == 0) { "transform: invalid segment size" }
+ require(every in 1..1024 && offset in 0 until every) { "transform: invalid pattern" }
+ return this
+ }
+}
+
+object PatternedBlowfish {
+ /** Lazy, seekable decoding: upstream starts at an absolute segment-aligned byte offset. */
+ fun open(input: InputStream, spec: FileTransform, position: Long = 0): InputStream {
+ spec.checked()
+ require(position >= 0 && position % spec.segmentBytes == 0L) { "transform: unaligned range" }
+ val key = SecretKeySpec(spec.keyHex.chunked(2).map { it.toInt(16).toByte() }.toByteArray(), "Blowfish")
+ val iv = IvParameterSpec(spec.ivHex.chunked(2).map { it.toInt(16).toByte() }.toByteArray())
+ val cipher = Cipher.getInstance("Blowfish/CBC/NoPadding")
+ return object : InputStream() {
+ var index = position / spec.segmentBytes
+ var block = ByteArray(0)
+ var at = 0
+ var ended = false
+ fun fill(): Boolean {
+ if (at < block.size) return true
+ if (ended) return false
+ val next = ByteArray(spec.segmentBytes)
+ var n = 0
+ while (n < next.size) {
+ if (Thread.currentThread().isInterrupted) throw InterruptedIOException("transform cancelled")
+ val got = input.read(next,n,next.size-n)
+ if (got < 0) { ended = true; break }
+ if (got == 0) {
+ val one = input.read()
+ if (one < 0) { ended = true; break }
+ next[n++] = one.toByte()
+ } else n += got
+ }
+ block = if (n == next.size && index % spec.every == spec.offset.toLong()) {
+ cipher.init(Cipher.DECRYPT_MODE,key,iv)
+ cipher.doFinal(next)
+ } else next.copyOf(n)
+ at = 0; index++
+ return n > 0
+ }
+ override fun read(): Int = if (fill()) block[at++].toInt() and 255 else -1
+ override fun read(b: ByteArray, off: Int, len: Int): Int {
+ require(off >= 0 && len >= 0 && off <= b.size-len)
+ if (len == 0) return 0
+ if (!fill()) return -1
+ val n = minOf(len,block.size-at)
+ block.copyInto(b,off,at,at+n); at += n
+ return n
+ }
+ override fun close() { block.fill(0); ended = true; input.close() }
+ }
+ }
+
+ /** Full-file processing uses the same decoder as progressive playback. Streams remain caller-owned. */
+ fun transform(input: InputStream, output: OutputStream, spec: FileTransform) {
+ open(input, spec).copyTo(output)
+ }
+}
diff --git a/base/extensions/src/main/resources/app/myspoti/extensions/prelude.js b/base/extensions/src/main/resources/app/myspoti/extensions/prelude.js
index 91fa2be9..3a62a096 100644
--- a/base/extensions/src/main/resources/app/myspoti/extensions/prelude.js
+++ b/base/extensions/src/main/resources/app/myspoti/extensions/prelude.js
@@ -124,7 +124,10 @@
* What this app understands beyond the first API, for an extension to adapt to: "audio.manifest",
* "audio.expires", "audio.quality", "audio.source", "audio.ahead", "http.all", "status", "raw.base64", "results" (source hooks may answer { outcome }), "player.localMute" only when the user granted playback.mirror, and "attest" only when it granted attest.
*/
- features: Object.freeze(['audio.manifest', 'audio.expires', 'audio.quality', 'audio.source', 'audio.ahead', 'http.all', 'status', 'raw.base64', 'results'].concat(parse(host.info()).mirror ? ['player.localMute'] : [], parse(host.info()).attest ? ['attest'] : [])),
+ sources: Object.freeze({ race: function (hook, query, lanes) {
+ return parse(host.sourceRace(String(hook),JSON.stringify(query),JSON.stringify(lanes)));
+ } }),
+ features: Object.freeze(['audio.manifest', 'audio.expires', 'audio.quality', 'audio.source', 'audio.ahead', 'http.all', 'status', 'raw.base64', 'results', 'download.transform', 'audio.transform', 'sources.race'].concat(parse(host.info()).mirror ? ['player.localMute'] : [], parse(host.info()).attest ? ['attest'] : [])),
/**
* A sign-in page (webview.auth): open({ url, callback, hidden }) shows url in a web view of its
diff --git a/base/extensions/src/test/kotlin/app/myspoti/extensions/BasicHttpProviderTest.kt b/base/extensions/src/test/kotlin/app/myspoti/extensions/BasicHttpProviderTest.kt
new file mode 100644
index 00000000..994258b2
--- /dev/null
+++ b/base/extensions/src/test/kotlin/app/myspoti/extensions/BasicHttpProviderTest.kt
@@ -0,0 +1,16 @@
+package app.myspoti.extensions
+import okhttp3.*
+import okhttp3.ResponseBody.Companion.toResponseBody
+import org.junit.Assert.*
+import org.junit.Test
+class BasicHttpProviderTest {
+ @Test fun providerUserAgentAndUtf8ArePreserved() {
+ val client=OkHttpClient.Builder().addInterceptor { chain ->
+ assertEquals("SpotiFLAC-Mobile/test",chain.request().header("User-Agent"))
+ Response.Builder().request(chain.request()).protocol(Protocol.HTTP_1_1).code(200).message("OK")
+ .body("Björk 東京".toResponseBody()).build()
+ }.build()
+ val reply=BasicHttp.send(HttpRequest("https://example.org/",headers=mapOf("User-Agent" to "SpotiFLAC-Mobile/test")),"host-default",client)
+ assertEquals("Björk 東京",reply.body)
+ }
+}
diff --git a/base/extensions/src/test/kotlin/app/myspoti/extensions/DownloadTransformTest.kt b/base/extensions/src/test/kotlin/app/myspoti/extensions/DownloadTransformTest.kt
new file mode 100644
index 00000000..f8cd8af9
--- /dev/null
+++ b/base/extensions/src/test/kotlin/app/myspoti/extensions/DownloadTransformTest.kt
@@ -0,0 +1,23 @@
+package app.myspoti.extensions
+
+import org.junit.Assert.*
+import org.junit.Test
+
+class DownloadTransformTest {
+ private val transform = """{"algorithm":"blowfish-cbc-pattern","keyHex":"0000000000000000","ivHex":"0000000000000000","segmentBytes":2048,"every":3,"offset":0}"""
+ @Test fun parsesDownloadTransform() {
+ val f = DownloadFile.parseList("""{"url":"https://example.org/a.flac","format":"flac","transform":$transform}""").single()
+ assertNotNull(f.transform)
+ assertEquals(2048, f.transform!!.segmentBytes)
+ }
+ @Test fun parsesStreamingTransform() {
+ val f = AudioStream.parse("""{"url":"https://example.org/a.flac","transform":$transform}""")!!
+ assertNotNull(f.transform)
+ }
+ @Test fun unknownTransformIsNotSilentlyIgnored() {
+ try {
+ DownloadFile.parseList("""{"url":"https://example.org/a.flac","format":"flac","transform":{"algorithm":"unknown"}}""")
+ fail("unknown transform accepted")
+ } catch (_: IllegalArgumentException) {} catch (_: ExtensionException) {}
+ }
+}
diff --git a/base/extensions/src/test/kotlin/app/myspoti/extensions/PatternedBlowfishTest.kt b/base/extensions/src/test/kotlin/app/myspoti/extensions/PatternedBlowfishTest.kt
new file mode 100644
index 00000000..b63e4ada
--- /dev/null
+++ b/base/extensions/src/test/kotlin/app/myspoti/extensions/PatternedBlowfishTest.kt
@@ -0,0 +1,72 @@
+package app.myspoti.extensions
+
+import org.junit.Assert.*
+import org.junit.Test
+import java.io.ByteArrayInputStream
+import java.io.ByteArrayOutputStream
+import javax.crypto.Cipher
+import javax.crypto.spec.SecretKeySpec
+import javax.crypto.spec.IvParameterSpec
+
+class PatternedBlowfishTest {
+ private val key = "0000000000000000"
+ private val iv = "0000000000000000"
+
+ @Test fun publishedBlowfishVector() {
+ val out = ByteArrayOutputStream()
+ PatternedBlowfish.transform(ByteArrayInputStream(hex("4ef997456198dd78")), out,
+ FileTransform(key, iv, 8, 1, 0))
+ assertArrayEquals(ByteArray(8), out.toByteArray())
+ }
+
+ @Test fun partialReadsPatternAndTail() {
+ val clear = ByteArray(2048 * 8 + 117) { (it * 31).toByte() }
+ val encrypted = clear.copyOf()
+ val cipher = Cipher.getInstance("Blowfish/CBC/NoPadding")
+ for (offset in 0 until clear.size step 2048) {
+ if (offset / 2048 % 3 == 0 && clear.size - offset >= 2048) {
+ cipher.init(Cipher.ENCRYPT_MODE, SecretKeySpec(hex(key), "Blowfish"), IvParameterSpec(hex(iv)))
+ cipher.doFinal(clear, offset, 2048).copyInto(encrypted, offset)
+ }
+ }
+ val input = object : ByteArrayInputStream(encrypted) {
+ override fun read(b: ByteArray, off: Int, len: Int) = super.read(b, off, minOf(len, 37))
+ }
+ val out = ByteArrayOutputStream()
+ PatternedBlowfish.transform(input, out, FileTransform(key, iv, 2048, 3, 0))
+ assertArrayEquals(clear, out.toByteArray())
+ }
+
+ @Test fun independentPythonEncryptedFlac() {
+ val encrypted = javaClass.getResourceAsStream("/blowfish/sine.flac.enc")!!.readBytes()
+ val expected = javaClass.getResourceAsStream("/blowfish/sine.sha256")!!.bufferedReader().readText().trim()
+ val out = ByteArrayOutputStream()
+ PatternedBlowfish.transform(ByteArrayInputStream(encrypted), out,
+ FileTransform("000102030405060708090a0b0c0d0e0f", "0001020304050607"))
+ val clear = out.toByteArray()
+ val digest = java.security.MessageDigest.getInstance("SHA-256").digest(clear).joinToString("") { "%02x".format(it.toInt() and 255) }
+ assertEquals(expected, digest)
+ assertEquals("fLaC", String(clear.copyOfRange(0, 4), Charsets.US_ASCII))
+ }
+
+ @Test fun seekedStreamMatchesWholeFileAtEveryBoundary() {
+ val encrypted = javaClass.getResourceAsStream("/blowfish/sine.flac.enc")!!.readBytes()
+ val spec = FileTransform("000102030405060708090a0b0c0d0e0f", "0001020304050607")
+ val whole = ByteArrayOutputStream().also { PatternedBlowfish.transform(ByteArrayInputStream(encrypted), it, spec) }.toByteArray()
+ for (at in listOf(0,1,2047,2048,4096,6143,6144,6145,whole.size-100)) {
+ val aligned = at / 2048 * 2048
+ val decoded = PatternedBlowfish.open(ByteArrayInputStream(encrypted.copyOfRange(aligned, encrypted.size)), spec, aligned.toLong())
+ var skip = at-aligned
+ while (skip-- > 0) assertTrue(decoded.read() >= 0)
+ assertArrayEquals(whole.copyOfRange(at,whole.size), decoded.readBytes())
+ }
+ }
+
+ @Test fun rejectsInvalidParameters() {
+ for (t in listOf(FileTransform("bad",iv),FileTransform(key,"00"),FileTransform(key,iv,7),FileTransform(key,iv,2048,0))) {
+ try { PatternedBlowfish.transform(ByteArrayInputStream(byteArrayOf(1)),ByteArrayOutputStream(),t); fail("invalid transform accepted") }
+ catch (_: IllegalArgumentException) {}
+ }
+ }
+ private fun hex(s: String) = s.chunked(2).map { it.toInt(16).toByte() }.toByteArray()
+}
diff --git a/base/extensions/src/test/kotlin/app/myspoti/extensions/SandboxTest.kt b/base/extensions/src/test/kotlin/app/myspoti/extensions/SandboxTest.kt
index f82b5267..c408ce2f 100644
--- a/base/extensions/src/test/kotlin/app/myspoti/extensions/SandboxTest.kt
+++ b/base/extensions/src/test/kotlin/app/myspoti/extensions/SandboxTest.kt
@@ -199,7 +199,7 @@ class SandboxTest {
}
@Test fun featuresNameWhatThisAppUnderstands() {
- assertEquals("""{"v":"audio.manifest,audio.expires,audio.quality,audio.source,audio.ahead,http.all,status,raw.base64,results"}""", eval("return myspoti.features.join(',');"))
+ assertEquals("""{"v":"audio.manifest,audio.expires,audio.quality,audio.source,audio.ahead,http.all,status,raw.base64,results,download.transform,audio.transform,sources.race"}""", eval("return myspoti.features.join(',');"))
}
@Test fun localMuteFeatureIsListedOnlyWhenMirrorIsGranted() {
diff --git a/base/extensions/src/test/kotlin/app/myspoti/extensions/SourceRaceSafetyTest.kt b/base/extensions/src/test/kotlin/app/myspoti/extensions/SourceRaceSafetyTest.kt
new file mode 100644
index 00000000..28956209
--- /dev/null
+++ b/base/extensions/src/test/kotlin/app/myspoti/extensions/SourceRaceSafetyTest.kt
@@ -0,0 +1,37 @@
+package app.myspoti.extensions
+import org.junit.Assert.*
+import org.junit.Test
+import java.util.concurrent.atomic.AtomicInteger
+class SourceRaceSafetyTest {
+ private fun run(script:String, limit:Int=120):Pair<String?,Int> {
+ val count=AtomicInteger()
+ val m=Manifest("io.test.safe_race","Race","1",author="Test",provides=listOf("download-source"),permissions=listOf(PermissionRequest("download.source"),PermissionRequest("network",listOf("example.org"))))
+ val host=object:Host {
+ val store=MemoryStorage()
+ override fun storage(extensionId:String)=store
+ override fun setting(extensionId:String,key:String):String?=null
+ override fun log(extensionId:String,message:String) {}
+ override fun http(extensionId:String,request:HttpRequest):HttpResponse { count.incrementAndGet();return HttpResponse(200,emptyMap(),"{}") }
+ }
+ val result=ExtensionRuntime(m,script,host,m.asks().map {it.key}.toSet(),ExtensionRuntime.Limits(requestsPerMinute=limit)).use {
+ it.start();it.call(Hook.DOWNLOAD_SOURCE,"""{"quality":"cd"}""",5000)
+ }
+ return result to count.get()
+ }
+ @Test fun lanesShareOneHttpQuota() {
+ val (out,count)=run("""
+ myspoti.provide('download-source',function(t){
+ if(!t.source)return myspoti.sources.race('download-source',t,['a','b','c']);
+ myspoti.http.get('https://example.org/'+t.source);
+ return [{url:'https://example.org/a.flac',format:'flac'}];
+ });
+ """,1)
+ assertNotNull(out);assertEquals(1,count)
+ }
+ @Test fun nestedRacesCannotMultiplyWorkers() {
+ val (out,count)=run("""
+ myspoti.provide('download-source',function(t){return myspoti.sources.race('download-source',t,['a','b']);});
+ """)
+ assertNull(out);assertEquals(0,count)
+ }
+}
diff --git a/base/extensions/src/test/kotlin/app/myspoti/extensions/SourceRaceTest.kt b/base/extensions/src/test/kotlin/app/myspoti/extensions/SourceRaceTest.kt
new file mode 100644
index 00000000..ae5e3821
--- /dev/null
+++ b/base/extensions/src/test/kotlin/app/myspoti/extensions/SourceRaceTest.kt
@@ -0,0 +1,40 @@
+package app.myspoti.extensions
+
+import org.junit.Assert.*
+import org.junit.Test
+
+class SourceRaceTest {
+ @Test fun firstUsableSourceWinsWithoutWaitingForSlowOne() {
+ val m = Manifest("io.test.race", "Race", "1", author="Test", provides=listOf("download-source"),
+ permissions=listOf(PermissionRequest("download.source"),PermissionRequest("network",listOf("example.org"))))
+ val host = object : Host {
+ val store = MemoryStorage()
+ override fun storage(extensionId:String)=store
+ override fun setting(extensionId:String,key:String):String?=null
+ override fun log(extensionId:String,message:String) {}
+ override fun http(extensionId:String,request:HttpRequest):HttpResponse {
+ when(request.url.substringAfterLast('/')) {
+ "slow" -> Thread.sleep(3000)
+ "fast" -> Thread.sleep(60)
+ }
+ return HttpResponse(200,emptyMap(),"{}")
+ }
+ }
+ val script = """
+ myspoti.provide('download-source', function(t) {
+ if (!t.source) return myspoti.sources.race('download-source', t, ['slow','empty','lossy','fast']);
+ myspoti.http.get('https://example.org/'+t.source);
+ if(t.source==='empty') return [];
+ if(t.source==='lossy') return [{url:'https://example.org/a.mp3',format:'mp3'}];
+ return [{url:'https://example.org/'+t.source+'.flac',format:'flac',isrcMatched:true}];
+ });
+ """
+ ExtensionRuntime(m,script,host,m.asks().map {it.key}.toSet()).use { rt ->
+ rt.start()
+ val start=System.nanoTime()
+ val files=DownloadFile.parseList(rt.call(Hook.DOWNLOAD_SOURCE,"""{"quality":"cd"}""",8000)!!)
+ assertTrue(files.single().url!!.contains("fast.flac"))
+ assertTrue("waited for losing source", (System.nanoTime()-start)/1_000_000 < 2500)
+ }
+ }
+}
diff --git a/base/extensions/src/test/resources/blowfish/sine.flac.enc b/base/extensions/src/test/resources/blowfish/sine.flac.enc
new file mode 100644
index 00000000..690100f0
Binary files /dev/null and b/base/extensions/src/test/resources/blowfish/sine.flac.enc differ
diff --git a/base/extensions/src/test/resources/blowfish/sine.sha256 b/base/extensions/src/test/resources/blowfish/sine.sha256
new file mode 100644
index 00000000..12a08f22
--- /dev/null
+++ b/base/extensions/src/test/resources/blowfish/sine.sha256
@@ -0,0 +1 @@
+10c726513e68fe145a561c659041b346a2b3d4f658708ad00c9841749b50ff3b
\ No newline at end of file
diff --git a/docs/extensions/API.md b/docs/extensions/API.md
index 625f7514..dfa4361a 100644
--- a/docs/extensions/API.md
+++ b/docs/extensions/API.md
@@ -11,6 +11,10 @@ API it unlocks; the user sees them as icons, each with your reason, before insta
This page documents what the build ships (API version 1). Nothing here is a promise about
anything else.
+Feature-gated media transforms and concurrent source resolution are described in
+[SOURCE-PIPELINES.md](SOURCE-PIPELINES.md). Providers and recording matching remain
+extension responsibilities; the host keeps its existing safe media transport.
+
## 1. A minimal extension
`manifest.json`:
diff --git a/docs/extensions/SOURCE-PIPELINES.md b/docs/extensions/SOURCE-PIPELINES.md
new file mode 100644
index 00000000..a50d39c0
--- /dev/null
+++ b/docs/extensions/SOURCE-PIPELINES.md
@@ -0,0 +1,52 @@
+# Source transforms and racing (experimental)
+
+Feature-detect exact strings in `myspoti.features`; never infer these from an app version.
+
+## `download.transform` and `audio.transform`
+
+A download file or audio answer may contain:
+
+```json
+{"transform":{"algorithm":"blowfish-cbc-pattern","keyHex":"000102030405060708090a0b0c0d0e0f","ivHex":"0001020304050607","segmentBytes":2048,"every":3,"offset":0}}
+```
+
+The host knows no provider or key-derivation scheme. The extension supplies the key and IV.
+CBC has no padding and restarts its IV for each selected complete segment. Segment indices
+start at zero; only indices congruent to `offset` modulo `every` are transformed. The final
+incomplete segment is copied unchanged. Key length is 4–56 bytes, IV exactly 8 bytes,
+segment size 8–65536 bytes and divisible by 8; `every` is 1–1024 and `offset` is below it.
+Malformed or unknown transforms are rejected, never silently ignored. DASH cannot carry one.
+
+Downloads transform into a temporary file before the existing audio identity/length checks
+and local encryption. Cancellation deletes the temporary output. Streaming decrypts on demand
+with bounded buffers; byte-range seeks align upstream to a segment boundary, preserve absolute
+segment indices, and discard the prefix before the requested byte. A bounded read still fetches
+complete cipher segments. Encrypted read-ahead heads are not reused as decoded data.
+
+Do not return an encrypted file without the appropriate feature. A normal clear file omits
+`transform` entirely. Never log session secrets or derived keys.
+
+## `sources.race`
+
+```js
+myspoti.provide('download-source', function (query) {
+ if (!query.source) {
+ return myspoti.sources.race('download-source', query, ['one', 'two']);
+ }
+ return resolveOnly(query.source, query);
+});
+```
+
+Also supports `audio-source`. One to four named lanes run independent copies of the same
+extension script, receiving `query.source`. They share the extension's own storage/settings,
+but never a Rhino scope. Top-level extension code must therefore be idempotent. Do not do
+pairing or other UI work in a lane. No nested races; source workers cannot call Spotify APIs
+or open sign-in windows. Network requests retain host checks and a shared parent HTTP quota;
+notifications share the parent's quota. Root hook time limits include all lane work.
+
+The first non-empty parseable answer wins; a CD/hi-res download cannot win with a lossy file.
+A failed/empty lane does not win. Remaining workers are interrupted and closed. Transport
+cancellation remains best-effort and bounded by request timeouts. This selects a descriptor;
+normal file validation still decides whether the audio is the requested recording. A hi-res
+preference with CD fallback remains a preference; implement strict quality in each resolver
+if required. Old hosts must keep a sequential fallback instead of calling this API.