Added guest share name in session info, #2812
Ylian Saint-Hilaire committed
Jun 25, 2021 at 11:49 UTC
63d199be20afd01cb80f5ca27a37c310c8a055b6
2 files changed
+48
-17
agents/meshcore.js
+42
-13
@@ -871,7 +871,7 @@ function handleServerCommand(data) {
871
woptions.checkServerIdentity.servertlshash = data.servertlshash;
872
873
//sendConsoleText(JSON.stringify(woptions));
874
- //sendConsoleText('TUNNEL: ' + JSON.stringify(data));
874
+ sendConsoleText('TUNNEL: ' + JSON.stringify(data));
875
var tunnel = http.request(woptions);
876
tunnel.upgrade = onTunnelUpgrade;
877
tunnel.on('error', function (e) { sendConsoleText("ERROR: Unable to connect relay tunnel to: " + this.url + ", " + JSON.stringify(e)); });
@@ -882,6 +882,7 @@ function handleServerCommand(data) {
882
tunnel.privacybartext = data.privacybartext ? data.privacybartext : "Sharing desktop with: {0}";
883
tunnel.username = data.username + (data.guestname ? (' - ' + data.guestname) : '');
884
tunnel.realname = (data.realname ? data.realname : data.username) + (data.guestname ? (' - ' + data.guestname) : '');
885
+ tunnel.guestname = data.guestname;
886
tunnel.userid = data.userid;
887
tunnel.remoteaddr = data.remoteaddr;
888
tunnel.state = 0;
@@ -1574,7 +1575,8 @@ function onTunnelUpgrade(response, s, head) {
1575
1576
// Add the TCP session to the count and update the server
1577
if (s.httprequest.userid != null) {
1577
- if (tunnelUserCount.tcp[s.httprequest.userid] == null) { tunnelUserCount.tcp[s.httprequest.userid] = 1; } else { tunnelUserCount.tcp[s.httprequest.userid]++; }
1578
+ var userid = getUserIdAndGuestNameFromHttpRequest(s.httprequest);
1579
+ if (tunnelUserCount.tcp[userid] == null) { tunnelUserCount.tcp[userid] = 1; } else { tunnelUserCount.tcp[userid]++; }
1580
try { mesh.SendCommand({ action: 'sessions', type: 'tcp', value: tunnelUserCount.tcp }); } catch (e) { }
1581
broadcastSessionsToRegisteredApps();
1582
}
@@ -1591,7 +1593,8 @@ function onTunnelUpgrade(response, s, head) {
1593
1594
// Add the UDP session to the count and update the server
1595
if (s.httprequest.userid != null) {
1594
- if (tunnelUserCount.udp[s.httprequest.userid] == null) { tunnelUserCount.udp[s.httprequest.userid] = 1; } else { tunnelUserCount.udp[s.httprequest.userid]++; }
1596
+ var userid = getUserIdAndGuestNameFromHttpRequest(s.httprequest);
1597
+ if (tunnelUserCount.udp[userid] == null) { tunnelUserCount.udp[userid] = 1; } else { tunnelUserCount.udp[userid]++; }
1598
try { mesh.SendCommand({ action: 'sessions', type: 'udp', value: tunnelUserCount.tcp }); } catch (e) { }
1599
broadcastSessionsToRegisteredApps();
1600
}
@@ -1601,6 +1604,12 @@ function onTunnelUpgrade(response, s, head) {
1604
}
1605
}
1606
1607
+// If the HTTP Request has a guest name, we need to form a userid that includes the guest name in hex.
1608
+// This is so we can tell the server that a session is for a given userid/guest sharing pair.
1609
+function getUserIdAndGuestNameFromHttpRequest(request) {
1610
+ if (request.guestname == null) return request.userid; else return request.userid + '/guest:' + Buffer.from(request.guestname).toString('base64');
1611
+}
1612
+
1613
// Called when UDP relay data is received // TODO****
1614
function onUdpRelayTargetTunnelConnect(data) {
1615
var peerTunnel = tunnels[this.peerindex];
@@ -1639,11 +1648,13 @@ function onTunnelClosed() {
1648
// If this is a routing session, clean up and send the new session counts.
1649
if (this.httprequest.userid != null) {
1650
if (this.httprequest.tcpport != null) {
1642
- if (tunnelUserCount.tcp[this.httprequest.userid] != null) { tunnelUserCount.tcp[this.httprequest.userid]--; if (tunnelUserCount.tcp[this.httprequest.userid] <= 0) { delete tunnelUserCount.tcp[this.httprequest.userid]; } }
1651
+ var userid = getUserIdAndGuestNameFromHttpRequest(s.httprequest);
1652
+ if (tunnelUserCount.tcp[userid] != null) { tunnelUserCount.tcp[userid]--; if (tunnelUserCount.tcp[userid] <= 0) { delete tunnelUserCount.tcp[userid]; } }
1653
try { mesh.SendCommand({ action: 'sessions', type: 'tcp', value: tunnelUserCount.tcp }); } catch (e) { }
1654
broadcastSessionsToRegisteredApps();
1655
} else if (this.httprequest.udpport != null) {
1646
- if (tunnelUserCount.udp[this.httprequest.userid] != null) { tunnelUserCount.udp[this.httprequest.userid]--; if (tunnelUserCount.udp[this.httprequest.userid] <= 0) { delete tunnelUserCount.udp[this.httprequest.userid]; } }
1656
+ var userid = getUserIdAndGuestNameFromHttpRequest(s.httprequest);
1657
+ if (tunnelUserCount.udp[userid] != null) { tunnelUserCount.udp[userid]--; if (tunnelUserCount.udp[userid] <= 0) { delete tunnelUserCount.udp[userid]; } }
1658
try { mesh.SendCommand({ action: 'sessions', type: 'udp', value: tunnelUserCount.udp }); } catch (e) { }
1659
broadcastSessionsToRegisteredApps();
1660
}
@@ -1786,7 +1797,8 @@ function onTunnelData(data) {
1797
1798
// Remove the terminal session to the count to update the server
1799
if (this.httprequest.userid != null) {
1789
- if (tunnelUserCount.terminal[this.httprequest.userid] != null) { tunnelUserCount.terminal[this.httprequest.userid]--; if (tunnelUserCount.terminal[this.httprequest.userid] <= 0) { delete tunnelUserCount.terminal[this.httprequest.userid]; } }
1800
+ var userid = getUserIdAndGuestNameFromHttpRequest(this.httprequest);
1801
+ if (tunnelUserCount.terminal[userid] != null) { tunnelUserCount.terminal[userid]--; if (tunnelUserCount.terminal[userid] <= 0) { delete tunnelUserCount.terminal[userid]; } }
1802
try { mesh.SendCommand({ action: 'sessions', type: 'terminal', value: tunnelUserCount.terminal }); } catch (e) { }
1803
broadcastSessionsToRegisteredApps();
1804
}
@@ -1977,7 +1989,8 @@ function onTunnelData(data) {
1989
1990
// Add the terminal session to the count to update the server
1991
if (this.ws.httprequest.userid != null) {
1980
- if (tunnelUserCount.terminal[this.ws.httprequest.userid] == null) { tunnelUserCount.terminal[this.ws.httprequest.userid] = 1; } else { tunnelUserCount.terminal[this.ws.httprequest.userid]++; }
1992
+ var userid = getUserIdAndGuestNameFromHttpRequest(this.ws.httprequest);
1993
+ if (tunnelUserCount.terminal[userid] == null) { tunnelUserCount.terminal[userid] = 1; } else { tunnelUserCount.terminal[userid]++; }
1994
try { mesh.SendCommand({ action: 'sessions', type: 'terminal', value: tunnelUserCount.terminal }); } catch (e) { }
1995
broadcastSessionsToRegisteredApps();
1996
}
@@ -2038,8 +2051,15 @@ function onTunnelData(data) {
2051
// Send a metadata update to all desktop sessions
2052
var users = {};
2053
if (this.httprequest.desktop.kvm.tunnels != null) {
2041
- for (var i in this.httprequest.desktop.kvm.tunnels) { try { var userid = this.httprequest.desktop.kvm.tunnels[i].httprequest.userid; if (users[userid] == null) { users[userid] = 1; } else { users[userid]++; } } catch (e) { } }
2042
- for (var i in this.httprequest.desktop.kvm.tunnels) { try { this.httprequest.desktop.kvm.tunnels[i].write(JSON.stringify({ ctrlChannel: '102938', type: 'metadata', users: users })); } catch (e) { } }
2054
+ for (var i in this.httprequest.desktop.kvm.tunnels) {
2055
+ try {
2056
+ var userid = getUserIdAndGuestNameFromHttpRequest(this.httprequest.desktop.kvm.tunnels[i].httprequest);
2057
+ if (users[userid] == null) { users[userid] = 1; } else { users[userid]++; }
2058
+ } catch (ex) { sendConsoleText(ex); }
2059
+ }
2060
+ for (var i in this.httprequest.desktop.kvm.tunnels) {
2061
+ try { this.httprequest.desktop.kvm.tunnels[i].write(JSON.stringify({ ctrlChannel: '102938', type: 'metadata', users: users })); } catch (e) { }
2062
+ }
2063
tunnelUserCount.desktop = users;
2064
try { mesh.SendCommand({ action: 'sessions', type: 'kvm', value: users }); } catch (e) { }
2065
broadcastSessionsToRegisteredApps();
@@ -2055,8 +2075,15 @@ function onTunnelData(data) {
2075
// Send a metadata update to all desktop sessions
2076
var users = {};
2077
if (this.httprequest.desktop.kvm.tunnels != null) {
2058
- for (var i in this.httprequest.desktop.kvm.tunnels) { try { var userid = this.httprequest.desktop.kvm.tunnels[i].httprequest.userid; if (users[userid] == null) { users[userid] = 1; } else { users[userid]++; } } catch (e) { } }
2059
- for (var i in this.httprequest.desktop.kvm.tunnels) { try { this.httprequest.desktop.kvm.tunnels[i].write(JSON.stringify({ ctrlChannel: '102938', type: 'metadata', users: users })); } catch (e) { } }
2078
+ for (var i in this.httprequest.desktop.kvm.tunnels) {
2079
+ try {
2080
+ var userid = getUserIdAndGuestNameFromHttpRequest(this.httprequest.desktop.kvm.tunnels[i].httprequest);
2081
+ if (users[userid] == null) { users[userid] = 1; } else { users[userid]++; }
2082
+ } catch (ex) { sendConsoleText(ex); }
2083
+ }
2084
+ for (var i in this.httprequest.desktop.kvm.tunnels) {
2085
+ try { this.httprequest.desktop.kvm.tunnels[i].write(JSON.stringify({ ctrlChannel: '102938', type: 'metadata', users: users })); } catch (e) { }
2086
+ }
2087
tunnelUserCount.desktop = users;
2088
try { mesh.SendCommand({ action: 'sessions', type: 'kvm', value: users }); } catch (e) { }
2089
broadcastSessionsToRegisteredApps();
@@ -2261,7 +2288,8 @@ function onTunnelData(data) {
2288
2289
// Add the files session to the count to update the server
2290
if (this.httprequest.userid != null) {
2264
- if (tunnelUserCount.files[this.httprequest.userid] == null) { tunnelUserCount.files[this.httprequest.userid] = 1; } else { tunnelUserCount.files[this.httprequest.userid]++; }
2291
+ var userid = getUserIdAndGuestNameFromHttpRequest(this.httprequest);
2292
+ if (tunnelUserCount.files[userid] == null) { tunnelUserCount.files[userid] = 1; } else { tunnelUserCount.files[userid]++; }
2293
try { mesh.SendCommand({ action: 'sessions', type: 'files', value: tunnelUserCount.files }); } catch (e) { }
2294
broadcastSessionsToRegisteredApps();
2295
}
@@ -2269,7 +2297,8 @@ function onTunnelData(data) {
2297
this.end = function () {
2298
// Remove the files session from the count to update the server
2299
if (this.httprequest.userid != null) {
2272
- if (tunnelUserCount.files[this.httprequest.userid] != null) { tunnelUserCount.files[this.httprequest.userid]--; if (tunnelUserCount.files[this.httprequest.userid] <= 0) { delete tunnelUserCount.files[this.httprequest.userid]; } }
2300
+ var userid = getUserIdAndGuestNameFromHttpRequest(this.httprequest);
2301
+ if (tunnelUserCount.files[userid] != null) { tunnelUserCount.files[userid]--; if (tunnelUserCount.files[userid] <= 0) { delete tunnelUserCount.files[userid]; } }
2302
try { mesh.SendCommand({ action: 'sessions', type: 'files', value: tunnelUserCount.files }); } catch (e) { }
2303
broadcastSessionsToRegisteredApps();
2304
}
views/default.handlebars
+6
-4
@@ -7884,9 +7884,7 @@
7884
if (conn.metadata.users) {
7885
for (var i in conn.metadata.users) {
7886
var val = (conn.metadata.users[i] == 1)?"1 connection":format("{0} connections", conn.metadata.users[i]);
7887
- var username = i.split('/')[2];
7888
- if ((users != null) && (users[i] != null)) { username = users[i].name; }
7889
- x += addHtmlValue4(format("User \"{0}\"", username), val);
7887
+ x += addHtmlValue4(getUserName(i), val);
7888
}
7889
}
7890
setDialogMode(2, "Session Information", 1, null, x, 'sessionMetadata' + cid);
@@ -15692,7 +15690,11 @@
15690
function nobreak(x) { return x.split(' ').join(' '); }
15691
function pad2(num) { var s = '00' + num; return s.substr(s.length - 2); }
15692
function encodeURIComponentEx(txt) { return encodeURIComponent(txt).replace(/'/g,'%27'); };
15695
- function getUserName(userid) { if (users && users[userid] != null) return users[userid].name; return userid.split('/')[2]; }
15693
+ function getUserName(userid) {
15694
+ var useridsplit = userid.split('/'), userid2 = userid[0] + '/' + userid[1] + '/' + userid[2], guestname = '';
15695
+ if ((useridsplit.length == 4) && (useridsplit[3].startsWith('guest:'))) { guestname = ' - ' + atob(useridsplit[3].substring(6)); }
15696
+ if (users && users[userid2] != null) return (users[userid2].name + guestname); return (useridsplit[2] + guestname);
15697
+ }
15698
function round(value, precision) { var multiplier = Math.pow(10, precision || 0); return Math.round(value * multiplier) / multiplier; }
15699
function safeNewWindow(url, target) { var newWindow = window.open(url, target, 'noopener,noreferrer'); if (newWindow) { newWindow.opener = null; } }
15700