dsh-mobile-plugin 0.5.33 → 0.5.35
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 +17 -2
- package/dsh-mobile-credentials/lib/credentials.js +49 -2
- package/extensions/README.md +25 -21
- package/host.js +16 -1
- package/lib/git/pr.js +375 -0
- package/lib/git/read.js +1 -0
- package/lib/host-capabilities.cjs +67 -0
- package/lib/http/routing.js +25 -0
- package/lib/impl.js +34 -26
- package/lib/mirror/history-blob.cjs +184 -0
- package/lib/mirror/pusher.cjs +276 -1
- package/lib/mobile/approval-facts.js +439 -0
- package/lib/mobile/approval-memory.js +199 -0
- package/lib/mobile/approvals.js +596 -30
- package/lib/mobile/auto-review-log.js +160 -0
- package/lib/mobile/auto-review.js +172 -0
- package/lib/mobile/credential-upgrade.js +14 -0
- package/lib/mobile/device-permissions.js +86 -0
- package/lib/mobile/directory-browse.js +23 -8
- package/lib/mobile/events.js +9 -1
- package/lib/mobile/extension-catalog.js +106 -44
- package/lib/mobile/extension-data.js +6 -0
- package/lib/mobile/file-ops.js +89 -76
- package/lib/mobile/git.js +108 -2
- package/lib/mobile/live-steps.js +107 -4
- package/lib/mobile/mail.js +621 -0
- package/lib/mobile/monitor.js +6 -0
- package/lib/mobile/notification-prefs.js +205 -0
- package/lib/mobile/pairing.js +15 -2
- package/lib/mobile/permissions.js +433 -0
- package/lib/mobile/project-write-queue.js +115 -0
- package/lib/mobile/project-writes.js +61 -0
- package/lib/mobile/push-notify.js +293 -13
- package/lib/mobile/questions.js +20 -3
- package/lib/mobile/recent-failures.js +48 -0
- package/lib/mobile/relay-control.js +5 -0
- package/lib/mobile/session-cache.js +50 -0
- package/lib/mobile/session-last-turn.js +24 -22
- package/lib/mobile/session-new.js +205 -0
- package/lib/mobile/session-send.js +0 -1
- package/lib/mobile/sessions.js +12 -84
- package/lib/mobile/skill-write.js +549 -0
- package/lib/mobile/stats-collect.cjs +33 -3
- package/lib/mobile/stats-ledger.cjs +39 -2
- package/lib/mobile/stream.js +24 -3
- package/lib/mobile/supervisor-broadcast.js +58 -11
- package/lib/mobile/supervisor-tools.js +1 -1
- package/lib/mobile/third-party-extensions.js +158 -0
- package/lib/mobile/uploads.js +30 -14
- package/lib/mobile/worktree-isolation.js +479 -0
- package/lib/mobile/worktree-lifecycle.js +79 -0
- package/lib/p2p/wiring.cjs +41 -16
- package/lib/panel/index.html +15 -15
- package/lib/read-receipt.cjs +89 -5
- package/lib/relay/core.js +25 -1
- package/lib/relay/lan-proxy.js +6 -1
- package/lib/relay/preview.js +21 -1
- package/lib/relay/protocol.js +1 -0
- package/lib/relay/status.js +6 -0
- package/lib/relay/tunnel-forwarder.cjs +178 -0
- package/lib/routes/control-plane.js +35 -1
- package/lib/routes/mobile-api.js +105 -52
- package/lib/shared/via-header.js +15 -2
- package/lib/sidecar-facade-worker.cjs +15 -2
- package/lib/sidecar-facade.cjs +5 -0
- package/lib/tailcat/host-process.cjs +1 -1
- package/lib/tailcat/ingress.cjs +2 -2
- package/lib/tailcat/wiring.cjs +34 -13
- package/package.json +11 -3
- package/assets/pets/README.md +0 -9
- package/assets/pets/deepy-whale/attention.gif +0 -0
- package/assets/pets/deepy-whale/done.gif +0 -0
- package/assets/pets/deepy-whale/error.gif +0 -0
- package/assets/pets/deepy-whale/idle.gif +0 -0
- package/assets/pets/deepy-whale/pack.json +0 -54
- package/assets/pets/deepy-whale/sleeping.gif +0 -0
- package/assets/pets/deepy-whale/working-3.gif +0 -0
- package/assets/pets/deepy-whale/working.gif +0 -0
- package/knowledge/apps/alipay.md +0 -30
- package/knowledge/apps/android-settings.md +0 -30
- package/knowledge/apps/baijiahao.md +0 -30
- package/knowledge/apps/bilibili.md +0 -31
- package/knowledge/apps/boss-zhipin.md +0 -32
- package/knowledge/apps/dedao.md +0 -33
- package/knowledge/apps/douyin.md +0 -32
- package/knowledge/apps/feishu.md +0 -35
- package/knowledge/apps/gmail.md +0 -33
- package/knowledge/apps/jike.md +0 -31
- package/knowledge/apps/kuaishou.md +0 -34
- package/knowledge/apps/linkedin.md +0 -33
- package/knowledge/apps/qqmail.md +0 -30
- package/knowledge/apps/taobao.md +0 -33
- package/knowledge/apps/telegram.md +0 -27
- package/knowledge/apps/toutiao.md +0 -32
- package/knowledge/apps/uber-eats.md +0 -26
- package/knowledge/apps/uber.md +0 -27
- package/knowledge/apps/wechat.md +0 -33
- package/knowledge/apps/weibo.md +0 -31
- package/knowledge/apps/whatsapp.md +0 -27
- package/knowledge/apps/x.md +0 -32
- package/knowledge/apps/xiaohongshu.md +0 -30
- package/knowledge/apps/zhihu.md +0 -31
- package/knowledge/sites/bilibili.com/SITE.md +0 -50
- package/knowledge/sites/bilibili.com/references/view-fields.md +0 -17
- package/knowledge/sites/github.com/SITE.md +0 -51
- package/knowledge/sites/github.com/references/repo-fields.md +0 -13
- package/knowledge/sites/x.com/SITE.md +0 -47
- package/knowledge/sites/xiaohongshu.com/SITE.md +0 -48
- package/knowledge/sites/xiaohongshu.com/references/note-fields.md +0 -18
- package/knowledge/sites/youtube.com/SITE.md +0 -49
- package/knowledge/sites/youtube.com/references/innertube-fields.md +0 -14
- package/knowledge/sites/zhihu.com/SITE.md +0 -47
- package/knowledge/sites/zhihu.com/references/api-fields.md +0 -21
- package/lib/mobile/pet-tools.js +0 -261
- package/lib/mobile/pets.js +0 -755
- package/lib/mobile/phone/access.js +0 -85
- package/lib/mobile/phone/binding.js +0 -247
- package/lib/mobile/phone/confirm.js +0 -189
- package/lib/mobile/phone/evidence.js +0 -228
- package/lib/mobile/phone/gate.js +0 -290
- package/lib/mobile/phone/grounding.js +0 -314
- package/lib/mobile/phone/guard.js +0 -465
- package/lib/mobile/phone/index.js +0 -83
- package/lib/mobile/phone/irreversible.js +0 -226
- package/lib/mobile/phone/knowledge-tools.js +0 -665
- package/lib/mobile/phone/learn.js +0 -329
- package/lib/mobile/phone/pacing.js +0 -173
- package/lib/mobile/phone/redact.js +0 -237
- package/lib/mobile/phone/risk.js +0 -95
- package/lib/mobile/phone/route-hint.js +0 -164
- package/lib/mobile/phone/routes-memory.js +0 -133
- package/lib/mobile/phone/routes.js +0 -279
- package/lib/mobile/phone/service.js +0 -500
- package/lib/mobile/phone/site-commands.js +0 -271
- package/lib/mobile/phone/step-summary.js +0 -201
- package/lib/mobile/phone/stop-summary.js +0 -235
- package/lib/mobile/phone/tool-groups.js +0 -125
- package/lib/mobile/phone/tools.js +0 -2282
- package/lib/mobile/phone/untrusted.js +0 -65
- package/lib/mobile/phone-knowledge.js +0 -317
- package/lib/mobile/wechat-tools.js +0 -257
- package/lib/mobile/wechat.js +0 -514
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 「这一代运行时少挂了什么」的活体台账。
|
|
5
|
+
*
|
|
6
|
+
* 宿主启动时会把官方那几行(提问工具 / 交付文件工具 / 目录浏览后端 / 预设里的子代理列表)
|
|
7
|
+
* 按**运行时**对齐一遍:解析不到同代的那份就维持原样、交给官方装载预检去禁用,或者整行
|
|
8
|
+
* 从预设副本里拿掉。这条路以前只写一行 stderr —— 现象是宿主照样起来、手机照样连上,用户
|
|
9
|
+
* 进了会话才发现「模型说没有这个工具」,而没有任何界面说过一句话。
|
|
10
|
+
*
|
|
11
|
+
* 所以这里把那份结论记下来,由 `/api/mobile/v1/status` 报给手机,App 才能明说
|
|
12
|
+
* 「这几项现在不可用」,而不是让人自己去撞。
|
|
13
|
+
*
|
|
14
|
+
* **同一进程内的内存传递**:`host.js` 与 `lib/impl.js` 是同一个宿主进程里的两段
|
|
15
|
+
* (后者由 loader 挂进来),所以一份模块级记录就够了,不需要落盘、也没有跨进程时序问题。
|
|
16
|
+
* **不要改成写文件**:那会多出一份能和实际装载结果不一致的状态。
|
|
17
|
+
*
|
|
18
|
+
* 记录只增不减:一次启动最多对齐一次,重载代不该把「这一次启动少了什么」擦掉。
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
let report = null
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 记下这一次启动的对齐结论。由 `host.js` 在 `boot()` 之前调用。
|
|
25
|
+
*
|
|
26
|
+
* @param {{ runtimeVersion: string, missing: Array<{ id: string, name: string, reason: string }> }} next
|
|
27
|
+
*/
|
|
28
|
+
function setCapabilityReport(next) {
|
|
29
|
+
report = {
|
|
30
|
+
runtimeVersion: typeof next?.runtimeVersion === 'string' ? next.runtimeVersion : null,
|
|
31
|
+
missing: Array.isArray(next?.missing)
|
|
32
|
+
? next.missing
|
|
33
|
+
.filter((item) => item && typeof item.id === 'string')
|
|
34
|
+
.map((item) => ({
|
|
35
|
+
id: item.id,
|
|
36
|
+
name: typeof item.name === 'string' ? item.name : null,
|
|
37
|
+
// 「没解析到同代的一份」与「这一代运行时里入口本身没了」是两件事:
|
|
38
|
+
// 前者可能换一份就回来,后者只能等上游把入口放回来或者我们改预设。
|
|
39
|
+
reason: item.reason === 'entry-removed' ? 'entry-removed' : 'no-compatible-generation',
|
|
40
|
+
}))
|
|
41
|
+
: [],
|
|
42
|
+
}
|
|
43
|
+
return report
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* 取这一次启动的对齐结论。
|
|
48
|
+
*
|
|
49
|
+
* 没跑过 `host.js` 的对齐(桌面 profile 走的是官方 `dsh web`,不是我们这个无头宿主)时
|
|
50
|
+
* 返回 `null` —— 「没检查过」和「检查过、什么都没少」是两件事,不能都回一个空数组。
|
|
51
|
+
*
|
|
52
|
+
* @returns {{ runtimeVersion: string | null, missing: Array<object> } | null}
|
|
53
|
+
*/
|
|
54
|
+
function readCapabilityReport() {
|
|
55
|
+
return report
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** 只给测试用:清掉记录,模拟一次全新启动。 */
|
|
59
|
+
function resetCapabilityReportForTests() {
|
|
60
|
+
report = null
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
module.exports = {
|
|
64
|
+
setCapabilityReport,
|
|
65
|
+
readCapabilityReport,
|
|
66
|
+
resetCapabilityReportForTests,
|
|
67
|
+
}
|
package/lib/http/routing.js
CHANGED
|
@@ -156,6 +156,12 @@ const readJson = (req) => readJsonBody(req, 256 * 1024).then(body => body || {})
|
|
|
156
156
|
const createGuarded = (ctx, authorized, note = null, extra = {}) => {
|
|
157
157
|
const onAuthorized = typeof extra.onAuthorized === 'function' ? extra.onAuthorized : null
|
|
158
158
|
const envelope = extra.envelope && typeof extra.envelope === 'object' ? extra.envelope : null
|
|
159
|
+
// 按设备权限子集做逐路由校验的钩子(见 lib/mobile/permissions.js)。两个函数由装配层注入,
|
|
160
|
+
// 本文件仍然**不含任何具体路由**(测试钉着这一点):resolve 把 path+method 摘成所需权限位,
|
|
161
|
+
// has 判断这条凭据记录够不够。不给就完全不校验(纯明文测试/老调用方)。
|
|
162
|
+
const permission = extra.permission && typeof extra.permission === 'object' ? extra.permission : null
|
|
163
|
+
const resolvePermission = permission && typeof permission.resolve === 'function' ? permission.resolve : null
|
|
164
|
+
const hasPermission = permission && typeof permission.has === 'function' ? permission.has : null
|
|
159
165
|
return (handler) => async (req, res) => {
|
|
160
166
|
// 鉴权失败要**说清是哪一种**(§3.8):被吊销 / 已过期 / 只是没有 token。
|
|
161
167
|
//
|
|
@@ -183,6 +189,25 @@ const createGuarded = (ctx, authorized, note = null, extra = {}) => {
|
|
|
183
189
|
try { onAuthorized(req) } catch { /* 同上 */ }
|
|
184
190
|
}
|
|
185
191
|
|
|
192
|
+
// 按设备权限子集校验本条路由(见 lib/mobile/permissions.js)。位置定死在「已鉴权、
|
|
193
|
+
// 已挂 record」之后、「解封」之前:越权能直接回**明文 403**,客户端不必解信封就能读懂;
|
|
194
|
+
// 而 401(没登录/被踢/过期)与 403(登录了但缺这一位)必须是两种处置,不能混。
|
|
195
|
+
// BASE 路由 resolve 回 null、老凭据 / 无记录 has 恒真,都自然放行。
|
|
196
|
+
if (resolvePermission) {
|
|
197
|
+
const need = resolvePermission(req.url || req.originalUrl || '', req.method)
|
|
198
|
+
if (need === 'unknown') {
|
|
199
|
+
// 没分类的路由 fail-closed:宁可这条功能报错,也别给它开一个静默的全权口子。
|
|
200
|
+
return sendJson(res, 403, { ok: false, error: { code: 'permission-unclassified', message: '这条接口没有登记所需权限,暂不放行' } })
|
|
201
|
+
}
|
|
202
|
+
if (need && (!hasPermission || !hasPermission(req.__dshRecord, need))) {
|
|
203
|
+
const granted = req.__dshRecord && Array.isArray(req.__dshRecord.permissions) ? req.__dshRecord.permissions.slice() : null
|
|
204
|
+
return sendJson(res, 403, {
|
|
205
|
+
ok: false,
|
|
206
|
+
error: { code: 'permission-denied', message: '这台设备没有执行该操作的权限', required: need, granted },
|
|
207
|
+
})
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
186
211
|
// **被动降级要被发现,不能悄悄变明文**(§8.3(7) 的 `e2ee-required`)。
|
|
187
212
|
//
|
|
188
213
|
// 判据:这枚凭据的记录里有 `deviceKey`(说明它当初是按协议 v2 配的),而这条请求没带
|
package/lib/impl.js
CHANGED
|
@@ -21,6 +21,7 @@ const { registerMobileRoutes } = require('./routes/mobile-api.js')
|
|
|
21
21
|
const { registerControlPlaneRoutes } = require('./routes/control-plane.js')
|
|
22
22
|
const { hostMetadata, createStatusView } = require('./relay/status.js')
|
|
23
23
|
const { authorized, authOutcome, presentedCredential } = require('./mobile/auth.js')
|
|
24
|
+
const permissions = require('./mobile/permissions.js') // 权限位/档位/逐路由所需位(见 lib/mobile/permissions.js)
|
|
24
25
|
const { readRawBody } = require('./shared/e2ee-request')
|
|
25
26
|
const streamRegistry = require('./mobile/stream-registry.js')
|
|
26
27
|
const { viaChannel } = require('./shared/via-header')
|
|
@@ -32,8 +33,8 @@ const { lanUrls, advertisedCandidates, phoneCandidates, ipv6Candidates, withManu
|
|
|
32
33
|
// 配对二维码的载荷:面板与终端命令共用一份,拼装在 `./relay/pairing-payload.cjs`。
|
|
33
34
|
const { pairingPayload, pairingText } = require('./relay/pairing-payload.cjs')
|
|
34
35
|
const { resolvePort } = require('./relay/port-allocator.cjs')
|
|
35
|
-
const { mountTailcat
|
|
36
|
-
const { mountP2P
|
|
36
|
+
const { mountTailcat } = require('./tailcat/wiring.cjs') // 直连隧道:入口 + 落点代理 + 助手会话 + 启停
|
|
37
|
+
const { mountP2P } = require('./p2p/wiring.cjs') // 点对点隧道:助手会话 + 落点代理(连接从隧道里进来,不必开回环门)
|
|
37
38
|
const { createPreviewService } = require('./relay/preview.js')
|
|
38
39
|
// 手机打开「电脑上的网页版」时用的那枚一次性登录令牌(见 mobile/host-web.js)。
|
|
39
40
|
const hostWeb = require('./mobile/host-web.js')
|
|
@@ -42,8 +43,9 @@ const hostWeb = require('./mobile/host-web.js')
|
|
|
42
43
|
// 入口按两条盘上布局分别找(嵌在插件包内 / 与插件包同级),见 shared/credentials-entry.js。
|
|
43
44
|
const credentials = require('./shared/credentials-entry.js').credentialsEntry()
|
|
44
45
|
const { createApiProxy } = require('./mobile/host-bridge.js')
|
|
45
|
-
const { installApprovalBridge } = require('./mobile/approvals.js')
|
|
46
|
-
const { installQuestionBridge } = require('./mobile/questions.js')
|
|
46
|
+
const { installApprovalBridge, setPushNotifier: setApprovalPushNotifier, setAutoReviewConfig: setApprovalAutoReview } = require('./mobile/approvals.js')
|
|
47
|
+
const { installQuestionBridge, setPushNotifier: setQuestionPushNotifier } = require('./mobile/questions.js')
|
|
48
|
+
const { createPushNotifier } = require('./mobile/push-notify.js')
|
|
47
49
|
const { installNarration } = require('./mobile/narration.js') // 过程播报:默认关,开关真值每步现读
|
|
48
50
|
const { mountSupervisorRuntime } = require('./mobile/supervisor-runtime/index.js') // 总管运行期:有活/没活判断 + 每次开口带任务账。
|
|
49
51
|
// 「手机上最近来过没有」:守卫记、控制面状态读。单独一个模块,装配层只转交。
|
|
@@ -54,10 +56,6 @@ const { installSessionCleanup } = require('./session-cleanup.cjs')
|
|
|
54
56
|
const statsScheduler = require('./mobile/stats-scheduler.cjs')
|
|
55
57
|
// 扩展卡的宿主侧:`extension_present` 工具与卡状态账本(见 mobile/extensions.js)。
|
|
56
58
|
const { installExtensions } = require('./mobile/extensions.js')
|
|
57
|
-
const { mountPhoneControl } = require('./mobile/phone/index.js') // 「让 AI 操作手机」,见 mobile/phone/
|
|
58
|
-
// 公众号文章库:给 AI 的两个工具(写 / 读),见 mobile/wechat-tools.js。
|
|
59
|
-
const { installWechatTools } = require('./mobile/wechat-tools.js')
|
|
60
|
-
const { installPetTools } = require('./mobile/pet-tools.js') // 悬浮宠物的两个工具(看 / 写),见 mobile/pet-tools.js。
|
|
61
59
|
const { mountSupervisorTools } = require('./mobile/supervisor-tools.js') // 总管那组读写工具(列会话 / 看进度 / 读正文 / 发消息 / 开新会话 / 中断)。
|
|
62
60
|
const { mount: mountSupervisorMemory } = require('./mobile/supervisor-memory/index.js') // 总管的两本账(任务账 + 知识库)。
|
|
63
61
|
const { activeGuard } = require('./host-guard.cjs')
|
|
@@ -114,6 +112,9 @@ export const RemoteControlSettingsSchema = z.object({
|
|
|
114
112
|
// why 'code' is not the old popup. Kept in the config schema so /api/relay/config
|
|
115
113
|
// can change it, and so an unknown value is rejected on write.
|
|
116
114
|
pairingPolicy: z.union(pairingApproval.POLICIES.map(value => z.const(value))).default(pairingApproval.DEFAULT_POLICY),
|
|
115
|
+
// 新配对设备的权限档位(只读 / 可答审批 / 全权)。配对时在电脑侧选定,展开成 permissions
|
|
116
|
+
// 写进凭据记录(见 lib/mobile/permissions.js)。缺省 full = 与今天单 token 全给等价。
|
|
117
|
+
pairingTier: z.union(permissions.TIER_NAMES.map(t => z.const(t))).default(permissions.DEFAULT_TIER), approvalAutoReview: z.boolean().default(false), // 审批自动审阅档(默认关,见 mobile/auto-review.js)
|
|
117
118
|
// 中转注册密钥(对偶于中转的 TUNNEL_REGISTER_SECRET)。桌面版拿不到 shell
|
|
118
119
|
// 环境变量,所以它和其他配置一样存在 relay.json(0600),由 /api/relay/config
|
|
119
120
|
// 在回环上写入;status / pairing / 二维码都不回显它。
|
|
@@ -143,6 +144,8 @@ function configOf(state) {
|
|
|
143
144
|
// The schema rejects anything outside the enum, so in practice this only
|
|
144
145
|
// ever normalises a value written before pairingPolicy existed.
|
|
145
146
|
pairingPolicy: pairingApproval.normalize(state.pairingPolicy),
|
|
147
|
+
pairingTier: permissions.isKnownTier(state.pairingTier) ? state.pairingTier : permissions.DEFAULT_TIER, // 未知值收敛回 full
|
|
148
|
+
approvalAutoReview: state.approvalAutoReview === true, // 非 true 一律当关
|
|
146
149
|
// 密钥只往 sidecar 和状态文件走:status() / pairingPayload() 都单独取字段,
|
|
147
150
|
// 不会把它回显给手机、二维码或日志。
|
|
148
151
|
registerSecret: typeof state.registerSecret === 'string' ? state.registerSecret : '',
|
|
@@ -269,6 +272,10 @@ function apply(ctx) {
|
|
|
269
272
|
return undefined
|
|
270
273
|
}
|
|
271
274
|
}
|
|
275
|
+
// 推送出口与镜像调度器都晚绑定:桥可能早于它们装,建好之后再往里填(标题取调度器的会话名)。
|
|
276
|
+
let pushNotify = null, mirror = null
|
|
277
|
+
const notifyInteraction = (sessionId, card, hints) => { if (pushNotify) { try { void pushNotify.notifyInteraction(sessionId, card, { ...(hints && typeof hints === 'object' ? hints : {}), title: mirror?.sessionTitleOf?.(sessionId) }) } catch { /* ignore */ } } }
|
|
278
|
+
setApprovalPushNotifier(notifyInteraction); setQuestionPushNotifier(notifyInteraction)
|
|
272
279
|
/**
|
|
273
280
|
* 装兼容桥与两条帧桥(审批 / 提问)。
|
|
274
281
|
*
|
|
@@ -301,7 +308,10 @@ function apply(ctx) {
|
|
|
301
308
|
// relay.json 是这个命名空间的种子。现在由 createConfigStore 直接以 relay.json
|
|
302
309
|
// 为准(见那里的说明与代价)。
|
|
303
310
|
const settings = createConfigStore(state)
|
|
304
|
-
Object.assign(state, settings.get())
|
|
311
|
+
Object.assign(state, settings.get()); setApprovalAutoReview(() => configOf(state)) // 自动审阅档开关取值器:钩子每拍现读
|
|
312
|
+
// 实例历史密钥(HK)的取值器:缺失时惰性生成并落盘(relay.json 单一真源)。配对响应、
|
|
313
|
+
// `/api/mobile/v1/history-key` 与镜像推送三条路共用这一个入口。
|
|
314
|
+
const getHistoryKey = () => core.ensureHistoryKey(state)
|
|
305
315
|
|
|
306
316
|
// 局域网直连端口自适应。
|
|
307
317
|
//
|
|
@@ -318,10 +328,10 @@ function apply(ctx) {
|
|
|
318
328
|
let lanPortProbe = null
|
|
319
329
|
const lanPortInUse = () => (state.lanEnabled && effectiveLanPort > 0 ? effectiveLanPort : state.lanPort)
|
|
320
330
|
const advertisedLanPort = () => (state.lanEnabled ? sidecarLifecycle.getAdvertisedLanPort() : state.lanPort) // 对外报地址用实绑端口,见 lifecycle
|
|
321
|
-
//
|
|
322
|
-
const tailcat = mountTailcat({ mount, warn, state, saveState: core.saveState,
|
|
323
|
-
//
|
|
324
|
-
const p2p = mountP2P({ mount, warn, state, saveState: core.saveState, localPort: lanPortInUse,
|
|
331
|
+
// 直连隧道:不配中转也能连的兜底通道;`webPort` 交给它自己起的落点代理,不能裸转(见 relay/tunnel-forwarder.cjs)。
|
|
332
|
+
const tailcat = mountTailcat({ mount, warn, state, saveState: core.saveState, lanEnabled: () => state.lanEnabled, lanPort: lanPortInUse, webPort: () => ctx.webServer.port, hostName: () => state.name, ctx, name })
|
|
333
|
+
// 点对点隧道:两端打洞直连、数据不经过任何服务器。落点判据与直连隧道同一份(见 chooseP2PTarget)。
|
|
334
|
+
const p2p = mountP2P({ mount, warn, state, saveState: core.saveState, localPort: lanPortInUse, lanEnabled: () => state.lanEnabled, lanPort: lanPortInUse, webPort: () => ctx.webServer.port, ctx, name })
|
|
325
335
|
// 每次同步前都跑一次探测,而不是只在启动时探一次:`/api/relay/config` 改了
|
|
326
336
|
// lanPort 之后同样要走这条路(否则新端口被占时又会退回死地址)。并发调用复用
|
|
327
337
|
// 在飞的那次——探测要占一个监听句柄,重复触发没有意义。
|
|
@@ -359,12 +369,14 @@ function apply(ctx) {
|
|
|
359
369
|
// 密钥(协议 v2 的请求在旁路里解封)。漏了票据时旁路读到的是空串,任何一次扫码配对
|
|
360
370
|
// 都回「配对码错误或桌面端已拒绝」,而主进程那条 `/pair` 一切正常 —— 症状是「同一个
|
|
361
371
|
// 二维码,走中转能配、走局域网不能」。
|
|
362
|
-
buildConfig: (lanPort) => ({ enabled: state.enabled, relayUrl: state.relayUrl, relayUrls: core.relayAddressesOf(state).relayUrls, instanceId: state.instanceId, pairingCode: state.pairingCode, pairingTicket: state.pairingTicket || '', name: state.name, upstreamPort: ctx.webServer.port, lanPort, lanEnabled: state.lanEnabled, lanSecret: state.lanSecret, registerSecret: state.registerSecret }),
|
|
372
|
+
buildConfig: (lanPort) => ({ enabled: state.enabled, relayUrl: state.relayUrl, relayUrls: core.relayAddressesOf(state).relayUrls, instanceId: state.instanceId, pairingCode: state.pairingCode, pairingTicket: state.pairingTicket || '', pairingTier: permissions.isKnownTier(state.pairingTier) ? state.pairingTier : permissions.DEFAULT_TIER, name: state.name, upstreamPort: ctx.webServer.port, lanPort, lanEnabled: state.lanEnabled, lanSecret: state.lanSecret, registerSecret: state.registerSecret }),
|
|
363
373
|
})
|
|
364
374
|
const { sidecarToken, syncSidecar, restartSidecar, tunnelConnected } = sidecarLifecycle
|
|
365
375
|
|
|
376
|
+
// iOS / APNs 推送出口(完成/失败 + 审批/提问卡):依赖 sidecar 的 pushSync 与隧道状态。
|
|
377
|
+
pushNotify = createPushNotifier({ getSidecar: sidecarLifecycle.getSidecar, tunnelConnected, instanceId: () => state.instanceId })
|
|
366
378
|
// 镜像推送调度器:会话事件立即推,后台循环每个 tick 补推一个冷会话;凭据摘要由装配层给取值器(凭据库归本进程),中转靠它认离线缓存的读者。
|
|
367
|
-
|
|
379
|
+
mirror = createMirrorPusher({ ctx, tunnelConnected, getSidecar: sidecarLifecycle.getSidecar, lastMobileAt: activity.snapshot, credentialDigests: () => credentials.activeDigests(state.instanceId), getHistoryKey, notifyTurnEnded: pushNotify.notifyTurnEnded })
|
|
368
380
|
// 隧道刚连上时补推一次全量:未连通期间每轮推送都被 worker 以 503 拒绝。
|
|
369
381
|
sidecarLifecycle.onTunnelUp(() => { void mirror.pushMirror(); })
|
|
370
382
|
// 择路选中了一条不是队首的地址 → 写回 `relay.json`(规则与理由见 sidecar-lifecycle 的 createRouteAdopter)。
|
|
@@ -387,14 +399,6 @@ function apply(ctx) {
|
|
|
387
399
|
}
|
|
388
400
|
return undefined
|
|
389
401
|
}, `${name}: extension tool`)
|
|
390
|
-
ctx.effect(() => {
|
|
391
|
-
// 缺 tools 注入的 profile 只少这两个工具;文章库本身与它的路由照常可用。
|
|
392
|
-
try { installWechatTools(ctx) } catch (error) {
|
|
393
|
-
if (!String(error).includes('without inject')) throw error
|
|
394
|
-
}
|
|
395
|
-
return undefined
|
|
396
|
-
}, `${name}: wechat article tools`)
|
|
397
|
-
mount('宠物工具', () => installPetTools(ctx)) // 缺 tools 注入的 profile 只少这两个,包目录与路由照常可用。
|
|
398
402
|
mount('总管工具', () => mountSupervisorTools(ctx)) // 同宠物工具:缺 tools 注入只少这六条,其余照常。
|
|
399
403
|
mount('总管两本账', () => ctx.effect(() => mountSupervisorMemory(ctx).dispose, `${name}: supervisor memory`)) // 任务账 + 知识库:跟着会话事件自动记,实现都在 mobile/supervisor-memory/。
|
|
400
404
|
mount('统计采集', () => ctx.effect(() => statsScheduler.start(ctx), `${name}: stats collect`))
|
|
@@ -503,6 +507,7 @@ function apply(ctx) {
|
|
|
503
507
|
if (record) credentials.touch(record, { channel: viaChannel(req), meta: deviceMetaFromHeaders(req) })
|
|
504
508
|
},
|
|
505
509
|
envelope,
|
|
510
|
+
permission: { resolve: permissions.resolvePermission, has: permissions.hasPermission }, // 逐路由按设备权限子集校验
|
|
506
511
|
})
|
|
507
512
|
|
|
508
513
|
// 内置浏览器预览的宿主端:手机按端口在它自己的环回上监听,把内置浏览器写下的原始
|
|
@@ -514,8 +519,7 @@ function apply(ctx) {
|
|
|
514
519
|
// 「把桌面端 GUI 搬进手机」这件事。
|
|
515
520
|
//
|
|
516
521
|
// `ownWebPort` 是同一条边界上的唯一例外:用户显式要求手机能打开网页版(2026-11-03),
|
|
517
|
-
// 所以 `mode=webapp`
|
|
518
|
-
// 理由同上:端口会被配置改,快照会让放行的那一个停在旧号上。
|
|
522
|
+
// 所以 `mode=webapp` 那一档只放行这一个端口(且目标必须是环回)。同样用取值器。
|
|
519
523
|
const preview = createPreviewService({
|
|
520
524
|
authorized,
|
|
521
525
|
protectedPorts: () => {
|
|
@@ -524,6 +528,7 @@ function apply(ctx) {
|
|
|
524
528
|
return ports
|
|
525
529
|
},
|
|
526
530
|
ownWebPort: () => ctx.webServer.port,
|
|
531
|
+
permission: permissions.createUpgradePermission({ credentials, instanceId: state.instanceId, presentedCredential }),
|
|
527
532
|
onLog: (message) => ctx.logger?.info?.(`${name}: %s`, message),
|
|
528
533
|
})
|
|
529
534
|
ctx.effect(() => () => preview.close(), `${name}: preview pipes`)
|
|
@@ -540,13 +545,14 @@ function apply(ctx) {
|
|
|
540
545
|
// 一样必须包在 ctx.effect 里,否则插件重载时重复注册会直接抛错。
|
|
541
546
|
registerUpgradeRoute: (route) => mountRoute(route, true),
|
|
542
547
|
preview,
|
|
543
|
-
phoneService: mountPhoneControl(ctx, { authorized, credentials, instanceId: state.instanceId, name }),
|
|
544
548
|
// 网页版登录令牌那条:handler 在这里造好、按依赖交给路由表,理由与 preview 一样——
|
|
545
549
|
// 它要读的是本进程的 `ctx.webServer` 与 `ctx.get('connection')`。
|
|
546
550
|
hostWeb: hostWeb.handleHostWeb,
|
|
547
551
|
guarded,
|
|
548
552
|
state,
|
|
549
553
|
hostMetadata,
|
|
554
|
+
// iOS 推送令牌上报(/push-token)的登记 / 删除出口;不传时路由只回 ok。
|
|
555
|
+
pushNotify,
|
|
550
556
|
// 取值器而非快照对象:snapshot 会被 applySidecarConfig 整体替换,闭包捕获对象会让
|
|
551
557
|
// 连接信息永远停在进程启动那一刻。
|
|
552
558
|
getSidecarSnapshot: sidecarLifecycle.getSnapshot,
|
|
@@ -562,6 +568,8 @@ function apply(ctx) {
|
|
|
562
568
|
pairingApproval,
|
|
563
569
|
// 配对成功之后换码 + 换票据(§5.1):状态变更在 core 里,这里只补「让旁路重读」。
|
|
564
570
|
rotatePairing: () => { core.rotatePairing(state); applyServices() },
|
|
571
|
+
// 历史 blob 的实例密钥(HK):配对封包与 `/api/mobile/v1/history-key` 都从这里取。
|
|
572
|
+
getHistoryKey,
|
|
565
573
|
// 「这台设备的中转与连接配置」那十条(见 lib/mobile/relay-control.js)。
|
|
566
574
|
// 它们与控制面 `/api/relay/*` 写的是**同一份** relay.json(同一个 `settings` store),
|
|
567
575
|
// 只是入口不同:控制面只认本机,移动端认已配对 token。这里的每一项都是本文件已有的
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 加密历史 blob 的信封与组帧(宿主侧)。
|
|
5
|
+
*
|
|
6
|
+
* 宿主离线时手机要能翻会话正文:电脑用一把「实例历史密钥」(HK)把正文封成密文 blob 推到
|
|
7
|
+
* 中转,中转只存密文、永不持钥、永不解密;手机用自己那份 HK 解开。**能力面不扩**——只多
|
|
8
|
+
* 一条只读的 blob 拉取路径,不暴露 shell / 系统设置 / 任意文件写。
|
|
9
|
+
*
|
|
10
|
+
* 这一层只负责「算字节」与「决定哪些会话该进 blob」:
|
|
11
|
+
* - 信封口径与 `app/src/services/viewmodels/history-blob.ts` 一字不差:AES-256-GCM,
|
|
12
|
+
* `nonce(12) || 密文 || tag(16)`,AAD `dsh-history-v1|<scope>|<key>`,外层
|
|
13
|
+
* `{v:1, z:0, n:b64url(nonce), c:b64url(密文+tag)}`;`z` 恒 0 表示未压缩。
|
|
14
|
+
* - 中转的 `blobPut` 帧里 `cipher` 是**信封 JSON 的 base64url**(中转只当不透明字节)。
|
|
15
|
+
* - **只做 `messages` 一档**:推上去的是会话正文,附件与交付文件不在内(那要另算字节流、
|
|
16
|
+
* 配额与分段)。`sealHistoryBlob` / `openHistoryBlob` 认 `file` 这个 scope 只是为将来
|
|
17
|
+
* 留口,今天没有生产者,别把「正文能离线看」读成「文件也能」。
|
|
18
|
+
*
|
|
19
|
+
* 必须是 .cjs:包根是 type=module,而 lib/mirror/ 没有自己的 `{"type":"commonjs"}`。
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
const { seal, open, b64url, unb64url, NONCE_BYTES } = require('../shared/e2ee.js');
|
|
23
|
+
|
|
24
|
+
const AAD_PREFIX = 'dsh-history-v1';
|
|
25
|
+
const ENVELOPE_VERSION = 1;
|
|
26
|
+
const KEY_BYTES = 32;
|
|
27
|
+
|
|
28
|
+
/** 信封 AAD:`dsh-history-v1|<scope>|<key>`。App 侧同一串,差一个字节都解不开。 */
|
|
29
|
+
const aadFor = (scope, key) => `${AAD_PREFIX}|${scope}|${key}`;
|
|
30
|
+
|
|
31
|
+
/** HK 归一成 32 字节 Buffer:接受 base64url 串本身或已是 32 字节的 Buffer。 */
|
|
32
|
+
function keyBytes(hk) {
|
|
33
|
+
const buf = Buffer.isBuffer(hk) ? hk : unb64url(hk);
|
|
34
|
+
if (buf.length !== KEY_BYTES) throw new Error('history key must be 32 bytes');
|
|
35
|
+
return buf;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* 封一个 JSON 明文成信封 JSON 字符串。
|
|
40
|
+
*
|
|
41
|
+
* @param {string|Buffer} hk 实例历史密钥(base64url 或 32 字节)
|
|
42
|
+
* @param {string} scope `messages` | `file`
|
|
43
|
+
* @param {string} key sessionId / fileId
|
|
44
|
+
* @param {object} plainObj 明文内容(会被 JSON 序列化)
|
|
45
|
+
* @returns {string} 信封 JSON
|
|
46
|
+
*/
|
|
47
|
+
function sealHistoryBlob(hk, scope, key, plainObj) {
|
|
48
|
+
const buf = keyBytes(hk);
|
|
49
|
+
const plain = Buffer.from(JSON.stringify(plainObj), 'utf8');
|
|
50
|
+
const sealed = seal(buf, plain, aadFor(scope, key));
|
|
51
|
+
const envelope = {
|
|
52
|
+
v: ENVELOPE_VERSION,
|
|
53
|
+
z: 0,
|
|
54
|
+
n: b64url(sealed.subarray(0, NONCE_BYTES)),
|
|
55
|
+
c: b64url(sealed.subarray(NONCE_BYTES)),
|
|
56
|
+
};
|
|
57
|
+
return JSON.stringify(envelope);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** 解一封信封 JSON 回明文对象。认证不过抛错,绝不返回半截明文。 */
|
|
61
|
+
function openHistoryBlob(hk, scope, key, envelopeJson) {
|
|
62
|
+
const buf = keyBytes(hk);
|
|
63
|
+
const envelope = typeof envelopeJson === 'string' ? JSON.parse(envelopeJson) : envelopeJson;
|
|
64
|
+
if (!envelope || envelope.v !== ENVELOPE_VERSION) throw new Error('unsupported history envelope version');
|
|
65
|
+
if (typeof envelope.n !== 'string' || typeof envelope.c !== 'string') throw new Error('bad history envelope');
|
|
66
|
+
const sealed = Buffer.concat([unb64url(envelope.n), unb64url(envelope.c)]);
|
|
67
|
+
const plain = open(buf, sealed, aadFor(scope, key));
|
|
68
|
+
return JSON.parse(plain.toString('utf8'));
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** 信封 JSON → `blobPut.cipher`(base64url)。 */
|
|
72
|
+
const envelopeCipher = (envelopeJson) => b64url(Buffer.from(envelopeJson, 'utf8'));
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* 这个会话该不该进 blob。
|
|
76
|
+
*
|
|
77
|
+
* 老会话(proto 1)一律不入 blob:它们走的是明文老通道,手机上也没有对应的 HK 消费路径。
|
|
78
|
+
* 消息快照本身不带协议标记,所以只有明确标了 `proto === 1` 才判为老会话;缺省(现状)
|
|
79
|
+
* 按 v2 处理,既有会话不会因为这一条被判掉。
|
|
80
|
+
*/
|
|
81
|
+
function isBlobEligible(body) {
|
|
82
|
+
if (!body || typeof body !== 'object') return false;
|
|
83
|
+
if (body.proto === 1 || body.proto === '1') return false;
|
|
84
|
+
return Array.isArray(body.items);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** 从载荷的会话清单里取这个会话的 updatedAt(取不到就 0)。 */
|
|
88
|
+
function sessionUpdatedAt(payload, sessionId) {
|
|
89
|
+
const items = payload && payload.sessions && Array.isArray(payload.sessions.items) ? payload.sessions.items : [];
|
|
90
|
+
const row = items.find((entry) => entry && String(entry.sessionId) === String(sessionId));
|
|
91
|
+
const at = row && Number.isFinite(row.updatedAt) ? row.updatedAt : 0;
|
|
92
|
+
return at;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* 由一份镜像载荷算出要发的 `blobPut` 帧(scope=messages)。
|
|
97
|
+
*
|
|
98
|
+
* **没有 HK 就一条都不发**(返回空数组):开关关 / 还没生成密钥的机器,转发行为一个字节
|
|
99
|
+
* 都不变。`creds` 复用镜像里那份设备凭据摘要(`{hash, expiresAt?}[]`),这里只取 `hash`
|
|
100
|
+
* 那串 sha256 hex —— 中转拿它做拉取鉴权。
|
|
101
|
+
*
|
|
102
|
+
* @param {object} payload 镜像推送载荷(含 messages / sessions)
|
|
103
|
+
* @param {string} hk 实例历史密钥(缺失/空串 → 不推)
|
|
104
|
+
* @param {Array<{hash: string}>} creds 当前有效设备凭据摘要
|
|
105
|
+
* @returns {Array<{scope: string, key: string, creds: string[], cipher: string}>}
|
|
106
|
+
*/
|
|
107
|
+
function buildBlobPuts(payload, hk, creds) {
|
|
108
|
+
if (!hk) return [];
|
|
109
|
+
const digestList = digestListOf(creds);
|
|
110
|
+
const messages = payload && payload.messages && typeof payload.messages === 'object' ? payload.messages : {};
|
|
111
|
+
const frames = [];
|
|
112
|
+
for (const [sessionId, body] of Object.entries(messages)) {
|
|
113
|
+
if (!isBlobEligible(body)) continue;
|
|
114
|
+
frames.push(buildMessagesBlobPut(hk, sessionId, body, sessionUpdatedAt(payload, sessionId), digestList));
|
|
115
|
+
}
|
|
116
|
+
return frames;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** 设备凭据摘要 → 只留 sha256 hex 串(中转按它做拉取鉴权)。 */
|
|
120
|
+
function digestListOf(creds) {
|
|
121
|
+
if (!Array.isArray(creds)) return [];
|
|
122
|
+
return creds
|
|
123
|
+
.map((entry) => (typeof entry === 'string' ? entry : entry && entry.hash))
|
|
124
|
+
.filter((hash) => typeof hash === 'string' && hash);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** 单条 messages blob 的上限:超过就从最旧的消息开始裁(见 `buildMessagesBlobPut`)。 */
|
|
128
|
+
const MESSAGES_MAX_CIPHER_BYTES = 8 * 1024 * 1024;
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* 一条 messages blob 的内容指纹(判「要不要重封重推」用)。
|
|
132
|
+
*
|
|
133
|
+
* 取语义内容:消息水位、条数、updatedAt、最后一条的 id/seq。不含易变值,同一份正文永远
|
|
134
|
+
* 同一个指纹。**只在推送成功之后才记**,失败的那条下一轮还会重试。
|
|
135
|
+
*/
|
|
136
|
+
function messagesBlobFingerprint(sessionId, body, updatedAt) {
|
|
137
|
+
const items = Array.isArray(body && body.items) ? body.items : [];
|
|
138
|
+
const lastSeq = body && Number.isFinite(body.lastSeq) ? body.lastSeq : 0;
|
|
139
|
+
const last = items.length ? items[items.length - 1] : null;
|
|
140
|
+
const tail = (last && (last.id || (Number.isFinite(last.seq) ? last.seq : ''))) || '';
|
|
141
|
+
return `${sessionId}:${lastSeq}:${items.length}:${updatedAt}:${tail}`;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* 封一条 messages blob,超过单 blob 上限就从最旧的消息开始裁。
|
|
146
|
+
*
|
|
147
|
+
* 裁剪比例按「当前体积 / 上限」估一个 keep 数,再逐次收敛;每次重封都从头算,不做增量。
|
|
148
|
+
* 消息只有 1 条还超限(单条巨大正文)就照发那一条 —— 少了它这条会话就整条离线不可读,
|
|
149
|
+
* 而一条消息的体积是宿主自己的事,不在这里替它截断正文。
|
|
150
|
+
*/
|
|
151
|
+
function buildMessagesBlobPut(hk, sessionId, body, updatedAt, digestList, maxBytes = MESSAGES_MAX_CIPHER_BYTES) {
|
|
152
|
+
const all = Array.isArray(body.items) ? body.items : [];
|
|
153
|
+
let keep = all.length;
|
|
154
|
+
let frame = null;
|
|
155
|
+
for (;;) {
|
|
156
|
+
const items = keep >= all.length ? all : all.slice(all.length - keep);
|
|
157
|
+
const plain = { sessionId, seq: Number.isFinite(body.lastSeq) ? body.lastSeq : 0, updatedAt, messages: items };
|
|
158
|
+
const cipher = envelopeCipher(sealHistoryBlob(hk, 'messages', sessionId, plain));
|
|
159
|
+
frame = { scope: 'messages', key: sessionId, creds: digestList, cipher };
|
|
160
|
+
if (cipher.length <= maxBytes || keep <= 1) break;
|
|
161
|
+
// 每次至少丢一条,按超出的比例估下一档,避免逐条试。
|
|
162
|
+
const ratio = maxBytes / cipher.length;
|
|
163
|
+
const next = Math.max(1, Math.floor(keep * ratio * 0.95));
|
|
164
|
+
keep = Math.min(keep - 1, next);
|
|
165
|
+
}
|
|
166
|
+
return frame;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
module.exports = {
|
|
171
|
+
AAD_PREFIX,
|
|
172
|
+
ENVELOPE_VERSION,
|
|
173
|
+
MESSAGES_MAX_CIPHER_BYTES,
|
|
174
|
+
aadFor,
|
|
175
|
+
sealHistoryBlob,
|
|
176
|
+
openHistoryBlob,
|
|
177
|
+
envelopeCipher,
|
|
178
|
+
isBlobEligible,
|
|
179
|
+
digestListOf,
|
|
180
|
+
sessionUpdatedAt,
|
|
181
|
+
messagesBlobFingerprint,
|
|
182
|
+
buildMessagesBlobPut,
|
|
183
|
+
buildBlobPuts,
|
|
184
|
+
};
|