Added AgentDownload to meshctrl.js
Ylian Saint-Hilaire committed
Aug 5, 2021 at 01:13 UTC
1b42b598071881be0751372fa7d5a67e083ba965
1 file changed
+66
-1
meshctrl.js
+66
-1
@@ -7,7 +7,7 @@ try { require('ws'); } catch (ex) { console.log('Missing module "ws", type "npm
7
var settings = {};
8
const crypto = require('crypto');
9
const args = require('minimist')(process.argv.slice(2));
10
-const possibleCommands = ['edituser', 'listusers', 'listusersessions', 'listdevicegroups', 'listdevices', 'listusersofdevicegroup', 'listevents', 'logintokens', 'serverinfo', 'userinfo', 'adduser', 'removeuser', 'adddevicegroup', 'removedevicegroup', 'editdevicegroup', 'broadcast', 'showevents', 'addusertodevicegroup', 'removeuserfromdevicegroup', 'addusertodevice', 'removeuserfromdevice', 'sendinviteemail', 'generateinvitelink', 'config', 'movetodevicegroup', 'deviceinfo', 'editdevice', 'addusergroup', 'listusergroups', 'removeusergroup', 'runcommand', 'shell', 'upload', 'download', 'deviceopenurl', 'devicemessage', 'devicetoast', 'addtousergroup', 'removefromusergroup', 'removeallusersfromusergroup', 'devicesharing', 'devicepower', 'indexagenterrorlog'];
10
+const possibleCommands = ['edituser', 'listusers', 'listusersessions', 'listdevicegroups', 'listdevices', 'listusersofdevicegroup', 'listevents', 'logintokens', 'serverinfo', 'userinfo', 'adduser', 'removeuser', 'adddevicegroup', 'removedevicegroup', 'editdevicegroup', 'broadcast', 'showevents', 'addusertodevicegroup', 'removeuserfromdevicegroup', 'addusertodevice', 'removeuserfromdevice', 'sendinviteemail', 'generateinvitelink', 'config', 'movetodevicegroup', 'deviceinfo', 'editdevice', 'addusergroup', 'listusergroups', 'removeusergroup', 'runcommand', 'shell', 'upload', 'download', 'deviceopenurl', 'devicemessage', 'devicetoast', 'addtousergroup', 'removefromusergroup', 'removeallusersfromusergroup', 'devicesharing', 'devicepower', 'indexagenterrorlog', 'agentdownload'];
11
if (args.proxy != null) { try { require('https-proxy-agent'); } catch (ex) { console.log('Missing module "https-proxy-agent", type "npm install https-proxy-agent" to install it.'); return; } }
12
13
if (args['_'].length == 0) {
@@ -58,6 +58,7 @@ if (args['_'].length == 0) {
58
console.log(" DeviceToast - Display a toast notification on a remote device.");
59
console.log(" DevicePower - Perform wake/sleep/reset/off operations on remote devices.");
60
console.log(" DeviceSharing - View, add and remove sharing links for a given device.");
61
+ console.log(" AgentDownload - Download an agent of a specific type for a device group.");
62
console.log("\r\nSupported login arguments:");
63
console.log(" --url [wss://server] - Server url, wss://localhost:443 is default.");
64
console.log(" - Use wss://localhost:443?key=xxx if login key is required.");
@@ -228,6 +229,15 @@ if (args['_'].length == 0) {
229
else { ok = true; }
230
break;
231
}
232
+ case 'agentdownload': {
233
+ if (args.type == null) { console.log(winRemoveSingleQuotes("Missing device type, use --type [agenttype]")); }
234
+ var at = parseInt(args.type);
235
+ if ((at == null) || isNaN(at) || (at < 1) || (at > 11000)) { console.log(winRemoveSingleQuotes("Invalid agent type, must be a number.")); }
236
+ if (args.id == null) { console.log(winRemoveSingleQuotes("Missing device id, use --id '[meshid]'")); }
237
+ if ((typeof args.id != 'string') || (args.id.length != 64)) { console.log(winRemoveSingleQuotes("Invalid meshid.")); }
238
+ else { ok = true; }
239
+ break;
240
+ }
241
case 'upload': {
242
if (args.id == null) { console.log(winRemoveSingleQuotes("Missing device id, use --id '[deviceid]'")); }
243
else if (args.file == null) { console.log("Local file missing, use --file [file] specify the file to upload"); }
@@ -828,6 +838,18 @@ if (args['_'].length == 0) {
838
console.log(" --duration [minutes] - Duration of the share, default is 60 minutes.");
839
break;
840
}
841
+ case 'agentdownload': {
842
+ console.log("Download an agent of a specific type for a given device group, Example usages:\r\n");
843
+ console.log(winRemoveSingleQuotes(" MeshCtrl AgentDownload --id 'groupid' --type 3"));
844
+ console.log("\r\nRequired arguments:\r\n");
845
+ console.log(" --type [ArchitectureNumber] - Agent architecture number.");
846
+ if (process.platform == 'win32') {
847
+ console.log(" --id [groupid] - The device group identifier.");
848
+ } else {
849
+ console.log(" --id '[groupid]' - The device group identifier.");
850
+ }
851
+ break;
852
+ }
853
case 'upload': {
854
console.log("Upload a local file to a remote device, Example usages:\r\n");
855
console.log(winRemoveSingleQuotes(" MeshCtrl Upload --id 'deviceid' --file sample.txt --target c:\\"));
@@ -1087,6 +1109,7 @@ function serverConnect() {
1109
} catch (ex) { console.log(ex.message); process.exit(); return; }
1110
}
1111
1112
+ settings.xxurl = url;
1113
if (ckey != null) {
1114
var domainid = '', username = 'admin';
1115
if (args.logindomain != null) { domainid = args.logindomain; }
@@ -1471,6 +1494,48 @@ function serverConnect() {
1494
}
1495
break;
1496
}
1497
+ case 'agentdownload': {
1498
+ // Download an agent
1499
+ var u = settings.xxurl.replace('wss://', 'https://').replace('/control.ashx', '/meshagents');
1500
+ if (u.indexOf('?') > 0) { u += '&'; } else { u += '?'; }
1501
+ u += 'id=' + args.type + '&meshid=' + args.id;
1502
+ const options = { rejectUnauthorized: false, checkServerIdentity: onVerifyServer }
1503
+ const fs = require('fs');
1504
+ const https = require('https');
1505
+ var downloadSize = 0;
1506
+ const req = https.request(u, options, function (res) {
1507
+ if (res.statusCode != 200) {
1508
+ console.log('Download error, statusCode: ' + res.statusCode);
1509
+ process.exit(1);
1510
+ } else {
1511
+ // Agent the agent filename
1512
+ var agentFileName = 'meshagent';
1513
+ if ((res.headers) && (res.headers['content-disposition'] != null)) {
1514
+ var i = res.headers['content-disposition'].indexOf('filename=\"');
1515
+ if (i >= 0) {
1516
+ agentFileName = res.headers['content-disposition'].substring(i + 10);
1517
+ i = agentFileName.indexOf('\"');
1518
+ if (i >= 0) { agentFileName = agentFileName.substring(0, i); }
1519
+ }
1520
+ }
1521
+ // Check if this file already exists
1522
+ if (fs.existsSync(agentFileName)) { console.log('File \"' + agentFileName + '\" already exists.'); process.exit(1); }
1523
+ var fd = fs.openSync(agentFileName, 'w'); // Open the file for writing
1524
+ res.on('data', function (d) {
1525
+ downloadSize += d.length;
1526
+ fs.writeSync(fd, d); // Save to file
1527
+ });
1528
+ res.on('end', function (d) {
1529
+ fs.closeSync(fd); // Close file
1530
+ console.log('Downloaded ' + downloadSize + ' byte(s) to \"' + agentFileName + '\"');
1531
+ process.exit(1);
1532
+ });
1533
+ }
1534
+ })
1535
+ req.on('error', function (error) { console.error(error); process.exit(1); })
1536
+ req.end()
1537
+ break;
1538
+ }
1539
case 'devicesharing': {
1540
if (args.add) {
1541
if (args.add.length == 0) { console.log("Invalid guest name."); process.exit(1); }