This commit is contained in:
open-trade 2022-01-26 18:58:55 +08:00
parent 6e711c25b9
commit fad130b29a
3 changed files with 127 additions and 22 deletions

View File

@ -2,7 +2,7 @@
"name": "web_hbb",
"version": "1.0.0",
"scripts": {
"dev": "curl -O https://raw.githubusercontent.com/rgov/js-theora-decoder/main/yuv-canvas-1.2.6.js; vite",
"dev": "curl -O https://raw.githubusercontent.com/rgov/js-theora-decoder/main/yuv-canvas-1.2.6.js && vite",
"build": "tsc && vite build",
"preview": "vite preview"
},

View File

@ -6,9 +6,8 @@ import * as sha256 from "fast-sha256";
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 SCHEMA = 'ws://';
type MsgboxCallback = (type: string, title: string, text: string) => void;
type DrawCallback = (data: Uint8Array) => void;
@ -26,6 +25,7 @@ export default class Connection {
_videoDecoder: any;
_audioDecoder: any;
_password: string | undefined;
_options: any;
constructor() {
this._msgbox = globals.msgbox;
@ -35,6 +35,11 @@ export default class Connection {
}
async start(id: string) {
try {
this._options = JSON.parse((localStorage.getItem('peers') || '{}'))[id] || {};
} catch (e) {
this._options = {};
}
this._interval = setInterval(() => {
while (this._msgs.length) {
this._ws?.sendMessage(this._msgs[0]);
@ -61,7 +66,7 @@ export default class Connection {
const natType = rendezvous.NatType.SYMMETRIC;
const punchHoleRequest = rendezvous.PunchHoleRequest.fromPartial({
id,
licenceKey,
licenceKey: localStorage.getItem('key') || undefined,
connType,
natType,
});
@ -109,7 +114,7 @@ export default class Connection {
console.log(new Date() + ": Connected to relay server");
this._ws = ws;
const requestRelay = rendezvous.RequestRelay.fromPartial({
licenceKey,
licenceKey: localStorage.getItem('key') || undefined,
uuid,
});
ws.sendRendezvous({ requestRelay });
@ -209,11 +214,11 @@ export default class Connection {
this.handleVideoFrame(msg?.videoFrame!);
} else if (msg?.clipboard) {
const cb = msg?.clipboard;
if (cb.compress) cb.content = globals.decompress(cb.content);
if (cb.compress) cb.content = globals.decompress(cb.content)!;
globals.pushEvent("clipboard", cb);
} else if (msg?.cursorData) {
const cd = msg?.cursorData;
cd.colors = globals.decompress(cd.colors);
cd.colors = globals.decompress(cd.colors)!;
globals.pushEvent("cursor_data", cd);
} else if (msg?.cursorId) {
globals.pushEvent("cursor_id", { id: msg?.cursorId });
@ -344,6 +349,14 @@ export default class Connection {
this.msgbox("error", "Connection Error", misc.closeReason);
}
}
getRemember(): any {
return this._options['remember'];
}
getOption(name: string): any {
return this._options[name];
}
}
// @ts-ignore
@ -354,7 +367,7 @@ async function testDelay() {
}
function getDefaultUri(isRelay: Boolean = false): string {
const host = localStorage.getItem("host");
const host = localStorage.getItem("custom-rendezvous-server");
return SCHEMA + (host || HOST) + ":" + (PORT + (isRelay ? 3 : 2));
}

View File

@ -1,14 +1,14 @@
import Connection from "./connection";
import _sodium from "libsodium-wrappers";
import { ZSTDecoder } from 'zstddec';
import * as zstd from 'zstddec';
import { CursorData } from "./message";
const decompressor = new ZSTDDecoder();
await decompressor.init();
const decompressor = new zstd.ZSTDDecoder();
var currentFrame = undefined;
var events = [];
window.currentConnection = undefined;
window.curConn = undefined;
window.getRgba = () => currentFrame;
window.getLanguage = () => navigator.language;
@ -36,11 +36,11 @@ export function draw(frame) {
}
export function setConn(conn) {
window.currentConnection = conn;
window.curConn = conn;
}
export function getConn() {
return window.currentConnection;
return window.curConn;
}
export function close() {
@ -50,7 +50,7 @@ export function close() {
}
export function newConn() {
window.currentConnection?.close();
window.curConn?.close();
const conn = new Connection();
setConn(conn);
return conn;
@ -126,38 +126,130 @@ export function decompress(compressedArray) {
}
window.setByName = (name, value) => {
try {
value = JSON.parse(value);
} catch (e) {}
switch (name) {
case 'connect':
case 'connect':
newConn();
curConn.start(value);
break;
case 'login':
currentConnection.login(value.password, value.remember);
curConn.login(value.password, value.remember || false);
break;
case 'close':
close();
break;
case 'refresh':
currentConnection.refresh();
curConn.refresh();
break;
case 'reconnect':
currentConnection.reconnect();
curConn.reconnect();
break;
case 'toggle_option':
curConn.toggleOption(value);
break;
case 'image_quality':
curConn.setImageQuality(value);
break;
case 'lock_screen':
curConn.lockScreen();
break;
case 'ctrl_alt_del':
curConn.ctrlAltDe();
break;
case 'switch_display':
curConn.switchDisplay(value);
break;
case 'remove':
const peers = JSON.parse(localStorage.getItem('peers') || '{}');
delete peers[value];
localStorage.setItem('peers', JSON.stringify(peers));
break;
case 'input_key':
curConn.inputKey(value.name, value.alt || false, value.ctrl || false, value.shift || false, value.command || false);
break;
case 'input_string':
curConn.inputString(value);
break;
case 'send_mouse':
let mask = 0;
switch (value.type) {
case 'down':
mask = 1;
break;
case 'up':
mask = 2;
break;
case 'wheel':
mask = 3;
break;
}
switch (value.buttons) {
case 'left':
mask |= 1 << 3;
break;
case 'right':
mask |= 2 << 3;
break;
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);
break;
case 'option':
localStorage.setItem(value.name, value.value);
break;
case 'peer_option':
curConn.setPeerOption(value.name, value.value);
break;
case 'input_os_password':
curConn.inputOsPassword(value, true);
break;
default:
break;
}
}
window.getByName = (name, value) => {
window.getByName = (name, arg) => {
try {
arg = JSON.parse(arg);
} catch (e) {}
switch (name) {
case 'peers':
return localStorage.getItem('peers');
break;
case 'remote_id':
return localStorage.getItem('remote-id') || '';
break;
case 'remember':
return curConn.getRemember();
break;
case 'event':
if (events.length) {
const e = events[0];
events.splice(0, 1);
return e;
return JSON.stringify(e);
}
break;
case 'toggle_option':
return curConn.getOption(arg);
break;
case 'option':
return localStorage.getItem(arg);
break;
case 'image_quality':
return curConn.getImageQuality();
break;
case 'translate':
return arg.text;
break;
case 'peer_option':
return curConn.getOption(arg);
break;
}
}
window.init = () => {
decompressor.init();
}