4* @description MeshCentral command line tool
5* @author Ylian Saint-Hilaire
6* @copyright Intel Corporation 2018-2022
11// Make sure we have the dependency modules
12try { require('minimist'); } catch (ex) { console.log('Missing module "minimist", type "npm install minimist" to install it.'); return; }
13try { require('ws'); } catch (ex) { console.log('Missing module "ws", type "npm install ws" to install it.'); return; }
16const crypto = require('crypto');
17const args = require('minimist')(process.argv.slice(2));
18const path = require('path');
19const 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', 'addlocaldevice', 'addamtdevice', 'addusergroup', 'listusergroups', 'removeusergroup', 'runcommand', 'shell', 'upload', 'download', 'deviceopenurl', 'devicemessage', 'devicetoast', 'addtousergroup', 'removefromusergroup', 'removeallusersfromusergroup', 'devicesharing', 'devicepower', 'indexagenterrorlog', 'agentdownload', 'report', 'grouptoast', 'groupmessage', 'webrelay'];
20if (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; } }
22if (args['_'].length == 0) {
23 console.log("MeshCtrl performs command line actions on a MeshCentral server.");
24 console.log("Information at: https://meshcentral.com");
25 console.log("No action specified, use MeshCtrl like this:\r\n\r\n meshctrl [action] [arguments]\r\n");
26 console.log("Supported actions:");
27 console.log(" Help [action] - Get help on an action.");
28 console.log(" ServerInfo - Show server information.");
29 console.log(" UserInfo - Show user information.");
30 console.log(" ListUsers - List user accounts.");
31 console.log(" ListUserSessions - List online users.");
32 console.log(" ListUserGroups - List user groups.");
33 console.log(" ListDevices - List devices.");
34 console.log(" ListDeviceGroups - List device groups.");
35 console.log(" ListUsersOfDeviceGroup - List the users in a device group.");
36 console.log(" ListEvents - List server events.");
37 console.log(" LoginTokens - List, create and remove login tokens.");
38 console.log(" DeviceInfo - Show information about a device.");
39 console.log(" AddLocalDevice - Add a local device.");
40 console.log(" AddAmtDevice - Add a AMT device.");
41 console.log(" EditDevice - Make changes to a device.");
42 console.log(" RemoveDevice - Delete a device.");
43 console.log(" Config - Perform operation on config.json file.");
44 console.log(" AddUser - Create a new user account.");
45 console.log(" EditUser - Change a user account.");
46 console.log(" RemoveUser - Delete a user account.");
47 console.log(" AddUserGroup - Create a new user group.");
48 console.log(" RemoveUserGroup - Delete a user group.");
49 console.log(" AddToUserGroup - Add a user, device or device group to a user group.");
50 console.log(" RemoveFromUserGroup - Remove a user, device or device group from a user group.");
51 console.log(" RemoveAllUsersFromUserGroup - Remove all users from a user group.");
52 console.log(" AddDeviceGroup - Create a new device group.");
53 console.log(" RemoveDeviceGroup - Delete a device group.");
54 console.log(" EditDeviceGroup - Change a device group values.");
55 console.log(" MoveToDeviceGroup - Move a device to a different device group.");
56 console.log(" AddUserToDeviceGroup - Add a user to a device group.");
57 console.log(" RemoveUserFromDeviceGroup - Remove a user from a device group.");
58 console.log(" AddUserToDevice - Add a user to a device.");
59 console.log(" RemoveUserFromDevice - Remove a user from a device.");
60 console.log(" SendInviteEmail - Send an agent install invitation email.");
61 console.log(" GenerateInviteLink - Create an invitation link.");
62 console.log(" Broadcast - Display a message to all online users.");
63 console.log(" ShowEvents - Display real-time server events in JSON format.");
64 console.log(" RunCommand - Run a shell command on a remote device.");
65 console.log(" Shell - Access command shell of a remote device.");
66 console.log(" Upload - Upload a file to a remote device.");
67 console.log(" Download - Download a file from a remote device.");
68 console.log(" WebRelay - Creates a HTTP/HTTPS webrelay link for a remote device.");
69 console.log(" DeviceOpenUrl - Open a URL on a remote device.");
70 console.log(" DeviceMessage - Open a message box on a remote device.");
71 console.log(" DeviceToast - Display a toast notification on a remote device.");
72 console.log(" GroupMessage - Open a message box on remote devices in a specific device group.");
73 console.log(" GroupToast - Display a toast notification on remote devices in a specific device group.");
74 console.log(" DevicePower - Perform wake/sleep/reset/off operations on remote devices.");
75 console.log(" DeviceSharing - View, add and remove sharing links for a given device.");
76 console.log(" AgentDownload - Download an agent of a specific type for a device group.");
77 console.log(" Report - Create and show a CSV report.");
78 console.log("\r\nSupported login arguments:");
79 console.log(" --url [wss://server] - Server url, wss://localhost:443 is default.");
80 console.log(" - Use wss://localhost:443?key=xxx if login key is required.");
81 console.log(" --loginuser [username] - Login username, admin is default.");
82 console.log(" --loginpass [password] - Login password OR Leave blank to enter password at prompt");
83 console.log(" --token [number] - 2nd factor authentication token.");
84 console.log(" --loginkey [hex] - Server login key in hex.");
85 console.log(" --loginkeyfile [file] - File containing server login key in hex.");
86 console.log(" --logindomain [domainid] - Domain id, default is empty, only used with loginkey.");
87 console.log(" --proxy [http://proxy:123] - Specify an HTTP proxy.");
90 settings.cmd = args['_'][0].toLowerCase();
91 if ((possibleCommands.indexOf(settings.cmd) == -1) && (settings.cmd != 'help')) { console.log("Invalid command. Possible commands are: " + possibleCommands.join(', ') + '.'); return; }
92 //console.log(settings.cmd);
95 switch (settings.cmd) {
96 case 'config': { performConfigOperations(args); return; }
97 case 'indexagenterrorlog': { indexAgentErrorLog(); return; }
98 case 'serverinfo': { ok = true; break; }
99 case 'userinfo': { ok = true; break; }
100 case 'listusers': { ok = true; break; }
101 case 'listusersessions': { ok = true; break; }
102 case 'listusergroups': { ok = true; break; }
103 case 'listdevicegroups': { ok = true; break; }
104 case 'listdevices': { ok = true; break; }
105 case 'listevents': { ok = true; break; }
106 case 'logintokens': { ok = true; break; }
107 case 'listusersofdevicegroup':
111 if (args.id == null) { console.log(winRemoveSingleQuotes("Missing device id, use --id '[deviceid]'")); }
115 case 'addlocaldevice': {
116 if (args.id == null) { console.log(winRemoveSingleQuotes("Missing device id, use --id '[deviceid]'")); }
117 else if (args.devicename == null) { console.log(winRemoveSingleQuotes("Missing devicename, use --devicename [devicename]")); }
118 else if (args.hostname == null) { console.log(winRemoveSingleQuotes("Missing hostname, use --hostname [hostname]")); }
122 case 'addamtdevice': {
123 if (args.id == null) { console.log(winRemoveSingleQuotes("Missing device id, use --id '[deviceid]'")); }
124 else if (args.devicename == null) { console.log(winRemoveSingleQuotes("Missing devicename, use --devicename [devicename]")); }
125 else if (args.hostname == null) { console.log(winRemoveSingleQuotes("Missing hostname, use --hostname [hostname]")); }
126 else if (args.user == null) { console.log(winRemoveSingleQuotes("Missing user, use --user [user]")); }
127 else if (args.pass == null) { console.log(winRemoveSingleQuotes("Missing pass, use --pass [pass]")); }
131 case 'addusertodevicegroup': {
132 if ((args.id == null) && (args.group == null)) { console.log(winRemoveSingleQuotes("Device group identifier missing, use --id '[groupid]' or --group [groupname]")); }
133 else if (args.userid == null) { console.log("Add user to group missing useid, use --userid [userid]"); }
137 case 'removeuserfromdevicegroup': {
138 if ((args.id == null) && (args.group == null)) { console.log(winRemoveSingleQuotes("Device group identifier missing, use --id '[groupid]' or --group [groupname]")); }
139 else if (args.userid == null) { console.log("Remove user from group missing useid, use --userid [userid]"); }
143 case 'addusertodevice': {
144 if (args.userid == null) { console.log("Add user to device missing userid, use --userid [userid]"); }
145 else if (args.id == null) { console.log(winRemoveSingleQuotes("Add user to device missing device id, use --id '[deviceid]'")); }
149 case 'removeuserfromdevice': {
150 if (args.userid == null) { console.log("Remove user from device missing userid, use --userid [userid]"); }
151 else if (args.id == null) { console.log(winRemoveSingleQuotes("Remove user from device missing device id, use --id '[deviceid]'")); }
155 case 'adddevicegroup': {
156 if (args.name == null) { console.log("Message group name, use --name [name]"); }
160 case 'editdevicegroup':
161 case 'removedevicegroup': {
162 if ((args.id == null) && (args.group == null)) { console.log(winRemoveSingleQuotes("Device group identifier missing, use --id '[groupid]' or --group [groupname]")); }
166 case 'movetodevicegroup': {
167 if ((args.id == null) && (args.group == null)) { console.log(winRemoveSingleQuotes("Device group identifier missing, use --id '[groupid]' or --group [groupname]")); }
168 else if (args.devid == null) { console.log(winRemoveSingleQuotes("Device identifier missing, use --devid '[deviceid]'")); }
173 if (args.msg == null) { console.log("Message missing, use --msg [message]"); }
182 if (args.user == null) { console.log("New account name missing, use --user [name]"); }
183 else if ((args.pass == null) && (args.randompass == null)) { console.log("New account password missing, use --pass [password] or --randompass"); }
188 if (args.userid == null) { console.log("Edit account user missing, use --userid [id]"); }
193 if (args.userid == null) { console.log("Remove account userid missing, use --userid [id]"); }
197 case 'addusergroup': {
198 if (args.name == null) { console.log("New user group name missing, use --name [name]"); }
202 case 'removeusergroup': {
203 if (args.groupid == null) { console.log(winRemoveSingleQuotes("Remove user group id missing, use --groupid '[id]'")); }
207 case 'addtousergroup': {
208 if (args.groupid == null) { console.log(winRemoveSingleQuotes("Group id missing, use --groupid '[id]'")); }
209 if (args.id == null) { console.log(winRemoveSingleQuotes("Missing identifier to add, use --id [id]")); }
213 case 'removefromusergroup': {
214 if (args.groupid == null) { console.log(winRemoveSingleQuotes("Group id missing, use --groupid '[id]'")); }
215 if (args.id == null) { console.log(winRemoveSingleQuotes("Missing identifier to remove, use --id [id]")); }
219 case 'removeallusersfromusergroup': {
220 if (args.groupid == null) { console.log(winRemoveSingleQuotes("Group id missing, use --groupid '[id]'")); }
224 case 'sendinviteemail': {
225 if ((args.id == null) && (args.group == null)) { console.log("Device group identifier missing, use --id '[groupid]' or --group [groupname]"); }
226 else if (args.email == null) { console.log("Device email is missing, use --email [email]"); }
230 case 'generateinvitelink': {
231 if ((args.id == null) && (args.group == null)) { console.log("Device group identifier missing, use --id '[groupid]' or --group [groupname]"); }
232 else if (args.hours == null) { console.log("Invitation validity period missing, use --hours [hours]"); }
237 if (args.id == null) { console.log(winRemoveSingleQuotes("Missing device id, use --id '[deviceid]'")); }
238 else if (args.run == null) { console.log("Missing run, use --run \"command\""); }
243 if (args.id == null) { console.log(winRemoveSingleQuotes("Missing device id, use --id '[deviceid]'")); }
247 case 'devicepower': {
248 if (args.id == null) { console.log(winRemoveSingleQuotes("Missing device id, use --id '[deviceid]'")); }
252 case 'devicesharing': {
253 if (args.id == null) { console.log(winRemoveSingleQuotes("Missing device id, use --id '[deviceid]'")); }
254 else if ((args.daily != null) && (args.weekly != null)) { console.log(winRemoveSingleQuotes("Can't specify both --daily and --weekly at the same time.")); }
258 case 'agentdownload': {
259 if (args.type == null) { console.log(winRemoveSingleQuotes("Missing device type, use --type [agenttype]")); }
260 else if ((parseInt(args.type) == null) || isNaN(parseInt(args.type)) || (parseInt(args.type) < 1) || (parseInt(args.type) > 11000)) { console.log(winRemoveSingleQuotes("Invalid agent type, must be a number.")); }
261 else if (args.id == null) { console.log(winRemoveSingleQuotes("Missing device id, use --id '[meshid]'")); }
262 else if ((typeof args.id != 'string') || (args.id.length != 64)) { console.log(winRemoveSingleQuotes("Invalid meshid.")); }
267 if (args.id == null) { console.log(winRemoveSingleQuotes("Missing device id, use --id '[deviceid]'")); }
268 else if (args.file == null) { console.log("Local file missing, use --file [file] specify the file to upload"); }
269 else if (args.target == null) { console.log("Remote target path missing, use --target [path] to specify the remote location"); }
270 else if (require('fs').existsSync(args.file) == false) { console.log("Local file does not exists, check --file"); }
275 if (args.id == null) { console.log(winRemoveSingleQuotes("Missing device id, use --id '[deviceid]'")); }
276 else if (args.file == null) { console.log("Remote file missing, use --file [file] specify the remote file to download"); }
277 else if (args.target == null) { console.log("Target path missing, use --target [path] to specify the local download location"); }
282 if (args.id == null) { console.log(winRemoveSingleQuotes("Missing device id, use --id '[deviceid]'")); }
283 else if (args.type == null) { console.log(winRemoveSingleQuotes("Missing protocol type, use --type [http,https]")); }
287 case 'deviceopenurl': {
288 if (args.id == null) { console.log(winRemoveSingleQuotes("Missing device id, use --id '[deviceid]'")); }
289 else if (args.openurl == null) { console.log("Remote URL, use --openurl [url] specify the link to open."); }
293 case 'devicemessage': {
294 if (args.id == null) { console.log(winRemoveSingleQuotes("Missing device id, use --id '[deviceid]'")); }
295 else if (args.msg == null) { console.log("Remote message, use --msg \"[message]\" specify a remote message."); }
299 case 'devicetoast': {
300 if (args.id == null) { console.log(winRemoveSingleQuotes("Missing device id, use --id '[deviceid]'")); }
301 else if (args.msg == null) { console.log("Remote message, use --msg \"[message]\" specify a remote message."); }
305 case 'groupmessage': {
306 if (args.id == null) { console.log(winRemoveSingleQuotes("Missing device group id, use --id '[devicegroupid]'")); }
307 else if (args.msg == null) { console.log("Remote message, use --msg \"[message]\" specify a remote message."); }
312 if (args.id == null) { console.log(winRemoveSingleQuotes("Missing device group id, use --id '[devicegroupid]'")); }
313 else if (args.msg == null) { console.log("Remote message, use --msg \"[message]\" specify a remote message."); }
318 if (args.type == null) { console.log(winRemoveSingleQuotes("Missing report type, use --type '[reporttype]'")); }
323 if (args['_'].length < 2) {
324 console.log("Get help on an action. Type:\r\n\r\n help [action]\r\n\r\nPossible actions are: " + possibleCommands.join(', ') + '.');
326 switch (args['_'][1].toLowerCase()) {
331 case 'sendinviteemail': {
332 console.log("Send invitation email with instructions on how to install the mesh agent for a specific device group. Example usage:\r\n");
333 console.log(winRemoveSingleQuotes(" MeshCtrl SendInviteEmail --id 'groupid' --message \"msg\" --email user@sample.com"));
334 console.log(winRemoveSingleQuotes(" MeshCtrl SendInviteEmail --group \"My Computers\" --name \"Jack\" --email user@sample.com"));
335 console.log("\r\nRequired arguments:\r\n");
336 if (process.platform == 'win32') {
337 console.log(" --id [groupid] - Device group identifier (or --group).");
339 console.log(" --id '[groupid]' - Device group identifier (or --group).");
341 console.log(" --group [groupname] - Device group name (or --id).");
342 console.log(" --email [email] - Email address.");
343 console.log("\r\nOptional arguments:\r\n");
344 console.log(" --name (name) - Name of recipient to be included in the email.");
345 console.log(" --message (msg) - Message to be included in the email.");
348 case 'generateinvitelink': {
349 console.log("Generate a agent invitation URL for a given group. Example usage:\r\n");
350 console.log(winRemoveSingleQuotes(" MeshCtrl GenerateInviteLink --id 'groupid' --hours 24"));
351 console.log(" MeshCtrl GenerateInviteLink --group \"My Computers\" --hours 0");
352 console.log("\r\nRequired arguments:\r\n");
353 if (process.platform == 'win32') {
354 console.log(" --id [groupid] - Device group identifier (or --group).");
356 console.log(" --id '[groupid]' - Device group identifier (or --group).");
358 console.log(" --group [groupname] - Device group name (or --id).");
359 console.log(" --hours [hours] - Validity period in hours or 0 for infinite.");
360 console.log("\r\nOptional arguments:\r\n");
361 console.log(" --flags [mode] - Mode flag for link type (0 = both, 1 = interactive only, 2 = background only)");
365 console.log("Show the server's event stream for this user account. Example usage:\r\n");
366 console.log(" MeshCtrl ShowEvents");
367 console.log(" MeshCtrl ShowEvents --filter nodeconnect");
368 console.log(" MeshCtrl ShowEvents --filter uicustomevent,changenode");
369 console.log("\r\nOptional arguments:\r\n");
370 console.log(" --filter [actions] - Show only specified actions.");
374 console.log("Get information on the MeshCentral server, Example usages:\r\n");
375 console.log(" MeshCtrl ServerInfo --loginuser myaccountname --loginpass mypassword");
376 console.log(" MeshCtrl ServerInfo --loginuser myaccountname --loginkeyfile key.txt");
377 console.log("\r\nOptional arguments:\r\n");
378 console.log(" --json - Show result as JSON.");
382 console.log("Get account information for the login account, Example usages:\r\n");
383 console.log(" MeshCtrl UserInfo --loginuser myaccountname --loginpass mypassword");
384 console.log(" MeshCtrl UserInfo --loginuser myaccountname --loginkeyfile key.txt");
385 console.log("\r\nOptional arguments:\r\n");
386 console.log(" --json - Show result as JSON.");
390 console.log("List the account on the MeshCentral server, Example usages:\r\n");
391 console.log(" MeshCtrl ListUsers");
392 console.log(" MeshCtrl ListUsers --json");
393 console.log(" MeshCtrl ListUsers --nameexists \"bob\"");
394 console.log(" MeshCtrl ListUsers --filter 2fa");
395 console.log("\r\nOptional arguments:\r\n");
396 console.log(" --idexists [id] - Return 1 if id exists, 0 if not.");
397 console.log(" --nameexists [name] - Return id if name exists.");
398 console.log(" --filter [filter1,...] - Filter user names: 2FA, NO2FA.");
399 console.log(" --json - Show result as JSON.");
402 case 'listusersessions': {
403 console.log("List active user sessions on the MeshCentral server, Example usages:\r\n");
404 console.log(" MeshCtrl ListUserSessions");
405 console.log(" MeshCtrl ListUserSessions --json");
408 case 'listusergroups': {
409 console.log("List user groups on the MeshCentral server, Example usages:\r\n");
410 console.log(" MeshCtrl ListUserGroups");
411 console.log(" MeshCtrl ListUserGroups --json");
414 case 'listdevicegroups': {
415 console.log("List the device groups for this account. Example usages:\r\n");
416 console.log(" MeshCtrl ListDeviceGroups ");
417 console.log(" MeshCtrl ListDeviceGroups --json");
418 console.log("\r\nOptional arguments:\r\n");
419 console.log(" --idexists [id] - Return 1 if id exists, 0 if not.");
420 console.log(" --nameexists [name] - Return id if name exists.");
421 console.log(" --emailexists [email] - Return id if email exists.");
422 console.log(" --hex - Display meshid in hex format.");
423 console.log(" --json - Show result as JSON.");
426 case 'listdevices': {
427 console.log("List devices. Example usages:\r\n");
428 console.log(" MeshCtrl ListDevices");
429 console.log(winRemoveSingleQuotes(" MeshCtrl ListDevices -id '[groupid]' --json"));
430 console.log("\r\nOptional arguments:\r\n");
431 if (process.platform == 'win32') {
432 console.log(" --id [groupid] - Filter by group identifier (or --group).");
434 console.log(" --id '[groupid]' - Filter by group identifier (or --group).");
436 console.log(" --group [groupname] - Filter by group name (or --id).");
437 console.log(" --count - Only return the device count.");
438 console.log(" --json - Show result as JSON.");
439 console.log(" --csv - Show result as comma separated values.");
440 console.log(" --filter \"[filter]\" - Filter devices using a filter string.");
441 console.log(" \"x\" - Devices with \"x\" in the name.");
442 console.log(" \"user:x or u:x\" - Devices with \"x\" in the name of currently logged in user.");
443 console.log(" \"ip:x\" - Devices \"x\" IP address.");
444 console.log(" \"group:x or g:x\" - Devices with \"x\" in device group name.");
445 console.log(" \"tag:x or t:x\" - Devices with \"x\" in device tag.");
446 console.log(" \"atag:x or a:x\" - Devices with \"x\" in device agent tag.");
447 console.log(" \"os:x\" - Devices with \"x\" in the device OS description.");
448 console.log(" \"amt:x\" - Devices with Intel AMT provisioning state (0, 1, 2).");
449 console.log(" \"desc:x\" - Devices with \"x\" in device description.");
450 console.log(" \"wsc:ok\" - Devices with Windows Security Center ok.");
451 console.log(" \"wsc:noav\" - Devices with Windows Security Center with anti-virus problem.");
452 console.log(" \"wsc:noupdate\" - Devices with Windows Security Center with update problem.");
453 console.log(" \"wsc:nofirewall\" - Devices with Windows Security Center with firewall problem.");
454 console.log(" \"wsc:any\" - Devices with Windows Security Center with any problem.");
455 console.log(" \"a and b\" - Match both conditions with precedence over OR. For example: \"lab and g:home\".");
456 console.log(" \"a or b\" - Math one of the conditions, for example: \"lab or g:home\".");
457 console.log(" --filterid [id,id...] - Show only results for devices with included id.");
458 console.log(" --details - Show all device details.");
461 case 'listusersofdevicegroup': {
462 console.log("List users that have permissions for a given device group. Example usage:\r\n");
463 console.log(" MeshCtrl ListUserOfDeviceGroup ");
464 console.log("\r\nRequired arguments:\r\n");
465 if (process.platform == 'win32') {
466 console.log(" --id [groupid] - Device group identifier.");
468 console.log(" --id '[groupid]' - Device group identifier.");
470 console.log("\r\nOptional arguments:\r\n");
471 console.log(" --json - Show result as JSON.");
475 console.log("List server events optionally filtered by user or device. Example usage:\r\n");
476 console.log(" MeshCtrl ListEvents ");
477 console.log("\r\nOptional arguments:\r\n");
478 console.log(" --userid [name] - User account identifier.");
479 console.log(" --id [deviceid] - The device identifier.");
480 console.log(" --limit [number] - Maximum number of events to list.");
481 console.log(" --raw - Output raw data in JSON format.");
482 console.log(" --json - Give results in JSON format.");
485 case 'logintokens': {
486 console.log("List account login tokens and allow addition and removal. Example usage:\r\n");
487 console.log(" MeshCtrl LoginTokens ");
488 console.log("\r\nOptional arguments:\r\n");
489 console.log(" --remove [name] - Remove a login token.");
490 console.log(" --add [name] - Add a login token.");
491 console.log(" --expire [minutes] - When adding a token, minutes until expire.");
492 console.log(" --json - Show login tokens in JSON format.");
496 console.log("Add a new user account. Example usages:\r\n");
497 console.log(" MeshCtrl AddUser --user newaccountname --pass newpassword");
498 console.log(" MeshCtrl AddUser --user newaccountname --randompass --rights full");
499 console.log("\r\nRequired arguments:\r\n");
500 console.log(" --user [name] - New account name.");
501 console.log(" --pass [password] - New account password.");
502 console.log(" --randompass - Create account with a random password.");
503 console.log("\r\nOptional arguments:\r\n");
504 console.log(" --domain [domain] - Account domain, only for cross-domain admins.");
505 console.log(" --email [email] - New account email address.");
506 console.log(" --emailverified - New account email is verified.");
507 console.log(" --resetpass - Request password reset on next login.");
508 console.log(" --realname [name] - Set the real name for this account.");
509 console.log(" --phone [number] - Set the account phone number.");
510 console.log(" --rights [none|full|a,b,c] - Comma separated list of server permissions. Possible values:");
511 console.log(" manageusers,serverbackup,serverrestore,serverupdate,fileaccess,locked,nonewgroups,notools,usergroups,recordings,locksettings,allevents,nonewdevices");
515 console.log("Edit a user account, Example usages:\r\n");
516 console.log(" MeshCtrl EditUser --userid user --rights locked,locksettings");
517 console.log(" MeshCtrl EditUser --userid user --realname Jones");
518 console.log("\r\nRequired arguments:\r\n");
519 console.log(" --userid [name] - User account identifier.");
520 console.log("\r\nOptional arguments:\r\n");
521 console.log(" --domain [domain] - Account domain, only for cross-domain admins.");
522 console.log(" --email [email] - Account email address.");
523 console.log(" --emailverified - Account email is verified.");
524 console.log(" --resetpass - Request password reset on next login.");
525 console.log(" --realname [name] - Set the real name for this account.");
526 console.log(" --phone [number] - Set the account phone number.");
527 console.log(" --rights [none|full|a,b,c] - Comma separated list of server permissions. Possible values:");
528 console.log(" manageusers,serverbackup,serverrestore,serverupdate,fileaccess,locked,nonewgroups,notools,usergroups,recordings,locksettings,allevents,nonewdevices");
532 console.log("Delete a user account, Example usages:\r\n");
533 console.log(" MeshCtrl RemoveUser --userid accountid");
534 console.log("\r\nRequired arguments:\r\n");
535 console.log(" --userid [id] - Account identifier.");
538 case 'addusergroup': {
539 console.log("Create a new user group, Example usages:\r\n");
540 console.log(" MeshCtrl AddUserGroup --name \"Test Group\"");
541 console.log("\r\nRequired arguments:\r\n");
542 console.log(" --name [name] - Name of the user group.");
545 case 'removeusergroup': {
546 console.log("Remove a user group, Example usages:\r\n");
547 console.log(winRemoveSingleQuotes(" MeshCtrl RemoveUserGroup --groupid 'ugrp//abcdf'"));
548 console.log("\r\nRequired arguments:\r\n");
549 if (process.platform == 'win32') {
550 console.log(" --groupid [groupid] - User group identifier.");
552 console.log(" --groupid '[groupid]' - User group identifier.");
556 case 'addtousergroup': {
557 console.log("Add a user, device or device group to a user group, Example usages:\r\n");
558 console.log(winRemoveSingleQuotes(" MeshCtrl AddToUserGroup --id 'user//abcdef' --groupid 'ugrp//abcdf'"));
559 console.log(winRemoveSingleQuotes(" MeshCtrl AddToUserGroup --id 'node//abcdef' --groupid 'ugrp//abcdf' --rights [rights]"));
560 console.log(winRemoveSingleQuotes(" MeshCtrl AddToUserGroup --id 'mesh//abcdef' --groupid 'ugrp//abcdf' --rights [rights]"));
561 console.log("\r\nRequired arguments:\r\n");
562 if (process.platform == 'win32') {
563 console.log(" --id [id] - Identifier to add.");
564 console.log(" --groupid [groupid] - User group identifier.");
566 console.log(" --id '[id]' - Identifier to add.");
567 console.log(" --groupid '[groupid]' - User group identifier.");
569 console.log("\r\nOptional arguments:\r\n");
570 console.log(" --rights [number] - Rights granted for adding device or device group.");
571 console.log(" - 4294967295 for full admin or the sum of the following numbers.");
572 console.log(" 1 = Edit Device Group 2 = Manage Users ");
573 console.log(" 4 = Manage Computers 8 = Remote Control ");
574 console.log(" 16 = Agent Console 32 = Server Files ");
575 console.log(" 64 = Wake Device 128 = Set Notes ");
576 console.log(" 256 = Remote View Only 512 = No Terminal ");
577 console.log(" 1024 = No Files 2048 = No Intel AMT ");
578 console.log(" 4096 = Desktop Limited Input 8192 = Limit Events ");
579 console.log(" 16384 = Chat / Notify 32768 = Uninstall Agent ");
580 console.log(" 65536 = No Remote Desktop 131072 = Remote Commands ");
581 console.log(" 262144 = Reset / Power off ");
584 case 'removefromusergroup': {
585 console.log("Remove a user, device or device group from a user group, Example usages:\r\n");
586 console.log(winRemoveSingleQuotes(" MeshCtrl RemoveUserFromUserGroup --userid 'user//abcdef' --groupid 'ugrp//abcdf'"));
587 console.log(winRemoveSingleQuotes(" MeshCtrl RemoveUserFromUserGroup --userid 'node//abcdef' --groupid 'ugrp//abcdf'"));
588 console.log(winRemoveSingleQuotes(" MeshCtrl RemoveUserFromUserGroup --userid 'mesh//abcdef' --groupid 'ugrp//abcdf'"));
589 console.log("\r\nRequired arguments:\r\n");
590 if (process.platform == 'win32') {
591 console.log(" --id [userid] - Identifier to remove.");
592 console.log(" --groupid [groupid] - User group identifier.");
594 console.log(" --id '[userid]' - Identifier to remove.");
595 console.log(" --groupid '[groupid]' - User group identifier.");
599 case 'removeallusersfromusergroup': {
600 console.log("Remove all users from a user group, Example usages:\r\n");
601 console.log(winRemoveSingleQuotes(" MeshCtrl RemoveAllUsersFromUserGroup --groupid 'ugrp//abcdf'"));
602 console.log("\r\nRequired arguments:\r\n");
603 if (process.platform == 'win32') {
604 console.log(" --groupid [groupid] - User group identifier.");
606 console.log(" --groupid '[groupid]' - User group identifier.");
610 case 'adddevicegroup': {
611 console.log("Add a device group, Example usages:\r\n");
612 console.log(" MeshCtrl AddDeviceGroup --name newgroupname");
613 console.log(" MeshCtrl AddDeviceGroup --name newgroupname --desc description --amtonly");
614 console.log(" MeshCtrl AddDeviceGroup --name newgroupname --features 1 --consent 7");
615 console.log("\r\nRequired arguments:\r\n");
616 console.log(" --name [name] - Name of the new group.");
617 console.log("\r\nOptional arguments:\r\n");
618 console.log(" --desc [description] - New group description.");
619 console.log(" --amtonly - New group is agent-less, Intel AMT only.");
620 console.log(" --agentless - New group is agent-less only.");
621 console.log(" --features [number] - Set device group features, sum of numbers below.");
622 console.log(" 1 = Auto-Remove 2 = Hostname Sync");
623 console.log(" 4 = Record Sessions");
624 console.log(" --consent [number] - Set device group user consent, sum of numbers below.");
625 console.log(" 1 = Desktop notify user 2 = Terminal notify user ");
626 console.log(" 4 = Files notify user 8 = Desktop prompt user ");
627 console.log(" 16 = Terminal prompt user 32 = Files prompt user ");
628 console.log(" 64 = Desktop Toolbar ");
631 case 'removedevicegroup': {
632 console.log("Remove a device group, Example usages:\r\n");
633 console.log(winRemoveSingleQuotes(" MeshCtrl RemoveDeviceGroup --id 'groupid'"));
634 console.log("\r\nRequired arguments:\r\n");
635 if (process.platform == 'win32') {
636 console.log(" --id [groupid] - Device group identifier (or --group).");
638 console.log(" --id '[groupid]' - Device group identifier (or --group).");
640 console.log(" --group [groupname] - Device group name (or --id).");
643 case 'editdevicegroup': {
644 console.log("Edit a device group, Example usages:\r\n");
645 console.log(winRemoveSingleQuotes(" MeshCtrl EditDeviceGroup --id 'groupid' --name \"New Name\""));
646 console.log(winRemoveSingleQuotes(" MeshCtrl EditDeviceGroup --id 'groupid' --desc \"Description\" --consent 63"));
647 console.log(winRemoveSingleQuotes(" MeshCtrl EditDeviceGroup --id 'groupid' --invitecodes \"code1,code2\" --backgroundonly"));
648 console.log("\r\nRequired arguments:\r\n");
649 if (process.platform == 'win32') {
650 console.log(" --id [groupid] - Device group identifier (or --group).");
652 console.log(" --id '[groupid]' - Device group identifier (or --group).");
654 console.log(" --group [groupname] - Device group name (or --id).");
655 console.log("\r\nOptional arguments:\r\n");
656 console.log(" --name [name] - Set new device group name.");
657 console.log(" --desc [description] - Set new device group description, blank to clear.");
658 console.log(" --flags [number] - Set device group flags, sum of the values below, 0 for none.");
659 console.log(" 1 = Auto remove device on disconnect.");
660 console.log(" 2 = Sync hostname.");
661 console.log(" --consent [number] - Set device group consent options, sum of the values below, 0 for none.");
662 console.log(" 1 = Desktop notify user.");
663 console.log(" 2 = Terminal notify user.");
664 console.log(" 4 = Files notify user.");
665 console.log(" 8 = Desktop prompt for user consent.");
666 console.log(" 16 = Terminal prompt for user consent.");
667 console.log(" 32 = Files prompt for user consent.");
668 console.log(" 64 = Desktop show connection toolbar.");
669 console.log(" --invitecodes [aa,bb] - Comma separated list of invite codes, blank to clear.");
670 console.log(" --backgroundonly - When used with invitecodes, set agent to only install in background.");
671 console.log(" --interactiveonly - When used with invitecodes, set agent to only run on demand.");
674 case 'movetodevicegroup': {
675 console.log("Move a device to a new device group, Example usages:\r\n");
676 console.log(winRemoveSingleQuotes(" MeshCtrl MoveToDeviceGroup --devid 'deviceid' --id 'groupid'"));
677 console.log("\r\nRequired arguments:\r\n");
678 if (process.platform == 'win32') {
679 console.log(" --id [groupid] - Device group identifier (or --group).");
681 console.log(" --id '[groupid]' - Device group identifier (or --group).");
683 console.log(" --group [groupname] - Device group name (or --id).");
684 if (process.platform == 'win32') {
685 console.log(" --devid [deviceid] - Device identifier.");
687 console.log(" --devid '[deviceid]' - Device identifier.");
691 case 'addusertodevicegroup': {
692 console.log("Add a user to a device group, Example usages:\r\n");
693 console.log(winRemoveSingleQuotes(" MeshCtrl AddUserToDeviceGroup --id 'groupid' --userid userid --fullrights"));
694 console.log(" MeshCtrl AddUserToDeviceGroup --group groupname --userid userid --editgroup --manageusers");
695 console.log("\r\nRequired arguments:\r\n");
696 if (process.platform == 'win32') {
697 console.log(" --id [groupid] - Device group identifier (or --group).");
699 console.log(" --id '[groupid]' - Device group identifier (or --group).");
701 console.log(" --group [groupname] - Device group name (or --id).");
702 console.log(" --userid [userid] - The user identifier.");
703 console.log("\r\nOptional arguments:\r\n");
704 console.log(" --fullrights - Allow full rights over this device group.");
705 console.log(" --editgroup - Allow the user to edit group information.");
706 console.log(" --manageusers - Allow the user to add/remove users.");
707 console.log(" --managedevices - Allow the user to edit device information.");
708 console.log(" --remotecontrol - Allow device remote control operations.");
709 console.log(" --agentconsole - Allow agent console operations.");
710 console.log(" --serverfiles - Allow access to group server files.");
711 console.log(" --wakedevices - Allow device wake operation.");
712 console.log(" --notes - Allow editing of device notes.");
713 console.log(" --desktopviewonly - Restrict user to view-only remote desktop.");
714 console.log(" --limiteddesktop - Limit remote desktop keys.");
715 console.log(" --noterminal - Hide the terminal tab from this user.");
716 console.log(" --nofiles - Hide the files tab from this user.");
717 console.log(" --noamt - Hide the Intel AMT tab from this user.");
718 console.log(" --limitedevents - User can only see his own events.");
719 console.log(" --chatnotify - Allow chat and notification options.");
720 console.log(" --uninstall - Allow remote uninstall of the agent.");
721 if (args.limiteddesktop) { meshrights |= 4096; }
722 if (args.limitedevents) { meshrights |= 8192; }
723 if (args.chatnotify) { meshrights |= 16384; }
724 if (args.uninstall) { meshrights |= 32768; }
728 case 'removeuserfromdevicegroup': {
729 console.log("Remove a user from a device group, Example usages:\r\n");
730 console.log(winRemoveSingleQuotes(" MeshCtrl RemoveuserFromDeviceGroup --id 'groupid' --userid userid"));
731 console.log("\r\nRequired arguments:\r\n");
732 if (process.platform == 'win32') {
733 console.log(" --id [groupid] - Device group identifier (or --group).");
735 console.log(" --id '[groupid]' - Device group identifier (or --group).");
737 console.log(" --group [groupname] - Device group name (or --id).");
738 console.log(" --userid [userid] - The user identifier.");
741 case 'addusertodevice': {
742 console.log("Add a user to a device, Example usages:\r\n");
743 console.log(winRemoveSingleQuotes(" MeshCtrl AddUserToDevice --id 'deviceid' --userid userid --fullrights"));
744 console.log(winRemoveSingleQuotes(" MeshCtrl AddUserToDevice --id 'deviceid' --userid userid --remotecontrol"));
745 console.log("\r\nRequired arguments:\r\n");
746 if (process.platform == 'win32') {
747 console.log(" --id [deviceid] - The device identifier.");
749 console.log(" --id '[deviceid]' - The device identifier.");
751 console.log(" --userid [userid] - The user identifier.");
752 console.log("\r\nOptional arguments:\r\n");
753 console.log(" --fullrights - Allow full rights over this device.");
754 console.log(" --remotecontrol - Allow device remote control operations.");
755 console.log(" --agentconsole - Allow agent console operations.");
756 console.log(" --serverfiles - Allow access to group server files.");
757 console.log(" --wakedevices - Allow device wake operation.");
758 console.log(" --notes - Allow editing of device notes.");
759 console.log(" --desktopviewonly - Restrict user to view-only remote desktop.");
760 console.log(" --limiteddesktop - Limit remote desktop keys.");
761 console.log(" --noterminal - Hide the terminal tab from this user.");
762 console.log(" --nofiles - Hide the files tab from this user.");
763 console.log(" --noamt - Hide the Intel AMT tab from this user.");
764 console.log(" --limitedevents - User can only see his own events.");
765 console.log(" --chatnotify - Allow chat and notification options.");
766 console.log(" --uninstall - Allow remote uninstall of the agent.");
769 case 'removeuserfromdevice': {
770 console.log("Remove a user from a device, Example usages:\r\n");
771 console.log(winRemoveSingleQuotes(" MeshCtrl RemoveuserFromDeviceGroup --id 'deviceid' --userid userid"));
772 console.log("\r\nRequired arguments:\r\n");
773 if (process.platform == 'win32') {
774 console.log(" --id [deviceid] - The device identifier.");
776 console.log(" --id '[deviceid]' - The device identifier.");
778 console.log(" --userid [userid] - The user identifier.");
782 console.log("Display a message to one or all logged in users, Example usages:\r\n");
783 console.log(" MeshCtrl Broadcast --msg \"This is a test\"");
784 console.log("\r\nRequired arguments:\r\n");
785 console.log(" --msg [message] - Message to display.");
786 console.log("\r\nOptional arguments:\r\n");
787 console.log(" --user [userid] - Send the message to the specified user.");
791 console.log("Display information about a device, Example usages:\r\n");
792 console.log(winRemoveSingleQuotes(" MeshCtrl DeviceInfo --id 'deviceid'"));
793 console.log(winRemoveSingleQuotes(" MeshCtrl DeviceInfo --id 'deviceid' --json"));
794 console.log("\r\nRequired arguments:\r\n");
795 if (process.platform == 'win32') {
796 console.log(" --id [deviceid] - The device identifier.");
798 console.log(" --id '[deviceid]' - The device identifier.");
800 console.log("\r\nOptional arguments:\r\n");
801 console.log(" --raw - Output raw data in JSON format.");
802 console.log(" --json - Give results in JSON format.");
805 case 'removedevice': {
806 console.log("Delete a device, Example usages:\r\n");
807 console.log(winRemoveSingleQuotes(" MeshCtrl RemoveDevice --id 'deviceid'"));
808 console.log("\r\nRequired arguments:\r\n");
809 if (process.platform == 'win32') {
810 console.log(" --id [deviceid] - The device identifier.");
812 console.log(" --id '[deviceid]' - The device identifier.");
816 case 'addlocaldevice': {
817 console.log("Add a Local Device, Example usages:\r\n");
818 console.log(winRemoveSingleQuotes(" MeshCtrl AddLocalDevice --id 'meshid' --devicename 'devicename' --hostname 'hostname'"));
819 console.log(winRemoveSingleQuotes(" MeshCtrl AddLocalDevice --id 'meshid' --devicename 'devicename' --hostname 'hostname' --type 6"));
820 console.log("\r\nRequired arguments:\r\n");
821 if (process.platform == 'win32') {
822 console.log(" --id [meshid] - The mesh identifier.");
823 console.log(" --devicename [devicename] - The device name.");
824 console.log(" --hostname [hostname] - The devices hostname or ip address.");
826 console.log(" --id '[meshid]' - The mesh identifier.");
827 console.log(" --devicename '[devicename]' - The device name.");
828 console.log(" --hostname '[hostname]' - The devices hostname or ip address.");
831 console.log("\r\nOptional arguments:\r\n");
832 console.log(" --type [TypeNumber] - With the following choices:");
833 console.log(" type 4 - Default, Windows (RDP)");
834 console.log(" type 6 - Linux (SSH/SCP/VNC)");
835 console.log(" type 29 - macOS (SSH/SCP/VNC)");
838 case 'addamtdevice': {
839 console.log("Add an Intel AMT Device, Example usages:\r\n");
840 console.log(winRemoveSingleQuotes(" MeshCtrl AddAmtDevice --id 'meshid' --devicename 'devicename' --hostname 'hostname --user 'admin' --pass 'admin'"));
841 console.log(winRemoveSingleQuotes(" MeshCtrl AddAmtDevice --id 'meshid' --devicename 'devicename' --hostname 'hostname --user 'admin' --pass 'admin' --notls"));
842 console.log("\r\nRequired arguments:\r\n");
843 if (process.platform == 'win32') {
844 console.log(" --id [meshid] - The mesh identifier.");
845 console.log(" --devicename [devicename] - The device name.");
846 console.log(" --hostname [hostname] - The devices hostname or ip address.");
847 console.log(" --user [user] - The devices AMT username.");
848 console.log(" --pass [pass] - The devices AMT password.");
851 console.log(" --id '[meshid]' - The mesh identifier.");
852 console.log(" --devicename '[devicename]' - The device name.");
853 console.log(" --hostname '[hostname]' - The devices hostname or ip address.");
854 console.log(" --user '[user]' - The devices AMT username.");
855 console.log(" --pass '[pass]' - The devices AMT password.");
857 console.log("\r\nOptional arguments:\r\n");
858 if (process.platform == 'win32') {
859 console.log(" --notls - Use No TLS Security.");
861 console.log(" --notls - Use No TLS Security.");
866 console.log("Change information about a device, Example usages:\r\n");
867 console.log(winRemoveSingleQuotes(" MeshCtrl EditDevice --id 'deviceid' --name 'device1'"));
868 console.log("\r\nRequired arguments:\r\n");
869 if (process.platform == 'win32') {
870 console.log(" --id [deviceid] - The device identifier.");
872 console.log(" --id '[deviceid]' - The device identifier.");
874 console.log("\r\nOptional arguments:\r\n");
875 if (process.platform == 'win32') {
876 console.log(" --name [name] - Change device name.");
877 console.log(" --desc [description] - Change device description.");
878 console.log(" --tags [tag1,tags2] - Change device tags.");
880 console.log(" --name '[name]' - Change device name.");
881 console.log(" --desc '[description]' - Change device description.");
882 console.log(" --tags '[tag1,tags2]' - Change device tags.");
884 console.log(" --icon [number] - Change the device icon (1 to 8).");
885 console.log(" --consent [flags] - Sum of the following numbers:");
886 console.log(" 1 = Desktop notify 2 = Terminal notify");
887 console.log(" 4 = Files notify 8 = Desktop prompt");
888 console.log(" 16 = Terminal prompt 32 = Files prompt");
889 console.log(" 64 = Desktop privacy bar");
893 console.log("Run a shell command on a remote device, Example usages:\r\n");
894 console.log(winRemoveSingleQuotes(" MeshCtrl RunCommand --id 'deviceid' --run \"command\""));
895 console.log(winRemoveSingleQuotes(" MeshCtrl RunCommand --id 'deviceid' --run \"command\" --powershell"));
896 console.log(winRemoveSingleQuotes(" MeshCtrl RunCommand --id 'deviceid' --run \"command\" --reply"));
897 console.log("\r\nRequired arguments:\r\n");
898 if (process.platform == 'win32') {
899 console.log(" --id [deviceid] - The device identifier.");
901 console.log(" --id '[deviceid]' - The device identifier.");
903 console.log(" --run \"[command]\" - Shell command to execute on the remote device.");
904 console.log("\r\nOptional arguments:\r\n");
905 console.log(" --powershell - Run in Windows PowerShell.");
906 console.log(" --runasuser - Attempt to run the command as logged in user.");
907 console.log(" --runasuseronly - Only run the command as the logged in user.");
908 console.log(" --reply - Return with the output from running the command.");
912 console.log("Access a command shell on a remote device, Example usages:\r\n");
913 console.log(winRemoveSingleQuotes(" MeshCtrl Shell --id 'deviceid'"));
914 console.log(winRemoveSingleQuotes(" MeshCtrl Shell --id 'deviceid' --powershell"));
915 console.log("\r\nRequired arguments:\r\n");
916 if (process.platform == 'win32') {
917 console.log(" --id [deviceid] - The device identifier.");
919 console.log(" --id '[deviceid]' - The device identifier.");
921 console.log("\r\nOptional arguments:\r\n");
922 console.log(" --powershell - Run a Windows PowerShell.");
925 case 'devicepower': {
926 console.log("Perform power operations on remote devices, Example usages:\r\n");
927 console.log(winRemoveSingleQuotes(" MeshCtrl DevicePower --wake --id 'deviceid'"));
928 console.log(winRemoveSingleQuotes(" MeshCtrl DevicePower --sleep --id 'deviceid'"));
929 console.log(winRemoveSingleQuotes(" MeshCtrl DevicePower --reset --id 'deviceid'"));
930 console.log(winRemoveSingleQuotes(" MeshCtrl DevicePower --off --id 'deviceid1,deviceid2'"));
931 console.log("\r\nNote that some power operations may take up to a minute to execute.\r\n");
932 console.log("Required arguments:\r\n");
933 if (process.platform == 'win32') {
934 console.log(" --id [deviceid1,deviceid2] - Device identifiers.");
936 console.log(" --id '[deviceid1,deviceid2]' - Device identifiers.");
938 console.log("\r\nOptional arguments:\r\n");
939 console.log(" --wake - Attempt to wake up the remote device.");
940 console.log(" --reset - Attempt to remote the remote device.");
941 console.log(" --sleep - Attempt to place the remote device in low power mode.");
942 console.log(" --off - Attempt to power off the remote device.");
943 console.log(" --amtoff - Attempt to power off the remote device using Intel AMT.");
944 console.log(" --amton - Attempt to power on the remote device using Intel AMT.");
945 console.log(" --amtreset - Attempt to reset the remote device using Intel AMT.");
948 case 'devicesharing': {
949 var tzoffset = (new Date()).getTimezoneOffset() * 60000; // Offset in milliseconds
950 var localISOTime = (new Date(Date.now() - tzoffset)).toISOString().slice(0, -5);
951 console.log("List sharing links for a specified device, Example usages:\r\n");
952 console.log(winRemoveSingleQuotes(" MeshCtrl DeviceSharing --id 'deviceid'"));
953 console.log(winRemoveSingleQuotes(" MeshCtrl DeviceSharing --id 'deviceid' --remove abcdef"));
954 console.log(winRemoveSingleQuotes(" MeshCtrl DeviceSharing --id 'deviceid' --add Guest --start " + localISOTime + " --duration 30"));
955 console.log(winRemoveSingleQuotes(" MeshCtrl DeviceSharing --id 'deviceid' --add Guest --start " + localISOTime + " --duration 30 --daily"));
956 console.log(winRemoveSingleQuotes(" MeshCtrl DeviceSharing --id 'deviceid' --add Guest --type desktop,terminal --consent prompt"));
957 console.log(winRemoveSingleQuotes(" MeshCtrl DeviceSharing --id 'deviceid' --add Guest --type http --port 80"));
958 console.log("\r\nRequired arguments:\r\n");
959 if (process.platform == 'win32') {
960 console.log(" --id [deviceid] - The device identifier.");
962 console.log(" --id '[deviceid]' - The device identifier.");
964 console.log("\r\nOptional arguments:\r\n");
965 console.log(" --remove [shareid] - Remove a device sharing link.");
966 console.log(" --add [guestname] - Add a device sharing link.");
967 console.log(" --type [desktop,terminal,files,http,https] - Type of sharing to add, can be combined. default is desktop.");
968 console.log(" --viewonly - Make desktop sharing view only.");
969 console.log(" --consent [notify,prompt,none] - Consent flags, default is notify.");
970 console.log(" --start [yyyy-mm-ddThh:mm:ss] - Start time, default is now.");
971 console.log(" --end [yyyy-mm-ddThh:mm:ss] - End time.");
972 console.log(" --duration [minutes] - Duration of the share, default is 60 minutes.");
973 console.log(" --daily - Add recurring daily device share.");
974 console.log(" --weekly - Add recurring weekly device share.");
975 console.log(" --port [portnumber] - Set alternative port for http or https, default is 80 for http and 443 for https.");
978 case 'agentdownload': {
979 console.log("Download an agent of a specific type for a given device group, Example usages:\r\n");
980 console.log(winRemoveSingleQuotes(" MeshCtrl AgentDownload --id 'groupid' --type 3"));
981 console.log(winRemoveSingleQuotes(" MeshCtrl AgentDownload --id 'groupid' --type 3 --installflags 1"));
982 console.log("\r\nRequired arguments:\r\n");
983 console.log(" --type [ArchitectureNumber] - Agent architecture number.");
984 if (process.platform == 'win32') {
985 console.log(" --id [groupid] - The device group identifier.");
987 console.log(" --id '[groupid]' - The device group identifier.");
989 console.log("\r\nOptional arguments:\r\n");
990 console.log(" --installflags [InstallFlagsNumber] - With the following choices:");
991 console.log(" installflags 0 - Default, Interactive & Background, offers connect button & install/uninstall");
992 console.log(" installflags 1 - Interactive only, offers only connect button, not install/uninstall");
993 console.log(" installflags 2 - Background only, offers only install/uninstall, not connect");
997 console.log("Upload a local file to a remote device, Example usages:\r\n");
998 console.log(winRemoveSingleQuotes(" MeshCtrl Upload --id 'deviceid' --file sample.txt --target c:\\"));
999 console.log(winRemoveSingleQuotes(" MeshCtrl Upload --id 'deviceid' --file sample.txt --target /tmp"));
1000 console.log("\r\nRequired arguments:\r\n");
1001 if (process.platform == 'win32') {
1002 console.log(" --id [deviceid] - The device identifier.");
1004 console.log(" --id '[deviceid]' - The device identifier.");
1006 console.log(" --file [localfile] - The local file to upload.");
1007 console.log(" --target [remotepath] - The remote path to upload the file to.");
1011 console.log("Download a file from a remote device, Example usages:\r\n");
1012 console.log(winRemoveSingleQuotes(" MeshCtrl Download --id 'deviceid' --file C:\\sample.txt --target c:\\temp"));
1013 console.log(winRemoveSingleQuotes(" MeshCtrl Download --id 'deviceid' --file /tmp/sample.txt --target /tmp"));
1014 console.log("\r\nRequired arguments:\r\n");
1015 if (process.platform == 'win32') {
1016 console.log(" --id [deviceid] - The device identifier.");
1018 console.log(" --id '[deviceid]' - The device identifier.");
1020 console.log(" --file [remotefile] - The remote file to download.");
1021 console.log("\r\nOptional arguments:\r\n");
1022 console.log(" --target [localpath] - The local path to download the file to.");
1026 console.log("Generate a webrelay URL to access a HTTP/HTTPS service on a remote device, Example usages:\r\n");
1027 console.log(winRemoveSingleQuotes(" MeshCtrl WebRelay --id 'deviceid' --type http --port 80"));
1028 console.log(winRemoveSingleQuotes(" MeshCtrl WebRelay --id 'deviceid' --type https --port 443"));
1029 console.log("\r\nRequired arguments:\r\n");
1030 if (process.platform == 'win32') {
1031 console.log(" --id [deviceid] - The device identifier.");
1033 console.log(" --id '[deviceid]' - The device identifier.");
1035 console.log(" --type [http,https] - Type of relay from remote device, http or https.");
1036 console.log("\r\nOptional arguments:\r\n");
1037 console.log(" --port [portnumber] - Set alternative port for http or https, default is 80 for http and 443 for https.");
1040 case 'deviceopenurl': {
1041 console.log("Open a web page on a remote device, Example usages:\r\n");
1042 console.log(winRemoveSingleQuotes(" MeshCtrl DeviceOpenUrl --id 'deviceid' --openurl http://meshcentral.com"));
1043 console.log("\r\nRequired arguments:\r\n");
1044 if (process.platform == 'win32') {
1045 console.log(" --id [deviceid] - The device identifier.");
1047 console.log(" --id '[deviceid]' - The device identifier.");
1049 console.log(" --openurl [url] - Link to the web page.");
1052 case 'devicemessage': {
1053 console.log("Display a message on the remote device, Example usages:\r\n");
1054 console.log(winRemoveSingleQuotes(" MeshCtrl DeviceMessage --id 'deviceid' --msg \"message\""));
1055 console.log(winRemoveSingleQuotes(" MeshCtrl DeviceMessage --id 'deviceid' --msg \"message\" --title \"title\""));
1056 console.log(winRemoveSingleQuotes(" MeshCtrl DeviceMessage --id 'deviceid' --msg \"message\" --title \"title\" --timeout 120000"));
1057 console.log("\r\nRequired arguments:\r\n");
1058 if (process.platform == 'win32') {
1059 console.log(" --id [deviceid] - The device identifier.");
1061 console.log(" --id '[deviceid]' - The device identifier.");
1063 console.log(" --msg [message] - The message to display.");
1064 console.log("\r\nOptional arguments:\r\n");
1065 console.log(" --title [title] - Messagebox title, default is \"MeshCentral\".");
1066 console.log(" --timeout [miliseconds] - After timeout messagebox vanishes, 0 keeps messagebox open until closed manually, default is 120000 (2 Minutes).");
1069 case 'devicetoast': {
1070 console.log("Display a toast message on the remote device, Example usages:\r\n");
1071 console.log(winRemoveSingleQuotes(" MeshCtrl DeviceToast --id 'deviceid' --msg \"message\""));
1072 console.log(winRemoveSingleQuotes(" MeshCtrl DeviceToast --id 'deviceid' --msg \"message\" --title \"title\""));
1073 console.log("\r\nRequired arguments:\r\n");
1074 if (process.platform == 'win32') {
1075 console.log(" --id [deviceid] - The device identifier.");
1077 console.log(" --id '[deviceid]' - The device identifier.");
1079 console.log(" --msg [message] - The message to display.");
1080 console.log("\r\nOptional arguments:\r\n");
1081 console.log(" --title [title] - Toast title, default is \"MeshCentral\".");
1084 case 'groupmessage': {
1085 console.log("Open a message box on remote devices in a specific device group, Example usages:\r\n");
1086 console.log(winRemoveSingleQuotes(" MeshCtrl GroupMessage --id 'devicegroupid' --msg \"message\""));
1087 console.log(winRemoveSingleQuotes(" MeshCtrl GroupMessage --id 'devicegroupid' --msg \"message\" --title \"title\""));
1088 console.log(winRemoveSingleQuotes(" MeshCtrl GroupMessage --id 'devicegroupid' --msg \"message\" --title \"title\" --timeout 120000"));
1089 console.log("\r\nRequired arguments:\r\n");
1090 if (process.platform == 'win32') {
1091 console.log(" --id [devicegroupid] - The device identifier.");
1093 console.log(" --id '[devicegroupid]' - The device identifier.");
1095 console.log(" --msg [message] - The message to display.");
1096 console.log("\r\nOptional arguments:\r\n");
1097 console.log(" --title [title] - Messagebox title, default is \"MeshCentral\".");
1098 console.log(" --timeout [miliseconds] - After timeout messagebox vanishes, 0 keeps messagebox open until closed manually, default is 120000 (2 Minutes).");
1101 case 'grouptoast': {
1102 console.log("Display a toast notification on remote devices in a specific device group, Example usages:\r\n");
1103 console.log(winRemoveSingleQuotes(" MeshCtrl GroupToast --id 'devicegroupid' --msg \"message\""));
1104 console.log(winRemoveSingleQuotes(" MeshCtrl GroupToast --id 'devicegroupid' --msg \"message\" --title \"title\""));
1105 console.log("\r\nRequired arguments:\r\n");
1106 if (process.platform == 'win32') {
1107 console.log(" --id [devicegroupid] - The device identifier.");
1109 console.log(" --id '[devicegroupid]' - The device identifier.");
1111 console.log(" --msg [message] - The message to display.");
1112 console.log("\r\nOptional arguments:\r\n");
1113 console.log(" --title [title] - Toast title, default is \"MeshCentral\".");
1117 console.log("Generate a CSV report, Example usages:\r\n");
1118 console.log(" MeshCtrl Report --type sessions --devicegroup mesh//...");
1119 console.log(" MeshCtrl Report --type traffic --json");
1120 console.log(" MeshCtrl Report --type logins --groupby day");
1121 console.log(" MeshCtrl Report --type db");
1122 console.log("\r\nOptional arguments:\r\n");
1123 console.log(" --start [yyyy-mm-ddThh:mm:ss] - Filter the results starting at that date. Defaults to last 24h and last week when used with --groupby day. Usable with sessions, traffic and logins");
1124 console.log(" --end [yyyy-mm-ddThh:mm:ss] - Filter the results ending at that date. Defaults to now. Usable with sessions, traffic and logins");
1125 console.log(" --groupby [name] - How to group results. Options: user, day, device. Defaults to user. User and day usable in sessions and logins, device usable in sessions.");
1126 console.log(" --devicegroup [devicegroupid] - Filter the results by device group. Usable in sessions");
1127 console.log(" --showtraffic - Add traffic data in sessions report");
1131 console.log("Get help on an action. Type:\r\n\r\n help [action]\r\n\r\nPossible actions are: " + possibleCommands.join(', ') + '.');
1140 if(args.loginpass===true){
1141 const readline = require('readline');
1142 const rl = readline.createInterface({
1143 input: process.stdin,
1144 output: process.stdout,
1147 process.stdout.write('Enter your password: ');
1148 const stdin = process.openStdin();
1149 stdin.setRawMode(true); // Set raw mode to prevent echoing of characters
1151 args.loginpass = '';
1152 process.stdin.on('data', (char) => {
1157 case '\u0004': // They've finished entering their password
1158 stdin.setRawMode(false);
1160 process.stdout.clearLine(); process.stdout.cursorTo(0);
1164 case '\u0003': // Ctrl+C
1165 process.stdout.write('\n');
1168 default: // Mask the password with "*"
1169 args.loginpass += char;
1170 process.stdout.clearLine(); process.stdout.cursorTo(0);
1171 process.stdout.write('Enter your password: ' + '*'.repeat(args.loginpass.length));
1181function displayConfigHelp() {
1182 console.log("Perform operations on the config.json file. Example usage:\r\n");
1183 console.log(" MeshCtrl config --show");
1184 console.log("\r\nOptional arguments:\r\n");
1185 console.log(" --show - Display the config.json file.");
1186 console.log(" --listdomains - Display non-default domains.");
1187 console.log(" --adddomain [domain] - Add a domain.");
1188 console.log(" --removedomain [domain] - Remove a domain.");
1189 console.log(" --settodomain [domain] - Set values to the domain.");
1190 console.log(" --removefromdomain [domain] - Remove values from the domain.");
1191 console.log("\r\nWith adddomain, removedomain, settodomain and removefromdomain you can add the key and value pair. For example:\r\n");
1192 console.log(" --adddomain \"MyDomain\" --title \"My Server Name\" --newAccounts false");
1193 console.log(" --settodomain \"MyDomain\" --themePack \"Stylish-UI\"");
1194 console.log(" --settodomain \"MyDomain\" --title \"My Server Name\"");
1195 console.log(" --removefromdomain \"MyDomain\" --title");
1198function performConfigOperations(args) {
1199 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', 'customFiles', 'themePack'];
1200 var domainObjectValues = ['ldapoptions', 'httpheaders', 'yubikey', 'passwordrequirements', 'limits', 'amtacmactivation', 'redirects', 'sessionrecording', 'customFiles'];
1201 var domainArrayValues = ['newaccountemaildomains', 'newaccountsrights', 'loginkey', 'agentconfig', 'themePack'];
1202 var configChange = false;
1203 var fs = require('fs');
1204 var path = require('path');
1205 var configFile = 'config.json';
1206 var didSomething = 0;
1207 if (fs.existsSync(configFile) == false) { configFile = path.join('meshcentral-data', 'config.json'); }
1208 if (fs.existsSync(configFile) == false) { configFile = path.join(__dirname, 'config.json'); }
1209 if (fs.existsSync(configFile) == false) { configFile = path.join(__dirname, 'meshcentral-data', 'config.json'); }
1210 if (fs.existsSync(configFile) == false) { configFile = path.join(__dirname, '..', 'meshcentral-data', 'config.json'); }
1211 if (fs.existsSync(configFile) == false) { configFile = path.join(__dirname, '..', '..', 'meshcentral-data', 'config.json'); }
1212 if (fs.existsSync(configFile) == false) { console.log("Unable to find config.json."); return; }
1214 try { config = fs.readFileSync(configFile).toString('utf8'); } catch (ex) { console.log("Error: Unable to read config.json"); return; }
1215 try { config = JSON.parse(fs.readFileSync(configFile)); } catch (e) { console.log('ERROR: Unable to parse ' + configFile + '.'); return null; }
1216 if (args.adddomain != null) {
1218 if (config.domains == null) { config.domains = {}; }
1219 if (config.domains[args.adddomain] != null) { console.log("Error: Domain \"" + args.adddomain + "\" already exists"); }
1221 configChange = true;
1222 config.domains[args.adddomain] = {};
1223 for (var i in args) {
1224 if (domainValues.indexOf(i.toLowerCase()) >= 0) {
1225 if (args[i] == 'true') { args[i] = true; } else if (args[i] == 'false') { args[i] = false; } else if (parseInt(args[i]) == args[i]) { args[i] = parseInt(args[i]); }
1226 config.domains[args.adddomain][i] = args[i];
1227 configChange = true;
1232 if (args.removedomain != null) {
1234 if (config.domains == null) { config.domains = {}; }
1235 if (config.domains[args.removedomain] == null) { console.log("Error: Domain \"" + args.removedomain + "\" does not exist"); }
1236 else { delete config.domains[args.removedomain]; configChange = true; }
1238 if (args.settodomain != null) {
1240 if (config.domains == null) { config.domains = {}; }
1241 if (args.settodomain == true) { args.settodomain = ''; }
1242 if (config.domains[args.settodomain] == null) { console.log("Error: Domain \"" + args.settodomain + "\" does not exist"); }
1244 for (var i in args) {
1245 if ((i == '_') || (i == 'settodomain')) continue;
1246 if (domainValues.indexOf(i.toLowerCase()) >= 0) {
1247 var isObj = (domainObjectValues.indexOf(i.toLowerCase()) >= 0);
1248 var isArr = (domainArrayValues.indexOf(i.toLowerCase()) >= 0);
1249 if ((isObj == false) && (isArr == false)) {
1251 if (args[i] == '') { delete config.domains[args.settodomain][i]; configChange = true; } else {
1252 if (args[i] == 'true') { args[i] = true; } else if (args[i] == 'false') { args[i] = false; } else if (parseInt(args[i]) == args[i]) { args[i] = parseInt(args[i]); }
1253 config.domains[args.settodomain][i] = args[i];
1254 configChange = true;
1256 } else if (isObj || isArr) {
1257 // Set an object/array value
1258 if (args[i] == '') { delete config.domains[args.settodomain][i]; configChange = true; } else {
1260 try { x = JSON.parse(args[i]); } catch (ex) { }
1261 if ((x == null) || (typeof x != 'object')) { console.log("Unable to parse JSON for " + i + "."); } else {
1262 if (isArr && Array.isArray(x) == false) {
1263 console.log("Value " + i + " must be an array.");
1264 } else if (!isArr && Array.isArray(x) == true) {
1265 console.log("Value " + i + " must be an object.");
1267 config.domains[args.settodomain][i] = x;
1268 configChange = true;
1274 console.log('Invalid configuration value: ' + i);
1279 if (args.removefromdomain != null) {
1281 if (config.domains == null) { config.domains = {}; }
1282 if (config.domains[args.removefromdomain] == null) { console.log("Error: Domain \"" + args.removefromdomain + "\" does not exist"); }
1283 else { for (var i in args) { if (domainValues.indexOf(i.toLowerCase()) >= 0) { delete config.domains[args.removefromdomain][i]; configChange = true; } } }
1286 try { fs.writeFileSync(configFile, JSON.stringify(config, null, 2)); } catch (ex) { console.log("Error: Unable to read config.json"); return; }
1288 if (args.show == 1) {
1289 console.log(JSON.stringify(config, null, 2)); return;
1290 } else if (args.listdomains == 1) {
1291 if (config.domains == null) {
1292 console.log('No domains found.'); return;
1294 // Show the list of active domains, skip the default one.
1295 for (var i in config.domains) { if ((i != '') && (i[0] != '_')) { console.log(i); } } return;
1298 if (didSomething == 0) {
1299 displayConfigHelp();
1301 console.log("Done.");
1306function onVerifyServer(clientName, certs) { return null; }
1308function serverConnect() {
1309 const WebSocket = require('ws');
1311 var url = 'wss://localhost/control.ashx';
1314 if (url.length < 5) { console.log("Invalid url."); process.exit(); return; }
1315 if ((url.startsWith('wss://') == false) && (url.startsWith('ws://') == false)) { console.log("Invalid url."); process.exit(); return; }
1316 var i = url.indexOf('?key='), loginKey = null;
1317 if (i >= 0) { loginKey = url.substring(i + 5); url = url.substring(0, i); }
1318 if (url.endsWith('/') == false) { url += '/'; }
1319 url += 'control.ashx';
1320 if (loginKey != null) { url += '?key=' + loginKey; }
1323 // TODO: checkServerIdentity does not work???
1324 var options = { rejectUnauthorized: false, checkServerIdentity: onVerifyServer }
1326 // Setup the HTTP proxy if needed
1327 if (args.proxy != null) {
1328 const HttpsProxyAgent = require('https-proxy-agent');
1329 options.agent = new HttpsProxyAgent(require('url').parse(args.proxy));
1332 // Password authentication
1333 if (args.loginpass != null) {
1334 var username = 'admin';
1335 if (args.loginuser != null) { username = args.loginuser; }
1337 if (args.token != null) { token = ',' + Buffer.from('' + args.token).toString('base64'); }
1338 options.headers = { 'x-meshauth': Buffer.from('' + username).toString('base64') + ',' + Buffer.from('' + args.loginpass).toString('base64') + token }
1341 // Cookie authentication
1342 var ckey = null, loginCookie = null;
1343 if (args.loginkey != null) {
1344 // User key passed in as argument hex
1345 if (args.loginkey.length != 160) { loginCookie = args.loginkey; }
1346 ckey = Buffer.from(args.loginkey, 'hex');
1347 if (ckey.length != 80) { ckey = null; loginCookie = args.loginkey; }
1348 } else if (args.loginkeyfile != null) {
1349 // Load key from hex file
1350 var fs = require('fs');
1352 var keydata = fs.readFileSync(args.loginkeyfile, 'utf8').split(' ').join('').split('\r').join('').split('\n').join('');
1353 ckey = Buffer.from(keydata, 'hex');
1354 if (ckey.length != 80) { ckey = null; loginCookie = args.loginkey; }
1355 } catch (ex) { console.log(ex.message); process.exit(); return; }
1358 settings.xxurl = url;
1360 var domainid = '', username = 'admin';
1361 if (args.logindomain != null) { domainid = args.logindomain; }
1362 if (args.loginuser != null) { username = args.loginuser; }
1363 url += (url.indexOf('?key=') >= 0 ? '&auth=' : '?auth=') + encodeCookie({ userid: 'user/' + domainid + '/' + username, domainid: domainid }, ckey);
1365 if (args.logindomain != null) { console.log("--logindomain can only be used along with --loginkey."); process.exit(); return; }
1366 if (loginCookie != null) { url += (url.indexOf('?key=') >= 0 ? '&auth=' : '?auth=') + loginCookie; }
1369 const ws = new WebSocket(url, options);
1370 //console.log('Connecting to ' + url);
1372 ws.on('open', function open() {
1373 //console.log('Connected.');
1374 switch (settings.cmd) {
1375 case 'serverinfo': { break; }
1376 case 'userinfo': { break; }
1377 case 'listusers': { ws.send(JSON.stringify({ action: 'users', responseid: 'meshctrl' })); break; }
1378 case 'listusersessions': { ws.send(JSON.stringify({ action: 'wssessioncount', responseid: 'meshctrl' })); break; }
1379 case 'removeallusersfromusergroup':
1380 case 'listusergroups': { ws.send(JSON.stringify({ action: 'usergroups', responseid: 'meshctrl' })); break; }
1381 case 'listdevicegroups': { ws.send(JSON.stringify({ action: 'meshes', responseid: 'meshctrl' })); break; }
1382 case 'listusersofdevicegroup': { ws.send(JSON.stringify({ action: 'meshes', responseid: 'meshctrl' })); break; }
1383 case 'listdevices': {
1385 // Get list of devices with lots of details
1386 ws.send(JSON.stringify({ action: 'getDeviceDetails', type: (args.csv) ? 'csv' : 'json' }));
1387 } else if (args.group) {
1388 ws.send(JSON.stringify({ action: 'nodes', meshname: args.group, responseid: 'meshctrl' }));
1389 } else if (args.id) {
1390 ws.send(JSON.stringify({ action: 'nodes', meshid: args.id, responseid: 'meshctrl' }));
1392 ws.send(JSON.stringify({ action: 'meshes' }));
1393 ws.send(JSON.stringify({ action: 'nodes', responseid: 'meshctrl' }));
1397 case 'listevents': {
1399 if (args.limit) { limit = parseInt(args.limit); }
1400 if ((typeof limit != 'number') || (limit < 1)) { limit = null; }
1404 cmd = { action: 'events', user: args.userid, responseid: 'meshctrl' };
1405 } else if (args.id) {
1406 cmd = { action: 'events', nodeid: args.id, responseid: 'meshctrl' };
1408 cmd = { action: 'events', responseid: 'meshctrl' };
1410 if (typeof limit == 'number') { cmd.limit = limit; }
1411 ws.send(JSON.stringify(cmd));
1414 case 'logintokens': {
1416 var cmd = { action: 'createLoginToken', name: args.add, expire: 0, responseid: 'meshctrl' };
1417 if (args.expire) { cmd.expire = parseInt(args.expire); }
1418 ws.send(JSON.stringify(cmd));
1420 var cmd = { action: 'loginTokens', responseid: 'meshctrl' };
1421 if (args.remove) { cmd.remove = [args.remove]; }
1422 ws.send(JSON.stringify(cmd));
1427 var siteadmin = getSiteAdminRights(args);
1428 if (args.randompass) { args.pass = getRandomAmtPassword(); }
1429 var op = { action: 'adduser', username: args.user, pass: args.pass, responseid: 'meshctrl' };
1430 if (args.email) { op.email = args.email; if (args.emailverified) { op.emailVerified = true; } }
1431 if (args.resetpass) { op.resetNextLogin = true; }
1432 if (siteadmin != -1) { op.siteadmin = siteadmin; }
1433 if (args.domain) { op.domain = args.domain; }
1434 if (args.phone === true) { op.phone = ''; }
1435 if (typeof args.phone == 'string') { op.phone = args.phone; }
1436 if (typeof args.realname == 'string') { op.realname = args.realname; }
1437 ws.send(JSON.stringify(op));
1441 var userid = args.userid;
1442 if ((args.domain != null) && (userid.indexOf('/') < 0)) { userid = 'user/' + args.domain + '/' + userid; }
1443 var siteadmin = getSiteAdminRights(args);
1444 var op = { action: 'edituser', userid: userid, responseid: 'meshctrl' };
1445 if (args.email) { op.email = args.email; if (args.emailverified) { op.emailVerified = true; } }
1446 if (args.resetpass) { op.resetNextLogin = true; }
1447 if (siteadmin != -1) { op.siteadmin = siteadmin; }
1448 if (args.domain) { op.domain = args.domain; }
1449 if (args.phone === true) { op.phone = ''; }
1450 if (typeof args.phone == 'string') { op.phone = args.phone; }
1451 if (typeof args.realname == 'string') { op.realname = args.realname; }
1452 if (args.realname === true) { op.realname = ''; }
1453 ws.send(JSON.stringify(op));
1456 case 'removeuser': {
1457 var userid = args.userid;
1458 if ((args.domain != null) && (userid.indexOf('/') < 0)) { userid = 'user/' + args.domain + '/' + userid; }
1459 ws.send(JSON.stringify({ action: 'deleteuser', userid: userid, responseid: 'meshctrl' }));
1462 case 'addusergroup': {
1463 var op = { action: 'createusergroup', name: args.name, desc: args.desc, responseid: 'meshctrl' };
1464 if (args.domain) { op.domain = args.domain; }
1465 ws.send(JSON.stringify(op));
1468 case 'removeusergroup': {
1469 var ugrpid = args.groupid;
1470 if ((args.domain != null) && (userid.indexOf('/') < 0)) { ugrpid = 'ugrp/' + args.domain + '/' + ugrpid; }
1471 ws.send(JSON.stringify({ action: 'deleteusergroup', ugrpid: ugrpid, responseid: 'meshctrl' }));
1474 case 'addtousergroup': {
1475 var ugrpid = args.groupid;
1476 if ((args.domain != null) && (userid.indexOf('/') < 0)) { ugrpid = 'ugrp/' + args.domain + '/' + ugrpid; }
1478 // Add a user to a user group
1479 if (args.userid != null) {
1480 var userid = args.userid;
1481 if ((args.domain != null) && (userid.indexOf('/') < 0)) { userid = 'user/' + args.domain + '/' + userid; }
1482 ws.send(JSON.stringify({ action: 'addusertousergroup', ugrpid: ugrpid, usernames: [userid.split('/')[2]], responseid: 'meshctrl' }));
1486 if ((args.id != null) && (args.id.startsWith('user/'))) {
1487 ws.send(JSON.stringify({ action: 'addusertousergroup', ugrpid: ugrpid, usernames: [args.id.split('/')[2]], responseid: 'meshctrl' }));
1492 if (args.rights != null) { rights = parseInt(args.rights); }
1494 // Add a device group to a user group
1495 if (args.meshid != null) {
1496 var meshid = args.meshid;
1497 if ((args.domain != null) && (userid.indexOf('/') < 0)) { meshid = 'mesh/' + args.domain + '/' + meshid; }
1498 ws.send(JSON.stringify({ action: 'addmeshuser', meshid: meshid, userid: ugrpid, meshadmin: rights, responseid: 'meshctrl' }));
1502 if ((args.id != null) && (args.id.startsWith('mesh/'))) {
1503 ws.send(JSON.stringify({ action: 'addmeshuser', meshid: args.id, userid: ugrpid, meshadmin: rights, responseid: 'meshctrl' }));
1507 // Add a device to a user group
1508 if (args.nodeid != null) {
1509 var nodeid = args.nodeid;
1510 if ((args.domain != null) && (userid.indexOf('/') < 0)) { nodeid = 'node/' + args.domain + '/' + nodeid; }
1511 ws.send(JSON.stringify({ action: 'adddeviceuser', nodeid: nodeid, userids: [ugrpid], rights: rights, responseid: 'meshctrl' }));
1515 if ((args.id != null) && (args.id.startsWith('node/'))) {
1516 ws.send(JSON.stringify({ action: 'adddeviceuser', nodeid: args.id, userids: [ugrpid], rights: rights, responseid: 'meshctrl' }));
1522 case 'removefromusergroup': {
1523 var ugrpid = args.groupid;
1524 if ((args.domain != null) && (userid.indexOf('/') < 0)) { ugrpid = 'ugrp/' + args.domain + '/' + ugrpid; }
1526 // Remove a user from a user group
1527 if (args.userid != null) {
1528 var userid = args.userid;
1529 if ((args.domain != null) && (userid.indexOf('/') < 0)) { userid = 'user/' + args.domain + '/' + userid; }
1530 ws.send(JSON.stringify({ action: 'removeuserfromusergroup', ugrpid: ugrpid, userid: userid, responseid: 'meshctrl' }));
1534 if ((args.id != null) && (args.id.startsWith('user/'))) {
1535 ws.send(JSON.stringify({ action: 'removeuserfromusergroup', ugrpid: ugrpid, userid: args.id, responseid: 'meshctrl' }));
1539 // Remove a device group from a user group
1540 if (args.meshid != null) {
1541 var meshid = args.meshid;
1542 if ((args.domain != null) && (userid.indexOf('/') < 0)) { meshid = 'mesh/' + args.domain + '/' + meshid; }
1543 ws.send(JSON.stringify({ action: 'removemeshuser', meshid: meshid, userid: ugrpid, responseid: 'meshctrl' }));
1547 if ((args.id != null) && (args.id.startsWith('mesh/'))) {
1548 ws.send(JSON.stringify({ action: 'removemeshuser', meshid: args.id, userid: ugrpid, responseid: 'meshctrl' }));
1552 // Remove a device from a user group
1553 if (args.nodeid != null) {
1554 var nodeid = args.nodeid;
1555 if ((args.domain != null) && (userid.indexOf('/') < 0)) { nodeid = 'node/' + args.domain + '/' + nodeid; }
1556 ws.send(JSON.stringify({ action: 'adddeviceuser', nodeid: nodeid, userids: [ugrpid], rights: 0, responseid: 'meshctrl', remove: true }));
1560 if ((args.id != null) && (args.id.startsWith('node/'))) {
1561 ws.send(JSON.stringify({ action: 'adddeviceuser', nodeid: args.id, userids: [ugrpid], rights: 0, responseid: 'meshctrl', remove: true }));
1567 case 'adddevicegroup': {
1568 var op = { action: 'createmesh', meshname: args.name, meshtype: 2, responseid: 'meshctrl' };
1569 if (args.desc) { op.desc = args.desc; }
1570 if (args.amtonly) { op.meshtype = 1; }
1571 if (args.agentless) { op.meshtype = 3; }
1572 if (args.features) { op.flags = parseInt(args.features); }
1573 if (args.consent) { op.consent = parseInt(args.consent); }
1574 ws.send(JSON.stringify(op));
1577 case 'removedevicegroup': {
1578 var op = { action: 'deletemesh', responseid: 'meshctrl' };
1579 if (args.id) { op.meshid = args.id; } else if (args.group) { op.meshname = args.group; }
1580 ws.send(JSON.stringify(op));
1583 case 'addamtdevice': {
1584 var op = { action: 'addamtdevice', amttls: 1, responseid: 'meshctrl' };
1585 if (args.id) { op.meshid = args.id; }
1586 if ((typeof args.devicename == 'string') && (args.devicename != '')) { op.devicename = args.devicename; }
1587 if ((typeof args.hostname == 'string') && (args.hostname != '')) { op.hostname = args.hostname; }
1588 if ((typeof args.user == 'string') && (args.user != '')) { op.amtusername = args.user; }
1589 if ((typeof args.pass == 'string') && (args.pass != '')) { op.amtpassword = args.pass; }
1590 if (args.notls) { op.amttls = 0; }
1591 ws.send(JSON.stringify(op));
1594 case 'addlocaldevice': {
1595 var op = { action: 'addlocaldevice', type: 4, responseid: 'meshctrl' };
1596 if (args.id) { op.meshid = args.id; }
1597 if ((typeof args.devicename == 'string') && (args.devicename != '')) { op.devicename = args.devicename; }
1598 if ((typeof args.hostname == 'string') && (args.hostname != '')) { op.hostname = args.hostname; }
1600 if ((typeof parseInt(args.type) != 'number') || isNaN(parseInt(args.type))) { console.log("Invalid type."); process.exit(1); return; }
1601 op.type = args.type;
1603 ws.send(JSON.stringify(op));
1606 case 'editdevicegroup': {
1607 var op = { action: 'editmesh', responseid: 'meshctrl' };
1608 if (args.id) { op.meshid = args.id; } else if (args.group) { op.meshidname = args.group; }
1609 if ((typeof args.name == 'string') && (args.name != '')) { op.meshname = args.name; }
1610 if (args.desc === true) { op.desc = ""; } else if (typeof args.desc == 'string') { op.desc = args.desc; }
1611 if (args.invitecodes === true) { op.invite = "*"; } else if (typeof args.invitecodes == 'string') {
1612 var invitecodes = args.invitecodes.split(','), invitecodes2 = [];
1613 for (var i in invitecodes) { if (invitecodes[i].length > 0) { invitecodes2.push(invitecodes[i]); } }
1614 if (invitecodes2.length > 0) {
1615 op.invite = { codes: invitecodes2, flags: 0 };
1616 if (args.backgroundonly === true) { op.invite.flags = 2; }
1617 else if (args.interactiveonly === true) { op.invite.flags = 1; }
1620 if (args.flags != null) {
1621 var flags = parseInt(args.flags);
1622 if (typeof flags == 'number') { op.flags = flags; }
1624 if (args.consent != null) {
1625 var consent = parseInt(args.consent);
1626 if (typeof consent == 'number') { op.consent = consent; }
1628 ws.send(JSON.stringify(op));
1631 case 'movetodevicegroup': {
1632 var op = { action: 'changeDeviceMesh', responseid: 'meshctrl', nodeids: [args.devid] };
1633 if (args.id) { op.meshid = args.id; } else if (args.group) { op.meshname = args.group; }
1634 ws.send(JSON.stringify(op));
1637 case 'addusertodevicegroup': {
1639 if (args.fullrights) { meshrights = 0xFFFFFFFF; }
1640 if (args.editgroup) { meshrights |= 1; }
1641 if (args.manageusers) { meshrights |= 2; }
1642 if (args.managedevices) { meshrights |= 4; }
1643 if (args.remotecontrol) { meshrights |= 8; }
1644 if (args.agentconsole) { meshrights |= 16; }
1645 if (args.serverfiles) { meshrights |= 32; }
1646 if (args.wakedevices) { meshrights |= 64; }
1647 if (args.notes) { meshrights |= 128; }
1648 if (args.desktopviewonly) { meshrights |= 256; }
1649 if (args.noterminal) { meshrights |= 512; }
1650 if (args.nofiles) { meshrights |= 1024; }
1651 if (args.noamt) { meshrights |= 2048; }
1652 if (args.limiteddesktop) { meshrights |= 4096; }
1653 if (args.limitedevents) { meshrights |= 8192; }
1654 if (args.chatnotify) { meshrights |= 16384; }
1655 if (args.uninstall) { meshrights |= 32768; }
1656 var op = { action: 'addmeshuser', usernames: [args.userid], meshadmin: meshrights, responseid: 'meshctrl' };
1657 if (args.id) { op.meshid = args.id; } else if (args.group) { op.meshname = args.group; }
1658 ws.send(JSON.stringify(op));
1661 case 'removeuserfromdevicegroup': {
1662 var op = { action: 'removemeshuser', userid: args.userid, responseid: 'meshctrl' };
1663 if (args.id) { op.meshid = args.id; } else if (args.group) { op.meshname = args.group; }
1664 ws.send(JSON.stringify(op));
1667 case 'addusertodevice': {
1669 if (args.fullrights) { meshrights = (8 + 16 + 32 + 64 + 128 + 16384 + 32768); }
1670 if (args.remotecontrol) { meshrights |= 8; }
1671 if (args.agentconsole) { meshrights |= 16; }
1672 if (args.serverfiles) { meshrights |= 32; }
1673 if (args.wakedevices) { meshrights |= 64; }
1674 if (args.notes) { meshrights |= 128; }
1675 if (args.desktopviewonly) { meshrights |= 256; }
1676 if (args.noterminal) { meshrights |= 512; }
1677 if (args.nofiles) { meshrights |= 1024; }
1678 if (args.noamt) { meshrights |= 2048; }
1679 if (args.limiteddesktop) { meshrights |= 4096; }
1680 if (args.limitedevents) { meshrights |= 8192; }
1681 if (args.chatnotify) { meshrights |= 16384; }
1682 if (args.uninstall) { meshrights |= 32768; }
1683 var op = { action: 'adddeviceuser', nodeid: args.id, usernames: [args.userid], rights: meshrights, responseid: 'meshctrl' };
1684 ws.send(JSON.stringify(op));
1687 case 'removeuserfromdevice': {
1688 var op = { action: 'adddeviceuser', nodeid: args.id, usernames: [args.userid], rights: 0, remove: true, responseid: 'meshctrl' };
1689 ws.send(JSON.stringify(op));
1692 case 'sendinviteemail': {
1693 var op = { action: 'inviteAgent', email: args.email, name: '', os: '0', responseid: 'meshctrl' }
1694 if (args.id) { op.meshid = args.id; } else if (args.group) { op.meshname = args.group; }
1695 if (args.name) { op.name = args.name; }
1696 if (args.message) { op.msg = args.message; }
1697 ws.send(JSON.stringify(op));
1700 case 'generateinvitelink': {
1701 var op = { action: 'createInviteLink', expire: args.hours, flags: 0, responseid: 'meshctrl' }
1702 if (args.id) { op.meshid = args.id; } else if (args.group) { op.meshname = args.group; }
1703 if (args.flags) { op.flags = args.flags; }
1704 ws.send(JSON.stringify(op));
1708 var op = { action: 'userbroadcast', msg: args.msg, responseid: 'meshctrl' };
1709 if (args.user) { op.userid = args.user; }
1710 ws.send(JSON.stringify(op));
1713 case 'showevents': {
1714 console.log('Connected. Press ctrl-c to end.');
1717 case 'deviceinfo': {
1718 settings.deviceinfocount = 4;
1719 ws.send(JSON.stringify({ action: 'nodes' }));
1720 ws.send(JSON.stringify({ action: 'getnetworkinfo', nodeid: args.id, responseid: 'meshctrl' }));
1721 ws.send(JSON.stringify({ action: 'lastconnect', nodeid: args.id, responseid: 'meshctrl' }));
1722 ws.send(JSON.stringify({ action: 'getsysinfo', nodeid: args.id, nodeinfo: true, responseid: 'meshctrl' }));
1725 case 'removedevice': {
1726 var op = { action: 'removedevices', nodeids: [ args.id ], responseid: 'meshctrl' };
1727 ws.send(JSON.stringify(op));
1730 case 'editdevice': {
1731 var op = { action: 'changedevice', nodeid: args.id, responseid: 'meshctrl' };
1732 if (typeof args.name == 'string') { op.name = args.name; }
1733 if (typeof args.name == 'number') { op.name = '' + args.name; }
1734 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; } }
1735 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; } }
1736 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; } }
1737 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; } }
1738 ws.send(JSON.stringify(op));
1741 case 'runcommand': {
1743 if (args.runasuser) { runAsUser = 1; } else if (args.runasuseronly) { runAsUser = 2; }
1745 if (args.reply) { reply = true; }
1746 ws.send(JSON.stringify({ action: 'runcommands', nodeids: [args.id], type: ((args.powershell) ? 2 : 0), cmds: args.run, responseid: 'meshctrl', runAsUser: runAsUser, reply: reply }));
1752 ws.send("{\"action\":\"authcookie\"}");
1755 case 'devicepower': {
1756 var nodes = args.id.split(',');
1759 ws.send(JSON.stringify({ action: 'wakedevices', nodeids: nodes, responseid: 'meshctrl' }));
1760 } else if (args.off) {
1761 // Power off operation
1762 ws.send(JSON.stringify({ action: 'poweraction', nodeids: nodes, actiontype: 2, responseid: 'meshctrl' }));
1763 } else if (args.reset) {
1765 ws.send(JSON.stringify({ action: 'poweraction', nodeids: nodes, actiontype: 3, responseid: 'meshctrl' }));
1766 } else if (args.sleep) {
1768 ws.send(JSON.stringify({ action: 'poweraction', nodeids: nodes, actiontype: 4, responseid: 'meshctrl' }));
1769 } else if (args.amton) {
1770 // Intel AMT Power on operation
1771 ws.send(JSON.stringify({ action: 'poweraction', nodeids: nodes, actiontype: 302, responseid: 'meshctrl' }));
1772 } else if (args.amtoff) {
1773 // Intel AMT Power off operation
1774 ws.send(JSON.stringify({ action: 'poweraction', nodeids: nodes, actiontype: 308, responseid: 'meshctrl' }));
1775 } else if (args.amtreset) {
1776 // Intel AMT Power reset operation
1777 ws.send(JSON.stringify({ action: 'poweraction', nodeids: nodes, actiontype: 310, responseid: 'meshctrl' }));
1779 console.log('No power operation specified.');
1784 case 'agentdownload': {
1785 // Download an agent
1786 var u = settings.xxurl.replace('wss://', 'https://').replace('/control.ashx', '/meshagents');
1787 if (u.indexOf('?') > 0) { u += '&'; } else { u += '?'; }
1788 u += 'id=' + args.type + '&meshid=' + args.id;
1789 if (args.installflags) {
1790 if ((typeof parseInt(args.installflags) != 'number') || isNaN(parseInt(args.installflags)) || (parseInt(args.installflags) < 0) || (parseInt(args.installflags) > 2)) { console.log("Invalid Installflags."); process.exit(1); return; }
1791 u += '&installflags=' + args.installflags;
1793 const options = { rejectUnauthorized: false, checkServerIdentity: onVerifyServer }
1794 const fs = require('fs');
1795 const https = require('https');
1796 var downloadSize = 0;
1797 const req = https.request(u, options, function (res) {
1798 if (res.statusCode != 200) {
1799 console.log('Download error, statusCode: ' + res.statusCode);
1802 // Agent the agent filename
1803 var agentFileName = 'meshagent';
1804 if ((res.headers) && (res.headers['content-disposition'] != null)) {
1805 var i = res.headers['content-disposition'].indexOf('filename=\"');
1807 agentFileName = res.headers['content-disposition'].substring(i + 10);
1808 i = agentFileName.indexOf('\"');
1809 if (i >= 0) { agentFileName = agentFileName.substring(0, i); }
1812 // Check if this file already exists
1813 if (fs.existsSync(agentFileName)) { console.log('File \"' + agentFileName + '\" already exists.'); process.exit(1); }
1814 var fd = fs.openSync(agentFileName, 'w'); // Open the file for writing
1815 res.on('data', function (d) {
1816 downloadSize += d.length;
1817 fs.writeSync(fd, d); // Save to file
1819 res.on('end', function (d) {
1820 fs.closeSync(fd); // Close file
1821 console.log('Downloaded ' + downloadSize + ' byte(s) to \"' + agentFileName + '\"');
1826 req.on('error', function (error) { console.error(error); process.exit(1); })
1831 var protocol = null;
1832 if (args.type != null) {
1833 if (args.type == 'http') {
1835 } else if (args.type == 'https') {
1838 console.log("Unknown protocol type: " + args.type); process.exit(1);
1842 if (typeof args.port == 'number') {
1843 if ((args.port < 1) || (args.port > 65535)) { console.log("Port number must be between 1 and 65535."); process.exit(1); }
1845 } else if (protocol == 1) {
1847 } else if (protocol == 2) {
1850 ws.send(JSON.stringify({ action: 'webrelay', nodeid: args.id, port: port, appid: protocol, responseid: 'meshctrl' }));
1853 case 'devicesharing': {
1855 if (args.add.length == 0) { console.log("Invalid guest name."); process.exit(1); }
1857 // Sharing type, desktop or terminal
1859 if (args.type != null) {
1860 var shareTypes = args.type.toLowerCase().split(',');
1861 for (var i in shareTypes) { if ((shareTypes[i] != 'terminal') && (shareTypes[i] != 'desktop') && (shareTypes[i] != 'files') && (shareTypes[i] != 'http') && (shareTypes[i] != 'https')) { console.log("Unknown sharing type: " + shareTypes[i]); process.exit(1); } }
1862 if (shareTypes.indexOf('terminal') >= 0) { p |= 1; }
1863 if (shareTypes.indexOf('desktop') >= 0) { p |= 2; }
1864 if (shareTypes.indexOf('files') >= 0) { p |= 4; }
1865 if (shareTypes.indexOf('http') >= 0) { p |= 8; }
1866 if (shareTypes.indexOf('https') >= 0) { p |= 16; }
1868 if (p == 0) { p = 2; } // Desktop
1870 // Sharing view only
1871 var viewOnly = false;
1872 if (args.viewonly) { viewOnly = true; }
1876 if (args.consent == null) {
1877 if ((p & 1) != 0) { consent = 0x0002; } // Terminal notify
1878 if ((p & 2) != 0) { consent = 0x0001; } // Desktop notify
1879 if ((p & 4) != 0) { consent = 0x0004; } // Files notify
1881 if (typeof args.consent == 'string') {
1882 var flagStrs = args.consent.split(',');
1883 for (var i in flagStrs) {
1884 var flagStr = flagStrs[i].toLowerCase();
1885 if (flagStr == 'none') { consent = 0; }
1886 else if (flagStr == 'notify') {
1887 if ((p & 1) != 0) { consent |= 0x0002; } // Terminal notify
1888 if ((p & 2) != 0) { consent |= 0x0001; } // Desktop notify
1889 if ((p & 4) != 0) { consent |= 0x0004; } // Files notify
1890 } else if (flagStr == 'prompt') {
1891 if ((p & 1) != 0) { consent |= 0x0010; } // Terminal prompt
1892 if ((p & 2) != 0) { consent |= 0x0008; } // Desktop prompt
1893 if ((p & 4) != 0) { consent |= 0x0020; } // Files prompt
1894 } else if (flagStr == 'bar') {
1895 if ((p & 2) != 0) { consent |= 0x0040; } // Desktop toolbar
1896 } else { console.log("Unknown consent type."); process.exit(1); return; }
1902 // Set Port Number if http or https
1903 if ((p & 8) || (p & 16)) {
1904 if (typeof args.port == 'number') {
1905 if ((args.port < 1) || (args.port > 65535)) { console.log("Port number must be between 1 and 65535."); process.exit(1); }
1907 } else if ((p & 8)) {
1909 } else if ((p & 16)) {
1914 // Start and end time
1915 var start = null, end = null;
1916 if (args.start) { start = Math.floor(Date.parse(args.start) / 1000); end = start + (60 * 60); }
1917 if (args.end) { if (start == null) { start = Math.floor(Date.now() / 1000) } end = Math.floor(Date.parse(args.end) / 1000); if (end <= start) { console.log("End time must be ahead of start time."); process.exit(1); return; } }
1918 if (args.duration) { if (start == null) { start = Math.floor(Date.now() / 1000) } end = start + parseInt(args.duration * 60); }
1922 if (args.daily) { recurring = 1; } else if (args.weekly) { recurring = 2; }
1923 if (recurring > 0) {
1924 if (args.end != null) { console.log("End time can't be specified for recurring shares, use --duration only."); process.exit(1); return; }
1925 if (args.duration == null) { args.duration = 60; } else { args.duration = parseInt(args.duration); }
1926 if (start == null) { start = Math.floor(Date.now() / 1000) }
1927 if ((typeof args.duration != 'number') || (args.duration < 1)) { console.log("Invalid duration value."); process.exit(1); return; }
1929 // Recurring sharing
1930 ws.send(JSON.stringify({ action: 'createDeviceShareLink', nodeid: args.id, guestname: args.add, p: p, consent: consent, start: start, expire: args.duration, recurring: recurring, viewOnly: viewOnly, port: port, responseid: 'meshctrl' }));
1932 if ((start == null) && (end == null)) {
1933 // Unlimited sharing
1934 ws.send(JSON.stringify({ action: 'createDeviceShareLink', nodeid: args.id, guestname: args.add, p: p, consent: consent, expire: 0, viewOnly: viewOnly, port: port, responseid: 'meshctrl' }));
1936 // Time limited sharing
1937 ws.send(JSON.stringify({ action: 'createDeviceShareLink', nodeid: args.id, guestname: args.add, p: p, consent: consent, start: start, end: end, viewOnly: viewOnly, port: port, responseid: 'meshctrl' }));
1940 } else if (args.remove) {
1941 ws.send(JSON.stringify({ action: 'removeDeviceShare', nodeid: args.id, publicid: args.remove, responseid: 'meshctrl' }));
1943 ws.send(JSON.stringify({ action: 'deviceShares', nodeid: args.id, responseid: 'meshctrl' }));
1947 case 'deviceopenurl': {
1948 ws.send(JSON.stringify({ action: 'msg', type: 'openUrl', nodeid: args.id, url: args.openurl, responseid: 'meshctrl' }));
1951 case 'devicemessage': {
1952 ws.send(JSON.stringify({ action: 'msg', type: 'messagebox', nodeid: args.id, title: args.title ? args.title : "MeshCentral", msg: args.msg, timeout: args.timeout ? args.timeout : 120000, responseid: 'meshctrl' }));
1955 case 'devicetoast': {
1956 ws.send(JSON.stringify({ action: 'toast', nodeids: [args.id], title: args.title ? args.title : "MeshCentral", msg: args.msg, responseid: 'meshctrl' }));
1959 case 'groupmessage': {
1960 ws.send(JSON.stringify({ action: 'nodes', meshid: args.id, responseid: 'meshctrl' }));
1963 case 'grouptoast': {
1964 ws.send(JSON.stringify({ action: 'nodes', meshid: args.id, responseid: 'meshctrl' }));
1981 var reportgroupby = 1;
1983 reportgroupby = args.groupby === 'device' ? 2 : args.groupby === 'day' ? 3: 1;
1986 var start = null, end = null;
1988 start = Math.floor(Date.parse(args.start) / 1000);
1990 start = reportgroupby === 3 ? Math.round(new Date().getTime() / 1000) - (168 * 3600) : Math.round(new Date().getTime() / 1000) - (24 * 3600);
1993 end = Math.floor(Date.parse(args.end) / 1000);
1995 end = Math.round(new Date().getTime() / 1000);
1997 if (end <= start) { console.log("End time must be ahead of start time."); process.exit(1); return; }
1999 ws.send(JSON.stringify({ action: 'report', type: reporttype, groupBy: reportgroupby, devGroup: args.devicegroup || null, start, end, tz: Intl.DateTimeFormat().resolvedOptions().timeZone, tf: new Date().getTimezoneOffset(), showTraffic: args.hasOwnProperty('showtraffic'), l: 'en', responseid: 'meshctrl' }));
2005 function getSiteAdminRights(args) {
2007 if (typeof args.rights == 'number') {
2008 siteadmin = args.rights;
2009 } else if (typeof args.rights == 'string') {
2011 var srights = args.rights.toLowerCase().split(',');
2012 if (srights.indexOf('full') != -1) { siteadmin = 0xFFFFFFFF; }
2013 if (srights.indexOf('none') != -1) { siteadmin = 0x00000000; }
2014 if (srights.indexOf('backup') != -1 || srights.indexOf('serverbackup') != -1) { siteadmin |= 0x00000001; }
2015 if (srights.indexOf('manageusers') != -1) { siteadmin |= 0x00000002; }
2016 if (srights.indexOf('restore') != -1 || srights.indexOf('serverrestore') != -1) { siteadmin |= 0x00000004; }
2017 if (srights.indexOf('fileaccess') != -1) { siteadmin |= 0x00000008; }
2018 if (srights.indexOf('update') != -1 || srights.indexOf('serverupdate') != -1) { siteadmin |= 0x00000010; }
2019 if (srights.indexOf('locked') != -1) { siteadmin |= 0x00000020; }
2020 if (srights.indexOf('nonewgroups') != -1) { siteadmin |= 0x00000040; }
2021 if (srights.indexOf('notools') != -1) { siteadmin |= 0x00000080; }
2022 if (srights.indexOf('usergroups') != -1) { siteadmin |= 0x00000100; }
2023 if (srights.indexOf('recordings') != -1) { siteadmin |= 0x00000200; }
2024 if (srights.indexOf('locksettings') != -1) { siteadmin |= 0x00000400; }
2025 if (srights.indexOf('allevents') != -1) { siteadmin |= 0x00000800; }
2026 if (srights.indexOf('nonewdevices') != -1) { siteadmin |= 0x00001000; }
2029 if (args.siteadmin) { siteadmin = 0xFFFFFFFF; }
2030 if (args.manageusers) { if (siteadmin == -1) { siteadmin = 0; } siteadmin |= 2; }
2031 if (args.fileaccess) { if (siteadmin == -1) { siteadmin = 0; } siteadmin |= 8; }
2032 if (args.serverupdate) { if (siteadmin == -1) { siteadmin = 0; } siteadmin |= 16; }
2033 if (args.locked) { if (siteadmin == -1) { siteadmin = 0; } siteadmin |= 32; }
2034 if (args.nonewgroups) { if (siteadmin == -1) { siteadmin = 0; } siteadmin |= 64; }
2035 if (args.notools) { if (siteadmin == -1) { siteadmin = 0; } siteadmin |= 128; }
2039 ws.on('close', function () { process.exit(); });
2040 ws.on('error', function (err) {
2041 if (err.code == 'ENOTFOUND') { console.log('Unable to resolve ' + url); }
2042 else if (err.code == 'ECONNREFUSED') { console.log('Unable to connect to ' + url); }
2043 else { console.log('Unable to connect to ' + url); }
2047 ws.on('message', function incoming(rawdata) {
2049 try { data = JSON.parse(rawdata); } catch (ex) { }
2050 if (data == null) { console.log('Unable to parse data: ' + rawdata); }
2051 if (settings.cmd == 'showevents') {
2052 if (args.filter == null) {
2053 // Display all events
2054 console.log(JSON.stringify(data, null, 2));
2056 // Display select events
2057 var filters = args.filter.split(',');
2058 if (typeof data.event == 'object') {
2059 if (filters.indexOf(data.event.action) >= 0) { console.log(JSON.stringify(data, null, 2) + '\r\n'); }
2061 if (filters.indexOf(data.action) >= 0) { console.log(JSON.stringify(data, null, 2) + '\r\n'); }
2066 switch (data.action) {
2067 case 'serverinfo': { // SERVERINFO
2068 settings.currentDomain = data.serverinfo.domain;
2069 if (settings.cmd == 'serverinfo') {
2071 console.log(JSON.stringify(data.serverinfo, ' ', 2));
2073 for (var i in data.serverinfo) { console.log(i + ':', data.serverinfo[i]); }
2080 if (settings.cmd == 'listevents') {
2083 console.log(JSON.stringify(data.events));
2084 } else if (args.json) {
2086 console.log(JSON.stringify(data.events, null, 2));
2088 if ((args.id == null) && (args.userid == null)) {
2090 console.log("time,type,action,nodeid,userid,msg");
2091 for (var i in data.events) {
2093 x.push(data.events[i].time);
2094 x.push(data.events[i].etype);
2095 x.push(data.events[i].action);
2096 x.push(data.events[i].nodeid);
2097 x.push(data.events[i].userid);
2098 x.push(data.events[i].msg);
2099 console.log(csvFormatArray(x));
2101 } else if (args.id != null) {
2103 console.log("time,type,action,userid,msg");
2104 for (var i in data.events) {
2106 x.push(data.events[i].time);
2107 x.push(data.events[i].etype);
2108 x.push(data.events[i].action);
2109 x.push(data.events[i].userid);
2110 x.push(data.events[i].msg);
2111 console.log(csvFormatArray(x));
2113 } else if (args.userid != null) {
2115 console.log("time,type,action,nodeid,msg");
2116 for (var i in data.events) {
2118 x.push(data.events[i].time);
2119 x.push(data.events[i].etype);
2120 x.push(data.events[i].action);
2121 x.push(data.events[i].nodeid);
2122 x.push(data.events[i].msg);
2123 console.log(csvFormatArray(x));
2131 case 'authcookie': { // SHELL, UPLOAD, DOWNLOAD
2132 if ((settings.cmd == 'shell') || (settings.cmd == 'upload') || (settings.cmd == 'download')) {
2133 var protocol = 1; // Terminal
2134 if ((settings.cmd == 'upload') || (settings.cmd == 'download')) { protocol = 5; } // Files
2135 if ((args.id.split('/') != 3) && (settings.currentDomain != null)) { args.id = 'node/' + settings.currentDomain + '/' + args.id; }
2136 var id = getRandomHex(6);
2137 ws.send(JSON.stringify({ action: 'msg', nodeid: args.id, type: 'tunnel', usage: 1, value: '*/meshrelay.ashx?p=' + protocol + '&nodeid=' + args.id + '&id=' + id + '&rauth=' + data.rcookie, responseid: 'meshctrl' }));
2138 connectTunnel(url.replace('/control.ashx', '/meshrelay.ashx?browser=1&p=' + protocol + '&nodeid=' + encodeURIComponent(args.id) + '&id=' + id + '&auth=' + data.cookie));
2142 case 'deviceShares': { // DEVICESHARING
2143 if (data.result != null) {
2144 console.log(data.result);
2146 if ((data.deviceShares == null) || (data.deviceShares.length == 0)) {
2147 console.log('No device sharing links for this device.');
2150 console.log(data.deviceShares);
2152 for (var i in data.deviceShares) {
2153 var share = data.deviceShares[i];
2155 if ((share.p & 1) != 0) { shareType.push("Terminal"); }
2156 if ((share.p & 2) != 0) { if (share.viewOnly) { shareType.push("View Only Desktop"); } else { shareType.push("Desktop"); } }
2157 if ((share.p & 4) != 0) { shareType.push("Files"); }
2158 shareType = shareType.join(' + ');
2159 if (shareType == '') { shareType = "Unknown"; }
2161 if ((share.consent & 0x0001) != 0) { consent.push("Desktop Notify"); }
2162 if ((share.consent & 0x0008) != 0) { consent.push("Desktop Prompt"); }
2163 if ((share.consent & 0x0040) != 0) { consent.push("Desktop Connection Toolbar"); }
2164 if ((share.consent & 0x0002) != 0) { consent.push("Terminal Notify"); }
2165 if ((share.consent & 0x0010) != 0) { consent.push("Terminal Prompt"); }
2166 if ((share.consent & 0x0004) != 0) { consent.push("Files Notify"); }
2167 if ((share.consent & 0x0020) != 0) { consent.push("Files Prompt"); }
2168 console.log('----------');
2169 console.log('Identifier: ' + share.publicid);
2170 console.log('Type: ' + shareType);
2171 console.log('UserId: ' + share.userid);
2172 console.log('Guest Name: ' + share.guestName);
2173 console.log('User Consent: ' + consent.join(', '));
2174 if (share.startTime) { console.log('Start Time: ' + new Date(share.startTime).toLocaleString()); }
2175 if (share.expireTime) { console.log('Expire Time: ' + new Date(share.expireTime).toLocaleString()); }
2176 if (share.duration) { console.log('Duration: ' + share.duration + ' minute' + ((share.duration > 1) ? 's' : '')); }
2177 if (share.recurring == 1) { console.log('Recurring: ' + 'Daily'); }
2178 if (share.recurring == 2) { console.log('Recurring: ' + 'Weekly'); }
2179 console.log('URL: ' + share.url);
2187 case 'userinfo': { // USERINFO
2188 if (settings.cmd == 'userinfo') {
2190 console.log(JSON.stringify(data.userinfo, ' ', 2));
2192 for (var i in data.userinfo) { console.log(i + ':', data.userinfo[i]); }
2198 case 'getsysinfo': { // DEVICEINFO
2199 if (settings.cmd == 'deviceinfo') {
2200 settings.sysinfo = (data.result) ? null : data;
2201 if (--settings.deviceinfocount == 0) { displayDeviceInfo(settings.sysinfo, settings.lastconnect, settings.networking, settings.nodes); process.exit(); }
2205 case 'lastconnect': {
2206 if (settings.cmd == 'deviceinfo') {
2207 settings.lastconnect = (data.result) ? null : data;
2208 if (--settings.deviceinfocount == 0) { displayDeviceInfo(settings.sysinfo, settings.lastconnect, settings.networking, settings.nodes); process.exit(); }
2212 case 'getnetworkinfo': {
2213 if (settings.cmd == 'deviceinfo') {
2214 settings.networking = (data.result) ? null : data;
2215 if (--settings.deviceinfocount == 0) { displayDeviceInfo(settings.sysinfo, settings.lastconnect, settings.networking, settings.nodes); process.exit(); }
2219 case 'msg': // SHELL
2220 case 'toast': // TOAST
2221 case 'adduser': // ADDUSER
2222 case 'edituser': // EDITUSER
2223 case 'addamtdevice': // ADDAMTDEVICE
2224 case 'addlocaldevice': // ADDLOCALDEVICE
2225 case 'removedevices': // REMOVEDEVICE
2226 case 'changedevice': // EDITDEVICE
2227 case 'deleteuser': // REMOVEUSER
2228 case 'createmesh': // ADDDEVICEGROUP
2229 case 'deletemesh': // REMOVEDEVICEGROUP
2230 case 'editmesh': // EDITDEVICEGROUP
2232 case 'changeDeviceMesh':
2233 case 'addmeshuser': //
2234 case 'removemeshuser': //
2235 case 'wakedevices': //
2236 case 'inviteAgent': //
2237 case 'adddeviceuser': //
2238 case 'createusergroup': //
2239 case 'deleteusergroup': //
2242 case 'addusertousergroup':
2243 case 'removeuserfromusergroup':
2244 case 'removeDeviceShare':
2245 case 'userbroadcast': { // BROADCAST
2246 if (((settings.cmd == 'shell') || (settings.cmd == 'upload') || (settings.cmd == 'download')) && (data.result == 'OK')) return;
2247 if ((data.type == 'runcommands') && (settings.cmd != 'runcommand')) return;
2248 if ((settings.multiresponse != null) && (settings.multiresponse > 1)) { settings.multiresponse--; break; }
2249 if (data.responseid == 'meshctrl') {
2250 if (data.meshid) { console.log(data.result, data.meshid); }
2251 else if (data.userid) { console.log(data.result, data.userid); }
2252 else console.log(data.result);
2257 case 'createDeviceShareLink':
2259 if (data.result == 'OK') {
2260 if (data.publicid) { console.log('ID: ' + data.publicid); }
2261 console.log('URL: ' + data.url);
2263 console.log(data.result);
2267 case 'createInviteLink':
2268 if (data.responseid == 'meshctrl') {
2269 if (data.url) { console.log(data.url); }
2270 else console.log(data.result);
2274 case 'wssessioncount': { // LIST USER SESSIONS
2276 console.log(JSON.stringify(data.wssessions, ' ', 2));
2278 for (var i in data.wssessions) { console.log(i + ', ' + ((data.wssessions[i] > 1) ? (data.wssessions[i] + ' sessions.') : ("1 session."))); }
2283 case 'usergroups': { // LIST USER GROUPS
2284 if (settings.cmd == 'listusergroups') {
2286 console.log(JSON.stringify(data.ugroups, ' ', 2));
2288 for (var i in data.ugroups) {
2289 var x = i + ', ' + data.ugroups[i].name;
2290 if (data.ugroups[i].desc && (data.ugroups[i].desc != '')) { x += ', ' + data.ugroups[i].desc; }
2292 var mesh = [], user = [], node = [];
2293 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); } } }
2294 console.log(' Users:');
2295 if (user.length > 0) { for (var j in user) { console.log(' ' + user[j]); } } else { console.log(' (None)'); }
2296 console.log(' Device Groups:');
2297 if (mesh.length > 0) { for (var j in mesh) { console.log(' ' + mesh[j] + ', ' + data.ugroups[i].links[mesh[j]].rights); } } else { console.log(' (None)'); }
2298 console.log(' Devices:');
2299 if (node.length > 0) { for (var j in node) { console.log(' ' + node[j] + ', ' + data.ugroups[i].links[node[j]].rights); } } else { console.log(' (None)'); }
2303 } else if (settings.cmd == 'removeallusersfromusergroup') {
2304 var ugrpid = args.groupid, exit = false;
2305 if ((args.domain != null) && (userid.indexOf('/') < 0)) { ugrpid = 'ugrp/' + args.domain + '/' + ugrpid; }
2306 var ugroup = data.ugroups[ugrpid];
2307 if (ugroup == null) {
2308 console.log('User group not found.');
2313 for (var i in ugroup.links) {
2314 if (i.startsWith('user/')) {
2316 ws.send(JSON.stringify({ action: 'removeuserfromusergroup', ugrpid: ugrpid, userid: i, responseid: 'meshctrl' }));
2317 console.log('Removing ' + i);
2321 if (usercount == 0) { console.log('No users in this user group.'); exit = true; } else { settings.multiresponse = usercount; }
2323 if (exit) { process.exit(); }
2327 case 'users': { // LISTUSERS
2328 if (data.result) { console.log(data.result); process.exit(); return; }
2330 // Filter the list of users
2331 var filters = args.filter.toLowerCase().split(',');
2332 var filteredusers = [];
2333 for (var i in data.users) {
2335 if ((filters.indexOf('2fa') >= 0) && ((data.users[i].otphkeys != null) || (data.users[i].otpkeys != null) || (data.users[i].otpsecret != null))) { ok = true; }
2336 if ((filters.indexOf('no2fa') >= 0) && ((data.users[i].otphkeys == null) && (data.users[i].otpkeys == null) && (data.users[i].otpsecret == null))) { ok = true; }
2337 if (ok == true) { filteredusers.push(data.users[i]); }
2339 data.users = filteredusers;
2342 console.log(JSON.stringify(data.users, ' ', 2));
2344 if (args.idexists) { for (var i in data.users) { const u = data.users[i]; if ((u._id == args.idexists) || (u._id.split('/')[2] == args.idexists)) { console.log('1'); process.exit(); return; } } console.log('0'); process.exit(); return; }
2345 if (args.nameexists) { for (var i in data.users) { const u = data.users[i]; if (u.name == args.nameexists) { console.log(u._id); process.exit(); return; } } process.exit(); return; }
2347 console.log('id, name, email\r\n---------------');
2348 for (var i in data.users) {
2349 const u = data.users[i];
2350 var t = "\"" + u._id.split('/')[2] + "\", \"" + u.name + "\"";
2351 if (u.email != null) { t += ", \"" + u.email + "\""; }
2359 if (settings.cmd == 'deviceinfo') {
2360 settings.nodes = (data.result) ? null : data;
2361 if (--settings.deviceinfocount == 0) { displayDeviceInfo(settings.sysinfo, settings.lastconnect, settings.networking, settings.nodes); process.exit(); }
2363 if ((settings.cmd == 'listdevices') && (data.responseid == 'meshctrl')) {
2364 if ((data.result != null) && (data.result != 'ok')) {
2365 console.log(data.result);
2367 // Filter devices based on device id.
2368 if (args.filterid) {
2369 var filteridSplit = args.filterid.split(','), filters = [];
2370 for (var i in filteridSplit) {
2371 var f = filteridSplit[i].trim();
2372 var g = f.split('/'); // If there is any / in the id, just grab the last part.
2373 if (g.length > 0) { f = g[g.length - 1]; }
2374 if (f != '') { filters.push(f); }
2376 if (filters.length > 0) {
2377 for (var mid in data.nodes) {
2378 var filteredNodes = [];
2379 for (var nid in data.nodes[mid]) {
2380 var n = data.nodes[mid][nid], match = false;
2381 for (var f in filters) { if (n._id.indexOf(filters[f]) >= 0) { match = true; } }
2382 if (match) { filteredNodes.push(n); }
2384 data.nodes[mid] = filteredNodes;
2389 // Filter devices based on filter string
2390 if (args.filter != null) {
2391 for (var meshid in data.nodes) {
2392 for (var d in data.nodes[meshid]) { data.nodes[meshid][d].meshid = meshid; }
2393 data.nodes[meshid] = parseSearchOrInput(data.nodes[meshid], args.filter.toString().toLowerCase());
2398 // Return a flat list
2400 for (var i in data.nodes) {
2401 var devicesInMesh = data.nodes[i];
2402 for (var j in devicesInMesh) {
2403 var n = devicesInMesh[j];
2405 if (settings.xmeshes && settings.xmeshes[i]) {
2406 console.log('\"' + settings.xmeshes[i]._id.split('/')[2] + '\",\"' + settings.xmeshes[i].name.split('\"').join('') + '\",\"' + n._id.split('/')[2] + '\",\"' + n.name.split('\"').join('') + '\",' + (n.icon ? n.icon : 0) + ',' + (n.conn ? n.conn : 0) + ',' + (n.pwr ? n.pwr : 0));
2408 console.log('\"\",\"\",\"' + n._id.split('/')[2] + '\",\"' + n.name.split('\"').join('') + '\",' + (n.icon ? n.icon : 0) + ',' + (n.conn ? n.conn : 0) + ',' + (n.pwr ? n.pwr : 0));
2412 if (nodecount == 0) { console.log('None'); }
2413 } else if (args.count) {
2414 // Return how many devices are in this group
2416 for (var i in data.nodes) { var devicesInMesh = data.nodes[i]; for (var j in devicesInMesh) { nodes.push(devicesInMesh[j]); } }
2417 console.log(nodes.length);
2418 } else if (args.json) {
2419 // Return all devices in JSON format
2422 for (var i in data.nodes) {
2423 const devicesInMesh = data.nodes[i];
2424 for (var j in devicesInMesh) {
2425 devicesInMesh[j].meshid = i; // Add device group id
2426 if (settings.xmeshes && settings.xmeshes[i] && settings.xmeshes[i].name) { devicesInMesh[j].groupname = settings.xmeshes[i].name; } // Add device group name
2427 nodes.push(devicesInMesh[j]);
2430 console.log(JSON.stringify(nodes, ' ', 2));
2432 // Display the list of nodes in text format
2434 for (var i in data.nodes) {
2435 var devicesInMesh = data.nodes[i];
2436 if (devicesInMesh.length > 0) {
2437 if (settings.xmeshes && settings.xmeshes[i] && settings.xmeshes[i].name) { console.log('\r\nDevice group: \"' + settings.xmeshes[i].name.split('\"').join('') + '\"'); }
2438 console.log('id, name, icon, conn, pwr\r\n-------------------------');
2439 for (var j in devicesInMesh) {
2440 var n = devicesInMesh[j];
2442 console.log('\"' + n._id.split('/')[2] + '\", \"' + n.name.split('\"').join('') + '\", ' + (n.icon ? n.icon : 0) + ', ' + (n.conn ? n.conn : 0) + ', ' + (n.pwr ? n.pwr : 0));
2446 if (nodecount == 0) { console.log('None'); }
2451 if ((settings.cmd == 'groupmessage') && (data.responseid == 'meshctrl')) {
2452 if ((data.nodes != null)) {
2453 for (var i in data.nodes) {
2454 for (let index = 0; index < data.nodes[i].length; index++) {
2455 const element = data.nodes[i][index];
2456 ws.send(JSON.stringify({ action: 'msg', type: 'messagebox', nodeid: element._id, title: args.title ? args.title : "MeshCentral", msg: args.msg, timeout: args.timeout ? args.timeout : 120000 }));
2461 setTimeout(function(){ console.log('ok'); process.exit(); }, 1000);
2463 if ((settings.cmd == 'grouptoast') && (data.responseid == 'meshctrl')) {
2464 if (data.nodes != null) {
2465 for (var i in data.nodes) {
2467 for (let index = 0; index < data.nodes[i].length; index++) {
2468 const element = data.nodes[i][index];
2469 nodes.push(element._id);
2471 ws.send(JSON.stringify({ action: 'toast', nodeids: nodes, title: args.title ? args.title : "MeshCentral", msg: args.msg, responseid: 'meshctrl' }));
2477 case 'meshes': { // LISTDEVICEGROUPS
2478 if (settings.cmd == 'listdevices') {
2479 // Store the list of device groups for later use
2480 settings.xmeshes = {}
2481 for (var i in data.meshes) { settings.xmeshes[data.meshes[i]._id] = data.meshes[i]; }
2482 } else if (settings.cmd == 'listdevicegroups') {
2484 // If asked, add the MeshID hex encoding to the JSON.
2485 if (args.hex) { for (var i in data.meshes) { data.meshes[i]._idhex = '0x' + Buffer.from(data.meshes[i]._id.split('/')[2].replace(/\@/g, '+').replace(/\$/g, '/'), 'base64').toString('hex').toUpperCase(); } }
2486 console.log(JSON.stringify(data.meshes, ' ', 2));
2488 if (args.idexists) { for (var i in data.meshes) { const u = data.meshes[i]; if ((u._id == args.idexists) || (u._id.split('/')[2] == args.idexists)) { console.log('1'); process.exit(); return; } } console.log('0'); process.exit(); return; }
2489 if (args.nameexists) { for (var i in data.meshes) { const u = data.meshes[i]; if (u.name == args.nameexists) { console.log(u._id); process.exit(); return; } } process.exit(); return; }
2491 console.log('id, name\r\n---------------');
2492 for (var i in data.meshes) {
2493 const m = data.meshes[i];
2494 var mid = m._id.split('/')[2];
2495 if (args.hex) { mid = '0x' + Buffer.from(mid.replace(/\@/g, '+').replace(/\$/g, '/'), 'base64').toString('hex').toUpperCase(); }
2496 var t = "\"" + mid + "\", \"" + m.name + "\"";
2501 } else if (settings.cmd == 'listusersofdevicegroup') {
2502 for (var i in data.meshes) {
2503 const m = data.meshes[i];
2504 var mid = m._id.split('/')[2];
2505 if (mid == args.id) {
2507 console.log(JSON.stringify(m.links, ' ', 2));
2509 console.log('userid, rights\r\n---------------');
2510 for (var l in m.links) {
2511 var rights = m.links[l].rights;
2513 if (rights == 4294967295) { rightsstr = ['FullAdministrator']; } else {
2514 if (rights & 1) { rightsstr.push('EditMesh'); }
2515 if (rights & 2) { rightsstr.push('ManageUsers'); }
2516 if (rights & 4) { rightsstr.push('ManageComputers'); }
2517 if (rights & 8) { rightsstr.push('RemoteControl'); }
2518 if (rights & 16) { rightsstr.push('AgentConsole'); }
2519 if (rights & 32) { rightsstr.push('ServerFiles'); }
2520 if (rights & 64) { rightsstr.push('WakeDevice'); }
2521 if (rights & 128) { rightsstr.push('SetNotes'); }
2522 if (rights & 256) { rightsstr.push('RemoteViewOnly'); }
2523 if (rights & 512) { rightsstr.push('NoTerminal'); }
2524 if (rights & 1024) { rightsstr.push('NoFiles'); }
2525 if (rights & 2048) { rightsstr.push('NoAMT'); }
2526 if (rights & 4096) { rightsstr.push('DesktopLimitedInput'); }
2528 console.log(l.split('/')[2] + ', ' + rightsstr.join(', '));
2535 console.log('Group id not found');
2541 if (data.cause == 'noauth') {
2542 if (data.msg == 'tokenrequired') {
2543 console.log('Authentication token required, use --token [number].');
2544 } else if (data.msg == 'nokey') {
2545 console.log('URL key is invalid or missing, please specify ?key=xxx in url');
2547 if ((args.loginkeyfile != null) || (args.loginkey != null)) {
2548 console.log('Invalid login, check the login key and that this computer has the correct time.');
2550 console.log('Invalid login.');
2557 case 'createLoginToken': {
2558 if (data.result != null) {
2559 console.log(data.result);
2565 console.log("New login token created.");
2566 if (data.name) { console.log("Token name: " + data.name); }
2567 if (data.created) { console.log("Created: " + new Date(data.created).toLocaleString()); }
2568 if (data.expire) { console.log("Expire: " + new Date(data.expire).toLocaleString()); }
2569 if (data.tokenUser) { console.log("Username: " + data.tokenUser); }
2570 if (data.tokenPass) { console.log("Password: " + data.tokenPass); }
2576 case 'loginTokens': {
2578 console.log(data.loginTokens);
2580 console.log("Name Username Expire");
2581 console.log("-------------------------------------------------------------------------------------");
2582 if (data.loginTokens.length == 0) {
2583 console.log("No login tokens");
2585 for (var i in data.loginTokens) {
2586 var t = data.loginTokens[i];
2587 var e = (t.expire == 0) ? "Unlimited" : new Date(t.expire).toLocaleString();
2588 console.log(padString(t.name, 28) + padString(t.tokenUser, 28) + e);
2595 case 'getDeviceDetails': {
2596 console.log(data.data);
2600 console.log('group,' + data.data.columns.flatMap(c => c.id).join(','));
2601 Object.keys(data.data.groups).forEach(gk => {
2602 data.data.groups[gk].entries.forEach(e => {
2603 console.log(gk + ',' + Object.values(e).join(','));
2610 //console.log('Data', data);
2611 //setTimeout(function timeout() { ws.send(Date.now()); }, 500);
2615// String padding function
2617function padString(str, pad) {
2619 if (str.length >= pad) return str; return str + xpad.substring(0, pad - str.length)
2622function parseSearchAndInput(nodes, x) {
2623 var s = x.split(' ' + "and" + ' '), r = null;
2625 var r2 = getDevicesThatMatchFilter(nodes, s[i]);
2626 if (r == null) { r = r2; } else { var r3 = []; for (var j in r2) { if (r.indexOf(r2[j]) >= 0) { r3.push(r2[j]); } } r = r3; }
2631function parseSearchOrInput(nodes, x) {
2632 var s = x.split(' ' + "or" + ' '), r = null;
2633 for (var i in s) { var r2 = parseSearchAndInput(nodes, s[i]); if (r == null) { r = r2; } else { for (var j in r2) { if (r.indexOf(r2[j] >= 0)) { r.push(r2[j]); } } } }
2637function getDevicesThatMatchFilter(nodes, x) {
2639 var userSearch = null, ipSearch = null, groupSearch = null, tagSearch = null, agentTagSearch = null, wscSearch = null, osSearch = null, amtSearch = null, descSearch = null;
2640 if (x.startsWith("user:".toLowerCase())) { userSearch = x.substring("user:".length); }
2641 else if (x.startsWith("u:".toLowerCase())) { userSearch = x.substring("u:".length); }
2642 else if (x.startsWith("ip:".toLowerCase())) { ipSearch = x.substring("ip:".length); }
2643 else if (x.startsWith("group:".toLowerCase())) { groupSearch = x.substring("group:".length); }
2644 else if (x.startsWith("g:".toLowerCase())) { groupSearch = x.substring("g:".length); }
2645 else if (x.startsWith("tag:".toLowerCase())) { tagSearch = x.substring("tag:".length); }
2646 else if (x.startsWith("t:".toLowerCase())) { tagSearch = x.substring("t:".length); }
2647 else if (x.startsWith("atag:".toLowerCase())) { agentTagSearch = x.substring("atag:".length); }
2648 else if (x.startsWith("a:".toLowerCase())) { agentTagSearch = x.substring("a:".length); }
2649 else if (x.startsWith("os:".toLowerCase())) { osSearch = x.substring("os:".length); }
2650 else if (x.startsWith("amt:".toLowerCase())) { amtSearch = x.substring("amt:".length); }
2651 else if (x.startsWith("desc:".toLowerCase())) { descSearch = x.substring("desc:".length); }
2652 else if (x == 'wsc:ok') { wscSearch = 1; }
2653 else if (x == 'wsc:noav') { wscSearch = 2; }
2654 else if (x == 'wsc:noupdate') { wscSearch = 3; }
2655 else if (x == 'wsc:nofirewall') { wscSearch = 4; }
2656 else if (x == 'wsc:any') { wscSearch = 5; }
2660 for (var d in nodes) { r.push(nodes[d]); }
2661 } else if (ipSearch != null) {
2662 // IP address search
2663 for (var d in nodes) { if ((nodes[d].ip != null) && (nodes[d].ip.indexOf(ipSearch) >= 0)) { r.push(nodes[d]); } }
2664 } else if (groupSearch != null) {
2666 if (settings.xmeshes) { for (var d in nodes) { if (settings.xmeshes[nodes[d].meshid] && settings.xmeshes[nodes[d].meshid].name.toLowerCase().indexOf(groupSearch) >= 0) { r.push(nodes[d]); } } }
2667 } else if (tagSearch != null) {
2669 for (var d in nodes) {
2670 if ((nodes[d].tags == null) && (tagSearch == '')) { r.push(nodes[d]); }
2671 else if (nodes[d].tags != null) { for (var j in nodes[d].tags) { if (nodes[d].tags[j].toLowerCase() == tagSearch) { r.push(nodes[d]); break; } } }
2673 } else if (agentTagSearch != null) {
2675 for (var d in nodes) {
2676 if ((((nodes[d].agent != null) && (nodes[d].agent.tag == null)) && (agentTagSearch == '')) || ((nodes[d].agent != null) && (nodes[d].agent.tag != null) && (nodes[d].agent.tag.toLowerCase().indexOf(agentTagSearch) >= 0))) { r.push(nodes[d]); };
2678 } else if (userSearch != null) {
2680 for (var d in nodes) {
2681 if (nodes[d].users && nodes[d].users.length > 0) { for (var i in nodes[d].users) { if (nodes[d].users[i].toLowerCase().indexOf(userSearch) >= 0) { r.push(nodes[d]); } } }
2683 } else if (osSearch != null) {
2685 for (var d in nodes) { if ((nodes[d].osdesc != null) && (nodes[d].osdesc.toLowerCase().indexOf(osSearch) >= 0)) { r.push(nodes[d]); }; }
2686 } else if (amtSearch != null) {
2688 for (var d in nodes) { if ((nodes[d].intelamt != null) && ((amtSearch == '') || (nodes[d].intelamt.state == amtSearch))) { r.push(nodes[d]); } }
2689 } else if (descSearch != null) {
2690 // Device description search
2691 for (var d in nodes) { if ((nodes[d].desc != null) && (nodes[d].desc != '') && ((descSearch == '') || (nodes[d].desc.toLowerCase().indexOf(descSearch) >= 0))) { r.push(nodes[d]); } }
2692 } else if (wscSearch != null) {
2693 // Windows Security Center
2694 for (var d in nodes) {
2696 if ((wscSearch == 1) && (nodes[d].wsc.antiVirus == 'OK') && (nodes[d].wsc.autoUpdate == 'OK') && (nodes[d].wsc.firewall == 'OK')) { r.push(nodes[d]); }
2697 else if (((wscSearch == 2) || (wscSearch == 5)) && (nodes[d].wsc.antiVirus != 'OK')) { r.push(nodes[d]); }
2698 else if (((wscSearch == 3) || (wscSearch == 5)) && (nodes[d].wsc.autoUpdate != 'OK')) { r.push(nodes[d]); }
2699 else if (((wscSearch == 4) || (wscSearch == 5)) && (nodes[d].wsc.firewall != 'OK')) { r.push(nodes[d]); }
2702 } else if (x == '*') {
2704 for (var d in nodes) { if (stars[nodes[d]._id] == 1) { r.push(nodes[d]); } }
2706 // Device name search
2708 var rs = x.split(/\s+/).join('|'), rx = new RegExp(rs); // In some cases (like +), this can throw an exception.
2709 for (var d in nodes) {
2710 //if (showRealNames) {
2711 //if (nodes[d].rnamel != null && rx.test(nodes[d].rnamel.toLowerCase())) { r.push(nodes[d]); }
2713 if (rx.test(nodes[d].name.toLowerCase())) { r.push(nodes[d]); }
2716 } catch (ex) { for (var d in nodes) { r.push(nodes[d]); } }
2723// Connect tunnel to a remote agent
2724function connectTunnel(url) {
2725 // Setup WebSocket options
2726 var options = { rejectUnauthorized: false, checkServerIdentity: onVerifyServer }
2728 // Setup the HTTP proxy if needed
2729 if (args.proxy != null) { const HttpsProxyAgent = require('https-proxy-agent'); options.agent = new HttpsProxyAgent(require('url').parse(args.proxy)); }
2731 // Connect the WebSocket
2732 console.log('Connecting...');
2733 const WebSocket = require('ws');
2734 settings.tunnelwsstate = 0;
2735 settings.tunnelws = new WebSocket(url, options);
2736 settings.tunnelws.on('open', function () { console.log('Waiting for Agent...'); }); // Wait for agent connection
2737 settings.tunnelws.on('close', function () { console.log('Connection Closed.'); process.exit(); });
2738 settings.tunnelws.on('error', function (err) { console.log(err); process.exit(); });
2740 if (settings.cmd == 'shell') {
2741 // This code does all of the work for a shell command
2742 settings.tunnelws.on('message', function (rawdata) {
2743 var data = rawdata.toString();
2744 if (settings.tunnelwsstate == 1) {
2745 // If the incoming text looks exactly like a control command, ignore it.
2746 if ((typeof data == 'string') && (data.startsWith('{"ctrlChannel":"102938","type":"'))) {
2748 try { ctrlCmd = JSON.parse(data); } catch (ex) { }
2749 if ((ctrlCmd != null) && (ctrlCmd.ctrlChannel == '102938') && (ctrlCmd.type != null)) return; // This is a control command, like ping/pong. Ignore it.
2751 process.stdout.write(data);
2752 } else if (settings.tunnelwsstate == 0) {
2753 if (data == 'c') { console.log('Connected.'); } else if (data == 'cr') { console.log('Connected, session is being recorded.'); } else return;
2754 // Send terminal size
2755 var termSize = null;
2756 if (typeof process.stdout.getWindowSize == 'function') { termSize = process.stdout.getWindowSize(); }
2757 if (termSize != null) { settings.tunnelws.send(JSON.stringify({ ctrlChannel: '102938', type: 'options', cols: termSize[0], rows: termSize[1] })); }
2758 settings.tunnelwsstate = 1;
2759 settings.tunnelws.send('1'); // Terminal
2760 process.stdin.setEncoding('utf8');
2761 process.stdin.setRawMode(true);
2762 process.stdout.setEncoding('utf8');
2763 process.stdin.unpipe(process.stdout);
2764 process.stdout.unpipe(process.stdin);
2765 process.stdin.on('data', function (data) { settings.tunnelws.send(Buffer.from(data)); });
2766 //process.stdin.on('readable', function () { var chunk; while ((chunk = process.stdin.read()) !== null) { settings.tunnelws.send(Buffer.from(chunk)); } });
2767 process.stdin.on('end', function () { process.exit(); });
2768 process.stdout.on('resize', function () {
2769 var termSize = null;
2770 if (typeof process.stdout.getWindowSize == 'function') { termSize = process.stdout.getWindowSize(); }
2771 if (termSize != null) { settings.tunnelws.send(JSON.stringify({ ctrlChannel: '102938', type: 'termsize', cols: termSize[0], rows: termSize[1] })); }
2775 } else if (settings.cmd == 'upload') {
2776 // This code does all of the work for a file upload
2777 // node meshctrl upload --id oL4Y6Eg0qjnpHFrp1AxfxnBPenbDGnDSkC@HSOnAheIyd51pKhqSCUgJZakzwfKl --file readme.md --target c:\
2778 settings.tunnelws.on('message', function (rawdata) {
2779 if (settings.tunnelwsstate == 1) {
2781 try { cmd = JSON.parse(rawdata.toString()); } catch (ex) { return; }
2782 if (cmd.reqid == 'up') {
2783 if ((cmd.action == 'uploadack') || (cmd.action == 'uploadstart')) {
2784 settings.inFlight--;
2785 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.
2786 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.
2787 for (var i = 0; i < loops; i++) {
2788 if (settings.uploadFile == null) continue;
2789 var buf = Buffer.alloc(65565);
2790 var len = require('fs').readSync(settings.uploadFile, buf, 1, 65564, settings.uploadPtr);
2792 settings.uploadPtr += len;
2794 if ((buf[1] == 0) || (buf[1] == 123)) { start = 0; buf[0] = 0; len++; } // If the buffer starts with 0 or 123, we must add an extra 0 at the start of the buffer
2795 settings.inFlight++;
2796 settings.tunnelws.send(buf.slice(start, start + len));
2798 console.log('Upload done, ' + settings.uploadPtr + ' bytes sent.');
2799 if (settings.uploadFile != null) { require('fs').closeSync(settings.uploadFile); delete settings.uploadFile; }
2800 if (settings.inFlight == 0) { process.exit(); return; } // File is closed, if there is no more in-flight data, exit.
2804 } else if (cmd.action == 'uploaderror') {
2805 if (settings.uploadFile != null) { require('fs').closeSync(settings.uploadFile); }
2806 console.log('Upload error.');
2810 } else if (settings.tunnelwsstate == 0) {
2811 var data = rawdata.toString();
2812 if (data == 'c') { console.log('Connected.'); } else if (data == 'cr') { console.log('Connected, session is being recorded.'); } else return;
2813 settings.tunnelwsstate = 1;
2814 settings.tunnelws.send('5'); // Files
2815 settings.uploadSize = require('fs').statSync(args.file).size;
2816 settings.uploadFile = require('fs').openSync(args.file, 'r');
2817 settings.uploadPtr = 0;
2818 settings.inFlight = 1;
2819 console.log('Uploading...');
2820 settings.tunnelws.send(JSON.stringify({ action: 'upload', reqid: 'up', path: args.target, name: require('path').basename(args.file), size: settings.uploadSize }));
2823 } else if (settings.cmd == 'download') {
2824 // This code does all of the work for a file download
2825 // node meshctrl download --id oL4Y6Eg0qjnpHFrp1AxfxnBPenbDGnDSkC@HSOnAheIyd51pKhqSCUgJZakzwfKl --file c:\temp\MC-8Languages.png --target c:\temp\bob.png
2826 settings.tunnelws.on('message', function (rawdata) {
2827 if (settings.tunnelwsstate == 1) {
2828 if ((rawdata.length > 0) && (rawdata.toString()[0] != '{')) {
2829 // This is binary data, this test is ok because 4 first bytes is a control value.
2830 if ((rawdata.length > 4) && (settings.downloadFile != null)) { settings.downloadSize += (rawdata.length - 4); require('fs').writeSync(settings.downloadFile, rawdata, 4, rawdata.length - 4); }
2831 if ((rawdata[3] & 1) != 0) { // Check end flag
2832 // File is done, close everything.
2833 if (settings.downloadFile != null) { require('fs').closeSync(settings.downloadFile); }
2834 console.log('Download completed, ' + settings.downloadSize + ' bytes written.');
2837 settings.tunnelws.send(JSON.stringify({ action: 'download', sub: 'ack', id: args.file })); // Send the ACK
2840 // This is text data
2842 try { cmd = JSON.parse(rawdata.toString()); } catch (ex) { return; }
2843 if (cmd.action == 'download') {
2844 if (cmd.id != args.file) return;
2845 if (cmd.sub == 'start') {
2846 if ((args.target.endsWith('\\')) || (args.target.endsWith('/'))) { args.target += path.parse(args.file).name; }
2847 try { settings.downloadFile = require('fs').openSync(args.target, 'w'); } catch (ex) { console.log("Unable to create file: " + args.target); process.exit(); return; }
2848 settings.downloadSize = 0;
2849 settings.tunnelws.send(JSON.stringify({ action: 'download', sub: 'startack', id: args.file }));
2850 console.log('Download started: ' + args.target);
2851 } else if (cmd.sub == 'cancel') {
2852 if (settings.downloadFile != null) { require('fs').closeSync(settings.downloadFile); }
2853 console.log('Download canceled.');
2858 } else if (settings.tunnelwsstate == 0) {
2859 var data = rawdata.toString();
2860 if (data == 'c') { console.log('Connected.'); } else if (data == 'cr') { console.log('Connected, session is being recorded.'); } else return;
2861 settings.tunnelwsstate = 1;
2862 settings.tunnelws.send('5'); // Files
2863 settings.tunnelws.send(JSON.stringify({ action: 'download', sub: 'start', id: args.file, path: args.file }));
2869// Encode an object as a cookie using a key using AES-GCM. (key must be 32 bytes or more)
2870function encodeCookie(o, key) {
2872 if (key == null) { return null; }
2873 o.time = Math.floor(Date.now() / 1000); // Add the cookie creation time
2874 const iv = Buffer.from(crypto.randomBytes(12), 'binary'), cipher = crypto.createCipheriv('aes-256-gcm', key.slice(0, 32), iv);
2875 const crypted = Buffer.concat([cipher.update(JSON.stringify(o), 'utf8'), cipher.final()]);
2876 return Buffer.concat([iv, cipher.getAuthTag(), crypted]).toString('base64').replace(/\+/g, '@').replace(/\//g, '$');
2877 } catch (e) { return null; }
2880// Generate a random Intel AMT password
2881function checkAmtPassword(p) { return (p.length > 7) && (/\d/.test(p)) && (/[a-z]/.test(p)) && (/[A-Z]/.test(p)) && (/\W/.test(p)); }
2882function getRandomAmtPassword() { var p; do { p = Buffer.from(crypto.randomBytes(9), 'binary').toString('base64').split('/').join('@'); } while (checkAmtPassword(p) == false); return p; }
2883function getRandomHex(count) { return Buffer.from(crypto.randomBytes(count), 'binary').toString('hex'); }
2884function format(format) { var args = Array.prototype.slice.call(arguments, 1); return format.replace(/{(\d+)}/g, function (match, number) { return typeof args[number] != 'undefined' ? args[number] : match; }); };
2885function winRemoveSingleQuotes(str) { if (process.platform != 'win32') return str; else return str.split('\'').join(''); }
2887function csvFormatArray(x) {
2889 for (var i in x) { if ((x[i] == null) || (x[i] == '')) { y.push(''); } else { y.push('"' + x[i].split('"').join('') + '"'); } }
2893function displayDeviceInfo(sysinfo, lastconnect, network, nodes) {
2894 //console.log('displayDeviceInfo', sysinfo, lastconnect, network, nodes);
2896 // Fetch the node information
2898 if (sysinfo != null && (sysinfo.node != null)) {
2899 // Node information came with system information
2900 node = sysinfo.node;
2902 // This device does not have system information, get node information from the nodes list.
2903 for (var m in nodes.nodes) {
2904 for (var n in nodes.nodes[m]) {
2905 if (nodes.nodes[m][n]._id.indexOf(args.id) >= 0) { node = nodes.nodes[m][n]; }
2909 if ((sysinfo == null && lastconnect == null && network == null) || (node == null)) {
2910 console.log("Invalid device id");
2911 process.exit(); return;
2916 //if (network != null) { sysinfo.netif = network.netif; }
2917 if (lastconnect != null) { node.lastconnect = lastconnect.time; node.lastaddr = lastconnect.addr; }
2918 if (args.raw) { console.log(JSON.stringify(sysinfo, ' ', 2)); return; }
2921 var output = {}, outputCount = 0;
2922 if (node.name) { output["Server Name"] = node.name; outputCount++; }
2923 if (node.rname) { output["Computer Name"] = node.rname; outputCount++; }
2924 if (node.host != null) { output["Hostname"] = node.host; outputCount++; }
2925 if (node.ip != null) { output["IP Address"] = node.ip; outputCount++; }
2926 if (node.desc != null) { output["Description"] = node.desc; outputCount++; }
2927 if (node.icon != null) { output["Icon"] = node.icon; outputCount++; }
2928 if (node.tags) { output["Tags"] = node.tags; outputCount++; }
2931 for (var i in node.av) {
2932 if (typeof node.av[i]['product'] == 'string') {
2933 var n = node.av[i]['product'];
2934 if (node.av[i]['updated'] === true) { n += ', updated'; }
2935 if (node.av[i]['updated'] === false) { n += ', not updated'; }
2936 if (node.av[i]['enabled'] === true) { n += ', enabled'; }
2937 if (node.av[i]['enabled'] === false) { n += ', disabled'; }
2941 output["AntiVirus"] = av; outputCount++;
2943 if (typeof node.wsc == 'object') {
2944 output["WindowsSecurityCenter"] = node.wsc; outputCount++;
2946 if (outputCount > 0) { info["General"] = output; }
2949 var hardware = null;
2950 if ((sysinfo != null) && (sysinfo.hardware != null)) { hardware = sysinfo.hardware; }
2951 if ((hardware && hardware.windows && hardware.windows.osinfo) || node.osdesc) {
2952 var output = {}, outputCount = 0;
2953 if (node.rname) { output["Name"] = node.rname; outputCount++; }
2954 if (node.osdesc) { output["Version"] = node.osdesc; outputCount++; }
2955 if (hardware && hardware.windows && hardware.windows.osinfo) { var m = hardware.windows.osinfo; if (m.OSArchitecture) { output["Architecture"] = m.OSArchitecture; outputCount++; } }
2956 if (outputCount > 0) { info["Operating System"] = output; }
2961 var output = {}, outputCount = 0;
2962 var agentsStr = ["Unknown", "Windows 32bit console", "Windows 64bit console", "Windows 32bit service", "Windows 64bit service", "Linux 32bit", "Linux 64bit", "MIPS", "XENx86", "Android", "Linux ARM", "macOS x86-32bit", "Android x86", "PogoPlug ARM", "Android", "Linux Poky x86-32bit", "macOS x86-64bit", "ChromeOS", "Linux Poky x86-64bit", "Linux NoKVM x86-32bit", "Linux NoKVM x86-64bit", "Windows MinCore console", "Windows MinCore service", "NodeJS", "ARM-Linaro", "ARMv6l / ARMv7l", "ARMv8 64bit", "ARMv6l / ARMv7l / NoKVM", "MIPS24KC (OpenWRT)", "Apple Silicon", "FreeBSD x86-64", "Unknown", "Linux ARM 64 bit", "Alpine Linux x86 64 Bit (MUSL)", "Assistant (Windows)", "Armada370 - ARM32/HF (libc/2.26)", "OpenWRT x86-64", "OpenBSD x86-64", "Unknown", "Unknown", "MIPSEL24KC (OpenWRT)", "ARMADA/CORTEX-A53/MUSL (OpenWRT)", "Windows ARM 64bit console", "Windows ARM 64bit service", "ARMVIRT32 (OpenWRT)", "RISC-V x86-64"];
2963 if ((node.agent != null) && (node.agent.id != null) && (node.agent.ver != null)) {
2965 if (node.agent.id <= agentsStr.length) { str = agentsStr[node.agent.id]; } else { str = agentsStr[0]; }
2966 if (node.agent.ver != 0) { str += ' v' + node.agent.ver; }
2967 output["Mesh Agent"] = str; outputCount++;
2969 if ((node.conn & 1) != 0) {
2970 output["Last agent connection"] = "Connected now"; outputCount++;
2972 if (node.lastconnect) { output["Last agent connection"] = new Date(node.lastconnect).toLocaleString(); outputCount++; }
2974 output["Agent status"] = (node.conn & 1) != 0 ? "Connected now" : "Offline"; outputCount++;
2975 if (node.lastaddr) {
2976 var splitip = node.lastaddr.split(':');
2977 if (splitip.length > 2) {
2978 output["Last agent address"] = node.lastaddr; outputCount++; // IPv6
2980 output["Last agent address"] = splitip[0]; outputCount++; // IPv4
2983 if ((node.agent != null) && (node.agent.tag != null)) {
2984 output["Tag"] = node.agent.tag; outputCount++;
2986 if (outputCount > 0) { info["Mesh Agent"] = output; }
2990 if (network.netif != null) {
2991 var output = {}, outputCount = 0, minfo = {};
2992 for (var i in network.netif) {
2993 var m = network.netif[i], moutput = {}, moutputCount = 0;
2994 if (m.desc) { moutput["Description"] = m.desc; moutputCount++; }
2997 moutput["MAC Layer"] = format("MAC: {0}, Gateway: {1}", m.mac, m.gatewaymac); moutputCount++;
2999 moutput["MAC Layer"] = format("MAC: {0}", m.mac); moutputCount++;
3002 if (m.v4addr && (m.v4addr != '0.0.0.0')) {
3003 if (m.v4gateway && m.v4mask) {
3004 moutput["IPv4 Layer"] = format("IP: {0}, Mask: {1}, Gateway: {2}", m.v4addr, m.v4mask, m.v4gateway); moutputCount++;
3006 moutput["IPv4 Layer"] = format("IP: {0}", m.v4addr); moutputCount++;
3009 if (moutputCount > 0) { minfo[m.name + (m.dnssuffix ? (', ' + m.dnssuffix) : '')] = moutput; info["Networking"] = minfo; }
3013 if (network.netif2 != null) {
3015 for (var i in network.netif2) {
3016 var m = network.netif2[i], moutput = {}, moutputCount = 0;
3018 if (Array.isArray(m) == false ||
3021 ((typeof m[0].mac == 'string') && (m[0].mac.startsWith('00:00:00:00')))
3025 var ifTitle = '' + i;
3026 if (m[0].fqdn != null && m[0].fqdn != '') ifTitle += ', ' + m[0].fqdn;
3028 if (typeof m[0].mac == 'string') {
3029 if (m[0].gatewaymac) {
3030 moutput['MAC Layer'] = format("MAC: {0}, Gateway: {1}", m[0].mac, m[0].gatewaymac);
3032 moutput['MAC Layer'] = format("MAC: {0}", m[0].mac);
3037 moutput['IPv4 Layer'] = '';
3038 moutput['IPv6 Layer'] = '';
3039 for (var j = 0; j < m.length; j++) {
3041 if (iplayer.family == 'IPv4' || iplayer.family == 'IPv6') {
3042 if (iplayer.gateway && iplayer.netmask) {
3043 moutput[iplayer.family + ' Layer'] += format("IP: {0}, Mask: {1}, Gateway: {2} ", iplayer.address, iplayer.netmask, iplayer.gateway);
3046 if (iplayer.address) {
3047 moutput[iplayer.family + ' Layer'] += format("IP: {0} ", iplayer.address);
3053 if (moutput['IPv4 Layer'] == '') delete moutput['IPv4 Layer'];
3054 if (moutput['IPv6 Layer'] == '') delete moutput['IPv6 Layer'];
3055 if (moutputCount > 0) {
3056 minfo[ifTitle] = moutput;
3057 info["Networking"] = minfo;
3063 if (node.intelamt != null) {
3064 var output = {}, outputCount = 0;
3065 output["Version"] = (node.intelamt.ver) ? ('v' + node.intelamt.ver) : ('<i>' + "Unknown" + '</i>'); outputCount++;
3066 var provisioningStates = { 0: "Not Activated (Pre)", 1: "Not Activated (In)", 2: "Activated" };
3067 var provisioningMode = '';
3068 if ((node.intelamt.state == 2) && node.intelamt.flags) { if (node.intelamt.flags & 2) { provisioningMode = (', ' + "Client Control Mode (CCM)"); } else if (node.intelamt.flags & 4) { provisioningMode = (', ' + "Admin Control Mode (ACM)"); } }
3069 output["Provisioning State"] = ((node.intelamt.state) ? (provisioningStates[node.intelamt.state]) : ('<i>' + "Unknown" + '</i>')) + provisioningMode; outputCount++;
3070 output["Security"] = (node.intelamt.tls == 1) ? "Secured using TLS" : "TLS is not setup"; outputCount++;
3071 output["Admin Credentials"] = (node.intelamt.user == null || node.intelamt.user == '') ? "Not Known" : "Known"; outputCount++;
3072 if (outputCount > 0) { info["Intel Active Management Technology (Intel AMT)"] = output; }
3075 if (hardware != null) {
3076 if (hardware.identifiers) {
3077 var output = {}, outputCount = 0, ident = hardware.identifiers;
3079 if (ident.bios_vendor) { output["Vendor"] = ident.bios_vendor; outputCount++; }
3080 if (ident.bios_version) { output["Version"] = ident.bios_version; outputCount++; }
3081 if (outputCount > 0) { info["BIOS"] = output; }
3082 output = {}, outputCount = 0;
3085 if (ident.board_vendor) { output["Vendor"] = ident.board_vendor; outputCount++; }
3086 if (ident.board_name) { output["Name"] = ident.board_name; outputCount++; }
3087 if (ident.board_serial && (ident.board_serial != '')) { output["Serial"] = ident.board_serial; outputCount++; }
3088 if (ident.board_version) { output["Version"] = ident.board_version; }
3089 if (ident.product_uuid) { output["Identifier"] = ident.product_uuid; }
3090 if (ident.cpu_name) { output["CPU"] = ident.cpu_name; }
3091 if (ident.gpu_name) { for (var i in ident.gpu_name) { output["GPU" + (parseInt(i) + 1)] = ident.gpu_name[i]; } }
3092 if (outputCount > 0) { info["Motherboard"] = output; }
3096 if (hardware.windows) {
3097 if (hardware.windows.memory) {
3098 var output = {}, outputCount = 0, minfo = {};
3099 hardware.windows.memory.sort(function (a, b) { if (a.BankLabel > b.BankLabel) return 1; if (a.BankLabel < b.BankLabel) return -1; return 0; });
3100 for (var i in hardware.windows.memory) {
3101 var m = hardware.windows.memory[i], moutput = {}, moutputCount = 0;
3102 if (m.Capacity) { moutput["Capacity/Speed"] = (m.Capacity / 1024 / 1024) + " Mb, " + m.Speed + " Mhz"; moutputCount++; }
3103 if (m.PartNumber) { moutput["Part Number"] = ((m.Manufacturer && m.Manufacturer != 'Undefined') ? (m.Manufacturer + ', ') : '') + m.PartNumber; moutputCount++; }
3104 if (moutputCount > 0) { minfo[m.BankLabel] = moutput; info["Memory"] = minfo; }
3110 if (hardware.identifiers && ident.storage_devices) {
3111 var output = {}, outputCount = 0, minfo = {};
3113 ident.storage_devices.sort(function (a, b) { if (a.Caption > b.Caption) return 1; if (a.Caption < b.Caption) return -1; return 0; });
3114 for (var i in ident.storage_devices) {
3115 var m = ident.storage_devices[i], moutput = {};
3117 if (m.Model && (m.Model != m.Caption)) { moutput["Model"] = m.Model; outputCount++; }
3118 if ((typeof m.Size == 'string') && (parseInt(m.Size) == m.Size)) { m.Size = parseInt(m.Size); }
3119 if (typeof m.Size == 'number') { moutput["Capacity"] = Math.floor(m.Size / 1024 / 1024) + 'Mb'; outputCount++; }
3120 if (typeof m.Size == 'string') { moutput["Capacity"] = m.Size; outputCount++; }
3121 if (moutputCount > 0) { minfo[m.Caption] = moutput; info["Storage"] = minfo; }
3127 // Display everything
3129 console.log(JSON.stringify(info, ' ', 2));
3131 for (var i in info) {
3132 console.log('--- ' + i + ' ---');
3133 for (var j in info[i]) {
3134 if ((typeof info[i][j] == 'string') || (typeof info[i][j] == 'number')) {
3135 console.log(' ' + j + ': ' + info[i][j]);
3137 console.log(' ' + j + ':');
3138 for (var k in info[i][j]) {
3139 console.log(' ' + k + ': ' + info[i][j][k]);
3147// Read the Mesh Agent error log and index it.
3148function indexAgentErrorLog() {
3149 // Index the messages
3150 const lines = require('fs').readFileSync('../meshcentral-data/agenterrorlogs.txt', { encoding: 'utf8', flag: 'r' }).split('\r\n');
3151 var errorIndex = {}; // "msg" --> [ { lineNumber, elemenetNumber } ]
3152 for (var i = 0; i < lines.length; i++) {
3153 const line = lines[i];
3154 if (line.length > 88) {
3155 var nodeid = line.substring(0, 70);
3156 var fetchTime = parseInt(line.substring(72, 85));
3157 var data = JSON.parse(line.substring(87));
3158 if ((data != null) && (data.action == 'errorlog') && (Array.isArray(data.log))) {
3159 for (var j = 0; j < data.log.length; j++) {
3160 var entry = data.log[j];
3161 if ((entry != null) && (typeof entry.t == 'number') && (typeof entry.m == 'string')) {
3162 const msg = entry.m;
3163 if (errorIndex[msg] == null) { errorIndex[msg] = []; }
3164 errorIndex[msg].push({ l: i, e: j });
3171 // Sort the messages by frequency
3172 var errorIndexCount = []; // [ { m: "msg", c: count } ]
3173 for (var i in errorIndex) { errorIndexCount.push({ m: i, c: errorIndex[i].length }); }
3174 errorIndexCount = errorIndexCount.sort(function (a, b) { return b.c - a.c })
3176 // Display the results
3177 for (var i = 0; i < errorIndexCount.length; i++) {
3178 const m = errorIndexCount[i].m;
3179 if ((m.indexOf('STUCK') >= 0) || (m.indexOf('FATAL') >= 0)) { console.log(errorIndexCount[i].c, m); }