diff --git a/server.js b/server.js index a9a781d..b3c538d 100644 --- a/server.js +++ b/server.js @@ -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}`); }