@dsh-mobile/mobile-remote 3.2.4 → 3.2.6
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/CHANGELOG.md +267 -0
- package/CONTRIBUTING.md +3 -1
- package/FAQ.md +4 -0
- package/README.md +25 -18
- package/cordis.patch.yml +10 -2
- package/docs/00-/345/274/200/345/217/221/346/200/273/347/272/262.md +413 -102
- package/docs/01-PRD.md +4 -4
- package/docs/02-architecture.md +2 -2
- package/docs/03-api.md +49 -1
- package/docs/04-security.md +2 -1
- package/docs/05-test-cases.md +7 -7
- package/docs/06-install-run.md +16 -3
- package/docs/07-user-manual.md +23 -1
- package/docs/08-/346/211/251/345/261/225/345/274/200/345/217/221/346/214/207/345/215/227.md +2 -2
- package/docs/09-compatibility.md +3 -3
- package/docs/11-remote-relay-guide.md +85 -0
- package/docs/APP-ENCRYPTION-DESCRIPTION.md +50 -0
- package/docs/APP-ENCRYPTION-DESCRIPTION.pdf +0 -0
- package/docs/APP-REVIEW-NOTES.md +135 -0
- package/docs/BRAND-ASSETS.md +142 -0
- package/docs/PRIVACY-POLICY.en.md +165 -0
- package/docs/PRIVACY-POLICY.md +133 -33
- package/docs/STORE-LAUNCH.md +196 -21
- package/docs/STORE-LISTING.md +150 -0
- package/docs/SUPPORT.md +4 -3
- package/docs/TERMS-OF-SERVICE.en.md +102 -0
- package/docs/TERMS-OF-SERVICE.md +102 -0
- package/lib/account-usage-query.js +278 -0
- package/lib/api-helpers.js +333 -0
- package/lib/client.js +283 -1
- package/lib/config.js +213 -0
- package/lib/host-compat.js +133 -0
- package/lib/http-util.js +69 -0
- package/lib/index.js +249 -2023
- package/lib/push.js +73 -0
- package/lib/rate-limit.js +46 -0
- package/lib/relay/connector.js +300 -0
- package/lib/relay/crypto.js +117 -0
- package/lib/relay/devices.js +138 -0
- package/lib/relay/identity.js +60 -0
- package/lib/relay/mux.js +419 -0
- package/lib/relay/noise.js +178 -0
- package/lib/relay/pairing.js +64 -0
- package/lib/relay/phone.js +114 -0
- package/lib/relay/serve.js +105 -0
- package/lib/rpc-adapter.js +88 -0
- package/lib/session-list.js +77 -0
- package/lib/timeline.js +492 -0
- package/lib/turn-outline.js +289 -0
- package/package.json +7 -1
- package/docs/ARCHITECTURE-CURRENT.md +0 -229
- package/docs/BUILD-ENVIRONMENT.md +0 -169
- package/docs/FEATURE-PARITY.md +0 -195
- package/docs/REA-UNDERSTANDING.md +0 -246
- package/docs/screenshots/01-home.png +0 -0
- package/docs/screenshots/02-notifications.png +0 -0
- package/docs/screenshots/03-sessions.png +0 -0
- package/docs/screenshots/04-chat.png +0 -0
- package/docs/screenshots/05-settings.png +0 -0
- package/docs/screenshots/06-new-session.png +0 -0
- package/docs/screenshots/pgy-appstore.jpg +0 -0
- package/docs/screenshots/pgy-mobile-app.jpg +0 -0
- package/docs/screenshots/pgy-pc-client.jpg +0 -0
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTTP/文本层的纯辅助:路径脱敏、请求头读取、截断、MIME、二维码地址排序、发送回执 TTL、服务端路径归一、问答校验、抓取失败归类。
|
|
3
|
+
* 从 lib/index.js 原样拆出(行为不变)。
|
|
4
|
+
*/
|
|
5
|
+
import { extname } from "node:path";
|
|
6
|
+
|
|
7
|
+
/** 主机绝对路径脱敏(v3.1.5 S7):抹掉 POSIX 多段路径与 Windows 盘符路径。
|
|
8
|
+
* 宿主错误 message 常带工作区/会话/设置文件路径。此前只在服务端日志与推送里用过,
|
|
9
|
+
* 结果 `/files`、`/directories`、`rpcError` 等把主机绝对路径原样回给了客户端——
|
|
10
|
+
* 现在日志、HTTP 错误详情、推送正文统一走这里(原文只进服务端日志)。
|
|
11
|
+
* 刻意保留单段 `/foo`(如 URL 的路径段不会被整段吞掉)。 */
|
|
12
|
+
const REDACT_PATH_PATTERN = /(?:\/[A-Za-z0-9_.-]+){2,}|[A-Za-z]:\\[^\s"']*/g;
|
|
13
|
+
|
|
14
|
+
export function redactPathText(value) {
|
|
15
|
+
return String(value ?? "").replace(REDACT_PATH_PATTERN, "<redacted>");
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* 普通文件下载能力:Range / HEAD / ETag(v3.2.3)。
|
|
20
|
+
*
|
|
21
|
+
* App 用它决定能不能"只取前 256 KiB 预览"和"大文件断点续传";不存在该能力位时
|
|
22
|
+
* 自动退回整文件 GET(旧插件行为)。
|
|
23
|
+
*/
|
|
24
|
+
export const FILE_RANGE_CAPABILITIES = {
|
|
25
|
+
version: 1,
|
|
26
|
+
supported: true,
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/** 读请求头单值文本(防御数组/非字符串)。 */
|
|
30
|
+
export function requestHeaderText(req, name) {
|
|
31
|
+
const raw = req?.headers?.[name];
|
|
32
|
+
const value = Array.isArray(raw) ? raw[0] : raw;
|
|
33
|
+
return typeof value === "string" ? value : "";
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** 截断字符串到上限(超出加省略号),避免移动端流量/渲染膨胀。
|
|
37
|
+
* P2:截断提示本身计入上限——输出总长严格 ≤ max(对齐文档的"≤ N 字符"语义)。 */
|
|
38
|
+
export function clampText(text, max) {
|
|
39
|
+
text = text === undefined || text === null ? "" : String(text);
|
|
40
|
+
if (text.length <= max) return text;
|
|
41
|
+
const suffix = "\n…(已截断)";
|
|
42
|
+
const keep = max - suffix.length;
|
|
43
|
+
return keep > 0 ? `${text.slice(0, keep)}${suffix}` : text.slice(0, max);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** v3.1.2:文件下载 MIME(扩展名映射,兜底 octet-stream)。 */
|
|
47
|
+
const FILE_MIME = {
|
|
48
|
+
".txt": "text/plain; charset=utf-8", ".md": "text/markdown; charset=utf-8", ".json": "application/json",
|
|
49
|
+
".pdf": "application/pdf", ".png": "image/png", ".jpg": "image/jpeg", ".jpeg": "image/jpeg",
|
|
50
|
+
".gif": "image/gif", ".webp": "image/webp", ".zip": "application/zip", ".gz": "application/gzip",
|
|
51
|
+
".doc": "application/msword", ".docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
52
|
+
".xls": "application/vnd.ms-excel", ".xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
53
|
+
".csv": "text/csv; charset=utf-8", ".yaml": "text/yaml; charset=utf-8", ".yml": "text/yaml; charset=utf-8",
|
|
54
|
+
".js": "text/javascript; charset=utf-8", ".ts": "text/plain; charset=utf-8", ".py": "text/plain; charset=utf-8",
|
|
55
|
+
".html": "text/html; charset=utf-8", ".mp4": "video/mp4", ".apk": "application/vnd.android.package-archive",
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
export const fileMimeOf = (name) => FILE_MIME[extname(name).toLowerCase()] ?? "application/octet-stream";
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* 有界并发映射(保持入参顺序)。用于**不能**一次性扇出的上游读取:
|
|
62
|
+
* 逐个 `readTitleSnapshot` 在真实宿主上每次都会自带一轮全语料枚举,
|
|
63
|
+
* `Promise.all(ids)` 会让 N 个枚举同时开跑。
|
|
64
|
+
*/
|
|
65
|
+
export const mapBounded = async (items, width, worker) => {
|
|
66
|
+
const list = [...items];
|
|
67
|
+
if (list.length === 0) return [];
|
|
68
|
+
const results = new Array(list.length);
|
|
69
|
+
let cursor = 0;
|
|
70
|
+
const run = async () => {
|
|
71
|
+
for (;;) {
|
|
72
|
+
const index = cursor;
|
|
73
|
+
cursor += 1;
|
|
74
|
+
if (index >= list.length) return;
|
|
75
|
+
results[index] = await worker(list[index], index);
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
// 评审 note 908:不能用 `Promise.all`——它**首拒即返回**,调用方(以及它背后的折叠串行门)
|
|
79
|
+
// 会在其余 worker 仍在清理时就被释放;重试随即再开一路,旧 3 路 + 新 4 路叠加实测
|
|
80
|
+
// maxActive=7,突破 ≤4 的读取宽度上限。改为等**全部** worker settle 之后再重抛原失败,
|
|
81
|
+
// 使"在途读取"真正受调用方生命周期约束。
|
|
82
|
+
const settled = await Promise.allSettled(Array.from({ length: Math.min(width, list.length) }, run));
|
|
83
|
+
const failed = settled.find((entry) => entry.status === "rejected");
|
|
84
|
+
if (failed) throw failed.reason;
|
|
85
|
+
return results;
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
// ── 宿主 API 代际探测(ADR 0017)────────────────────────────────────────────
|
|
89
|
+
// 宿主不为 API 声明契约版本,插件只能按**结构特征**探测当前是哪一代,而不是查版本号。
|
|
90
|
+
// 下列四处差异都在宿主 0.2.0 一并发生;各自用最贴近该处差异的结构特征判别,
|
|
91
|
+
// 是为了避免"用一个特征的版本号去推断另一个特征"这种脆弱做法。
|
|
92
|
+
// 判别必须是"服务存在但语义不同"也能识别的——这正是能力语义漂移的形态。
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* 二维码地址排序(issue #49)。
|
|
96
|
+
*
|
|
97
|
+
* 默认保持「局域网私有地址优先」:在家扫码必须给手机可达的局域网地址,组网地址
|
|
98
|
+
* (Tailscale CGNAT 100.64/10)排在其后,避免「桌面有 Tailscale、手机没登录 → 扫到黑洞」。
|
|
99
|
+
*
|
|
100
|
+
* 例外:**当前没有任何局域网私有地址**时,把 Tailscale 提到最前。这类机器(校园网 / 热点 /
|
|
101
|
+
* 只剩虚拟网卡)若不这样排,唯一可用的组网地址会被「其他」段压到后面,二维码首选变成不可达地址。
|
|
102
|
+
* 两种情形都按「手机能否直接到达」取最优,而不是按网卡枚举顺序。
|
|
103
|
+
*
|
|
104
|
+
* 排序:0 = 10./192.168.x;1 = 172.16-31;2 = 100.64/10;3 = 其他。
|
|
105
|
+
*/
|
|
106
|
+
export const orderQrAddresses = (ips) => {
|
|
107
|
+
const rankOf = (ip) => {
|
|
108
|
+
const o = String(ip).split(".").map(Number);
|
|
109
|
+
if (o.length !== 4 || o.some((n) => !Number.isFinite(n))) return 3;
|
|
110
|
+
if (o[0] === 10 || (o[0] === 192 && o[1] === 168)) return 0;
|
|
111
|
+
if (o[0] === 172 && o[1] >= 16 && o[1] <= 31) return 1;
|
|
112
|
+
if (o[0] === 100 && o[1] >= 64 && o[1] <= 127) return 2;
|
|
113
|
+
return 3;
|
|
114
|
+
};
|
|
115
|
+
const hasLanPrivate = ips.some((ip) => {
|
|
116
|
+
const rank = rankOf(ip);
|
|
117
|
+
return rank === 0 || rank === 1;
|
|
118
|
+
});
|
|
119
|
+
// 没有局域网私有地址时,把 CGNAT 从 2 提到 0(其余保持原相对次序)
|
|
120
|
+
const adjusted = (ip) => (rankOf(ip) === 2 && !hasLanPrivate ? 0 : rankOf(ip));
|
|
121
|
+
return [...ips].sort((a, b) => adjusted(a) - adjusted(b));
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
/** v3.0.0(热修 07):回执过期判定(顶层纯函数,便于单测)——now 超过 at+ttl 即过期。 */
|
|
125
|
+
export function receiptExpired(at, now, ttl = 15 * 60 * 1000) {
|
|
126
|
+
return now - at > ttl;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** v3.0.0(热修 08):回执**全量**过期/上限清理(顶层纯函数,可单测)——返回是否有删除。
|
|
130
|
+
* 语义对齐 receiptExpired:恰好 TTL 边界不算过期,仅超过才算;超上限按最旧淘汰。 */
|
|
131
|
+
export function pruneReceiptMap(receipts, now = Date.now(), ttl = 15 * 60 * 1000, max = 2000) {
|
|
132
|
+
let changed = false;
|
|
133
|
+
for (const [k, v] of receipts) {
|
|
134
|
+
if (now - v.at > ttl) { receipts.delete(k); changed = true; }
|
|
135
|
+
}
|
|
136
|
+
while (receipts.size > max) { receipts.delete(receipts.keys().next().value); changed = true; }
|
|
137
|
+
return changed;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** v3.1.1(issue #5):把客户端传来的路径按目标平台归一化分隔符。
|
|
141
|
+
* 旧版移动端按 Windows 习惯用 `\` 拼路径(WSL/Linux 上把 `/home` 拼成 `/\home`,
|
|
142
|
+
* readdir 必然 ENOENT);服务端统一换成当前平台分隔符后浏览/建夹/建会话才能命中
|
|
143
|
+
* 真实目录。仅换分隔符:不展开 ..、不解析软链、不改其余字符。
|
|
144
|
+
* platform 参数仅用于单测固定平台;调用点一律用默认值。 */
|
|
145
|
+
export function normalizeServerPath(p, platform = process.platform) {
|
|
146
|
+
if (typeof p !== "string") return p;
|
|
147
|
+
return platform === "win32" ? p.replaceAll("/", "\\") : p.replaceAll("\\", "/");
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* v3.1.5(PR #11 P1):校验手机端提交的问询答案,不信任移动端 UI。
|
|
152
|
+
*
|
|
153
|
+
* 内核 `dsh-tool-ask-user` 直接展开答案字段(`selected: [...answer.selected]`,
|
|
154
|
+
* 见 @deepseek-ai/dsh-tool-ask-user/lib/index.js:107-111),`ctx.userQuestions.ask()`
|
|
155
|
+
* 本身不做任何形状校验(@deepseek-ai/dsh-user-questions/lib/index.js:69-78):
|
|
156
|
+
* `selected` 缺失/为 null 会在内核里抛 TypeError,选项未声明/一问多答则会把
|
|
157
|
+
* 模型收到伪造答案。故在插件边界逐条校验,非法一律 400 且保留 pending 可重试。
|
|
158
|
+
*
|
|
159
|
+
* @returns `{ ok: true }` 或 `{ ok: false, code, detail }`。
|
|
160
|
+
*/
|
|
161
|
+
export function validateQuestionAnswers(questions, answers) {
|
|
162
|
+
if (!Array.isArray(questions) || questions.length === 0) {
|
|
163
|
+
return { ok: false, code: "question-answer-invalid", detail: "问询条目缺少 questions" };
|
|
164
|
+
}
|
|
165
|
+
if (!Array.isArray(answers) || answers.length !== questions.length) {
|
|
166
|
+
return { ok: false, code: "question-answer-invalid", detail: "每个问题必须恰好对应一个答案" };
|
|
167
|
+
}
|
|
168
|
+
const byId = new Map();
|
|
169
|
+
for (const answer of answers) {
|
|
170
|
+
// id 必须是自身属性且不重复(重复 id 会让"每问一答"变成一问多答)
|
|
171
|
+
if (!answer || typeof answer !== "object" || typeof answer.id !== "string" || byId.has(answer.id)) {
|
|
172
|
+
return { ok: false, code: "question-answer-invalid", detail: "答案 id 无效或重复" };
|
|
173
|
+
}
|
|
174
|
+
byId.set(answer.id, answer);
|
|
175
|
+
}
|
|
176
|
+
for (const question of questions) {
|
|
177
|
+
if (!question || typeof question.id !== "string") {
|
|
178
|
+
return { ok: false, code: "question-answer-invalid", detail: "问题 id 无效" };
|
|
179
|
+
}
|
|
180
|
+
const answer = byId.get(question.id);
|
|
181
|
+
if (!answer) return { ok: false, code: "question-answer-invalid", detail: `缺少问题 ${question.id} 的答案` };
|
|
182
|
+
// selected 必须是自身属性且为 string[]:不接受 null/undefined 兜底,
|
|
183
|
+
// 否则内核 `[...answer.selected]` 抛 TypeError(本函数的存在理由之一)
|
|
184
|
+
if (!Object.hasOwn(answer, "selected") || !Array.isArray(answer.selected)) {
|
|
185
|
+
return { ok: false, code: "question-answer-invalid", detail: `问题 ${question.id} 的 selected 无效` };
|
|
186
|
+
}
|
|
187
|
+
const selected = answer.selected;
|
|
188
|
+
let custom = "";
|
|
189
|
+
if (Object.hasOwn(answer, "custom")) {
|
|
190
|
+
if (typeof answer.custom !== "string") {
|
|
191
|
+
return { ok: false, code: "question-answer-invalid", detail: `问题 ${question.id} 的 custom 无效` };
|
|
192
|
+
}
|
|
193
|
+
custom = answer.custom;
|
|
194
|
+
}
|
|
195
|
+
if (selected.some((label) => typeof label !== "string")) {
|
|
196
|
+
return { ok: false, code: "question-answer-invalid", detail: `问题 ${question.id} 的 selected 无效` };
|
|
197
|
+
}
|
|
198
|
+
if (new Set(selected).size !== selected.length) {
|
|
199
|
+
return { ok: false, code: "question-answer-invalid", detail: `问题 ${question.id} 的选项重复` };
|
|
200
|
+
}
|
|
201
|
+
const labels = new Set((Array.isArray(question.options) ? question.options : [])
|
|
202
|
+
.filter((option) => option && typeof option.label === "string")
|
|
203
|
+
.map((option) => option.label));
|
|
204
|
+
if (selected.some((label) => !labels.has(label))) {
|
|
205
|
+
return { ok: false, code: "question-answer-invalid", detail: `问题 ${question.id} 含未声明选项` };
|
|
206
|
+
}
|
|
207
|
+
if (!question.multiSelect && selected.length > 1) {
|
|
208
|
+
return { ok: false, code: "question-answer-invalid", detail: `问题 ${question.id} 只能单选` };
|
|
209
|
+
}
|
|
210
|
+
if (!question.multiSelect && custom.trim() !== "" && selected.length > 0) {
|
|
211
|
+
return { ok: false, code: "question-answer-invalid", detail: `问题 ${question.id} 不能同时提交选项和自定义答案` };
|
|
212
|
+
}
|
|
213
|
+
if (selected.length === 0 && custom.trim() === "") {
|
|
214
|
+
return { ok: false, code: "question-answer-invalid", detail: `问题 ${question.id} 未回答` };
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
return { ok: true };
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* 把网络/请求失败归类为**稳定错误码**(issue #21 评审 BLOCKING 1)。
|
|
222
|
+
*
|
|
223
|
+
* 为什么不回传异常原文:凭据里若含内嵌 CR/LF,原生 fetch 的 Headers 校验会抛出
|
|
224
|
+
* `Headers.append: "Bearer sk-…\n…" is an invalid header value.` —— **消息里带完整密钥**,
|
|
225
|
+
* 而 `redactPathText` 只脱敏主机路径、不脱敏凭据,所以"脱敏后回传异常消息"并不安全。
|
|
226
|
+
* 移动端只需要知道失败**类别**;细节留在服务端日志(那里本就有权限边界)。
|
|
227
|
+
*/
|
|
228
|
+
export const classifyFetchFailure = (err) => {
|
|
229
|
+
const name = typeof err?.name === "string" ? err.name : "";
|
|
230
|
+
const code = typeof err?.code === "string" ? err.code : (typeof err?.cause?.code === "string" ? err.cause.code : "");
|
|
231
|
+
if (name === "TimeoutError" || name === "AbortError") return "timeout";
|
|
232
|
+
if (code === "UND_ERR_CONNECT_TIMEOUT") return "connect-timeout";
|
|
233
|
+
if (code === "ENOTFOUND" || code === "EAI_AGAIN") return "dns";
|
|
234
|
+
if (code === "ECONNREFUSED") return "connection-refused";
|
|
235
|
+
if (code === "ECONNRESET") return "connection-reset";
|
|
236
|
+
if (code === "UND_ERR_SOCKET") return "socket-error";
|
|
237
|
+
// issue #30 评审 WARNING 4:宿主模块已经给出**结构化**失败——拿到 HTTP 响应就**不是**网络不可达,
|
|
238
|
+
// 归到 unreachable 会让用户排查错方向(限流/登录失效去查网络)。只接受 allow-list 的稳定 code
|
|
239
|
+
// 与**校验过的数值** status,仍然不接收任何异常原文。
|
|
240
|
+
if (code === "OPENAI_CODEX_REAUTH_REQUIRED") return "reauth-required";
|
|
241
|
+
const status = Number.isInteger(err?.status)
|
|
242
|
+
? err.status
|
|
243
|
+
: (Number.isInteger(err?.cause?.status) ? err.cause.status : undefined);
|
|
244
|
+
if (status !== undefined && status >= 400 && status <= 599) {
|
|
245
|
+
return status === 429 ? "rate-limited" : "http-error";
|
|
246
|
+
}
|
|
247
|
+
// TypeError 覆盖 Headers/URL 的本地校验失败——**凭据形态问题会落在这里**,
|
|
248
|
+
// 因此这一类绝不能附带原文。
|
|
249
|
+
if (name === "TypeError") return "invalid-request";
|
|
250
|
+
return "unreachable";
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
/** 错误码 → 固定摘要(allow-list,逐字写死,永不包含异常原文或凭据)。 */
|
|
254
|
+
const FAILURE_SUMMARY = {
|
|
255
|
+
timeout: "请求超时",
|
|
256
|
+
"connect-timeout": "连接超时",
|
|
257
|
+
dns: "域名解析失败",
|
|
258
|
+
"connection-refused": "连接被拒绝",
|
|
259
|
+
"connection-reset": "连接被重置",
|
|
260
|
+
"socket-error": "连接异常中断",
|
|
261
|
+
"invalid-request": "请求构造被拒(可能是凭据或地址形态问题)",
|
|
262
|
+
// 结构化失败(issue #30 评审 WARNING 4):已收到 HTTP 响应 / 宿主明确要求重新登录
|
|
263
|
+
"reauth-required": "需重新登录",
|
|
264
|
+
"rate-limited": "请求被限流",
|
|
265
|
+
"http-error": "服务端返回错误",
|
|
266
|
+
unreachable: "网络不可达",
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
/** 供移动端展示的安全失败摘要:稳定错误码 + 固定文案,不含任何异常原文。 */
|
|
270
|
+
export const safeFailureReason = (err) => {
|
|
271
|
+
const code = classifyFetchFailure(err);
|
|
272
|
+
return `${code}(${FAILURE_SUMMARY[code] ?? FAILURE_SUMMARY.unreachable})`;
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* 请求级取消:客户端断开(超时 / 关页面 / 切网络)时,停止服务端仍在进行的扫描。
|
|
277
|
+
*
|
|
278
|
+
* **为什么需要**(issue #20):`/sessions` 的代价与历史语料规模成正比——实测单次请求
|
|
279
|
+
* 72 秒、约 95 CPU-秒、读取 1.24 GB,而客户端通常三十秒就超时走人了。宿主**本来就支持
|
|
280
|
+
* 取消**(`listSessions(signal?)`、`readTitleSnapshots(ids, signal?)`,持久化层还会
|
|
281
|
+
* `signal.throwIfAborted()`),插件此前一处都没传,于是断开的请求照样跑完:
|
|
282
|
+
* 实测**约 86% 的算力与 I/O 花在已经离开的客户端上**,并发重试时还会互相叠加。
|
|
283
|
+
*
|
|
284
|
+
* **为什么在 `res` 的 close 上判 `writableEnded`**:正常完成的响应同样会触发 close,
|
|
285
|
+
* 不判就会把每一次成功响应都当成取消。
|
|
286
|
+
*
|
|
287
|
+
* 监听器刻意**不显式移除**:`res` 是每请求一个的对象,请求结束即随对象回收,不存在
|
|
288
|
+
* 跨请求累积;而在响应之后再触发 abort 也没有任何等待者,是无害的空操作。
|
|
289
|
+
*/
|
|
290
|
+
export const requestAbort = (res) => {
|
|
291
|
+
const ac = new AbortController();
|
|
292
|
+
res.once("close", () => {
|
|
293
|
+
if (!res.writableEnded) ac.abort();
|
|
294
|
+
});
|
|
295
|
+
return ac.signal;
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
/** 插件主体。 */
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* 解析 `Range: bytes=...`(v3.2.3)。
|
|
302
|
+
*
|
|
303
|
+
* 只支持单区间(多区间用不上,还容易被拿来放大响应);越界/格式错返回
|
|
304
|
+
* `{ invalid: true }`,调用方回 416 并带 `Content-Range: bytes * /size`。
|
|
305
|
+
*/
|
|
306
|
+
export const parseByteRange = (value, size) => {
|
|
307
|
+
const text = String(value ?? "").trim();
|
|
308
|
+
if (text === "") return null;
|
|
309
|
+
const match = /^bytes=(\d*)-(\d*)$/.exec(text);
|
|
310
|
+
if (!match) return { invalid: true };
|
|
311
|
+
const [, startText, endText] = match;
|
|
312
|
+
if (startText === "" && endText === "") return { invalid: true };
|
|
313
|
+
if (startText === "") {
|
|
314
|
+
const suffix = Number(endText);
|
|
315
|
+
if (!Number.isSafeInteger(suffix) || suffix <= 0) return { invalid: true };
|
|
316
|
+
return { start: Math.max(0, size - suffix), end: size - 1 };
|
|
317
|
+
}
|
|
318
|
+
const start = Number(startText);
|
|
319
|
+
if (!Number.isSafeInteger(start) || start >= size) return { invalid: true };
|
|
320
|
+
const end = endText === "" ? size - 1 : Number(endText);
|
|
321
|
+
if (!Number.isSafeInteger(end) || end < start) return { invalid: true };
|
|
322
|
+
return { start, end: Math.min(end, size - 1) };
|
|
323
|
+
};
|
|
324
|
+
/** `If-None-Match` 是否命中当前 ETag(支持 `*` 与逗号列表)。 */
|
|
325
|
+
export const ifNoneMatch = (req, etag) => {
|
|
326
|
+
if (!etag) return false;
|
|
327
|
+
const raw = requestHeaderText(req, "if-none-match");
|
|
328
|
+
if (raw === "") return false;
|
|
329
|
+
return raw
|
|
330
|
+
.split(",")
|
|
331
|
+
.map((part) => part.trim())
|
|
332
|
+
.some((part) => part === "*" || part === etag);
|
|
333
|
+
};
|
package/lib/client.js
CHANGED
|
@@ -23,7 +23,6 @@ window.__ModuleLoader__.load({
|
|
|
23
23
|
exports.inject = ["slots"];
|
|
24
24
|
|
|
25
25
|
var QR_CONFIG = "/m/api/qr-config";
|
|
26
|
-
var QR_IMAGE = "/m/qr.png";
|
|
27
26
|
|
|
28
27
|
var rowStyle = {
|
|
29
28
|
display: "flex",
|
|
@@ -147,6 +146,288 @@ window.__ModuleLoader__.load({
|
|
|
147
146
|
}
|
|
148
147
|
exports.buildQrPayload = buildQrPayload;
|
|
149
148
|
|
|
149
|
+
// ── 远程访问(端到端加密中继)──────────────────────────────────────────────
|
|
150
|
+
// 数据来自 /m/api/relay/*(与 qr-config 一样仅限电脑本机桌面访问)。下面这几个是不碰 DOM 的纯函数,
|
|
151
|
+
// 导出供测试直接调用。
|
|
152
|
+
|
|
153
|
+
var RELAY_API = "/m/api/relay";
|
|
154
|
+
var LINK_LABELS = { online: "已连接", connecting: "连接中…", offline: "离线,自动重试中", idle: "未启动" };
|
|
155
|
+
|
|
156
|
+
/** 中继连接状态的中文说明。 */
|
|
157
|
+
function relayLinkLabel(link) {
|
|
158
|
+
var base = LINK_LABELS[link && link.state] || "未知";
|
|
159
|
+
return link && link.state === "offline" && link.lastError ? base + "(" + link.lastError + ")" : base;
|
|
160
|
+
}
|
|
161
|
+
/** 配对码剩余时间 → "4:59";已过期返回 null。 */
|
|
162
|
+
function formatCountdown(ms) {
|
|
163
|
+
if (!(ms > 0)) return null;
|
|
164
|
+
var total = Math.ceil(ms / 1000);
|
|
165
|
+
var sec = total % 60;
|
|
166
|
+
return Math.floor(total / 60) + ":" + (sec < 10 ? "0" : "") + sec;
|
|
167
|
+
}
|
|
168
|
+
/** 「N 分钟前」。 */
|
|
169
|
+
function relativeTime(ts, now) {
|
|
170
|
+
if (!ts) return "从未连接";
|
|
171
|
+
var s = Math.max(0, Math.round((now - ts) / 1000));
|
|
172
|
+
if (s < 60) return "刚刚";
|
|
173
|
+
if (s < 3600) return Math.floor(s / 60) + " 分钟前";
|
|
174
|
+
if (s < 86400) return Math.floor(s / 3600) + " 小时前";
|
|
175
|
+
return Math.floor(s / 86400) + " 天前";
|
|
176
|
+
}
|
|
177
|
+
/** 未启用时给用户的配置片段。 */
|
|
178
|
+
var RELAY_SNIPPET = [
|
|
179
|
+
"- insert:",
|
|
180
|
+
" - id: mobile-remote",
|
|
181
|
+
" name: dsh-mobile-remote",
|
|
182
|
+
" config:",
|
|
183
|
+
" relay:",
|
|
184
|
+
" enabled: true",
|
|
185
|
+
" urls:",
|
|
186
|
+
" - wss://<你部署的中继域名>",
|
|
187
|
+
].join("\n");
|
|
188
|
+
/**
|
|
189
|
+
* 中继 3.2.6 起默认开启(随包补丁与 schema 默认值都指向 wss://dsh.claud.ing),面板正常出现。
|
|
190
|
+
* 如果用户显式关闭中继(relay.enabled: false),默认不展示(advertiseWhenOff=false);
|
|
191
|
+
* 要恢复「未启用 → 怎么启用」的引导,把它改成 true。做成对象是为了测试可以切换;生产代码只读。
|
|
192
|
+
*/
|
|
193
|
+
var relayUi = { advertiseWhenOff: false };
|
|
194
|
+
exports.relayLinkLabel = relayLinkLabel;
|
|
195
|
+
exports.formatCountdown = formatCountdown;
|
|
196
|
+
exports.relativeTime = relativeTime;
|
|
197
|
+
exports.RELAY_SNIPPET = RELAY_SNIPPET;
|
|
198
|
+
exports.relayUi = relayUi;
|
|
199
|
+
|
|
200
|
+
/** 调用中继管理端点;失败时抛出带 .code 的 Error(detail 优先作为文案)。 */
|
|
201
|
+
function relayCall(path, method, body) {
|
|
202
|
+
return fetch(RELAY_API + path, {
|
|
203
|
+
method: method,
|
|
204
|
+
headers: { accept: "application/json", "content-type": "application/json" },
|
|
205
|
+
cache: "no-store",
|
|
206
|
+
body: body === undefined ? undefined : JSON.stringify(body),
|
|
207
|
+
}).then(function (res) {
|
|
208
|
+
return res
|
|
209
|
+
.json()
|
|
210
|
+
.catch(function () {
|
|
211
|
+
return {};
|
|
212
|
+
})
|
|
213
|
+
.then(function (json) {
|
|
214
|
+
if (!res.ok || json.ok === false) {
|
|
215
|
+
var err = new Error(String(json.detail || json.error || "HTTP " + res.status));
|
|
216
|
+
err.code = json.error;
|
|
217
|
+
throw err;
|
|
218
|
+
}
|
|
219
|
+
return json;
|
|
220
|
+
});
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
var mutedStyle = { fontSize: "12px", color: "var(--dsw-alias-label-tertiary, #aaa)", lineHeight: "1.6" };
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* 「远程访问」面板:手机不在同一 Wi-Fi 时的连接方式。
|
|
228
|
+
* 未启用 → 说明怎么启用;已启用 → 中继连接状态、生成一次性配对二维码、已配对手机列表(可撤销)。
|
|
229
|
+
*/
|
|
230
|
+
function RelayPanel() {
|
|
231
|
+
var stateHook = useState({ status: "loading" });
|
|
232
|
+
var st = stateHook[0];
|
|
233
|
+
var setSt = stateHook[1];
|
|
234
|
+
var pairingHook = useState(null);
|
|
235
|
+
var pairing = pairingHook[0];
|
|
236
|
+
var setPairing = pairingHook[1];
|
|
237
|
+
var nowHook = useState(Date.now());
|
|
238
|
+
var now = nowHook[0];
|
|
239
|
+
var setNow = nowHook[1];
|
|
240
|
+
var busyHook = useState(false);
|
|
241
|
+
var busy = busyHook[0];
|
|
242
|
+
var setBusy = busyHook[1];
|
|
243
|
+
|
|
244
|
+
/** 读状态并刷新界面;成功返回状态对象,失败返回 null。 */
|
|
245
|
+
function load() {
|
|
246
|
+
return relayCall("/status", "GET").then(
|
|
247
|
+
function (data) {
|
|
248
|
+
setSt({ status: "ready", data: data });
|
|
249
|
+
return data;
|
|
250
|
+
},
|
|
251
|
+
function (err) {
|
|
252
|
+
setSt({ status: "error", message: String((err && err.message) || err) });
|
|
253
|
+
return null;
|
|
254
|
+
}
|
|
255
|
+
);
|
|
256
|
+
}
|
|
257
|
+
useEffect(function () {
|
|
258
|
+
var h = null;
|
|
259
|
+
function tick() {
|
|
260
|
+
return load().then(function (data) {
|
|
261
|
+
// 电脑上没启用中继:要改配置并重启 DSH 才会变,没必要再每 4 秒问一次
|
|
262
|
+
if (data && data.enabled === false && h !== null) {
|
|
263
|
+
clearInterval(h);
|
|
264
|
+
h = null;
|
|
265
|
+
}
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
tick();
|
|
269
|
+
h = setInterval(tick, 4000);
|
|
270
|
+
return function () {
|
|
271
|
+
if (h !== null) clearInterval(h);
|
|
272
|
+
};
|
|
273
|
+
}, []);
|
|
274
|
+
// 配对二维码显示期间每秒刷新倒计时;到期自动收起。
|
|
275
|
+
useEffect(
|
|
276
|
+
function () {
|
|
277
|
+
if (!pairing) return undefined;
|
|
278
|
+
var h = setInterval(function () {
|
|
279
|
+
setNow(Date.now());
|
|
280
|
+
}, 1000);
|
|
281
|
+
return function () {
|
|
282
|
+
clearInterval(h);
|
|
283
|
+
};
|
|
284
|
+
},
|
|
285
|
+
[pairing]
|
|
286
|
+
);
|
|
287
|
+
// 手机扫码成功 = 名单里多了一台设备 → 收起二维码(一次性码此时已作废)。
|
|
288
|
+
useEffect(
|
|
289
|
+
function () {
|
|
290
|
+
if (!pairing || st.status !== "ready") return;
|
|
291
|
+
var fresh = (st.data.devices || []).filter(function (d) {
|
|
292
|
+
return pairing.knownIds.indexOf(d.id) < 0;
|
|
293
|
+
});
|
|
294
|
+
if (fresh.length > 0) {
|
|
295
|
+
setPairing(null);
|
|
296
|
+
toast("已配对:" + fresh[0].name);
|
|
297
|
+
}
|
|
298
|
+
},
|
|
299
|
+
[st, pairing]
|
|
300
|
+
);
|
|
301
|
+
|
|
302
|
+
function createPairing() {
|
|
303
|
+
setBusy(true);
|
|
304
|
+
var known = ((st.status === "ready" && st.data.devices) || []).map(function (d) {
|
|
305
|
+
return d.id;
|
|
306
|
+
});
|
|
307
|
+
relayCall("/pairing", "POST", {}).then(
|
|
308
|
+
function (res) {
|
|
309
|
+
setBusy(false);
|
|
310
|
+
setNow(Date.now());
|
|
311
|
+
setPairing({ uri: res.uri, qr: res.qr, expiresAt: res.expiresAt, knownIds: known });
|
|
312
|
+
},
|
|
313
|
+
function (err) {
|
|
314
|
+
setBusy(false);
|
|
315
|
+
toast("生成失败:" + String((err && err.message) || err));
|
|
316
|
+
}
|
|
317
|
+
);
|
|
318
|
+
}
|
|
319
|
+
function revoke(device) {
|
|
320
|
+
var ok = window.confirm("撤销「" + device.name + "」?该手机会立即断开,之后需要重新扫码配对才能再连接。");
|
|
321
|
+
if (!ok) return;
|
|
322
|
+
relayCall("/revoke", "POST", { id: device.id }).then(
|
|
323
|
+
function () {
|
|
324
|
+
toast("已撤销");
|
|
325
|
+
load();
|
|
326
|
+
},
|
|
327
|
+
function (err) {
|
|
328
|
+
toast("撤销失败:" + String((err && err.message) || err));
|
|
329
|
+
}
|
|
330
|
+
);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
var title = jsxRuntime.jsx("div", { style: { fontSize: "13px", fontWeight: 600, margin: "18px 0 2px" }, children: "远程访问(手机不在同一 Wi-Fi 时)" });
|
|
334
|
+
var wrap = function (children) {
|
|
335
|
+
return jsxRuntime.jsx("div", { style: { display: "flex", flexDirection: "column", gap: "6px" }, children: [title].concat(children) });
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
// 还不知道中继有没有启用之前(加载中 / 读不到状态)也不露出面板,免得没启用的人先闪一下「远程访问」
|
|
339
|
+
if (st.status === "loading") return relayUi.advertiseWhenOff ? wrap([jsxRuntime.jsx("div", { style: mutedStyle, children: "加载中…" }, "l")]) : null;
|
|
340
|
+
if (st.status === "error") {
|
|
341
|
+
if (!relayUi.advertiseWhenOff) return null;
|
|
342
|
+
return wrap([jsxRuntime.jsx("div", { style: mutedStyle, children: "无法读取远程访问状态:" + st.message + "(只能在这台电脑的 DSH 桌面端管理)" }, "e")]);
|
|
343
|
+
}
|
|
344
|
+
var data = st.data;
|
|
345
|
+
if (!data.enabled) {
|
|
346
|
+
if (!relayUi.advertiseWhenOff) return null;
|
|
347
|
+
return wrap([
|
|
348
|
+
jsxRuntime.jsx(
|
|
349
|
+
"div",
|
|
350
|
+
{
|
|
351
|
+
style: mutedStyle,
|
|
352
|
+
children:
|
|
353
|
+
"未启用。启用后手机用 4G/5G 或任何 Wi-Fi 都能连到这台电脑:电脑只向外连接中继服务,不开放端口,不需要装 VPN;内容端到端加密,中继看不到。需要先部署中继服务(见仓库 relay/README.md)。",
|
|
354
|
+
},
|
|
355
|
+
"d"
|
|
356
|
+
),
|
|
357
|
+
jsxRuntime.jsx(
|
|
358
|
+
"pre",
|
|
359
|
+
{ style: { margin: 0, padding: "10px 12px", borderRadius: "8px", background: "rgba(128,128,128,.12)", fontSize: "12px", overflowX: "auto" }, children: RELAY_SNIPPET },
|
|
360
|
+
"s"
|
|
361
|
+
),
|
|
362
|
+
jsxRuntime.jsx("div", { style: { display: "flex" }, children: jsxRuntime.jsx("button", { style: btnStyle, onClick: function () { copyText(RELAY_SNIPPET); }, children: "复制配置" }) }, "c"),
|
|
363
|
+
jsxRuntime.jsx("div", { style: mutedStyle, children: "把它合并进 cordis.patch.yml 里 mobile-remote 的 config 后重启 DSH。" }, "h"),
|
|
364
|
+
]);
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
var remaining = pairing ? pairing.expiresAt - now : 0;
|
|
368
|
+
var countdown = formatCountdown(remaining);
|
|
369
|
+
var rows = [];
|
|
370
|
+
(data.relays || []).forEach(function (l) {
|
|
371
|
+
rows.push(
|
|
372
|
+
jsxRuntime.jsx(
|
|
373
|
+
"div",
|
|
374
|
+
{
|
|
375
|
+
style: rowStyle,
|
|
376
|
+
children: [
|
|
377
|
+
jsxRuntime.jsx("span", { style: { wordBreak: "break-all", fontFamily: "monospace", fontSize: "12.5px" }, children: l.url }),
|
|
378
|
+
jsxRuntime.jsx("span", { style: { marginLeft: "auto", flex: "none", color: l.state === "online" ? "var(--dsw-alias-text-success, #30a46c)" : "var(--dsw-alias-label-secondary, #888)" }, children: relayLinkLabel(l) }),
|
|
379
|
+
],
|
|
380
|
+
},
|
|
381
|
+
"r:" + l.url
|
|
382
|
+
)
|
|
383
|
+
);
|
|
384
|
+
});
|
|
385
|
+
rows.push(
|
|
386
|
+
jsxRuntime.jsx(
|
|
387
|
+
"div",
|
|
388
|
+
{
|
|
389
|
+
style: { display: "flex", flexDirection: "column", alignItems: "center", gap: "10px", padding: "12px 0" },
|
|
390
|
+
children:
|
|
391
|
+
pairing && countdown
|
|
392
|
+
? [
|
|
393
|
+
jsxRuntime.jsx("img", { src: pairing.qr, alt: "配对二维码", style: { width: 216, height: 216, borderRadius: "12px", background: "#fff", padding: 8 } }, "img"),
|
|
394
|
+
jsxRuntime.jsx("div", { style: mutedStyle, children: "手机 App → 添加电脑 → 扫码配对。一次性," + countdown + " 后失效。" }, "t"),
|
|
395
|
+
jsxRuntime.jsx("div", { style: { display: "flex", gap: "8px" }, children: [
|
|
396
|
+
jsxRuntime.jsx("button", { style: btnStyle, onClick: function () { copyText(pairing.uri); }, children: "复制配对链接" }, "cp"),
|
|
397
|
+
jsxRuntime.jsx("button", { style: btnStyle, onClick: function () { setPairing(null); }, children: "关闭" }, "x"),
|
|
398
|
+
] }, "b"),
|
|
399
|
+
]
|
|
400
|
+
: [
|
|
401
|
+
jsxRuntime.jsx("button", { style: Object.assign({}, btnStyle, { marginLeft: 0, padding: "6px 16px", fontSize: "13px" }), disabled: busy, onClick: createPairing, children: pairing ? "二维码已过期,重新生成" : "生成配对二维码" }, "gen"),
|
|
402
|
+
jsxRuntime.jsx("div", { style: mutedStyle, children: "配对码只能用一次、5 分钟内有效;二维码里没有长期口令。" }, "hint"),
|
|
403
|
+
],
|
|
404
|
+
},
|
|
405
|
+
"pair"
|
|
406
|
+
)
|
|
407
|
+
);
|
|
408
|
+
rows.push(jsxRuntime.jsx("div", { style: { fontSize: "13px", fontWeight: 600, margin: "4px 0 2px" }, children: "已配对的手机(" + (data.devices || []).length + ")" }, "dh"));
|
|
409
|
+
if ((data.devices || []).length === 0) rows.push(jsxRuntime.jsx("div", { style: mutedStyle, children: "还没有。点上面的按钮,用手机扫码即可。" }, "none"));
|
|
410
|
+
(data.devices || []).forEach(function (d) {
|
|
411
|
+
rows.push(
|
|
412
|
+
jsxRuntime.jsx(
|
|
413
|
+
"div",
|
|
414
|
+
{
|
|
415
|
+
style: rowStyle,
|
|
416
|
+
children: [
|
|
417
|
+
jsxRuntime.jsx("span", { children: d.name }),
|
|
418
|
+
jsxRuntime.jsx("span", { style: mutedStyle, children: "最近 " + relativeTime(d.lastSeen, now) }),
|
|
419
|
+
jsxRuntime.jsx("button", { style: btnStyle, onClick: function () { revoke(d); }, children: "撤销" }),
|
|
420
|
+
],
|
|
421
|
+
},
|
|
422
|
+
"d:" + d.id
|
|
423
|
+
)
|
|
424
|
+
);
|
|
425
|
+
});
|
|
426
|
+
rows.push(jsxRuntime.jsx("div", { style: mutedStyle, children: "手机丢了或不再使用:撤销后它会立即断开,其它手机不受影响。" }, "foot"));
|
|
427
|
+
return wrap(rows);
|
|
428
|
+
}
|
|
429
|
+
exports.RelayPanel = RelayPanel;
|
|
430
|
+
|
|
150
431
|
/**
|
|
151
432
|
* 设置页 section:连接移动端设备。
|
|
152
433
|
* 挂载后从 /m/api/qr-config 拉取地址与口令,渲染二维码 + 连接信息。
|
|
@@ -278,6 +559,7 @@ window.__ModuleLoader__.load({
|
|
|
278
559
|
style: { fontSize: "12px", color: "var(--dsw-alias-label-tertiary, #aaa)", marginTop: "6px" },
|
|
279
560
|
children: "二维码包含访问口令,仅在本机显示;请勿截屏转发。App 与手机浏览器(/m 网页)共用同一连接。",
|
|
280
561
|
}),
|
|
562
|
+
jsxRuntime.jsx(RelayPanel, {}),
|
|
281
563
|
],
|
|
282
564
|
});
|
|
283
565
|
}
|