cofluxd 0.4.0 → 0.5.0
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.md +20 -2
- package/cofluxd.mjs +143 -42
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -14,7 +14,7 @@ npm i -g cofluxd
|
|
|
14
14
|
cofluxd # 首次=up(起服务后打印浏览器授权链接),之后=看状态
|
|
15
15
|
cofluxd up # 幂等:零参数即可装/起;已装则按当前配置重装服务并重启
|
|
16
16
|
cofluxd status # 服务器/登记(含"等待授权")/服务/连接状态
|
|
17
|
-
cofluxd doctor #
|
|
17
|
+
cofluxd doctor # 中心网络 + gateway/grant/loopback + daemon 状态分层自检
|
|
18
18
|
cofluxd logs -f # 看 daemon 日志
|
|
19
19
|
cofluxd update # 更新本地 supervisor 二进制并重启(worker 由 server 自动热升级)
|
|
20
20
|
cofluxd down # 停止
|
|
@@ -25,7 +25,25 @@ cofluxd uninstall [--purge] # 卸载(--purge 连二进制/配置/凭证一
|
|
|
25
25
|
|
|
26
26
|
`cofluxd up` 起服务后会打印一个一次性授权链接,在浏览器用已登录的账号打开确认即可(链接可在任意设备打开,包括无头设备),无需先去 web 控制台生成密钥。已登记设备重跑 `up` 不会重新触发授权。
|
|
27
27
|
|
|
28
|
-
|
|
28
|
+
## 本地优先与 doctor
|
|
29
|
+
|
|
30
|
+
desktop web 与 daemon 同机时,terminal/普通 Device RPC 优先连接本机固定 gateway(默认
|
|
31
|
+
`127.0.0.1:8788`);失败会自动走中心 opaque relay。远端访问始终可走 relay。`cofluxd doctor` 把两条
|
|
32
|
+
路径分开诊断:
|
|
33
|
+
|
|
34
|
+
```text
|
|
35
|
+
中心:DNS → TCP → TLS → WebSocket
|
|
36
|
+
本地:gateway bind → 持久 grant/Origin → loopback WebSocket
|
|
37
|
+
状态:daemon → 中心的实际连接状态
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
- 本地项失败:结论是“直连降级”,只影响同机低延迟路径;中心 relay 正常时 daemon 仍在线可用。
|
|
41
|
+
- 中心项失败:已经加载、已经配对且 cached direct 可用的页面仍能控制存活 session;刷新/冷启动不保证。
|
|
42
|
+
- 网络层都通但 daemon 未连接:查看 `cofluxd logs`,通常是认证、版本或服务进程问题。
|
|
43
|
+
|
|
44
|
+
doctor 只读取 gateway store 的结构、grant/Origin 数量和 bind 状态,不打印 browser 私钥、grant id、
|
|
45
|
+
device token 或其它凭证。它的 loopback 检查只做主机侧 WebSocket upgrade;浏览器自身的 LNA/permission
|
|
46
|
+
仍以 Chrome/Safari/Firefox 页面实测为准。
|
|
29
47
|
|
|
30
48
|
> `onboard`、`reload` 命令已移除:onboard 并入零参数 `up`,reload 并入幂等化后的 `up`(重跑 `up` 即按 settings.json 重装服务并重启)。
|
|
31
49
|
|
package/cofluxd.mjs
CHANGED
|
@@ -24,6 +24,7 @@ const LOG_FILE = join(HOME, "daemon.log");
|
|
|
24
24
|
const CRED = join(HOME, "credentials.json");
|
|
25
25
|
const PENDING_AUTH = join(HOME, "pending-auth.json"); // worker 落盘的待授权链接(daemon.authorizePending)
|
|
26
26
|
const CONN_STATE = join(HOME, "conn-state.json"); // worker 落盘的连接态快照(plan 033,见 crates/worker/src/conn_state.rs)
|
|
27
|
+
const LOCAL_GATEWAY_STORE = join(HOME, "local-gateway.json"); // gateway key/origin/grant;doctor 只读结构与数量,绝不打印秘密
|
|
27
28
|
const FDA_STATUS = join(HOME, "fda-status"); // supervisor 启动时探测落盘(仅 macOS,见 crates/supervisor/src/fda.rs)
|
|
28
29
|
const SUP_BIN = join(BIN_DIR, "coflux-supervisor");
|
|
29
30
|
const WRK_BIN = join(BIN_DIR, "coflux-worker");
|
|
@@ -31,6 +32,7 @@ const IS_MAC = platform() === "darwin";
|
|
|
31
32
|
const IS_LINUX = platform() === "linux";
|
|
32
33
|
const PLIST = join(homedir(), "Library", "LaunchAgents", "com.coflux.daemon.plist");
|
|
33
34
|
const UNIT = join(homedir(), ".config", "systemd", "user", "coflux-daemon.service");
|
|
35
|
+
const DEFAULT_LOCAL_GATEWAY_PORT = 8788; // 与 packages/crates protocol 的 LOCAL_GATEWAY_PORT 保持一致
|
|
34
36
|
|
|
35
37
|
const die = (m) => { console.error("✗ " + m); process.exit(1); };
|
|
36
38
|
const run = (cmd, args, opts = {}) => spawnSync(cmd, args, { encoding: "utf8", ...opts });
|
|
@@ -53,6 +55,46 @@ function readPendingAuth() {
|
|
|
53
55
|
function readConnState() {
|
|
54
56
|
try { return JSON.parse(fs.readFileSync(CONN_STATE, "utf8")); } catch { return null; }
|
|
55
57
|
}
|
|
58
|
+
|
|
59
|
+
function readLocalGatewaySummary() {
|
|
60
|
+
let store;
|
|
61
|
+
try { store = JSON.parse(fs.readFileSync(LOCAL_GATEWAY_STORE, "utf8")); }
|
|
62
|
+
catch {
|
|
63
|
+
return {
|
|
64
|
+
ok: false,
|
|
65
|
+
ready: false,
|
|
66
|
+
// JSON.parse 的错误在部分 Node 版本会带原文片段;store 含私钥,诊断输出绝不能回显。
|
|
67
|
+
error: fs.existsSync(LOCAL_GATEWAY_STORE) ? "grant store 无法解析" : "grant store 尚未创建",
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
if (store?.version !== 1 || !Array.isArray(store.origins) || !Array.isArray(store.grants)) {
|
|
71
|
+
return { ok: false, ready: false, error: "grant store 结构/版本无效" };
|
|
72
|
+
}
|
|
73
|
+
const origins = store.origins.filter((origin) => typeof origin === "string" && origin.length > 0);
|
|
74
|
+
const grants = store.grants.filter((grant) => grant && typeof grant.origin === "string" && grant.origin.length > 0);
|
|
75
|
+
const origin = grants.find((grant) => origins.includes(grant.origin))?.origin || origins[0];
|
|
76
|
+
const ready = grants.length > 0 && !!origin;
|
|
77
|
+
const summary = ready
|
|
78
|
+
? `${grants.length} 个持久 grant / ${origins.length} 个 Origin`
|
|
79
|
+
: `${grants.length} 个 grant / ${origins.length} 个 Origin(尚无可用浏览器配对)`;
|
|
80
|
+
return {
|
|
81
|
+
ok: ready,
|
|
82
|
+
ready,
|
|
83
|
+
origin,
|
|
84
|
+
detail: ready ? summary : undefined,
|
|
85
|
+
error: ready ? undefined : summary,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function localGatewayPort() {
|
|
90
|
+
const raw = process.env.COFLUX_LOCAL_GATEWAY_PORT;
|
|
91
|
+
if (raw === undefined || raw === "") return { ok: true, port: DEFAULT_LOCAL_GATEWAY_PORT };
|
|
92
|
+
const port = Number(raw);
|
|
93
|
+
if (!Number.isInteger(port) || port < 1 || port > 65535) {
|
|
94
|
+
return { ok: false, error: `COFLUX_LOCAL_GATEWAY_PORT=${raw} 无法定位固定监听端口` };
|
|
95
|
+
}
|
|
96
|
+
return { ok: true, port };
|
|
97
|
+
}
|
|
56
98
|
const CONN_STATE_LABEL = { connecting: "连接中", connected: "已连接", reconnecting: "重连中" };
|
|
57
99
|
function formatDuration(ms) {
|
|
58
100
|
const s = Math.max(0, Math.round(ms / 1000));
|
|
@@ -315,10 +357,9 @@ function cmdStatus() {
|
|
|
315
357
|
}
|
|
316
358
|
}
|
|
317
359
|
|
|
318
|
-
/* ------------------------------ doctor
|
|
319
|
-
//
|
|
320
|
-
//
|
|
321
|
-
// WS 升级对 server 是一条未认证连接,server 侧 authDeadline 自然回收。
|
|
360
|
+
/* ------------------------------ doctor:中心 + 本地直连分层自检 ------------------------------ */
|
|
361
|
+
// 中心与 loopback 都只做传输层探测,不解析 coflux 协议消息——CLI 保持零协议。中心 WS
|
|
362
|
+
// 成功升级后立即断开;loopback 只验证已持久 Origin 能拿到 101,不发送 browser 私钥或 grant。
|
|
322
363
|
const DOCTOR_TIMEOUT_MS = 5000;
|
|
323
364
|
|
|
324
365
|
function parseServerUrl(serverUrl) {
|
|
@@ -368,12 +409,19 @@ function probeTls(host, port) {
|
|
|
368
409
|
}
|
|
369
410
|
|
|
370
411
|
// 手写一条最小 HTTP/1.1 Upgrade 请求,只看是否拿到 101——不建立真实 WebSocket 帧连接。
|
|
371
|
-
function probeWsUpgrade({ host, port, path, useTls }) {
|
|
412
|
+
function probeWsUpgrade({ host, port, path, useTls, origin }) {
|
|
372
413
|
return new Promise((resolve) => {
|
|
373
414
|
const t0 = Date.now();
|
|
374
415
|
const key = crypto.randomBytes(16).toString("base64");
|
|
375
|
-
const
|
|
376
|
-
const
|
|
416
|
+
const hostHeader = host.includes(":") ? `[${host}]:${port}` : `${host}:${port}`;
|
|
417
|
+
const originHeader = origin ? `Origin: ${origin}\r\n` : "";
|
|
418
|
+
const req = `GET ${path} HTTP/1.1\r\nHost: ${hostHeader}\r\n${originHeader}Connection: Upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: ${key}\r\n\r\n`;
|
|
419
|
+
let settled = false;
|
|
420
|
+
const finish = (r) => {
|
|
421
|
+
if (settled) return;
|
|
422
|
+
settled = true;
|
|
423
|
+
resolve({ ms: Date.now() - t0, ...r });
|
|
424
|
+
};
|
|
377
425
|
const onOpen = (socket) => {
|
|
378
426
|
let buf = "";
|
|
379
427
|
const timer = setTimeout(() => { socket.destroy(); finish({ ok: false, error: `升级响应超时(>${DOCTOR_TIMEOUT_MS}ms)` }); }, DOCTOR_TIMEOUT_MS);
|
|
@@ -404,85 +452,138 @@ function printLayer(name, r) {
|
|
|
404
452
|
console.log(` ${mark} ${name} (${r.ms}ms)${msg ? ` ${msg}` : ""}`);
|
|
405
453
|
}
|
|
406
454
|
|
|
455
|
+
function printLocalLayer(name, r) {
|
|
456
|
+
const mark = r.ok ? "✓" : "⚠";
|
|
457
|
+
const msg = r.ok ? (r.detail ? `→ ${r.detail}` : "") : (r.error || "");
|
|
458
|
+
const elapsed = Number.isFinite(r.ms) ? ` (${r.ms}ms)` : "";
|
|
459
|
+
console.log(` ${mark} ${name}${elapsed}${msg ? ` ${msg}` : ""}`);
|
|
460
|
+
}
|
|
461
|
+
|
|
407
462
|
// 本地事实汇总:服务进程存活、conn-state.json 连接态、凭证有无、FDA。返回连接态三态:
|
|
408
463
|
// "connected" | "not-connected"(有快照但非 connected,如 connecting/reconnecting)
|
|
409
464
|
// | "unknown"(服务未运行,或无快照——daemon 版本较旧还没写、或刚启动)。
|
|
410
465
|
// 三态区分是因为"无快照"不等于"未连接":旧版 worker 不写 conn-state.json,把它当"未连接"
|
|
411
466
|
// 会把"连接态未知"误报成"认证/授权层有问题"(2026-07-23 实操验收发现)。
|
|
412
467
|
function printLocalFacts() {
|
|
413
|
-
console.log("\n
|
|
414
|
-
|
|
468
|
+
console.log("\n Daemon 状态\n ───────────");
|
|
469
|
+
const registered = fs.existsSync(CRED);
|
|
470
|
+
console.log(` 凭证: ${registered ? "已登记" : "未登记"}`);
|
|
415
471
|
const { running, active } = serviceRunningInfo();
|
|
416
472
|
console.log(` 服务: ${active}`);
|
|
417
473
|
let connState = "unknown";
|
|
418
474
|
if (running) {
|
|
419
475
|
const conn = readConnState();
|
|
420
476
|
if (conn?.state && CONN_STATE_LABEL[conn.state]) {
|
|
421
|
-
console.log(`
|
|
477
|
+
console.log(` 中心: ${CONN_STATE_LABEL[conn.state]}`);
|
|
422
478
|
connState = conn.state === "connected" ? "connected" : "not-connected";
|
|
423
479
|
} else {
|
|
424
|
-
console.log("
|
|
480
|
+
console.log(" 中心: (无连接态快照)");
|
|
425
481
|
}
|
|
482
|
+
} else {
|
|
483
|
+
console.log(" 中心: 未探测(服务未运行)");
|
|
426
484
|
}
|
|
427
485
|
if (IS_MAC) console.log(` FDA: ${fdaLabel(readFdaStatus())}`);
|
|
428
486
|
console.log("");
|
|
429
|
-
return connState;
|
|
487
|
+
return { registered, running, connState };
|
|
430
488
|
}
|
|
431
489
|
|
|
432
|
-
function printConclusion(
|
|
433
|
-
|
|
490
|
+
function printConclusion(level, msg) {
|
|
491
|
+
const mark = level === "ok" ? "✓" : level === "warning" ? "⚠" : "✗";
|
|
492
|
+
console.log(` ${mark} ${msg}\n`);
|
|
434
493
|
}
|
|
435
494
|
|
|
436
|
-
async function
|
|
437
|
-
const s = readSettings();
|
|
438
|
-
const serverUrl = s.serverUrl || DEFAULT_SERVER;
|
|
439
|
-
console.log(`\n 连通性自检 —— ${serverUrl}\n ────────────────────────────\n`);
|
|
440
|
-
let target;
|
|
441
|
-
try { target = parseServerUrl(serverUrl); }
|
|
442
|
-
catch (e) { die(`server_url 解析失败: ${serverUrl}(${e.message})`); }
|
|
495
|
+
async function probeCenter(target) {
|
|
443
496
|
const { host, port, useTls } = target;
|
|
444
|
-
|
|
445
497
|
const dnsR = await probeDns(host);
|
|
446
498
|
printLayer("DNS 解析", dnsR);
|
|
447
499
|
if (!dnsR.ok) {
|
|
448
|
-
|
|
449
|
-
printLocalFacts();
|
|
450
|
-
return;
|
|
500
|
+
return { ok: false, reason: "DNS 解析失败——检查网络连接/DNS 配置,或该域名是否可达。" };
|
|
451
501
|
}
|
|
452
502
|
|
|
453
503
|
const tcpR = await probeTcp(host, port);
|
|
454
504
|
printLayer(`TCP 连接 (${host}:${port})`, tcpR);
|
|
455
505
|
if (!tcpR.ok) {
|
|
456
|
-
|
|
457
|
-
printLocalFacts();
|
|
458
|
-
return;
|
|
506
|
+
return { ok: false, reason: "DNS 可解析但 TCP 连不上——防火墙/代理拦截,或目标端口未开放。" };
|
|
459
507
|
}
|
|
460
508
|
|
|
461
509
|
if (useTls) {
|
|
462
510
|
const tlsR = await probeTls(host, port);
|
|
463
511
|
printLayer("TLS 握手", tlsR);
|
|
464
512
|
if (!tlsR.ok) {
|
|
465
|
-
|
|
466
|
-
printLocalFacts();
|
|
467
|
-
return;
|
|
513
|
+
return { ok: false, reason: "TCP 可连但 TLS 握手失败——可能是企业代理 MITM 证书、系统时间错误,或服务端证书问题。" };
|
|
468
514
|
}
|
|
469
515
|
}
|
|
470
516
|
|
|
471
517
|
const wsR = await probeWsUpgrade(target);
|
|
472
518
|
printLayer("WS 升级握手", wsR);
|
|
473
519
|
if (!wsR.ok) {
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
520
|
+
return { ok: false, reason: "网络层通但 WebSocket 升级被拒——可能是反代/负载均衡未正确转发 Upgrade 头,或路径不对。" };
|
|
521
|
+
}
|
|
522
|
+
return { ok: true };
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
async function probeLocalDirect() {
|
|
526
|
+
const portResult = localGatewayPort();
|
|
527
|
+
if (!portResult.ok) {
|
|
528
|
+
printLocalLayer("Gateway bind", { ok: false, error: portResult.error });
|
|
529
|
+
printLocalLayer("Loopback WS", { ok: false, error: "固定 gateway 端口未知,无法探测" });
|
|
530
|
+
const grant = readLocalGatewaySummary();
|
|
531
|
+
printLocalLayer("本地 grant", grant);
|
|
532
|
+
return { ready: false };
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
const bind = await probeTcp("127.0.0.1", portResult.port);
|
|
536
|
+
printLocalLayer(`Gateway bind (127.0.0.1:${portResult.port})`, bind);
|
|
537
|
+
const grant = readLocalGatewaySummary();
|
|
538
|
+
printLocalLayer("本地 grant", grant);
|
|
539
|
+
|
|
540
|
+
let loopback;
|
|
541
|
+
if (!bind.ok) {
|
|
542
|
+
loopback = { ok: false, error: "gateway 未监听,跳过 WS 握手" };
|
|
543
|
+
} else {
|
|
544
|
+
loopback = await probeWsUpgrade({
|
|
545
|
+
host: "127.0.0.1",
|
|
546
|
+
port: portResult.port,
|
|
547
|
+
path: "/device",
|
|
548
|
+
useTls: false,
|
|
549
|
+
// 没有持久 Origin 时仍发一个合法 Origin;403 能区分“coflux gateway 可达但未配对”。
|
|
550
|
+
origin: grant.origin || "http://127.0.0.1",
|
|
551
|
+
});
|
|
477
552
|
}
|
|
553
|
+
printLocalLayer("Loopback WS(主机侧)", loopback);
|
|
554
|
+
return { ready: bind.ok && grant.ready && loopback.ok };
|
|
555
|
+
}
|
|
478
556
|
|
|
479
|
-
|
|
480
|
-
const
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
557
|
+
async function cmdDoctor() {
|
|
558
|
+
const s = readSettings();
|
|
559
|
+
const serverUrl = s.serverUrl || DEFAULT_SERVER;
|
|
560
|
+
console.log(`\n 中心网络 —— ${serverUrl}\n ────────────────────────────`);
|
|
561
|
+
let center;
|
|
562
|
+
try {
|
|
563
|
+
center = await probeCenter(parseServerUrl(serverUrl));
|
|
564
|
+
} catch (error) {
|
|
565
|
+
printLayer("server URL", { ok: false, ms: 0, error: `${serverUrl}(${error.message})` });
|
|
566
|
+
center = { ok: false, reason: "中心地址配置无效。" };
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
console.log("\n 本地直连\n ────────");
|
|
570
|
+
const direct = await probeLocalDirect();
|
|
571
|
+
const facts = printLocalFacts();
|
|
572
|
+
const relayReady = center.ok && facts.running && facts.connState === "connected";
|
|
573
|
+
|
|
574
|
+
if (direct.ready && relayReady) {
|
|
575
|
+
printConclusion("ok", "本地直连与中心 relay 均可用。");
|
|
576
|
+
} else if (!direct.ready && relayReady) {
|
|
577
|
+
printConclusion("warning", "直连降级:中心 relay 仍可用,daemon 不是离线;检查上面的 gateway/grant/loopback 项。");
|
|
578
|
+
} else if (direct.ready && !center.ok) {
|
|
579
|
+
printConclusion("warning", "中心不可达,但本地直连可用;已加载且已配对页面可继续会话,刷新/冷启动不保证。");
|
|
580
|
+
} else if (direct.ready) {
|
|
581
|
+
printConclusion("warning", "本地直连可用;中心网络可达,但 daemon→中心连接未确认,relay 状态未知。");
|
|
582
|
+
} else if (center.ok) {
|
|
583
|
+
printConclusion("warning", "直连降级;中心网络可达,但 daemon→中心连接未确认。不要据此把 daemon 判为离线,查 `cofluxd logs`。");
|
|
584
|
+
} else {
|
|
585
|
+
printConclusion("error", `${center.reason || "中心不可达"} 同时本地直连降级;当前两条路径都未确认可用。`);
|
|
586
|
+
}
|
|
486
587
|
}
|
|
487
588
|
|
|
488
589
|
function cmdLogs(v) {
|
|
@@ -529,7 +630,7 @@ const HELP = `cofluxd —— coflux daemon 管理
|
|
|
529
630
|
cofluxd 首次=up(打印浏览器授权链接),已配置=status
|
|
530
631
|
cofluxd up [flags] 幂等:首次装+起,已装则按当前 settings.json 重装服务并重启
|
|
531
632
|
cofluxd status 服务器/登记(含"等待授权")/服务/连接状态
|
|
532
|
-
cofluxd doctor
|
|
633
|
+
cofluxd doctor 中心网络 + gateway bind/grant/loopback + daemon 状态分层自检
|
|
533
634
|
cofluxd update 更新本地 supervisor 二进制并重启(worker 由 server 自动热升级)
|
|
534
635
|
cofluxd fda [仅 macOS] 引导授予完全磁盘访问权限(避免 PTY 因 TCC 弹窗卡住)
|
|
535
636
|
cofluxd logs [-f] 看 daemon 日志
|