Browse Source

feat(webclient): add new query_online function

There may be a loss of performance
Therefore, it is not enabled by default
lejianwen 1 year ago
parent
commit
589a2a5123
4 changed files with 111 additions and 22 deletions
  1. 1 0
      conf/config.yaml
  2. 2 0
      config/rustdesk.go
  3. 5 2
      http/controller/web/index.go
  4. 103 20
      resources/web2/js/dist/index.js

+ 1 - 0
conf/config.yaml

@@ -28,6 +28,7 @@ rustdesk:
28
   key: ""
28
   key: ""
29
   key-file: "./conf/data/id_ed25519.pub"
29
   key-file: "./conf/data/id_ed25519.pub"
30
   personal: 1
30
   personal: 1
31
+  webclient-magic-queryonline: 0
31
 logger:
32
 logger:
32
   path: "./runtime/log.txt"
33
   path: "./runtime/log.txt"
33
   level: "warn" #trace,debug,info,warn,error,fatal
34
   level: "warn" #trace,debug,info,warn,error,fatal

+ 2 - 0
config/rustdesk.go

@@ -11,6 +11,8 @@ type Rustdesk struct {
11
 	Key         string `mapstructure:"key"`
11
 	Key         string `mapstructure:"key"`
12
 	KeyFile     string `mapstructure:"key-file"`
12
 	KeyFile     string `mapstructure:"key-file"`
13
 	Personal    int    `mapstructure:"personal"`
13
 	Personal    int    `mapstructure:"personal"`
14
+	//webclient-magic-queryonline
15
+	WebclientMagicQueryonline int `mapstructure:"webclient-magic-queryonline"`
14
 }
16
 }
15
 
17
 
16
 func LoadKeyFile(rustdesk *Rustdesk) {
18
 func LoadKeyFile(rustdesk *Rustdesk) {

+ 5 - 2
http/controller/web/index.go

@@ -3,6 +3,7 @@ package web
3
 import (
3
 import (
4
 	"Gwen/global"
4
 	"Gwen/global"
5
 	"github.com/gin-gonic/gin"
5
 	"github.com/gin-gonic/gin"
6
+	"strconv"
6
 )
7
 )
7
 
8
 
8
 type Index struct {
9
 type Index struct {
@@ -14,11 +15,13 @@ func (i *Index) Index(c *gin.Context) {
14
 
15
 
15
 func (i *Index) ConfigJs(c *gin.Context) {
16
 func (i *Index) ConfigJs(c *gin.Context) {
16
 	apiServer := global.Config.Rustdesk.ApiServer
17
 	apiServer := global.Config.Rustdesk.ApiServer
17
-
18
+	magicQueryonline := strconv.Itoa(global.Config.Rustdesk.WebclientMagicQueryonline)
18
 	tmp := `
19
 	tmp := `
19
 localStorage.setItem('api-server', "` + apiServer + `")
20
 localStorage.setItem('api-server', "` + apiServer + `")
20
 const ws2_prefix = 'wc-'
21
 const ws2_prefix = 'wc-'
21
 localStorage.setItem(ws2_prefix+'api-server', "` + apiServer + `")
22
 localStorage.setItem(ws2_prefix+'api-server', "` + apiServer + `")
22
-`
23
+
24
+window.webclient_magic_queryonline = ` + magicQueryonline + ``
25
+
23
 	c.String(200, tmp)
26
 	c.String(200, tmp)
24
 }
27
 }

+ 103 - 20
resources/web2/js/dist/index.js

@@ -11112,6 +11112,71 @@ function nt(u, e) {
11112
     ye("callback_query_onlines", {onlines: u.join(","), offlines: e.join(",")})
11112
     ye("callback_query_onlines", {onlines: u.join(","), offlines: e.join(",")})
11113
 }
11113
 }
11114
 
11114
 
11115
+const onlineCache = {}
11116
+
11117
+// Query onlines
11118
+async function myQueryOnline(id) {
11119
+    const last_online = onlineCache[id]
11120
+    if (last_online && new Date().getTime() - last_online < 20 * 1000) {
11121
+        return true
11122
+    }
11123
+    // 映射 方便后期更新
11124
+    const maps = {
11125
+        uri: P4(),
11126
+        ws: j4,
11127
+        conn_type: ne.DEFAULT_CONN,
11128
+        nat_type: pt.SYMMETRIC,
11129
+        token: Pt(),
11130
+        version: se,
11131
+        licence_key: Io(),
11132
+        rendezvousPunchHoleRequest: Ku,
11133
+        rendezvousPunchHoleResponse_Failure: Z0
11134
+    }
11135
+
11136
+    const s = new maps.ws(maps.uri, !0, "rendezvous");
11137
+    await s.open();
11138
+    const punch_hole_request = maps.rendezvousPunchHoleRequest.fromPartial({
11139
+        id: id,
11140
+        licence_key: maps.licence_key,
11141
+        conn_type: maps.conn_type,
11142
+        nat_type: maps.nat_type,
11143
+        token: maps.token,
11144
+        version: maps.version
11145
+    });
11146
+    s.sendRendezvous({punch_hole_request: punch_hole_request});
11147
+    const msg = await s.next();
11148
+    s.close();
11149
+    let online = false
11150
+    const phr = msg.punch_hole_response, rr = msg.relay_response;
11151
+    if (phr) {
11152
+        online = true
11153
+        if (phr != null && phr.other_failure) {
11154
+            online = false
11155
+            return online
11156
+        }
11157
+        if (phr.failure != maps.rendezvousPunchHoleResponse_Failure.UNRECOGNIZED) switch (phr == null ? void 0 : phr.failure) {
11158
+            case maps.rendezvousPunchHoleResponse_Failure.ID_NOT_EXIST:
11159
+            case maps.rendezvousPunchHoleResponse_Failure.OFFLINE:
11160
+            case maps.rendezvousPunchHoleResponse_Failure.LICENSE_MISMATCH:
11161
+            case maps.rendezvousPunchHoleResponse_Failure.LICENSE_OVERUSE:
11162
+                online = false
11163
+                break
11164
+        }
11165
+    } else if (rr) {
11166
+        online = true
11167
+        if (!rr.version) {
11168
+            online = false
11169
+            return online
11170
+        }
11171
+    }
11172
+    if (online) {
11173
+        onlineCache[id] = new Date().getTime()
11174
+    } else if (onlineCache[id]) {
11175
+        delete onlineCache[id]
11176
+    }
11177
+    return online
11178
+}
11179
+
11115
 async function wn(u) {
11180
 async function wn(u) {
11116
     let e = [];
11181
     let e = [];
11117
     try {
11182
     try {
@@ -11121,28 +11186,46 @@ async function wn(u) {
11121
         return
11186
         return
11122
     }
11187
     }
11123
     if (e.length === 0) return;
11188
     if (e.length === 0) return;
11124
-    const i = bn(), o = new j4(i, !0, "query onlines");
11125
-    try {
11126
-        await o.open();
11127
-        const a = Ju.fromPartial({id: N4(), peers: e});
11128
-        o.sendRendezvous({online_request: a})
11129
-    } catch (a) {
11130
-        console.error("Failed to query onlines, ", a), nt([], e), o.close();
11131
-        return
11132
-    }
11133
-    for (let a = 0; a < 2; a++) {
11134
-        const t = await Ro(o, 3e3);
11135
-        if (!t || (t == null ? void 0 : t.key_exchange) || (t == null ? void 0 : t.online_response) === void 0) continue;
11136
-        const s = t.online_response.states;
11137
-        let l = [], E = [];
11138
-        for (let c = 0; c < e.length; c++) {
11139
-            const C = 1 << 7 - c % 8;
11140
-            (s[Math.floor(c / 8)] & C) === C ? l.push(e[c]) : E.push(e[c])
11189
+
11190
+    if (window.webclient_magic_queryonline) {
11191
+        const onlines = []
11192
+        const offlines = []
11193
+        for (let i = 0; i < e.length; i++) {
11194
+            let online = await myQueryOnline(e[i])
11195
+            if (online) {
11196
+                onlines.push(e[i])
11197
+            } else {
11198
+                offlines.push(e[i])
11199
+            }
11141
         }
11200
         }
11142
-        nt(l, E), o.close();
11143
-        return
11201
+        console.log("onlines: ", onlines, "offlines: ", offlines)
11202
+        nt(onlines, offlines)
11203
+    } else {
11204
+        const i = bn(), o = new j4(i, !0, "query onlines");
11205
+        try {
11206
+            await o.open();
11207
+            const a = Ju.fromPartial({id: N4(), peers: e});
11208
+            o.sendRendezvous({online_request: a})
11209
+        } catch (a) {
11210
+            console.error("Failed to query onlines, ", a), nt([], e), o.close();
11211
+            return
11212
+        }
11213
+        for (let a = 0; a < 2; a++) {
11214
+            const t = await Ro(o, 3e3);
11215
+            if (!t || (t == null ? void 0 : t.key_exchange) || (t == null ? void 0 : t.online_response) === void 0) continue;
11216
+            const s = t.online_response.states;
11217
+            let l = [], E = [];
11218
+            for (let c = 0; c < e.length; c++) {
11219
+                const C = 1 << 7 - c % 8;
11220
+                (s[Math.floor(c / 8)] & C) === C ? l.push(e[c]) : E.push(e[c])
11221
+            }
11222
+            nt(l, E), o.close();
11223
+            return
11224
+        }
11225
+        o.close(), console.error("Failed to query online states, no online response")
11144
     }
11226
     }
11145
-    o.close(), console.error("Failed to query online states, no online response")
11227
+
11228
+
11146
 }
11229
 }
11147
 
11230
 
11148
 window.curConn = void 0;
11231
 window.curConn = void 0;