page-chat 0.1.1: opened from another machine (magpie web on a server), the launcher explains the SSH tunnel instead of navigating to a refused 127.0.0.1

This commit is contained in:
yetone
2026-10-29 13:14:24 +08:00
parent 1f56706556
commit 8a9680b4cb
4 changed files with 54 additions and 3 deletions
+10
View File
@@ -155,6 +155,16 @@ test("the launcher page only navigates: no fetch, XHR or socket, scripts from fi
expect(html).not.toMatch(/<script>(?!\s*src)/)
})
test("the launcher page explains the SSH tunnel instead of navigating when magpie web is opened from another machine", () => {
const boot = readFileSync(join(dir, "page", "boot.js"), "utf8")
const tail = boot.slice(boot.indexOf("function isLoopback"))
const { isLoopback } = new Function(tail + "; return { isLoopback }")()
for (const h of ["127.0.0.1", "localhost", "[::1]", "magpie.localhost", ""]) expect(isLoopback(h)).toBe(true)
for (const h of ["192.168.1.20", "magpie.example.com", "[2001:db8::1]"]) expect(isLoopback(h)).toBe(false)
expect(boot).toMatch(/if \(!isLoopback\(location\.hostname\)\)/)
expect(boot).toContain("ssh -N -L ")
})
test("the chat UI sends the token on every API call", () => {
const app = readFileSync(join(dir, "chat", "app.js"), "utf8")
// two calls reach the service: api() with its init, and the chat stream
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/page-chat",
"version": "0.1.0",
"version": "0.1.1",
"description": "在 magpie 窗口里加一个「对话」页面:用 magpie 自己的模型、MCP 服务器、指令和技能聊天,支持图片与 LaTeX。A chat page inside magpie's window: talk with magpie's own models, MCP servers, instructions and skills, with images and LaTeX.",
"author": "Magic_zero (https://github.com/Magiczerowxy)",
"files": [
+39 -1
View File
@@ -51,5 +51,43 @@
}
}
location.replace("http://127.0.0.1:" + port + "/?" + q.toString());
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)。
// 这种情况先说明原因,给出 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);
}
+4 -1
View File
@@ -13,11 +13,14 @@
background: var(--magpie-bg, Canvas); color: var(--magpie-fg, CanvasText);
}
.box { text-align: center; color: var(--magpie-muted, GrayText); }
.box { max-width: 520px; padding: 0 16px; }
.box p { margin: 6px 0; }
.box button { margin-top: 8px; font: inherit; padding: 4px 14px; }
.box b { display: block; color: var(--magpie-fg, CanvasText); font-size: 14px; margin-bottom: 4px; }
</style>
</head>
<body>
<div class="box"><b>对话</b><span id="said">正在连接到本地对话服务…</span></div>
<div class="box"><b>对话</b><div id="said">正在连接到本地对话服务…</div></div>
<script src="_magpie/bridge.js"></script>
<script src="boot.js"></script>
</body>