Improved MeshCtrl to allow device group names instead of group id in some commands.

Ylian Saint-Hilaire committed Apr 7, 2020 at 16:13 UTC 9956c04aa27d25ec8c6d9f729e277b4f3ae02113
3 files changed +45 -16
meshctrl.js
+19 -9
@@ -113,13 +113,13 @@ if (args['_'].length == 0) {
113 break;
114 }
115 case 'sendinviteemail': {
116 - if (args.id == null) { console.log("Device group identifier id missing, use --id [groupid]"); }
116 + if ((args.id == null) && (args.group == null)) { console.log("Device group identifier missing, use --id [groupid] or --group [groupname]"); }
117 else if (args.email == null) { console.log("Device email is missing, use --email [email]"); }
118 - else { ok = true; }
118 + else { ok = true; }
119 break;
120 }
121 case 'generateinvitelink': {
122 - if (args.id == null) { console.log("Device group identifier id missing, use --id [groupid]"); }
122 + if ((args.id == null) && (args.group == null)) { console.log("Device group identifier missing, use --id [groupid] or --group [groupname]"); }
123 else if (args.hours == null) { console.log("Invitation validity period missing, use --hours [hours]"); }
124 else { ok = true; }
125 break;
@@ -135,18 +135,24 @@ if (args['_'].length == 0) {
135 }
136 case 'sendinviteemail': {
137 console.log("Send invitation email with instructions on how to install the mesh agent for a specific device group. Example usage:\r\n");
138 - console.log(" MeshCtrl SendInviteEmail --id devicegroupid --email user@sample.com");
138 + console.log(" MeshCtrl SendInviteEmail --id devicegroupid --message \"msg\" --email user@sample.com");
139 + console.log(" MeshCtrl SendInviteEmail --group \"My Computers\" --name \"Jack\" --email user@sample.com");
140 console.log("\r\nRequired arguments:\r\n");
140 - console.log(" --id [groupid] - Device group identifier.");
141 + console.log(" --id [groupid] - Device group identifier (or --group).");
142 + console.log(" --group [groupname] - Device group name (or --id).");
143 console.log(" --email [email] - Email address.");
144 + console.log("\r\nOptional arguments:\r\n");
145 + console.log(" --name (name) - Name of recipient to be included in the email.");
146 + console.log(" --message (msg) - Message to be included in the email.");
147 break;
148 }
149 case 'generateinvitelink': {
150 console.log("Generate a agent invitation URL for a given group. Example usage:\r\n");
151 console.log(" MeshCtrl GenerateInviteLink --id devicegroupid --hours 24");
147 - console.log(" MeshCtrl GenerateInviteLink --id devicegroupid --hours 0");
152 + console.log(" MeshCtrl GenerateInviteLink --group \"My Computers\" --hours 0");
153 console.log("\r\nRequired arguments:\r\n");
149 - console.log(" --id [groupid] - Device group identifier.");
154 + console.log(" --id [groupid] - Device group identifier (or --group).");
155 + console.log(" --group [groupname] - Device group name (or --id).");
156 console.log(" --hours [hours] - Validity period in hours or 0 for infinit.");
157 break;
158 }
@@ -624,12 +630,16 @@ function serverConnect() {
630 break;
631 }
632 case 'sendinviteemail': {
627 - var op = { action: 'inviteAgent', meshid: args.id, email: args.email, name: '', os: '0', responseid: 'meshctrl' }
633 + var op = { action: 'inviteAgent', email: args.email, name: '', os: '0', responseid: 'meshctrl' }
634 + if (args.id) { op.meshid = args.id; } else if (args.group) { op.meshname = args.group; }
635 + if (args.name) { op.name = args.name; }
636 + if (args.message) { op.msg = args.message; }
637 ws.send(JSON.stringify(op));
638 break;
639 }
640 case 'generateinvitelink': {
632 - var op = { action: 'createInviteLink', meshid: args.id, expire: args.hours, flags: 0, responseid: 'meshctrl' }
641 + var op = { action: 'createInviteLink', expire: args.hours, flags: 0, responseid: 'meshctrl' }
642 + if (args.id) { op.meshid = args.id; } else if (args.group) { op.meshname = args.group; }
643 ws.send(JSON.stringify(op));
644 break;
645 }
meshuser.js
+25 -6
@@ -3126,6 +3126,14 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
3126 }
3127 case 'inviteAgent':
3128 {
3129 + // Resolve the device group name if needed
3130 + if ((typeof command.meshname == 'string') && (command.meshid == null)) {
3131 + for (var i in parent.meshes) {
3132 + var m = parent.meshes[i];
3133 + if ((m.mtype == 2) && (m.name == command.meshname) && parent.IsMeshViewable(user, m)) { command.meshid = m._id; break; }
3134 + }
3135 + }
3136 +
3137 var err = null, mesh = null;
3138 try {
3139 if ((parent.parent.mailserver == null) || (args.lanonly == true)) { err = 'Unsupported feature'; } // This operation requires the email server
@@ -3573,13 +3581,26 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
3581 break;
3582 }
3583 case 'createInviteLink': {
3584 + // Resolve the device group name if needed
3585 + if ((typeof command.meshname == 'string') && (command.meshid == null)) {
3586 + for (var i in parent.meshes) {
3587 + var m = parent.meshes[i];
3588 + if ((m.mtype == 2) && (m.name == command.meshname) && parent.IsMeshViewable(user, m)) { command.meshid = m._id; break; }
3589 + }
3590 + }
3591 +
3592 var err = null;
3593 if (common.validateString(command.meshid, 8, 128) == false) { err = 'Invalid group id'; } // Check the meshid
3594 else if (common.validateInt(command.expire, 0, 99999) == false) { err = 'Invalid expire time'; } // Check the expire time in hours
3579 - else if (common.validateInt(command.flags, 0, 256) == false) { err = 'Invalid flags'; }; // Check the flags
3580 - if (command.meshid.split('/').length == 1) { command.meshid = 'mesh/' + domain.id + '/' + command.meshid; }
3581 - var smesh = command.meshid.split('/');
3582 - if ((smesh.length != 3) || (smesh[0] != 'mesh') || (smesh[1] != domain.id)) { err = 'Invalid group id'; }
3595 + else if (common.validateInt(command.flags, 0, 256) == false) { err = 'Invalid flags'; } // Check the flags
3596 + else if (common.validateString(command.meshid, 1, 1024) == false) { err = 'Invalid group identifier'; } // Check meshid
3597 + else {
3598 + if (command.meshid.split('/').length == 1) { command.meshid = 'mesh/' + domain.id + '/' + command.meshid; }
3599 + var smesh = command.meshid.split('/');
3600 + if ((smesh.length != 3) || (smesh[0] != 'mesh') || (smesh[1] != domain.id)) { err = 'Invalid group id'; }
3601 + mesh = parent.meshes[command.meshid];
3602 + if ((mesh == null) || (parent.IsMeshViewable(user, mesh) == false)) { err = 'Invalid group id'; }
3603 + }
3604 var serverName = parent.getWebServerName(domain);
3605
3606 // Handle any errors
@@ -3588,8 +3609,6 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
3609 break;
3610 }
3611
3591 - mesh = parent.meshes[command.meshid];
3592 - if (mesh == null) { if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'createInviteLink', responseid: command.responseid, result: 'Unable to find this device group id' })); } catch (ex) { } } break; }
3612 const inviteCookie = parent.parent.encodeCookie({ a: 4, mid: command.meshid, f: command.flags, expire: command.expire * 60 }, parent.parent.invitationLinkEncryptionKey);
3613 if (inviteCookie == null) { if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'createInviteLink', responseid: command.responseid, result: 'Unable to generate invitation cookie' })); } catch (ex) { } } break; }
3614
package.json
+1 -1
@@ -1,6 +1,6 @@
1 {
2 "name": "meshcentral",
3 - "version": "0.5.1-i",
3 + "version": "0.5.1-j",
4 "keywords": [
5 "Remote Management",
6 "Intel AMT",