Added listusersessions to meshctrl

This commit is contained in:
Ylian Saint-Hilaire 2020-05-14 13:20:45 -07:00
parent a1a6e0c434
commit 957caf15c2
2 changed files with 24 additions and 5 deletions

View File

@ -7,7 +7,7 @@ try { require('ws'); } catch (ex) { console.log('Missing module "ws", type "npm
var settings = {}; var settings = {};
const crypto = require('crypto'); const crypto = require('crypto');
const args = require('minimist')(process.argv.slice(2)); const args = require('minimist')(process.argv.slice(2));
const possibleCommands = ['listusers', 'listdevicegroups', 'listdevices', 'listusersofdevicegroup', 'serverinfo', 'userinfo', 'adduser', 'removeuser', 'adddevicegroup', 'removedevicegroup', 'broadcast', 'showevents', 'addusertodevicegroup', 'removeuserfromdevicegroup', 'addusertodevice', 'removeuserfromdevice', 'sendinviteemail', 'generateinvitelink', 'config', 'movetodevicegroup', 'deviceinfo']; const possibleCommands = ['listusers', 'listusersessions', 'listdevicegroups', 'listdevices', 'listusersofdevicegroup', 'serverinfo', 'userinfo', 'adduser', 'removeuser', 'adddevicegroup', 'removedevicegroup', 'broadcast', 'showevents', 'addusertodevicegroup', 'removeuserfromdevicegroup', 'addusertodevice', 'removeuserfromdevice', 'sendinviteemail', 'generateinvitelink', 'config', 'movetodevicegroup', 'deviceinfo'];
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; } } 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; } }
if (args['_'].length == 0) { if (args['_'].length == 0) {
@ -19,6 +19,7 @@ if (args['_'].length == 0) {
console.log(" ServerInfo - Show server information."); console.log(" ServerInfo - Show server information.");
console.log(" UserInfo - Show user information."); console.log(" UserInfo - Show user information.");
console.log(" ListUsers - List user accounts."); console.log(" ListUsers - List user accounts.");
console.log(" ListUsersSessions - List online users.");
console.log(" ListDevices - List devices."); console.log(" ListDevices - List devices.");
console.log(" ListDeviceGroups - List device groups."); console.log(" ListDeviceGroups - List device groups.");
console.log(" ListUsersOfDeviceGroup - List the users in a device group."); console.log(" ListUsersOfDeviceGroup - List the users in a device group.");
@ -58,6 +59,7 @@ if (args['_'].length == 0) {
case 'serverinfo': { ok = true; break; } case 'serverinfo': { ok = true; break; }
case 'userinfo': { ok = true; break; } case 'userinfo': { ok = true; break; }
case 'listusers': { ok = true; break; } case 'listusers': { ok = true; break; }
case 'listusersessions': { ok = true; break; }
case 'listdevicegroups': { ok = true; break; } case 'listdevicegroups': { ok = true; break; }
case 'listdevices': { ok = true; break; } case 'listdevices': { ok = true; break; }
case 'listusersofdevicegroup': { case 'listusersofdevicegroup': {
@ -203,6 +205,12 @@ if (args['_'].length == 0) {
console.log(" --json - Show result as JSON."); console.log(" --json - Show result as JSON.");
break; break;
} }
case 'listusersessions': {
console.log("List active user sessions on the MeshCentral server, Example usages:\r\n");
console.log(" MeshCtrl ListUserSessions");
console.log(" MeshCtrl ListUserSessions --json");
break;
}
case 'listdevicegroups': { case 'listdevicegroups': {
console.log("List the device groups for this account, Example usages:\r\n"); console.log("List the device groups for this account, Example usages:\r\n");
console.log(" MeshCtrl ListDeviceGroups "); console.log(" MeshCtrl ListDeviceGroups ");
@ -582,17 +590,19 @@ function serverConnect() {
case 'serverinfo': { break; } case 'serverinfo': { break; }
case 'userinfo': { break; } case 'userinfo': { break; }
case 'listusers': { ws.send(JSON.stringify({ action: 'users' })); break; } case 'listusers': { ws.send(JSON.stringify({ action: 'users' })); break; }
case 'listusersessions': { ws.send(JSON.stringify({ action: 'wssessioncount' })); }
case 'listdevicegroups': { ws.send(JSON.stringify({ action: 'meshes' })); break; } case 'listdevicegroups': { ws.send(JSON.stringify({ action: 'meshes' })); break; }
case 'listusersofdevicegroup': { ws.send(JSON.stringify({ action: 'meshes' })); break; } case 'listusersofdevicegroup': { ws.send(JSON.stringify({ action: 'meshes' })); break; }
case 'listdevices': { case 'listdevices': {
if (args.group) { if (args.group) {
ws.send(JSON.stringify({ action: 'nodes', meshname: args.group, responseid: 'meshctrl' })); break; ws.send(JSON.stringify({ action: 'nodes', meshname: args.group, responseid: 'meshctrl' }));
} else if (args.id) { } else if (args.id) {
ws.send(JSON.stringify({ action: 'nodes', meshid: args.id, responseid: 'meshctrl' })); break; ws.send(JSON.stringify({ action: 'nodes', meshid: args.id, responseid: 'meshctrl' }));
} else { } else {
ws.send(JSON.stringify({ action: 'meshes' })); ws.send(JSON.stringify({ action: 'meshes' }));
ws.send(JSON.stringify({ action: 'nodes', responseid: 'meshctrl' })); break; ws.send(JSON.stringify({ action: 'nodes', responseid: 'meshctrl' }));
} }
break;
} }
case 'adduser': { case 'adduser': {
var siteadmin = 0; var siteadmin = 0;
@ -813,6 +823,15 @@ function serverConnect() {
process.exit(); process.exit();
} }
break; break;
case 'wssessioncount': { // LIST USER SESSIONS
if (args.json) {
console.log(JSON.stringify(data.wssessions, ' ', 2));
} else {
for (var i in data.wssessions) { console.log(i + ', ' + ((data.wssessions[i] > 1) ? (data.wssessions[i] + ' sessions.') : ("1 session."))); }
}
process.exit();
break;
}
case 'users': { // LISTUSERS case 'users': { // LISTUSERS
if (args.filter) { if (args.filter) {
// Filter the list of users // Filter the list of users

View File

@ -1387,7 +1387,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
{ {
// Request a list of all web socket user session count // Request a list of all web socket user session count
var wssessions = {}; var wssessions = {};
if ((user.siteadmin & 2) == 0) break; if ((user.siteadmin & 2) == 0) { try { ws.send(JSON.stringify({ action: 'wssessioncount', wssessions: {}, tag: command.tag })); } catch (ex) { } break; }
if (parent.parent.multiServer == null) { if (parent.parent.multiServer == null) {
// No peering, use simple session counting // No peering, use simple session counting
for (i in parent.wssessions) { for (i in parent.wssessions) {