dsh-mobile-plugin 0.5.51 → 0.5.53

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.
Files changed (39) hide show
  1. package/README.md +10 -0
  2. package/dsh-mobile-credentials/lib/credentials.js +34 -2
  3. package/lib/client.js +203 -11
  4. package/lib/impl.js +5 -0
  5. package/lib/mobile/channels.js +18 -21
  6. package/lib/mobile/directory-listing.js +3 -1
  7. package/lib/mobile/events.js +2 -0
  8. package/lib/mobile/host-bridge/endpoints.js +2 -1
  9. package/lib/mobile/host-bridge/event-synthesis.js +48 -39
  10. package/lib/mobile/host-web.js +2 -2
  11. package/lib/mobile/model-caps.js +15 -5
  12. package/lib/mobile/model-meta-table.cjs +5 -4
  13. package/lib/mobile/model-meta.js +5 -3
  14. package/lib/mobile/permissions.js +1 -3
  15. package/lib/mobile/stream.js +2 -0
  16. package/lib/mobile/workspaces.js +6 -5
  17. package/lib/panel/index.html +15 -15
  18. package/lib/relay/lan-proxy.js +15 -8
  19. package/lib/remote/browser-notice.cjs +118 -0
  20. package/lib/remote/controller.cjs +212 -0
  21. package/lib/remote/crypto.cjs +197 -0
  22. package/lib/remote/host-control.cjs +59 -0
  23. package/lib/remote/host.cjs +226 -0
  24. package/lib/remote/local-headers.cjs +102 -0
  25. package/lib/remote/local-upstream.cjs +144 -0
  26. package/lib/remote/local.cjs +144 -0
  27. package/lib/remote/network.cjs +42 -0
  28. package/lib/remote/router.cjs +194 -0
  29. package/lib/remote/routes.cjs +73 -0
  30. package/lib/remote/runtime.cjs +36 -0
  31. package/lib/remote/store.cjs +60 -0
  32. package/lib/remote/transport.cjs +184 -0
  33. package/lib/remote/tunnel-helper.cjs +167 -0
  34. package/lib/remote/tunnels.cjs +190 -0
  35. package/lib/routes/mobile-api.js +7 -2
  36. package/lib/shared/route-order.cjs +202 -0
  37. package/lib/tailcat/session.cjs +31 -36
  38. package/package.json +7 -6
  39. package/lib/mobile/perf-trace.js +0 -142
@@ -2,7 +2,7 @@
2
2
 
3
3
  /**
4
4
  * 直连隧道这一整套东西的宿主侧会话:拉起助手进程、把它的身份存进 relay.json、
5
- * 开配对窗口、同步白名单、给面板一份状态。
5
+ * 开配对窗口、同步白名单、给面板一份状态;每次取码等待独立结束并回收定时器。
6
6
  *
7
7
  * 只做「把助手和插件的存储接起来」,不掺 HTTP、不认识路由:路由那边拿 `status()` 和
8
8
  * `openPairingWindow()` 就行,面板那边同理。这样它能被单测直接驱动(假助手进程即可)。
@@ -89,30 +89,34 @@ function createTailcatSession(options) {
89
89
  }
90
90
  }
91
91
 
92
+ function settlePairingWindow(value) {
93
+ const current = pendingWindow;
94
+ if (!current) return;
95
+ pendingWindow = null;
96
+ clearTimeout(current.timer);
97
+ current.resolve(value);
98
+ }
99
+
92
100
  function handleEvent(event) {
93
101
  switch (event.event) {
94
102
  case 'ready':
95
103
  lastError = '';
96
104
  break;
97
105
  case 'pairing':
98
- if (pendingWindow) {
99
- const done = pendingWindow;
100
- pendingWindow = null;
101
- done.resolve({
102
- address: event.address,
103
- port: PAIRING_PORT,
104
- key: event.key,
105
- proof: event.proof,
106
- expiresAt: event.expiresAt,
107
- });
108
- }
106
+ settlePairingWindow({
107
+ address: event.address,
108
+ port: PAIRING_PORT,
109
+ key: event.key,
110
+ proof: event.proof,
111
+ expiresAt: event.expiresAt,
112
+ });
109
113
  break;
110
114
  case 'paired': {
111
115
  // 助手已经把手机加进白名单;存下来,并让二维码作废(码里那把钥匙只服务这一次)。
112
116
  const state = snapshot();
113
117
  const allowed = state.allowed.includes(event.client) ? state.allowed : [...state.allowed, event.client];
114
118
  persist({ [STATE.allowed]: allowed });
115
- if (pendingWindowTimeout) clearTimeout(pendingWindowTimeout);
119
+ settlePairingWindow(null);
116
120
  break;
117
121
  }
118
122
  case 'persist': {
@@ -124,27 +128,17 @@ function createTailcatSession(options) {
124
128
  break;
125
129
  }
126
130
  case 'pairingClosed':
127
- if (pendingWindow) {
128
- const done = pendingWindow;
129
- pendingWindow = null;
130
- done.resolve(null);
131
- }
131
+ settlePairingWindow(null);
132
132
  break;
133
133
  case 'error':
134
134
  lastError = event.code;
135
- if (pendingWindow) {
136
- const done = pendingWindow;
137
- pendingWindow = null;
138
- done.resolve(null);
139
- }
135
+ settlePairingWindow(null);
140
136
  break;
141
137
  default:
142
138
  break;
143
139
  }
144
140
  }
145
141
 
146
- let pendingWindowTimeout = null;
147
-
148
142
  /**
149
143
  * 找一次助手。已经找到就沿用(一次成功之后不会再变),没找到就趁入口这次机会再探一遍。
150
144
  *
@@ -179,10 +173,7 @@ function createTailcatSession(options) {
179
173
  }
180
174
 
181
175
  function stop() {
182
- if (pendingWindow) {
183
- pendingWindow.resolve(null);
184
- pendingWindow = null;
185
- }
176
+ settlePairingWindow(null);
186
177
  const current = host;
187
178
  host = null;
188
179
  if (current) return current.close();
@@ -225,14 +216,18 @@ function createTailcatSession(options) {
225
216
  const promise = new Promise(resolve => {
226
217
  resolveFn = resolve;
227
218
  });
228
- pendingWindow = { promise, resolve: resolveFn };
229
- pendingWindowTimeout = setTimeout(() => {
230
- if (!pendingWindow) return;
231
- pendingWindow = null;
232
- resolveFn(null);
219
+ const current = { promise, resolve: resolveFn, timer: null };
220
+ pendingWindow = current;
221
+ current.timer = setTimeout(() => {
222
+ if (pendingWindow === current) settlePairingWindow(null);
233
223
  }, PAIRING_WINDOW_TIMEOUT_MS);
234
- pendingWindowTimeout.unref?.();
235
- host.pair(ttlSeconds);
224
+ current.timer.unref?.();
225
+ try {
226
+ host.pair(ttlSeconds);
227
+ } catch (error) {
228
+ lastError = error && error.message ? error.message : String(error);
229
+ settlePairingWindow(null);
230
+ }
236
231
  return promise;
237
232
  }
238
233
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-mobile-plugin",
3
- "version": "0.5.51",
3
+ "version": "0.5.53",
4
4
  "description": "Mobile API (/api/mobile/v1) and relay client for the DSH mobile app — desktop profile build (dsh 0.2.x, peers resolved by the host)",
5
5
  "license": "MIT",
6
6
  "icon": "./icon.png",
@@ -48,6 +48,7 @@
48
48
  "lib/**/*.cjs",
49
49
  "lib/**/*.html",
50
50
  "lib/**/package.json",
51
+ "native/**",
51
52
  "dsh-mobile-credentials",
52
53
  "locale/*.json",
53
54
  "icon.png",
@@ -73,11 +74,11 @@
73
74
  "ws": "8.18.0"
74
75
  },
75
76
  "optionalDependencies": {
76
- "dsh-mobile-tailcat-darwin-arm64": "1.1.1",
77
- "dsh-mobile-tailcat-darwin-x64": "1.1.1",
78
- "dsh-mobile-tailcat-linux-arm64": "1.1.1",
79
- "dsh-mobile-tailcat-linux-x64": "1.1.1",
80
- "dsh-mobile-tailcat-win32-x64": "1.1.1"
77
+ "dsh-mobile-tailcat-darwin-arm64": "1.1.2",
78
+ "dsh-mobile-tailcat-darwin-x64": "1.1.2",
79
+ "dsh-mobile-tailcat-linux-arm64": "1.1.2",
80
+ "dsh-mobile-tailcat-linux-x64": "1.1.2",
81
+ "dsh-mobile-tailcat-win32-x64": "1.1.2"
81
82
  },
82
83
  "peerDependencies": {
83
84
  "@deepseek-ai/cordis": ">=4.0.1",
@@ -1,142 +0,0 @@
1
- 'use strict';
2
-
3
- /**
4
- * 操作记录回传:手机把「调试面板 → 操作记录」导出的一段正文交给宿主,宿主原样落到本机磁盘。
5
- *
6
- * **数据只落在用户自己这台电脑上,不出网。** 这条路由不转发给中转、不落任何远端存储:
7
- * 收到什么就写进 `$DSH_HOME/mobile-traces/` 里的一个 JSON 文件,仅此而已。检查「手机上滑得
8
- * 顺不顺」要的是那条设备自己记的时间线,留在本机就够了;把它送上云反而多一份没人要的副本。
9
- *
10
- * 正文(`text`)按原样存,不压成一行:这份文件是给人直接打开看的,转义过的 `\n` 一整片
11
- * 读不出任何东西。
12
- *
13
- * 同目录的 `index.jsonl` 是一份轻量目录(每条一行:文件名 / 收到时刻 / 字节数 / 设备),
14
- * 方便按时间翻;它写不进去不影响这次回传本身 —— 正文已经落盘了。
15
- */
16
-
17
- const fs = require('node:fs');
18
- const path = require('node:path');
19
-
20
- const { dshHome } = require('../shared/paths');
21
- const { sendJson, readJsonBody } = require('../shared/http-util');
22
-
23
- /** 正文上限:4MB(按 UTF-8 字节数算)。一次操作记录是文本时间线,到这个量已经是"记太久"。 */
24
- const MAX_TEXT_BYTES = 4 * 1024 * 1024;
25
- /**
26
- * 请求体上限:比正文宽一档。JSON 里换行 / 引号都要转义,4MB 正文的请求体可能大出几成;
27
- * 留到 12MB 是为了「正文合法但转义撑大了」不被误判成超限,真正的判据仍是 `text` 的字节数。
28
- */
29
- const MAX_BODY_BYTES = 12 * 1024 * 1024;
30
- /** label 进文件名的最长长度。 */
31
- const MAX_LABEL = 40;
32
- /** deviceId 短码取前几位。 */
33
- const DEVICE_SHORT = 8;
34
-
35
- /** 操作记录目录:`$DSH_HOME/mobile-traces`。 */
36
- function tracesDir() {
37
- return path.join(dshHome(), 'mobile-traces');
38
- }
39
-
40
- /** 文件名安全化:只留 `[a-zA-Z0-9_-]`,其它一律变 `_`。 */
41
- function safeSegment(value, fallback) {
42
- const raw = typeof value === 'string' ? value : '';
43
- const cleaned = raw.replace(/[^a-zA-Z0-9_-]/g, '_');
44
- return cleaned || fallback;
45
- }
46
-
47
- /** 本地时间戳,冒号换掉(`:` 在文件名里不该出现):`2026-11-08T23-47-45-123`。 */
48
- function localStamp(date) {
49
- const pad = (n, width = 2) => String(n).padStart(width, '0');
50
- return [
51
- `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`,
52
- `${pad(date.getHours())}-${pad(date.getMinutes())}-${pad(date.getSeconds())}-${pad(date.getMilliseconds(), 3)}`,
53
- ].join('T');
54
- }
55
-
56
- /**
57
- * 这次回传要落的文件名:`<本地时间戳>-<设备短码>-<label>.json`。
58
- *
59
- * @param {{ receivedAt: Date, deviceId?: string, label?: string }} input
60
- */
61
- function traceFileName({ receivedAt, deviceId, label }) {
62
- const device = safeSegment(deviceId, 'unknown').slice(0, DEVICE_SHORT);
63
- const tag = safeSegment(label, 'trace').slice(0, MAX_LABEL);
64
- return `${localStamp(receivedAt)}-${device || 'unknown'}-${tag}.json`;
65
- }
66
-
67
- /**
68
- * 处理 `POST /api/mobile/v1/perf-trace`。
69
- *
70
- * 请求体:`{ deviceId?, label?, startedAt: number, text: string, meta? }`。
71
- * 成功回 `{ ok: true, file, path }`。
72
- *
73
- * @param {object} _ctx 插件 ctx(这条路由用不到)
74
- * @param {object} req node:http IncomingMessage(信封请求的明文已在 `req.body`)
75
- * @param {object} res node:http ServerResponse
76
- */
77
- async function handlePerfTrace(_ctx, req, res) {
78
- const body = await readJsonBody(req, MAX_BODY_BYTES);
79
- if (!body || typeof body !== 'object' || Array.isArray(body)) {
80
- return sendJson(res, 400, { ok: false, error: '请求体不是合法 JSON(或超过 12MB 上限)' });
81
- }
82
-
83
- const text = body.text;
84
- if (typeof text !== 'string' || text.length === 0) {
85
- return sendJson(res, 400, { ok: false, error: 'text 必须是非空字符串' });
86
- }
87
- const bytes = Buffer.byteLength(text, 'utf8');
88
- if (bytes > MAX_TEXT_BYTES) {
89
- return sendJson(res, 413, { ok: false, error: `text 太大:${bytes} 字节,上限 ${MAX_TEXT_BYTES} 字节(4MB)` });
90
- }
91
-
92
- const startedAt = body.startedAt;
93
- if (typeof startedAt !== 'number' || !Number.isFinite(startedAt)) {
94
- return sendJson(res, 400, { ok: false, error: 'startedAt 必须是有限数字(毫秒时间戳)' });
95
- }
96
-
97
- const deviceId = typeof body.deviceId === 'string' ? body.deviceId : '';
98
- const label = typeof body.label === 'string' ? body.label : '';
99
- const meta = body.meta && typeof body.meta === 'object' ? body.meta : null;
100
-
101
- const receivedAt = new Date();
102
- const dir = tracesDir();
103
- const fileName = traceFileName({ receivedAt, deviceId, label });
104
- const filePath = path.join(dir, fileName);
105
-
106
- // 收到的原文 + 几格来源信息。2 空格缩进:这份文件是给人直接翻的。
107
- const payload = {
108
- receivedAt: receivedAt.toISOString(),
109
- deviceId,
110
- label,
111
- startedAt,
112
- meta,
113
- text,
114
- };
115
- const serialized = `${JSON.stringify(payload, null, 2)}\n`;
116
-
117
- try {
118
- fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
119
- fs.writeFileSync(filePath, serialized, { encoding: 'utf8', mode: 0o600 });
120
- } catch (error) {
121
- const reason = error && error.message ? error.message : String(error);
122
- return sendJson(res, 500, { ok: false, error: `写盘失败: ${reason}` });
123
- }
124
-
125
- // 目录索引:一条一行。它只是方便翻,写不进去不影响上面已经落好的正文。
126
- try {
127
- const entry = { file: fileName, receivedAt: receivedAt.toISOString(), bytes: Buffer.byteLength(serialized, 'utf8'), deviceId };
128
- fs.appendFileSync(path.join(dir, 'index.jsonl'), `${JSON.stringify(entry)}\n`, { encoding: 'utf8', mode: 0o600 });
129
- } catch { /* 索引可选,不影响主请求 */ }
130
-
131
- return sendJson(res, 200, { ok: true, file: fileName, path: filePath });
132
- }
133
-
134
- module.exports = {
135
- handlePerfTrace,
136
- tracesDir,
137
- traceFileName,
138
- safeSegment,
139
- MAX_TEXT_BYTES,
140
- MAX_LABEL,
141
- DEVICE_SHORT,
142
- };