|
|
@@ -0,0 +1,774 @@
|
|
|
1
|
+import Websock from "./websock";
|
|
|
2
|
+import * as message from "./message.js";
|
|
|
3
|
+import * as rendezvous from "./rendezvous.js";
|
|
|
4
|
+import { loadVp9 } from "./codec";
|
|
|
5
|
+import * as sha256 from "fast-sha256";
|
|
|
6
|
+import * as globals from "./globals";
|
|
|
7
|
+import { decompress, mapKey, sleep } from "./common";
|
|
|
8
|
+
|
|
|
9
|
+const PORT = 21116;
|
|
|
10
|
+// only the first is used to init `HOST`
|
|
|
11
|
+const HOSTS = [
|
|
|
12
|
+ "rs-sg.rustdesk.com",
|
|
|
13
|
+ "rs-cn.rustdesk.com",
|
|
|
14
|
+ "rs-us.rustdesk.com",
|
|
|
15
|
+];
|
|
|
16
|
+let HOST = localStorage.getItem("rendezvous-server") || HOSTS[0];
|
|
|
17
|
+const SCHEMA = "ws://";
|
|
|
18
|
+
|
|
|
19
|
+type MsgboxCallback = (type: string, title: string, text: string) => void;
|
|
|
20
|
+type DrawCallback = (data: Uint8Array) => void;
|
|
|
21
|
+//const cursorCanvas = document.createElement("canvas");
|
|
|
22
|
+
|
|
|
23
|
+export default class Connection {
|
|
|
24
|
+ _msgs: any[];
|
|
|
25
|
+ _ws: Websock | undefined;
|
|
|
26
|
+ _interval: any;
|
|
|
27
|
+ _id: string;
|
|
|
28
|
+ _hash: message.Hash | undefined;
|
|
|
29
|
+ _msgbox: MsgboxCallback;
|
|
|
30
|
+ _draw: DrawCallback;
|
|
|
31
|
+ _peerInfo: message.PeerInfo | undefined;
|
|
|
32
|
+ _firstFrame: Boolean | undefined;
|
|
|
33
|
+ _videoDecoder: any;
|
|
|
34
|
+ _password: Uint8Array | undefined;
|
|
|
35
|
+ _options: any;
|
|
|
36
|
+ _videoTestSpeed: number[];
|
|
|
37
|
+ //_cursors: { [name: number]: any };
|
|
|
38
|
+
|
|
|
39
|
+ constructor() {
|
|
|
40
|
+ this._msgbox = globals.msgbox;
|
|
|
41
|
+ this._draw = globals.draw;
|
|
|
42
|
+ this._msgs = [];
|
|
|
43
|
+ this._id = "";
|
|
|
44
|
+ this._videoTestSpeed = [0, 0];
|
|
|
45
|
+ //this._cursors = {};
|
|
|
46
|
+ }
|
|
|
47
|
+
|
|
|
48
|
+ async start(id: string) {
|
|
|
49
|
+ try {
|
|
|
50
|
+ await this._start(id);
|
|
|
51
|
+ } catch (e: any) {
|
|
|
52
|
+ this.msgbox(
|
|
|
53
|
+ "error",
|
|
|
54
|
+ "Connection Error",
|
|
|
55
|
+ e.type == "close" ? "Reset by the peer" : String(e)
|
|
|
56
|
+ );
|
|
|
57
|
+ }
|
|
|
58
|
+ }
|
|
|
59
|
+
|
|
|
60
|
+ async _start(id: string) {
|
|
|
61
|
+ if (!this._options) {
|
|
|
62
|
+ this._options = globals.getPeers()[id] || {};
|
|
|
63
|
+ }
|
|
|
64
|
+ if (!this._password) {
|
|
|
65
|
+ const p = this.getOption("password");
|
|
|
66
|
+ if (p) {
|
|
|
67
|
+ try {
|
|
|
68
|
+ this._password = Uint8Array.from(JSON.parse("[" + p + "]"));
|
|
|
69
|
+ } catch (e) {
|
|
|
70
|
+ console.error(e);
|
|
|
71
|
+ }
|
|
|
72
|
+ }
|
|
|
73
|
+ }
|
|
|
74
|
+ this._interval = setInterval(() => {
|
|
|
75
|
+ while (this._msgs.length) {
|
|
|
76
|
+ this._ws?.sendMessage(this._msgs[0]);
|
|
|
77
|
+ this._msgs.splice(0, 1);
|
|
|
78
|
+ }
|
|
|
79
|
+ }, 1);
|
|
|
80
|
+ this.loadVideoDecoder();
|
|
|
81
|
+ const uri = getDefaultUri();
|
|
|
82
|
+ const ws = new Websock(uri, true);
|
|
|
83
|
+ this._ws = ws;
|
|
|
84
|
+ this._id = id;
|
|
|
85
|
+ console.log(
|
|
|
86
|
+ new Date() + ": Conntecting to rendezvoous server: " + uri + ", for " + id
|
|
|
87
|
+ );
|
|
|
88
|
+ await ws.open();
|
|
|
89
|
+ console.log(new Date() + ": Connected to rendezvoous server");
|
|
|
90
|
+ const conn_type = rendezvous.ConnType.DEFAULT_CONN;
|
|
|
91
|
+ const nat_type = rendezvous.NatType.SYMMETRIC;
|
|
|
92
|
+ const punch_hole_request = rendezvous.PunchHoleRequest.fromPartial({
|
|
|
93
|
+ id,
|
|
|
94
|
+ licence_key: localStorage.getItem("key") || undefined,
|
|
|
95
|
+ conn_type,
|
|
|
96
|
+ nat_type,
|
|
|
97
|
+ token: localStorage.getItem("access_token") || undefined,
|
|
|
98
|
+ });
|
|
|
99
|
+ ws.sendRendezvous({ punch_hole_request });
|
|
|
100
|
+ const msg = (await ws.next()) as rendezvous.RendezvousMessage;
|
|
|
101
|
+ ws.close();
|
|
|
102
|
+ console.log(new Date() + ": Got relay response");
|
|
|
103
|
+ const phr = msg.punch_hole_response;
|
|
|
104
|
+ const rr = msg.relay_response;
|
|
|
105
|
+ if (phr) {
|
|
|
106
|
+ if (phr?.other_failure) {
|
|
|
107
|
+ this.msgbox("error", "Error", phr?.other_failure);
|
|
|
108
|
+ return;
|
|
|
109
|
+ }
|
|
|
110
|
+ if (phr.failure != rendezvous.PunchHoleResponse_Failure.UNRECOGNIZED) {
|
|
|
111
|
+ switch (phr?.failure) {
|
|
|
112
|
+ case rendezvous.PunchHoleResponse_Failure.ID_NOT_EXIST:
|
|
|
113
|
+ this.msgbox("error", "Error", "ID does not exist");
|
|
|
114
|
+ break;
|
|
|
115
|
+ case rendezvous.PunchHoleResponse_Failure.OFFLINE:
|
|
|
116
|
+ this.msgbox("error", "Error", "Remote desktop is offline");
|
|
|
117
|
+ break;
|
|
|
118
|
+ case rendezvous.PunchHoleResponse_Failure.LICENSE_MISMATCH:
|
|
|
119
|
+ this.msgbox("error", "Error", "Key mismatch");
|
|
|
120
|
+ break;
|
|
|
121
|
+ case rendezvous.PunchHoleResponse_Failure.LICENSE_OVERUSE:
|
|
|
122
|
+ this.msgbox("error", "Error", "Key overuse");
|
|
|
123
|
+ break;
|
|
|
124
|
+ }
|
|
|
125
|
+ }
|
|
|
126
|
+ } else if (rr) {
|
|
|
127
|
+ if (!rr.version) {
|
|
|
128
|
+ this.msgbox("error", "Error", "Remote version is low, not support web");
|
|
|
129
|
+ return;
|
|
|
130
|
+ }
|
|
|
131
|
+ await this.connectRelay(rr);
|
|
|
132
|
+ }
|
|
|
133
|
+ }
|
|
|
134
|
+
|
|
|
135
|
+ async connectRelay(rr: rendezvous.RelayResponse) {
|
|
|
136
|
+ const pk = rr.pk;
|
|
|
137
|
+ let uri = rr.relay_server;
|
|
|
138
|
+ if (uri) {
|
|
|
139
|
+ uri = getrUriFromRs(uri, true, 2);
|
|
|
140
|
+ } else {
|
|
|
141
|
+ uri = getDefaultUri(true);
|
|
|
142
|
+ }
|
|
|
143
|
+ const uuid = rr.uuid;
|
|
|
144
|
+ console.log(new Date() + ": Connecting to relay server: " + uri);
|
|
|
145
|
+ const ws = new Websock(uri, false);
|
|
|
146
|
+ await ws.open();
|
|
|
147
|
+ console.log(new Date() + ": Connected to relay server");
|
|
|
148
|
+ this._ws = ws;
|
|
|
149
|
+ const request_relay = rendezvous.RequestRelay.fromPartial({
|
|
|
150
|
+ licence_key: localStorage.getItem("key") || undefined,
|
|
|
151
|
+ uuid,
|
|
|
152
|
+ });
|
|
|
153
|
+ ws.sendRendezvous({ request_relay });
|
|
|
154
|
+ const secure = (await this.secure(pk)) || false;
|
|
|
155
|
+ globals.pushEvent("connection_ready", { secure, direct: false });
|
|
|
156
|
+ await this.msgLoop();
|
|
|
157
|
+ }
|
|
|
158
|
+
|
|
|
159
|
+ async secure(pk: Uint8Array | undefined) {
|
|
|
160
|
+ if (pk) {
|
|
|
161
|
+ const RS_PK = "OeVuKk5nlHiXp+APNn0Y3pC1Iwpwn44JGqrQCsWqmBw=";
|
|
|
162
|
+ try {
|
|
|
163
|
+ pk = await globals.verify(pk, localStorage.getItem("key") || RS_PK);
|
|
|
164
|
+ if (pk) {
|
|
|
165
|
+ const idpk = message.IdPk.decode(pk);
|
|
|
166
|
+ if (idpk.id == this._id) {
|
|
|
167
|
+ pk = idpk.pk;
|
|
|
168
|
+ }
|
|
|
169
|
+ }
|
|
|
170
|
+ if (pk?.length != 32) {
|
|
|
171
|
+ pk = undefined;
|
|
|
172
|
+ }
|
|
|
173
|
+ } catch (e) {
|
|
|
174
|
+ console.error(e);
|
|
|
175
|
+ pk = undefined;
|
|
|
176
|
+ }
|
|
|
177
|
+ if (!pk)
|
|
|
178
|
+ console.error(
|
|
|
179
|
+ "Handshake failed: invalid public key from rendezvous server"
|
|
|
180
|
+ );
|
|
|
181
|
+ }
|
|
|
182
|
+ if (!pk) {
|
|
|
183
|
+ // send an empty message out in case server is setting up secure and waiting for first message
|
|
|
184
|
+ const public_key = message.PublicKey.fromPartial({});
|
|
|
185
|
+ this._ws?.sendMessage({ public_key });
|
|
|
186
|
+ return;
|
|
|
187
|
+ }
|
|
|
188
|
+ const msg = (await this._ws?.next()) as message.Message;
|
|
|
189
|
+ let signedId: any = msg?.signed_id;
|
|
|
190
|
+ if (!signedId) {
|
|
|
191
|
+ console.error("Handshake failed: invalid message type");
|
|
|
192
|
+ const public_key = message.PublicKey.fromPartial({});
|
|
|
193
|
+ this._ws?.sendMessage({ public_key });
|
|
|
194
|
+ return;
|
|
|
195
|
+ }
|
|
|
196
|
+ try {
|
|
|
197
|
+ signedId = await globals.verify(signedId.id, Uint8Array.from(pk!));
|
|
|
198
|
+ } catch (e) {
|
|
|
199
|
+ console.error(e);
|
|
|
200
|
+ // fall back to non-secure connection in case pk mismatch
|
|
|
201
|
+ console.error("pk mismatch, fall back to non-secure");
|
|
|
202
|
+ const public_key = message.PublicKey.fromPartial({});
|
|
|
203
|
+ this._ws?.sendMessage({ public_key });
|
|
|
204
|
+ return;
|
|
|
205
|
+ }
|
|
|
206
|
+ const idpk = message.IdPk.decode(signedId);
|
|
|
207
|
+ const id = idpk.id;
|
|
|
208
|
+ const theirPk = idpk.pk;
|
|
|
209
|
+ if (id != this._id!) {
|
|
|
210
|
+ console.error("Handshake failed: sign failure");
|
|
|
211
|
+ const public_key = message.PublicKey.fromPartial({});
|
|
|
212
|
+ this._ws?.sendMessage({ public_key });
|
|
|
213
|
+ return;
|
|
|
214
|
+ }
|
|
|
215
|
+ if (theirPk.length != 32) {
|
|
|
216
|
+ console.error(
|
|
|
217
|
+ "Handshake failed: invalid public box key length from peer"
|
|
|
218
|
+ );
|
|
|
219
|
+ const public_key = message.PublicKey.fromPartial({});
|
|
|
220
|
+ this._ws?.sendMessage({ public_key });
|
|
|
221
|
+ return;
|
|
|
222
|
+ }
|
|
|
223
|
+ const [mySk, asymmetric_value] = globals.genBoxKeyPair();
|
|
|
224
|
+ const secret_key = globals.genSecretKey();
|
|
|
225
|
+ const symmetric_value = globals.seal(secret_key, theirPk, mySk);
|
|
|
226
|
+ const public_key = message.PublicKey.fromPartial({
|
|
|
227
|
+ asymmetric_value,
|
|
|
228
|
+ symmetric_value,
|
|
|
229
|
+ });
|
|
|
230
|
+ this._ws?.sendMessage({ public_key });
|
|
|
231
|
+ this._ws?.setSecretKey(secret_key);
|
|
|
232
|
+ console.log("secured");
|
|
|
233
|
+ return true;
|
|
|
234
|
+ }
|
|
|
235
|
+
|
|
|
236
|
+ async msgLoop() {
|
|
|
237
|
+ while (true) {
|
|
|
238
|
+ const msg = (await this._ws?.next()) as message.Message;
|
|
|
239
|
+ if (msg?.hash) {
|
|
|
240
|
+ this._hash = msg?.hash;
|
|
|
241
|
+ if (!this._password)
|
|
|
242
|
+ this.msgbox("input-password", "Password Required", "");
|
|
|
243
|
+ this.login();
|
|
|
244
|
+ } else if (msg?.test_delay) {
|
|
|
245
|
+ const test_delay = msg?.test_delay;
|
|
|
246
|
+ console.log(test_delay);
|
|
|
247
|
+ if (!test_delay.from_client) {
|
|
|
248
|
+ this._ws?.sendMessage({ test_delay });
|
|
|
249
|
+ }
|
|
|
250
|
+ } else if (msg?.login_response) {
|
|
|
251
|
+ const r = msg?.login_response;
|
|
|
252
|
+ if (r.error) {
|
|
|
253
|
+ if (r.error == "Wrong Password") {
|
|
|
254
|
+ this._password = undefined;
|
|
|
255
|
+ this.msgbox(
|
|
|
256
|
+ "re-input-password",
|
|
|
257
|
+ r.error,
|
|
|
258
|
+ "Do you want to enter again?"
|
|
|
259
|
+ );
|
|
|
260
|
+ } else {
|
|
|
261
|
+ this.msgbox("error", "Login Error", r.error);
|
|
|
262
|
+ }
|
|
|
263
|
+ } else if (r.peer_info) {
|
|
|
264
|
+ this.handlePeerInfo(r.peer_info);
|
|
|
265
|
+ }
|
|
|
266
|
+ } else if (msg?.video_frame) {
|
|
|
267
|
+ this.handleVideoFrame(msg?.video_frame!);
|
|
|
268
|
+ } else if (msg?.clipboard) {
|
|
|
269
|
+ const cb = msg?.clipboard;
|
|
|
270
|
+ if (cb.compress) {
|
|
|
271
|
+ const c = await decompress(cb.content);
|
|
|
272
|
+ if (!c) continue;
|
|
|
273
|
+ cb.content = c;
|
|
|
274
|
+ }
|
|
|
275
|
+ try {
|
|
|
276
|
+ globals.copyToClipboard(new TextDecoder().decode(cb.content));
|
|
|
277
|
+ } catch (e) {
|
|
|
278
|
+ console.error(e);
|
|
|
279
|
+ }
|
|
|
280
|
+ // globals.pushEvent("clipboard", cb);
|
|
|
281
|
+ } else if (msg?.cursor_data) {
|
|
|
282
|
+ const cd = msg?.cursor_data;
|
|
|
283
|
+ const c = await decompress(cd.colors);
|
|
|
284
|
+ if (!c) continue;
|
|
|
285
|
+ cd.colors = c;
|
|
|
286
|
+ globals.pushEvent("cursor_data", cd);
|
|
|
287
|
+ /*
|
|
|
288
|
+ let ctx = cursorCanvas.getContext("2d");
|
|
|
289
|
+ cursorCanvas.width = cd.width;
|
|
|
290
|
+ cursorCanvas.height = cd.height;
|
|
|
291
|
+ let imgData = new ImageData(
|
|
|
292
|
+ new Uint8ClampedArray(c),
|
|
|
293
|
+ cd.width,
|
|
|
294
|
+ cd.height
|
|
|
295
|
+ );
|
|
|
296
|
+ ctx?.clearRect(0, 0, cd.width, cd.height);
|
|
|
297
|
+ ctx?.putImageData(imgData, 0, 0);
|
|
|
298
|
+ let url = cursorCanvas.toDataURL();
|
|
|
299
|
+ const img = document.createElement("img");
|
|
|
300
|
+ img.src = url;
|
|
|
301
|
+ this._cursors[cd.id] = img;
|
|
|
302
|
+ //cursorCanvas.width /= 2.;
|
|
|
303
|
+ //cursorCanvas.height /= 2.;
|
|
|
304
|
+ //ctx?.drawImage(img, cursorCanvas.width, cursorCanvas.height);
|
|
|
305
|
+ url = cursorCanvas.toDataURL();
|
|
|
306
|
+ document.body.style.cursor =
|
|
|
307
|
+ "url(" + url + ")" + cd.hotx + " " + cd.hoty + ", default";
|
|
|
308
|
+ console.log(document.body.style.cursor);
|
|
|
309
|
+ */
|
|
|
310
|
+ } else if (msg?.cursor_id) {
|
|
|
311
|
+ globals.pushEvent("cursor_id", { id: msg?.cursor_id });
|
|
|
312
|
+ } else if (msg?.cursor_position) {
|
|
|
313
|
+ globals.pushEvent("cursor_position", msg?.cursor_position);
|
|
|
314
|
+ } else if (msg?.misc) {
|
|
|
315
|
+ if (!this.handleMisc(msg?.misc)) break;
|
|
|
316
|
+ } else if (msg?.audio_frame) {
|
|
|
317
|
+ globals.playAudio(msg?.audio_frame.data);
|
|
|
318
|
+ }
|
|
|
319
|
+ }
|
|
|
320
|
+ }
|
|
|
321
|
+
|
|
|
322
|
+ msgbox(type_: string, title: string, text: string) {
|
|
|
323
|
+ this._msgbox?.(type_, title, text);
|
|
|
324
|
+ }
|
|
|
325
|
+
|
|
|
326
|
+ draw(frame: any) {
|
|
|
327
|
+ this._draw?.(frame);
|
|
|
328
|
+ globals.draw(frame);
|
|
|
329
|
+ }
|
|
|
330
|
+
|
|
|
331
|
+ close() {
|
|
|
332
|
+ this._msgs = [];
|
|
|
333
|
+ clearInterval(this._interval);
|
|
|
334
|
+ this._ws?.close();
|
|
|
335
|
+ this._videoDecoder?.close();
|
|
|
336
|
+ }
|
|
|
337
|
+
|
|
|
338
|
+ refresh() {
|
|
|
339
|
+ const misc = message.Misc.fromPartial({ refresh_video: true });
|
|
|
340
|
+ this._ws?.sendMessage({ misc });
|
|
|
341
|
+ }
|
|
|
342
|
+
|
|
|
343
|
+ setMsgbox(callback: MsgboxCallback) {
|
|
|
344
|
+ this._msgbox = callback;
|
|
|
345
|
+ }
|
|
|
346
|
+
|
|
|
347
|
+ setDraw(callback: DrawCallback) {
|
|
|
348
|
+ this._draw = callback;
|
|
|
349
|
+ }
|
|
|
350
|
+
|
|
|
351
|
+ login(password: string | undefined = undefined) {
|
|
|
352
|
+ if (password) {
|
|
|
353
|
+ const salt = this._hash?.salt;
|
|
|
354
|
+ let p = hash([password, salt!]);
|
|
|
355
|
+ this._password = p;
|
|
|
356
|
+ const challenge = this._hash?.challenge;
|
|
|
357
|
+ p = hash([p, challenge!]);
|
|
|
358
|
+ this.msgbox("connecting", "Connecting...", "Logging in...");
|
|
|
359
|
+ this._sendLoginMessage(p);
|
|
|
360
|
+ } else {
|
|
|
361
|
+ let p = this._password;
|
|
|
362
|
+ if (p) {
|
|
|
363
|
+ const challenge = this._hash?.challenge;
|
|
|
364
|
+ p = hash([p, challenge!]);
|
|
|
365
|
+ }
|
|
|
366
|
+ this._sendLoginMessage(p);
|
|
|
367
|
+ }
|
|
|
368
|
+ }
|
|
|
369
|
+
|
|
|
370
|
+ async reconnect() {
|
|
|
371
|
+ this.close();
|
|
|
372
|
+ await this.start(this._id);
|
|
|
373
|
+ }
|
|
|
374
|
+
|
|
|
375
|
+ _sendLoginMessage(password: Uint8Array | undefined = undefined) {
|
|
|
376
|
+ const login_request = message.LoginRequest.fromPartial({
|
|
|
377
|
+ username: this._id!,
|
|
|
378
|
+ my_id: "web", // to-do
|
|
|
379
|
+ my_name: "web", // to-do
|
|
|
380
|
+ password,
|
|
|
381
|
+ option: this.getOptionMessage(),
|
|
|
382
|
+ video_ack_required: true,
|
|
|
383
|
+ });
|
|
|
384
|
+ this._ws?.sendMessage({ login_request });
|
|
|
385
|
+ }
|
|
|
386
|
+
|
|
|
387
|
+ getOptionMessage(): message.OptionMessage | undefined {
|
|
|
388
|
+ let n = 0;
|
|
|
389
|
+ const msg = message.OptionMessage.fromPartial({});
|
|
|
390
|
+ const q = this.getImageQualityEnum(this.getImageQuality(), true);
|
|
|
391
|
+ const yes = message.OptionMessage_BoolOption.Yes;
|
|
|
392
|
+ if (q != undefined) {
|
|
|
393
|
+ msg.image_quality = q;
|
|
|
394
|
+ n += 1;
|
|
|
395
|
+ }
|
|
|
396
|
+ if (this._options["show-remote-cursor"]) {
|
|
|
397
|
+ msg.show_remote_cursor = yes;
|
|
|
398
|
+ n += 1;
|
|
|
399
|
+ }
|
|
|
400
|
+ if (this._options["lock-after-session-end"]) {
|
|
|
401
|
+ msg.lock_after_session_end = yes;
|
|
|
402
|
+ n += 1;
|
|
|
403
|
+ }
|
|
|
404
|
+ if (this._options["privacy-mode"]) {
|
|
|
405
|
+ msg.privacy_mode = yes;
|
|
|
406
|
+ n += 1;
|
|
|
407
|
+ }
|
|
|
408
|
+ if (this._options["disable-audio"]) {
|
|
|
409
|
+ msg.disable_audio = yes;
|
|
|
410
|
+ n += 1;
|
|
|
411
|
+ }
|
|
|
412
|
+ if (this._options["disable-clipboard"]) {
|
|
|
413
|
+ msg.disable_clipboard = yes;
|
|
|
414
|
+ n += 1;
|
|
|
415
|
+ }
|
|
|
416
|
+ return n > 0 ? msg : undefined;
|
|
|
417
|
+ }
|
|
|
418
|
+
|
|
|
419
|
+ sendVideoReceived() {
|
|
|
420
|
+ const misc = message.Misc.fromPartial({ video_received: true });
|
|
|
421
|
+ this._ws?.sendMessage({ misc });
|
|
|
422
|
+ }
|
|
|
423
|
+
|
|
|
424
|
+ handleVideoFrame(vf: message.VideoFrame) {
|
|
|
425
|
+ if (!this._firstFrame) {
|
|
|
426
|
+ this.msgbox("", "", "");
|
|
|
427
|
+ this._firstFrame = true;
|
|
|
428
|
+ }
|
|
|
429
|
+ if (vf.vp9s) {
|
|
|
430
|
+ const dec = this._videoDecoder;
|
|
|
431
|
+ var tm = new Date().getTime();
|
|
|
432
|
+ var i = 0;
|
|
|
433
|
+ const n = vf.vp9s?.frames.length;
|
|
|
434
|
+ vf.vp9s.frames.forEach((f) => {
|
|
|
435
|
+ dec.processFrame(f.data.slice(0).buffer, (ok: any) => {
|
|
|
436
|
+ i++;
|
|
|
437
|
+ if (i == n) this.sendVideoReceived();
|
|
|
438
|
+ if (ok && dec.frameBuffer && n == i) {
|
|
|
439
|
+ this.draw(dec.frameBuffer);
|
|
|
440
|
+ const now = new Date().getTime();
|
|
|
441
|
+ var elapsed = now - tm;
|
|
|
442
|
+ this._videoTestSpeed[1] += elapsed;
|
|
|
443
|
+ this._videoTestSpeed[0] += 1;
|
|
|
444
|
+ if (this._videoTestSpeed[0] >= 30) {
|
|
|
445
|
+ console.log(
|
|
|
446
|
+ "video decoder: " +
|
|
|
447
|
+ parseInt(
|
|
|
448
|
+ "" + this._videoTestSpeed[1] / this._videoTestSpeed[0]
|
|
|
449
|
+ )
|
|
|
450
|
+ );
|
|
|
451
|
+ this._videoTestSpeed = [0, 0];
|
|
|
452
|
+ }
|
|
|
453
|
+ }
|
|
|
454
|
+ });
|
|
|
455
|
+ });
|
|
|
456
|
+ }
|
|
|
457
|
+ }
|
|
|
458
|
+
|
|
|
459
|
+ handlePeerInfo(pi: message.PeerInfo) {
|
|
|
460
|
+ this._peerInfo = pi;
|
|
|
461
|
+ if (pi.displays.length == 0) {
|
|
|
462
|
+ this.msgbox("error", "Remote Error", "No Display");
|
|
|
463
|
+ return;
|
|
|
464
|
+ }
|
|
|
465
|
+ this.msgbox("success", "Successful", "Connected, waiting for image...");
|
|
|
466
|
+ globals.pushEvent("peer_info", pi);
|
|
|
467
|
+ const p = this.shouldAutoLogin();
|
|
|
468
|
+ if (p) this.inputOsPassword(p);
|
|
|
469
|
+ const username = this.getOption("info")?.username;
|
|
|
470
|
+ if (username && !pi.username) pi.username = username;
|
|
|
471
|
+ this.setOption("info", pi);
|
|
|
472
|
+ if (this.getRemember()) {
|
|
|
473
|
+ if (this._password?.length) {
|
|
|
474
|
+ const p = this._password.toString();
|
|
|
475
|
+ if (p != this.getOption("password")) {
|
|
|
476
|
+ this.setOption("password", p);
|
|
|
477
|
+ console.log("remember password of " + this._id);
|
|
|
478
|
+ }
|
|
|
479
|
+ }
|
|
|
480
|
+ } else {
|
|
|
481
|
+ this.setOption("password", undefined);
|
|
|
482
|
+ }
|
|
|
483
|
+ }
|
|
|
484
|
+
|
|
|
485
|
+ shouldAutoLogin(): string {
|
|
|
486
|
+ const l = this.getOption("lock-after-session-end");
|
|
|
487
|
+ const a = !!this.getOption("auto-login");
|
|
|
488
|
+ const p = this.getOption("os-password");
|
|
|
489
|
+ if (p && l && a) {
|
|
|
490
|
+ return p;
|
|
|
491
|
+ }
|
|
|
492
|
+ return "";
|
|
|
493
|
+ }
|
|
|
494
|
+
|
|
|
495
|
+ handleMisc(misc: message.Misc) {
|
|
|
496
|
+ if (misc.audio_format) {
|
|
|
497
|
+ globals.initAudio(
|
|
|
498
|
+ misc.audio_format.channels,
|
|
|
499
|
+ misc.audio_format.sample_rate
|
|
|
500
|
+ );
|
|
|
501
|
+ } else if (misc.chat_message) {
|
|
|
502
|
+ globals.pushEvent("chat", { text: misc.chat_message.text });
|
|
|
503
|
+ } else if (misc.permission_info) {
|
|
|
504
|
+ const p = misc.permission_info;
|
|
|
505
|
+ console.info("Change permission " + p.permission + " -> " + p.enabled);
|
|
|
506
|
+ let name;
|
|
|
507
|
+ switch (p.permission) {
|
|
|
508
|
+ case message.PermissionInfo_Permission.Keyboard:
|
|
|
509
|
+ name = "keyboard";
|
|
|
510
|
+ break;
|
|
|
511
|
+ case message.PermissionInfo_Permission.Clipboard:
|
|
|
512
|
+ name = "clipboard";
|
|
|
513
|
+ break;
|
|
|
514
|
+ case message.PermissionInfo_Permission.Audio:
|
|
|
515
|
+ name = "audio";
|
|
|
516
|
+ break;
|
|
|
517
|
+ default:
|
|
|
518
|
+ return;
|
|
|
519
|
+ }
|
|
|
520
|
+ globals.pushEvent("permission", { [name]: p.enabled });
|
|
|
521
|
+ } else if (misc.switch_display) {
|
|
|
522
|
+ this.loadVideoDecoder();
|
|
|
523
|
+ globals.pushEvent("switch_display", misc.switch_display);
|
|
|
524
|
+ } else if (misc.close_reason) {
|
|
|
525
|
+ this.msgbox("error", "Connection Error", misc.close_reason);
|
|
|
526
|
+ this.close();
|
|
|
527
|
+ return false;
|
|
|
528
|
+ }
|
|
|
529
|
+ return true;
|
|
|
530
|
+ }
|
|
|
531
|
+
|
|
|
532
|
+ getRemember(): Boolean {
|
|
|
533
|
+ return this._options["remember"] || false;
|
|
|
534
|
+ }
|
|
|
535
|
+
|
|
|
536
|
+ setRemember(v: Boolean) {
|
|
|
537
|
+ this.setOption("remember", v);
|
|
|
538
|
+ }
|
|
|
539
|
+
|
|
|
540
|
+ getOption(name: string): any {
|
|
|
541
|
+ return this._options[name];
|
|
|
542
|
+ }
|
|
|
543
|
+
|
|
|
544
|
+ setOption(name: string, value: any) {
|
|
|
545
|
+ if (value == undefined) {
|
|
|
546
|
+ delete this._options[name];
|
|
|
547
|
+ } else {
|
|
|
548
|
+ this._options[name] = value;
|
|
|
549
|
+ }
|
|
|
550
|
+ this._options["tm"] = new Date().getTime();
|
|
|
551
|
+ const peers = globals.getPeers();
|
|
|
552
|
+ peers[this._id] = this._options;
|
|
|
553
|
+ localStorage.setItem("peers", JSON.stringify(peers));
|
|
|
554
|
+ }
|
|
|
555
|
+
|
|
|
556
|
+ inputKey(
|
|
|
557
|
+ name: string,
|
|
|
558
|
+ down: boolean,
|
|
|
559
|
+ press: boolean,
|
|
|
560
|
+ alt: Boolean,
|
|
|
561
|
+ ctrl: Boolean,
|
|
|
562
|
+ shift: Boolean,
|
|
|
563
|
+ command: Boolean
|
|
|
564
|
+ ) {
|
|
|
565
|
+ const key_event = mapKey(name, globals.isDesktop());
|
|
|
566
|
+ if (!key_event) return;
|
|
|
567
|
+ if (alt && (name == "VK_MENU" || name == "RAlt")) {
|
|
|
568
|
+ alt = false;
|
|
|
569
|
+ }
|
|
|
570
|
+ if (ctrl && (name == "VK_CONTROL" || name == "RControl")) {
|
|
|
571
|
+ ctrl = false;
|
|
|
572
|
+ }
|
|
|
573
|
+ if (shift && (name == "VK_SHIFT" || name == "RShift")) {
|
|
|
574
|
+ shift = false;
|
|
|
575
|
+ }
|
|
|
576
|
+ if (command && (name == "Meta" || name == "RWin")) {
|
|
|
577
|
+ command = false;
|
|
|
578
|
+ }
|
|
|
579
|
+ key_event.down = down;
|
|
|
580
|
+ key_event.press = press;
|
|
|
581
|
+ key_event.modifiers = this.getMod(alt, ctrl, shift, command);
|
|
|
582
|
+ this._ws?.sendMessage({ key_event });
|
|
|
583
|
+ }
|
|
|
584
|
+
|
|
|
585
|
+ ctrlAltDel() {
|
|
|
586
|
+ const key_event = message.KeyEvent.fromPartial({ down: true });
|
|
|
587
|
+ if (this._peerInfo?.platform == "Windows") {
|
|
|
588
|
+ key_event.control_key = message.ControlKey.CtrlAltDel;
|
|
|
589
|
+ } else {
|
|
|
590
|
+ key_event.control_key = message.ControlKey.Delete;
|
|
|
591
|
+ key_event.modifiers = this.getMod(true, true, false, false);
|
|
|
592
|
+ }
|
|
|
593
|
+ this._ws?.sendMessage({ key_event });
|
|
|
594
|
+ }
|
|
|
595
|
+
|
|
|
596
|
+ inputString(seq: string) {
|
|
|
597
|
+ const key_event = message.KeyEvent.fromPartial({ seq });
|
|
|
598
|
+ this._ws?.sendMessage({ key_event });
|
|
|
599
|
+ }
|
|
|
600
|
+
|
|
|
601
|
+ switchDisplay(display: number) {
|
|
|
602
|
+ const switch_display = message.SwitchDisplay.fromPartial({ display });
|
|
|
603
|
+ const misc = message.Misc.fromPartial({ switch_display });
|
|
|
604
|
+ this._ws?.sendMessage({ misc });
|
|
|
605
|
+ }
|
|
|
606
|
+
|
|
|
607
|
+ async inputOsPassword(seq: string) {
|
|
|
608
|
+ this.inputMouse();
|
|
|
609
|
+ await sleep(50);
|
|
|
610
|
+ this.inputMouse(0, 3, 3);
|
|
|
611
|
+ await sleep(50);
|
|
|
612
|
+ this.inputMouse(1 | (1 << 3));
|
|
|
613
|
+ this.inputMouse(2 | (1 << 3));
|
|
|
614
|
+ await sleep(1200);
|
|
|
615
|
+ const key_event = message.KeyEvent.fromPartial({ press: true, seq });
|
|
|
616
|
+ this._ws?.sendMessage({ key_event });
|
|
|
617
|
+ }
|
|
|
618
|
+
|
|
|
619
|
+ lockScreen() {
|
|
|
620
|
+ const key_event = message.KeyEvent.fromPartial({
|
|
|
621
|
+ down: true,
|
|
|
622
|
+ control_key: message.ControlKey.LockScreen,
|
|
|
623
|
+ });
|
|
|
624
|
+ this._ws?.sendMessage({ key_event });
|
|
|
625
|
+ }
|
|
|
626
|
+
|
|
|
627
|
+ getMod(alt: Boolean, ctrl: Boolean, shift: Boolean, command: Boolean) {
|
|
|
628
|
+ const mod: message.ControlKey[] = [];
|
|
|
629
|
+ if (alt) mod.push(message.ControlKey.Alt);
|
|
|
630
|
+ if (ctrl) mod.push(message.ControlKey.Control);
|
|
|
631
|
+ if (shift) mod.push(message.ControlKey.Shift);
|
|
|
632
|
+ if (command) mod.push(message.ControlKey.Meta);
|
|
|
633
|
+ return mod;
|
|
|
634
|
+ }
|
|
|
635
|
+
|
|
|
636
|
+ inputMouse(
|
|
|
637
|
+ mask: number = 0,
|
|
|
638
|
+ x: number = 0,
|
|
|
639
|
+ y: number = 0,
|
|
|
640
|
+ alt: Boolean = false,
|
|
|
641
|
+ ctrl: Boolean = false,
|
|
|
642
|
+ shift: Boolean = false,
|
|
|
643
|
+ command: Boolean = false
|
|
|
644
|
+ ) {
|
|
|
645
|
+ const mouse_event = message.MouseEvent.fromPartial({
|
|
|
646
|
+ mask,
|
|
|
647
|
+ x,
|
|
|
648
|
+ y,
|
|
|
649
|
+ modifiers: this.getMod(alt, ctrl, shift, command),
|
|
|
650
|
+ });
|
|
|
651
|
+ this._ws?.sendMessage({ mouse_event });
|
|
|
652
|
+ }
|
|
|
653
|
+
|
|
|
654
|
+ toggleOption(name: string) {
|
|
|
655
|
+ const v = !this._options[name];
|
|
|
656
|
+ const option = message.OptionMessage.fromPartial({});
|
|
|
657
|
+ const v2 = v
|
|
|
658
|
+ ? message.OptionMessage_BoolOption.Yes
|
|
|
659
|
+ : message.OptionMessage_BoolOption.No;
|
|
|
660
|
+ switch (name) {
|
|
|
661
|
+ case "show-remote-cursor":
|
|
|
662
|
+ option.show_remote_cursor = v2;
|
|
|
663
|
+ break;
|
|
|
664
|
+ case "disable-audio":
|
|
|
665
|
+ option.disable_audio = v2;
|
|
|
666
|
+ break;
|
|
|
667
|
+ case "disable-clipboard":
|
|
|
668
|
+ option.disable_clipboard = v2;
|
|
|
669
|
+ break;
|
|
|
670
|
+ case "lock-after-session-end":
|
|
|
671
|
+ option.lock_after_session_end = v2;
|
|
|
672
|
+ break;
|
|
|
673
|
+ case "privacy-mode":
|
|
|
674
|
+ option.privacy_mode = v2;
|
|
|
675
|
+ break;
|
|
|
676
|
+ case "block-input":
|
|
|
677
|
+ option.block_input = message.OptionMessage_BoolOption.Yes;
|
|
|
678
|
+ break;
|
|
|
679
|
+ case "unblock-input":
|
|
|
680
|
+ option.block_input = message.OptionMessage_BoolOption.No;
|
|
|
681
|
+ break;
|
|
|
682
|
+ default:
|
|
|
683
|
+ return;
|
|
|
684
|
+ }
|
|
|
685
|
+ if (name.indexOf("block-input") < 0) this.setOption(name, v);
|
|
|
686
|
+ const misc = message.Misc.fromPartial({ option });
|
|
|
687
|
+ this._ws?.sendMessage({ misc });
|
|
|
688
|
+ }
|
|
|
689
|
+
|
|
|
690
|
+ getImageQuality() {
|
|
|
691
|
+ return this.getOption("image-quality");
|
|
|
692
|
+ }
|
|
|
693
|
+
|
|
|
694
|
+ getImageQualityEnum(
|
|
|
695
|
+ value: string,
|
|
|
696
|
+ ignoreDefault: Boolean
|
|
|
697
|
+ ): message.ImageQuality | undefined {
|
|
|
698
|
+ switch (value) {
|
|
|
699
|
+ case "low":
|
|
|
700
|
+ return message.ImageQuality.Low;
|
|
|
701
|
+ case "best":
|
|
|
702
|
+ return message.ImageQuality.Best;
|
|
|
703
|
+ case "balanced":
|
|
|
704
|
+ return ignoreDefault ? undefined : message.ImageQuality.Balanced;
|
|
|
705
|
+ default:
|
|
|
706
|
+ return undefined;
|
|
|
707
|
+ }
|
|
|
708
|
+ }
|
|
|
709
|
+
|
|
|
710
|
+ setImageQuality(value: string) {
|
|
|
711
|
+ this.setOption("image-quality", value);
|
|
|
712
|
+ const image_quality = this.getImageQualityEnum(value, false);
|
|
|
713
|
+ if (image_quality == undefined) return;
|
|
|
714
|
+ const option = message.OptionMessage.fromPartial({ image_quality });
|
|
|
715
|
+ const misc = message.Misc.fromPartial({ option });
|
|
|
716
|
+ this._ws?.sendMessage({ misc });
|
|
|
717
|
+ }
|
|
|
718
|
+
|
|
|
719
|
+ loadVideoDecoder() {
|
|
|
720
|
+ this._videoDecoder?.close();
|
|
|
721
|
+ loadVp9((decoder: any) => {
|
|
|
722
|
+ this._videoDecoder = decoder;
|
|
|
723
|
+ console.log("vp9 loaded");
|
|
|
724
|
+ console.log(decoder);
|
|
|
725
|
+ });
|
|
|
726
|
+ }
|
|
|
727
|
+}
|
|
|
728
|
+
|
|
|
729
|
+function testDelay() {
|
|
|
730
|
+ var nearest = "";
|
|
|
731
|
+ HOSTS.forEach((host) => {
|
|
|
732
|
+ const now = new Date().getTime();
|
|
|
733
|
+ new Websock(getrUriFromRs(host), true).open().then(() => {
|
|
|
734
|
+ console.log("latency of " + host + ": " + (new Date().getTime() - now));
|
|
|
735
|
+ if (!nearest) {
|
|
|
736
|
+ HOST = host;
|
|
|
737
|
+ localStorage.setItem("rendezvous-server", host);
|
|
|
738
|
+ }
|
|
|
739
|
+ });
|
|
|
740
|
+ });
|
|
|
741
|
+}
|
|
|
742
|
+
|
|
|
743
|
+testDelay();
|
|
|
744
|
+
|
|
|
745
|
+function getDefaultUri(isRelay: Boolean = false): string {
|
|
|
746
|
+ const host = localStorage.getItem("custom-rendezvous-server");
|
|
|
747
|
+ return getrUriFromRs(host || HOST, isRelay);
|
|
|
748
|
+}
|
|
|
749
|
+
|
|
|
750
|
+function getrUriFromRs(
|
|
|
751
|
+ uri: string,
|
|
|
752
|
+ isRelay: Boolean = false,
|
|
|
753
|
+ roffset: number = 0
|
|
|
754
|
+): string {
|
|
|
755
|
+ if (uri.indexOf(":") > 0) {
|
|
|
756
|
+ const tmp = uri.split(":");
|
|
|
757
|
+ const port = parseInt(tmp[1]);
|
|
|
758
|
+ uri = tmp[0] + ":" + (port + (isRelay ? roffset || 3 : 2));
|
|
|
759
|
+ } else {
|
|
|
760
|
+ uri += ":" + (PORT + (isRelay ? 3 : 2));
|
|
|
761
|
+ }
|
|
|
762
|
+ return SCHEMA + uri;
|
|
|
763
|
+}
|
|
|
764
|
+
|
|
|
765
|
+function hash(datas: (string | Uint8Array)[]): Uint8Array {
|
|
|
766
|
+ const hasher = new sha256.Hash();
|
|
|
767
|
+ datas.forEach((data) => {
|
|
|
768
|
+ if (typeof data == "string") {
|
|
|
769
|
+ data = new TextEncoder().encode(data);
|
|
|
770
|
+ }
|
|
|
771
|
+ return hasher.update(data);
|
|
|
772
|
+ });
|
|
|
773
|
+ return hasher.digest();
|
|
|
774
|
+}
|