gateway: HTTPS for other computers, from magpie's CA or an imported certificate (zhangly1 on X)

zhangly1 on X: Claude on another computer on the LAN needed magpie at
https://. Settings → Network and sharing → HTTPS (magpie https on)
serves other computers TLS on the gateway's own port, with a
certificate magpie's CA (the mTLS one, in ~/.config/magpie/mtls) signs
for this computer's names and addresses; this computer keeps plain
http://. The switch is read per connection, so no restart.

- Certificate row: Import… takes the user's own PEM certificate (with
  chain) and unencrypted key, in any files and order; a pair that can't
  be served (no key, no cert, a stranger key, a passphrase, run out,
  nothing PEM) is refused with the reason in the page's language and
  leaves nothing behind. Remove goes back to magpie's CA. --tls-cert
  still comes first.
- magpie's CA row: SHA-256 shown, Save puts magpie-ca.crt in Downloads;
  the gateway serves it without a key at /magpie-ca.crt to any address
  the share lets in. Rows under it give curl -kO, macOS, Windows, Linux
  and NODE_EXTRA_CA_CERTS steps with Copy.
- A page open from another computer can't turn HTTPS on or off under
  itself (it would lose its own address).
- magpie https status|on|off|import|remove|ca --out; the serve banner
  names magpie's CA or the imported file instead of "(mTLS)".
- docs/reference.md and gateway-routing.md say all of it, and that
  Windows' curl.exe needs --ssl-no-revoke for any private CA.

Run:
- gofmt -l clean; go vet linux -tags nogui ./internal/..., GOOS=windows
  and GOOS=darwin -tags nogui vet of the touched packages (WSL, mote
  refused ssh all evening).
- WSL: go test -tags nogui gateway -run HTTPS|MTLS|ClientCert|CAFile|TLS|LAN
  ok; gui -run HTTPS|MTLS|Keep ok; settings, settingsapi, mtls ok;
  cli -run Help|HTTPS|TLS|Serve|Banner|Flag|Unknown ok. The whole cli
  package hit its 40m timeout in TestEveryAgentFieldNameWritesNothing
  (#5234, #4247, not this change).
- GUI (WSL Playwright, chromium + webkit): https-settings (en, zh, 390px,
  strings in all four languages) ok, plus mtls-settings, allow-ips,
  browser-access, cors-origins, gui-de/ja/zh-tw, i18n-keys,
  i18n-en-no-cjk, lan-*, public-url*, api-key-theme,
  connect-address-kept, gateway-connect-keys, gateway-caller-keys,
  side-gateway-lan, menu-scroll. Reds at load ~130: api-key-theme
  (#5044), connect-address-kept (#5972), gateway-caller-keys (#2572),
  gui-ja (#5943), lan-docker (#4692), all red the same at base
  10bef5fe0 run beside it; allow-ips and gateway-connect-keys went red
  once under that load and pass 18/18 at concurrency 1, base and this
  change side by side.
- Real: a sandbox HOME gateway built from this tree on WSL
  (MAGPIE_ADDR=0.0.0.0:3593, a gateway key, magpie https on, magpie
  serve). From the Windows host: curl -k /magpie-ca.crt 200 without a
  key; Node fetch with NODE_EXTRA_CA_CERTS=magpie-ca.crt 200 on
  /v1/models, 401 without the key, SELF_SIGNED_CERT_IN_CHAIN without
  the CA; plain http:// 400; curl.exe --cacert needs --ssl-no-revoke
  (schannel), then 200. Cert SANs: localhost, the hostname, .local,
  127.0.0.1, ::1 and every address. magpie https import of an openssl
  CA's leaf on the running gateway: Node trusting that CA 200, magpie's
  CA alone refused. remove + off on the running gateway: plain http://
  from Windows 200. This computer's plain http:// 200 throughout.
- At the pushed tree (on origin/main ee818e8bc), WSL:
  gofmt -l clean; go vet -tags nogui linux, GOOS=windows, GOOS=darwin
  nogui of gateway, gui, cli ok; go test -run 'HTTPS|CAFile|MTLS|LAN'
  gateway, gui ok. GUI https-settings, mtls-settings, i18n-keys,
  gui-de/ja/zh-tw, allow-ips, lan-* (both engines): 147/155; reds are
  gui-ja (#5943), lan-docker (#4692), allow-ips webkit under load, and
  gui-zh-tw chromium under load (2 of 3 green alone, filed #5983).

Broken by hand:
- TLSOn without HTTPSOn: TestHTTPSOtherComputer red.
- lanGuard without the caFileOpen exemption: TestHTTPSCAFile red.
- mtls config without GetCertificate = settingsCert: all three
  serving tests red.
- app.js without renderHTTPS(s): https-settings red in both engines.

Not tried: the macOS, Windows and Linux system trust commands on a real
store (they change the box's trust); Claude Desktop itself; a darwin
GUI run (mote unreachable); the full suite before the tag (started
after it).
This commit is contained in:
yetone
2026-11-10 23:05:47 +08:00
parent 6c3f113cee
commit 4623f54190
26 changed files with 1383 additions and 12 deletions
+244
View File
@@ -0,0 +1,244 @@
// Run with Node's test runner and Playwright on the module path; see README.md.
// HTTPS for other computers (zhangly1 on X: Claude on another computer on
// the LAN needed magpie at https://): Settings › Network, under Require a
// client certificate, turns HTTPS on; the Certificate row says which is
// served and imports the user's own PEM files (or removes them); magpie's
// CA row shows its SHA-256 and saves it to Downloads, the path said; the
// rows under it give the download and each system's trust command with a
// Copy button. A refused import or a far page's switch is told in the
// page's language. Hidden while magpie answers only this computer.
// English and Chinese, Chromium and WebKit, 390px; the strings in all
// four languages; no backend, the API is faked here.
const assert = require("node:assert/strict");
const fs = require("node:fs/promises");
const path = require("node:path");
const { test } = require("node:test");
const { chromium, webkit } = require("playwright");
const { watchSettingsReads, settingsDrawn, reach } = require("./reader.cjs");
const assets = path.resolve(__dirname, "../assets");
const file = async (route, url) => {
const f = path.join(assets, url.pathname === "/" ? "index.html" : url.pathname);
const contentType = { ".html": "text/html", ".js": "text/javascript", ".css": "text/css", ".svg": "image/svg+xml", ".png": "image/png" }[path.extname(f)];
await route.fulfill({ body: await fs.readFile(f), contentType });
};
const boot = (route, lang) => route.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${lang}",theme:"light",web:true};` });
const FP = Array.from({ length: 32 }, (_, i) => (i * 7 + 16).toString(16).toUpperCase().padStart(2, "0")).join(":");
const FAR = "this page is open from another computer, and HTTPS changes how it is reached: turn it on or off from the computer magpie runs on";
const NOKEY = "a certificate but no private key: give the key's file too";
function server(lang, posts, over = {}) {
const s = {
theme: "light", lang, tray: "panel", currency: "usd", version: "0.1.400", dir: "/config/magpie",
gateway: "http://127.0.0.1:3425", proxyNow: "none", proxySource: "none", login: false,
visionModels: [], imageGenModels: [], allowIPs: [],
lan: true, lanURLs: ["http://192.168.1.20:3425"], mtls: false, mtlsCerts: [],
fx: { rate: 7.2, at: new Date().toISOString(), stale: false }, ...over.settings,
};
let h = { on: false, served: false };
const served = (from) => ({
on: true, served: true,
cert: { from, names: from === "magpie" ? ["mac.local", "192.168.1.20"] : ["nas.home"], issuer: from === "magpie" ? "magpie CA" : "Test Root", until: "2027-11-10T00:00:00Z" },
...(from === "magpie" ? { ca: FP, caURLs: ["https://192.168.1.20:3425/magpie-ca.crt"] } : {}),
});
return async (route) => {
const url = new URL(route.request().url());
const json = (data, status = 200) => route.fulfill({ json: data, status });
if (url.pathname === "/boot.js") return boot(route, lang);
if (url.pathname === "/wails/runtime.js") return route.fulfill({ contentType: "text/javascript", body: "export const Window = {};" });
if (url.pathname === "/api/state") return json({ agents: [], profiles: [], settings: { lang, theme: "light" }, fx: s.fx });
if (url.pathname === "/api/settings") return json(s);
if (url.pathname === "/api/settings/https" && route.request().method() === "GET") return json(h);
if (url.pathname.startsWith("/api/settings/https")) {
const body = route.request().postDataJSON();
posts.push({ path: url.pathname, body });
if (url.pathname === "/api/settings/https") {
if (over.farPage) return json({ error: FAR }, 400);
h = body.on ? served("magpie") : { on: false, served: false };
} else if (url.pathname.endsWith("/import")) {
if (body.files.length < 2) return json({ error: NOKEY }, 400);
h = served("imported");
} else if (url.pathname.endsWith("/remove")) {
h = served("magpie");
} else if (url.pathname.endsWith("/ca")) {
return json({ ...h, saved: "~/Downloads/magpie-ca.crt" });
}
return json(h);
}
if (url.pathname === "/api/copy") { posts.push({ path: url.pathname, body: route.request().postDataJSON() }); return json({}); }
if (url.pathname === "/api/plugins") return json({ plugins: [] });
if (url.pathname === "/api/usage/quotas") return json([]);
if (url.pathname === "/api/groups") return json({ groups: [], models: [] });
if (url.pathname.startsWith("/api/")) return json({});
await file(route, url);
};
}
const en = {
sub: "Other computers call magpie at https://; this computer can still use http://",
mine: "Your certificate",
signed: "Signed by magpie’s CA",
ca: "magpie’s CA",
node: "Claude Code and other Node apps",
far: "This page is open from another computer: turn HTTPS on or off from the computer magpie runs on",
nokey: "A certificate but no private key: choose the key’s file too",
};
const zh = { sub: "其他电脑通过 https:// 访问 magpie;本机仍可用 http://", ca: "magpie 的 CA" };
const others = [
"Already served over https:// by Require a client certificate or --tls-cert",
"Trust it once on each computer that calls magpie · SHA-256 {fp}",
"A private key but no certificate: choose the certificate’s file too",
"The private key belongs to none of the certificates",
"The private key has a passphrase: choose one without",
"No PEM certificate or key in the files chosen",
];
async function open(browser, lang, posts, over) {
const page = await (await browser.newContext({ viewport: { width: 390, height: 640 }, reducedMotion: "reduce" })).newPage();
page.setDefaultTimeout(5000);
page.setDefaultNavigationTimeout(60000);
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", server(lang, posts, over));
await watchSettingsReads(page);
await page.goto("http://127.0.0.1:4999/?view=settings&tab=network");
await settingsDrawn(page);
return { page, errors };
}
async function fits(box) {
const bad = await box.evaluate((b) => {
const out = [];
const right = b.getBoundingClientRect().right + 0.5;
for (const e of b.querySelectorAll(".row, button, .name, code")) {
const r = e.getBoundingClientRect();
if (r.right > right || r.width === 0) out.push(e.className + " " + r.right + ">" + right);
if (e.matches("button, .name") && e.scrollWidth > e.clientWidth + 1) out.push("cut: " + e.textContent);
}
if (document.documentElement.scrollWidth > innerWidth) out.push("page scrolls sideways");
return out;
});
assert.deepEqual(bad, []);
}
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
test(engine + ": HTTPS in Settings (zhangly1 on X)", async (t) => {
assert(["chromium", "webkit"].includes(engine), "BROWSER must be chromium or webkit");
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
await t.test("every language has the strings", async () => {
const { page } = await open(browser, "en", []);
for (const lang of ["zh", "zh-TW", "ja", "de"]) {
const missing = await page.evaluate(([lang, keys]) => keys.filter((k) => !I18N[lang]?.[k] || I18N[lang][k] === k),
[lang, [...Object.values(en), ...others]]);
assert.deepEqual(missing, [], lang);
}
await page.context().close();
});
await t.test("hidden while magpie answers only this computer", async () => {
const { page, errors } = await open(browser, "en", [], { settings: { lan: false, lanURLs: [] } });
await page.locator("#lanList .row").first().waitFor();
await page.waitForTimeout(200);
assert.equal(await page.locator("#httpsList .row").count(), 0);
assert.deepEqual(errors, []);
await page.context().close();
});
for (const lang of ["en", "zh"]) {
await t.test(lang + ": on, the CA and its steps, import, remove, off", async () => {
const posts = [];
const { page, errors } = await open(browser, lang, posts);
const w = {};
for (const [k, s] of Object.entries(en)) {
w[k] = await page.evaluate((s) => t(s), s);
if (lang !== "en") assert.notEqual(w[k], s, `"${s}" has no ${lang} translation`);
if (lang === "zh" && zh[k]) assert.equal(w[k], zh[k]);
}
const box = page.locator("#httpsList");
const toggle = box.locator(".https-row");
await toggle.waitFor();
assert.equal((await toggle.locator(".name").textContent()).trim(), "HTTPS");
assert.equal((await toggle.locator(".sub").textContent()).trim(), w.sub);
assert.equal(await box.locator(".https-cert").count(), 0, "no certificate row while off");
await fits(box);
const on = toggle.locator("button.opt").last();
await reach(page, on);
await on.click();
await box.locator(".https-ca").waitFor();
assert.deepEqual(posts.at(-1), { path: "/api/settings/https", body: { on: true } });
const certSub = (await box.locator(".https-cert .sub").textContent()).trim();
assert.ok(certSub.startsWith(w.signed + " · mac.local, 192.168.1.20 · "), certSub);
assert.ok((await box.locator(".https-ca .sub").textContent()).includes("SHA-256 " + FP));
const steps = await box.locator(".https-step").evaluateAll((rs) => rs.map((r) => [r.querySelector(".name").textContent, r.querySelector("code").textContent]));
assert.deepEqual(steps.map((s) => s[0]).slice(1), ["macOS", "Windows", "Linux", w.node]);
assert.equal(steps[0][1], "curl -kO https://192.168.1.20:3425/magpie-ca.crt");
assert.match(steps[1][1], /add-trusted-cert .* magpie-ca\.crt$/);
assert.equal(steps[2][1], "certutil -addstore -f Root magpie-ca.crt");
await fits(box);
// Copy sends the command itself
const copy = box.locator(".https-step").nth(2).locator("button.copy");
await reach(page, copy);
await copy.click();
await page.waitForFunction(() => document.querySelector("#status")?.textContent);
assert.deepEqual(posts.at(-1), { path: "/api/copy", body: { text: "certutil -addstore -f Root magpie-ca.crt" } });
const save = box.locator(".https-ca button");
await reach(page, save);
await save.click();
const savedSays = await page.evaluate(() => t("Saved to {path}", { path: "~/Downloads/magpie-ca.crt" }));
await page.waitForFunction((s) => document.querySelector("#status")?.textContent.includes(s), savedSays);
assert.deepEqual(posts.at(-1), { path: "/api/settings/https/ca", body: {} });
// a certificate without its key is refused, in the page's language
const input = box.locator(".https-cert input[type=file]");
await input.setInputFiles([{ name: "cert.pem", mimeType: "application/x-pem-file", buffer: Buffer.from("-----BEGIN CERTIFICATE-----\nAA==\n-----END CERTIFICATE-----\n") }]);
await page.waitForFunction((s) => document.querySelector("#status")?.textContent.includes(s), w.nokey);
assert.equal(posts.at(-1).path, "/api/settings/https/import");
await input.setInputFiles([
{ name: "cert.pem", mimeType: "application/x-pem-file", buffer: Buffer.from("CERT") },
{ name: "key.pem", mimeType: "application/x-pem-file", buffer: Buffer.from("KEY") },
]);
await box.locator(".https-cert .sub", { hasText: w.mine }).waitFor();
assert.deepEqual(posts.at(-1), { path: "/api/settings/https/import", body: { files: ["CERT", "KEY"] } });
assert.equal(await box.locator(".https-ca").count(), 0, "no CA row with the user's own certificate");
const remove = box.locator(".https-cert button").last();
await reach(page, remove);
await remove.click();
await box.locator(".https-ca").waitFor();
assert.deepEqual(posts.at(-1), { path: "/api/settings/https/remove", body: {} });
const off = box.locator(".https-row button.opt").first();
await reach(page, off);
await off.click();
await page.waitForFunction(() => !document.querySelector("#httpsList .https-cert"));
assert.deepEqual(posts.at(-1), { path: "/api/settings/https", body: { on: false } });
const border = await box.locator(".row").first().evaluate((r) => getComputedStyle(r).borderLeftWidth);
assert(border === "0px" || border === "", "no left border accent: " + border);
assert.equal(await box.locator("select").count(), 0);
assert.deepEqual(errors, []);
await page.context().close();
});
}
await t.test("a page on another computer is told why it can't, in its language", async () => {
const posts = [];
const { page, errors } = await open(browser, "zh", posts, { farPage: true });
const toggle = page.locator("#httpsList .https-row");
await toggle.waitFor();
const on = toggle.locator("button.opt").last();
await reach(page, on);
await on.click();
const says = await page.evaluate((s) => t(s), en.far);
assert.notEqual(says, en.far);
await page.waitForFunction((s) => document.querySelector("#status")?.textContent.includes(s), says);
await page.waitForFunction(() => document.querySelector("#httpsList .https-row .opt")?.classList.contains("on"));
assert.deepEqual(errors, []);
await page.context().close();
});
});
}