more api
This commit is contained in:
parent
f17aff8bb4
commit
b750ec7ac5
@ -51,6 +51,9 @@ def main():
|
|||||||
print('export const KEY_MAP: any = {')
|
print('export const KEY_MAP: any = {')
|
||||||
print(KEY_MAP[0])
|
print(KEY_MAP[0])
|
||||||
print('}')
|
print('}')
|
||||||
|
for ln in open('../hbb/Cargo.toml'):
|
||||||
|
if ln.startswith('version ='):
|
||||||
|
print('export const ' + ln)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -25,7 +25,7 @@ export default class Connection {
|
|||||||
_firstFrame: Boolean | undefined;
|
_firstFrame: Boolean | undefined;
|
||||||
_videoDecoder: any;
|
_videoDecoder: any;
|
||||||
_audioDecoder: any;
|
_audioDecoder: any;
|
||||||
_password: string | undefined;
|
_password: Uint8Array | undefined;
|
||||||
_options: any;
|
_options: any;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
@ -36,11 +36,18 @@ export default class Connection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async start(id: string) {
|
async start(id: string) {
|
||||||
try {
|
if (!this._options) {
|
||||||
this._options =
|
this._options = globals.getPeers()[id] || {};
|
||||||
JSON.parse(localStorage.getItem("peers") || "{}")[id] || {};
|
}
|
||||||
} catch (e) {
|
if (!this._password) {
|
||||||
this._options = {};
|
const p = this.getOption("password");
|
||||||
|
if (p) {
|
||||||
|
try {
|
||||||
|
this._password = Uint8Array.from(JSON.parse("[" + p + "]"));
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
this._interval = setInterval(() => {
|
this._interval = setInterval(() => {
|
||||||
while (this._msgs.length) {
|
while (this._msgs.length) {
|
||||||
@ -203,9 +210,10 @@ export default class Connection {
|
|||||||
this._hash = msg?.hash;
|
this._hash = msg?.hash;
|
||||||
if (!this._password)
|
if (!this._password)
|
||||||
this.msgbox("input-password", "Password Required", "");
|
this.msgbox("input-password", "Password Required", "");
|
||||||
this.login(this._password);
|
this.login();
|
||||||
} else if (msg?.test_delay) {
|
} else if (msg?.test_delay) {
|
||||||
const test_delay = msg?.test_delay;
|
const test_delay = msg?.test_delay;
|
||||||
|
console.log(test_delay);
|
||||||
if (!test_delay.from_client) {
|
if (!test_delay.from_client) {
|
||||||
this._ws?.sendMessage({ test_delay });
|
this._ws?.sendMessage({ test_delay });
|
||||||
}
|
}
|
||||||
@ -261,9 +269,7 @@ export default class Connection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
refresh() {
|
refresh() {
|
||||||
const misc = message.Misc.fromPartial({
|
const misc = message.Misc.fromPartial({ refresh_video: true });
|
||||||
refresh_video: true,
|
|
||||||
});
|
|
||||||
this._ws?.sendMessage({ misc });
|
this._ws?.sendMessage({ misc });
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -275,17 +281,22 @@ export default class Connection {
|
|||||||
this._draw = callback;
|
this._draw = callback;
|
||||||
}
|
}
|
||||||
|
|
||||||
login(password: string | undefined, _remember: Boolean = false) {
|
login(password: string | undefined = undefined) {
|
||||||
this._password = password;
|
|
||||||
if (password) {
|
if (password) {
|
||||||
const salt = this._hash?.salt;
|
const salt = this._hash?.salt;
|
||||||
let p = hash([password, salt!]);
|
let p = hash([password, salt!]);
|
||||||
|
this._password = p;
|
||||||
const challenge = this._hash?.challenge;
|
const challenge = this._hash?.challenge;
|
||||||
p = hash([p, challenge!]);
|
p = hash([p, challenge!]);
|
||||||
this.msgbox("connecting", "Connecting...", "Logging in...");
|
this.msgbox("connecting", "Connecting...", "Logging in...");
|
||||||
this._sendLoginMessage(p);
|
this._sendLoginMessage(p);
|
||||||
} else {
|
} 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...");
|
this.msgbox("success", "Successful", "Connected, waiting for image...");
|
||||||
globals.pushEvent("peer_info", pi);
|
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) {
|
handleMisc(misc: message.Misc) {
|
||||||
@ -397,12 +434,24 @@ export default class Connection {
|
|||||||
return this._options["remember"];
|
return this._options["remember"];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setRemember(v: Boolean) {
|
||||||
|
this.setOption("remember", v);
|
||||||
|
}
|
||||||
|
|
||||||
getOption(name: string): any {
|
getOption(name: string): any {
|
||||||
return this._options[name];
|
return this._options[name];
|
||||||
}
|
}
|
||||||
|
|
||||||
setOption(name: string, value: any) {
|
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(
|
inputKey(
|
||||||
|
|||||||
@ -741,3 +741,5 @@ export const KEY_MAP: any = {
|
|||||||
"LOCK_SCREEN": "LockScreen",
|
"LOCK_SCREEN": "LockScreen",
|
||||||
|
|
||||||
}
|
}
|
||||||
|
export const version = "1.1.9"
|
||||||
|
|
||||||
|
|||||||
@ -2,7 +2,7 @@ import Connection from "./connection";
|
|||||||
import _sodium from "libsodium-wrappers";
|
import _sodium from "libsodium-wrappers";
|
||||||
import { CursorData } from "./message";
|
import { CursorData } from "./message";
|
||||||
import { loadOpus, loadVp9 } from "./codec";
|
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";
|
import { initZstd, translate } from "./common";
|
||||||
|
|
||||||
var currentFrame = undefined;
|
var currentFrame = undefined;
|
||||||
@ -62,6 +62,7 @@ export async function startConn(id) {
|
|||||||
try {
|
try {
|
||||||
currentFrame = undefined;
|
currentFrame = undefined;
|
||||||
events = [];
|
events = [];
|
||||||
|
setByName('remote_id', id);
|
||||||
await curConn.start(id);
|
await curConn.start(id);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(e);
|
console.log(e);
|
||||||
@ -136,16 +137,18 @@ export function decrypt(signed, nonce, key) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
window.setByName = (name, value) => {
|
window.setByName = (name, value) => {
|
||||||
try {
|
|
||||||
value = JSON.parse(value);
|
|
||||||
} catch (e) { }
|
|
||||||
switch (name) {
|
switch (name) {
|
||||||
|
case 'remote_id':
|
||||||
|
localStorage.setItem('remote-id', value);
|
||||||
|
break;
|
||||||
case 'connect':
|
case 'connect':
|
||||||
newConn();
|
newConn();
|
||||||
startConn(String(value));
|
startConn(value);
|
||||||
break;
|
break;
|
||||||
case 'login':
|
case 'login':
|
||||||
curConn.login(value.password, value.remember || false);
|
value = JSON.parse(value);
|
||||||
|
curConn.setRemember(value.remember == 'true');
|
||||||
|
curConn.login(value.password);
|
||||||
break;
|
break;
|
||||||
case 'close':
|
case 'close':
|
||||||
close();
|
close();
|
||||||
@ -172,11 +175,12 @@ window.setByName = (name, value) => {
|
|||||||
curConn.switchDisplay(value);
|
curConn.switchDisplay(value);
|
||||||
break;
|
break;
|
||||||
case 'remove':
|
case 'remove':
|
||||||
const peers = JSON.parse(localStorage.getItem('peers') || '{}');
|
const peers = getPeers();
|
||||||
delete peers[value];
|
delete peers[value];
|
||||||
localStorage.setItem('peers', JSON.stringify(peers));
|
localStorage.setItem('peers', JSON.stringify(peers));
|
||||||
break;
|
break;
|
||||||
case 'input_key':
|
case 'input_key':
|
||||||
|
value = JSON.parse(value);
|
||||||
curConn.inputKey(value.name, value.alt || false, value.ctrl || false, value.shift || false, value.command || false);
|
curConn.inputKey(value.name, value.alt || false, value.ctrl || false, value.shift || false, value.command || false);
|
||||||
break;
|
break;
|
||||||
case 'input_string':
|
case 'input_string':
|
||||||
@ -184,6 +188,7 @@ window.setByName = (name, value) => {
|
|||||||
break;
|
break;
|
||||||
case 'send_mouse':
|
case 'send_mouse':
|
||||||
let mask = 0;
|
let mask = 0;
|
||||||
|
value = JSON.parse(value);
|
||||||
switch (value.type) {
|
switch (value.type) {
|
||||||
case 'down':
|
case 'down':
|
||||||
mask = 1;
|
mask = 1;
|
||||||
@ -205,12 +210,14 @@ window.setByName = (name, value) => {
|
|||||||
case 'wheel':
|
case 'wheel':
|
||||||
mask |= 4 << 3;
|
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;
|
break;
|
||||||
case 'option':
|
case 'option':
|
||||||
|
value = JSON.parse(value);
|
||||||
localStorage.setItem(value.name, value.value);
|
localStorage.setItem(value.name, value.value);
|
||||||
break;
|
break;
|
||||||
case 'peer_option':
|
case 'peer_option':
|
||||||
|
value = JSON.parse(value);
|
||||||
curConn.setOption(value.name, value.value);
|
curConn.setOption(value.name, value.value);
|
||||||
break;
|
break;
|
||||||
case 'input_os_password':
|
case 'input_os_password':
|
||||||
@ -251,6 +258,8 @@ window.getByName = (name, arg) => {
|
|||||||
return curConn.getOption(arg);
|
return curConn.getOption(arg);
|
||||||
case 'test_if_valid_server':
|
case 'test_if_valid_server':
|
||||||
break;
|
break;
|
||||||
|
case 'version':
|
||||||
|
return version;
|
||||||
}
|
}
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
@ -259,4 +268,12 @@ window.init = async () => {
|
|||||||
loadOpus(() => { });
|
loadOpus(() => { });
|
||||||
loadVp9(() => { });
|
loadVp9(() => { });
|
||||||
await initZstd();
|
await initZstd();
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getPeers() {
|
||||||
|
try {
|
||||||
|
return JSON.parse(localStorage.getItem('peers')) || {};
|
||||||
|
} catch (e) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -78,7 +78,7 @@ export default class Websock {
|
|||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
if (this._status != "open") {
|
if (this._status != "open") {
|
||||||
reject(this._status || "timeout");
|
reject(this._status || "Timeout");
|
||||||
}
|
}
|
||||||
}, timeout);
|
}, timeout);
|
||||||
this._websocket.onopen = () => {
|
this._websocket.onopen = () => {
|
||||||
@ -131,7 +131,7 @@ export default class Websock {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (new Date().getTime() > tm0 + timeout) {
|
if (new Date().getTime() > tm0 + timeout) {
|
||||||
reject("timeout");
|
reject("Timeout");
|
||||||
} else {
|
} else {
|
||||||
setTimeout(() => func(resolve, reject, tm0), 1);
|
setTimeout(() => func(resolve, reject, tm0), 1);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user