fix(agent): use gateway-relative dashboard URL when behind hive-gateway

When the agent page is accessed via hive-gateway (path is /agent/<name>/)
the dashboard lives at the same origin's root (/), not at the direct
TCP port. Both app.js (overflow menu / setHeader) and stats.js
(dashboard-link anchor) previously hardcoded http://hostname:port which
is wrong or unreachable behind HTTPS TLS termination.

Detection: if location.pathname starts with '/agent/', use
location.origin + '/' as the dashboard base. Otherwise fall back to the
original direct-TCP construction for non-gateway deploys.
This commit is contained in:
iris 2026-06-05 11:34:26 +02:00 committed by mara
commit defcb5df0b
2 changed files with 15 additions and 2 deletions

View file

@ -155,7 +155,14 @@ window.marked = marked;
const hiveLabel = hiveName || null;
const tab = qualifiedLabel && qualifiedLabel !== label ? qualifiedLabel : label;
document.title = hiveLabel ? `${label} // ${hiveLabel}` : `${tab} // hyperhive`;
const dashUrl = `${location.protocol}//${location.hostname}:${dashboardPort}/`;
// When accessed via hive-gateway the page lives at
// `/agent/<name>/` on the same origin as the dashboard (`/`).
// Detect this by the path prefix and use `location.origin` instead
// of the direct TCP port — the direct port is unreachable or
// wrong scheme behind HTTPS TLS termination.
const dashUrl = location.pathname.startsWith('/agent/')
? location.origin + '/'
: `${location.protocol}//${location.hostname}:${dashboardPort}/`;
dashboardBase = dashUrl;
populateOverflowMenu(label, dashUrl);
// Swarm label in the chrome: show "swarm/hive" when both set, just

View file

@ -330,7 +330,13 @@ window.Chart = Chart;
document.title = 'stats · ' + s.label;
document.getElementById('title').textContent = '◆ ' + s.label + ' ◆';
const dl = document.getElementById('dashboard-link');
dl.href = 'http://' + window.location.hostname + ':' + s.dashboard_port + '/';
// When accessed via hive-gateway the page lives at `/agent/<name>/`
// on the same origin as the dashboard (`/`). Detect via path prefix
// rather than the direct port (which is unreachable or wrong scheme
// behind HTTPS TLS termination).
dl.href = window.location.pathname.startsWith('/agent/')
? window.location.origin + '/'
: 'http://' + window.location.hostname + ':' + s.dashboard_port + '/';
} catch (_) { /* non-fatal */ }
}