dsh-plugin-mesh 0.5.15 → 0.5.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/client/client.js CHANGED
@@ -4389,6 +4389,9 @@ window.__ModuleLoader__.load({
4389
4389
  "aria-modal": "true",
4390
4390
  "aria-label": `${name} 的 DSH Web`,
4391
4391
  "data-mesh-view": "web",
4392
+ // 收起来的面板只隐藏、不卸载:卸载会销毁 iframe,对端页面要重新加载,
4393
+ // 会话状态和滚动位置全丢。宿主侧同时保留代理宽限期,两边配套。
4394
+ style: panel.hidden ? { display: "none" } : undefined,
4392
4395
  },
4393
4396
  h(
4394
4397
  "div",
@@ -8355,9 +8358,29 @@ window.__ModuleLoader__.load({
8355
8358
  * 也不受对端 cookie 的 SameSite=Strict 影响。
8356
8359
  * 反代起不来时才退回老的"浏览器页签 / 外部浏览器"那条链。
8357
8360
  */
8358
- const openWeb = async (peer) => {
8361
+ const openWeb = async (peer, opts) => {
8362
+ const force = !!(opts && opts.force);
8359
8363
  const name = (peer && peer.machine) || "对端";
8360
- setWebPanel({ peer, name, state: "loading", reloadKey: 0 });
8364
+ const key = (peer && (peer.machine || peer.id)) || "";
8365
+ const cachedKey =
8366
+ webPanel && webPanel.peer ? webPanel.peer.machine || webPanel.peer.id || "" : "";
8367
+ // 之前收起来的同一台设备:直接显示,不请求宿主、不重建 iframe。
8368
+ // 页面状态、滚动位置、对端会话原样还在(宿主那边代理也在宽限期内)。
8369
+ // 超过宽限期就不再走快路——那时宿主可能已经把代理回收了,直接显示
8370
+ // 只会看到一个连不上的 iframe。
8371
+ const hiddenFor = webPanel && webPanel.hiddenAt ? Date.now() - webPanel.hiddenAt : Infinity;
8372
+ if (!force && webPanel && webPanel.url && cachedKey && cachedKey === key && hiddenFor < 8 * 60 * 1000) {
8373
+ setWebPanel((prev) => ({ ...(prev || { peer, name }), peer, name, hidden: false, hiddenAt: 0, state: "ready" }));
8374
+ return;
8375
+ }
8376
+ // force = 用户点「重新加载面板」:保留同一个代理地址,只让 iframe 重载
8377
+ setWebPanel((prev) => ({
8378
+ peer,
8379
+ name,
8380
+ state: "loading",
8381
+ hidden: false,
8382
+ reloadKey: force && prev && prev.url ? (prev.reloadKey || 0) + 1 : 0,
8383
+ }));
8361
8384
  try {
8362
8385
  const r = await api("/web-panel", { peer: (peer && (peer.machine || peer.id)) || "" });
8363
8386
  if (r && r.ok === true && typeof r.url === "string") {
@@ -8368,7 +8391,8 @@ window.__ModuleLoader__.load({
8368
8391
  remote: r.remote || "",
8369
8392
  hasSession: r.hasSession !== false,
8370
8393
  note: r.note || "",
8371
- reloadKey: 0,
8394
+ hidden: false,
8395
+ reloadKey: (prev && prev.reloadKey) || 0,
8372
8396
  }));
8373
8397
  if (r.hasSession === false) {
8374
8398
  push("warn", `${name} 那边没给出可用 token,面板可能停在登录页。`);
@@ -8437,7 +8461,9 @@ window.__ModuleLoader__.load({
8437
8461
  */
8438
8462
  const closeWebPanel = () => {
8439
8463
  const machine = webPanel && webPanel.peer ? webPanel.peer.machine || webPanel.peer.id || "" : "";
8440
- setWebPanel(null);
8464
+ // 只收起、不卸载:iframe 留着,对端页面的会话状态和滚动位置都还在;
8465
+ // 宿主收到 close 只把它标记成空闲,宽限期内不拆代理和出站流。
8466
+ setWebPanel((prev) => (prev ? { ...prev, hidden: true, hiddenAt: Date.now() } : null));
8441
8467
  if (!machine) return;
8442
8468
  api("/web-panel/close", { peer: machine }).catch(() => {
8443
8469
  /* 关闭是尽力而为 */
@@ -8448,7 +8474,7 @@ window.__ModuleLoader__.load({
8448
8474
  const reloadWebPanel = () => {
8449
8475
  const peer = webPanel && webPanel.peer;
8450
8476
  if (!peer) return;
8451
- void openWeb(peer);
8477
+ void openWeb(peer, { force: true });
8452
8478
  };
8453
8479
 
8454
8480
  /** 派发成功的统一收尾:toast + 让轮询立刻刷新。
package/lib/index.js CHANGED
@@ -108,7 +108,7 @@ import { TsnetNode, channelKey, channelSignature } from "./tsnet/manager.js";
108
108
  import { resolveTailnetChannels } from "./tsnet/channels.js";
109
109
  import { tailnetFetch, tailnetOrigin, isTailnetAddress } from "./tsnet/bridge.js";
110
110
  import { createWebProxy } from "./web-proxy.js";
111
- import { planWebTarget, needsReachabilityProbe } from "./web-target.js";
111
+ import { planWebTarget, needsReachabilityProbe, isLoopbackHost } from "./web-target.js";
112
112
  import { planSshTarget, sshDirectCandidates } from "./ssh-target.js";
113
113
  import { createSecretReader } from "./desktop-auth.js";
114
114
  import {
@@ -129,7 +129,7 @@ import {
129
129
  export const name = "dsh-plugin-mesh";
130
130
  export const inject = ["tools", "webServer"];
131
131
 
132
- const VERSION = "0.5.15";
132
+ const VERSION = "0.5.17";
133
133
  // 诊断用:进程启动时间 + 入口真实路径。磁盘上的版本**不在这里缓存** ——
134
134
  // 升级之后进程还跑旧代码,版本号必须现读磁盘才能立刻反映出来(真踩过:
135
135
  // 磁盘升到 0.5.1,进程还在 0.5.0,界面显示 0.5.0)。
@@ -4868,6 +4868,16 @@ function registerUiRoutes(ctx, { discovery, config, identity, inbound, outbound,
4868
4868
  void closeWebPanels();
4869
4869
  });
4870
4870
 
4871
+ // 空闲面板的定期回收:宽限期到了才真的关代理、收回 tailnet 出站流。
4872
+ // 60 秒扫一次足够——宽限期是分钟级的,不需要更细。
4873
+ ctx.effect(() => {
4874
+ const timer = setInterval(() => {
4875
+ void sweepWebPanels();
4876
+ }, 60 * 1000);
4877
+ if (typeof timer.unref === "function") timer.unref();
4878
+ return () => clearInterval(timer);
4879
+ });
4880
+
4871
4881
  /** 取对端的 Web 地址与启动 token;老版本对端只能退回按约定拼地址。 */
4872
4882
  /**
4873
4883
  * 取对端的 Web 地址与启动 token。
@@ -4953,11 +4963,35 @@ function registerUiRoutes(ctx, { discovery, config, identity, inbound, outbound,
4953
4963
  probeNote: direct.note,
4954
4964
  });
4955
4965
  if (plan.mode === "stream") {
4966
+ const passthrough = Boolean(plan.ticket);
4967
+ const upstreamHeaders = passthrough
4968
+ ? { [plan.header || PEER_WEB_TICKET_HEADER]: plan.ticket, "x-dsh-mesh-token": currentSecret() }
4969
+ : {};
4970
+ // 快路:对端除了相对前缀,还把隧道的**局域网绝对地址**(tunnel.url)给了。
4971
+ // 同网段时直接打那个地址:sidecar 拨 tailnet 要多一跳,遇到 DERP 中继还会
4972
+ // 明显变慢——页面所有请求都吃这个延迟。拨不通再回落到 tailnet 出站流。
4973
+ const lanBase = lanTunnelBase(tunnel);
4974
+ if (lanBase) {
4975
+ const probe = await verifyWebBase(lanBase);
4976
+ if (probe.reachable !== false) {
4977
+ return {
4978
+ remoteBase: lanBase,
4979
+ pathPrefix: plan.prefix || "",
4980
+ passthrough,
4981
+ lanReachable: true,
4982
+ token,
4983
+ guiPort,
4984
+ guiBind,
4985
+ stream: null,
4986
+ upstreamHeaders,
4987
+ note: `经对端局域网 mesh 口隧道(${lanBase}${String(plan.prefix || "").replace(/^\//, "")})`,
4988
+ };
4989
+ }
4990
+ }
4956
4991
  const opened = await openWebStream(target, plan.address);
4957
4992
  if (opened.error) {
4958
4993
  return { error: `${plan.note},但${opened.error}` };
4959
4994
  }
4960
- const passthrough = Boolean(plan.ticket);
4961
4995
  return {
4962
4996
  remoteBase: `http://127.0.0.1:${opened.port}/`,
4963
4997
  pathPrefix: plan.prefix || "",
@@ -4967,9 +5001,7 @@ function registerUiRoutes(ctx, { discovery, config, identity, inbound, outbound,
4967
5001
  guiPort,
4968
5002
  guiBind,
4969
5003
  stream: { address: plan.address, node: opened.node },
4970
- upstreamHeaders: passthrough
4971
- ? { [plan.header || PEER_WEB_TICKET_HEADER]: plan.ticket, "x-dsh-mesh-token": currentSecret() }
4972
- : {},
5004
+ upstreamHeaders,
4973
5005
  note: plan.note,
4974
5006
  };
4975
5007
  }
@@ -5007,6 +5039,24 @@ function registerUiRoutes(ctx, { discovery, config, identity, inbound, outbound,
5007
5039
  }
5008
5040
  }
5009
5041
 
5042
+ /**
5043
+ * 对端给的局域网绝对隧道地址(`tunnel.url`,形如 `http://192.168.1.5:45930/mesh/web/`)。
5044
+ *
5045
+ * 只取 `scheme://host:port/` 这一截:路径前缀由 pathPrefix 单独拼,避免拼两次。
5046
+ * 回环地址一律不用——那是"对端自己"的地址,拨过去只会打到本机。
5047
+ */
5048
+ function lanTunnelBase(tunnel) {
5049
+ try {
5050
+ const raw = String(tunnel?.url ?? "").trim();
5051
+ if (!raw) return "";
5052
+ const u = new URL(raw);
5053
+ if (!u.hostname || isLoopbackHost(u.hostname)) return "";
5054
+ return `${u.protocol}//${u.host}/`;
5055
+ } catch {
5056
+ return "";
5057
+ }
5058
+ }
5059
+
5010
5060
  /** 在对端名片的 tailnet 端点(100.64/10)上开会话出口。 */
5011
5061
  async function openWebStream(target, address) {
5012
5062
  const ep = orderedEndpoints(target).find((item) => item?.transport === "tsnet" && item.host);
@@ -5056,6 +5106,35 @@ function registerUiRoutes(ctx, { discovery, config, identity, inbound, outbound,
5056
5106
  return 0;
5057
5107
  }
5058
5108
 
5109
+ /**
5110
+ * 面板关掉后代理与出站流**保留多久**。
5111
+ *
5112
+ * 为什么不是立刻销毁:关掉面板/切走再回来只隔几秒,重新解析目标 + 重新拨
5113
+ * tailnet 出站流 + 重新握手,代价是好几秒(移动网络下更久),页面还要重新
5114
+ * 渲染一遍。留一段宽限期,期间再打开直接复用同一个代理和端口,页面只是
5115
+ * iframe 重新加载,会话 cookie 也还在。
5116
+ */
5117
+ const WEB_PANEL_IDLE_MS = (() => {
5118
+ const raw = Number(process.env.DSH_MESH_WEBPANEL_IDLE_MS);
5119
+ return Number.isFinite(raw) && raw >= 0 ? raw : 10 * 60 * 1000;
5120
+ })();
5121
+
5122
+ /** 关掉超时的面板代理(保留期内不动)。 */
5123
+ async function sweepWebPanels() {
5124
+ const now = Date.now();
5125
+ for (const [machine, entry] of [...webPanels.entries()]) {
5126
+ if (now - (entry.lastUsedAt ?? now) < WEB_PANEL_IDLE_MS) continue;
5127
+ webPanels.delete(machine);
5128
+ try {
5129
+ await entry.proxy.close();
5130
+ } catch (err) {
5131
+ warn(`回收超时 Web 面板代理失败:${err?.message ?? err}`);
5132
+ }
5133
+ await closePanelStream(entry);
5134
+ log(`Web 面板代理已回收(空闲超过 ${Math.round(WEB_PANEL_IDLE_MS / 1000)}s):${machine}`);
5135
+ }
5136
+ }
5137
+
5059
5138
  post("/mesh/api/web-panel", async (req, res) => {
5060
5139
  const body = await readJson(req);
5061
5140
  const found = findPeer(String(body?.peer ?? "").trim());
@@ -5063,6 +5142,22 @@ function registerUiRoutes(ctx, { discovery, config, identity, inbound, outbound,
5063
5142
  const target = found.peer;
5064
5143
  const machine = target.machine || String(body?.peer ?? "");
5065
5144
 
5145
+ // 快速复用:宽限期内再次打开,连目标都不重新解析 —— 解析本身就要打一次
5146
+ // 对端 /mesh/v1/web-url,出站流重建更贵。这里是"秒开"的关键。
5147
+ const reused = webPanels.get(machine);
5148
+ if (reused && Date.now() - (reused.lastUsedAt ?? 0) < WEB_PANEL_IDLE_MS) {
5149
+ reused.lastUsedAt = Date.now();
5150
+ return send(res, 200, {
5151
+ ok: true,
5152
+ url: reused.proxy.url,
5153
+ remote: reused.proxy.remote,
5154
+ hasSession: reused.proxy.hasSession(),
5155
+ note: "复用上一次的面板代理(未重新建连)",
5156
+ lanReachable: true,
5157
+ reused: true,
5158
+ });
5159
+ }
5160
+
5066
5161
  const resolved = await resolveWebTarget(
5067
5162
  target,
5068
5163
  typeof body?.token === "string" ? body.token.trim() : "",
@@ -5110,7 +5205,7 @@ function registerUiRoutes(ctx, { discovery, config, identity, inbound, outbound,
5110
5205
  log,
5111
5206
  warn,
5112
5207
  });
5113
- webPanels.set(machine, { proxy, remote: resolved.remoteBase, signature, stream: resolved.stream });
5208
+ webPanels.set(machine, { proxy, remote: resolved.remoteBase, signature, stream: resolved.stream, lastUsedAt: Date.now() });
5114
5209
  return send(res, 200, {
5115
5210
  ok: true,
5116
5211
  url: proxy.url,
@@ -5128,7 +5223,11 @@ function registerUiRoutes(ctx, { discovery, config, identity, inbound, outbound,
5128
5223
  const body = await readJson(req);
5129
5224
  const machine = String(body?.peer ?? "").trim();
5130
5225
  const entry = webPanels.get(machine);
5131
- if (entry) {
5226
+ if (!entry) return send(res, 200, { ok: true });
5227
+ // 关面板 ≠ 立刻拆连接:留一段宽限期,短时间再打开直接复用(秒开,
5228
+ // 会话 cookie 也还在)。真正的回收交给 sweepWebPanels 按空闲时长做。
5229
+ // 需要立刻释放的调用方显式传 force:true。
5230
+ if (body?.force === true) {
5132
5231
  webPanels.delete(machine);
5133
5232
  try {
5134
5233
  await entry.proxy.close();
@@ -5136,8 +5235,15 @@ function registerUiRoutes(ctx, { discovery, config, identity, inbound, outbound,
5136
5235
  warn(`关闭 Web 面板代理失败:${err?.message ?? err}`);
5137
5236
  }
5138
5237
  await closePanelStream(entry);
5238
+ return send(res, 200, { ok: true, closed: true });
5139
5239
  }
5140
- return send(res, 200, { ok: true });
5240
+ entry.lastUsedAt = Date.now();
5241
+ entry.idle = true;
5242
+ return send(res, 200, {
5243
+ ok: true,
5244
+ kept: true,
5245
+ idleMs: WEB_PANEL_IDLE_MS,
5246
+ });
5141
5247
  });
5142
5248
 
5143
5249
  /** 关掉面板时顺手收回它用的 tailnet 出站流(端口不回收会一直挂着)。 */