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,193 @@
|
|
|
1
|
+
// 由服务端登录 VNC 服务,再对页面(noVNC)装成一个不用认证的 VNC 服务。两种情况用得上:
|
|
2
|
+
// - macOS 屏幕共享的苹果认证(Apple Remote Desktop,RFB 安全类型 30):用 Mac 的用户名和登录密码登录。
|
|
3
|
+
// 屏幕共享没开「VNC 显示程序可以使用密码控制屏幕」时只给苹果自己的几种认证,普通 VNC 密码那种没有。
|
|
4
|
+
// 登录密码不用发到浏览器;noVNC 自己做这种认证要用 crypto.subtle,用 http://IP 访问时没有。
|
|
5
|
+
// - 转码(vnc-transcode.ts):服务端自己当 VNC 客户端,普通 VNC 密码、不用认证的也都由这里登录。
|
|
6
|
+
//
|
|
7
|
+
// 苹果认证:服务端给 DH 的生成元、素数和它的公钥;双方算出共享密钥,MD5 一下当 AES-128 的钥匙,
|
|
8
|
+
// 把「用户名 \0 … | 密码 \0 …」各 64 字节(空的地方填随机数)用 AES-ECB 加密,连同自己的公钥发回去。
|
|
9
|
+
import crypto from "node:crypto";
|
|
10
|
+
import { createRequire } from "node:module";
|
|
11
|
+
import path from "node:path";
|
|
12
|
+
import { pathToFileURL } from "node:url";
|
|
13
|
+
|
|
14
|
+
/** 按需读字节:数据一段段进来(TCP 的 data、WebSocket 的 message),要多少等多少 */
|
|
15
|
+
export class ByteReader {
|
|
16
|
+
/** 收到还没读的:一段段排着,读的时候才拼(一帧几十 MB 时每来一段就整个拼一遍会慢得要命) */
|
|
17
|
+
private chunks: Buffer[] = [];
|
|
18
|
+
/** 第一段里已经读掉的字节数 */
|
|
19
|
+
private offset = 0;
|
|
20
|
+
private length = 0;
|
|
21
|
+
private waiting: { n: number; resolve: (b: Buffer) => void; reject: (e: Error) => void } | null = null;
|
|
22
|
+
private error: Error | null = null;
|
|
23
|
+
|
|
24
|
+
push(chunk: Buffer): void {
|
|
25
|
+
if (!chunk.length) return;
|
|
26
|
+
this.chunks.push(chunk);
|
|
27
|
+
this.length += chunk.length;
|
|
28
|
+
this.flush();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** 那头断了:在等的和以后要读的都报这个错 */
|
|
32
|
+
fail(err: Error): void {
|
|
33
|
+
this.error ??= err;
|
|
34
|
+
const w = this.waiting;
|
|
35
|
+
this.waiting = null;
|
|
36
|
+
w?.reject(this.error);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
read(n: number): Promise<Buffer> {
|
|
40
|
+
if (this.waiting) return Promise.reject(new Error("上一次读还没完"));
|
|
41
|
+
return new Promise((resolve, reject) => {
|
|
42
|
+
this.waiting = { n, resolve, reject };
|
|
43
|
+
this.flush();
|
|
44
|
+
if (this.waiting && this.error) this.fail(this.error);
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** 握手完了:多读进来的那点交给后面原样转发 */
|
|
49
|
+
rest(): Buffer {
|
|
50
|
+
return this.take(this.length);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
private take(n: number): Buffer {
|
|
54
|
+
const out = Buffer.allocUnsafe(n);
|
|
55
|
+
let filled = 0;
|
|
56
|
+
while (filled < n) {
|
|
57
|
+
const c = this.chunks[0]!;
|
|
58
|
+
const k = Math.min(n - filled, c.length - this.offset);
|
|
59
|
+
c.copy(out, filled, this.offset, this.offset + k);
|
|
60
|
+
filled += k;
|
|
61
|
+
this.offset += k;
|
|
62
|
+
if (this.offset === c.length) {
|
|
63
|
+
this.chunks.shift();
|
|
64
|
+
this.offset = 0;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
this.length -= n;
|
|
68
|
+
return out;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
private flush(): void {
|
|
72
|
+
const w = this.waiting;
|
|
73
|
+
if (!w || this.length < w.n) return;
|
|
74
|
+
this.waiting = null;
|
|
75
|
+
w.resolve(this.take(w.n));
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** 认证没过(用户名密码不对、服务不支持):message 原样给页面看 */
|
|
80
|
+
export class VncAuthError extends Error {}
|
|
81
|
+
|
|
82
|
+
const TYPE_NONE = 1;
|
|
83
|
+
const TYPE_VNC = 2;
|
|
84
|
+
const TYPE_ARD = 30;
|
|
85
|
+
|
|
86
|
+
/** 左边补 0 到 len 字节(DH 的数可能比素数短) */
|
|
87
|
+
function pad(b: Buffer, len: number): Buffer {
|
|
88
|
+
return b.length >= len ? b.subarray(b.length - len) : Buffer.concat([Buffer.alloc(len - b.length), b]);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** 按苹果认证算出要发回去的:加密后的 128 字节凭据 + 自己的公钥 */
|
|
92
|
+
export function ardResponse(generator: Buffer, prime: Buffer, serverKey: Buffer, username: string, password: string): Buffer {
|
|
93
|
+
const dh = crypto.createDiffieHellman(prime, generator);
|
|
94
|
+
const publicKey = pad(dh.generateKeys(), prime.length);
|
|
95
|
+
const shared = pad(dh.computeSecret(serverKey), prime.length);
|
|
96
|
+
const creds = crypto.randomBytes(128);
|
|
97
|
+
// 各最多 63 字节,后面跟一个 \0(和 noVNC、苹果自己的客户端一样)
|
|
98
|
+
const user = Buffer.from(username, "utf8").subarray(0, 63);
|
|
99
|
+
const pass = Buffer.from(password, "utf8").subarray(0, 63);
|
|
100
|
+
user.copy(creds, 0);
|
|
101
|
+
creds[user.length] = 0;
|
|
102
|
+
pass.copy(creds, 64);
|
|
103
|
+
creds[64 + pass.length] = 0;
|
|
104
|
+
const cipher = crypto.createCipheriv("aes-128-ecb", crypto.createHash("md5").update(shared).digest(), null);
|
|
105
|
+
cipher.setAutoPadding(false);
|
|
106
|
+
return Buffer.concat([cipher.update(creds), cipher.final(), publicKey]);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** 普通 VNC 密码认证:用密码(最多 8 位)当 DES 钥匙加密服务端给的 16 字节。Node 的 OpenSSL 3 默认没有 DES,借 noVNC 的 */
|
|
110
|
+
async function vncAuthResponse(password: string, challenge: Buffer): Promise<Buffer> {
|
|
111
|
+
const require = createRequire(import.meta.url);
|
|
112
|
+
const file = path.join(path.dirname(require.resolve("@novnc/novnc")), "crypto", "des.js");
|
|
113
|
+
const { DESECBCipher } = (await import(pathToFileURL(file).href)) as {
|
|
114
|
+
DESECBCipher: { importKey(key: number[]): { encrypt(alg: unknown, data: Uint8Array): Uint8Array } };
|
|
115
|
+
};
|
|
116
|
+
// 和 noVNC 的 RFB.genDES 一样:密码按字符码当钥匙
|
|
117
|
+
const key = password.split("").map((c) => c.charCodeAt(0) & 0xff);
|
|
118
|
+
return Buffer.from(DESECBCipher.importKey(key).encrypt(null, new Uint8Array(challenge)));
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export type VncCreds = { username?: string; password: string };
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* 作为客户端和 VNC 服务走完版本协商和认证,停在 ClientInit 之前。
|
|
125
|
+
* 填了用户名就用苹果认证;否则有 VNC 密码认证就用它,再不然不用认证的。
|
|
126
|
+
* 失败抛 VncAuthError(认证的问题)或普通 Error(连接断了)
|
|
127
|
+
*/
|
|
128
|
+
export async function vncLogin(r: ByteReader, write: (b: Buffer) => void, creds: VncCreds): Promise<void> {
|
|
129
|
+
const version = (await r.read(12)).toString("latin1");
|
|
130
|
+
const m = /^RFB (\d{3})\.(\d{3})\n$/.exec(version);
|
|
131
|
+
if (!m) throw new VncAuthError("对方不是 VNC 服务");
|
|
132
|
+
// 3.3 的服务端不给安全类型列表,现在基本没有了
|
|
133
|
+
if (Number(m[1]) < 3 || (Number(m[1]) === 3 && Number(m[2]) < 7)) throw new VncAuthError(`VNC 服务的协议太老(${version.trim()})`);
|
|
134
|
+
// 苹果的是 003.889,按 3.8 谈就行
|
|
135
|
+
write(Buffer.from("RFB 003.008\n", "latin1"));
|
|
136
|
+
const count = (await r.read(1))[0]!;
|
|
137
|
+
if (count === 0) {
|
|
138
|
+
const len = (await r.read(4)).readUInt32BE(0);
|
|
139
|
+
throw new VncAuthError(`VNC 服务拒绝连接:${(await r.read(len)).toString("utf8")}`);
|
|
140
|
+
}
|
|
141
|
+
const types = [...(await r.read(count))];
|
|
142
|
+
if (creds.username) {
|
|
143
|
+
if (!types.includes(TYPE_ARD)) throw new VncAuthError("这个 VNC 服务不支持用 Mac 用户名登录:把设置里的用户名留空,只填 VNC 密码");
|
|
144
|
+
write(Buffer.from([TYPE_ARD]));
|
|
145
|
+
const head = await r.read(4);
|
|
146
|
+
const generator = head.subarray(0, 2);
|
|
147
|
+
const keyLength = head.readUInt16BE(2);
|
|
148
|
+
const prime = await r.read(keyLength);
|
|
149
|
+
const serverKey = await r.read(keyLength);
|
|
150
|
+
write(ardResponse(generator, prime, serverKey, creds.username, creds.password));
|
|
151
|
+
if ((await r.read(4)).readUInt32BE(0) !== 0) throw new VncAuthError("Mac 的用户名或密码不对");
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
if (types.includes(TYPE_VNC)) {
|
|
155
|
+
write(Buffer.from([TYPE_VNC]));
|
|
156
|
+
const challenge = await r.read(16);
|
|
157
|
+
write(await vncAuthResponse(creds.password, challenge));
|
|
158
|
+
if ((await r.read(4)).readUInt32BE(0) !== 0) throw new VncAuthError("VNC 密码不对");
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
if (types.includes(TYPE_NONE)) {
|
|
162
|
+
write(Buffer.from([TYPE_NONE]));
|
|
163
|
+
if ((await r.read(4)).readUInt32BE(0) !== 0) throw new VncAuthError("VNC 服务拒绝连接");
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
if (types.includes(TYPE_ARD)) {
|
|
167
|
+
throw new VncAuthError("这个 VNC 服务要用户名登录(macOS 屏幕共享没开 VNC 密码):在 设置 → 远程控制 里填上 Mac 的用户名和登录密码");
|
|
168
|
+
}
|
|
169
|
+
throw new VncAuthError(`VNC 服务要的认证方式都不支持(${types.join("、")})`);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* 对页面那头装成不用认证的 VNC 服务(3.8,只给安全类型 None)。
|
|
174
|
+
* login 是和真正的 VNC 服务认证的过程:它失败时把原因当作认证失败告诉页面
|
|
175
|
+
*/
|
|
176
|
+
export async function serveNoAuth(r: ByteReader, write: (b: Buffer) => void, login: Promise<void>): Promise<void> {
|
|
177
|
+
// 页面那头还没谈完 login 就失败了:先接住,下面 await 时再处理(不然算没人接的 rejection,进程会退出)
|
|
178
|
+
login.catch(() => {});
|
|
179
|
+
write(Buffer.from("RFB 003.008\n", "latin1"));
|
|
180
|
+
await r.read(12);
|
|
181
|
+
write(Buffer.from([1, 1]));
|
|
182
|
+
await r.read(1);
|
|
183
|
+
try {
|
|
184
|
+
await login;
|
|
185
|
+
} catch (err) {
|
|
186
|
+
const reason = Buffer.from(err instanceof VncAuthError ? err.message : `连不上 VNC 服务:${err instanceof Error ? err.message : err}`, "utf8");
|
|
187
|
+
const len = Buffer.alloc(4);
|
|
188
|
+
len.writeUInt32BE(reason.length);
|
|
189
|
+
write(Buffer.concat([Buffer.from([0, 0, 0, 1]), len, reason]));
|
|
190
|
+
throw err;
|
|
191
|
+
}
|
|
192
|
+
write(Buffer.from([0, 0, 0, 0]));
|
|
193
|
+
}
|