diff --git a/src/connection.ts b/src/connection.ts index cfbb72f11..2c3153112 100644 --- a/src/connection.ts +++ b/src/connection.ts @@ -6,8 +6,8 @@ import * as sha256 from "fast-sha256"; import * as globals from "./globals"; const PORT = 21116; -const HOST = 'rs-sg.rustdesk.com'; -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; @@ -36,7 +36,8 @@ export default class Connection { async start(id: string) { try { - this._options = JSON.parse((localStorage.getItem('peers') || '{}'))[id] || {}; + this._options = + JSON.parse(localStorage.getItem("peers") || "{}")[id] || {}; } catch (e) { this._options = {}; } @@ -66,7 +67,7 @@ export default class Connection { const natType = rendezvous.NatType.SYMMETRIC; const punchHoleRequest = rendezvous.PunchHoleRequest.fromPartial({ id, - licenceKey: localStorage.getItem('key') || undefined, + licenceKey: localStorage.getItem("key") || undefined, connType, natType, }); @@ -114,7 +115,7 @@ export default class Connection { console.log(new Date() + ": Connected to relay server"); this._ws = ws; const requestRelay = rendezvous.RequestRelay.fromPartial({ - licenceKey: localStorage.getItem('key') || undefined, + licenceKey: localStorage.getItem("key") || undefined, uuid, }); ws.sendRendezvous({ requestRelay }); @@ -351,7 +352,7 @@ export default class Connection { } getRemember(): any { - return this._options['remember']; + return this._options["remember"]; } getOption(name: string): any { diff --git a/src/globals.js b/src/globals.js index 713d8baed..3562bbdef 100644 --- a/src/globals.js +++ b/src/globals.js @@ -13,16 +13,16 @@ window.getRgba = () => currentFrame; window.getLanguage = () => navigator.language; export function msgbox(type, title, text) { - text = text.toLowerCase(); - var hasRetry = msgtype == "error" + const text2 = text.toLowerCase(); + var hasRetry = type == "error" && title == "Connection Error" - && !text.indexOf("offline") >= 0 - && !text.indexOf("exist") >= 0 - && !text.indexOf("handshake") >= 0 - && !text.indexOf("failed") >= 0 - && !text.indexOf("resolve") >= 0 - && !text.indexOf("mismatch") >= 0 - && !text.indexOf("manually") >= 0; + && text2.indexOf("offline") < 0 + && text2.indexOf("exist") < 0 + && text2.indexOf("handshake") < 0 + && text2.indexOf("failed") < 0 + && text2.indexOf("resolve") < 0 + && text2.indexOf("mismatch") < 0 + && text2.indexOf("manually") < 0; events.push({ name: 'msgbox', type, title, text, hasRetry }); } @@ -43,6 +43,15 @@ export function getConn() { return window.curConn; } +export async function startConn(id) { + try { + await curConn.start(id); + } catch (e) { + console.log(e); + msgbox('error', 'Error', String(e)); + } +} + export function close() { getConn()?.close(); setConn(undefined); @@ -128,11 +137,11 @@ export function decompress(compressedArray) { window.setByName = (name, value) => { try { value = JSON.parse(value); - } catch (e) {} + } catch (e) { } switch (name) { case 'connect': newConn(); - curConn.start(value); + startConn(value); break; case 'login': curConn.login(value.password, value.remember || false); @@ -214,10 +223,10 @@ window.setByName = (name, value) => { window.getByName = (name, arg) => { try { arg = JSON.parse(arg); - } catch (e) {} + } catch (e) { } switch (name) { case 'peers': - return localStorage.getItem('peers'); + return localStorage.getItem('peers') || '[]'; break; case 'remote_id': return localStorage.getItem('remote-id') || ''; @@ -247,7 +256,10 @@ window.getByName = (name, arg) => { case 'peer_option': return curConn.getOption(arg); break; + case 'test_if_valid_server': + break; } + return ''; } window.init = () => { diff --git a/src/ui.js b/src/ui.js index c5c55bf5c..f506d0120 100644 --- a/src/ui.js +++ b/src/ui.js @@ -31,7 +31,7 @@ if (app) { document.body.onload = () => { const host = document.querySelector('#host'); - host.value = localStorage.getItem('host'); + host.value = localStorage.getItem('custom-rendezvous-server'); const id = document.querySelector('#id'); id.value = localStorage.getItem('id'); const key = document.querySelector('#key'); @@ -41,7 +41,7 @@ if (app) { window.connect = () => { const host = document.querySelector('#host'); - localStorage.setItem('host', host.value); + localStorage.setItem('custom-rendezvous-server', host.value); const id = document.querySelector('#id'); localStorage.setItem('id', id.value); const key = document.querySelector('#key'); diff --git a/src/websock.ts b/src/websock.ts index 1ce2c45e7..1087495a3 100644 --- a/src/websock.ts +++ b/src/websock.ts @@ -11,6 +11,7 @@ export default class Websock { _status: any; _latency: number; _secretKey: [Uint8Array, number, number] | undefined; + _uri: string; constructor(uri: string) { this._eventHandlers = { @@ -19,6 +20,7 @@ export default class Websock { close: () => {}, error: () => {}, }; + this._uri = uri; this._status = ""; this._buf = []; this._websocket = new WebSocket(uri); @@ -36,7 +38,9 @@ export default class Websock { } sendMessage(json: any) { - let data = message.Message.encode(message.Message.fromPartial(json)).finish(); + let data = message.Message.encode( + message.Message.fromPartial(json) + ).finish(); let k = this._secretKey; if (k) { k[1] += 1; @@ -98,6 +102,10 @@ export default class Websock { reject(e); }; this._websocket.onerror = (e) => { + if (!this._status) { + reject('Failed to connect to ' + this._uri); + return; + } this._status = e; console.error("WebSock.onerror: " + e); this._eventHandlers.error(e);