dsh-mobile-plugin 0.5.117 → 0.5.118

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/lib/client.js CHANGED
@@ -1,6 +1,7 @@
1
1
  // 用途:桌面端入口与诊断反馈;打开配置页失败附带稳定原因码,不外发原始错误和访问凭据。
2
2
  // 2026-11-24:两种诊断页都把「完整重启 DSH」摆第一条;打开失败的那一屏补上版本/来源/激活时间/shellChanged。
3
3
  // 2026-11-24:就地反馈附件补 plugin_source / activated_at / shell_changed,便于判断装完没重启。
4
+ // 2026-11-25:会话状态在 DSH 0.2.3 拆成独立服务,绿点同步改为同时兼容独立服务与普通属性两种宿主。
4
5
  /**
5
6
  * 移动端插件提供电脑插件页面目录;标准页面保留宿主上下文并支持直接导航。
6
7
  * 聊天嵌入服务合并顶部栏显示声明,随提供方与消费方生命周期释放。
@@ -337,6 +338,32 @@ window.__ModuleLoader__.load({
337
338
  return out;
338
339
  }
339
340
 
341
+ /**
342
+ * 取桌面端的会话状态表,兼容 DSH 0.2.2 / 0.2.3 两种形状。
343
+ *
344
+ * 0.2.3 把 `sessionStatus` 从 `uiSession` 的普通属性拆成独立服务
345
+ * `uiSession.sessionStatus`,并用 Cordis 的 traceable 代理把属性读取改道到该服务;
346
+ * 官方要求消费方在 inject 里声明这个子服务名,否则属性读取被门禁拒绝。但本包还要
347
+ * 支持 0.2.2 —— 那一代没有这个独立服务,把它写进 inject 会让整条 effect 因依赖
348
+ * 永不满足而不再启动(绿点同步彻底失效)。
349
+ *
350
+ * 所以用 `reflect.get(name, false)` 直取服务:它是 Cordis 自带的「按服务名读取、
351
+ * 不受 inject 门禁约束」入口,两代都在。有独立服务时拿到它;0.2.2 没有该服务、
352
+ * 返回 undefined,再退回普通属性读取。每次 scan 都现取,服务晚到也能自愈。
353
+ * 只用于读快照,不注册、不持有,因此不绕开任何生命周期——注入的仍是 uiSession 本身。
354
+ */
355
+ function readSessionStatusStore(scope, uiSession) {
356
+ if (scope && typeof scope.reflect?.get === 'function') {
357
+ try {
358
+ const service = scope.reflect.get('uiSession.sessionStatus', false);
359
+ if (service && typeof service.getSnapshot === 'function') return service;
360
+ } catch {
361
+ // 老宿主没有这个服务名:落到下面的普通属性。
362
+ }
363
+ }
364
+ return uiSession && uiSession.sessionStatus;
365
+ }
366
+
340
367
  /**
341
368
  * 方向一:桌面端读过了 → 告诉宿主,手机端侧栏灭灯。
342
369
  *
@@ -390,7 +417,7 @@ window.__ModuleLoader__.load({
390
417
  const scan = () => {
391
418
  if (isDisposed()) return;
392
419
  const uiSession = scope.uiSession;
393
- const store = uiSession && uiSession.sessionStatus;
420
+ const store = readSessionStatusStore(scope, uiSession);
394
421
  if (!store || typeof store.getSnapshot !== 'function') return;
395
422
  let snapshot = null;
396
423
  try {
@@ -9,8 +9,13 @@
9
9
  *
10
10
  * 令牌是凭据,所以这条端点走 `guarded`——只有已配对设备拿得到,令牌也绝不进日志。
11
11
  *
12
- * 两个值都现取,不缓存:端口会被配置改,连接服务会随宿主重启换一份实例。任何一环取不到
13
- * 都如实回「不可用」,让 App 退回「请在电脑上打开网页版」;这里抛错只会把一次能力探测
12
+ * 令牌有两条来路:老宿主的 `connection.browserAuth`,以及 0.2.3-alpha.1 起认证拆包后由
13
+ * `webserver/entry-url` 事件同步交回的入口。后者是兼容适配,事件名与字段格式属宿主内部
14
+ * 约定,本仓无法独立证实,待真机验证。
15
+ *
16
+ * 两个值都现取,不缓存:端口会被配置改,连接服务会随宿主重启换一份实例,令牌是否在同一
17
+ * 实例内轮换也无据可查(同步 emit 很便宜,宁可每次现取)。任何一环取不到都如实回
18
+ * 「不可用」,让 App 退回「请在电脑上打开网页版」;这里抛错只会把一次能力探测
14
19
  * 变成一条 500,反而看不出是哪一格缺了。
15
20
  */
16
21
 
@@ -31,17 +36,57 @@ function hostPortOf(ctx) {
31
36
  }
32
37
 
33
38
  /**
34
- * 网页版的一次性登录令牌,挂在连接服务的 `browserAuth` 上。
39
+ * 新宿主兜底:主动派发一次 `webserver/entry-url`,让认证监听器把入口同步填进数组。
40
+ *
41
+ * baseUrl 用已知端口自拼,不调 `webServer.getOrigin()` —— 本仓只用 `webServer.port/.host`,
42
+ * 那个方法是否存在无法证实。事件名、入口的 `url` 字段和 `token` 参数也都是宿主内部约定,
43
+ * 本仓 0 命中;宿主再改版这条兜底会再失效,这是兼容适配不是根因已证。
44
+ *
45
+ * 只认环回且端口就是本插件 web 端口的入口:同一事件会跑所有监听器,别的服务的入口也带
46
+ * 自己的 token,不按 origin/host 过滤就会把凭据串给人家。
47
+ */
48
+ function launchTokenFromEntries(ctx) {
49
+ const port = hostPortOf(ctx);
50
+ const webServer = ctx?.webServer;
51
+ if (port === null || !webServer || typeof ctx?.emit !== 'function') return null;
52
+ const entries = [];
53
+ ctx.emit(webServer, 'webserver/entry-url', `http://127.0.0.1:${port}`, entries);
54
+ if (!Array.isArray(entries)) return null;
55
+ for (const entry of entries) {
56
+ const raw = entry && entry.url;
57
+ if (typeof raw !== 'string') continue;
58
+ let url;
59
+ try {
60
+ url = new URL(raw);
61
+ } catch {
62
+ continue;
63
+ }
64
+ if (url.protocol !== 'http:') continue;
65
+ if (url.hostname !== '127.0.0.1' && url.hostname !== 'localhost') continue;
66
+ if (url.port !== String(port)) continue;
67
+ const token = url.searchParams.get('token');
68
+ if (typeof token === 'string' && token.length > 0) return token;
69
+ }
70
+ return null;
71
+ }
72
+
73
+ /**
74
+ * 网页版的一次性登录令牌。
75
+ *
76
+ * 老宿主挂在连接服务的 `browserAuth` 上;0.2.3-alpha.1 起认证拆到独立包、这个字段被删掉,
77
+ * 就走 `webserver/entry-url` 同步收回(见 `launchTokenFromEntries`)。两条路都给不出才算
78
+ * 这台宿主现在没有令牌。
35
79
  *
36
80
  * 用 `ctx.get('connection')` 而不是把它写进 `inject`:注入一个这个宿主版本里没有的服务,
37
- * 会让整行永远 pending(见 `host-bridge/transport.js` 里 `peerOf` 的注释)。服务不在、
38
- * 字段不在、值不是非空字符串,都算这台宿主现在给不出令牌。
81
+ * 会让整行永远 pending(见 `host-bridge/transport.js` 里 `peerOf` 的注释)。值不是非空字符串
82
+ * 也算取不到。任何异常一律回 null,能力探测不该变成 500。
39
83
  */
40
84
  function launchTokenOf(ctx) {
41
85
  try {
42
86
  const connection = typeof ctx?.get === 'function' ? ctx.get('connection') : null;
43
- const token = connection && connection.browserAuth ? connection.browserAuth.launchToken : null;
44
- return typeof token === 'string' && token.length > 0 ? token : null;
87
+ const legacy = connection && connection.browserAuth ? connection.browserAuth.launchToken : null;
88
+ if (typeof legacy === 'string' && legacy.length > 0) return legacy;
89
+ return launchTokenFromEntries(ctx);
45
90
  } catch {
46
91
  return null;
47
92
  }
@@ -31,6 +31,9 @@ const { probeInstanceId, verifyRelayRegistration } = require('../relay/verify.js
31
31
  const { probeRelayHealth, relayFailureMessage } = require('../relay/relay-probe.js');
32
32
  const { canListen } = require('../relay/port-allocator.cjs');
33
33
  const { lanCandidates, publicCandidates, ipv6Candidates, withManualIpv6Candidate } = require('../relay/lan-network.cjs');
34
+ // 地址分栏校验与面板保存路径共用同一份实现和文案(面板那条路曾只挡类型 / 长度,Tailscale
35
+ // 这类存了也不生效的地址能被默默存下)。
36
+ const { addressSlotProblem } = require('../relay/address-slot.cjs');
34
37
  const { sendJson, readJsonBody } = require('../shared/http-util');
35
38
  const WebSocket = require('ws');
36
39
  const { controlWsUrl } = require('../relay/control.js');
@@ -354,33 +357,6 @@ function createRelayControlHandlers(deps) {
354
357
  };
355
358
  }
356
359
 
357
- /**
358
- * 地址分栏校验:什么地址能进哪个栏。返回一句中文错误,空串表示没问题。
359
- *
360
- * - 优先的局域网地址必须在这台电脑当前检测到的局域网 / 公网候选里(空串 = 清掉,合法)。
361
- * 候选拿不到时(端口没监听 / 局域网关)不硬拦,交给排序层按「不在清单里」自然忽略。
362
- * - 直连地址不能填成某条中转:两者是不同通道,串栏正是「局域网栏出现中转地址」的根源。
363
- */
364
- function addressSlotProblem(patch) {
365
- if (patch.preferredLanAddress) {
366
- const view = addressView();
367
- const candidates = [...(view.lan || []), ...(view.public || [])]
368
- .map((item) => String((item && item.address) || '').trim()).filter(Boolean);
369
- if (candidates.length && !candidates.includes(patch.preferredLanAddress)) {
370
- return '优先的局域网地址不在这台电脑当前检测到的地址里';
371
- }
372
- }
373
- if (patch.manualLanUrl) {
374
- const want = core.normalizeRelayUrl(patch.manualLanUrl);
375
- const relays = [state.relayUrl, state.publicRelayAddress, ...(Array.isArray(state.relayUrls) ? state.relayUrls : [])]
376
- .map((url) => (typeof url === 'string' ? core.normalizeRelayUrl(url) : '')).filter(Boolean);
377
- if (want && relays.includes(want)) {
378
- return '直连地址不能填成中转地址:这两条是不同通道,请把中转填到中转那一栏';
379
- }
380
- }
381
- return '';
382
- }
383
-
384
360
  /** 故障台账摘要:只给最近几条的 kind / 时间 / 一句话,**不带堆栈**。 */
385
361
  function faultsView() {
386
362
  const guard = typeof activeGuard === 'function' ? activeGuard() : null;
@@ -462,8 +438,13 @@ function createRelayControlHandlers(deps) {
462
438
  }
463
439
  // 「什么地址能进哪个栏」在宿主这一侧也核一遍:面板前端会核,但 APP 也可以直接推上来,
464
440
  // 只靠前端等于没核。两类错误都当场拒:优先的局域网地址不在当前检测到的候选里;直连地址
465
- // 填成了某条中转(两条是不同通道,串栏正是这个 bug 的根源)。
466
- const slotProblem = addressSlotProblem(parsed.patch);
441
+ // 填成了某条中转(两条是不同通道,串栏正是这个 bug 的根源)。判据与面板保存路径共用
442
+ // `relay/address-slot.cjs`,避免两条路各写一遍又分叉。
443
+ const slotProblem = addressSlotProblem(
444
+ parsed.patch,
445
+ state,
446
+ typeof lanPortInUse === 'function' ? lanPortInUse() : state.lanPort,
447
+ );
467
448
  if (slotProblem) {
468
449
  return sendJson(res, 400, { ok: false, error: { code: 'bad-request', message: slotProblem } });
469
450
  }