dsh-mobile-plugin 0.5.66 → 0.5.68
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/docs/extension-authoring.md +20 -1
- package/lib/client.js +127 -16
- package/lib/frpc/provision.cjs +76 -12
- package/lib/impl.js +20 -0
- package/lib/mobile/file-read.js +65 -36
- package/lib/mobile/mux.js +231 -0
- package/lib/mobile/permissions.js +4 -0
- package/lib/mobile/session-models.js +6 -1
- package/lib/panel/index.html +12 -13
- package/lib/relay/status.js +5 -1
- package/lib/routes/control-plane.js +11 -10
- package/lib/routes/mobile-api.js +7 -1
- package/lib/shell/updater.js +4 -1
- package/package.json +1 -1
package/lib/mobile/file-read.js
CHANGED
|
@@ -80,25 +80,13 @@ function dispositionOf(name) {
|
|
|
80
80
|
}
|
|
81
81
|
|
|
82
82
|
/**
|
|
83
|
-
*
|
|
83
|
+
* 判一条绝对路径认不认、认了盘上在不在、是不是普通文件 —— `/file/read` 取字节与
|
|
84
|
+
* `/file/stat` 问大小共用这**一份**放行判定。抄第二份就是给「能力面悄悄扩大」留后门:
|
|
85
|
+
* 能 stat 的路径必须与能下载的完全重合。
|
|
84
86
|
*
|
|
85
|
-
*
|
|
86
|
-
* @param {object} req node:http IncomingMessage
|
|
87
|
-
* @param {object} res node:http ServerResponse
|
|
87
|
+
* 回 `{ ok:true, target: realTarget, info }` 或 `{ ok:false, status, code, message }`。
|
|
88
88
|
*/
|
|
89
|
-
async function
|
|
90
|
-
const body = await readJsonBody(req);
|
|
91
|
-
const declared = body && typeof body.path === 'string' ? body.path.trim() : '';
|
|
92
|
-
const sessionId = body && typeof body.sessionId === 'string' ? body.sessionId.trim() : '';
|
|
93
|
-
if (!declared) {
|
|
94
|
-
return sendJson(res, 400, { ok: false, error: { code: 'bad-request', message: '需要 { path }(绝对路径)' } });
|
|
95
|
-
}
|
|
96
|
-
if (!path.isAbsolute(declared)) {
|
|
97
|
-
return sendJson(res, 400, {
|
|
98
|
-
ok: false,
|
|
99
|
-
error: { code: 'bad-request', message: 'path 必须是绝对路径' },
|
|
100
|
-
});
|
|
101
|
-
}
|
|
89
|
+
async function resolveReadableFile(ctx, declared, sessionId) {
|
|
102
90
|
const target = path.resolve(declared);
|
|
103
91
|
|
|
104
92
|
let roots;
|
|
@@ -106,19 +94,10 @@ async function handleFileRead(ctx, req, res) {
|
|
|
106
94
|
roots = await allowedRoots(ctx, sessionId);
|
|
107
95
|
} catch (error) {
|
|
108
96
|
// 宿主那句原话回给手机:这条路上「没取到」与「你没有这个目录」必须分得开。
|
|
109
|
-
return
|
|
110
|
-
ok: false,
|
|
111
|
-
error: {
|
|
112
|
-
code: (error && error.code) || 'workspace-list-failed',
|
|
113
|
-
message: String((error && error.message) || error),
|
|
114
|
-
},
|
|
115
|
-
});
|
|
97
|
+
return { ok: false, status: 502, code: (error && error.code) || 'workspace-list-failed', message: String((error && error.message) || error) };
|
|
116
98
|
}
|
|
117
99
|
if (!inAllowedZone(roots, target)) {
|
|
118
|
-
return
|
|
119
|
-
ok: false,
|
|
120
|
-
error: { code: 'outside-allowed', message: '这个文件不在能看的范围里(只放行工作区目录和这条会话的项目目录)' },
|
|
121
|
-
});
|
|
100
|
+
return { ok: false, status: 403, code: 'outside-allowed', message: '这个文件不在能看的范围里(只放行工作区目录和这条会话的项目目录)' };
|
|
122
101
|
}
|
|
123
102
|
|
|
124
103
|
// 字符串落点在允许区里还不够:工作区里的软链可能指到区外。与写侧(`file-ops.js` 的
|
|
@@ -126,24 +105,49 @@ async function handleFileRead(ctx, req, res) {
|
|
|
126
105
|
const realTarget = await realpath(target).catch(() => null);
|
|
127
106
|
if (!realTarget) {
|
|
128
107
|
// realpath 失败 = 盘上没有了(或断了):与 stat 失败同一个 404 口子。
|
|
129
|
-
return
|
|
108
|
+
return { ok: false, status: 404, code: 'missing', message: '文件已经不在这台电脑上了' };
|
|
130
109
|
}
|
|
131
110
|
if (!inAllowedZone(await realRootsOf(roots), realTarget)) {
|
|
132
|
-
return
|
|
133
|
-
ok: false,
|
|
134
|
-
error: { code: 'outside-allowed', message: '这个文件的物理位置不在能看的范围里(可能经过了符号链接)' },
|
|
135
|
-
});
|
|
111
|
+
return { ok: false, status: 403, code: 'outside-allowed', message: '这个文件的物理位置不在能看的范围里(可能经过了符号链接)' };
|
|
136
112
|
}
|
|
137
113
|
|
|
138
114
|
let info;
|
|
139
115
|
try {
|
|
140
116
|
info = await stat(realTarget);
|
|
141
117
|
} catch {
|
|
142
|
-
return
|
|
118
|
+
return { ok: false, status: 404, code: 'missing', message: '文件已经不在这台电脑上了' };
|
|
143
119
|
}
|
|
144
120
|
if (!info.isFile()) {
|
|
145
|
-
return
|
|
121
|
+
return { ok: false, status: 400, code: 'not-a-file', message: '这条路径不是一个文件' };
|
|
122
|
+
}
|
|
123
|
+
return { ok: true, target: realTarget, info };
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* POST { path(绝对), sessionId? } → 该文件的原始字节。
|
|
128
|
+
*
|
|
129
|
+
* @param {object} ctx 插件上下文
|
|
130
|
+
* @param {object} req node:http IncomingMessage
|
|
131
|
+
* @param {object} res node:http ServerResponse
|
|
132
|
+
*/
|
|
133
|
+
async function handleFileRead(ctx, req, res) {
|
|
134
|
+
const body = await readJsonBody(req);
|
|
135
|
+
const declared = body && typeof body.path === 'string' ? body.path.trim() : '';
|
|
136
|
+
const sessionId = body && typeof body.sessionId === 'string' ? body.sessionId.trim() : '';
|
|
137
|
+
if (!declared) {
|
|
138
|
+
return sendJson(res, 400, { ok: false, error: { code: 'bad-request', message: '需要 { path }(绝对路径)' } });
|
|
139
|
+
}
|
|
140
|
+
if (!path.isAbsolute(declared)) {
|
|
141
|
+
return sendJson(res, 400, {
|
|
142
|
+
ok: false,
|
|
143
|
+
error: { code: 'bad-request', message: 'path 必须是绝对路径' },
|
|
144
|
+
});
|
|
146
145
|
}
|
|
146
|
+
const resolved = await resolveReadableFile(ctx, declared, sessionId);
|
|
147
|
+
if (!resolved.ok) {
|
|
148
|
+
return sendJson(res, resolved.status, { ok: false, error: { code: resolved.code, message: resolved.message } });
|
|
149
|
+
}
|
|
150
|
+
const { target: realTarget, info } = resolved;
|
|
147
151
|
if (info.size > SESSION_FILE_MAX_BYTES) {
|
|
148
152
|
return sendJson(res, 413, {
|
|
149
153
|
ok: false,
|
|
@@ -167,4 +171,29 @@ async function handleFileRead(ctx, req, res) {
|
|
|
167
171
|
});
|
|
168
172
|
}
|
|
169
173
|
|
|
170
|
-
|
|
174
|
+
/**
|
|
175
|
+
* POST { path(绝对), sessionId? } → `{ ok:true, size }`。
|
|
176
|
+
*
|
|
177
|
+
* 只回**原文件明文字节数**,不取字节。给 App 的下载兜底用:下载中途断在原生那一侧时,
|
|
178
|
+
* App 拿这个数核一下已经落盘的密文解出来是不是正好这么多,齐了就直接认、别从 0 重下。
|
|
179
|
+
* 放行面与 `/file/read` 完全同一份(`resolveReadableFile`),不比「能下载的」多认一条路径。
|
|
180
|
+
* 不受 512MB 上限约束:问大小不需要把文件读起来。
|
|
181
|
+
*/
|
|
182
|
+
async function handleFileStat(ctx, req, res) {
|
|
183
|
+
const body = await readJsonBody(req);
|
|
184
|
+
const declared = body && typeof body.path === 'string' ? body.path.trim() : '';
|
|
185
|
+
const sessionId = body && typeof body.sessionId === 'string' ? body.sessionId.trim() : '';
|
|
186
|
+
if (!declared) {
|
|
187
|
+
return sendJson(res, 400, { ok: false, error: { code: 'bad-request', message: '需要 { path }(绝对路径)' } });
|
|
188
|
+
}
|
|
189
|
+
if (!path.isAbsolute(declared)) {
|
|
190
|
+
return sendJson(res, 400, { ok: false, error: { code: 'bad-request', message: 'path 必须是绝对路径' } });
|
|
191
|
+
}
|
|
192
|
+
const resolved = await resolveReadableFile(ctx, declared, sessionId);
|
|
193
|
+
if (!resolved.ok) {
|
|
194
|
+
return sendJson(res, resolved.status, { ok: false, error: { code: resolved.code, message: resolved.message } });
|
|
195
|
+
}
|
|
196
|
+
return sendJson(res, 200, { ok: true, size: Number(resolved.info.size) || 0 });
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
module.exports = { handleFileRead, handleFileStat, mediaTypeOf };
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 对话页长连接(mux):一条 WebSocket 上的双向多路复用服务(宿主侧)。
|
|
5
|
+
*
|
|
6
|
+
* 背景见 `docs/mux-long-connection-design.md`。要点:App 侧握住一条持久 WS,把「发送」「实时事件」
|
|
7
|
+
* 以及其它**文本类**请求都变成这条 WS 上的逻辑流(按 id 并行);宿主在这里按 id 把它们逐条转成
|
|
8
|
+
* 本地回环 HTTP(`127.0.0.1:<webPort>`)打到既有的移动 API 上。于是:
|
|
9
|
+
*
|
|
10
|
+
* - 业务处理器、鉴权、E2EE、按路由权限统统**一行不动**——mux 只是搬运工,逐路由权限仍由回环
|
|
11
|
+
* HTTP 那一层(`guarded`)判;本服务只负责「这条 WS 是否一台已配对设备」和按 id 拆装。
|
|
12
|
+
* - 中转零改动:手机 WS 经中转原样代理到宿主这个升级路由(与内置浏览器预览同一条现成管道)。
|
|
13
|
+
* - 兜底永远在:mux 连不上/不支持时 App 走原来的 HTTPS+SSE,最坏不比今天差。
|
|
14
|
+
*
|
|
15
|
+
* 载体口径:**文本帧**(JSON 直接放 UTF-8 文本)。mux 只承载文本类移动 API(发送 / 事件订阅 /
|
|
16
|
+
* JSON 读),二进制下载(文件、附件字节)仍走 HTTP,不进 mux。宿主用 `StringDecoder` 切响应体,
|
|
17
|
+
* 保证分块边界不会劈开一个多字节字符。
|
|
18
|
+
*
|
|
19
|
+
* 帧契约见设计文档 §11:一条 WS 消息 = 一帧 JSON。
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
const http = require('node:http');
|
|
23
|
+
const { StringDecoder } = require('node:string_decoder');
|
|
24
|
+
const { WebSocketServer } = require('ws');
|
|
25
|
+
|
|
26
|
+
/** 单帧上限:与中转 `MAX_WS_MESSAGE_BYTES` / 宿主预览管道对齐(8 MiB)。 */
|
|
27
|
+
const MAX_MESSAGE_BYTES = 8 * 1024 * 1024;
|
|
28
|
+
|
|
29
|
+
/** 单连接并行逻辑流上限:够用且挡住失控。 */
|
|
30
|
+
const MAX_STREAMS_PER_CONNECTION = 64;
|
|
31
|
+
/** 宿主侧同时存在的 mux 连接上限。 */
|
|
32
|
+
const MAX_CONNECTIONS = 32;
|
|
33
|
+
|
|
34
|
+
/** 往手机方向的水位:缓冲超过高水位就把上游按住,落到低水位再放开。 */
|
|
35
|
+
const SEND_HIGH_WATER_BYTES = 4 * 1024 * 1024;
|
|
36
|
+
const SEND_LOW_WATER_BYTES = 1 * 1024 * 1024;
|
|
37
|
+
const SEND_RESUME_POLL_MS = 20;
|
|
38
|
+
|
|
39
|
+
/** 上游拨号超时:只覆盖「还没拿到响应头」这一段,之后是长连接,不能有读超时。 */
|
|
40
|
+
const UPSTREAM_HEADER_TIMEOUT_MS = 30_000;
|
|
41
|
+
|
|
42
|
+
/** 不往上游转发的逐跳头(与隧道/预览同一口径)。 */
|
|
43
|
+
const HOP_BY_HOP = new Set([
|
|
44
|
+
'connection', 'keep-alive', 'proxy-authenticate', 'proxy-authorization',
|
|
45
|
+
'te', 'trailer', 'transfer-encoding', 'upgrade', 'host',
|
|
46
|
+
]);
|
|
47
|
+
|
|
48
|
+
function safeJson(text) {
|
|
49
|
+
try { return JSON.parse(text); } catch { return null; }
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* 建一个 mux 服务。
|
|
54
|
+
*
|
|
55
|
+
* @param {object} opts
|
|
56
|
+
* @param {{host?:string, port:number}} opts.upstream 本地移动 API 地址(一般 `127.0.0.1:<webPort>`)。
|
|
57
|
+
* @param {(req: import('node:http').IncomingMessage) => boolean} opts.authorize 握手鉴权。
|
|
58
|
+
* @param {(msg: string) => void} [opts.onLog]
|
|
59
|
+
*/
|
|
60
|
+
function createMuxService(opts = {}) {
|
|
61
|
+
const upstreamHost = opts.upstream && opts.upstream.host ? opts.upstream.host : '127.0.0.1';
|
|
62
|
+
const upstreamPort = opts.upstream ? Number(opts.upstream.port) : 0;
|
|
63
|
+
const authorize = typeof opts.authorize === 'function' ? opts.authorize : () => false;
|
|
64
|
+
const onLog = typeof opts.onLog === 'function' ? opts.onLog : () => {};
|
|
65
|
+
|
|
66
|
+
const wss = new WebSocketServer({ noServer: true, maxPayload: MAX_MESSAGE_BYTES });
|
|
67
|
+
let connectionCount = 0;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* 宿主 webServer 的升级处理器(`ctx.webServer.registerUpgrade`)。
|
|
71
|
+
*
|
|
72
|
+
* 鉴权与拒绝必须发生在 `handleUpgrade` **之前**:那时 socket 还是普通 HTTP 连接,能干净地回
|
|
73
|
+
* 一个 401 关掉;握手之后再拒就只是给对端一条立断的 WS。
|
|
74
|
+
*/
|
|
75
|
+
function handleUpgrade(req, socket, head) {
|
|
76
|
+
let allowed = false;
|
|
77
|
+
try { allowed = authorize(req) === true; } catch { allowed = false; }
|
|
78
|
+
if (!allowed) {
|
|
79
|
+
try { socket.write('HTTP/1.1 401 Unauthorized\r\nconnection: close\r\ncontent-length: 0\r\n\r\n'); } catch { /* 已断 */ }
|
|
80
|
+
try { socket.destroy(); } catch { /* ignore */ }
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
if (connectionCount >= MAX_CONNECTIONS) {
|
|
84
|
+
try { socket.write('HTTP/1.1 503 Service Unavailable\r\nconnection: close\r\ncontent-length: 0\r\n\r\n'); } catch { /* ignore */ }
|
|
85
|
+
try { socket.destroy(); } catch { /* ignore */ }
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
wss.handleUpgrade(req, socket, head, (ws) => openConnection(ws));
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function openConnection(ws) {
|
|
92
|
+
connectionCount += 1;
|
|
93
|
+
/** id → 一条在飞的本地请求。 */
|
|
94
|
+
const streams = new Map();
|
|
95
|
+
let closed = false;
|
|
96
|
+
|
|
97
|
+
const send = (obj) => {
|
|
98
|
+
if (closed || ws.readyState !== 1 /* OPEN */) return;
|
|
99
|
+
try { ws.send(JSON.stringify(obj)); } catch { /* 断了,close 会收尾 */ }
|
|
100
|
+
};
|
|
101
|
+
const fail = (id, message) => send({ t: 'err', id, message: String(message || 'error') });
|
|
102
|
+
|
|
103
|
+
/** 收掉一条逻辑流:销毁上游请求、释放槽位。 */
|
|
104
|
+
const dropStream = (id, reason) => {
|
|
105
|
+
const entry = streams.get(id);
|
|
106
|
+
if (!entry) return;
|
|
107
|
+
streams.delete(id);
|
|
108
|
+
if (entry.resumeTimer) clearTimeout(entry.resumeTimer);
|
|
109
|
+
try { entry.req && entry.req.destroy(); } catch { /* ignore */ }
|
|
110
|
+
if (reason) fail(id, reason);
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
const scheduleResume = (entry, res, id) => {
|
|
114
|
+
if (entry.resumeTimer) return;
|
|
115
|
+
entry.resumeTimer = setTimeout(() => {
|
|
116
|
+
entry.resumeTimer = null;
|
|
117
|
+
if (closed || streams.get(id) !== entry) return;
|
|
118
|
+
if (ws.bufferedAmount > SEND_LOW_WATER_BYTES) { scheduleResume(entry, res, id); return; }
|
|
119
|
+
entry.paused = false;
|
|
120
|
+
try { res.resume(); } catch { /* ignore */ }
|
|
121
|
+
}, SEND_RESUME_POLL_MS);
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
const startRequest = (frame) => {
|
|
125
|
+
const id = frame.id;
|
|
126
|
+
if (typeof id !== 'number') return;
|
|
127
|
+
if (streams.has(id)) { fail(id, 'duplicate id'); return; }
|
|
128
|
+
if (streams.size >= MAX_STREAMS_PER_CONNECTION) { fail(id, 'too many streams'); return; }
|
|
129
|
+
const method = String(frame.method || 'GET').toUpperCase();
|
|
130
|
+
const path = typeof frame.path === 'string' && frame.path ? frame.path : '/';
|
|
131
|
+
const headers = {};
|
|
132
|
+
if (frame.headers && typeof frame.headers === 'object') {
|
|
133
|
+
for (const [k, v] of Object.entries(frame.headers)) {
|
|
134
|
+
if (!k || HOP_BY_HOP.has(k.toLowerCase())) continue;
|
|
135
|
+
headers[k] = v;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
const entry = { req: null, res: null, paused: false, resumeTimer: null, decoder: new StringDecoder('utf8') };
|
|
139
|
+
streams.set(id, entry);
|
|
140
|
+
|
|
141
|
+
let req;
|
|
142
|
+
try {
|
|
143
|
+
req = http.request({ host: upstreamHost, port: upstreamPort, method, path, headers }, (res) => {
|
|
144
|
+
if (closed || streams.get(id) !== entry) { res.destroy(); return; }
|
|
145
|
+
entry.res = res;
|
|
146
|
+
const respHeaders = {};
|
|
147
|
+
for (const [k, v] of Object.entries(res.headers)) {
|
|
148
|
+
if (!k || HOP_BY_HOP.has(k.toLowerCase()) || k.toLowerCase() === 'content-length') continue;
|
|
149
|
+
respHeaders[k] = v;
|
|
150
|
+
}
|
|
151
|
+
send({ t: 'resp', id, status: res.statusCode || 200, headers: respHeaders });
|
|
152
|
+
res.on('data', (buf) => {
|
|
153
|
+
if (closed || streams.get(id) !== entry) return;
|
|
154
|
+
// dec.write 只吐出完整字符,残缺的多字节尾巴自己留着,等下一块补上。
|
|
155
|
+
const text = entry.decoder.write(buf);
|
|
156
|
+
if (text) send({ t: 'chunk', id, text });
|
|
157
|
+
if (!entry.paused && ws.bufferedAmount > SEND_HIGH_WATER_BYTES) {
|
|
158
|
+
entry.paused = true;
|
|
159
|
+
res.pause();
|
|
160
|
+
scheduleResume(entry, res, id);
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
res.on('end', () => {
|
|
164
|
+
if (streams.get(id) !== entry) return;
|
|
165
|
+
streams.delete(id);
|
|
166
|
+
const rest = entry.decoder.end();
|
|
167
|
+
if (rest) send({ t: 'chunk', id, text: rest });
|
|
168
|
+
send({ t: 'end', id });
|
|
169
|
+
});
|
|
170
|
+
res.on('error', () => { if (streams.get(id) === entry) dropStream(id, 'upstream response error'); });
|
|
171
|
+
});
|
|
172
|
+
} catch (error) {
|
|
173
|
+
dropStream(id, error && error.message ? error.message : 'upstream request failed');
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
entry.req = req;
|
|
177
|
+
req.setTimeout(UPSTREAM_HEADER_TIMEOUT_MS, () => dropStream(id, 'upstream header timeout'));
|
|
178
|
+
req.on('error', (error) => { if (streams.get(id) === entry) dropStream(id, error && error.message ? error.message : 'upstream error'); });
|
|
179
|
+
if (frame.body !== undefined && frame.body !== null) {
|
|
180
|
+
if (typeof frame.body !== 'string') { dropStream(id, 'bad body'); return; }
|
|
181
|
+
try { req.write(Buffer.from(frame.body, 'utf8')); } catch { /* error 会收 */ }
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
const onFrame = (frame) => {
|
|
186
|
+
if (!frame || typeof frame !== 'object') return;
|
|
187
|
+
switch (frame.t) {
|
|
188
|
+
case 'req': startRequest(frame); break;
|
|
189
|
+
case 'body': {
|
|
190
|
+
const entry = streams.get(frame.id);
|
|
191
|
+
if (!entry || !entry.req) return;
|
|
192
|
+
if (typeof frame.text !== 'string') { fail(frame.id, 'bad body'); return; }
|
|
193
|
+
try { entry.req.write(Buffer.from(frame.text, 'utf8')); } catch { /* ignore */ }
|
|
194
|
+
break;
|
|
195
|
+
}
|
|
196
|
+
case 'bodyEnd': {
|
|
197
|
+
const entry = streams.get(frame.id);
|
|
198
|
+
if (entry && entry.req) { try { entry.req.end(); } catch { /* ignore */ } }
|
|
199
|
+
break;
|
|
200
|
+
}
|
|
201
|
+
case 'cancel': dropStream(frame.id); break;
|
|
202
|
+
case 'ping': send({ t: 'pong' }); break;
|
|
203
|
+
default: break; // 未知帧忽略(前向兼容)
|
|
204
|
+
}
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
const cleanup = () => {
|
|
208
|
+
if (closed) return;
|
|
209
|
+
closed = true;
|
|
210
|
+
connectionCount -= 1;
|
|
211
|
+
for (const entry of streams.values()) {
|
|
212
|
+
if (entry.resumeTimer) clearTimeout(entry.resumeTimer);
|
|
213
|
+
try { entry.req && entry.req.destroy(); } catch { /* ignore */ }
|
|
214
|
+
}
|
|
215
|
+
streams.clear();
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
ws.on('message', (data, isBinary) => {
|
|
219
|
+
if (isBinary) return; // 契约只走 JSON 文本帧
|
|
220
|
+
const frame = safeJson(data.toString('utf8'));
|
|
221
|
+
if (!frame) { onLog('mux: bad json frame'); return; }
|
|
222
|
+
onFrame(frame);
|
|
223
|
+
});
|
|
224
|
+
ws.on('close', cleanup);
|
|
225
|
+
ws.on('error', cleanup);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
return { handleUpgrade, MAX_MESSAGE_BYTES };
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
module.exports = { createMuxService, MAX_MESSAGE_BYTES, MAX_STREAMS_PER_CONNECTION, MAX_CONNECTIONS };
|
|
@@ -224,6 +224,7 @@ const MAP = Object.freeze({
|
|
|
224
224
|
|
|
225
225
|
// ── files.read:读文件 / 目录 / 工作区 / git / 技能正文 ─────────
|
|
226
226
|
'/api/mobile/v1/file/read': V.FILES_READ,
|
|
227
|
+
'/api/mobile/v1/file/stat': V.FILES_READ,
|
|
227
228
|
'/api/mobile/v1/directory/list': V.FILES_READ,
|
|
228
229
|
'/api/mobile/v1/directory/roots': V.FILES_READ,
|
|
229
230
|
'/api/mobile/v1/directory/listing': V.FILES_READ,
|
|
@@ -293,6 +294,9 @@ const MAP = Object.freeze({
|
|
|
293
294
|
// 这条关掉的是 `permissions: []` 那种「明确什么都不给」的设备 —— 它们原来也能连上来。
|
|
294
295
|
// 它能拨到哪儿另有独立的边界(protectedPorts / webapp 那一档,见 relay/preview.js)。
|
|
295
296
|
'/api/mobile/v1/preview': V.VIEW,
|
|
297
|
+
// 对话页长连接(WebSocket 升级路由):与 preview 同档,归只读;逐路由权限仍由 mux 转发的
|
|
298
|
+
// 本地回环 HTTP 自己判(mux 只是搬运,不另立权限面)。
|
|
299
|
+
'/api/mobile/v1/mux': V.VIEW,
|
|
296
300
|
|
|
297
301
|
// ── files.write:写文件 / 工作区 / git 写 / 技能写 / 画布写 ─────
|
|
298
302
|
'/api/mobile/v1/file/create': V.FILES_WRITE,
|
|
@@ -205,7 +205,12 @@ async function handleSelectModel(ctx, req, res) {
|
|
|
205
205
|
const payload = { sessionId: body.sessionId, provider: body.provider, model: body.model };
|
|
206
206
|
if (typeof body.reasoningEffort === 'string' && body.reasoningEffort) payload.reasoningEffort = body.reasoningEffort;
|
|
207
207
|
const { value, error } = unwrap(await selectSessionModel(ctx, payload));
|
|
208
|
-
if (error)
|
|
208
|
+
if (error) {
|
|
209
|
+
// 老宿主没有「只切当前会话模型」的能力,或新建会话还没有可切模型:与 `/session/send`
|
|
210
|
+
// 同一口径——静默放过、用会话当前模型继续,别让新建会话这一步把第一条消息卡死。
|
|
211
|
+
if (error.code === 'host-unsupported') return sendJson(res, 200, { ok: true, applied: false });
|
|
212
|
+
return sendJson(res, 400, { ok: false, error });
|
|
213
|
+
}
|
|
209
214
|
invalidateSessionModels(ctx, req, body.sessionId);
|
|
210
215
|
sendJson(res, 200, { ok: true, ...(value || {}) });
|
|
211
216
|
}
|