feat(webclient): add new query_online function

There may be a loss of performance
Therefore, it is not enabled by default
This commit is contained in:
lejianwen
2024-12-21 21:15:06 +08:00
parent 184d3d357d
commit 589a2a5123
4 changed files with 111 additions and 22 deletions
+1
View File
@@ -28,6 +28,7 @@ rustdesk:
key: "" key: ""
key-file: "./conf/data/id_ed25519.pub" key-file: "./conf/data/id_ed25519.pub"
personal: 1 personal: 1
webclient-magic-queryonline: 0
logger: logger:
path: "./runtime/log.txt" path: "./runtime/log.txt"
level: "warn" #trace,debug,info,warn,error,fatal level: "warn" #trace,debug,info,warn,error,fatal
+2
View File
@@ -11,6 +11,8 @@ type Rustdesk struct {
Key string `mapstructure:"key"` Key string `mapstructure:"key"`
KeyFile string `mapstructure:"key-file"` KeyFile string `mapstructure:"key-file"`
Personal int `mapstructure:"personal"` Personal int `mapstructure:"personal"`
//webclient-magic-queryonline
WebclientMagicQueryonline int `mapstructure:"webclient-magic-queryonline"`
} }
func LoadKeyFile(rustdesk *Rustdesk) { func LoadKeyFile(rustdesk *Rustdesk) {
+5 -2
View File
@@ -3,6 +3,7 @@ package web
import ( import (
"Gwen/global" "Gwen/global"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"strconv"
) )
type Index struct { type Index struct {
@@ -14,11 +15,13 @@ func (i *Index) Index(c *gin.Context) {
func (i *Index) ConfigJs(c *gin.Context) { func (i *Index) ConfigJs(c *gin.Context) {
apiServer := global.Config.Rustdesk.ApiServer apiServer := global.Config.Rustdesk.ApiServer
magicQueryonline := strconv.Itoa(global.Config.Rustdesk.WebclientMagicQueryonline)
tmp := ` tmp := `
localStorage.setItem('api-server', "` + apiServer + `") localStorage.setItem('api-server', "` + apiServer + `")
const ws2_prefix = 'wc-' const ws2_prefix = 'wc-'
localStorage.setItem(ws2_prefix+'api-server', "` + apiServer + `") localStorage.setItem(ws2_prefix+'api-server', "` + apiServer + `")
`
window.webclient_magic_queryonline = ` + magicQueryonline + ``
c.String(200, tmp) c.String(200, tmp)
} }
+83
View File
@@ -11112,6 +11112,71 @@ function nt(u, e) {
ye("callback_query_onlines", {onlines: u.join(","), offlines: e.join(",")}) 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) { async function wn(u) {
let e = []; let e = [];
try { try {
@@ -11121,6 +11186,21 @@ async function wn(u) {
return return
} }
if (e.length === 0) return; if (e.length === 0) return;
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])
}
}
console.log("onlines: ", onlines, "offlines: ", offlines)
nt(onlines, offlines)
} else {
const i = bn(), o = new j4(i, !0, "query onlines"); const i = bn(), o = new j4(i, !0, "query onlines");
try { try {
await o.open(); await o.open();
@@ -11143,6 +11223,9 @@ async function wn(u) {
return 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; window.curConn = void 0;