Added removedevice command to meshctrl.js (#4753)
Ylian Saint-Hilaire committed
Nov 14, 2022 at 16:31 UTC
876641f1ac824a56502df306c767af0897bbf37b
2 files changed
+41
-26
meshctrl.js
+37
-26
@@ -16,7 +16,7 @@ var settings = {};
16
const crypto = require('crypto');
17
const args = require('minimist')(process.argv.slice(2));
18
const path = require('path');
19
-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', 'agentdownload'];
19
+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', 'removedevice', 'editdevice', 'addusergroup', 'listusergroups', 'removeusergroup', 'runcommand', 'shell', 'upload', 'download', 'deviceopenurl', 'devicemessage', 'devicetoast', 'addtousergroup', 'removefromusergroup', 'removeallusersfromusergroup', 'devicesharing', 'devicepower', 'indexagenterrorlog', 'agentdownload'];
20
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; } }
21
22
if (args['_'].length == 0) {
@@ -37,6 +37,7 @@ if (args['_'].length == 0) {
37
console.log(" LoginTokens - List, create and remove login tokens.");
38
console.log(" DeviceInfo - Show information about a device.");
39
console.log(" EditDevice - Make changes to a device.");
40
+ console.log(" RemoveDevice - Delete a device.");
41
console.log(" Config - Perform operation on config.json file.");
42
console.log(" AddUser - Create a new user account.");
43
console.log(" EditUser - Change a user account.");
@@ -97,16 +98,9 @@ if (args['_'].length == 0) {
98
case 'listdevices': { ok = true; break; }
99
case 'listevents': { ok = true; break; }
100
case 'logintokens': { ok = true; break; }
100
- case 'listusersofdevicegroup': {
101
- if (args.id == null) { console.log(winRemoveSingleQuotes("Missing group id, use --id '[groupid]'")); }
102
- else { ok = true; }
103
- break;
104
- }
105
- case 'deviceinfo': {
106
- if (args.id == null) { console.log(winRemoveSingleQuotes("Missing device id, use --id '[deviceid]'")); }
107
- else { ok = true; }
108
- break;
109
- }
101
+ case 'listusersofdevicegroup':
102
+ case 'deviceinfo':
103
+ case 'removedevice':
104
case 'editdevice': {
105
if (args.id == null) { console.log(winRemoveSingleQuotes("Missing device id, use --id '[deviceid]'")); }
106
else { ok = true; }
@@ -208,7 +202,7 @@ if (args['_'].length == 0) {
202
case 'sendinviteemail': {
203
if ((args.id == null) && (args.group == null)) { console.log("Device group identifier missing, use --id '[groupid]' or --group [groupname]"); }
204
else if (args.email == null) { console.log("Device email is missing, use --email [email]"); }
211
- else { ok = true; }
205
+ else { ok = true; }
206
break;
207
}
208
case 'generateinvitelink': {
@@ -304,7 +298,7 @@ if (args['_'].length == 0) {
298
console.log(" --email [email] - Email address.");
299
console.log("\r\nOptional arguments:\r\n");
300
console.log(" --name (name) - Name of recipient to be included in the email.");
307
- console.log(" --message (msg) - Message to be included in the email.");
301
+ console.log(" --message (msg) - Message to be included in the email.");
302
break;
303
}
304
case 'generateinvitelink': {
@@ -764,6 +758,17 @@ if (args['_'].length == 0) {
758
console.log(" --json - Give results in JSON format.");
759
break;
760
}
761
+ case 'removedevice': {
762
+ console.log("Delete a device, Example usages:\r\n");
763
+ console.log(winRemoveSingleQuotes(" MeshCtrl RemoveDevice --id 'deviceid'"));
764
+ console.log("\r\nRequired arguments:\r\n");
765
+ if (process.platform == 'win32') {
766
+ console.log(" --id [deviceid] - The device identifier.");
767
+ } else {
768
+ console.log(" --id '[deviceid]' - The device identifier.");
769
+ }
770
+ break;
771
+ }
772
case 'editdevice': {
773
console.log("Change information about a device, Example usages:\r\n");
774
console.log(winRemoveSingleQuotes(" MeshCtrl EditDevice --id 'deviceid' --name 'device1'"));
@@ -986,8 +991,8 @@ function displayConfigHelp() {
991
992
function performConfigOperations(args) {
993
var domainValues = ['title', 'title2', 'titlepicture', 'trustedcert', 'welcomepicture', 'welcometext', 'userquota', 'meshquota', 'newaccounts', 'usernameisemail', 'newaccountemaildomains', 'newaccountspass', 'newaccountsrights', 'geolocation', 'lockagentdownload', 'userconsentflags', 'Usersessionidletimeout', 'auth', 'ldapoptions', 'ldapusername', 'ldapuserbinarykey', 'ldapuseremail', 'footer', 'certurl', 'loginKey', 'userallowedip', 'agentallowedip', 'agentnoproxy', 'agentconfig', 'orphanagentuser', 'httpheaders', 'yubikey', 'passwordrequirements', 'limits', 'amtacmactivation', 'redirects', 'sessionrecording', 'hide'];
989
- var domainObjectValues = [ 'ldapoptions', 'httpheaders', 'yubikey', 'passwordrequirements', 'limits', 'amtacmactivation', 'redirects', 'sessionrecording' ];
990
- var domainArrayValues = [ 'newaccountemaildomains', 'newaccountsrights', 'loginkey', 'agentconfig' ];
994
+ var domainObjectValues = ['ldapoptions', 'httpheaders', 'yubikey', 'passwordrequirements', 'limits', 'amtacmactivation', 'redirects', 'sessionrecording'];
995
+ var domainArrayValues = ['newaccountemaildomains', 'newaccountsrights', 'loginkey', 'agentconfig'];
996
var configChange = false;
997
var fs = require('fs');
998
var path = require('path');
@@ -1172,7 +1177,7 @@ function serverConnect() {
1177
case 'listdevices': {
1178
if (args.details) {
1179
// Get list of devices with lots of details
1175
- ws.send(JSON.stringify({ action: 'getDeviceDetails', type: (args.csv)?'csv':'json' }));
1180
+ ws.send(JSON.stringify({ action: 'getDeviceDetails', type: (args.csv) ? 'csv' : 'json' }));
1181
} else if (args.group) {
1182
ws.send(JSON.stringify({ action: 'nodes', meshname: args.group, responseid: 'meshctrl' }));
1183
} else if (args.id) {
@@ -1185,7 +1190,7 @@ function serverConnect() {
1190
}
1191
case 'listevents': {
1192
limit = null;
1188
- if (args.limit) { limit = parseInt(args.limit); }
1193
+ if (args.limit) { limit = parseInt(args.limit); }
1194
if ((typeof limit != 'number') || (limit < 1)) { limit = null; }
1195
1196
var cmd = null;
@@ -1395,7 +1400,7 @@ function serverConnect() {
1400
break;
1401
}
1402
case 'movetodevicegroup': {
1398
- var op = { action: 'changeDeviceMesh', responseid: 'meshctrl', nodeids: [ args.devid ] };
1403
+ var op = { action: 'changeDeviceMesh', responseid: 'meshctrl', nodeids: [args.devid] };
1404
if (args.id) { op.meshid = args.id; } else if (args.group) { op.meshname = args.group; }
1405
ws.send(JSON.stringify(op));
1406
break;
@@ -1488,6 +1493,11 @@ function serverConnect() {
1493
ws.send(JSON.stringify({ action: 'getsysinfo', nodeid: args.id, nodeinfo: true, responseid: 'meshctrl' }));
1494
break;
1495
}
1496
+ case 'removedevice': {
1497
+ var op = { action: 'removedevices', nodeids: [ args.id ], responseid: 'meshctrl' };
1498
+ ws.send(JSON.stringify(op));
1499
+ break;
1500
+ }
1501
case 'editdevice': {
1502
var op = { action: 'changedevice', nodeid: args.id, responseid: 'meshctrl' };
1503
if (typeof args.name == 'string') { op.name = args.name; }
@@ -1709,7 +1719,7 @@ function serverConnect() {
1719
return siteadmin;
1720
}
1721
1712
- ws.on('close', function() { process.exit(); });
1722
+ ws.on('close', function () { process.exit(); });
1723
ws.on('error', function (err) {
1724
if (err.code == 'ENOTFOUND') { console.log('Unable to resolve ' + url); }
1725
else if (err.code == 'ECONNREFUSED') { console.log('Unable to connect to ' + url); }
@@ -1893,6 +1903,7 @@ function serverConnect() {
1903
case 'toast': // TOAST
1904
case 'adduser': // ADDUSER
1905
case 'edituser': // EDITUSER
1906
+ case 'removedevices': // REMOVEDEVICE
1907
case 'changedevice': // EDITDEVICE
1908
case 'deleteuser': // REMOVEUSER
1909
case 'createmesh': // ADDDEVICEGROUP
@@ -1959,11 +1970,11 @@ function serverConnect() {
1970
console.log(x);
1971
var mesh = [], user = [], node = [];
1972
if (data.ugroups[i].links != null) { for (var j in data.ugroups[i].links) { if (j.startsWith('mesh/')) { mesh.push(j); } if (j.startsWith('user/')) { user.push(j); } if (j.startsWith('node/')) { node.push(j); } } }
1962
- console.log(' Users:');
1973
+ console.log(' Users:');
1974
if (user.length > 0) { for (var j in user) { console.log(' ' + user[j]); } } else { console.log(' (None)'); }
1964
- console.log(' Device Groups:');
1975
+ console.log(' Device Groups:');
1976
if (mesh.length > 0) { for (var j in mesh) { console.log(' ' + mesh[j] + ', ' + data.ugroups[i].links[mesh[j]].rights); } } else { console.log(' (None)'); }
1966
- console.log(' Devices:');
1977
+ console.log(' Devices:');
1978
if (node.length > 0) { for (var j in node) { console.log(' ' + node[j] + ', ' + data.ugroups[i].links[node[j]].rights); } } else { console.log(' (None)'); }
1979
}
1980
}
@@ -2086,7 +2097,7 @@ function serverConnect() {
2097
} else if (args.json) {
2098
// Return all devices in JSON format
2099
var nodes = [];
2089
-
2100
+
2101
for (var i in data.nodes) {
2102
const devicesInMesh = data.nodes[i];
2103
for (var j in devicesInMesh) {
@@ -2341,9 +2352,9 @@ function getDevicesThatMatchFilter(nodes, x) {
2352
var rs = x.split(/\s+/).join('|'), rx = new RegExp(rs); // In some cases (like +), this can throw an exception.
2353
for (var d in nodes) {
2354
//if (showRealNames) {
2344
- //if (nodes[d].rnamel != null && rx.test(nodes[d].rnamel.toLowerCase())) { r.push(nodes[d]); }
2355
+ //if (nodes[d].rnamel != null && rx.test(nodes[d].rnamel.toLowerCase())) { r.push(nodes[d]); }
2356
//} else {
2346
- if (rx.test(nodes[d].name.toLowerCase())) { r.push(nodes[d]); }
2357
+ if (rx.test(nodes[d].name.toLowerCase())) { r.push(nodes[d]); }
2358
//}
2359
}
2360
} catch (ex) { for (var d in nodes) { r.push(nodes[d]); } }
@@ -2416,7 +2427,7 @@ function connectTunnel(url) {
2427
if ((cmd.action == 'uploadack') || (cmd.action == 'uploadstart')) {
2428
settings.inFlight--;
2429
if (settings.uploadFile == null) { if (settings.inFlight == 0) { process.exit(); } return; } // If the file is closed and there is no more in-flight data, exit.
2419
- var loops = (cmd.action == 'uploadstart')?16:1; // If this is the first data to be sent, hot start now. We are going to have 16 blocks of data in-flight.
2430
+ var loops = (cmd.action == 'uploadstart') ? 16 : 1; // If this is the first data to be sent, hot start now. We are going to have 16 blocks of data in-flight.
2431
for (var i = 0; i < loops; i++) {
2432
if (settings.uploadFile == null) continue;
2433
var buf = Buffer.alloc(65565);
meshuser.js
+4
@@ -2781,6 +2781,10 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
2781
}
2782
});
2783
}
2784
+
2785
+ // Send response if required, in this case we always send ok which is not ideal.
2786
+ if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'removedevices', responseid: command.responseid, result: 'ok' })); } catch (ex) { } }
2787
+
2788
break;
2789
}
2790
case 'wakedevices':