Added listusersessions to meshctrl
Ylian Saint-Hilaire committed
May 14, 2020 at 13:20 UTC
3c0eed1369ccffbc0551f8586f333bc952853982
2 files changed
+24
-5
meshctrl.js
+23
-4
@@ -7,7 +7,7 @@ try { require('ws'); } catch (ex) { console.log('Missing module "ws", type "npm
7
var settings = {};
8
const crypto = require('crypto');
9
const args = require('minimist')(process.argv.slice(2));
10
-const possibleCommands = ['listusers', 'listdevicegroups', 'listdevices', 'listusersofdevicegroup', 'serverinfo', 'userinfo', 'adduser', 'removeuser', 'adddevicegroup', 'removedevicegroup', 'broadcast', 'showevents', 'addusertodevicegroup', 'removeuserfromdevicegroup', 'addusertodevice', 'removeuserfromdevice', 'sendinviteemail', 'generateinvitelink', 'config', 'movetodevicegroup', 'deviceinfo'];
10
+const possibleCommands = ['listusers', 'listusersessions', 'listdevicegroups', 'listdevices', 'listusersofdevicegroup', 'serverinfo', 'userinfo', 'adduser', 'removeuser', 'adddevicegroup', 'removedevicegroup', 'broadcast', 'showevents', 'addusertodevicegroup', 'removeuserfromdevicegroup', 'addusertodevice', 'removeuserfromdevice', 'sendinviteemail', 'generateinvitelink', 'config', 'movetodevicegroup', 'deviceinfo'];
11
if (args.proxy != null) { try { require('https-proxy-agent'); } catch (ex) { console.log('Missing module "https-proxy-agent", type "npm install https-proxy-agent" to install it.'); return; } }
12
13
if (args['_'].length == 0) {
@@ -19,6 +19,7 @@ if (args['_'].length == 0) {
19
console.log(" ServerInfo - Show server information.");
20
console.log(" UserInfo - Show user information.");
21
console.log(" ListUsers - List user accounts.");
22
+ console.log(" ListUsersSessions - List online users.");
23
console.log(" ListDevices - List devices.");
24
console.log(" ListDeviceGroups - List device groups.");
25
console.log(" ListUsersOfDeviceGroup - List the users in a device group.");
@@ -58,6 +59,7 @@ if (args['_'].length == 0) {
59
case 'serverinfo': { ok = true; break; }
60
case 'userinfo': { ok = true; break; }
61
case 'listusers': { ok = true; break; }
62
+ case 'listusersessions': { ok = true; break; }
63
case 'listdevicegroups': { ok = true; break; }
64
case 'listdevices': { ok = true; break; }
65
case 'listusersofdevicegroup': {
@@ -203,6 +205,12 @@ if (args['_'].length == 0) {
205
console.log(" --json - Show result as JSON.");
206
break;
207
}
208
+ case 'listusersessions': {
209
+ console.log("List active user sessions on the MeshCentral server, Example usages:\r\n");
210
+ console.log(" MeshCtrl ListUserSessions");
211
+ console.log(" MeshCtrl ListUserSessions --json");
212
+ break;
213
+ }
214
case 'listdevicegroups': {
215
console.log("List the device groups for this account, Example usages:\r\n");
216
console.log(" MeshCtrl ListDeviceGroups ");
@@ -582,17 +590,19 @@ function serverConnect() {
590
case 'serverinfo': { break; }
591
case 'userinfo': { break; }
592
case 'listusers': { ws.send(JSON.stringify({ action: 'users' })); break; }
593
+ case 'listusersessions': { ws.send(JSON.stringify({ action: 'wssessioncount' })); }
594
case 'listdevicegroups': { ws.send(JSON.stringify({ action: 'meshes' })); break; }
595
case 'listusersofdevicegroup': { ws.send(JSON.stringify({ action: 'meshes' })); break; }
596
case 'listdevices': {
597
if (args.group) {
589
- ws.send(JSON.stringify({ action: 'nodes', meshname: args.group, responseid: 'meshctrl' })); break;
598
+ ws.send(JSON.stringify({ action: 'nodes', meshname: args.group, responseid: 'meshctrl' }));
599
} else if (args.id) {
591
- ws.send(JSON.stringify({ action: 'nodes', meshid: args.id, responseid: 'meshctrl' })); break;
600
+ ws.send(JSON.stringify({ action: 'nodes', meshid: args.id, responseid: 'meshctrl' }));
601
} else {
602
ws.send(JSON.stringify({ action: 'meshes' }));
594
- ws.send(JSON.stringify({ action: 'nodes', responseid: 'meshctrl' })); break;
603
+ ws.send(JSON.stringify({ action: 'nodes', responseid: 'meshctrl' }));
604
}
605
+ break;
606
}
607
case 'adduser': {
608
var siteadmin = 0;
@@ -813,6 +823,15 @@ function serverConnect() {
823
process.exit();
824
}
825
break;
826
+ case 'wssessioncount': { // LIST USER SESSIONS
827
+ if (args.json) {
828
+ console.log(JSON.stringify(data.wssessions, ' ', 2));
829
+ } else {
830
+ for (var i in data.wssessions) { console.log(i + ', ' + ((data.wssessions[i] > 1) ? (data.wssessions[i] + ' sessions.') : ("1 session."))); }
831
+ }
832
+ process.exit();
833
+ break;
834
+ }
835
case 'users': { // LISTUSERS
836
if (args.filter) {
837
// Filter the list of users
meshuser.js
+1
-1
@@ -1387,7 +1387,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
1387
{
1388
// Request a list of all web socket user session count
1389
var wssessions = {};
1390
- if ((user.siteadmin & 2) == 0) break;
1390
+ if ((user.siteadmin & 2) == 0) { try { ws.send(JSON.stringify({ action: 'wssessioncount', wssessions: {}, tag: command.tag })); } catch (ex) { } break; }
1391
if (parent.parent.multiServer == null) {
1392
// No peering, use simple session counting
1393
for (i in parent.wssessions) {