decode not work

This commit is contained in:
rustdesk
2022-01-20 21:58:28 +08:00
parent 3dcbbcc416
commit 50834353b2
8 changed files with 468 additions and 272 deletions

View File

@@ -1,16 +1,17 @@
import Websock from './websock';
import * as message from './message.js';
import * as rendezvous from './rendezvous.js';
import { loadVp9, loadOpus } from './codec';
import Websock from "./websock";
import * as message from "./message.js";
import * as rendezvous from "./rendezvous.js";
import { loadVp9, loadOpus } from "./codec";
import * as sha256 from "fast-sha256";
import * as globals from './globals';
import * as globals from "./globals";
const PORT = 21116;
const HOST = 'rs-sg.rustdesk.com';
const licenceKey = '';
const SCHEMA = 'ws://';
const HOST = "rs-sg.rustdesk.com";
const licenceKey = "";
const SCHEMA = "ws://";
type MsgboxCallback = (type: string, title: string, text: string) => void;
type DrawCallback = (Uint8Array) => void;
export default class Connection {
_msgs: any[];
@@ -19,18 +20,30 @@ export default class Connection {
_id: string;
_hash: message.Hash | undefined;
_msgbox: MsgboxCallback | undefined;
_draw: DrawCallback | undefined;
_peerInfo: message.PeerInfo | undefined;
_firstFrame: Boolean | undefined;
_firstFrame: Boolean | undefined;
_videoDecoder: any;
_audioDecoder: any;
constructor() {
this._msgs = [];
this._id = '';
this._id = "";
this._interval = setInterval(() => {
while (this._msgs.length) {
this._ws?.sendMessage(this._msgs[0]);
this._msgs.splice(0, 1);
}
}, 1);
loadVp9((decoder: any) => {
this._videoDecoder = decoder;
console.log("vp9 loaded");
console.log(decoder);
});
loadOpus((decoder: any) => {
this._audioDecoder = decoder;
console.log("opus loaded");
});
}
async start(id: string) {
@@ -38,9 +51,9 @@ export default class Connection {
const ws = new Websock(uri);
this._ws = ws;
this._id = id;
console.log(new Date() + ': Conntecting to rendezvoous server: ' + uri);
console.log(new Date() + ": Conntecting to rendezvoous server: " + uri);
await ws.open();
console.log(new Date() + ': Connected to rendezvoous server');
console.log(new Date() + ": Connected to rendezvoous server");
const connType = rendezvous.ConnType.DEFAULT_CONN;
const natType = rendezvous.NatType.SYMMETRIC;
const punchHoleRequest = rendezvous.PunchHoleRequest.fromPartial({
@@ -52,24 +65,24 @@ export default class Connection {
ws.sendRendezvous({ punchHoleRequest });
const msg = ws.parseRendezvous(await ws.next());
ws.close();
console.log(new Date() + ': Got relay response');
console.log(new Date() + ": Got relay response");
const phr = msg.punchHoleResponse;
const rr = msg.relayResponse;
if (phr) {
if (phr.failure != rendezvous.PunchHoleResponse_Failure.UNKNOWN) {
switch (phr?.failure) {
case rendezvous.PunchHoleResponse_Failure.ID_NOT_EXIST:
this.msgbox('error', 'Error', 'ID does not exist');
this.msgbox("error", "Error", "ID does not exist");
break;
case rendezvous.PunchHoleResponse_Failure.OFFLINE:
this.msgbox('error', 'Error', 'Remote desktop is offline');
this.msgbox("error", "Error", "Remote desktop is offline");
break;
case rendezvous.PunchHoleResponse_Failure.LICENSE_MISMATCH:
this.msgbox('error', 'Error', 'Key mismatch');
this.msgbox("error", "Error", "Key mismatch");
break;
default:
if (phr?.otherFailure) {
this.msgbox('error', 'Error', phr?.otherFailure);
this.msgbox("error", "Error", phr?.otherFailure);
}
}
}
@@ -87,10 +100,10 @@ export default class Connection {
uri = getDefaultUri(true);
}
const uuid = rr.uuid;
console.log(new Date() + ': Connecting to relay server: ' + uri);
console.log(new Date() + ": Connecting to relay server: " + uri);
const ws = new Websock(uri);
await ws.open();
console.log(new Date() + ': Connected to relay server');
console.log(new Date() + ": Connected to relay server");
this._ws = ws;
const requestRelay = rendezvous.RequestRelay.fromPartial({
licenceKey,
@@ -103,7 +116,7 @@ export default class Connection {
async secure(pk: Uint8Array | undefined) {
if (pk) {
const RS_PK = 'OeVuKk5nlHiXp+APNn0Y3pC1Iwpwn44JGqrQCsWqmBw=';
const RS_PK = "OeVuKk5nlHiXp+APNn0Y3pC1Iwpwn44JGqrQCsWqmBw=";
try {
pk = await globals.verify(pk, RS_PK).catch();
if (pk?.length != 32) {
@@ -113,7 +126,10 @@ export default class Connection {
console.error(e);
pk = undefined;
}
if (!pk) console.error('Handshake failed: invalid public key from rendezvous server');
if (!pk)
console.error(
"Handshake failed: invalid public key from rendezvous server"
);
}
if (!pk) {
// send an empty message out in case server is setting up secure and waiting for first message
@@ -138,7 +154,7 @@ export default class Connection {
return;
}
signedId = new TextDecoder().decode(signedId!);
const tmp = signedId.split('\0');
const tmp = signedId.split("\0");
const id = tmp[0];
let theirPk = tmp[1];
if (id != this._id!) {
@@ -148,16 +164,21 @@ export default class Connection {
}
theirPk = globals.decodeBase64(theirPk);
if (theirPk.length != 32) {
console.error("Handshake failed: invalid public box key length from peer");
console.error(
"Handshake failed: invalid public box key length from peer"
);
await this._ws?.sendMessage({});
return;
}
const [mySk, asymmetricValue] = globals.genBoxKeyPair();
const secretKey = globals.genSecretKey();
const symmetricValue = globals.seal(secretKey, theirPk, mySk);
const publicKey = message.PublicKey.fromPartial({ asymmetricValue, symmetricValue });
const publicKey = message.PublicKey.fromPartial({
asymmetricValue,
symmetricValue,
});
await this._ws?.sendMessage({ publicKey });
this._ws?.setSecretKey(secretKey)
this._ws?.setSecretKey(secretKey);
}
async msgLoop() {
@@ -175,13 +196,17 @@ export default class Connection {
} else if (msg?.loginResponse) {
const r = msg?.loginResponse;
if (r.error) {
this.msgbox('error', 'Error', r.error);
this.msgbox("error", "Error", r.error);
} else if (r.peerInfo) {
this._peerInfo = r.peerInfo;
this.msgbox('success', 'Successful', 'Connected, waiting for image...');
this.msgbox(
"success",
"Successful",
"Connected, waiting for image..."
);
}
} else if (msg?.videoFrame) {
this.handleVideoFrame();
this.handleVideoFrame(msg?.videoFrame!);
}
}
}
@@ -194,6 +219,10 @@ export default class Connection {
this._msgbox?.(type_, title, text);
}
draw(frame: Uint8Array) {
this._draw?.(frame);
}
close() {
clearInterval(this._interval);
this._ws?.close();
@@ -203,8 +232,12 @@ export default class Connection {
this._msgbox = callback;
}
setDraw(callback: DrawCallback) {
this._draw = callback;
}
async login(password: string) {
this.msgbox('connecting', 'Connecting...', 'Logging in...');
this.msgbox("connecting", "Connecting...", "Logging in...");
let salt = this._hash?.salt;
if (salt) {
let p = hash([password, salt]);
@@ -219,21 +252,34 @@ export default class Connection {
async _sendLoginMessage(password: Uint8Array | undefined = undefined) {
const loginRequest = message.LoginRequest.fromPartial({
username: this._id!,
myId: 'web', // to-do
myName: 'web', // to-do
myId: "web", // to-do
myName: "web", // to-do
password,
});
await this._ws?.sendMessage({ loginRequest });
}
handleVideoFrame() {
handleVideoFrame(vf: message.VideoFrame) {
if (!this._firstFrame) {
this.msgbox('', '', '');
this.msgbox("", "", "");
this._firstFrame = true;
}
if (vf.vp9s) {
let dec = this._videoDecoder;
vf.vp9s.frames.forEach((f) => {
dec.processFrame(f.data.buffer, (ok: any) => {
console.log(ok);
if (dec.frameBuffer) {
console.log(dec.frameBuffer);
this.draw(dec.frameBuffer);
}
});
});
}
}
}
// @ts-ignore
async function testDelay() {
const ws = new Websock(getDefaultUri(false));
await ws.open();
@@ -241,17 +287,17 @@ async function testDelay() {
}
function getDefaultUri(isRelay: Boolean = false): string {
const host = localStorage.getItem('host');
return SCHEMA + (host || HOST) + ':' + (PORT + (isRelay ? 3 : 2));
const host = localStorage.getItem("host");
return SCHEMA + (host || HOST) + ":" + (PORT + (isRelay ? 3 : 2));
}
function getrUriFromRs(uri: string): string {
if (uri.indexOf(':') > 0) {
const tmp = uri.split(':');
if (uri.indexOf(":") > 0) {
const tmp = uri.split(":");
const port = parseInt(tmp[1]);
uri = tmp[0] + ':' + (port + 2);
uri = tmp[0] + ":" + (port + 2);
} else {
uri += ':' + (PORT + 3);
uri += ":" + (PORT + 3);
}
return SCHEMA + uri;
}
@@ -259,10 +305,10 @@ function getrUriFromRs(uri: string): string {
function hash(datas: (string | Uint8Array)[]): Uint8Array {
const hasher = new sha256.Hash();
datas.forEach((data) => {
if (typeof data == 'string') {
if (typeof data == "string") {
data = new TextEncoder().encode(data);
}
return hasher.update(data);
});
return hasher.digest();
}
}