will do more on socket error

This commit is contained in:
rustdesk 2022-01-27 01:30:29 +08:00
parent fad130b29a
commit 6c0030bbe2
4 changed files with 43 additions and 22 deletions

View File

@ -6,8 +6,8 @@ import * as sha256 from "fast-sha256";
import * as globals from "./globals"; import * as globals from "./globals";
const PORT = 21116; const PORT = 21116;
const HOST = 'rs-sg.rustdesk.com'; const HOST = "rs-sg.rustdesk.com";
const SCHEMA = 'ws://'; const SCHEMA = "ws://";
type MsgboxCallback = (type: string, title: string, text: string) => void; type MsgboxCallback = (type: string, title: string, text: string) => void;
type DrawCallback = (data: Uint8Array) => void; type DrawCallback = (data: Uint8Array) => void;
@ -36,7 +36,8 @@ export default class Connection {
async start(id: string) { async start(id: string) {
try { try {
this._options = JSON.parse((localStorage.getItem('peers') || '{}'))[id] || {}; this._options =
JSON.parse(localStorage.getItem("peers") || "{}")[id] || {};
} catch (e) { } catch (e) {
this._options = {}; this._options = {};
} }
@ -66,7 +67,7 @@ export default class Connection {
const natType = rendezvous.NatType.SYMMETRIC; const natType = rendezvous.NatType.SYMMETRIC;
const punchHoleRequest = rendezvous.PunchHoleRequest.fromPartial({ const punchHoleRequest = rendezvous.PunchHoleRequest.fromPartial({
id, id,
licenceKey: localStorage.getItem('key') || undefined, licenceKey: localStorage.getItem("key") || undefined,
connType, connType,
natType, natType,
}); });
@ -114,7 +115,7 @@ export default class Connection {
console.log(new Date() + ": Connected to relay server"); console.log(new Date() + ": Connected to relay server");
this._ws = ws; this._ws = ws;
const requestRelay = rendezvous.RequestRelay.fromPartial({ const requestRelay = rendezvous.RequestRelay.fromPartial({
licenceKey: localStorage.getItem('key') || undefined, licenceKey: localStorage.getItem("key") || undefined,
uuid, uuid,
}); });
ws.sendRendezvous({ requestRelay }); ws.sendRendezvous({ requestRelay });
@ -351,7 +352,7 @@ export default class Connection {
} }
getRemember(): any { getRemember(): any {
return this._options['remember']; return this._options["remember"];
} }
getOption(name: string): any { getOption(name: string): any {

View File

@ -13,16 +13,16 @@ window.getRgba = () => currentFrame;
window.getLanguage = () => navigator.language; window.getLanguage = () => navigator.language;
export function msgbox(type, title, text) { export function msgbox(type, title, text) {
text = text.toLowerCase(); const text2 = text.toLowerCase();
var hasRetry = msgtype == "error" var hasRetry = type == "error"
&& title == "Connection Error" && title == "Connection Error"
&& !text.indexOf("offline") >= 0 && text2.indexOf("offline") < 0
&& !text.indexOf("exist") >= 0 && text2.indexOf("exist") < 0
&& !text.indexOf("handshake") >= 0 && text2.indexOf("handshake") < 0
&& !text.indexOf("failed") >= 0 && text2.indexOf("failed") < 0
&& !text.indexOf("resolve") >= 0 && text2.indexOf("resolve") < 0
&& !text.indexOf("mismatch") >= 0 && text2.indexOf("mismatch") < 0
&& !text.indexOf("manually") >= 0; && text2.indexOf("manually") < 0;
events.push({ name: 'msgbox', type, title, text, hasRetry }); events.push({ name: 'msgbox', type, title, text, hasRetry });
} }
@ -43,6 +43,15 @@ export function getConn() {
return window.curConn; 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() { export function close() {
getConn()?.close(); getConn()?.close();
setConn(undefined); setConn(undefined);
@ -132,7 +141,7 @@ window.setByName = (name, value) => {
switch (name) { switch (name) {
case 'connect': case 'connect':
newConn(); newConn();
curConn.start(value); startConn(value);
break; break;
case 'login': case 'login':
curConn.login(value.password, value.remember || false); curConn.login(value.password, value.remember || false);
@ -217,7 +226,7 @@ window.getByName = (name, arg) => {
} catch (e) { } } catch (e) { }
switch (name) { switch (name) {
case 'peers': case 'peers':
return localStorage.getItem('peers'); return localStorage.getItem('peers') || '[]';
break; break;
case 'remote_id': case 'remote_id':
return localStorage.getItem('remote-id') || ''; return localStorage.getItem('remote-id') || '';
@ -247,7 +256,10 @@ window.getByName = (name, arg) => {
case 'peer_option': case 'peer_option':
return curConn.getOption(arg); return curConn.getOption(arg);
break; break;
case 'test_if_valid_server':
break;
} }
return '';
} }
window.init = () => { window.init = () => {

View File

@ -31,7 +31,7 @@ if (app) {
document.body.onload = () => { document.body.onload = () => {
const host = document.querySelector('#host'); const host = document.querySelector('#host');
host.value = localStorage.getItem('host'); host.value = localStorage.getItem('custom-rendezvous-server');
const id = document.querySelector('#id'); const id = document.querySelector('#id');
id.value = localStorage.getItem('id'); id.value = localStorage.getItem('id');
const key = document.querySelector('#key'); const key = document.querySelector('#key');
@ -41,7 +41,7 @@ if (app) {
window.connect = () => { window.connect = () => {
const host = document.querySelector('#host'); const host = document.querySelector('#host');
localStorage.setItem('host', host.value); localStorage.setItem('custom-rendezvous-server', host.value);
const id = document.querySelector('#id'); const id = document.querySelector('#id');
localStorage.setItem('id', id.value); localStorage.setItem('id', id.value);
const key = document.querySelector('#key'); const key = document.querySelector('#key');

View File

@ -11,6 +11,7 @@ export default class Websock {
_status: any; _status: any;
_latency: number; _latency: number;
_secretKey: [Uint8Array, number, number] | undefined; _secretKey: [Uint8Array, number, number] | undefined;
_uri: string;
constructor(uri: string) { constructor(uri: string) {
this._eventHandlers = { this._eventHandlers = {
@ -19,6 +20,7 @@ export default class Websock {
close: () => {}, close: () => {},
error: () => {}, error: () => {},
}; };
this._uri = uri;
this._status = ""; this._status = "";
this._buf = []; this._buf = [];
this._websocket = new WebSocket(uri); this._websocket = new WebSocket(uri);
@ -36,7 +38,9 @@ export default class Websock {
} }
sendMessage(json: any) { 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; let k = this._secretKey;
if (k) { if (k) {
k[1] += 1; k[1] += 1;
@ -98,6 +102,10 @@ export default class Websock {
reject(e); reject(e);
}; };
this._websocket.onerror = (e) => { this._websocket.onerror = (e) => {
if (!this._status) {
reject('Failed to connect to ' + this._uri);
return;
}
this._status = e; this._status = e;
console.error("WebSock.onerror: " + e); console.error("WebSock.onerror: " + e);
this._eventHandlers.error(e); this._eventHandlers.error(e);