Fixed to relay metadata and RTT calculation.
Ylian Saint-Hilaire committed
Apr 14, 2020 at 02:53 UTC
a793685e1f23c17ab2c9cba9afd2711eaa100542
4 files changed
+32
-37
meshrelay.js
+3
-6
@@ -20,7 +20,6 @@ module.exports.CreateMeshRelay = function (parent, ws, req, domain, user, cookie
20
obj.user = user;
21
obj.ruserid = null;
22
obj.req = req; // Used in multi-server.js
23
- obj.metadata = {};
23
24
// Check relay authentication
25
if ((user == null) && (obj.req.query != null) && (obj.req.query.rauth != null)) {
@@ -269,7 +268,7 @@ module.exports.CreateMeshRelay = function (parent, ws, req, domain, user, cookie
268
} else {
269
// Wait for other relay connection
270
ws._socket.pause(); // Hold traffic until the other connection
272
- parent.wsrelays[obj.id] = { peer1: obj, state: 1, metadata: obj.metadata, timeout: setTimeout(function () { closeBothSides(); }, 30000) };
271
+ parent.wsrelays[obj.id] = { peer1: obj, state: 1, timeout: setTimeout(function () { closeBothSides(); }, 30000) };
272
parent.parent.debug('relay', 'Relay holding: ' + obj.id + ' (' + cleanRemoteAddr(obj.req.ip) + ') ' + (obj.authenticated ? 'Authenticated' : ''));
273
274
// Check if a peer server has this connection
@@ -421,8 +420,7 @@ module.exports.CreateMeshRelay = function (parent, ws, req, domain, user, cookie
420
421
// Check if this user has permission to manage this computer
422
if ((parent.GetNodeRights(user, node.meshid, node._id) & MESHRIGHT_REMOTECONTROL) == 0) { console.log('ERR: Access denied (1)'); try { obj.close(); } catch (e) { } return; }
424
- obj.metadata.peer2 = { name: node.name };
425
- obj.metadata.authUser = user;
423
+
424
// Send connection request to agent
425
const rcookie = parent.parent.encodeCookie({ ruserid: user._id }, parent.parent.loginCookieEncryptionKey);
426
if (obj.id == undefined) { obj.id = ('' + Math.random()).substring(2); } // If there is no connection id, generate one.
@@ -440,8 +438,7 @@ module.exports.CreateMeshRelay = function (parent, ws, req, domain, user, cookie
438
439
// Check if this user has permission to manage this computer
440
if ((parent.GetNodeRights(user, node.meshid, node._id) & MESHRIGHT_REMOTECONTROL) == 0) { console.log('ERR: Access denied (2)'); try { obj.close(); } catch (e) { } return; }
443
- obj.metadata.peer2 = { name: node.name };
444
- obj.metadata.authUser = user;
441
+
442
// Send connection request to agent
443
if (obj.id == null) { obj.id = ('' + Math.random()).substring(2); } // If there is no connection id, generate one.
444
const rcookie = parent.parent.encodeCookie({ ruserid: user._id }, parent.parent.loginCookieEncryptionKey);
meshuser.js
+9
-4
@@ -1041,10 +1041,15 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
1041
}
1042
case 'relays': {
1043
for (var i in parent.wsrelays) {
1044
- r += 'id: ' + i + ', state: ' + parent.wsrelays[i].state;
1045
- if (parent.wsrelays[i].peer1 != null) { r += ', peer1: ' + cleanRemoteAddr(parent.wsrelays[i].peer1.req.ip); }
1046
- if (parent.wsrelays[i].peer2 != null) { r += ', peer2: ' + cleanRemoteAddr(parent.wsrelays[i].peer2.req.ip); }
1047
- if (parent.wsrelays[i].metadata != null) { r += ', ' + parent.wsrelays[i].metadata.authUser._id + ' connected to ' + parent.wsrelays[i].metadata.peer2.name; }
1044
+ r += 'id: ' + i + ', ' + ((parent.wsrelays[i].state == 2)?'connected':'pending');
1045
+ if (parent.wsrelays[i].peer1 != null) {
1046
+ r += ', ' + cleanRemoteAddr(parent.wsrelays[i].peer1.req.ip);
1047
+ if (parent.wsrelays[i].peer1.user) { r += ' (User:' + parent.wsrelays[i].peer1.user.name + ')' }
1048
+ }
1049
+ if (parent.wsrelays[i].peer2 != null) {
1050
+ r += ' to ' + cleanRemoteAddr(parent.wsrelays[i].peer2.req.ip);
1051
+ if (parent.wsrelays[i].peer2.user) { r += ' (User:' + parent.wsrelays[i].peer2.user.name + ')' }
1052
+ }
1053
r += '\r\n';
1054
}
1055
if (r == '') { r = 'No relays.'; }
public/scripts/agent-redir-ws-0.1.1.js
+13
-22
@@ -28,7 +28,7 @@ var CreateAgentRedirect = function (meshserver, module, serverPublicNamePort, au
28
obj.webrtc = null;
29
obj.debugmode = 0;
30
obj.serverIsRecording = false;
31
- obj.latency = { timer: null, lastSend: 0, current: 0, send: false, callbacks: [] };
31
+ obj.latency = { lastSend: null, current: -1, callback: null };
32
if (domainUrl == null) { domainUrl = '/'; }
33
34
// Console Message
@@ -74,9 +74,9 @@ var CreateAgentRedirect = function (meshserver, module, serverPublicNamePort, au
74
if (controlMsg.type == 'console') {
75
obj.consoleMessage = controlMsg.msg;
76
if (obj.onConsoleMessageChange) { obj.onConsoleMessageChange(obj, obj.consoleMessage); }
77
- } else if (controlMsg.type = 'latency') {
77
+ } else if ((controlMsg.type = 'latency') && (typeof controlMsg.time == 'number')) {
78
obj.latency.current = (new Date().getTime()) - controlMsg.time;
79
- obj.latency.onUpdate();
79
+ if (obj.latency.callbacks != null) { obj.latency.callback(obj.latency.current); }
80
} else if (obj.webrtc != null) {
81
if (controlMsg.type == 'answer') {
82
obj.webrtc.setRemoteDescription(new RTCSessionDescription(controlMsg), function () { /*console.log('WebRTC remote ok');*/ }, obj.xxCloseWebRTC);
@@ -95,24 +95,14 @@ var CreateAgentRedirect = function (meshserver, module, serverPublicNamePort, au
95
96
function performWebRtcSwitch() {
97
if ((obj.webSwitchOk == true) && (obj.webRtcActive == true)) {
98
+ obj.latency.current = -1;
99
obj.sendCtrlMsg('{"ctrlChannel":"102938","type":"webrtc0"}'); // Indicate to the meshagent that it can start traffic switchover
100
obj.sendCtrlMsg('{"ctrlChannel":"102938","type":"webrtc1"}'); // Indicate to the meshagent that data traffic will no longer be sent over websocket.
101
// TODO: Hold/Stop sending data over websocket
102
if (obj.onStateChanged != null) { obj.onStateChanged(obj, obj.State); }
103
}
104
}
104
-
105
- obj.latencyTimer = function() {
106
- obj.latency.send = true;
107
- }
108
-
109
- obj.latency.onUpdate = function(func) {
110
- if (func != null) { obj.latency.callbacks.push(func); return; }
111
- if (obj.latency.callbacks.length > 0) {
112
- for (var x in obj.latency.callbacks) obj.latency.callbacks[x](obj.latency.current);
113
- }
114
- };
115
-
105
+
106
obj.xxOnMessage = function (e) {
107
//console.log('Recv', e.data, e.data.byteLength, obj.State);
108
if (obj.State < 3) {
@@ -153,6 +143,7 @@ var CreateAgentRedirect = function (meshserver, module, serverPublicNamePort, au
143
}, obj.xxCloseWebRTC, { mandatory: { OfferToReceiveAudio: false, OfferToReceiveVideo: false } });
144
}
145
}
146
+
147
return;
148
}
149
}
@@ -164,11 +155,6 @@ var CreateAgentRedirect = function (meshserver, module, serverPublicNamePort, au
155
}
156
157
if (typeof e.data == 'object') {
167
- if (obj.latency.timer == null) {
168
- obj.latency.timer = setInterval(obj.latencyTimer, 3000);
169
- }
170
- if (obj.latency.send) { obj.latency.send = false; obj.sendCtrlMsg('{"ctrlChannel":"102938","type":"latency","time":'+ new Date().getTime() +'}'); }
171
-
158
if (fileReaderInuse == true) { fileReaderAcc.push(e.data); return; }
159
if (fileReader.readAsBinaryString && (obj.m.ProcessBinaryData == null)) {
160
// Chrome & Firefox (Draft)
@@ -188,6 +174,12 @@ var CreateAgentRedirect = function (meshserver, module, serverPublicNamePort, au
174
// If we get a string object, it maybe the WebRTC confirm. Ignore it.
175
obj.xxOnSocketData(e.data);
176
}
177
+
178
+ // Request RTT mesure, don't use this if WebRTC is active
179
+ if (obj.webRtcActive != true) {
180
+ var ticks = new Date().getTime();
181
+ if ((obj.latency.lastSend == null) || ((ticks - obj.latency.lastSend) > 5000)) { obj.latency.lastSend = ticks; obj.sendCtrlMsg('{"ctrlChannel":"102938","type":"latency","time":' + ticks + '}'); }
182
+ }
183
};
184
185
// Setup the file reader
@@ -268,8 +260,7 @@ var CreateAgentRedirect = function (meshserver, module, serverPublicNamePort, au
260
261
obj.Stop = function (x) {
262
if (obj.debugmode == 1) { console.log('stop', x); }
271
- obj.latency.current = 0;
272
- obj.latency.onUpdate();
263
+
264
// Clean up WebRTC
265
obj.xxCloseWebRTC();
266
views/default.handlebars
+7
-5
@@ -515,7 +515,6 @@
515
<span id=connectbutton1hspan> <input type=button id=connectbutton1h value="HW Connect" title="Connect using Intel AMT hardware KVM" onclick=connectDesktop(event,2) onkeypress="return false" onkeydown="return false" disabled="disabled" /></span>
516
<span id=disconnectbutton1span> <input type=button id=disconnectbutton1 value="Disconnect" onclick=connectDesktop(event,0) onkeypress="return false" onkeydown="return false" /></span>
517
<span id="deskstatus">Disconnected</span>
518
- <span id="connectLatency">(Ping: <span id="connectLatencyTime">0</span>ms)</span>
518
</div>
519
</div>
520
<div id=deskarea2 style="">
@@ -554,6 +553,7 @@
553
</div>
554
<div id=deskarea4 class="areaFoot">
555
<div class="toright2">
556
+ <span id="DeskLatency" title="Desktop Session Latency"></span>
557
<span id="DeskTimer" title="Session time"></span>
558
<select id=termdisplays style="display:none" onchange=deskSetDisplay(event) onkeypress="return false" onkeydown="return false"></select>
559
<input id=DeskToolsButton type=button value=Tools title="Toggle tools view" onkeypress="return false" onkeydown="return false" onclick="toggleDeskTools()" />
@@ -5861,7 +5861,7 @@
5861
desktop.m.onDisplayinfo = deskDisplayInfo;
5862
desktop.m.onScreenSizeChange = deskAdjust;
5863
desktop.Start(desktopNode._id);
5864
- desktop.latency.onUpdate(function(ms) { QH('connectLatencyTime', ms); });
5864
+ desktop.latency.callback = function(ms) { console.log('latency', ms); updateSessionTime(); };
5865
desktop.contype = 1;
5866
} else if (contype == 3) {
5867
// Ask for user sessions
@@ -5941,10 +5941,11 @@
5941
5942
function updateSessionTime() {
5943
// Desktop
5944
- var seconds = 0;
5944
+ var latencyStr = '', seconds = 0;
5945
if (desktop && desktop.startTime) {
5946
+ if (desktop.latency && (desktop.latency.current >= 0)) { latencyStr = format('{0} ms, ', desktop.latency.current); }
5947
seconds = Math.floor((new Date() - desktop.startTime) / 1000);
5947
- QH('DeskTimer', zeroPad(Math.floor(seconds / 3600), 2) + ':' + zeroPad((Math.floor(seconds / 60) % 60), 2) + ':' + zeroPad((seconds % 60), 2));
5948
+ QH('DeskTimer', latencyStr + zeroPad(Math.floor(seconds / 3600), 2) + ':' + zeroPad((Math.floor(seconds / 60) % 60), 2) + ':' + zeroPad((seconds % 60), 2));
5949
} else {
5950
QH('DeskTimer', '');
5951
}
@@ -5952,8 +5953,9 @@
5953
// Terminal
5954
seconds = 0;
5955
if (terminal && terminal.startTime) {
5956
+ if (terminal.latency && (terminal.latency.current >= 0)) { latencyStr = format('{0} ms, ', terminal.latency.current); }
5957
seconds = Math.floor((new Date() - terminal.startTime) / 1000);
5956
- QH('TermTimer', zeroPad(Math.floor(seconds / 3600), 2) + ':' + zeroPad((Math.floor(seconds / 60) % 60), 2) + ':' + zeroPad((seconds % 60), 2));
5958
+ QH('TermTimer', latencyStr + zeroPad(Math.floor(seconds / 3600), 2) + ':' + zeroPad((Math.floor(seconds / 60) % 60), 2) + ':' + zeroPad((seconds % 60), 2));
5959
} else {
5960
QH('TermTimer', '');
5961
}