agent-loft 0.2.42 → 0.2.44
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 +4 -1
- package/package.json +2 -1
- package/src/channels/web/api.ts +106 -0
- package/src/channels/web/desktop.ts +68 -5
- package/src/channels/web/server.ts +1 -1
- package/src/core/desktop.ts +1 -1
- package/src/core/hub.ts +14 -0
- package/src/core/reply-files.ts +142 -0
- package/src/core/uploads.ts +1 -1
- package/src/core/visualize.ts +123 -0
- package/src/core/vnc-auth.ts +193 -0
- package/src/core/vnc-transcode.ts +765 -0
- package/web/css/messages.css +271 -0
- package/web/lib/dom.js +1 -0
- package/web/lib/markdown.js +5 -3
- package/web/views/desktop.js +32 -1
- package/web/views/file-preview.js +342 -0
- package/web/views/messages.js +3 -2
- package/src/core/vnc-ard.ts +0 -133
|
@@ -0,0 +1,765 @@
|
|
|
1
|
+
// 远程桌面转码:服务端自己当 VNC 客户端连上桌面,把画面缩小、压成 JPEG 再给页面(noVNC)。
|
|
2
|
+
//
|
|
3
|
+
// 为什么要:macOS 屏幕共享只给 ZRLE(无损)和原始像素,不给 JPEG;5K 屏一整屏 ZRLE 有十几 MB,
|
|
4
|
+
// 隔着公网又卡又费流量,noVNC 的「画质」参数对它也不起作用。这里的做法:
|
|
5
|
+
// - 和 VNC 服务之间收全分辨率的画面,存成内存里的一份「原画」。VNC 服务在本机时要原始像素(本机不怕大,
|
|
6
|
+
// macOS 出原始像素比出 ZRLE 快几倍:5K 屏约 1 秒一帧,ZRLE 要 4 秒);在别的机器上时要 ZRLE;
|
|
7
|
+
// - macOS 每次都把整屏发过来(哪怕只变了一点):和原画逐格比,真变了的格子才更新原画、
|
|
8
|
+
// 按面积平均缩到「输出画面」上,并记下输出画面上哪些格子脏了;
|
|
9
|
+
// - 页面要画面(FramebufferUpdateRequest)时,把脏的格子拼成几个矩形,用 sharp 压成 JPEG,按 Tight 编码发过去;
|
|
10
|
+
// - 页面发来的鼠标坐标按比例放大回原画的坐标,键盘、剪贴板原样转发。
|
|
11
|
+
// 对页面来说就是一个分辨率小一些、只用 Tight JPEG 的 VNC 服务(认证由 vnc-auth.ts 在服务端做完)。
|
|
12
|
+
//
|
|
13
|
+
// 流量控制:页面画完一帧才会要下一帧,WebSocket 积压多了也先不发;和 VNC 服务之间每帧最少隔 FRAME_MS。
|
|
14
|
+
import zlib from "node:zlib";
|
|
15
|
+
import { ByteReader, serveNoAuth, vncLogin, type VncCreds } from "./vnc-auth.js";
|
|
16
|
+
|
|
17
|
+
/** 画质档位:长边缩到多少像素、JPEG 质量 */
|
|
18
|
+
export const DESKTOP_QUALITIES = {
|
|
19
|
+
smooth: { edge: 1280, jpeg: 50 },
|
|
20
|
+
standard: { edge: 1920, jpeg: 70 },
|
|
21
|
+
high: { edge: 2560, jpeg: 82 },
|
|
22
|
+
} as const;
|
|
23
|
+
export type DesktopQuality = keyof typeof DESKTOP_QUALITIES;
|
|
24
|
+
|
|
25
|
+
export function isDesktopQuality(q: unknown): q is DesktopQuality {
|
|
26
|
+
return typeof q === "string" && Object.hasOwn(DESKTOP_QUALITIES, q);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const ENC_RAW = 0;
|
|
30
|
+
const ENC_COPYRECT = 1;
|
|
31
|
+
const ENC_TIGHT = 7;
|
|
32
|
+
const ENC_ZRLE = 16;
|
|
33
|
+
const ENC_DESKTOP_SIZE = -223;
|
|
34
|
+
const ENC_CURSOR = -239;
|
|
35
|
+
|
|
36
|
+
/** 脏格子的边长(输出画面上的像素) */
|
|
37
|
+
const TILE = 64;
|
|
38
|
+
/** 拼出来的矩形最高这么多格:一个 JPEG 别太大(Tight 的长度字段最多 4MB) */
|
|
39
|
+
const MAX_RECT_TILES = 8;
|
|
40
|
+
/** 和 VNC 服务之间每帧最少隔多久(毫秒):解码、缩放都在主线程上,别占满 CPU */
|
|
41
|
+
const FRAME_MS = 40;
|
|
42
|
+
/** 页面收得慢:WebSocket 里积压到这么多就先不发 */
|
|
43
|
+
const HIGH_WATER = 2 * 1024 * 1024;
|
|
44
|
+
|
|
45
|
+
type Rect = { x: number; y: number; w: number; h: number };
|
|
46
|
+
|
|
47
|
+
/** 原画和缩小后的输出画面(都是每像素 4 字节 R G B X),外加输出画面上的脏格子 */
|
|
48
|
+
export class Frame {
|
|
49
|
+
srcW = 0;
|
|
50
|
+
srcH = 0;
|
|
51
|
+
outW = 0;
|
|
52
|
+
outH = 0;
|
|
53
|
+
src = Buffer.alloc(0);
|
|
54
|
+
out = new Uint8Array(0);
|
|
55
|
+
/** 每个输出像素对应原画的哪一段:[x0[i], x1[i]) */
|
|
56
|
+
private x0 = new Int32Array(0);
|
|
57
|
+
private x1 = new Int32Array(0);
|
|
58
|
+
private y0 = new Int32Array(0);
|
|
59
|
+
private y1 = new Int32Array(0);
|
|
60
|
+
private tilesX = 0;
|
|
61
|
+
private tilesY = 0;
|
|
62
|
+
private dirty = new Uint8Array(0);
|
|
63
|
+
private dirtyCount = 0;
|
|
64
|
+
|
|
65
|
+
constructor(
|
|
66
|
+
srcW: number,
|
|
67
|
+
srcH: number,
|
|
68
|
+
private readonly edge: number,
|
|
69
|
+
) {
|
|
70
|
+
this.resize(srcW, srcH);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
resize(srcW: number, srcH: number): void {
|
|
74
|
+
const k = Math.min(1, this.edge / Math.max(srcW, srcH, 1));
|
|
75
|
+
this.srcW = srcW;
|
|
76
|
+
this.srcH = srcH;
|
|
77
|
+
this.outW = Math.max(1, Math.round(srcW * k));
|
|
78
|
+
this.outH = Math.max(1, Math.round(srcH * k));
|
|
79
|
+
this.src = Buffer.alloc(srcW * srcH * 4);
|
|
80
|
+
// 不用缩:输出画面就是原画
|
|
81
|
+
this.out = this.scaled ? new Uint8Array(this.outW * this.outH * 4) : this.src;
|
|
82
|
+
const ranges = (outN: number, srcN: number) => {
|
|
83
|
+
const lo = new Int32Array(outN);
|
|
84
|
+
const hi = new Int32Array(outN);
|
|
85
|
+
for (let i = 0; i < outN; i++) {
|
|
86
|
+
lo[i] = Math.floor((i * srcN) / outN);
|
|
87
|
+
hi[i] = Math.max(lo[i]! + 1, Math.min(srcN, Math.floor(((i + 1) * srcN) / outN)));
|
|
88
|
+
}
|
|
89
|
+
return [lo, hi] as const;
|
|
90
|
+
};
|
|
91
|
+
[this.x0, this.x1] = ranges(this.outW, srcW);
|
|
92
|
+
[this.y0, this.y1] = ranges(this.outH, srcH);
|
|
93
|
+
this.tilesX = Math.ceil(this.outW / TILE);
|
|
94
|
+
this.tilesY = Math.ceil(this.outH / TILE);
|
|
95
|
+
this.dirty = new Uint8Array(this.tilesX * this.tilesY);
|
|
96
|
+
this.dirtyCount = 0;
|
|
97
|
+
this.markOut(0, 0, this.outW, this.outH);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
get scaled(): boolean {
|
|
101
|
+
return this.outW !== this.srcW || this.outH !== this.srcH;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
get hasDirty(): boolean {
|
|
105
|
+
return this.dirtyCount > 0;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** 原画上比较变化用的格子边长:对应输出画面上一个脏格子 */
|
|
109
|
+
private get cell(): number {
|
|
110
|
+
return Math.max(TILE, Math.ceil((TILE * this.srcW) / this.outW));
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* VNC 服务发来原画上 (x, y, w, h) 这块的新像素(紧凑的 R G B X):逐格和原画比,变了的才写进去、记脏。
|
|
115
|
+
* 返回变了的格子数
|
|
116
|
+
*/
|
|
117
|
+
update(x: number, y: number, w: number, h: number, data: Buffer): number {
|
|
118
|
+
const cell = this.cell;
|
|
119
|
+
const stride = this.srcW * 4;
|
|
120
|
+
let changed = 0;
|
|
121
|
+
for (let cy = y; cy < y + h; cy += cell) {
|
|
122
|
+
const ch = Math.min(cell, y + h - cy);
|
|
123
|
+
for (let cx = x; cx < x + w; cx += cell) {
|
|
124
|
+
const cw = Math.min(cell, x + w - cx);
|
|
125
|
+
const bytes = cw * 4;
|
|
126
|
+
let differs = false;
|
|
127
|
+
for (let row = 0; row < ch && !differs; row++) {
|
|
128
|
+
const from = ((cy - y + row) * w + (cx - x)) * 4;
|
|
129
|
+
const to = (cy + row) * stride + cx * 4;
|
|
130
|
+
differs = data.compare(this.src, to, to + bytes, from, from + bytes) !== 0;
|
|
131
|
+
}
|
|
132
|
+
if (!differs) continue;
|
|
133
|
+
for (let row = 0; row < ch; row++) {
|
|
134
|
+
const from = ((cy - y + row) * w + (cx - x)) * 4;
|
|
135
|
+
data.copy(this.src, (cy + row) * stride + cx * 4, from, from + bytes);
|
|
136
|
+
}
|
|
137
|
+
this.damage(cx, cy, cw, ch);
|
|
138
|
+
changed++;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return changed;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** 原画上这块变了:缩到输出画面上,记脏 */
|
|
145
|
+
damage(x: number, y: number, w: number, h: number): void {
|
|
146
|
+
if (w <= 0 || h <= 0) return;
|
|
147
|
+
const ox0 = Math.max(0, Math.floor((x * this.outW) / this.srcW));
|
|
148
|
+
const oy0 = Math.max(0, Math.floor((y * this.outH) / this.srcH));
|
|
149
|
+
const ox1 = Math.min(this.outW, Math.ceil(((x + w) * this.outW) / this.srcW));
|
|
150
|
+
const oy1 = Math.min(this.outH, Math.ceil(((y + h) * this.outH) / this.srcH));
|
|
151
|
+
if (this.scaled) this.downscale(ox0, oy0, ox1, oy1);
|
|
152
|
+
this.markOut(ox0, oy0, ox1 - ox0, oy1 - oy0);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** 输出画面上这块要重发(页面要整屏时) */
|
|
156
|
+
markOut(x: number, y: number, w: number, h: number): void {
|
|
157
|
+
const tx1 = Math.min(this.tilesX, Math.ceil((x + w) / TILE));
|
|
158
|
+
const ty1 = Math.min(this.tilesY, Math.ceil((y + h) / TILE));
|
|
159
|
+
for (let ty = Math.max(0, Math.floor(y / TILE)); ty < ty1; ty++) {
|
|
160
|
+
for (let tx = Math.max(0, Math.floor(x / TILE)); tx < tx1; tx++) {
|
|
161
|
+
const i = ty * this.tilesX + tx;
|
|
162
|
+
if (!this.dirty[i]) {
|
|
163
|
+
this.dirty[i] = 1;
|
|
164
|
+
this.dirtyCount++;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** 按面积平均:每个输出像素取它盖住的那几个原画像素的平均 */
|
|
171
|
+
private downscale(ox0: number, oy0: number, ox1: number, oy1: number): void {
|
|
172
|
+
const { src, out, srcW, outW, x0, x1, y0, y1 } = this;
|
|
173
|
+
for (let oy = oy0; oy < oy1; oy++) {
|
|
174
|
+
const sy0 = y0[oy]!;
|
|
175
|
+
const sy1 = y1[oy]!;
|
|
176
|
+
let o = (oy * outW + ox0) * 4;
|
|
177
|
+
for (let ox = ox0; ox < ox1; ox++, o += 4) {
|
|
178
|
+
const sx0 = x0[ox]!;
|
|
179
|
+
const sx1 = x1[ox]!;
|
|
180
|
+
let r = 0;
|
|
181
|
+
let g = 0;
|
|
182
|
+
let b = 0;
|
|
183
|
+
for (let sy = sy0; sy < sy1; sy++) {
|
|
184
|
+
let i = (sy * srcW + sx0) * 4;
|
|
185
|
+
for (let sx = sx0; sx < sx1; sx++, i += 4) {
|
|
186
|
+
r += src[i]!;
|
|
187
|
+
g += src[i + 1]!;
|
|
188
|
+
b += src[i + 2]!;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
const n = (sx1 - sx0) * (sy1 - sy0);
|
|
192
|
+
out[o] = (r / n + 0.5) | 0;
|
|
193
|
+
out[o + 1] = (g / n + 0.5) | 0;
|
|
194
|
+
out[o + 2] = (b / n + 0.5) | 0;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** 把脏格子拼成矩形(横着连成一段,上下段一样宽的再并起来),清掉脏标记 */
|
|
200
|
+
takeDirty(): Rect[] {
|
|
201
|
+
if (!this.dirtyCount) return [];
|
|
202
|
+
const done: Rect[] = [];
|
|
203
|
+
/** 上一行的段:key 是 "起始格,结束格",还能往下接 */
|
|
204
|
+
let open = new Map<string, { tx0: number; tx1: number; ty0: number; ty1: number }>();
|
|
205
|
+
const close = (s: { tx0: number; tx1: number; ty0: number; ty1: number }) => {
|
|
206
|
+
const x = s.tx0 * TILE;
|
|
207
|
+
const y = s.ty0 * TILE;
|
|
208
|
+
done.push({ x, y, w: Math.min(this.outW, s.tx1 * TILE) - x, h: Math.min(this.outH, s.ty1 * TILE) - y });
|
|
209
|
+
};
|
|
210
|
+
for (let ty = 0; ty < this.tilesY; ty++) {
|
|
211
|
+
const next = new Map<string, { tx0: number; tx1: number; ty0: number; ty1: number }>();
|
|
212
|
+
for (let tx = 0; tx < this.tilesX; ) {
|
|
213
|
+
if (!this.dirty[ty * this.tilesX + tx]) {
|
|
214
|
+
tx++;
|
|
215
|
+
continue;
|
|
216
|
+
}
|
|
217
|
+
const tx0 = tx;
|
|
218
|
+
while (tx < this.tilesX && this.dirty[ty * this.tilesX + tx]) tx++;
|
|
219
|
+
const key = `${tx0},${tx}`;
|
|
220
|
+
const prev = open.get(key);
|
|
221
|
+
if (prev && prev.ty1 - prev.ty0 < MAX_RECT_TILES) {
|
|
222
|
+
prev.ty1 = ty + 1;
|
|
223
|
+
open.delete(key);
|
|
224
|
+
next.set(key, prev);
|
|
225
|
+
} else next.set(key, { tx0, tx1: tx, ty0: ty, ty1: ty + 1 });
|
|
226
|
+
}
|
|
227
|
+
for (const s of open.values()) close(s);
|
|
228
|
+
open = next;
|
|
229
|
+
}
|
|
230
|
+
for (const s of open.values()) close(s);
|
|
231
|
+
this.dirty.fill(0);
|
|
232
|
+
this.dirtyCount = 0;
|
|
233
|
+
return done;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/** 输出画面上一块,拷成紧凑的 RGBX(给 sharp) */
|
|
237
|
+
region(r: Rect): Buffer {
|
|
238
|
+
const buf = Buffer.allocUnsafe(r.w * r.h * 4);
|
|
239
|
+
for (let row = 0; row < r.h; row++) {
|
|
240
|
+
const start = ((r.y + row) * this.outW + r.x) * 4;
|
|
241
|
+
buf.set(this.out.subarray(start, start + r.w * 4), row * r.w * 4);
|
|
242
|
+
}
|
|
243
|
+
return buf;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/** 输出画面的坐标 → 原画的坐标(鼠标) */
|
|
247
|
+
toSrc(x: number, y: number): [number, number] {
|
|
248
|
+
const sx = Math.floor(((x + 0.5) * this.srcW) / this.outW);
|
|
249
|
+
const sy = Math.floor(((y + 0.5) * this.srcH) / this.outH);
|
|
250
|
+
return [Math.min(this.srcW - 1, Math.max(0, sx)), Math.min(this.srcH - 1, Math.max(0, sy))];
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// ---------- ZRLE ----------
|
|
255
|
+
|
|
256
|
+
/** ZRLE 整条连接共用一个 zlib 流,每个矩形的数据都以 sync flush 结尾 */
|
|
257
|
+
class Inflater {
|
|
258
|
+
private z = zlib.createInflate();
|
|
259
|
+
private chunks: Buffer[] = [];
|
|
260
|
+
private error: Error | null = null;
|
|
261
|
+
|
|
262
|
+
constructor() {
|
|
263
|
+
this.z.on("data", (d: Buffer) => this.chunks.push(d));
|
|
264
|
+
this.z.on("error", (e) => (this.error = e));
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
inflate(data: Buffer): Promise<Buffer> {
|
|
268
|
+
return new Promise((resolve, reject) => {
|
|
269
|
+
this.z.write(data);
|
|
270
|
+
this.z.flush(zlib.constants.Z_SYNC_FLUSH, () => {
|
|
271
|
+
if (this.error) return reject(this.error);
|
|
272
|
+
let c: Buffer | null;
|
|
273
|
+
while ((c = this.z.read() as Buffer | null) !== null) this.chunks.push(c);
|
|
274
|
+
const out = Buffer.concat(this.chunks);
|
|
275
|
+
this.chunks = [];
|
|
276
|
+
resolve(out);
|
|
277
|
+
});
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
close(): void {
|
|
282
|
+
this.z.destroy();
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/** 解一个 w×h 的 ZRLE 矩形到 fb(紧凑的 R G B X)。像素格式是我们定的 32 位,CPIXEL 是前 3 个字节 */
|
|
287
|
+
export function decodeZrle(d: Buffer, fb: Uint8Array, w: number, h: number): void {
|
|
288
|
+
const W = w;
|
|
289
|
+
const x = 0;
|
|
290
|
+
const y = 0;
|
|
291
|
+
let p = 0;
|
|
292
|
+
const palette = new Uint8Array(128 * 3);
|
|
293
|
+
const runLength = () => {
|
|
294
|
+
let n = 1;
|
|
295
|
+
let b: number;
|
|
296
|
+
do {
|
|
297
|
+
b = d[p++]!;
|
|
298
|
+
n += b;
|
|
299
|
+
} while (b === 255);
|
|
300
|
+
return n;
|
|
301
|
+
};
|
|
302
|
+
for (let ty = y; ty < y + h; ty += 64) {
|
|
303
|
+
const th = Math.min(64, y + h - ty);
|
|
304
|
+
for (let tx = x; tx < x + w; tx += 64) {
|
|
305
|
+
const tw = Math.min(64, x + w - tx);
|
|
306
|
+
const sub = d[p++]!;
|
|
307
|
+
if (sub === 0) {
|
|
308
|
+
for (let j = 0; j < th; j++) {
|
|
309
|
+
let i = ((ty + j) * W + tx) * 4;
|
|
310
|
+
for (let k = 0; k < tw; k++, i += 4, p += 3) {
|
|
311
|
+
fb[i] = d[p]!;
|
|
312
|
+
fb[i + 1] = d[p + 1]!;
|
|
313
|
+
fb[i + 2] = d[p + 2]!;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
} else if (sub === 1) {
|
|
317
|
+
const r = d[p]!;
|
|
318
|
+
const g = d[p + 1]!;
|
|
319
|
+
const b = d[p + 2]!;
|
|
320
|
+
p += 3;
|
|
321
|
+
for (let j = 0; j < th; j++) {
|
|
322
|
+
let i = ((ty + j) * W + tx) * 4;
|
|
323
|
+
for (let k = 0; k < tw; k++, i += 4) {
|
|
324
|
+
fb[i] = r;
|
|
325
|
+
fb[i + 1] = g;
|
|
326
|
+
fb[i + 2] = b;
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
} else if (sub <= 16) {
|
|
330
|
+
// 调色板 + 每像素 1 / 2 / 4 位的索引,每行补齐到整字节,高位在前
|
|
331
|
+
for (let c = 0; c < sub * 3; c++) palette[c] = d[p++]!;
|
|
332
|
+
const bits = sub === 2 ? 1 : sub <= 4 ? 2 : 4;
|
|
333
|
+
const mask = (1 << bits) - 1;
|
|
334
|
+
for (let j = 0; j < th; j++) {
|
|
335
|
+
let i = ((ty + j) * W + tx) * 4;
|
|
336
|
+
let byte = 0;
|
|
337
|
+
let left = 0;
|
|
338
|
+
for (let k = 0; k < tw; k++, i += 4) {
|
|
339
|
+
if (left === 0) {
|
|
340
|
+
byte = d[p++]!;
|
|
341
|
+
left = 8;
|
|
342
|
+
}
|
|
343
|
+
left -= bits;
|
|
344
|
+
const c = ((byte >> left) & mask) * 3;
|
|
345
|
+
fb[i] = palette[c]!;
|
|
346
|
+
fb[i + 1] = palette[c + 1]!;
|
|
347
|
+
fb[i + 2] = palette[c + 2]!;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
} else if (sub === 128 || sub >= 130) {
|
|
351
|
+
// 游程:128 是颜色 + 长度;130 以上是调色板索引(高位为 1 时后面跟长度)
|
|
352
|
+
const paletted = sub >= 130;
|
|
353
|
+
if (paletted) for (let c = 0; c < (sub - 128) * 3; c++) palette[c] = d[p++]!;
|
|
354
|
+
const total = tw * th;
|
|
355
|
+
let n = 0;
|
|
356
|
+
while (n < total) {
|
|
357
|
+
let r: number;
|
|
358
|
+
let g: number;
|
|
359
|
+
let b: number;
|
|
360
|
+
let run = 1;
|
|
361
|
+
if (paletted) {
|
|
362
|
+
const v = d[p++]!;
|
|
363
|
+
const c = (v & 127) * 3;
|
|
364
|
+
r = palette[c]!;
|
|
365
|
+
g = palette[c + 1]!;
|
|
366
|
+
b = palette[c + 2]!;
|
|
367
|
+
if (v & 128) run = runLength();
|
|
368
|
+
} else {
|
|
369
|
+
r = d[p]!;
|
|
370
|
+
g = d[p + 1]!;
|
|
371
|
+
b = d[p + 2]!;
|
|
372
|
+
p += 3;
|
|
373
|
+
run = runLength();
|
|
374
|
+
}
|
|
375
|
+
const end = Math.min(total, n + run);
|
|
376
|
+
for (; n < end; n++) {
|
|
377
|
+
const i = ((ty + ((n / tw) | 0)) * W + tx + (n % tw)) * 4;
|
|
378
|
+
fb[i] = r;
|
|
379
|
+
fb[i + 1] = g;
|
|
380
|
+
fb[i + 2] = b;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
} else throw new Error(`ZRLE 里有不认识的子编码 ${sub}`);
|
|
384
|
+
if (p > d.length) throw new Error("ZRLE 数据不完整");
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
// ---------- 会话 ----------
|
|
390
|
+
|
|
391
|
+
export type Endpoint = { reader: ByteReader; write: (b: Buffer) => void };
|
|
392
|
+
export type TranscodeOptions = {
|
|
393
|
+
vnc: Endpoint;
|
|
394
|
+
/** backlog:已经交给页面、还没发出去的字节数(WebSocket 的 bufferedAmount) */
|
|
395
|
+
page: Endpoint & { backlog: () => number };
|
|
396
|
+
creds: VncCreds;
|
|
397
|
+
quality: DesktopQuality;
|
|
398
|
+
/** VNC 服务在本机:要原始像素(见文件开头) */
|
|
399
|
+
local: boolean;
|
|
400
|
+
/** 两头都握手完了(之后就是正常收发画面) */
|
|
401
|
+
onReady?: () => void;
|
|
402
|
+
};
|
|
403
|
+
|
|
404
|
+
/** 「流畅」档和本机的 VNC 服务之间用 16 位色(RGB565):少一半数据,macOS 出图快三成;缩小、压 JPEG 后看不出来 */
|
|
405
|
+
function pixelFormat16(): Buffer {
|
|
406
|
+
const b = Buffer.alloc(16);
|
|
407
|
+
b[0] = 16;
|
|
408
|
+
b[1] = 16;
|
|
409
|
+
b[3] = 1;
|
|
410
|
+
b.writeUInt16BE(31, 4);
|
|
411
|
+
b.writeUInt16BE(63, 6);
|
|
412
|
+
b.writeUInt16BE(31, 8);
|
|
413
|
+
b[10] = 11;
|
|
414
|
+
b[11] = 5;
|
|
415
|
+
b[12] = 0;
|
|
416
|
+
return b;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/** RGB565(小端)→ R G B X */
|
|
420
|
+
function expand565(src: Buffer, out: Buffer, n: number): void {
|
|
421
|
+
for (let i = 0, o = 0; i < n; i++, o += 4) {
|
|
422
|
+
const v = src[i * 2]! | (src[i * 2 + 1]! << 8);
|
|
423
|
+
const r = v >> 11;
|
|
424
|
+
const g = (v >> 5) & 63;
|
|
425
|
+
const b = v & 31;
|
|
426
|
+
out[o] = (r << 3) | (r >> 2);
|
|
427
|
+
out[o + 1] = (g << 2) | (g >> 4);
|
|
428
|
+
out[o + 2] = (b << 3) | (b >> 2);
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
/** 我们和页面都用的像素格式:32 位、24 位色深、小端、真彩色,R G B 依次在低到高的字节 */
|
|
433
|
+
function pixelFormat(): Buffer {
|
|
434
|
+
const b = Buffer.alloc(16);
|
|
435
|
+
b[0] = 32;
|
|
436
|
+
b[1] = 24;
|
|
437
|
+
b[2] = 0;
|
|
438
|
+
b[3] = 1;
|
|
439
|
+
b.writeUInt16BE(255, 4);
|
|
440
|
+
b.writeUInt16BE(255, 6);
|
|
441
|
+
b.writeUInt16BE(255, 8);
|
|
442
|
+
b[10] = 0;
|
|
443
|
+
b[11] = 8;
|
|
444
|
+
b[12] = 16;
|
|
445
|
+
return b;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
function rectHeader(x: number, y: number, w: number, h: number, enc: number): Buffer {
|
|
449
|
+
const b = Buffer.alloc(12);
|
|
450
|
+
b.writeUInt16BE(x, 0);
|
|
451
|
+
b.writeUInt16BE(y, 2);
|
|
452
|
+
b.writeUInt16BE(w, 4);
|
|
453
|
+
b.writeUInt16BE(h, 6);
|
|
454
|
+
b.writeInt32BE(enc, 8);
|
|
455
|
+
return b;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/** Tight 的长度:每字节 7 位,高位为 1 表示后面还有,最多 3 字节 */
|
|
459
|
+
function compactLength(n: number): Buffer {
|
|
460
|
+
if (n < 0x80) return Buffer.from([n]);
|
|
461
|
+
if (n < 0x4000) return Buffer.from([(n & 0x7f) | 0x80, n >> 7]);
|
|
462
|
+
return Buffer.from([(n & 0x7f) | 0x80, ((n >> 7) & 0x7f) | 0x80, n >> 14]);
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
type Cursor = { hotX: number; hotY: number; w: number; h: number; pixels: Buffer; mask: Buffer };
|
|
466
|
+
|
|
467
|
+
/** 光标跟着画面一起缩(最小缩到 16 像素),最近邻 */
|
|
468
|
+
function scaleCursor(c: Cursor, k: number): Cursor {
|
|
469
|
+
const s = Math.min(1, Math.max(k, 16 / Math.max(c.w, c.h, 1)));
|
|
470
|
+
if (s >= 1) return c;
|
|
471
|
+
const w = Math.max(1, Math.round(c.w * s));
|
|
472
|
+
const h = Math.max(1, Math.round(c.h * s));
|
|
473
|
+
const srcStride = Math.ceil(c.w / 8);
|
|
474
|
+
const stride = Math.ceil(w / 8);
|
|
475
|
+
const pixels = Buffer.alloc(w * h * 4);
|
|
476
|
+
const mask = Buffer.alloc(stride * h);
|
|
477
|
+
for (let y = 0; y < h; y++) {
|
|
478
|
+
const sy = Math.min(c.h - 1, Math.floor(y / s));
|
|
479
|
+
for (let x = 0; x < w; x++) {
|
|
480
|
+
const sx = Math.min(c.w - 1, Math.floor(x / s));
|
|
481
|
+
c.pixels.copy(pixels, (y * w + x) * 4, (sy * c.w + sx) * 4, (sy * c.w + sx) * 4 + 4);
|
|
482
|
+
if (c.mask[sy * srcStride + (sx >> 3)]! & (0x80 >> (sx & 7))) mask[y * stride + (x >> 3)]! |= 0x80 >> (x & 7);
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
return { hotX: Math.min(w - 1, Math.round(c.hotX * s)), hotY: Math.min(h - 1, Math.round(c.hotY * s)), w, h, pixels, mask };
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
type Sharp = (typeof import("sharp"))["default"];
|
|
489
|
+
let sharpLoad: Promise<Sharp> | null = null;
|
|
490
|
+
/** sharp 是原生模块:用到时才加载,装不上时只影响转码(调用方退回原样转发) */
|
|
491
|
+
export function loadSharp(): Promise<Sharp> {
|
|
492
|
+
sharpLoad ??= import("sharp").then((m) => m.default);
|
|
493
|
+
sharpLoad.catch(() => (sharpLoad = null));
|
|
494
|
+
return sharpLoad;
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
/**
|
|
498
|
+
* 跑一个转码会话,直到哪一头断开。认证失败时已经按 VNC 的认证失败告诉页面了,再抛出来。
|
|
499
|
+
*/
|
|
500
|
+
export async function transcode(opts: TranscodeOptions): Promise<void> {
|
|
501
|
+
const { vnc, page, creds } = opts;
|
|
502
|
+
const preset = DESKTOP_QUALITIES[opts.quality];
|
|
503
|
+
const sharp = await loadSharp();
|
|
504
|
+
|
|
505
|
+
// 先和 VNC 服务登录、拿到画面大小;同时对页面走完不用认证的握手
|
|
506
|
+
const login = (async () => {
|
|
507
|
+
await vncLogin(vnc.reader, vnc.write, creds);
|
|
508
|
+
// ClientInit:共享(别把别人踢下线)
|
|
509
|
+
vnc.write(Buffer.from([1]));
|
|
510
|
+
const init = await vnc.reader.read(24);
|
|
511
|
+
const name = await vnc.reader.read(init.readUInt32BE(20));
|
|
512
|
+
return { w: init.readUInt16BE(0), h: init.readUInt16BE(2), name };
|
|
513
|
+
})();
|
|
514
|
+
login.catch(() => {});
|
|
515
|
+
await serveNoAuth(
|
|
516
|
+
page.reader,
|
|
517
|
+
page.write,
|
|
518
|
+
login.then(() => {}),
|
|
519
|
+
);
|
|
520
|
+
const server = await login;
|
|
521
|
+
const frame = new Frame(server.w, server.h, preset.edge);
|
|
522
|
+
|
|
523
|
+
// 我们这头要的格式和编码
|
|
524
|
+
const bpp = opts.local && opts.quality === "smooth" ? 2 : 4;
|
|
525
|
+
vnc.write(Buffer.concat([Buffer.from([0, 0, 0, 0]), bpp === 2 ? pixelFormat16() : pixelFormat()]));
|
|
526
|
+
// 光标形状按我们的像素格式发:16 位时不要(noVNC 会画一个小圆点)
|
|
527
|
+
const encs = [
|
|
528
|
+
...(opts.local ? [ENC_RAW, ENC_COPYRECT] : [ENC_ZRLE, ENC_COPYRECT, ENC_RAW]),
|
|
529
|
+
ENC_DESKTOP_SIZE,
|
|
530
|
+
...(bpp === 4 ? [ENC_CURSOR] : []),
|
|
531
|
+
];
|
|
532
|
+
const se = Buffer.alloc(4 + encs.length * 4);
|
|
533
|
+
se[0] = 2;
|
|
534
|
+
se.writeUInt16BE(encs.length, 2);
|
|
535
|
+
encs.forEach((e, i) => se.writeInt32BE(e, 4 + i * 4));
|
|
536
|
+
vnc.write(se);
|
|
537
|
+
|
|
538
|
+
// 页面的 ClientInit,回 ServerInit(缩小后的大小)
|
|
539
|
+
await page.reader.read(1);
|
|
540
|
+
const nameLen = Buffer.alloc(4);
|
|
541
|
+
nameLen.writeUInt32BE(server.name.length);
|
|
542
|
+
page.write(Buffer.concat([Buffer.from([frame.outW >> 8, frame.outW & 255, frame.outH >> 8, frame.outH & 255]), pixelFormat(), nameLen, server.name]));
|
|
543
|
+
opts.onReady?.();
|
|
544
|
+
|
|
545
|
+
// ---------- 状态 ----------
|
|
546
|
+
let closed = false;
|
|
547
|
+
/** 页面支持什么 */
|
|
548
|
+
const pageEnc = { tight: false, cursor: false, desktopSize: false };
|
|
549
|
+
/** 页面的像素格式和我们的一样(光标像素要按它的格式发) */
|
|
550
|
+
let pageFormatOk = true;
|
|
551
|
+
/** 页面在等下一帧 */
|
|
552
|
+
let wanted = false;
|
|
553
|
+
let sending = false;
|
|
554
|
+
let sizeChanged = false;
|
|
555
|
+
let cursor: Cursor | null = null;
|
|
556
|
+
let cursorChanged = false;
|
|
557
|
+
/** VNC 服务的第一帧到了:之前原画还是全黑,别发 */
|
|
558
|
+
let gotFirst = false;
|
|
559
|
+
|
|
560
|
+
const pump = (): void => {
|
|
561
|
+
if (closed || !wanted || sending) return;
|
|
562
|
+
if (!pageEnc.tight || !gotFirst) return;
|
|
563
|
+
if (page.backlog() > HIGH_WATER) {
|
|
564
|
+
setTimeout(pump, 30);
|
|
565
|
+
return;
|
|
566
|
+
}
|
|
567
|
+
const sendCursor = cursorChanged && cursor && pageEnc.cursor && pageFormatOk;
|
|
568
|
+
if (!frame.hasDirty && !sizeChanged && !sendCursor) return;
|
|
569
|
+
wanted = false;
|
|
570
|
+
sending = true;
|
|
571
|
+
const parts: Buffer[] = [];
|
|
572
|
+
let count = 0;
|
|
573
|
+
if (sizeChanged && pageEnc.desktopSize) {
|
|
574
|
+
parts.push(rectHeader(0, 0, frame.outW, frame.outH, ENC_DESKTOP_SIZE));
|
|
575
|
+
count++;
|
|
576
|
+
}
|
|
577
|
+
sizeChanged = false;
|
|
578
|
+
if (sendCursor) {
|
|
579
|
+
const c = scaleCursor(cursor!, frame.outW / frame.srcW);
|
|
580
|
+
parts.push(rectHeader(c.hotX, c.hotY, c.w, c.h, ENC_CURSOR), c.pixels, c.mask);
|
|
581
|
+
count++;
|
|
582
|
+
}
|
|
583
|
+
cursorChanged = false;
|
|
584
|
+
// 先同步把要发的像素拷出来,压 JPEG 时画面再变也不影响这一帧
|
|
585
|
+
const rects = frame.takeDirty().map((r) => ({ r, raw: frame.region(r) }));
|
|
586
|
+
void Promise.all(
|
|
587
|
+
rects.map(({ r, raw }) =>
|
|
588
|
+
sharp(raw, { raw: { width: r.w, height: r.h, channels: 4 } })
|
|
589
|
+
.removeAlpha()
|
|
590
|
+
.jpeg({ quality: preset.jpeg })
|
|
591
|
+
.toBuffer(),
|
|
592
|
+
),
|
|
593
|
+
).then(
|
|
594
|
+
(jpegs) => {
|
|
595
|
+
sending = false;
|
|
596
|
+
if (closed) return;
|
|
597
|
+
rects.forEach(({ r }, i) => {
|
|
598
|
+
parts.push(rectHeader(r.x, r.y, r.w, r.h, ENC_TIGHT), Buffer.from([0x90]), compactLength(jpegs[i]!.length), jpegs[i]!);
|
|
599
|
+
});
|
|
600
|
+
count += rects.length;
|
|
601
|
+
const head = Buffer.alloc(4);
|
|
602
|
+
head.writeUInt16BE(count, 2);
|
|
603
|
+
page.write(Buffer.concat([head, ...parts]));
|
|
604
|
+
// 发的这会儿里页面又要了(不太会)、画面又变了:接着发
|
|
605
|
+
pump();
|
|
606
|
+
},
|
|
607
|
+
(err) => {
|
|
608
|
+
sending = false;
|
|
609
|
+
fail(err);
|
|
610
|
+
},
|
|
611
|
+
);
|
|
612
|
+
};
|
|
613
|
+
|
|
614
|
+
let lastRequest = 0;
|
|
615
|
+
const requestVnc = (incremental: boolean): void => {
|
|
616
|
+
if (closed) return;
|
|
617
|
+
lastRequest = Date.now();
|
|
618
|
+
const b = Buffer.alloc(10);
|
|
619
|
+
b[0] = 3;
|
|
620
|
+
b[1] = incremental ? 1 : 0;
|
|
621
|
+
b.writeUInt16BE(frame.srcW, 6);
|
|
622
|
+
b.writeUInt16BE(frame.srcH, 8);
|
|
623
|
+
vnc.write(b);
|
|
624
|
+
};
|
|
625
|
+
|
|
626
|
+
let failure: ((err: unknown) => void) | null = null;
|
|
627
|
+
const fail = (err: unknown): void => failure?.(err);
|
|
628
|
+
const done = new Promise<void>((_resolve, reject) => {
|
|
629
|
+
failure = (err) => {
|
|
630
|
+
if (closed) return;
|
|
631
|
+
closed = true;
|
|
632
|
+
reject(err instanceof Error ? err : new Error(String(err)));
|
|
633
|
+
};
|
|
634
|
+
});
|
|
635
|
+
|
|
636
|
+
// ---------- VNC 服务 → 我们 ----------
|
|
637
|
+
/** 解码用的暂存:一帧整屏几十 MB,别每次都新分配 */
|
|
638
|
+
let scratch = Buffer.alloc(0);
|
|
639
|
+
const scratchOf = (n: number) => {
|
|
640
|
+
if (scratch.length < n) scratch = Buffer.allocUnsafe(n);
|
|
641
|
+
return scratch.subarray(0, n);
|
|
642
|
+
};
|
|
643
|
+
const fromVnc = async () => {
|
|
644
|
+
const inflater = new Inflater();
|
|
645
|
+
try {
|
|
646
|
+
requestVnc(false);
|
|
647
|
+
for (;;) {
|
|
648
|
+
const type = (await vnc.reader.read(1))[0];
|
|
649
|
+
if (type === 0) {
|
|
650
|
+
const n = (await vnc.reader.read(3)).readUInt16BE(1);
|
|
651
|
+
for (let i = 0; i < n; i++) {
|
|
652
|
+
const hd = await vnc.reader.read(12);
|
|
653
|
+
const x = hd.readUInt16BE(0);
|
|
654
|
+
const y = hd.readUInt16BE(2);
|
|
655
|
+
const w = hd.readUInt16BE(4);
|
|
656
|
+
const h = hd.readUInt16BE(6);
|
|
657
|
+
const enc = hd.readInt32BE(8);
|
|
658
|
+
if (enc === ENC_DESKTOP_SIZE) {
|
|
659
|
+
frame.resize(w, h);
|
|
660
|
+
sizeChanged = true;
|
|
661
|
+
continue;
|
|
662
|
+
}
|
|
663
|
+
if (enc === ENC_CURSOR) {
|
|
664
|
+
const pixels = await vnc.reader.read(w * h * 4);
|
|
665
|
+
const mask = await vnc.reader.read(Math.ceil(w / 8) * h);
|
|
666
|
+
cursor = { hotX: x, hotY: y, w, h, pixels, mask };
|
|
667
|
+
cursorChanged = true;
|
|
668
|
+
continue;
|
|
669
|
+
}
|
|
670
|
+
if (x + w > frame.srcW || y + h > frame.srcH) throw new Error("VNC 服务发来的矩形超出了画面");
|
|
671
|
+
if (enc === ENC_ZRLE) {
|
|
672
|
+
const len = (await vnc.reader.read(4)).readUInt32BE(0);
|
|
673
|
+
const px = scratchOf(w * h * 4);
|
|
674
|
+
decodeZrle(await inflater.inflate(await vnc.reader.read(len)), px, w, h);
|
|
675
|
+
frame.update(x, y, w, h, px);
|
|
676
|
+
} else if (enc === ENC_RAW) {
|
|
677
|
+
const data = await vnc.reader.read(w * h * bpp);
|
|
678
|
+
if (bpp === 2) {
|
|
679
|
+
const px = scratchOf(w * h * 4);
|
|
680
|
+
expand565(data, px, w * h);
|
|
681
|
+
frame.update(x, y, w, h, px);
|
|
682
|
+
} else frame.update(x, y, w, h, data);
|
|
683
|
+
} else if (enc === ENC_COPYRECT) {
|
|
684
|
+
const s = await vnc.reader.read(4);
|
|
685
|
+
const sx = s.readUInt16BE(0);
|
|
686
|
+
const sy = s.readUInt16BE(2);
|
|
687
|
+
const stride = frame.srcW * 4;
|
|
688
|
+
// 往下挪时从下往上拷,免得覆盖还没拷的
|
|
689
|
+
const rows = [...Array(h).keys()];
|
|
690
|
+
if (y > sy) rows.reverse();
|
|
691
|
+
for (const row of rows) {
|
|
692
|
+
const from = (sy + row) * stride + sx * 4;
|
|
693
|
+
frame.src.copyWithin((y + row) * stride + x * 4, from, from + w * 4);
|
|
694
|
+
}
|
|
695
|
+
frame.damage(x, y, w, h);
|
|
696
|
+
} else throw new Error(`VNC 服务用了不认识的编码 ${enc}`);
|
|
697
|
+
}
|
|
698
|
+
gotFirst = true;
|
|
699
|
+
pump();
|
|
700
|
+
// 下一帧:本机的 VNC 服务会等到画面有变化才回
|
|
701
|
+
setTimeout(() => requestVnc(true), Math.max(0, FRAME_MS - (Date.now() - lastRequest)));
|
|
702
|
+
} else if (type === 1) {
|
|
703
|
+
const n = (await vnc.reader.read(5)).readUInt16BE(3);
|
|
704
|
+
await vnc.reader.read(n * 6);
|
|
705
|
+
} else if (type === 2) {
|
|
706
|
+
page.write(Buffer.from([2]));
|
|
707
|
+
} else if (type === 3) {
|
|
708
|
+
const head = await vnc.reader.read(7);
|
|
709
|
+
const text = await vnc.reader.read(head.readUInt32BE(3));
|
|
710
|
+
page.write(Buffer.concat([Buffer.from([3]), head, text]));
|
|
711
|
+
} else throw new Error(`VNC 服务发来了不认识的消息 ${type}`);
|
|
712
|
+
}
|
|
713
|
+
} finally {
|
|
714
|
+
inflater.close();
|
|
715
|
+
}
|
|
716
|
+
};
|
|
717
|
+
|
|
718
|
+
// ---------- 页面 → 我们 ----------
|
|
719
|
+
const fromPage = async () => {
|
|
720
|
+
for (;;) {
|
|
721
|
+
const type = (await page.reader.read(1))[0];
|
|
722
|
+
if (type === 0) {
|
|
723
|
+
const pf = (await page.reader.read(19)).subarray(3);
|
|
724
|
+
pageFormatOk = pf[0] === 32 && pf[2] === 0 && pf[3] === 1 && pf[10] === 0 && pf[11] === 8 && pf[12] === 16;
|
|
725
|
+
} else if (type === 2) {
|
|
726
|
+
const n = (await page.reader.read(3)).readUInt16BE(1);
|
|
727
|
+
const list = await page.reader.read(n * 4);
|
|
728
|
+
const set = new Set(Array.from({ length: n }, (_, i) => list.readInt32BE(i * 4)));
|
|
729
|
+
pageEnc.tight = set.has(ENC_TIGHT);
|
|
730
|
+
pageEnc.cursor = set.has(ENC_CURSOR);
|
|
731
|
+
pageEnc.desktopSize = set.has(ENC_DESKTOP_SIZE);
|
|
732
|
+
if (!pageEnc.tight) throw new Error("页面不支持 Tight 编码");
|
|
733
|
+
cursorChanged = !!cursor;
|
|
734
|
+
} else if (type === 3) {
|
|
735
|
+
const req = await page.reader.read(9);
|
|
736
|
+
if (!req[0]) frame.markOut(req.readUInt16BE(1), req.readUInt16BE(3), req.readUInt16BE(5), req.readUInt16BE(7));
|
|
737
|
+
wanted = true;
|
|
738
|
+
pump();
|
|
739
|
+
} else if (type === 4) {
|
|
740
|
+
vnc.write(Buffer.concat([Buffer.from([4]), await page.reader.read(7)]));
|
|
741
|
+
} else if (type === 5) {
|
|
742
|
+
const ev = await page.reader.read(5);
|
|
743
|
+
const [sx, sy] = frame.toSrc(ev.readUInt16BE(1), ev.readUInt16BE(3));
|
|
744
|
+
const out = Buffer.alloc(6);
|
|
745
|
+
out[0] = 5;
|
|
746
|
+
out[1] = ev[0]!;
|
|
747
|
+
out.writeUInt16BE(sx, 2);
|
|
748
|
+
out.writeUInt16BE(sy, 4);
|
|
749
|
+
vnc.write(out);
|
|
750
|
+
} else if (type === 6) {
|
|
751
|
+
const head = await page.reader.read(7);
|
|
752
|
+
const text = await page.reader.read(head.readUInt32BE(3));
|
|
753
|
+
vnc.write(Buffer.concat([Buffer.from([6]), head, text]));
|
|
754
|
+
} else throw new Error(`页面发来了不认识的消息 ${type}`);
|
|
755
|
+
}
|
|
756
|
+
};
|
|
757
|
+
|
|
758
|
+
fromVnc().catch(fail);
|
|
759
|
+
fromPage().catch(fail);
|
|
760
|
+
try {
|
|
761
|
+
await done;
|
|
762
|
+
} finally {
|
|
763
|
+
closed = true;
|
|
764
|
+
}
|
|
765
|
+
}
|