Plex: read the server identity as JSON or XML and show ids on mismatch
build-container / build (push) Successful in 2m3s

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
nmemmertandClaude Sonnet 5.5 committed 2026-10-20 14:27:42 -04:00
1 parent 732b73677b
commit cff7e9e59c
1 file changed
+5 -2
+5 -2
View File
@@ -216,8 +216,11 @@ async function plexApi(req, res) {
// /identity needs no token, so the token is never sent to a host before we know it is this server.
const t = await fetch(c.uri + '/identity', { headers: PLEX_HEADERS, signal: AbortSignal.timeout(c.t) });
if (!t.ok) { tried.push(`${c.label}: HTTP ${t.status}`); continue; }
const mid = (await t.json().catch(() => ({}))).MediaContainer?.machineIdentifier;
if (mid !== id) { tried.push(`${c.label}: a different server answered`); continue; }
// Plex answers /identity as JSON or XML depending on the server; accept both.
const body = await t.text();
let mid; try { mid = JSON.parse(body).MediaContainer?.machineIdentifier; } catch { mid = /machineIdentifier="([^"]+)"/.exec(body)?.[1]; }
if (!mid) { tried.push(`${c.label}: answered, but not like a Plex server`); continue; }
if (String(mid).toLowerCase() !== String(id).toLowerCase()) { tried.push(`${c.label}: a different server answered (id ${String(mid).slice(0, 6)}…, wanted ${String(id).slice(0, 6)}…)`); continue; }
setCookies(req, res, { plex_srv: signed(c.uri), plex_stk: r.accessToken, plex_name: r.name });
return json(res, 200, { ok: true, name: r.name, via: c.label });
} catch (e) { tried.push(`${c.label}: ${e.cause?.code || e.name || e.message}`); }