Added editdevice to meshctrl.js
Ylian Saint-Hilaire committed
Jun 11, 2021 at 12:48 UTC
57552aee86878d6423f37dc41d8c0f9bfb33db6e
2 files changed
+73
-5
meshctrl.js
+46
-1
@@ -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 = ['edituser', 'listusers', 'listusersessions', 'listdevicegroups', 'listdevices', 'listusersofdevicegroup', 'listevents', 'logintokens', 'serverinfo', 'userinfo', 'adduser', 'removeuser', 'adddevicegroup', 'removedevicegroup', 'editdevicegroup', 'broadcast', 'showevents', 'addusertodevicegroup', 'removeuserfromdevicegroup', 'addusertodevice', 'removeuserfromdevice', 'sendinviteemail', 'generateinvitelink', 'config', 'movetodevicegroup', 'deviceinfo', 'addusergroup', 'listusergroups', 'removeusergroup', 'runcommand', 'shell', 'upload', 'download', 'deviceopenurl', 'devicemessage', 'devicetoast', 'addtousergroup', 'removefromusergroup', 'removeallusersfromusergroup', 'devicesharing', 'devicepower', 'indexagenterrorlog'];
10
+const possibleCommands = ['edituser', 'listusers', 'listusersessions', 'listdevicegroups', 'listdevices', 'listusersofdevicegroup', 'listevents', 'logintokens', 'serverinfo', 'userinfo', 'adduser', 'removeuser', 'adddevicegroup', 'removedevicegroup', 'editdevicegroup', 'broadcast', 'showevents', 'addusertodevicegroup', 'removeuserfromdevicegroup', 'addusertodevice', 'removeuserfromdevice', 'sendinviteemail', 'generateinvitelink', 'config', 'movetodevicegroup', 'deviceinfo', 'editdevice', 'addusergroup', 'listusergroups', 'removeusergroup', 'runcommand', 'shell', 'upload', 'download', 'deviceopenurl', 'devicemessage', 'devicetoast', 'addtousergroup', 'removefromusergroup', 'removeallusersfromusergroup', 'devicesharing', 'devicepower', 'indexagenterrorlog'];
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) {
@@ -27,6 +27,7 @@ if (args['_'].length == 0) {
27
console.log(" ListEvents - List server events.");
28
console.log(" LoginTokens - List, create and remove login tokens.");
29
console.log(" DeviceInfo - Show information about a device.");
30
+ console.log(" EditDevice - Make changes to a device.");
31
console.log(" Config - Perform operation on config.json file.");
32
console.log(" AddUser - Create a new user account.");
33
console.log(" EditUser - Change a user account.");
@@ -96,6 +97,11 @@ if (args['_'].length == 0) {
97
else { ok = true; }
98
break;
99
}
100
+ case 'editdevice': {
101
+ if (args.id == null) { console.log(winRemoveSingleQuotes("Missing device id, use --id '[deviceid]'")); }
102
+ else { ok = true; }
103
+ break;
104
+ }
105
case 'addusertodevicegroup': {
106
if ((args.id == null) && (args.group == null)) { console.log(winRemoveSingleQuotes("Device group identifier missing, use --id '[groupid]' or --group [groupname]")); }
107
else if (args.userid == null) { console.log("Add user to group missing useid, use --userid [userid]"); }
@@ -719,6 +725,33 @@ if (args['_'].length == 0) {
725
console.log(" --json - Give results in JSON format.");
726
break;
727
}
728
+ case 'editdevice': {
729
+ console.log("Change information about a device, Example usages:\r\n");
730
+ console.log(winRemoveSingleQuotes(" MeshCtrl EditDevice --id 'deviceid' --name 'device1'"));
731
+ console.log("\r\nRequired arguments:\r\n");
732
+ if (process.platform == 'win32') {
733
+ console.log(" --id [deviceid] - The device identifier.");
734
+ } else {
735
+ console.log(" --id '[deviceid]' - The device identifier.");
736
+ }
737
+ console.log("\r\nOptional arguments:\r\n");
738
+ if (process.platform == 'win32') {
739
+ console.log(" --name [name] - Change device name.");
740
+ console.log(" --desc [description] - Change device description.");
741
+ console.log(" --tags [tag1,tags2] - Change device tags.");
742
+ } else {
743
+ console.log(" --name '[name]' - Change device name.");
744
+ console.log(" --desc '[description]' - Change device description.");
745
+ console.log(" --tags '[tag1,tags2]' - Change device tags.");
746
+ }
747
+ console.log(" --icon [number] - Change the device icon (1 to 8).");
748
+ console.log(" --consent [flags] - Sum of the following numbers:");
749
+ console.log(" 1 = Desktop notify 2 = Terminal notify");
750
+ console.log(" 4 = Files notify 8 = Desktop prompt");
751
+ console.log(" 16 = Terminal prompt 32 = Files prompt");
752
+ console.log(" 64 = Desktop privacy bar");
753
+ break;
754
+ }
755
case 'runcommand': {
756
console.log("Run a shell command on a remote device, Example usages:\r\n");
757
console.log(winRemoveSingleQuotes(" MeshCtrl RunCommand --id 'deviceid' --run \"command\""));
@@ -1391,6 +1424,17 @@ function serverConnect() {
1424
ws.send(JSON.stringify({ action: 'getsysinfo', nodeid: args.id, nodeinfo: true, responseid: 'meshctrl' }));
1425
break;
1426
}
1427
+ case 'editdevice': {
1428
+ var op = { action: 'changedevice', nodeid: args.id, responseid: 'meshctrl' };
1429
+ if (typeof args.name == 'string') { op.name = args.name; }
1430
+ if (typeof args.name == 'number') { op.name = '' + args.name; }
1431
+ if (args.desc) { if (args.desc === true) { op.desc = ''; } else if (typeof args.desc == 'string') { op.desc = args.desc; } else if (typeof args.desc == 'number') { op.desc = '' + args.desc; } }
1432
+ if (args.tags) { if (args.tags === true) { op.tags = ''; } else if (typeof args.tags == 'string') { op.tags = args.tags.split(','); } else if (typeof args.tags == 'number') { op.tags = '' + args.tags; } }
1433
+ if (args.icon) { op.icon = parseInt(args.icon); if ((typeof op.icon != 'number') || isNaN(op.icon) || (op.icon < 1) || (op.icon > 8)) { console.log("Icon must be between 1 and 8."); process.exit(1); return; } }
1434
+ if (args.consent) { op.consent = parseInt(args.consent); if ((typeof op.consent != 'number') || isNaN(op.consent) || (op.consent < 1)) { console.log("Invalid consent flags."); process.exit(1); return; } }
1435
+ ws.send(JSON.stringify(op));
1436
+ break;
1437
+ }
1438
case 'runcommand': {
1439
var runAsUser = 0;
1440
if (args.runasuser) { runAsUser = 1; } else if (args.runasuseronly) { runAsUser = 2; }
@@ -1693,6 +1737,7 @@ function serverConnect() {
1737
case 'toast': // TOAST
1738
case 'adduser': // ADDUSER
1739
case 'edituser': // EDITUSER
1740
+ case 'changedevice': // EDITDEVICE
1741
case 'deleteuser': // REMOVEUSER
1742
case 'createmesh': // ADDDEVICEGROUP
1743
case 'deletemesh': // REMOVEDEVICEGROUP
meshuser.js
+27
-4
@@ -4154,7 +4154,10 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
4154
// Get the node and the rights for this node
4155
parent.GetNodeWithRights(domain, user, command.nodeids[i], function (node, rights, visible) {
4156
// Check we have the rights to notify this device
4157
- if ((rights & MESHRIGHT_CHATNOTIFY) == 0) return;
4157
+ if ((rights & MESHRIGHT_CHATNOTIFY) == 0) {
4158
+ if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'toast', responseid: command.responseid, result: 'Access Denied' })); } catch (ex) { } }
4159
+ return;
4160
+ }
4161
4162
// Get this device and send toast command
4163
const agent = parent.wsagents[node._id];
@@ -4199,13 +4202,30 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
4202
}
4203
case 'changedevice':
4204
{
4205
+ var err = null;
4206
+
4207
// Argument validation
4203
- if (common.validateString(command.nodeid, 1, 1024) == false) break; // Check nodeid
4204
- if ((command.userloc) && (command.userloc.length != 2) && (command.userloc.length != 0)) return;
4208
+ try {
4209
+ if (common.validateString(command.nodeid, 1, 1024) == false) { err = "Invalid nodeid"; } // Check nodeid
4210
+ else {
4211
+ if (command.nodeid.indexOf('/') == -1) { command.nodeid = 'node/' + domain.id + '/' + command.nodeid; }
4212
+ if ((command.nodeid.split('/').length != 3) || (command.nodeid.split('/')[1] != domain.id)) { err = "Invalid nodeid"; } // Invalid domain, operation only valid for current domain
4213
+ else if ((command.userloc) && (command.userloc.length != 2) && (command.userloc.length != 0)) { err = "Invalid user location"; }
4214
+ }
4215
+ } catch (ex) { console.log(ex); err = "Validation exception: " + ex; }
4216
+
4217
+ // Handle any errors
4218
+ if (err != null) {
4219
+ if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'changedevice', responseid: command.responseid, result: err })); } catch (ex) { } }
4220
+ break;
4221
+ }
4222
4223
// Get the node and the rights for this node
4224
parent.GetNodeWithRights(domain, user, command.nodeid, function (node, rights, visible) {
4208
- if ((rights & MESHRIGHT_MANAGECOMPUTERS) == 0) return;
4225
+ if ((rights & MESHRIGHT_MANAGECOMPUTERS) == 0) {
4226
+ if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'changedevice', responseid: command.responseid, result: 'Access Denied' })); } catch (ex) { } }
4227
+ return;
4228
+ }
4229
var mesh = parent.meshes[node.meshid], amtchange = 0;
4230
4231
// Ready the node change event
@@ -4302,6 +4322,9 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
4322
if (db.changeStream) { event.noact = 1; } // If DB change stream is active, don't use this event to change the node. Another event will come.
4323
parent.parent.DispatchEvent(parent.CreateNodeDispatchTargets(node.meshid, node._id, [user._id]), obj, event);
4324
}
4325
+
4326
+ // Send response if required
4327
+ if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'changedevice', responseid: command.responseid, result: 'ok' })); } catch (ex) { } }
4328
});
4329
break;
4330
}