Rooms sync: a TV never syncs with itself

Each TV finds its own room on the Wi-Fi too; upstairs synced with itself on its first round and
recorded its own address. Rounds now skip this TV's own addresses, and a sync from one of them
is refused. 0.108.103.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-07 23:00:08 +00:00
co-authored by Claude Opus 5.5
parent b3fb2f518f
commit b97af84330
4 changed files with 14 additions and 7 deletions
+2 -2
View File
@@ -9,8 +9,8 @@ android {
applicationId = "tv.orbit.classic"
minSdk = 26
targetSdk = 36
versionCode = 262
versionName = "0.108.102-rooms-sync"
versionCode = 263
versionName = "0.108.103-sync-not-self"
}
buildFeatures { compose = true }
// The finished build for the TVs (Paul, 6 November 2026: the test build stuttered on the box). Not debuggable, so
@@ -62,9 +62,9 @@ internal class HttpsEmbyTransport(internal val deviceId: String = UUID.randomUUI
}
/** The sign-in headers for this kind of server. */
internal fun authHeaders(token: String?): Map<String, String> = when (kind) {
ServerKind.Emby -> mapOf("X-Emby-Authorization" to "Emby Client=\"Orbit TV\", Device=\"Orbit TV\", DeviceId=\"$deviceId\", Version=\"0.108.102\"") +
ServerKind.Emby -> mapOf("X-Emby-Authorization" to "Emby Client=\"Orbit TV\", Device=\"Orbit TV\", DeviceId=\"$deviceId\", Version=\"0.108.103\"") +
(token?.let { mapOf("X-Emby-Token" to it) } ?: emptyMap())
ServerKind.Jellyfin -> mapOf("Authorization" to "MediaBrowser Client=\"Orbit TV\", Device=\"Orbit TV\", DeviceId=\"$deviceId\", Version=\"0.108.102\"" +
ServerKind.Jellyfin -> mapOf("Authorization" to "MediaBrowser Client=\"Orbit TV\", Device=\"Orbit TV\", DeviceId=\"$deviceId\", Version=\"0.108.103\"" +
(token?.let { ", Token=\"$it\"" } ?: ""))
}
/** One picture from the provider, or nothing. Same fixed origin and no redirects, and failures are never shown or logged. */
@@ -108,6 +108,7 @@ internal object RoomSync {
fun answer(context: Context, scheduleDir: File, body: String, from: String?, nowMs: Long = System.currentTimeMillis()): Pair<Int, JSONObject> {
val req = runCatching { JSONObject(body) }.getOrNull() ?: return 400 to JSONObject()
val link = RoomLinks.of(req.optString("link"))?.takeIf { it.on } ?: return 404 to JSONObject()
if (from != null && from in ownAddresses()) return 409 to JSONObject()
val o = open(link, req.optString("sealed"), nowMs) ?: return 403 to JSONObject()
merge(context, scheduleDir, o, nowMs)
val reply = state(context, scheduleDir, o.optLong("since"), nowMs)
@@ -116,10 +117,16 @@ internal object RoomSync {
return 200 to JSONObject().put("sealed", RoomCrypto.seal(link.key, reply.toString().toByteArray()))
}
/** One round from this TV: each paired room it can reach, at its last address or as found on the Wi-Fi. */
/** This TV's own addresses: it finds itself on the Wi-Fi too, and must never sync with itself (seen 7 November 2026). */
fun ownAddresses(): Set<String> = runCatching {
java.net.NetworkInterface.getNetworkInterfaces().toList().flatMap { it.inetAddresses.toList() }.mapNotNull { it.hostAddress?.substringBefore('%') }.toSet()
}.getOrDefault(emptySet())
/** One round from this TV: each paired room it can reach, at its last address or as found on the Wi-Fi, never itself. */
fun round(context: Context, scheduleDir: File, found: List<String>, nowMs: Long = System.currentTimeMillis()) {
val mine = ownAddresses()
for (link in RoomLinks.links.filter { it.on }) {
for (address in (listOfNotNull(link.address) + found).distinct()) {
for (address in (listOfNotNull(link.address) + found).distinct().filter { it !in mine }) {
val call = roomTransport(address) ?: continue
val sent = state(context, scheduleDir, link.lastSyncMs, nowMs)
val (status, text) = runCatching { call("POST", "/room/sync", JSONObject().put("link", link.id).put("sealed", RoomCrypto.seal(link.key, sent.toString().toByteArray())).toString()) }
@@ -39,7 +39,7 @@ internal class SecureMediaDataSource(private val session: EmbySampleSession) : B
next.instanceFollowRedirects = false
next.connectTimeout = 10_000
next.readTimeout = 20_000
next.setRequestProperty("User-Agent", "OrbitTV/0.108.102")
next.setRequestProperty("User-Agent", "OrbitTV/0.108.103")
next.setRequestProperty("Accept-Encoding", "identity")
session.mediaHeaders(address).forEach { (key, value) -> next.setRequestProperty(key, value) }
if (dataSpec.position != 0L || dataSpec.length != C.LENGTH_UNSET.toLong()) {