Re-pick Plex server address on network change, foreground, and stream failure (dev 36)
The address chosen at sign-in (LAN first) was kept forever, so away from home streams hit a dead LAN address (TLS error) or the relay (503). Store every address plex.tv lists, probe them all bypassing the offline cache, and keep the best one that answers. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9f68aa588b
commit
e02428ab70
7 files changed
+100
-17
No files matched your search
@@ -375,21 +375,41 @@ final class PlexServer: MediaSource, @unchecked Sendable {
|
||||
/// The first address that answers /identity as the expected server, local ones first, relay last.
|
||||
func reach(_ r: PlexResource, userToken: String) async -> String? {
|
||||
let tok = r.accessToken.isEmpty ? userToken : r.accessToken
|
||||
let ordered = r.connections.filter { !$0.uri.isEmpty }.sorted { rank($0) < rank($1) }
|
||||
for c in ordered {
|
||||
let base = c.uri.trimmingCharacters(in: CharacterSet(charactersIn: "/"))
|
||||
guard let u = URL(string: base + "/identity") else { continue }
|
||||
var req = request(u, withToken: false)
|
||||
req.setValue(tok, forHTTPHeaderField: "X-Plex-Token")
|
||||
req.timeoutInterval = 3.5
|
||||
guard let body = try? await http.data(req),
|
||||
let id = try? decode(body).machineIdentifier, id == r.clientIdentifier || r.clientIdentifier.isEmpty else { continue }
|
||||
return base
|
||||
}
|
||||
return nil
|
||||
return await firstAlive(Self.ranked(r), machineId: r.clientIdentifier, token: tok)
|
||||
}
|
||||
|
||||
private func rank(_ c: PlexResource.Connection) -> Int { c.relay ? 2 : (c.local ? 0 : 1) }
|
||||
/// A resource's addresses, local first, then remote, relay last.
|
||||
static func ranked(_ r: PlexResource) -> [String] {
|
||||
r.connections.filter { !$0.uri.isEmpty }
|
||||
.sorted { ($0.relay ? 2 : ($0.local ? 0 : 1)) < ($1.relay ? 2 : ($1.local ? 0 : 1)) }
|
||||
.map { $0.uri.trimmingCharacters(in: CharacterSet(charactersIn: "/")) }
|
||||
}
|
||||
|
||||
/// Probes every address's /identity at once, straight to the network (the offline cache
|
||||
/// would make a dead LAN address look alive), and returns the earliest one in [uris] that
|
||||
/// answers as [machineId].
|
||||
func firstAlive(_ uris: [String], machineId: String, token: String, timeout: TimeInterval = 4) async -> String? {
|
||||
let bases = uris.map { $0.trimmingCharacters(in: CharacterSet(charactersIn: "/")) }
|
||||
let alive: [Int] = await withTaskGroup(of: Int?.self) { group in
|
||||
for (i, b) in bases.enumerated() {
|
||||
group.addTask { [self] in
|
||||
guard let u = URL(string: b + "/identity") else { return nil }
|
||||
var req = request(u, withToken: false)
|
||||
req.setValue(token, forHTTPHeaderField: "X-Plex-Token")
|
||||
req.timeoutInterval = timeout
|
||||
guard let (body, resp) = try? await http.session.data(for: req),
|
||||
(resp as? HTTPURLResponse)?.statusCode == 200,
|
||||
let id = try? decode(body).machineIdentifier,
|
||||
machineId.isEmpty || id == machineId else { return nil }
|
||||
return i
|
||||
}
|
||||
}
|
||||
var out: [Int] = []
|
||||
for await i in group { if let i { out.append(i) } }
|
||||
return out
|
||||
}
|
||||
return alive.min().map { bases[$0] }
|
||||
}
|
||||
|
||||
func checkPin(_ id: Int64) async throws -> PlexPin {
|
||||
let req = request(URL(string: "https://plex.tv/api/v2/pins/\(id)")!, withToken: false)
|
||||
|
||||
Reference in new issue
Block a user