From b750ec7ac50ba1f9069ba0f4ce380b41ca813df6 Mon Sep 17 00:00:00 2001 From: rustdesk Date: Sun, 30 Jan 2022 19:48:41 +0800 Subject: [PATCH] more api --- gen_js_from_hbb.py | 3 ++ src/connection.ts | 77 ++++++++++++++++++++++++++++++++++-------- src/gen_js_from_hbb.ts | 2 ++ src/globals.js | 33 +++++++++++++----- src/websock.ts | 4 +-- 5 files changed, 95 insertions(+), 24 deletions(-) diff --git a/gen_js_from_hbb.py b/gen_js_from_hbb.py index 73a1f8ebe..024cef387 100755 --- a/gen_js_from_hbb.py +++ b/gen_js_from_hbb.py @@ -51,6 +51,9 @@ def main(): print('export const KEY_MAP: any = {') print(KEY_MAP[0]) print('}') + for ln in open('../hbb/Cargo.toml'): + if ln.startswith('version ='): + print('export const ' + ln) diff --git a/src/connection.ts b/src/connection.ts index 27998bab5..11d1b39e1 100644 --- a/src/connection.ts +++ b/src/connection.ts @@ -25,7 +25,7 @@ export default class Connection { _firstFrame: Boolean | undefined; _videoDecoder: any; _audioDecoder: any; - _password: string | undefined; + _password: Uint8Array | undefined; _options: any; constructor() { @@ -36,11 +36,18 @@ export default class Connection { } async start(id: string) { - try { - this._options = - JSON.parse(localStorage.getItem("peers") || "{}")[id] || {}; - } catch (e) { - this._options = {}; + if (!this._options) { + this._options = globals.getPeers()[id] || {}; + } + if (!this._password) { + const p = this.getOption("password"); + if (p) { + try { + this._password = Uint8Array.from(JSON.parse("[" + p + "]")); + } catch (e) { + console.error(e); + } + } } this._interval = setInterval(() => { while (this._msgs.length) { @@ -203,9 +210,10 @@ export default class Connection { this._hash = msg?.hash; if (!this._password) this.msgbox("input-password", "Password Required", ""); - this.login(this._password); + this.login(); } else if (msg?.test_delay) { const test_delay = msg?.test_delay; + console.log(test_delay); if (!test_delay.from_client) { this._ws?.sendMessage({ test_delay }); } @@ -261,9 +269,7 @@ export default class Connection { } refresh() { - const misc = message.Misc.fromPartial({ - refresh_video: true, - }); + const misc = message.Misc.fromPartial({ refresh_video: true }); this._ws?.sendMessage({ misc }); } @@ -275,17 +281,22 @@ export default class Connection { this._draw = callback; } - login(password: string | undefined, _remember: Boolean = false) { - this._password = password; + login(password: string | undefined = undefined) { if (password) { const salt = this._hash?.salt; let p = hash([password, salt!]); + this._password = p; const challenge = this._hash?.challenge; p = hash([p, challenge!]); this.msgbox("connecting", "Connecting...", "Logging in..."); this._sendLoginMessage(p); } else { - this._sendLoginMessage(); + let p = this._password; + if (p) { + const challenge = this._hash?.challenge; + p = hash([p, challenge!]); + } + this._sendLoginMessage(p); } } @@ -363,6 +374,32 @@ export default class Connection { } this.msgbox("success", "Successful", "Connected, waiting for image..."); globals.pushEvent("peer_info", pi); + const p = this.shouldAutoLogin(); + if (p) this.inputOsPassword(p); + const username = this.getOption("info")?.username; + if (username && !pi.username) pi.username = username; + this.setOption("info", pi); + if (this.getRemember()) { + if (this._password?.length) { + const p = this._password.toString(); + if (p != this.getOption("password")) { + this.setOption("password", p); + console.log("remember password of " + this._id); + } + } + } else { + this.setOption("password", undefined); + } + } + + shouldAutoLogin(): string { + const l = this.getOption("lock-after-session-end"); + const a = !!this.getOption("auto-login"); + const p = this.getOption("os-password"); + if (p && l && a) { + return p; + } + return ""; } handleMisc(misc: message.Misc) { @@ -397,12 +434,24 @@ export default class Connection { return this._options["remember"]; } + setRemember(v: Boolean) { + this.setOption("remember", v); + } + getOption(name: string): any { return this._options[name]; } setOption(name: string, value: any) { - this._options[name] = value; + if (value == undefined) { + delete this._options[name]; + } else { + this._options[name] = value; + } + this._options["tm"] = new Date().getTime(); + const peers = globals.getPeers(); + peers[this._id] = this._options; + localStorage.setItem("peers", JSON.stringify(peers)); } inputKey( diff --git a/src/gen_js_from_hbb.ts b/src/gen_js_from_hbb.ts index e9af6cf20..2ad8407a9 100644 --- a/src/gen_js_from_hbb.ts +++ b/src/gen_js_from_hbb.ts @@ -741,3 +741,5 @@ export const KEY_MAP: any = { "LOCK_SCREEN": "LockScreen", } +export const version = "1.1.9" + diff --git a/src/globals.js b/src/globals.js index 3e30d9d5b..d7154043e 100644 --- a/src/globals.js +++ b/src/globals.js @@ -2,7 +2,7 @@ import Connection from "./connection"; import _sodium from "libsodium-wrappers"; import { CursorData } from "./message"; import { loadOpus, loadVp9 } from "./codec"; -import { checkIfRetry } from "./gen_js_from_hbb"; +import { checkIfRetry, version } from "./gen_js_from_hbb"; import { initZstd, translate } from "./common"; var currentFrame = undefined; @@ -62,6 +62,7 @@ export async function startConn(id) { try { currentFrame = undefined; events = []; + setByName('remote_id', id); await curConn.start(id); } catch (e) { console.log(e); @@ -136,16 +137,18 @@ export function decrypt(signed, nonce, key) { } window.setByName = (name, value) => { - try { - value = JSON.parse(value); - } catch (e) { } switch (name) { + case 'remote_id': + localStorage.setItem('remote-id', value); + break; case 'connect': newConn(); - startConn(String(value)); + startConn(value); break; case 'login': - curConn.login(value.password, value.remember || false); + value = JSON.parse(value); + curConn.setRemember(value.remember == 'true'); + curConn.login(value.password); break; case 'close': close(); @@ -172,11 +175,12 @@ window.setByName = (name, value) => { curConn.switchDisplay(value); break; case 'remove': - const peers = JSON.parse(localStorage.getItem('peers') || '{}'); + const peers = getPeers(); delete peers[value]; localStorage.setItem('peers', JSON.stringify(peers)); break; case 'input_key': + value = JSON.parse(value); curConn.inputKey(value.name, value.alt || false, value.ctrl || false, value.shift || false, value.command || false); break; case 'input_string': @@ -184,6 +188,7 @@ window.setByName = (name, value) => { break; case 'send_mouse': let mask = 0; + value = JSON.parse(value); switch (value.type) { case 'down': mask = 1; @@ -205,12 +210,14 @@ window.setByName = (name, value) => { case 'wheel': mask |= 4 << 3; } - curConn.inputMouse(mask, value.x || 0, value.y || 0, value.alt || false, value.ctrl || false, value.shift || false, value.command || false); + curConn.inputMouse(mask, parseInt(value.x || '0'), parseInt(value.y || '0'), value.alt || false, value.ctrl || false, value.shift || false, value.command || false); break; case 'option': + value = JSON.parse(value); localStorage.setItem(value.name, value.value); break; case 'peer_option': + value = JSON.parse(value); curConn.setOption(value.name, value.value); break; case 'input_os_password': @@ -251,6 +258,8 @@ window.getByName = (name, arg) => { return curConn.getOption(arg); case 'test_if_valid_server': break; + case 'version': + return version; } return ''; } @@ -259,4 +268,12 @@ window.init = async () => { loadOpus(() => { }); loadVp9(() => { }); await initZstd(); +} + +export function getPeers() { + try { + return JSON.parse(localStorage.getItem('peers')) || {}; + } catch (e) { + return {}; + } } \ No newline at end of file diff --git a/src/websock.ts b/src/websock.ts index 6b05827de..5eda025d6 100644 --- a/src/websock.ts +++ b/src/websock.ts @@ -78,7 +78,7 @@ export default class Websock { return new Promise((resolve, reject) => { setTimeout(() => { if (this._status != "open") { - reject(this._status || "timeout"); + reject(this._status || "Timeout"); } }, timeout); this._websocket.onopen = () => { @@ -131,7 +131,7 @@ export default class Websock { return; } if (new Date().getTime() > tm0 + timeout) { - reject("timeout"); + reject("Timeout"); } else { setTimeout(() => func(resolve, reject, tm0), 1); }