From 589a2a5123a4ae6675c357bbed53b9a8e1438b1d Mon Sep 17 00:00:00 2001 From: lejianwen <84855512@qq.com> Date: Sat, 21 Dec 2024 21:15:06 +0800 Subject: [PATCH] feat(webclient): add new query_online function There may be a loss of performance Therefore, it is not enabled by default --- conf/config.yaml | 1 + config/rustdesk.go | 2 + http/controller/web/index.go | 7 +- resources/web2/js/dist/index.js | 123 ++++++++++++++++++++++++++------ 4 files changed, 111 insertions(+), 22 deletions(-) diff --git a/conf/config.yaml b/conf/config.yaml index ecef3ef..2befe9f 100644 --- a/conf/config.yaml +++ b/conf/config.yaml @@ -28,6 +28,7 @@ rustdesk: key: "" key-file: "./conf/data/id_ed25519.pub" personal: 1 + webclient-magic-queryonline: 0 logger: path: "./runtime/log.txt" level: "warn" #trace,debug,info,warn,error,fatal diff --git a/config/rustdesk.go b/config/rustdesk.go index 59056d4..f1903d1 100644 --- a/config/rustdesk.go +++ b/config/rustdesk.go @@ -11,6 +11,8 @@ type Rustdesk struct { Key string `mapstructure:"key"` KeyFile string `mapstructure:"key-file"` Personal int `mapstructure:"personal"` + //webclient-magic-queryonline + WebclientMagicQueryonline int `mapstructure:"webclient-magic-queryonline"` } func LoadKeyFile(rustdesk *Rustdesk) { diff --git a/http/controller/web/index.go b/http/controller/web/index.go index 8b6b326..af3b1b6 100644 --- a/http/controller/web/index.go +++ b/http/controller/web/index.go @@ -3,6 +3,7 @@ package web import ( "Gwen/global" "github.com/gin-gonic/gin" + "strconv" ) type Index struct { @@ -14,11 +15,13 @@ func (i *Index) Index(c *gin.Context) { func (i *Index) ConfigJs(c *gin.Context) { apiServer := global.Config.Rustdesk.ApiServer - + magicQueryonline := strconv.Itoa(global.Config.Rustdesk.WebclientMagicQueryonline) tmp := ` localStorage.setItem('api-server', "` + apiServer + `") const ws2_prefix = 'wc-' localStorage.setItem(ws2_prefix+'api-server', "` + apiServer + `") -` + +window.webclient_magic_queryonline = ` + magicQueryonline + `` + c.String(200, tmp) } diff --git a/resources/web2/js/dist/index.js b/resources/web2/js/dist/index.js index fc4b8b4..35ec81f 100644 --- a/resources/web2/js/dist/index.js +++ b/resources/web2/js/dist/index.js @@ -11112,6 +11112,71 @@ function nt(u, e) { ye("callback_query_onlines", {onlines: u.join(","), offlines: e.join(",")}) } +const onlineCache = {} + +// Query onlines +async function myQueryOnline(id) { + const last_online = onlineCache[id] + if (last_online && new Date().getTime() - last_online < 20 * 1000) { + return true + } + // 映射 方便后期更新 + const maps = { + uri: P4(), + ws: j4, + conn_type: ne.DEFAULT_CONN, + nat_type: pt.SYMMETRIC, + token: Pt(), + version: se, + licence_key: Io(), + rendezvousPunchHoleRequest: Ku, + rendezvousPunchHoleResponse_Failure: Z0 + } + + const s = new maps.ws(maps.uri, !0, "rendezvous"); + await s.open(); + const punch_hole_request = maps.rendezvousPunchHoleRequest.fromPartial({ + id: id, + licence_key: maps.licence_key, + conn_type: maps.conn_type, + nat_type: maps.nat_type, + token: maps.token, + version: maps.version + }); + s.sendRendezvous({punch_hole_request: punch_hole_request}); + const msg = await s.next(); + s.close(); + let online = false + const phr = msg.punch_hole_response, rr = msg.relay_response; + if (phr) { + online = true + if (phr != null && phr.other_failure) { + online = false + return online + } + if (phr.failure != maps.rendezvousPunchHoleResponse_Failure.UNRECOGNIZED) switch (phr == null ? void 0 : phr.failure) { + case maps.rendezvousPunchHoleResponse_Failure.ID_NOT_EXIST: + case maps.rendezvousPunchHoleResponse_Failure.OFFLINE: + case maps.rendezvousPunchHoleResponse_Failure.LICENSE_MISMATCH: + case maps.rendezvousPunchHoleResponse_Failure.LICENSE_OVERUSE: + online = false + break + } + } else if (rr) { + online = true + if (!rr.version) { + online = false + return online + } + } + if (online) { + onlineCache[id] = new Date().getTime() + } else if (onlineCache[id]) { + delete onlineCache[id] + } + return online +} + async function wn(u) { let e = []; try { @@ -11121,28 +11186,46 @@ async function wn(u) { return } if (e.length === 0) return; - const i = bn(), o = new j4(i, !0, "query onlines"); - try { - await o.open(); - const a = Ju.fromPartial({id: N4(), peers: e}); - o.sendRendezvous({online_request: a}) - } catch (a) { - console.error("Failed to query onlines, ", a), nt([], e), o.close(); - return - } - for (let a = 0; a < 2; a++) { - const t = await Ro(o, 3e3); - if (!t || (t == null ? void 0 : t.key_exchange) || (t == null ? void 0 : t.online_response) === void 0) continue; - const s = t.online_response.states; - let l = [], E = []; - for (let c = 0; c < e.length; c++) { - const C = 1 << 7 - c % 8; - (s[Math.floor(c / 8)] & C) === C ? l.push(e[c]) : E.push(e[c]) + + if (window.webclient_magic_queryonline) { + const onlines = [] + const offlines = [] + for (let i = 0; i < e.length; i++) { + let online = await myQueryOnline(e[i]) + if (online) { + onlines.push(e[i]) + } else { + offlines.push(e[i]) + } } - nt(l, E), o.close(); - return + console.log("onlines: ", onlines, "offlines: ", offlines) + nt(onlines, offlines) + } else { + const i = bn(), o = new j4(i, !0, "query onlines"); + try { + await o.open(); + const a = Ju.fromPartial({id: N4(), peers: e}); + o.sendRendezvous({online_request: a}) + } catch (a) { + console.error("Failed to query onlines, ", a), nt([], e), o.close(); + return + } + for (let a = 0; a < 2; a++) { + const t = await Ro(o, 3e3); + if (!t || (t == null ? void 0 : t.key_exchange) || (t == null ? void 0 : t.online_response) === void 0) continue; + const s = t.online_response.states; + let l = [], E = []; + for (let c = 0; c < e.length; c++) { + const C = 1 << 7 - c % 8; + (s[Math.floor(c / 8)] & C) === C ? l.push(e[c]) : E.push(e[c]) + } + nt(l, E), o.close(); + return + } + o.close(), console.error("Failed to query online states, no online response") } - o.close(), console.error("Failed to query online states, no online response") + + } window.curConn = void 0;