diff --git a/internal/gui/api.go b/internal/gui/api.go index 30bb9260a..5d709cbfe 100644 --- a/internal/gui/api.go +++ b/internal/gui/api.go @@ -894,8 +894,13 @@ func Handler(w Windows, gw *gateway.Server) http.Handler { // and MAGPIE_PUBLIC_URL while other machines are let in, which // a page they open (magpie web) shows instead (noahhrreion on // Discord: in Docker the line said the container's loopback) - if u := gateway.PublicURL(); u != "" && (s.LAN || gateway.OnNetwork()) { - side["publicURL"] = u + // and, shared, the addresses other machines use, which the + // window's line names from the first paint (coeo91 on Discord) + if s.LAN || gateway.OnNetwork() { + side["lan"], side["lanURLs"] = true, gateway.LANURLs() + if u := gateway.PublicURL(); u != "" { + side["publicURL"] = u + } } boot["sideGateway"] = side } diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js index c8fb87bb3..14e339f91 100644 --- a/internal/gui/assets/app.js +++ b/internal/gui/assets/app.js @@ -24065,7 +24065,7 @@ function renderSideGw(g) { if (b.hidden) return; // a page other machines open (magpie web) says where they reach it, // MAGPIE_PUBLIC_URL, as the Gateway page does (#1551; noahhrreion) - const url = web && g.publicURL || g.url; + const url = self.sideLAN?.(g) || web && g.publicURL || g.url; let host = url; try { host = new URL(url).host; } catch {} b.classList.toggle("off", !g.running); @@ -24075,7 +24075,7 @@ function renderSideGw(g) { // gateway leaves it no room ("Zugangspunkt") says the address alone, the // word kept in its tooltip if (g.running && s.scrollWidth > s.clientWidth) s.textContent = host; - b.title = g.running ? t("Gateway") + " " + url : url; + b.title = (g.running ? t("Gateway") + " " + url : url) + (web || url == g.url ? "" : " · " + g.url); } if (mode === "window") { $("#sideGw").onclick = () => { show("gateway"); $("#sideGw").blur(); }; diff --git a/internal/gui/assets/plugins/gateway/settings.js b/internal/gui/assets/plugins/gateway/settings.js index db87d88c3..331aaeb45 100644 --- a/internal/gui/assets/plugins/gateway/settings.js +++ b/internal/gui/assets/plugins/gateway/settings.js @@ -240,6 +240,15 @@ function lanShown(urls, container) { sub: t("Where this page was opened, on the gateway’s port; MAGPIE_PUBLIC_URL sets another") }; return { urls, sub: t("The container’s own addresses, which other devices can’t reach: set MAGPIE_PUBLIC_URL to the host’s") }; } +// sideLAN: where other machines reach the gateway while it is shared, for +// the sidebar's line (coeo91 on Discord): MAGPIE_PUBLIC_URL, else the +// address picked here, else the first +function sideLAN(g) { + const u = g.lan && g.lanURLs || []; + return u[0] && (g.publicURL || (u.includes(lanWant) ? lanWant : u[0])); +} +// the line was drawn from boot.js before this script came +if (sideGwAt) renderSideGw(); function renderLAN(s) { const box = $("#lanList"); box.replaceChildren(); @@ -257,6 +266,8 @@ function renderLAN(s) { }; const set = (body) => writingPrefs(api("settings/lan", body)).then((ns) => { prefs = ns; renderSettings(); + // the sidebar's gateway names the address shared, or this computer's + api("providers").then((r) => renderSideGw(r?.gateway), () => {}); if (ns.rekeyed) status(ns.rekeyed, "err"); }) .catch((e) => { status(t(e.message), "err"); renderSettings(); }); @@ -281,7 +292,7 @@ function renderLAN(s) { if (address.classList.contains("open")) return closeProtoMenu(); const suffix = lanProtocol === "openai" ? "/v1" : ""; openProtoMenu(address, urls.map((u) => ({ v: u, name: u + suffix, note: far.has(u) ? t("On the internet") : "" })), lanSelectedURL, - (v) => { lanSelectedURL = lanWant = v; keepPick("magpie.lanAddress", v); update(); }, "Address", "lan-address-menu"); + (v) => { lanSelectedURL = lanWant = v; keepPick("magpie.lanAddress", v); update(); renderSideGw(); }, "Address", "lan-address-menu"); }; } const copyControl = el("span", "lan-copy"); diff --git a/internal/gui/builtins_test.go b/internal/gui/builtins_test.go index fadcceb62..2dcde8b9a 100644 --- a/internal/gui/builtins_test.go +++ b/internal/gui/builtins_test.go @@ -160,18 +160,20 @@ func TestBootSaysWhereItsGatewayListens(t *testing.T) { t.Fatalf("served here: %s", b) } t.Setenv("MAGPIE_ADDR", "0.0.0.0:3483") - if b := boot(); !strings.Contains(b, `"sideGateway":{"boot":true,"running":true,"url":"http://127.0.0.1:3483"}`) { + // on the network, it says so, with the addresses other machines use, + // for the window's line to name them (coeo91 on Discord) + if b := boot(); !strings.Contains(b, `"sideGateway":{"boot":true,"lan":true,`) || !strings.Contains(b, `"running":true,"url":"http://127.0.0.1:3483"}`) { t.Fatalf("every interface, reached on loopback: %s", b) } // MAGPIE_PUBLIC_URL goes with it while other machines are let in, for // magpie web's page to show (noahhrreion on Discord, in Docker), and // not while the gateway is this computer's alone t.Setenv("MAGPIE_PUBLIC_URL", "http://nas.local:3425") - if b := boot(); !strings.Contains(b, `"sideGateway":{"boot":true,"publicURL":"http://nas.local:3425","running":true,"url":"http://127.0.0.1:3483"}`) { + if b := boot(); !strings.Contains(b, `"sideGateway":{"boot":true,"lan":true,"lanURLs":["http://nas.local:3425"],"publicURL":"http://nas.local:3425","running":true,"url":"http://127.0.0.1:3483"}`) { t.Fatalf("on the network, with MAGPIE_PUBLIC_URL: %s", b) } t.Setenv("MAGPIE_ADDR", "127.0.0.1:3483") - if b := boot(); strings.Contains(b, "nas.local") { + if b := boot(); strings.Contains(b, "nas.local") || strings.Contains(b, `"lan"`) { t.Fatalf("on loopback, not shared: %s", b) } t.Setenv("MAGPIE_PUBLIC_URL", "") diff --git a/internal/gui/tests/fixtures/caller-keys.cjs b/internal/gui/tests/fixtures/caller-keys.cjs index fad841045..3482e6e81 100644 --- a/internal/gui/tests/fixtures/caller-keys.cjs +++ b/internal/gui/tests/fixtures/caller-keys.cjs @@ -60,9 +60,11 @@ function fixture(lang, theme, events, options = {}) { const req = route.request(), url = new URL(req.url()); const json = (data) => route.fulfill({ json: data }); if (url.pathname === "/boot.js") return route.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = ${JSON.stringify({ lang, theme, web: !!options.web, - // with options.publicURL, where this process's gateway listens, as - // magpie's boot.js says it, MAGPIE_PUBLIC_URL with it while shared - ...(options.publicURL ? { sideGateway: { url: "http://127.0.0.1:3999", running: true, boot: true, ...publicURL() } } : {}) })};` }); + // with options.publicURL or options.bootGateway, where this process's + // gateway listens, as magpie's boot.js says it, and while shared the + // addresses other machines use, MAGPIE_PUBLIC_URL with them + ...(options.publicURL || options.bootGateway ? { sideGateway: { url: "http://127.0.0.1:3999", running: true, boot: true, + ...(lan ? { lan, lanURLs } : {}), ...publicURL() } } : {}) })};` }); if (url.pathname === "/wails/runtime.js") return route.fulfill({ contentType: "text/javascript", body: "export const Window = {};" }); if (url.pathname === "/favicon.ico") return route.fulfill({ status: 204 }); if (url.pathname === "/api/state") return json({ agents: [], profiles: [], settings: { lang, theme } }); diff --git a/internal/gui/tests/public-url-default.test.cjs b/internal/gui/tests/public-url-default.test.cjs index bf7ef4b0d..b4626c8de 100644 --- a/internal/gui/tests/public-url-default.test.cjs +++ b/internal/gui/tests/public-url-default.test.cjs @@ -68,7 +68,12 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", assert.doesNotMatch(await shown("#connect pre.snip"), /127\.0\.0\.1/); // in the app, the gateway's own address stays this computer's assert.equal(await shown("#gatewayLan .gw-url:first-child code"), LOCAL); - if (width >= 1100) assert.match(await shown("#sideGw span"), /127\.0\.0\.1:3999/); + // the sidebar's line, shared, names where other machines reach it + // (coeo91), this computer's in its tooltip + if (width >= 1100) { + assert.match(await shown("#sideGw span"), /magpie\.example\.com/); + assert.match(await page.locator("#sideGw").getAttribute("title"), /127\.0\.0\.1:3999/); + } await page.locator("#connectAddress").click(); assert.match(await page.locator(".proto-menu .pm-item", { hasText: "magpie.example.com" }).textContent(), new RegExp(words[lang])); await fits("Gateway"); diff --git a/internal/gui/tests/side-gateway-lan.test.cjs b/internal/gui/tests/side-gateway-lan.test.cjs new file mode 100644 index 000000000..77c5403a4 --- /dev/null +++ b/internal/gui/tests/side-gateway-lan.test.cjs @@ -0,0 +1,85 @@ +const assert = require("node:assert/strict"); +const { test } = require("node:test"); +const { chromium, webkit } = require("playwright"); +const { fixture } = require("./fixtures/caller-keys.cjs"); +const { openPart, reach } = require("./reader.cjs"); + +// coeo91 on Discord: with Share on local network on, the sidebar's gateway +// line at its foot still said "Gateway 127.0.0.1:3425", which other +// computers can't reach. Shared, it names the LAN address, the one picked +// under Share on local network, from the first paint; its tooltip names +// both that and this computer's. Sharing turned off or on in Settings, or +// another address picked there, redraws it at once. Not shared, it is +// this computer's, as before. +const LOCAL = "http://127.0.0.1:3999"; +const lanURLs = ["http://192.168.1.10:3999", "http://10.0.0.10:3999"]; + +for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) { + for (const lang of ["en", "zh", "zh-TW", "ja", "de"]) { + test(`${engine} ${lang}: the sidebar's gateway names the address shared on the local network`, async (t) => { + const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" })); + t.after(() => browser.close()); + const page = await (await browser.newContext({ viewport: { width: 1100, height: 800 }, reducedMotion: "reduce" })).newPage(); + page.setDefaultTimeout(6000); + const errors = []; + page.on("pageerror", (e) => errors.push(e.message)); + // boot.js says the gateway (bootGateway); the providers' read comes + // 1.5s after, so what is read at once is boot.js's line + await page.route("**/*", fixture(lang, "light", [], { lan: true, lanURLs, bootGateway: true })); + const line = () => page.locator("#sideGw span").textContent(); + const tip = () => page.locator("#sideGw").getAttribute("title"); + const says = (host) => page.waitForFunction((h) => document.querySelector("#sideGw span")?.textContent.includes(h), host); + + await page.goto("http://magpie.test/?view=settings"); + await page.locator("#sideGw").waitFor(); + assert.match(await line(), /192\.168\.1\.10:3999/); + assert.doesNotMatch(await line(), /127\.0\.0\.1/); + assert.match(await tip(), /192\.168\.1\.10:3999/); + assert.match(await tip(), /127\.0\.0\.1:3999/); + + // another address picked under Share on local network: the line + // follows at once, and after a reload + await openPart(page, "network"); + await page.locator("#lanList .lan-address-row").waitFor(); + await reach(page, page.locator("#lanList .lan-interface")); + await page.locator("#lanList .lan-interface").click(); + await page.locator(".proto-menu .pm-item", { hasText: "10.0.0.10" }).click(); + await says("10.0.0.10:3999"); + await page.reload(); + await page.locator("#sideGw").waitFor(); + assert.match(await line(), /10\.0\.0\.10:3999/); + + // sharing off: this computer's address, and nothing else in the tooltip + await openPart(page, "network"); + await page.locator("#lanList .lan-address-row").waitFor(); + await page.locator("#lanList .segs").first().locator(".opt").nth(0).click(); + await page.locator("#lanList .lan-address-row").waitFor({ state: "detached" }); + await says("127.0.0.1:3999"); + assert.doesNotMatch(await tip(), /192\.168|10\.0\.0/); + assert.match(await tip(), new RegExp(LOCAL.replace(/\./g, "\\.") + "$")); + // and on again: the address picked comes back + await page.locator("#lanList .segs").first().locator(".opt").nth(1).click(); + await page.locator("#lanList .lan-address-row").waitFor(); + await says("10.0.0.10:3999"); + assert.deepEqual(errors, []); + }); + } + test(`${engine}: not shared, the sidebar's gateway is this computer's`, async (t) => { + const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" })); + t.after(() => browser.close()); + const page = await (await browser.newContext({ viewport: { width: 1100, height: 800 }, reducedMotion: "reduce" })).newPage(); + page.setDefaultTimeout(6000); + const errors = []; + page.on("pageerror", (e) => errors.push(e.message)); + await page.route("**/*", fixture("en", "light", [], { lanURLs, bootGateway: true })); + // past the providers' read, which says the same + const read = page.waitForResponse((r) => r.url().endsWith("/api/providers")); + await page.goto("http://magpie.test/?view=settings"); + await page.locator("#sideGw").waitFor(); + await read; + await page.evaluate(() => new Promise(requestAnimationFrame)); + assert.equal(await page.locator("#sideGw span").textContent(), "Gateway 127.0.0.1:3999"); + assert.equal(await page.locator("#sideGw").getAttribute("title"), "Gateway " + LOCAL); + assert.deepEqual(errors, []); + }); +}