aigcfree (Discord DM) runs `magpie web --addr 0.0.0.0:3430` on an Ubuntu
server behind nginx, and opens it from a Windows PC. The chat tab showed
"127.0.0.1 refused to connect": boot.js sent the frame to 127.0.0.1, the
viewer's own PC. 0.1.1 (8a9680b) explains an SSH tunnel instead. With a
magpie that proxies a page's service, no tunnel is needed:
- package.json declares magpie.page.service {port: 3477, option: "port"},
the same port index.mjs listens on. magpie web then proxies the service
under the page's token (/_magpie/plugin/<token>/_magpie/service/) and the
bridge hands that address to the page as magpie.service.
- boot.js goes to magpie.service when it is an http(s) address. Without
one (the desktop window, or an older magpie) it behaves as 0.1.1: on
loopback it goes to 127.0.0.1, from another machine it explains the
tunnel.
- The chat client asks api/... by relative address (app.js, index.html,
library.mjs's icon URLs), so it works under the proxied path. Served
from 127.0.0.1:<port>/ these resolve as before.
The service still listens on 127.0.0.1 only, and still checks Host,
Origin and its token. magpie's proxy sets Host to 127.0.0.1:<port>.
Run: bun test packages/chat (16 pass) and bun scripts/check.mjs on mote.
Real: a sandbox magpie web on mote (0.0.0.0, LAN), this package installed
from a folder, a fake gateway. Playwright on the Windows box opened the
chat tab over the LAN in Chromium and WebKit, sent a message, and got the
streamed answer; the fake gateway logged both requests.
Broken by hand: the service branch in boot.js off, 2 tests red.
Not tried: behind a real nginx; the desktop window with this version (the
fallback is the 0.1.1 path, covered by the launcher tests).
111 lines
4.9 KiB
JavaScript
111 lines
4.9 KiB
JavaScript
// 带路页:把它自己(也就是 magpie 页面那个框架)导航到插件的服务。
|
||
//
|
||
// 这里必须"直接跳",不能先探测:
|
||
// 页面的 CSP 是 connect-src 'none',fetch/XHR/WebSocket 全被禁止 ——
|
||
// 连 no-cors 的请求都会被直接拒绝。所以「先探一下服务在不在」这条路走不通,
|
||
// alive() 永远返回 false,页面会永远停在等待页。
|
||
// magpie 文档也明说了:这个页面唯一能做的联网动作就是把自己跳转到别处。
|
||
//
|
||
// 端口用插件选项里的 port(默认 3477),服务端(index.mjs)读的是同一个值。
|
||
//
|
||
// 跳到哪:magpie 把页面给浏览器时(`magpie web`,可能在另一台电脑上打开),
|
||
// 127.0.0.1 是看页面那台电脑自己,不是跑服务的机器。这时 bridge 会给出
|
||
// magpie.service:magpie web 在它自己的地址上替本服务转发的路径(package.json
|
||
// 的 magpie.page.service 声明了它)。有就跳那里;没有(桌面窗口,或旧版
|
||
// magpie)就照旧跳 127.0.0.1。
|
||
//
|
||
// 顺手把窗口的主题和设计令牌一起带上(magpie 的 bridge 已经把它们算好了),
|
||
// 这样对话页的配色和字体跟窗口完全一致,连用户的自定义主题也算在内。
|
||
(async () => {
|
||
const DEFAULT_PORT = 3477;
|
||
let port = DEFAULT_PORT;
|
||
let theme = "";
|
||
let tokens = null;
|
||
let service = "";
|
||
|
||
try {
|
||
const t = await window.magpie?.ready; // { dark, lang, tokens }
|
||
if (t) {
|
||
if (typeof t.dark === "boolean") theme = t.dark ? "dark" : "light";
|
||
if (t.tokens && typeof t.tokens === "object") tokens = t.tokens;
|
||
}
|
||
} catch { /* 没有 bridge 就退回默认主题 */ }
|
||
try {
|
||
const s = window.magpie?.service;
|
||
if (typeof s === "string" && /^https?:\/\//.test(s)) service = s;
|
||
} catch { /* 旧版 magpie 没有 service */ }
|
||
|
||
try {
|
||
const o = await window.magpie?.options?.get?.();
|
||
if (o && Number.isFinite(Number(o.port)) && Number(o.port) > 0) port = Number(o.port);
|
||
} catch { /* 没有 options 就用默认端口 */ }
|
||
|
||
// 字体不在这里传。
|
||
//
|
||
// 试过从 magpie 页面的 CSS 变量里读 --ui-font 再带过去,但时机靠不住:
|
||
// magpie 是设置加载完之后才把 --ui-font 写到 :root 上的,这里 await 完
|
||
// magpie.ready 立刻读,经常读到空。而且页面 CSP 是 connect-src 'none',
|
||
// 读不到也没法重试。现在改成服务端直接读 magpie 的 settings.json,
|
||
// 由 /api/fonts.css 提供给对话页 —— 那边什么时候读都准。
|
||
|
||
const q = new URLSearchParams();
|
||
if (theme) q.set("theme", theme);
|
||
if (true) {
|
||
// 只留非空的令牌,压成一个短串;令牌本身就是 magpie 自己的 CSS 变量值。
|
||
const slim = {};
|
||
for (const [k, v] of Object.entries(tokens || {})) {
|
||
const s = String(v ?? "").trim();
|
||
if (s) slim[k] = s;
|
||
}
|
||
if (Object.keys(slim).length) {
|
||
try { q.set("tokens", btoa(unescape(encodeURIComponent(JSON.stringify(slim))))); } catch { /* 编码不了就只带主题 */ }
|
||
}
|
||
}
|
||
|
||
// magpie 给了 service 就走它:这是 magpie web 在自己地址上的转发,远程也能用。
|
||
if (service) {
|
||
location.replace(service + "?" + q.toString());
|
||
return;
|
||
}
|
||
|
||
const target = "http://127.0.0.1:" + port + "/?" + q.toString();
|
||
|
||
// 服务只听本机的 127.0.0.1,跳过去的是「打开这个页面的浏览器」所在的机器。
|
||
// magpie web 部署在服务器上、从别的电脑打开时,那台电脑的 127.0.0.1 上没有服务,
|
||
// 直接跳只会得到「127.0.0.1 拒绝连接」(Discord DM: AIGCFREE,magpie web + nginx)。
|
||
// 旧版 magpie 不给 service 时,先说明原因,给出 SSH 端口转发的做法,转好了再点「继续」。
|
||
if (!isLoopback(location.hostname)) {
|
||
showRemote(port, target);
|
||
return;
|
||
}
|
||
location.replace(target);
|
||
})();
|
||
|
||
function isLoopback(host) {
|
||
const h = String(host || "").toLowerCase().replace(/^\[|\]$/g, "");
|
||
return h === "" || h === "localhost" || h.endsWith(".localhost") || h === "::1" || /^127\./.test(h);
|
||
}
|
||
|
||
function showRemote(port, target) {
|
||
const said = document.getElementById("said");
|
||
if (!said) return;
|
||
const host = location.hostname.replace(/^\[|\]$/g, "");
|
||
said.textContent = "";
|
||
const p = (text) => {
|
||
const el = document.createElement("p");
|
||
el.textContent = text;
|
||
said.appendChild(el);
|
||
return el;
|
||
};
|
||
p("对话页的服务只在运行 magpie 的那台机器上监听 127.0.0.1:" + port + ",从这台电脑的浏览器连不到它。");
|
||
p("在这台电脑上开一个 SSH 端口转发,然后点「继续」:");
|
||
const code = p("ssh -N -L " + port + ":127.0.0.1:" + port + " <用户名>@" + host);
|
||
code.style.fontFamily = "ui-monospace, Menlo, Consolas, monospace";
|
||
code.style.userSelect = "all";
|
||
const go = document.createElement("button");
|
||
go.type = "button";
|
||
go.textContent = "继续";
|
||
go.addEventListener("click", () => location.replace(target));
|
||
said.appendChild(go);
|
||
}
|