dsh-plugin-mesh 0.5.11 → 0.5.13
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/README.en.md +6 -5
- package/README.md +6 -4
- package/client/client.js +1 -1
- package/cordis.patch.yml +0 -6
- package/lib/_selftest-approval.mjs +22 -16
- package/lib/_selftest-peer-web.mjs +317 -0
- package/lib/approval.js +9 -7
- package/lib/discovery.js +41 -1
- package/lib/index.js +542 -19
- package/lib/index.js.ws.bak +4589 -0
- package/lib/peer-web.js +226 -0
- package/lib/web-peers.js +187 -0
- package/lib/web-proxy.js +57 -6
- package/llms.txt +29 -3
- package/package.json +5 -4
package/lib/index.js
CHANGED
|
@@ -48,6 +48,7 @@ import { createUserMessage } from "@deepseek-ai/dsh-llm";
|
|
|
48
48
|
*/
|
|
49
49
|
const brandString = (value) => value;
|
|
50
50
|
import { MeshDiscovery, preferredAddress, lanIPv4Addresses, macForAddress, orderedEndpoints } from "./discovery.js";
|
|
51
|
+
import { discoverWebPeers, tunnelUrl } from "./web-peers.js";
|
|
51
52
|
import { candidateAddresses, scanForPeers } from "./scan.js";
|
|
52
53
|
import { machineFingerprint } from "./identity.js";
|
|
53
54
|
import {
|
|
@@ -107,6 +108,13 @@ import { resolveTailnetChannels } from "./tsnet/channels.js";
|
|
|
107
108
|
import { tailnetFetch, tailnetOrigin } from "./tsnet/bridge.js";
|
|
108
109
|
import { createWebProxy } from "./web-proxy.js";
|
|
109
110
|
import { createSecretReader } from "./desktop-auth.js";
|
|
111
|
+
import {
|
|
112
|
+
PEER_WEB_PREFIX,
|
|
113
|
+
PEER_WEB_TICKET_HEADER,
|
|
114
|
+
createPeerWebForwarder,
|
|
115
|
+
createPeerWebTicket,
|
|
116
|
+
verifyPeerWebTicket,
|
|
117
|
+
} from "./peer-web.js";
|
|
110
118
|
import { acceptUpgrade } from "./ws-mini.js";
|
|
111
119
|
import {
|
|
112
120
|
createDesktopWebForwarder,
|
|
@@ -118,7 +126,7 @@ import {
|
|
|
118
126
|
export const name = "dsh-plugin-mesh";
|
|
119
127
|
export const inject = ["tools", "webServer"];
|
|
120
128
|
|
|
121
|
-
const VERSION = "0.5.
|
|
129
|
+
const VERSION = "0.5.13";
|
|
122
130
|
// 诊断用:进程启动时间 + 入口真实路径。磁盘上的版本**不在这里缓存** ——
|
|
123
131
|
// 升级之后进程还跑旧代码,版本号必须现读磁盘才能立刻反映出来(真踩过:
|
|
124
132
|
// 磁盘升到 0.5.1,进程还在 0.5.0,界面显示 0.5.0)。
|
|
@@ -134,6 +142,16 @@ const MAX_BODY = 256 * 1024;
|
|
|
134
142
|
const MAX_HISTORY = 200;
|
|
135
143
|
/** 默认固定端口:内网里"扫这个端口 + ping 一下"就能建立连接,无需任何配置。 */
|
|
136
144
|
export const MESH_DEFAULT_PORT = 45917;
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* 「陌生设备首次派活要本机点一次允许」这道闸,已经不是配置项了。
|
|
148
|
+
*
|
|
149
|
+
* 默认关:装上就能派活是这件事的全部价值,该收口的场景交给配对码。
|
|
150
|
+
* 保留实现是为了排查「谁在往我这派活」—— 需要时设环境变量:
|
|
151
|
+
* DSH_MESH_REQUIRE_APPROVAL=1
|
|
152
|
+
* 等有人真的需要常开,再把它捡回配置项(见 docs/DESIGN-NOTES.md)。
|
|
153
|
+
*/
|
|
154
|
+
export const DEBUG_REQUIRE_APPROVAL = process.env.DSH_MESH_REQUIRE_APPROVAL === "1";
|
|
137
155
|
/** 对端 DSH Web 的部署默认端口(回环代理拿不到对端自报地址时按它拼)。 */
|
|
138
156
|
const WEB_PANEL_DEFAULT_PORT = 3080;
|
|
139
157
|
|
|
@@ -235,6 +253,19 @@ export const Config = z.object({
|
|
|
235
253
|
scanTimeoutMs: z.natural().default(800),
|
|
236
254
|
/** 定期 ping 已知对端(毫秒),0 = 关闭。这条让发现不依赖组播也能保活 */
|
|
237
255
|
pingIntervalMs: z.natural().default(10000),
|
|
256
|
+
|
|
257
|
+
// ── 页面(浏览器)模式 ──────────────────────────────────────────────
|
|
258
|
+
/**
|
|
259
|
+
* 汇合点地址:页面从它加载,也从它拿名单、走它的同源隧道连桌面。
|
|
260
|
+
* 留空则用页面自己的 origin —— 页面永远是从某个 webdsh 上加载的。
|
|
261
|
+
*/
|
|
262
|
+
webHub: z.string().default(""),
|
|
263
|
+
/** 页面里是否扫本网段(汇合点所在 /24)。手填永远生效,这个只是兜底 */
|
|
264
|
+
webScan: z.boolean().default(true),
|
|
265
|
+
/** 页面里手填的对端地址,如 ["192.168.1.9:45917"] */
|
|
266
|
+
manualPeers: z.array(String).default([]),
|
|
267
|
+
/** 页面里重新发现一轮的间隔(毫秒),0 = 只在启动时 */
|
|
268
|
+
webDiscoverIntervalMs: z.natural().default(120000),
|
|
238
269
|
/**
|
|
239
270
|
* tailnet 通道的探测间隔(毫秒)。局域网走 pingIntervalMs。
|
|
240
271
|
*
|
|
@@ -251,18 +282,6 @@ export const Config = z.object({
|
|
|
251
282
|
*/
|
|
252
283
|
requirePairing: z.boolean().default(false),
|
|
253
284
|
|
|
254
|
-
/**
|
|
255
|
-
* 陌生设备第一次派活时,本机要不要先点一次「允许」。默认 **true**。
|
|
256
|
-
*
|
|
257
|
-
* 为什么默认开:默认零配置意味着同一网段里谁连得上 45917 都能让本机开一个
|
|
258
|
-
* agent 会话。对"自己家三台电脑"这是方便;对"公司 / 合租 / 咖啡厅"就是敞着门。
|
|
259
|
-
* 原来的答案是"你去开配对码",但那是个设置项,没人会主动打开。
|
|
260
|
-
*
|
|
261
|
-
* 只问**第一次**:允许过之后写进 state.json 的 approvedPeers,同一台设备不再问。
|
|
262
|
-
* 开了 requirePairing 就不再重复问 —— 配对码是更强的信任动作。
|
|
263
|
-
*/
|
|
264
|
-
requireApproval: z.boolean().default(true),
|
|
265
|
-
|
|
266
285
|
/** 对端任务在本机新建会话时用的 agent preset */
|
|
267
286
|
agentPreset: z.string().default("standard"),
|
|
268
287
|
/** 对端任务在本机新建会话时的权限;默认不给 danger-full-access */
|
|
@@ -595,7 +614,7 @@ function forgetApprovedPeer(target) {
|
|
|
595
614
|
*/
|
|
596
615
|
function inboundNeedsApproval(config, task) {
|
|
597
616
|
return needsApproval({
|
|
598
|
-
|
|
617
|
+
enabled: DEBUG_REQUIRE_APPROVAL,
|
|
599
618
|
approved: approvedPeers(),
|
|
600
619
|
peer: peerOfTask(task),
|
|
601
620
|
pairingRequired: pairingRequired(config),
|
|
@@ -1085,6 +1104,10 @@ function applyUnsafe(ctx, config) {
|
|
|
1085
1104
|
peerServer = handle;
|
|
1086
1105
|
lastPort = handle.port;
|
|
1087
1106
|
discovery.setPort(handle.port);
|
|
1107
|
+
// The card has to say which transports this build offers, or a peer never
|
|
1108
|
+
// learns it can skip plain HTTP. Set here because it becomes true exactly
|
|
1109
|
+
// when the server — and therefore `/mesh/v1/ws` — is listening.
|
|
1110
|
+
discovery.transports = ["lan", "ws"];
|
|
1088
1111
|
return handle;
|
|
1089
1112
|
};
|
|
1090
1113
|
|
|
@@ -1223,6 +1246,59 @@ function applyUnsafe(ctx, config) {
|
|
|
1223
1246
|
if (wantLan) startLanDiscovery();
|
|
1224
1247
|
}
|
|
1225
1248
|
|
|
1249
|
+
// ── 页面与汇合点 ──
|
|
1250
|
+
//
|
|
1251
|
+
// A browser page is the one shape that cannot be found and cannot be dialed:
|
|
1252
|
+
// no multicast, no listening socket. So it does the only thing it can —
|
|
1253
|
+
// attaches to the server it was loaded from, and learns peers from there.
|
|
1254
|
+
// Desktops pointed at the same hub get the reverse: they see the pages in
|
|
1255
|
+
// that registry and dispatch to them through it.
|
|
1256
|
+
if (inPage()) {
|
|
1257
|
+
meshHub = config.webHub !== "" ? String(config.webHub) : location.origin;
|
|
1258
|
+
meshTunnelToken = String(config.tunnelToken ?? "");
|
|
1259
|
+
discovery.transports = ["ws"];
|
|
1260
|
+
try {
|
|
1261
|
+
if (stopHub === undefined) {
|
|
1262
|
+
stopHub = attachHub({
|
|
1263
|
+
hubBase: meshHub,
|
|
1264
|
+
token: meshTunnelToken,
|
|
1265
|
+
identity: { id: discovery.selfRecord().id, machine: config.machineName || hostname() },
|
|
1266
|
+
log,
|
|
1267
|
+
});
|
|
1268
|
+
}
|
|
1269
|
+
const runWebDiscovery = async () => {
|
|
1270
|
+
try {
|
|
1271
|
+
const result = await discoverWebPeers({
|
|
1272
|
+
hubBase: meshHub,
|
|
1273
|
+
discovery,
|
|
1274
|
+
port: discovery.port || MESH_DEFAULT_PORT,
|
|
1275
|
+
manual: config.manualPeers ?? [],
|
|
1276
|
+
scan: config.webScan !== false,
|
|
1277
|
+
token: meshTunnelToken,
|
|
1278
|
+
concurrency: 24,
|
|
1279
|
+
onProgress: (record, host) => log(`页面发现:${record.machine || "?"} @ ${host}`),
|
|
1280
|
+
});
|
|
1281
|
+
if (result.found.length > 0) log(`页面发现 ${String(result.found.length)} 台对端`);
|
|
1282
|
+
} catch (error) {
|
|
1283
|
+
warn(`页面发现失败:${error?.message ?? error}`);
|
|
1284
|
+
}
|
|
1285
|
+
};
|
|
1286
|
+
} catch (error) {
|
|
1287
|
+
warn(`页面接入汇合点失败:${error?.message ?? String(error)}`);
|
|
1288
|
+
}
|
|
1289
|
+
void runWebDiscovery();
|
|
1290
|
+
if (webDiscoveryTimer === undefined && config.webDiscoverIntervalMs > 0) {
|
|
1291
|
+
webDiscoveryTimer = setInterval(() => { void runWebDiscovery() }, config.webDiscoverIntervalMs);
|
|
1292
|
+
webDiscoveryTimer.unref?.();
|
|
1293
|
+
}
|
|
1294
|
+
} else if (config.webHub !== "") {
|
|
1295
|
+
// Desktop pointed at a hub: the pages there are peers whose only address
|
|
1296
|
+
// is the hub itself.
|
|
1297
|
+
meshHub = String(config.webHub);
|
|
1298
|
+
meshTunnelToken = String(config.tunnelToken ?? "");
|
|
1299
|
+
void discoverHubPages({ hubBase: meshHub, discovery, log, warn, port: MESH_DEFAULT_PORT });
|
|
1300
|
+
}
|
|
1301
|
+
|
|
1226
1302
|
// ── 互联网启动(后台跑:首次要下载 sidecar,不能让界面/接口干等)──
|
|
1227
1303
|
if (wantNet && !internetUp) {
|
|
1228
1304
|
void ensureInternet(reason).catch((err) => warn(`互联网通道启动异常:${err?.message ?? err}`));
|
|
@@ -2369,6 +2445,8 @@ async function peerFetch(peer, path, opts = {}) {
|
|
|
2369
2445
|
// 宁可明确说"这条通道关了",也不要让它退化成"所有通道都失败" —— 后者会让
|
|
2370
2446
|
// 用户以为对端掉线了,实际是本机自己关的(这类误判在这个项目里踩过)。
|
|
2371
2447
|
const eps = all.filter((ep) => (ep.transport === "tsnet" ? transportGate.internet : transportGate.lan));
|
|
2448
|
+
// (`ws` and `hub` are both lan-scoped: same network, different framing.)
|
|
2449
|
+
// (`ws` is a lan-scoped transport: same host, same port, different framing.)
|
|
2372
2450
|
if (eps.length === 0) {
|
|
2373
2451
|
const closed = new Set();
|
|
2374
2452
|
for (const ep of all) {
|
|
@@ -2399,7 +2477,11 @@ async function peerFetch(peer, path, opts = {}) {
|
|
|
2399
2477
|
const res =
|
|
2400
2478
|
ep.transport === "tsnet"
|
|
2401
2479
|
? await fetchViaTailnet(ep, url, { ...rest, timeoutMs })
|
|
2402
|
-
:
|
|
2480
|
+
: ep.transport === "ws"
|
|
2481
|
+
? await fetchViaMeshWs(ep, url, { ...rest, timeoutMs })
|
|
2482
|
+
: ep.transport === "hub"
|
|
2483
|
+
? await fetchViaHub(ep, url, { ...rest, timeoutMs })
|
|
2484
|
+
: await fetchViaLan(ep, url, { ...rest, timeoutMs });
|
|
2403
2485
|
activeDiscovery?.markEndpoint(peer.id, endpointKeyOf(ep), { ok: true, rttMs: Date.now() - started });
|
|
2404
2486
|
return res;
|
|
2405
2487
|
} catch (err) {
|
|
@@ -2684,9 +2766,256 @@ async function pingKnownPeers({ discovery, config, warn }) {
|
|
|
2684
2766
|
}
|
|
2685
2767
|
|
|
2686
2768
|
/** 按**指定**端点发一个请求(保活探测用,不做失败切换)。 */
|
|
2769
|
+
/**
|
|
2770
|
+
* One request over the peer's mesh WebSocket.
|
|
2771
|
+
*
|
|
2772
|
+
* The frame carries what the HTTP request carried — method, path, body — and the
|
|
2773
|
+
* peer hands it to the same route handler, so a caller cannot tell which
|
|
2774
|
+
* transport answered. This is the path a page uses for everything: it can dial
|
|
2775
|
+
* out and read replies, and it can be reached by nothing.
|
|
2776
|
+
*
|
|
2777
|
+
* The pairing code travels in the first frame rather than a header, because a
|
|
2778
|
+
* browser cannot set headers on a WebSocket handshake; the URL would put it in
|
|
2779
|
+
* every proxy log on the way.
|
|
2780
|
+
* @param ep - the endpoint (`host`, `port`, `transport: "ws"`).
|
|
2781
|
+
* @param url - the absolute URL the caller built, of which only path/query matter.
|
|
2782
|
+
* @param opts - `method`, `headers`, `body`, `timeoutMs`.
|
|
2783
|
+
* @returns a `Response`, like the HTTP path returns.
|
|
2784
|
+
*/
|
|
2785
|
+
/** Whether this host half is running inside a browser page. */
|
|
2786
|
+
const inPage = () => typeof window !== "undefined" && typeof location !== "undefined";
|
|
2787
|
+
|
|
2788
|
+
/**
|
|
2789
|
+
* The hub a page came from, and its token.
|
|
2790
|
+
*
|
|
2791
|
+
* Module-level rather than threaded through every call: a page has exactly one
|
|
2792
|
+
* hub — the origin that served it — and the endpoint objects discovery stores
|
|
2793
|
+
* carry only host and port. Set once during apply, read on each send.
|
|
2794
|
+
*/
|
|
2795
|
+
let meshHub = "";
|
|
2796
|
+
let meshTunnelToken = "";
|
|
2797
|
+
|
|
2798
|
+
/** The hub attachment (page mode) and the rediscovery timer. */
|
|
2799
|
+
let stopHub;
|
|
2800
|
+
let webDiscoveryTimer;
|
|
2801
|
+
|
|
2802
|
+
/**
|
|
2803
|
+
* Put every page the hub knows about into the peer table.
|
|
2804
|
+
*
|
|
2805
|
+
* A page has no address of its own, so its endpoint is the hub plus its id —
|
|
2806
|
+
* `fetchViaHub` turns that back into the relay request. Desktops need this to
|
|
2807
|
+
* dispatch to a page at all.
|
|
2808
|
+
* @param options.hubBase - the hub origin.
|
|
2809
|
+
* @param options.discovery - the registrar.
|
|
2810
|
+
* @returns how many pages were added.
|
|
2811
|
+
*/
|
|
2812
|
+
async function discoverHubPages({ hubBase, discovery, log, warn, port }) {
|
|
2813
|
+
try {
|
|
2814
|
+
const response = await fetch(new URL("/__mesh/api/peers", hubBase), { cache: "no-store" });
|
|
2815
|
+
if (!response.ok) return 0;
|
|
2816
|
+
const body = await response.json();
|
|
2817
|
+
const hub = new URL(hubBase);
|
|
2818
|
+
let added = 0;
|
|
2819
|
+
for (const page of body?.pages ?? []) {
|
|
2820
|
+
if (typeof page?.id !== "string") continue;
|
|
2821
|
+
const record = {
|
|
2822
|
+
app: "dsh-mesh",
|
|
2823
|
+
id: String(page.id),
|
|
2824
|
+
machine: String(page.machine ?? page.id),
|
|
2825
|
+
port,
|
|
2826
|
+
transports: ["hub"],
|
|
2827
|
+
};
|
|
2828
|
+
const changed = discovery.note(record, hub.hostname, {
|
|
2829
|
+
transport: "hub",
|
|
2830
|
+
channel: `hub:${page.id}`,
|
|
2831
|
+
port: Number(hub.port) || (hub.protocol === "https:" ? 443 : 80),
|
|
2832
|
+
probeIntervalMs: 60_000,
|
|
2833
|
+
});
|
|
2834
|
+
if (changed) added += 1;
|
|
2835
|
+
}
|
|
2836
|
+
if (added > 0) log(`汇合点上有 ${String(added)} 个页面节点`);
|
|
2837
|
+
return added;
|
|
2838
|
+
} catch (error) {
|
|
2839
|
+
warn(`读取汇合点名单失败:${error?.message ?? error}`);
|
|
2840
|
+
return 0;
|
|
2841
|
+
}
|
|
2842
|
+
}
|
|
2843
|
+
|
|
2844
|
+
/** The hub origin: configured, else the page's own. */
|
|
2845
|
+
function hubOrigin() {
|
|
2846
|
+
return meshHub !== "" ? meshHub : (typeof location !== "undefined" ? location.origin : "");
|
|
2847
|
+
}
|
|
2848
|
+
|
|
2849
|
+
/** The tunnel token, when the hub requires one. */
|
|
2850
|
+
function tunnelToken() {
|
|
2851
|
+
return meshTunnelToken;
|
|
2852
|
+
}
|
|
2853
|
+
|
|
2854
|
+
/** Routes one frame through this instance's peer-server routes (page mode). */
|
|
2855
|
+
let frameRouter;
|
|
2856
|
+
|
|
2857
|
+
/**
|
|
2858
|
+
* Attach this page to its hub: announce, then answer what the hub relays.
|
|
2859
|
+
*
|
|
2860
|
+
* The socket is the page's only inbound path — nothing can POST to a browser —
|
|
2861
|
+
* so a task addressed to this page arrives here as a `request` frame, goes
|
|
2862
|
+
* through the same routes an HTTP caller would have hit, and its answer goes
|
|
2863
|
+
* back on the socket. Reconnects on close: a page that sleeps (a backgrounded
|
|
2864
|
+
* tab) wakes up as a peer again without a reload.
|
|
2865
|
+
* @param options.hubBase - the hub origin.
|
|
2866
|
+
* @param options.token - tunnel/signal token, when the hub requires one.
|
|
2867
|
+
* @param options.identity - `{ id, machine, capabilities }` to announce.
|
|
2868
|
+
* @param options.log - progress sink.
|
|
2869
|
+
* @returns a stop function.
|
|
2870
|
+
*/
|
|
2871
|
+
function attachHub({ hubBase, token = "", identity, log }) {
|
|
2872
|
+
let stopped = false;
|
|
2873
|
+
let socket;
|
|
2874
|
+
let retry = 1000;
|
|
2875
|
+
const connect = () => {
|
|
2876
|
+
if (stopped) return;
|
|
2877
|
+
const url = new URL("/__mesh/signal", hubBase);
|
|
2878
|
+
url.protocol = url.protocol === "https:" ? "wss:" : "ws:";
|
|
2879
|
+
if (token !== "") url.searchParams.set("t", token);
|
|
2880
|
+
try {
|
|
2881
|
+
socket = new WebSocket(url.href);
|
|
2882
|
+
} catch (error) {
|
|
2883
|
+
log?.(`页面接入汇合点失败:${error?.message ?? error}`);
|
|
2884
|
+
return;
|
|
2885
|
+
}
|
|
2886
|
+
socket.onopen = () => {
|
|
2887
|
+
retry = 1000;
|
|
2888
|
+
socket.send(JSON.stringify({ type: "hello", ...identity, kind: "page" }));
|
|
2889
|
+
log?.("已接入汇合点");
|
|
2890
|
+
};
|
|
2891
|
+
socket.onmessage = async (event) => {
|
|
2892
|
+
let message;
|
|
2893
|
+
try { message = JSON.parse(String(event.data)); } catch { return; }
|
|
2894
|
+
if (message?.type !== "request") return;
|
|
2895
|
+
// The hub asked this page to handle something. Answer even when routing
|
|
2896
|
+
// throws — a caller waiting on a reply must get one, not a timeout.
|
|
2897
|
+
let answer;
|
|
2898
|
+
try {
|
|
2899
|
+
answer = await frameRouter?.({ method: message.method, path: message.path, body: message.body });
|
|
2900
|
+
} catch (error) {
|
|
2901
|
+
answer = { status: 500, body: JSON.stringify({ ok: false, error: error?.message ?? String(error) }) };
|
|
2902
|
+
}
|
|
2903
|
+
try {
|
|
2904
|
+
socket.send(JSON.stringify({
|
|
2905
|
+
type: "answer",
|
|
2906
|
+
id: message.id,
|
|
2907
|
+
status: answer?.status ?? 500,
|
|
2908
|
+
body: answer?.body ?? "",
|
|
2909
|
+
}));
|
|
2910
|
+
} catch { /* 已断开,重连后再来 */ }
|
|
2911
|
+
};
|
|
2912
|
+
socket.onclose = (event) => {
|
|
2913
|
+
if (stopped) return;
|
|
2914
|
+
setTimeout(connect, (retry = Math.min(retry * 2, 15000)));
|
|
2915
|
+
};
|
|
2916
|
+
socket.onerror = () => { try { socket.close() } catch { /* 已关 */ } };
|
|
2917
|
+
};
|
|
2918
|
+
connect();
|
|
2919
|
+
return () => { stopped = true; try { socket?.close() } catch { /* 已关 */ } };
|
|
2920
|
+
}
|
|
2921
|
+
|
|
2922
|
+
/**
|
|
2923
|
+
* One request relayed to a page that only the hub can reach.
|
|
2924
|
+
*
|
|
2925
|
+
* The desktop keeps an ordinary `fetch` here: it posts to the hub's relay
|
|
2926
|
+
* endpoint and the hub turns that into a frame on the page's own outbound
|
|
2927
|
+
* socket. Which is why a page can be a full peer without ever being able to
|
|
2928
|
+
* accept a connection.
|
|
2929
|
+
* @param ep - endpoint with `channel: "hub:<pageId>"` and the hub's host/port.
|
|
2930
|
+
* @param url - the absolute URL the caller built; only path/query travel.
|
|
2931
|
+
* @param opts - `method`, `body`, `timeoutMs`.
|
|
2932
|
+
* @returns a `Response`, like the HTTP path returns.
|
|
2933
|
+
*/
|
|
2934
|
+
async function fetchViaHub(ep, url, opts = {}) {
|
|
2935
|
+
const { timeoutMs = 120000, method = "GET", body } = opts;
|
|
2936
|
+
const parsed = new URL(url);
|
|
2937
|
+
const pageId = String(ep.channel ?? "").replace(/^hub:/, "");
|
|
2938
|
+
const target = `http://${ep.hubHost}:${ep.hubPort}/__mesh/api/task?target=${encodeURIComponent(pageId)}`;
|
|
2939
|
+
const response = await fetch(target, {
|
|
2940
|
+
method: "POST",
|
|
2941
|
+
headers: { "content-type": "application/json" },
|
|
2942
|
+
body: JSON.stringify({
|
|
2943
|
+
method,
|
|
2944
|
+
path: `${parsed.pathname}${parsed.search}`,
|
|
2945
|
+
body: body === undefined ? undefined : (typeof body === "string" ? body : Buffer.from(body).toString("utf8")),
|
|
2946
|
+
}),
|
|
2947
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
2948
|
+
});
|
|
2949
|
+
const text = await response.text();
|
|
2950
|
+
return new Response(text, { status: response.status, headers: { "content-type": "application/json; charset=utf-8" } });
|
|
2951
|
+
}
|
|
2952
|
+
|
|
2953
|
+
async function fetchViaMeshWs(ep, url, opts = {}) {
|
|
2954
|
+
const { timeoutMs = 20000, method = "GET", headers = {}, body } = opts;
|
|
2955
|
+
const parsed = new URL(url);
|
|
2956
|
+
// In a page every socket has to leave through the server the page came from:
|
|
2957
|
+
// an https page cannot open `ws://`, and the peer has no certificate. The
|
|
2958
|
+
// tunnel completes the last hop on the private network, so this build only
|
|
2959
|
+
// ever needs the one certificate the browser already trusts.
|
|
2960
|
+
const target = inPage()
|
|
2961
|
+
? tunnelUrl(`${ep.host}:${ep.port}`, hubOrigin(), tunnelToken())
|
|
2962
|
+
: `ws://${ep.host}:${ep.port}/mesh/v1/ws`;
|
|
2963
|
+
return await new Promise((resolvePromise, rejectPromise) => {
|
|
2964
|
+
let settled = false;
|
|
2965
|
+
let socket;
|
|
2966
|
+
const finish = (fn, value) => {
|
|
2967
|
+
if (settled) return;
|
|
2968
|
+
settled = true;
|
|
2969
|
+
clearTimeout(timer);
|
|
2970
|
+
try { socket?.close(); } catch { /* 已关 */ }
|
|
2971
|
+
fn(value);
|
|
2972
|
+
};
|
|
2973
|
+
const timer = setTimeout(() => finish(rejectPromise, new Error(`ws ${ep.host}:${ep.port} timed out`)), timeoutMs);
|
|
2974
|
+
const requestId = randomBytes(6).toString("hex");
|
|
2975
|
+
try {
|
|
2976
|
+
socket = new WebSocket(target);
|
|
2977
|
+
} catch (error) {
|
|
2978
|
+
finish(rejectPromise, error);
|
|
2979
|
+
return;
|
|
2980
|
+
}
|
|
2981
|
+
socket.onopen = () => {
|
|
2982
|
+
socket.send(JSON.stringify({ type: "hello", token: currentSecret() ?? "" }));
|
|
2983
|
+
};
|
|
2984
|
+
socket.onmessage = (event) => {
|
|
2985
|
+
let message;
|
|
2986
|
+
try { message = JSON.parse(String(event.data)); } catch { return; }
|
|
2987
|
+
if (message?.type === "hello") {
|
|
2988
|
+
socket.send(JSON.stringify({
|
|
2989
|
+
type: "req",
|
|
2990
|
+
id: requestId,
|
|
2991
|
+
method,
|
|
2992
|
+
path: `${parsed.pathname}${parsed.search}`,
|
|
2993
|
+
headers,
|
|
2994
|
+
...(body === undefined ? {} : { body: typeof body === "string" ? body : Buffer.from(body).toString("utf8") }),
|
|
2995
|
+
}));
|
|
2996
|
+
return;
|
|
2997
|
+
}
|
|
2998
|
+
if (message?.type === "error") {
|
|
2999
|
+
finish(rejectPromise, new Error(`ws ${ep.host}:${ep.port}: ${message.error ?? "refused"}`));
|
|
3000
|
+
return;
|
|
3001
|
+
}
|
|
3002
|
+
if (message?.type === "res" && message.id === requestId) {
|
|
3003
|
+
finish(resolvePromise, new Response(String(message.body ?? ""), {
|
|
3004
|
+
status: Number(message.status) || 200,
|
|
3005
|
+
headers: { "content-type": "application/json; charset=utf-8" },
|
|
3006
|
+
}));
|
|
3007
|
+
}
|
|
3008
|
+
};
|
|
3009
|
+
socket.onerror = () => finish(rejectPromise, new Error(`ws ${ep.host}:${ep.port} unreachable`));
|
|
3010
|
+
socket.onclose = () => finish(rejectPromise, new Error(`ws ${ep.host}:${ep.port} closed before a reply`));
|
|
3011
|
+
});
|
|
3012
|
+
}
|
|
3013
|
+
|
|
2687
3014
|
async function peerFetchEndpoint(ep, url, opts = {}) {
|
|
2688
3015
|
const { timeoutMs = 20000, ...rest } = opts;
|
|
2689
3016
|
if (ep.transport === "tsnet") return fetchViaTailnet(ep, url, { ...rest, timeoutMs });
|
|
3017
|
+
if (ep.transport === "ws") return fetchViaMeshWs(ep, url, { ...rest, timeoutMs });
|
|
3018
|
+
if (ep.transport === "hub") return fetchViaHub(ep, url, { ...rest, timeoutMs });
|
|
2690
3019
|
return fetchViaLan(ep, url, { ...rest, timeoutMs });
|
|
2691
3020
|
}
|
|
2692
3021
|
|
|
@@ -2704,6 +3033,17 @@ async function peerFetchEndpoint(ep, url, opts = {}) {
|
|
|
2704
3033
|
* @param options.bindHost 监听地址。局域网关着时传 "127.0.0.1"。
|
|
2705
3034
|
*/
|
|
2706
3035
|
function startPeerServer(ctx, { config, discovery, inbound, outbound, log, warn, waker, threadState, capFor, capOverrides, throttled, onHistoryChange, diagnostics, port: fixedPort, bindHost = "0.0.0.0" }) {
|
|
3036
|
+
// mesh 口的**实际**监听端口(被占用时会退回随机),隧道地址要用它而不是配置值。
|
|
3037
|
+
let meshPort = 0;
|
|
3038
|
+
// 对端 GUI 的隧道:请求进到这里就转发给本机 127.0.0.1:<Web 端口>,
|
|
3039
|
+
// 认证(会话 cookie)与来源改写都在 peer-web.js 里完成。
|
|
3040
|
+
const peerWeb = createPeerWebForwarder({
|
|
3041
|
+
portOf: () => ctx.get("webServer")?.port ?? 0,
|
|
3042
|
+
secretOf: createSecretReader(join(process.env.DSH_HOME || join(homedir(), ".dsh"), ".credentials.yaml")),
|
|
3043
|
+
log,
|
|
3044
|
+
warn,
|
|
3045
|
+
});
|
|
3046
|
+
|
|
2707
3047
|
const send = (res, code, payload) => {
|
|
2708
3048
|
const body = Buffer.from(JSON.stringify(payload), "utf8");
|
|
2709
3049
|
res.writeHead(code, { "Content-Type": "application/json; charset=utf-8", "Content-Length": body.length });
|
|
@@ -2729,7 +3069,7 @@ function startPeerServer(ctx, { config, discovery, inbound, outbound, log, warn,
|
|
|
2729
3069
|
return transportGate.lan ? "" : "局域网 mesh 已关闭";
|
|
2730
3070
|
};
|
|
2731
3071
|
|
|
2732
|
-
const
|
|
3072
|
+
const handleRequest = async (req, res) => {
|
|
2733
3073
|
const url = new URL(req.url, "http://localhost");
|
|
2734
3074
|
const path = url.pathname;
|
|
2735
3075
|
const self = { ...discovery.selfRecord(), instanceId: discovery.instanceId };
|
|
@@ -2752,6 +3092,25 @@ function startPeerServer(ctx, { config, discovery, inbound, outbound, log, warn,
|
|
|
2752
3092
|
});
|
|
2753
3093
|
}
|
|
2754
3094
|
|
|
3095
|
+
// 对端 GUI 隧道:票据自证,所以放在配对闸门之前 —— 票据本身是
|
|
3096
|
+
// /mesh/v1/web-url 在**鉴权之后**签发的,等于"拿得到地址的人才用得了这条通道"。
|
|
3097
|
+
if (path === PEER_WEB_PREFIX || path.startsWith(`${PEER_WEB_PREFIX}/`)) {
|
|
3098
|
+
// 安全策略与 mesh 口其它路由**完全一致**:配对码打开时,每个请求都必须带
|
|
3099
|
+
// 共享密钥(观看端代理会把 X-Dsh-Mesh-Token 附在每个转发请求上);关闭时
|
|
3100
|
+
// 与派发任务同权。票据是**额外**一层:只在鉴权后的 /mesh/v1/web-url 发放、
|
|
3101
|
+
// 12 小时过期,用来把"看得见地址"和"能一直用"分开。
|
|
3102
|
+
if (!authed(req)) return send(res, 403, { ok: false, error: "未配对:两台机器的配对码不一致(本机开启了 requirePairing)" });
|
|
3103
|
+
const ticket = String(req.headers[PEER_WEB_TICKET_HEADER] ?? url.searchParams.get("t") ?? "");
|
|
3104
|
+
const verdict = verifyPeerWebTicket(currentSecret(), ticket);
|
|
3105
|
+
if (!verdict) return send(res, 403, { ok: false, error: "隧道票据无效或已过期,请在对端重新打开面板" });
|
|
3106
|
+
// 帧路由(页面/WS 传输)没有 pipe,也不该被当成 HTTP 转发。
|
|
3107
|
+
if (typeof req.pipe !== "function") {
|
|
3108
|
+
return send(res, 400, { ok: false, error: "GUI 隧道只接受 HTTP 载体" });
|
|
3109
|
+
}
|
|
3110
|
+
peerWeb.handle(req, res);
|
|
3111
|
+
return;
|
|
3112
|
+
}
|
|
3113
|
+
|
|
2755
3114
|
if (!authed(req)) return send(res, 403, { ok: false, error: "未配对:两台机器的配对码不一致(本机开启了 requirePairing)" });
|
|
2756
3115
|
|
|
2757
3116
|
// 一次性文件搬运:给对端取本机插件包里的文件(同版本部署用)。
|
|
@@ -2805,6 +3164,12 @@ function startPeerServer(ctx, { config, discovery, inbound, outbound, log, warn,
|
|
|
2805
3164
|
// 只绑了回环时如实说:地址虽然给得出来,但局域网里连不通。
|
|
2806
3165
|
const lanReachable = bind === "0.0.0.0" && !!lanHost;
|
|
2807
3166
|
const host = lanReachable ? lanHost : "127.0.0.1";
|
|
3167
|
+
// 回环-only 的宿主(尤其 DSH Desktop:它把 host 硬编码成 127.0.0.1,
|
|
3168
|
+
// 改配置也开不了端口)给一条走 mesh 口的隧道:零证书、零新端口,
|
|
3169
|
+
// 转发发生在**我这边**的进程里,所以 GUI 不必对外监听。
|
|
3170
|
+
const tunnelUrl = !lanReachable && lanHost && meshPort
|
|
3171
|
+
? `http://${lanHost}:${meshPort}${PEER_WEB_PREFIX}/`
|
|
3172
|
+
: "";
|
|
2808
3173
|
return send(res, 200, {
|
|
2809
3174
|
ok: true,
|
|
2810
3175
|
supported: true,
|
|
@@ -2812,6 +3177,9 @@ function startPeerServer(ctx, { config, discovery, inbound, outbound, log, warn,
|
|
|
2812
3177
|
bind,
|
|
2813
3178
|
lanReachable,
|
|
2814
3179
|
port,
|
|
3180
|
+
...(tunnelUrl
|
|
3181
|
+
? { tunnel: { url: tunnelUrl, ticket: createPeerWebTicket(currentSecret()), header: PEER_WEB_TICKET_HEADER } }
|
|
3182
|
+
: {}),
|
|
2815
3183
|
});
|
|
2816
3184
|
} catch (err) {
|
|
2817
3185
|
return send(res, 200, { ok: false, supported: false, error: String(err?.message ?? err) });
|
|
@@ -2896,6 +3264,135 @@ function startPeerServer(ctx, { config, discovery, inbound, outbound, log, warn,
|
|
|
2896
3264
|
}
|
|
2897
3265
|
|
|
2898
3266
|
return send(res, 404, { ok: false, error: "not found" });
|
|
3267
|
+
};
|
|
3268
|
+
|
|
3269
|
+
const server = createServer((req, res) => { void handleRequest(req, res); });
|
|
3270
|
+
|
|
3271
|
+
// A page has no reachable server, so its own copy of these routes is only
|
|
3272
|
+
// ever entered through frames from the hub. Publishing the router instead of
|
|
3273
|
+
// duplicating it is what keeps one implementation of every route.
|
|
3274
|
+
frameRouter = async (frame) => await new Promise((resolvePromise) => {
|
|
3275
|
+
void handleRequest(frameRequest(frame), frameResponse(resolvePromise));
|
|
3276
|
+
});
|
|
3277
|
+
|
|
3278
|
+
/** Which transports this build offers; echoed in `hello` so peers need not probe. */
|
|
3279
|
+
const wsTransports = () => ["lan", "ws"];
|
|
3280
|
+
|
|
3281
|
+
/**
|
|
3282
|
+
* The same routes, over a WebSocket, for peers that cannot make an inbound
|
|
3283
|
+
* HTTP connection to this machine.
|
|
3284
|
+
*
|
|
3285
|
+
* A browser page is exactly that: it can dial out, and nothing can dial in.
|
|
3286
|
+
* Running the page as a full peer therefore needs a transport the page itself
|
|
3287
|
+
* can open and read replies on — so this endpoint speaks frames that carry the
|
|
3288
|
+
* same `{method, path, body}` a caller would have sent over HTTP, hands them
|
|
3289
|
+
* to the very same route handler, and returns `{status, body}`. Nothing in the
|
|
3290
|
+
* routes below knows which transport the request arrived on.
|
|
3291
|
+
*
|
|
3292
|
+
* Auth moves from a header to the first frame: a browser cannot set
|
|
3293
|
+
* `x-dsh-mesh-token` on a WebSocket handshake, and putting the pairing code in
|
|
3294
|
+
* a URL would write it into every proxy log on the way.
|
|
3295
|
+
*/
|
|
3296
|
+
const wsConnections = new Set();
|
|
3297
|
+
|
|
3298
|
+
/** A request object with the surface `handleRequest` reads. */
|
|
3299
|
+
const frameRequest = ({ method, path, headers, body } = {}) => {
|
|
3300
|
+
const request = new EventEmitter();
|
|
3301
|
+
request.method = String(method ?? "GET").toUpperCase();
|
|
3302
|
+
request.url = String(path ?? "/");
|
|
3303
|
+
request.headers = { ...(headers ?? {}) };
|
|
3304
|
+
request.socket = {
|
|
3305
|
+
remoteAddress: String(request.headers["x-forwarded-for"] ?? "").split(",")[0].trim() || "127.0.0.1",
|
|
3306
|
+
};
|
|
3307
|
+
request.setTimeout = () => request;
|
|
3308
|
+
setImmediate(() => {
|
|
3309
|
+
if (body !== undefined && body !== null) {
|
|
3310
|
+
request.emit("data", Buffer.from(typeof body === "string" ? body : JSON.stringify(body), "utf8"));
|
|
3311
|
+
}
|
|
3312
|
+
request.emit("end");
|
|
3313
|
+
});
|
|
3314
|
+
return request;
|
|
3315
|
+
};
|
|
3316
|
+
|
|
3317
|
+
/** A response object that resolves the frame the peer is waiting for. */
|
|
3318
|
+
const frameResponse = (resolve) => {
|
|
3319
|
+
const chunks = [];
|
|
3320
|
+
let status = 200;
|
|
3321
|
+
let headers = {};
|
|
3322
|
+
return {
|
|
3323
|
+
writeHead(code, next) { status = code; headers = next ?? {}; return this; },
|
|
3324
|
+
setHeader() { return this; },
|
|
3325
|
+
write(chunk) { if (chunk) chunks.push(Buffer.from(chunk)); return true; },
|
|
3326
|
+
end(chunk) {
|
|
3327
|
+
if (chunk) chunks.push(Buffer.from(chunk));
|
|
3328
|
+
resolve({ status, headers, body: Buffer.concat(chunks).toString("utf8") });
|
|
3329
|
+
},
|
|
3330
|
+
};
|
|
3331
|
+
};
|
|
3332
|
+
|
|
3333
|
+
server.on("upgrade", (req, socket, head) => {
|
|
3334
|
+
const url = new URL(req.url ?? "/", "http://localhost");
|
|
3335
|
+
// GUI 隧道的流式载波(/api/remote.mux):缺了这条,对端界面会一直"重新连接中"。
|
|
3336
|
+
if (url.pathname.startsWith(`${PEER_WEB_PREFIX}/`)) {
|
|
3337
|
+
// 与 HTTP 侧同一套:配对开启时升级请求也要带共享密钥。
|
|
3338
|
+
if (!authed(req)) {
|
|
3339
|
+
socket.destroy();
|
|
3340
|
+
return;
|
|
3341
|
+
}
|
|
3342
|
+
const ticket = String(req.headers[PEER_WEB_TICKET_HEADER] ?? url.searchParams.get("t") ?? "");
|
|
3343
|
+
if (!verifyPeerWebTicket(currentSecret(), ticket)) {
|
|
3344
|
+
socket.destroy();
|
|
3345
|
+
return;
|
|
3346
|
+
}
|
|
3347
|
+
peerWeb.upgrade(req, socket, head);
|
|
3348
|
+
return;
|
|
3349
|
+
}
|
|
3350
|
+
if (url.pathname !== "/mesh/v1/ws") {
|
|
3351
|
+
// Other upgrade routes belong to someone else on this origin; leave the
|
|
3352
|
+
// socket alone rather than destroying a desktop-web stream.
|
|
3353
|
+
return;
|
|
3354
|
+
}
|
|
3355
|
+
const self = { ...discovery.selfRecord(), instanceId: discovery.instanceId };
|
|
3356
|
+
let authenticated = false;
|
|
3357
|
+
let handle;
|
|
3358
|
+
const send = (payload) => handle?.send(JSON.stringify(payload));
|
|
3359
|
+
const timer = setTimeout(() => {
|
|
3360
|
+
if (!authenticated) {
|
|
3361
|
+
send({ type: "error", error: "unauthenticated: no hello within 5s" });
|
|
3362
|
+
handle?.close();
|
|
3363
|
+
}
|
|
3364
|
+
}, 5000);
|
|
3365
|
+
|
|
3366
|
+
handle = acceptUpgrade(req, socket, head, {
|
|
3367
|
+
onMessage: (text) => {
|
|
3368
|
+
let message;
|
|
3369
|
+
try { message = JSON.parse(text); } catch { return; }
|
|
3370
|
+
if (!authenticated) {
|
|
3371
|
+
if (message?.type !== "hello") return;
|
|
3372
|
+
const ok = !pairingRequired(config) || safeEqual(String(message.token ?? ""), currentSecret());
|
|
3373
|
+
if (!ok) {
|
|
3374
|
+
send({ type: "error", error: "unauthenticated: wrong pairing code" });
|
|
3375
|
+
handle?.close();
|
|
3376
|
+
return;
|
|
3377
|
+
}
|
|
3378
|
+
authenticated = true;
|
|
3379
|
+
clearTimeout(timer);
|
|
3380
|
+
wsConnections.add(handle);
|
|
3381
|
+
// `transports` tells the caller which channels this build offers, so a
|
|
3382
|
+
// peer knows to prefer ws without probing for it on every send.
|
|
3383
|
+
send({ type: "hello", ...self, transports: wsTransports(), instanceId: discovery.instanceId });
|
|
3384
|
+
return;
|
|
3385
|
+
}
|
|
3386
|
+
if (message?.type === "ping") { send({ type: "pong", at: Date.now() }); return; }
|
|
3387
|
+
if (message?.type !== "req") return;
|
|
3388
|
+
const response = frameResponse((result) => send({ type: "res", id: message.id, status: result.status, body: result.body }));
|
|
3389
|
+
void handleRequest(frameRequest(message), response);
|
|
3390
|
+
},
|
|
3391
|
+
onClose: () => {
|
|
3392
|
+
clearTimeout(timer);
|
|
3393
|
+
if (handle) wsConnections.delete(handle);
|
|
3394
|
+
},
|
|
3395
|
+
});
|
|
2899
3396
|
});
|
|
2900
3397
|
|
|
2901
3398
|
return new Promise((resolvePromise, rejectPromise) => {
|
|
@@ -2911,6 +3408,7 @@ function startPeerServer(ctx, { config, discovery, inbound, outbound, log, warn,
|
|
|
2911
3408
|
});
|
|
2912
3409
|
server.listen(port, bindHost, () => {
|
|
2913
3410
|
const addr = server.address();
|
|
3411
|
+
meshPort = addr.port;
|
|
2914
3412
|
log(`对端服务监听 ${addr.address}:${addr.port}${isFallback ? "(随机)" : "(固定)"}`);
|
|
2915
3413
|
resolvePromise({ port: addr.port, host: addr.address, server });
|
|
2916
3414
|
});
|
|
@@ -3366,8 +3864,8 @@ function registerUiRoutes(ctx, { discovery, config, identity, inbound, outbound,
|
|
|
3366
3864
|
// 界面开关一改就得立刻反映出来(端到端测试就是在这抓到过一次 stale)。
|
|
3367
3865
|
requirePairing: pairingRequired(config),
|
|
3368
3866
|
requirePairingPinned: pairingPinned(config),
|
|
3369
|
-
//
|
|
3370
|
-
requireApproval:
|
|
3867
|
+
// 陌生设备首次派活是否要本机点一次「允许」(仅调试用,见 DEBUG_REQUIRE_APPROVAL)
|
|
3868
|
+
requireApproval: DEBUG_REQUIRE_APPROVAL,
|
|
3371
3869
|
useMulticast: config.useMulticast !== false,
|
|
3372
3870
|
scanOnStart: config.scanOnStart !== false,
|
|
3373
3871
|
scanIntervalMs: config.scanIntervalMs,
|
|
@@ -4365,6 +4863,23 @@ function registerUiRoutes(ctx, { discovery, config, identity, inbound, outbound,
|
|
|
4365
4863
|
parsed.searchParams.delete("token");
|
|
4366
4864
|
remoteBase = parsed.href;
|
|
4367
4865
|
lanReachable = rb.lanReachable === true;
|
|
4866
|
+
// 对端自报回环-only,但给了 mesh 口隧道:直接用它。
|
|
4867
|
+
// 这是零证书路径,且不需要对端开任何端口(Desktop 也适用)。
|
|
4868
|
+
const tunnel = rb.tunnel;
|
|
4869
|
+
if (!lanReachable && tunnel && typeof tunnel.url === "string" && tunnel.url) {
|
|
4870
|
+
if (typeof tunnel.ticket !== "string" || !tunnel.ticket) {
|
|
4871
|
+
note = "对端给了隧道地址但没给票据(对端插件版本不一致)";
|
|
4872
|
+
} else {
|
|
4873
|
+
return {
|
|
4874
|
+
remoteBase: tunnel.url,
|
|
4875
|
+
pathPrefix: PEER_WEB_PREFIX,
|
|
4876
|
+
passthrough: true,
|
|
4877
|
+
lanReachable: true,
|
|
4878
|
+
ticket: tunnel.ticket,
|
|
4879
|
+
note: "经对端 mesh 口隧道(对端 GUI 仍只听回环)",
|
|
4880
|
+
};
|
|
4881
|
+
}
|
|
4882
|
+
}
|
|
4368
4883
|
} else {
|
|
4369
4884
|
note = rb?.error ? String(rb.error) : `对端 HTTP ${r.status}`;
|
|
4370
4885
|
}
|
|
@@ -4393,7 +4908,8 @@ function registerUiRoutes(ctx, { discovery, config, identity, inbound, outbound,
|
|
|
4393
4908
|
);
|
|
4394
4909
|
if (resolved.error) return send(res, 400, { ok: false, error: resolved.error });
|
|
4395
4910
|
|
|
4396
|
-
|
|
4911
|
+
// 票据写进签名:隧道票据会过期,重开面板时拿到新的就该重建代理(别复用旧票据)。
|
|
4912
|
+
const signature = `${resolved.remoteBase}|${resolved.pathPrefix || ""}|${resolved.passthrough ? "p" : "c"}|${resolved.ticket ?? ""}`;
|
|
4397
4913
|
try {
|
|
4398
4914
|
const existing = webPanels.get(machine);
|
|
4399
4915
|
if (existing && existing.signature === signature) {
|
|
@@ -4418,6 +4934,13 @@ function registerUiRoutes(ctx, { discovery, config, identity, inbound, outbound,
|
|
|
4418
4934
|
onCookie: (cookie) => writePanelCookie(resolved.remoteBase, cookie),
|
|
4419
4935
|
pathPrefix: resolved.pathPrefix || "",
|
|
4420
4936
|
passthrough: resolved.passthrough === true,
|
|
4937
|
+
upstreamHeaders: resolved.ticket
|
|
4938
|
+
? {
|
|
4939
|
+
[PEER_WEB_TICKET_HEADER]: resolved.ticket,
|
|
4940
|
+
// 与 mesh 口其它路由同一套鉴权:配对码打开时对端会校验这个头。
|
|
4941
|
+
"x-dsh-mesh-token": currentSecret(),
|
|
4942
|
+
}
|
|
4943
|
+
: {},
|
|
4421
4944
|
log,
|
|
4422
4945
|
warn,
|
|
4423
4946
|
});
|