dsh-plugin-mesh 0.5.3 → 0.5.4
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/AGENTS.md +5 -4
- package/README.md +61 -1
- package/client/client.js +324 -122
- package/lib/_selftest-tsnet.mjs +173 -1
- package/lib/_selftest-workbench.mjs +53 -0
- package/lib/discovery.js +89 -18
- package/lib/index.js +543 -178
- package/lib/tsnet/channels.js +72 -0
- package/lib/tsnet/manager.js +187 -48
- package/llms.txt +17 -10
- package/package.json +2 -2
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* tailnet 通道列表的解析(纯函数,便于自测)。
|
|
3
|
+
*
|
|
4
|
+
* 一台机器可以同时加入多个 tailnet:每个通道一个 sidecar、一个 tailnet 地址。
|
|
5
|
+
* **设备身份仍然只有一个** —— instanceId 不变,通道只是这个节点的多条可达路径。
|
|
6
|
+
*
|
|
7
|
+
* 优先级:
|
|
8
|
+
* 1. profile 配置里的 `tsnetChannels`(部署方钉死,界面改不了)
|
|
9
|
+
* 2. `state.json` 里的 `tsnetChannels`(界面添加/编辑的,0600)
|
|
10
|
+
* 3. 都没有时退回旧版单授权码字段,合成一个 id = `default` 的通道
|
|
11
|
+
*
|
|
12
|
+
* 这一层刻意不碰文件与网络:调用方把 config / state 传进来,输出可预测。
|
|
13
|
+
* 通道 id 会进 sidecar 的状态目录,所以它的稳定性就是 tailnet 里的设备身份。
|
|
14
|
+
*/
|
|
15
|
+
import { createHash } from "node:crypto";
|
|
16
|
+
import { channelKey } from "./manager.js";
|
|
17
|
+
|
|
18
|
+
/** 没给 id 时按「显示名 + 控制服务器」推导一个稳定 id。 */
|
|
19
|
+
export function deriveChannelId(label, controlUrl) {
|
|
20
|
+
return `ch-${createHash("sha256").update(`${label}\n${controlUrl}`).digest("hex").slice(0, 10)}`;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @param {object} args
|
|
25
|
+
* @param {object} args.config profile 配置(只读其中的 tsnet* 字段)
|
|
26
|
+
* @param {object} args.state state.json 解析结果
|
|
27
|
+
* @returns {{id,label,authKey,controlUrl,hostname,enabled,legacy,source}[]}
|
|
28
|
+
*/
|
|
29
|
+
export function resolveTailnetChannels({ config = {}, state = {} } = {}) {
|
|
30
|
+
const pick = (fromConfig, fromState) => String(fromConfig ?? "").trim() || String(fromState ?? "").trim();
|
|
31
|
+
const cfgList = Array.isArray(config.tsnetChannels) ? config.tsnetChannels : [];
|
|
32
|
+
const stList = Array.isArray(state.tsnetChannels) ? state.tsnetChannels : [];
|
|
33
|
+
const raw = cfgList.length > 0 ? cfgList : stList;
|
|
34
|
+
const fromConfigFile = cfgList.length > 0;
|
|
35
|
+
|
|
36
|
+
const list = [];
|
|
37
|
+
const used = new Set();
|
|
38
|
+
const add = (item) => {
|
|
39
|
+
let id = channelKey(item.id, "");
|
|
40
|
+
if (!id) id = deriveChannelId(String(item.label ?? "").trim(), String(item.controlUrl ?? "").trim());
|
|
41
|
+
if (used.has(id)) {
|
|
42
|
+
let n = 2;
|
|
43
|
+
while (used.has(`${id}-${n}`)) n += 1;
|
|
44
|
+
id = `${id}-${n}`;
|
|
45
|
+
}
|
|
46
|
+
used.add(id);
|
|
47
|
+
list.push({
|
|
48
|
+
id,
|
|
49
|
+
label: String(item.label ?? "").trim() || (id === "default" ? "Tailscale" : `Tailnet ${list.length + 1}`),
|
|
50
|
+
authKey: String(item.authKey ?? "").trim(),
|
|
51
|
+
controlUrl: String(item.controlUrl ?? "").trim(),
|
|
52
|
+
hostname: String(item.hostname ?? "").trim(),
|
|
53
|
+
enabled: item.enabled !== false,
|
|
54
|
+
legacy: item.legacy === true,
|
|
55
|
+
source: fromConfigFile ? "config" : "state",
|
|
56
|
+
});
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
if (raw.length > 0) {
|
|
60
|
+
for (const c of raw) if (c && typeof c === "object") add(c);
|
|
61
|
+
} else {
|
|
62
|
+
// 旧版单通道:`default` 沿用旧目录,升级不丢设备身份。
|
|
63
|
+
const enabled = config.tsnetEnabled === true || state.tsnetEnabled === true;
|
|
64
|
+
const authKey = pick(config.tsnetAuthKey, state.tsnetAuthKey);
|
|
65
|
+
const controlUrl = pick(config.tsnetControlUrl, state.tsnetControlUrl);
|
|
66
|
+
const hostname = pick(config.tsnetHostname, state.tsnetHostname);
|
|
67
|
+
if (enabled || authKey || controlUrl) {
|
|
68
|
+
add({ id: "default", label: "Tailscale", authKey, controlUrl, hostname, enabled, legacy: true });
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return list;
|
|
72
|
+
}
|
package/lib/tsnet/manager.js
CHANGED
|
@@ -1,10 +1,17 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* tsnet sidecar
|
|
2
|
+
* tsnet sidecar 的进程管理(机器级,每个 tailnet 通道一个节点)。
|
|
3
3
|
*
|
|
4
4
|
* 为什么是「机器级」而不是「profile 级」:
|
|
5
5
|
* 每个 sidecar 就是 tailnet 里的一个设备,会占用一个设备名额。同一台机器上
|
|
6
|
-
* 跑 desktop / meshtest 两个 profile
|
|
7
|
-
* sidecar,由第一个用到的实例拉起,后来的实例通过 rendezvous 文件复用。
|
|
6
|
+
* 跑 desktop / meshtest 两个 profile 不应该变成两台设备,所以整机每个通道只跑
|
|
7
|
+
* 一个 sidecar,由第一个用到的实例拉起,后来的实例通过 rendezvous 文件复用。
|
|
8
|
+
*
|
|
9
|
+
* 为什么是「多个通道」:
|
|
10
|
+
* 一台机器可能同时属于多个 tailnet(个人账号 / 公司 tailnet / 自建 Headscale)。
|
|
11
|
+
* 一个 tsnet 节点只能加入一个 tailnet,所以每条通道各有自己的 sidecar、状态目录
|
|
12
|
+
* 和 rendezvous。通道 id 是稳定标识:`default` 沿用旧目录,升级不丢设备身份;
|
|
13
|
+
* 其它通道各占 `tsnet/<id>/`。**设备身份仍然只有一个** —— 多条通道只是同一个
|
|
14
|
+
* mesh 节点的多条可达路径。
|
|
8
15
|
*
|
|
9
16
|
* 生命周期:
|
|
10
17
|
* · sidecar 监听 stdin EOF 自杀 —— 即使 DSH 被 kill -9,也不会留下野进程
|
|
@@ -13,12 +20,12 @@
|
|
|
13
20
|
*/
|
|
14
21
|
import { EventEmitter } from "node:events";
|
|
15
22
|
import { spawn } from "node:child_process";
|
|
16
|
-
import {
|
|
23
|
+
import { mkdirSync, readFileSync, renameSync, rmSync, unlinkSync, writeFileSync } from "node:fs";
|
|
17
24
|
import { request as httpRequest } from "node:http";
|
|
18
25
|
import { createHash, randomBytes } from "node:crypto";
|
|
19
26
|
import { join } from "node:path";
|
|
20
27
|
import { homedir } from "node:os";
|
|
21
|
-
import { SIDECAR_PROTOCOL, binaryPath, ensureBinary, cacheIsCurrent, binaryInfo, clearBinary
|
|
28
|
+
import { SIDECAR_PROTOCOL, binaryPath, ensureBinary, cacheIsCurrent, binaryInfo, clearBinary } from "./download.js";
|
|
22
29
|
|
|
23
30
|
/** 重启退避(毫秒);用完就停手,避免疯狂重启把日志刷爆。 */
|
|
24
31
|
const RESTART_BACKOFF_MS = [2000, 8000, 20000];
|
|
@@ -33,22 +40,56 @@ function meshDir() {
|
|
|
33
40
|
}
|
|
34
41
|
|
|
35
42
|
/** 机器级共享目录:sidecar 状态、rendezvous、二进制缓存都挂在这下面。 */
|
|
36
|
-
function
|
|
43
|
+
export function tsnetRootDir() {
|
|
37
44
|
return join(meshDir(), "tsnet");
|
|
38
45
|
}
|
|
39
46
|
|
|
40
|
-
|
|
41
|
-
|
|
47
|
+
/**
|
|
48
|
+
* 通道 id 的合法化:只保留 [a-z0-9_-]。
|
|
49
|
+
*
|
|
50
|
+
* 通道 id 会进文件路径,所以这里必须挡掉 `/`、`..` 之类;空值兜底成
|
|
51
|
+
* `default`,正好与旧版本的单节点目录对应。
|
|
52
|
+
*/
|
|
53
|
+
export function channelKey(raw, fallback = "default") {
|
|
54
|
+
const s = String(raw ?? "")
|
|
55
|
+
.trim()
|
|
56
|
+
.toLowerCase()
|
|
57
|
+
.replace(/[^a-z0-9_-]+/g, "-")
|
|
58
|
+
.replace(/^-+|-+$/g, "")
|
|
59
|
+
.slice(0, 40);
|
|
60
|
+
return s || fallback;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* 通道的私有目录。
|
|
65
|
+
*
|
|
66
|
+
* `default` 沿用旧布局(`mesh/tsnet/` 根目录),这样升级前注册过的那台设备
|
|
67
|
+
* 身份不丢;其它通道各自一个子目录 —— 每个 sidecar 是一个独立 tailnet 节点,
|
|
68
|
+
* 状态目录绝不能共享,否则第二个节点会顶掉第一个的身份。
|
|
69
|
+
*/
|
|
70
|
+
export function channelDir(key) {
|
|
71
|
+
const k = channelKey(key);
|
|
72
|
+
return k === "default" ? tsnetRootDir() : join(tsnetRootDir(), k);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function rendezvousPath(key) {
|
|
76
|
+
return join(channelDir(key), "rendezvous.json");
|
|
42
77
|
}
|
|
43
78
|
|
|
44
79
|
/** tsnet 的节点身份目录 —— 必须持久,否则每次重启都会注册成新设备。 */
|
|
45
|
-
function
|
|
46
|
-
return join(
|
|
80
|
+
export function channelStateDir(key) {
|
|
81
|
+
return join(channelDir(key), "state");
|
|
47
82
|
}
|
|
48
83
|
|
|
49
|
-
|
|
84
|
+
/** 授权码指纹:只用来判断"rendezvous 里的节点是不是用同一把 key 拉起的"。 */
|
|
85
|
+
function authKeyTag(authKey) {
|
|
86
|
+
const s = String(authKey ?? "").trim();
|
|
87
|
+
return s ? createHash("sha256").update(s).digest("hex").slice(0, 12) : "";
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function readRendezvous(key) {
|
|
50
91
|
try {
|
|
51
|
-
const raw = JSON.parse(readFileSync(rendezvousPath(), "utf8"));
|
|
92
|
+
const raw = JSON.parse(readFileSync(rendezvousPath(key), "utf8"));
|
|
52
93
|
if (raw && typeof raw.port === "number" && typeof raw.pid === "number") return raw;
|
|
53
94
|
} catch {
|
|
54
95
|
/* 没有或坏了,当作没有 */
|
|
@@ -56,16 +97,18 @@ function readRendezvous() {
|
|
|
56
97
|
return null;
|
|
57
98
|
}
|
|
58
99
|
|
|
59
|
-
function writeRendezvous(info) {
|
|
60
|
-
|
|
61
|
-
|
|
100
|
+
function writeRendezvous(key, info) {
|
|
101
|
+
const dir = channelDir(key);
|
|
102
|
+
mkdirSync(dir, { recursive: true });
|
|
103
|
+
const target = rendezvousPath(key);
|
|
104
|
+
const tmp = `${target}.tmp`;
|
|
62
105
|
writeFileSync(tmp, JSON.stringify(info, null, 2), { mode: 0o600 });
|
|
63
|
-
renameSync(tmp,
|
|
106
|
+
renameSync(tmp, target);
|
|
64
107
|
}
|
|
65
108
|
|
|
66
|
-
function clearRendezvous() {
|
|
109
|
+
function clearRendezvous(key) {
|
|
67
110
|
try {
|
|
68
|
-
unlinkSync(rendezvousPath());
|
|
111
|
+
unlinkSync(rendezvousPath(key));
|
|
69
112
|
} catch {
|
|
70
113
|
/* 本来就没有 */
|
|
71
114
|
}
|
|
@@ -101,11 +144,33 @@ export function toHostname(raw) {
|
|
|
101
144
|
return name;
|
|
102
145
|
}
|
|
103
146
|
|
|
147
|
+
/**
|
|
148
|
+
* 连接参数指纹:授权码 / 控制服务器 / 节点名。
|
|
149
|
+
*
|
|
150
|
+
* 上层用它判断"配置变了没有":没变就复用活着的节点,变了才重建 sidecar。
|
|
151
|
+
* 只输出哈希,不泄露授权码明文。
|
|
152
|
+
*/
|
|
153
|
+
export function channelSignature(channel = {}) {
|
|
154
|
+
return createHash("sha256")
|
|
155
|
+
.update(
|
|
156
|
+
JSON.stringify([
|
|
157
|
+
String(channel.authKey ?? "").trim(),
|
|
158
|
+
String(channel.controlUrl ?? "").trim(),
|
|
159
|
+
String(channel.hostname ?? "").trim(),
|
|
160
|
+
]),
|
|
161
|
+
)
|
|
162
|
+
.digest("hex")
|
|
163
|
+
.slice(0, 12);
|
|
164
|
+
}
|
|
165
|
+
|
|
104
166
|
/**
|
|
105
167
|
* 一个 tsnet 节点的句柄。可以自己拉起 sidecar,也可以复用它人已拉起的。
|
|
106
168
|
*/
|
|
107
169
|
export class TsnetNode extends EventEmitter {
|
|
108
|
-
#
|
|
170
|
+
#channel;
|
|
171
|
+
#key;
|
|
172
|
+
#label;
|
|
173
|
+
#signature;
|
|
109
174
|
#log;
|
|
110
175
|
#warn;
|
|
111
176
|
#child = null;
|
|
@@ -121,20 +186,64 @@ export class TsnetNode extends EventEmitter {
|
|
|
121
186
|
#forwards = new Set();
|
|
122
187
|
#hostname;
|
|
123
188
|
|
|
124
|
-
|
|
189
|
+
/**
|
|
190
|
+
* @param channel 通道描述:{ id, label, authKey, controlUrl, hostname, downloadSources }
|
|
191
|
+
* 也兼容旧调用方式(直接传 config,单通道 = default)。
|
|
192
|
+
*/
|
|
193
|
+
constructor({ config = {}, channel = null, log = () => {}, warn = () => {}, machineName = "" }) {
|
|
125
194
|
super();
|
|
126
|
-
this.#config = config;
|
|
127
195
|
this.#log = log;
|
|
128
196
|
this.#warn = warn;
|
|
129
|
-
|
|
130
|
-
|
|
197
|
+
const ch = channel ?? {
|
|
198
|
+
id: "default",
|
|
199
|
+
label: "Tailscale",
|
|
200
|
+
authKey: config.tsnetAuthKey ?? "",
|
|
201
|
+
controlUrl: config.tsnetControlUrl ?? "",
|
|
202
|
+
hostname: config.tsnetHostname ?? "",
|
|
203
|
+
};
|
|
204
|
+
this.#key = channelKey(ch.id);
|
|
205
|
+
this.#label = String(ch.label ?? "").trim() || this.#key;
|
|
206
|
+
this.#channel = {
|
|
207
|
+
id: this.#key,
|
|
208
|
+
label: this.#label,
|
|
209
|
+
authKey: String(ch.authKey ?? "").trim(),
|
|
210
|
+
controlUrl: String(ch.controlUrl ?? "").trim(),
|
|
211
|
+
hostname: String(ch.hostname ?? "").trim(),
|
|
212
|
+
downloadSources: Array.isArray(ch.downloadSources) ? ch.downloadSources : (config.tsnetDownloadSources ?? []),
|
|
213
|
+
};
|
|
214
|
+
// 机器级节点名:整机一个,不带 profile 后缀。多通道同一台机器在不同 tailnet
|
|
215
|
+
// 里可以同名(互不相干);同一 tailnet 里挂两条通道才会撞名,由用户显式命名。
|
|
216
|
+
this.#hostname = toHostname(this.#channel.hostname || machineName || "dsh-node");
|
|
131
217
|
this.#status.hostname = this.#hostname;
|
|
218
|
+
// 连接参数的指纹:上层用它判断"配置变了没有",没变就复用活着的节点,
|
|
219
|
+
// 不要在用户重试时把等待授权的 sidecar 反复踢掉重来。
|
|
220
|
+
this.#signature = channelSignature(this.#channel);
|
|
132
221
|
}
|
|
133
222
|
|
|
134
223
|
get hostname() {
|
|
135
224
|
return this.#hostname;
|
|
136
225
|
}
|
|
137
226
|
|
|
227
|
+
/** 通道 id(稳定,进文件路径与端点 key)。 */
|
|
228
|
+
get channelKey() {
|
|
229
|
+
return this.#key;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** 通道显示名。 */
|
|
233
|
+
get label() {
|
|
234
|
+
return this.#label;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/** 连接参数指纹(授权码/控制服务器/节点名)。不含明文授权码。 */
|
|
238
|
+
get signature() {
|
|
239
|
+
return this.#signature;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/** 本机在这条 tailnet 上的地址(未连接时为空)。 */
|
|
243
|
+
get selfIpv4() {
|
|
244
|
+
return this.#status.selfIpv4;
|
|
245
|
+
}
|
|
246
|
+
|
|
138
247
|
get phase() {
|
|
139
248
|
return this.#status.phase;
|
|
140
249
|
}
|
|
@@ -156,7 +265,16 @@ export class TsnetNode extends EventEmitter {
|
|
|
156
265
|
} catch (err) {
|
|
157
266
|
binary = { installed: false, path: "", size: 0, error: String(err?.message ?? err) };
|
|
158
267
|
}
|
|
159
|
-
return {
|
|
268
|
+
return {
|
|
269
|
+
...this.#status,
|
|
270
|
+
channelKey: this.#key,
|
|
271
|
+
label: this.#label,
|
|
272
|
+
controlUrl: this.#channel.controlUrl,
|
|
273
|
+
protocol: SIDECAR_PROTOCOL,
|
|
274
|
+
ownsProcess: this.#ownsProcess,
|
|
275
|
+
port: this.#port,
|
|
276
|
+
binary,
|
|
277
|
+
};
|
|
160
278
|
}
|
|
161
279
|
|
|
162
280
|
/** 最近若干条 sidecar 事件,给 UI 做诊断用。 */
|
|
@@ -297,21 +415,29 @@ export class TsnetNode extends EventEmitter {
|
|
|
297
415
|
|
|
298
416
|
/** 尝试接管 rendezvous 里记录的 sidecar;成功返回 true。 */
|
|
299
417
|
async #tryAdopt() {
|
|
300
|
-
const rv = readRendezvous();
|
|
418
|
+
const rv = readRendezvous(this.#key);
|
|
301
419
|
if (!rv) return false;
|
|
302
420
|
if (!pidAlive(rv.pid)) {
|
|
303
|
-
this.#log(`rendezvous 里的 sidecar(pid ${rv.pid})已不在,清理后重新拉起`);
|
|
304
|
-
clearRendezvous();
|
|
421
|
+
this.#log(`[${this.#label}] rendezvous 里的 sidecar(pid ${rv.pid})已不在,清理后重新拉起`);
|
|
422
|
+
clearRendezvous(this.#key);
|
|
305
423
|
return false;
|
|
306
424
|
}
|
|
307
425
|
if (rv.protocol !== SIDECAR_PROTOCOL) {
|
|
308
|
-
this.#log(
|
|
426
|
+
this.#log(`[${this.#label}] 运行中的 sidecar 协议 v${rv.protocol} 与本插件 v${SIDECAR_PROTOCOL} 不一致,重启它`);
|
|
309
427
|
await this.#killStale(rv);
|
|
310
428
|
return false;
|
|
311
429
|
}
|
|
312
430
|
if (rv.hostname && rv.hostname !== this.#hostname) {
|
|
313
431
|
// 机器名改了:旧节点的 MagicDNS 名字已经不对,重启以应用新名字。
|
|
314
|
-
this.#log(`sidecar 节点名 ${rv.hostname} 与当前机器名 ${this.#hostname} 不一致,重启它`);
|
|
432
|
+
this.#log(`[${this.#label}] sidecar 节点名 ${rv.hostname} 与当前机器名 ${this.#hostname} 不一致,重启它`);
|
|
433
|
+
await this.#killStale(rv);
|
|
434
|
+
return false;
|
|
435
|
+
}
|
|
436
|
+
// 授权码换了:旧 sidecar 还在旧 tailnet / 旧账号里,必须重建。
|
|
437
|
+
// 兼容没有 authKeyTag 的老 rendezvous:不确定就不动它(宁可沿用,不要无故踢掉)。
|
|
438
|
+
const tag = authKeyTag(this.#channel.authKey);
|
|
439
|
+
if (rv.authKeyTag && tag && rv.authKeyTag !== tag) {
|
|
440
|
+
this.#log(`[${this.#label}] 授权码已变更,重启 sidecar 以加入新的 tailnet`);
|
|
315
441
|
await this.#killStale(rv);
|
|
316
442
|
return false;
|
|
317
443
|
}
|
|
@@ -326,13 +452,13 @@ export class TsnetNode extends EventEmitter {
|
|
|
326
452
|
this.#rendezvous = rv;
|
|
327
453
|
this.#ownsProcess = false;
|
|
328
454
|
this.#setStatus({ pid: rv.pid });
|
|
329
|
-
this.#log(
|
|
455
|
+
this.#log(`[${this.#label}] 复用已在运行的 sidecar(pid ${rv.pid},端口 ${rv.port})`);
|
|
330
456
|
return true;
|
|
331
457
|
} catch (err) {
|
|
332
458
|
this.#port = prevPort;
|
|
333
459
|
this.#token = prevToken;
|
|
334
|
-
this.#log(`rendezvous 里的 sidecar 无响应(${err.message}),清理后重新拉起`);
|
|
335
|
-
clearRendezvous();
|
|
460
|
+
this.#log(`[${this.#label}] rendezvous 里的 sidecar 无响应(${err.message}),清理后重新拉起`);
|
|
461
|
+
clearRendezvous(this.#key);
|
|
336
462
|
return false;
|
|
337
463
|
}
|
|
338
464
|
}
|
|
@@ -344,7 +470,7 @@ export class TsnetNode extends EventEmitter {
|
|
|
344
470
|
} catch {
|
|
345
471
|
/* 已经没了 */
|
|
346
472
|
}
|
|
347
|
-
clearRendezvous();
|
|
473
|
+
clearRendezvous(this.#key);
|
|
348
474
|
// 等它释放 tailnet 监听,否则紧接着的 bind 会撞端口。
|
|
349
475
|
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
350
476
|
const deadline = Date.now() + 4000;
|
|
@@ -380,7 +506,7 @@ export class TsnetNode extends EventEmitter {
|
|
|
380
506
|
// 命中缓存时它只做一次 stat + 读一个几十字节的标记文件,不联网。
|
|
381
507
|
if (!cacheIsCurrent()) this.#setStatus({ phase: "downloading", error: "" });
|
|
382
508
|
const res = await ensureBinary({
|
|
383
|
-
extraSources: this.#
|
|
509
|
+
extraSources: this.#channel.downloadSources ?? [],
|
|
384
510
|
log: (m) => {
|
|
385
511
|
this.#log(m);
|
|
386
512
|
this.#setStatus({ lastLog: m });
|
|
@@ -389,17 +515,21 @@ export class TsnetNode extends EventEmitter {
|
|
|
389
515
|
});
|
|
390
516
|
this.#setStatus({ downloadPercent: 100, binaryPath: res.path });
|
|
391
517
|
|
|
392
|
-
|
|
518
|
+
const dir = channelStateDir(this.#key);
|
|
519
|
+
mkdirSync(dir, { recursive: true });
|
|
393
520
|
const token = randomBytes(24).toString("hex");
|
|
394
|
-
const args = ["-hostname", this.#hostname, "-dir",
|
|
395
|
-
if (this.#
|
|
521
|
+
const args = ["-hostname", this.#hostname, "-dir", dir, "-token", token];
|
|
522
|
+
if (this.#channel.controlUrl) args.push("-control-url", this.#channel.controlUrl);
|
|
396
523
|
|
|
397
|
-
const authKey =
|
|
524
|
+
const authKey = this.#channel.authKey;
|
|
398
525
|
const env = { ...process.env };
|
|
399
526
|
if (authKey) env.DSH_TSNET_AUTHKEY = authKey; // 走环境变量:macOS 上 ps 能看到命令行,看不到环境
|
|
400
527
|
else delete env.DSH_TSNET_AUTHKEY;
|
|
401
528
|
|
|
402
|
-
this.#log(
|
|
529
|
+
this.#log(
|
|
530
|
+
`[${this.#label}] 拉起 sidecar:${binaryPath()} -hostname ${this.#hostname}` +
|
|
531
|
+
`${authKey ? " (已带授权码)" : " (无授权码,需交互登录)"}`,
|
|
532
|
+
);
|
|
403
533
|
const child = spawn(binaryPath(), args, { stdio: ["pipe", "pipe", "pipe"], env, windowsHide: true });
|
|
404
534
|
this.#child = child;
|
|
405
535
|
this.#ownsProcess = true;
|
|
@@ -436,9 +566,18 @@ export class TsnetNode extends EventEmitter {
|
|
|
436
566
|
this.#port = ev.port;
|
|
437
567
|
// 让上层能直接看到 sidecar 的 pid(诊断和"是否留了孤儿"的判断都要用)
|
|
438
568
|
this.#setStatus({ pid: ev.pid ?? child.pid ?? 0 });
|
|
439
|
-
this.#rendezvous = {
|
|
440
|
-
|
|
441
|
-
|
|
569
|
+
this.#rendezvous = {
|
|
570
|
+
pid: child.pid,
|
|
571
|
+
port: ev.port,
|
|
572
|
+
token,
|
|
573
|
+
hostname: this.#hostname,
|
|
574
|
+
channelKey: this.#key,
|
|
575
|
+
authKeyTag: authKeyTag(authKey),
|
|
576
|
+
protocol: SIDECAR_PROTOCOL,
|
|
577
|
+
startedAt: Date.now(),
|
|
578
|
+
};
|
|
579
|
+
writeRendezvous(this.#key, this.#rendezvous);
|
|
580
|
+
this.#log(`[${this.#label}] sidecar 已就绪(pid ${child.pid},控制端口 ${ev.port})`);
|
|
442
581
|
|
|
443
582
|
child.on("exit", (code, signal) => this.#onExit(code, signal));
|
|
444
583
|
}
|
|
@@ -513,8 +652,8 @@ export class TsnetNode extends EventEmitter {
|
|
|
513
652
|
#onExit(code, signal) {
|
|
514
653
|
if (this.#stopping) return;
|
|
515
654
|
this.#setStatus({ phase: "exited", backendState: "", error: `sidecar 退出(code=${code} signal=${signal})` });
|
|
516
|
-
this.#warn(`sidecar 意外退出(code=${code} signal=${signal})`);
|
|
517
|
-
clearRendezvous();
|
|
655
|
+
this.#warn(`[${this.#label}] sidecar 意外退出(code=${code} signal=${signal})`);
|
|
656
|
+
clearRendezvous(this.#key);
|
|
518
657
|
this.#port = 0;
|
|
519
658
|
this.#child = null;
|
|
520
659
|
|
|
@@ -569,7 +708,7 @@ export class TsnetNode extends EventEmitter {
|
|
|
569
708
|
if (!this.#ownsProcess && !pidAlive(this.#rendezvous?.pid ?? 0)) {
|
|
570
709
|
// 复用的 sidecar 消失了:下次扫描时上层会重新 start()。
|
|
571
710
|
this.#setStatus({ phase: "exited", error: `共享 sidecar 已消失:${err.message}` });
|
|
572
|
-
clearRendezvous();
|
|
711
|
+
clearRendezvous(this.#key);
|
|
573
712
|
this.#port = 0;
|
|
574
713
|
}
|
|
575
714
|
}
|
|
@@ -581,7 +720,7 @@ export class TsnetNode extends EventEmitter {
|
|
|
581
720
|
clearBinary();
|
|
582
721
|
this.#setStatus({ phase: "idle", error: "" });
|
|
583
722
|
return ensureBinary({
|
|
584
|
-
extraSources: this.#
|
|
723
|
+
extraSources: this.#channel.downloadSources ?? [],
|
|
585
724
|
log: (m) => this.#log(m),
|
|
586
725
|
onProgress: (p) => this.#setStatus({ downloadPercent: p.percent }),
|
|
587
726
|
force: true,
|
|
@@ -592,7 +731,7 @@ export class TsnetNode extends EventEmitter {
|
|
|
592
731
|
async relogin() {
|
|
593
732
|
await this.stop({ keepRemote: false });
|
|
594
733
|
try {
|
|
595
|
-
rmSync(
|
|
734
|
+
rmSync(channelStateDir(this.#key), { recursive: true, force: true });
|
|
596
735
|
} catch {
|
|
597
736
|
/* 忽略 */
|
|
598
737
|
}
|
|
@@ -646,7 +785,7 @@ export class TsnetNode extends EventEmitter {
|
|
|
646
785
|
resolve();
|
|
647
786
|
});
|
|
648
787
|
});
|
|
649
|
-
clearRendezvous();
|
|
788
|
+
clearRendezvous(this.#key);
|
|
650
789
|
this.#port = 0;
|
|
651
790
|
this.#child = null;
|
|
652
791
|
}
|
package/llms.txt
CHANGED
|
@@ -26,7 +26,7 @@ Verify after restart: `GET http://127.0.0.1:45917/mesh/v1/health` returns JSON c
|
|
|
26
26
|
|
|
27
27
|
- Sends a JSON heartbeat to UDP multicast `239.255.42.99:45892` every `announceIntervalMs` (default 5000 ms) and listens on the same group.
|
|
28
28
|
- Serves a small HTTP peer API on TCP port 45917 (configurable via `port`).
|
|
29
|
-
- Optionally starts
|
|
29
|
+
- Optionally starts bundled Go sidecars, each running a userspace Tailscale node (tsnet) for one tailnet channel. Multiple channels can run at once: one machine identity, several reachability paths. No TUN device is created, so the host cannot route to `100.x.y.z`; all tailnet traffic is proxied through the matching sidecar. The binary is downloaded per-platform on first use and verified against checksums pinned in the plugin source.
|
|
30
30
|
- Auto-creates an agent session on the receiving machine when a task arrives, then routes that task to a new or reused session.
|
|
31
31
|
- Reports a structured `osInfo` card entry (host) and a `systems[]` array: host first, then environments such as WSL distros on Windows (`kind: "wsl"`, Linux `platform`, distro name/version). Older peers only send `os`, and the UI falls back to parsing it; purely display data, at most 4 systems.
|
|
32
32
|
- On Windows, probes runtimes inside **running** WSL distros (`wsl.exe -d <name> -e sh -lc …`) and reports them as `capabilities.wsl[]`; stopped distros are never started just to probe. Host and WSL runtimes are displayed separately.
|
|
@@ -77,10 +77,11 @@ Override by id in the profile's `cordis.patch.yml`:
|
|
|
77
77
|
| `agentPreset` | string | `"standard"` | Agent preset for auto-created sessions |
|
|
78
78
|
| `permissionPreset` | string | `"workspace-write"` | Permission preset for auto-created sessions |
|
|
79
79
|
| `workspaceRoot` | string | `""` | |
|
|
80
|
-
| `
|
|
81
|
-
| `
|
|
82
|
-
| `
|
|
83
|
-
| `
|
|
80
|
+
| `tsnetChannels` | array | `[]` | Multi-tailnet channels: `{id, label, authKey, controlUrl, hostname, enabled}`. A non-empty list pins the channel set (UI read-only) and wins over the legacy single-channel fields. `id` is the stable sidecar directory / tailnet device identity — always give it explicitly when hand-editing; renaming `label` alone derives a new id and therefore registers a new device |
|
|
81
|
+
| `tsnetEnabled` | boolean | `false` | Legacy single-channel switch; only used when `tsnetChannels` is empty |
|
|
82
|
+
| `tsnetAuthKey` | string | `""` | Legacy single channel. Tailscale auth key; must be reusable |
|
|
83
|
+
| `tsnetControlUrl` | string | `""` | Legacy single channel |
|
|
84
|
+
| `tsnetHostname` | string | `""` | Legacy single channel. The plugin prefixes `dsh-`; do not include it yourself |
|
|
84
85
|
| `tsnetPermissionPreset` | string | `""` | Empty = same as `permissionPreset` |
|
|
85
86
|
| `maxWakesPerThread` | natural | `30` | Per-thread wake cap per window |
|
|
86
87
|
| `threadWindowMs` | natural | `3600000` | Window for the cap above |
|
|
@@ -95,10 +96,16 @@ if you are not signed in yet. The key **must be Reusable**; single-use keys fail
|
|
|
95
96
|
with `invalid key` on the next start. If device approval is enabled in the tailnet,
|
|
96
97
|
tick Pre-approved as well.
|
|
97
98
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
never
|
|
101
|
-
to hunt for it.
|
|
99
|
+
Add a channel in the Mesh panel (name + key, optionally a Headscale control URL), or
|
|
100
|
+
set `tsnetChannels` in config. Keys pasted in the UI are stored in
|
|
101
|
+
`~/.dsh/mesh/state.json` (mode 0600) and never need re-entry. The plugin's UI links to
|
|
102
|
+
the key page directly; do not tell users to hunt for it.
|
|
103
|
+
|
|
104
|
+
One machine can join several tailnets at once. Each channel gets its own sidecar and
|
|
105
|
+
its own state directory; the mesh device identity stays the same, so peers see one
|
|
106
|
+
machine with several tailnet endpoints. Endpoint health is tracked per channel: one
|
|
107
|
+
tailnet going down neither takes the device offline nor affects the other channels.
|
|
108
|
+
Outbound dispatch tries LAN first, then each tailnet channel in order.
|
|
102
109
|
|
|
103
110
|
## Network and security facts
|
|
104
111
|
|
|
@@ -119,7 +126,7 @@ to hunt for it.
|
|
|
119
126
|
|
|
120
127
|
```bash
|
|
121
128
|
npm run check # version consistency + TDZ scan + syntax + loadability
|
|
122
|
-
npm test # = check +
|
|
129
|
+
npm test # = check + regression suites (~280 assertions, network-free)
|
|
123
130
|
npm run selftest:caps # capability collection: sanitising, GPU parsing, retries, claim rules
|
|
124
131
|
npm run selftest:client # client bundle rendering without a browser
|
|
125
132
|
npm run selftest:prompt # task prompt generation
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-plugin-mesh",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.4",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "DSH Mesh:局域网自动发现同网段的 DSH 实例、显示每台机器上报的能力,并双向派发任务;可用内置的用户态 Tailscale 节点跨互联网组网。Discovers other DSH instances on the LAN, shows the capabilities each one reports, and dispatches tasks between machines in both directions, with an optional userspace Tailscale node for links across networks.",
|
|
6
6
|
"license": "MIT",
|
|
@@ -67,7 +67,7 @@
|
|
|
67
67
|
"selftest:diagnostics": "node lib/_selftest-diagnostics.mjs",
|
|
68
68
|
"selftest:osinfo": "node lib/_selftest-osinfo.mjs",
|
|
69
69
|
"test": "npm run check && npm run selftest && npm run selftest:prompt && npm run selftest:pairing && npm run selftest:client && npm run selftest:caps && npm run selftest:release && npm run selftest:reply && npm run selftest:reasoning && npm run selftest:resume && npm run selftest:persist && npm run selftest:layout && npm run selftest:diagnostics && npm run selftest:osinfo",
|
|
70
|
-
"check": "node scripts/check-version.mjs && node scripts/check-tdz.mjs && node --check lib/index.js && node --check lib/layout.js && node --check lib/discovery.js && node --check lib/conversation.js && node --check lib/identity.js && node --check lib/capabilities.js && node --check lib/prompt.js && node --check lib/pairing.js && node --check lib/scan.js && node --check lib/tsnet/download.js && node --check lib/tsnet/manager.js && node --check lib/tsnet/bridge.js && node --check client/client.js && node --check scripts/release-plan.mjs && npm run check:loadable && npm run check:docs",
|
|
70
|
+
"check": "node scripts/check-version.mjs && node scripts/check-tdz.mjs && node --check lib/index.js && node --check lib/layout.js && node --check lib/discovery.js && node --check lib/conversation.js && node --check lib/identity.js && node --check lib/capabilities.js && node --check lib/prompt.js && node --check lib/pairing.js && node --check lib/scan.js && node --check lib/tsnet/download.js && node --check lib/tsnet/manager.js && node --check lib/tsnet/bridge.js && node --check lib/tsnet/channels.js && node --check client/client.js && node --check scripts/release-plan.mjs && npm run check:loadable && npm run check:docs",
|
|
71
71
|
"build:sidecar": "bash scripts/build-sidecar.sh",
|
|
72
72
|
"verify:mirror": "node scripts/verify-lan-mirror.mjs",
|
|
73
73
|
"release:plan": "node scripts/release-plan.mjs",
|