Added device sharing link management support to MeshCtrl.js
Ylian Saint-Hilaire committed
Jan 5, 2021 at 17:02 UTC
a98cdc1d5e63e5b33838611a74c8784cb97861cc
4 files changed
+156
-10
meshctrl.js
+116
-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', 'serverinfo', 'userinfo', 'adduser', 'removeuser', 'adddevicegroup', 'removedevicegroup', 'editdevicegroup', 'broadcast', 'showevents', 'addusertodevicegroup', 'removeuserfromdevicegroup', 'addusertodevice', 'removeuserfromdevice', 'sendinviteemail', 'generateinvitelink', 'config', 'movetodevicegroup', 'deviceinfo', 'addusergroup', 'listusergroups', 'removeusergroup', 'runcommand', 'shell', 'upload', 'download', 'deviceopenurl', 'devicemessage', 'devicetoast', 'addtousergroup', 'removefromusergroup', 'removeallusersfromusergroup'];
10
+const possibleCommands = ['edituser', 'listusers', 'listusersessions', 'listdevicegroups', 'listdevices', 'listusersofdevicegroup', 'serverinfo', 'userinfo', 'adduser', 'removeuser', 'adddevicegroup', 'removedevicegroup', 'editdevicegroup', 'broadcast', 'showevents', 'addusertodevicegroup', 'removeuserfromdevicegroup', 'addusertodevice', 'removeuserfromdevice', 'sendinviteemail', 'generateinvitelink', 'config', 'movetodevicegroup', 'deviceinfo', 'addusergroup', 'listusergroups', 'removeusergroup', 'runcommand', 'shell', 'upload', 'download', 'deviceopenurl', 'devicemessage', 'devicetoast', 'addtousergroup', 'removefromusergroup', 'removeallusersfromusergroup', 'devicesharing'];
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) {
@@ -53,6 +53,7 @@ if (args['_'].length == 0) {
53
console.log(" DeviceOpenUrl - Open a URL on a remote device.");
54
console.log(" DeviceMessage - Open a message box on a remote device.");
55
console.log(" DeviceToast - Display a toast notification on a remote device.");
56
+ console.log(" DeviceSharing - View, add and remove sharing links for a given device.");
57
console.log("\r\nSupported login arguments:");
58
console.log(" --url [wss://server] - Server url, wss://localhost:443 is default.");
59
console.log(" - Use wss://localhost:443?key=xxx if login key is required.");
@@ -205,6 +206,11 @@ if (args['_'].length == 0) {
206
else { ok = true; }
207
break;
208
}
209
+ case 'devicesharing': {
210
+ if (args.id == null) { console.log(winRemoveSingleQuotes("Missing device id, use --id '[deviceid]'")); }
211
+ else { ok = true; }
212
+ break;
213
+ }
214
case 'upload': {
215
if (args.id == null) { console.log(winRemoveSingleQuotes("Missing device id, use --id '[deviceid]'")); }
216
else if (args.file == null) { console.log("Local file missing, use --file [file] specify the file to upload"); }
@@ -709,6 +715,30 @@ if (args['_'].length == 0) {
715
console.log(" --powershell - Run a Windows PowerShell.");
716
break;
717
}
718
+ case 'devicesharing': {
719
+ var tzoffset = (new Date()).getTimezoneOffset() * 60000; // Offset in milliseconds
720
+ var localISOTime = (new Date(Date.now() - tzoffset)).toISOString().slice(0, -5);
721
+ console.log("List sharing links for a specified device, Example usages:\r\n");
722
+ console.log(winRemoveSingleQuotes(" MeshCtrl DeviceSharing --id 'deviceid'"));
723
+ console.log(winRemoveSingleQuotes(" MeshCtrl DeviceSharing --id 'deviceid' --remote abcdef"));
724
+ console.log(winRemoveSingleQuotes(" MeshCtrl DeviceSharing --id 'deviceid' --add Guest --start " + localISOTime + " --duration 30"));
725
+ console.log(winRemoveSingleQuotes(" MeshCtrl DeviceSharing --id 'deviceid' --add Guest --type terminal --consent prompt"));
726
+ console.log("\r\nRequired arguments:\r\n");
727
+ if (process.platform == 'win32') {
728
+ console.log(" --id [deviceid] - The device identifier.");
729
+ } else {
730
+ console.log(" --id '[deviceid]' - The device identifier.");
731
+ }
732
+ console.log("\r\nOptional arguments:\r\n");
733
+ console.log(" --remove [shareid] - Remove a device sharing link.");
734
+ console.log(" --add [guestname] - Add a device sharing link.");
735
+ console.log(" --type [desktop/terminal] - Type of sharing to add, default is desktop.");
736
+ console.log(" --consent [notify,prompt] - Consent flags, default is notify.");
737
+ console.log(" --start [yyyy-mm-ddThh:mm:ss] - Start time, default is now.");
738
+ console.log(" --end [yyyy-mm-ddThh:mm:ss] - End time.");
739
+ console.log(" --duration [minutes] - Duration of the share, default is 60 minutes.");
740
+ break;
741
+ }
742
case 'upload': {
743
console.log("Upload a local file to a remote device, Example usages:\r\n");
744
console.log(winRemoveSingleQuotes(" MeshCtrl Upload --id 'deviceid' --file sample.txt --target c:\\"));
@@ -1287,6 +1317,54 @@ function serverConnect() {
1317
ws.send("{\"action\":\"authcookie\"}");
1318
break;
1319
}
1320
+ case 'devicesharing': {
1321
+ if (args.add) {
1322
+ if (args.add.length == 0) { console.log("Invalid guest name."); process.exit(1); }
1323
+
1324
+ // Sharing type, desktop or terminal
1325
+ var p = 2; // Desktop
1326
+ if (args.type != null) {
1327
+ if (args.type.toLowerCase() == 'terminal') { p = 1; }
1328
+ else if (args.type.toLowerCase() == 'desktop') { p = 2; }
1329
+ else { console.log("Unknown type."); process.exit(1); return; }
1330
+ }
1331
+
1332
+ // User consent
1333
+ var consent = 0;
1334
+ if (args.consent == null) {
1335
+ if (p == 1) { consent = 0x0002; } // Terminal notify
1336
+ if (p == 2) { consent = 0x0001; } // Desktop notify
1337
+ } else {
1338
+ var flagStrs = args.consent.split(',');
1339
+ for (var i in flagStrs) {
1340
+ var flagStr = flagStrs[i].toLowerCase();
1341
+ if (flagStr == 'none') { consent = 0; }
1342
+ else if (flagStr == 'notify') {
1343
+ if (p == 1) { consent |= 0x0002; } // Terminal notify
1344
+ if (p == 2) { consent |= 0x0001; } // Desktop notify
1345
+ } else if (flagStr == 'prompt') {
1346
+ if (p == 1) { consent |= 0x0010; } // Terminal prompt
1347
+ if (p == 2) { consent |= 0x0008; } // Desktop prompt
1348
+ } else if (flagStr == 'bar') {
1349
+ if (p == 2) { consent |= 0x0040; } // Desktop toolbar
1350
+ } else { console.log("Unknown consent type."); process.exit(1); return; }
1351
+ }
1352
+ }
1353
+
1354
+ // Start and end time
1355
+ var start = Math.floor(Date.now() / 1000), end = start + (60 * 60);
1356
+ if (args.start) { start = Math.floor(Date.parse(args.start) / 1000); end = start + (60 * 60); }
1357
+ if (args.end) { 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; } }
1358
+ if (args.duration) { end = start + parseInt(args.duration * 60); }
1359
+
1360
+ ws.send(JSON.stringify({ action: 'createDeviceShareLink', nodeid: args.id, guestname: args.add, p: p, consent: consent, start: start, end: end, responseid: 'meshctrl' }));
1361
+ } else if (args.remove) {
1362
+ ws.send(JSON.stringify({ action: 'removeDeviceShare', nodeid: args.id, publicid: args.remove, responseid: 'meshctrl' }));
1363
+ } else {
1364
+ ws.send(JSON.stringify({ action: 'deviceShares', nodeid: args.id, responseid: 'meshctrl' }));
1365
+ }
1366
+ break;
1367
+ }
1368
case 'deviceopenurl': {
1369
ws.send(JSON.stringify({ action: 'msg', type: 'openUrl', nodeid: args.id, url: args.openurl, responseid: 'meshctrl' }));
1370
break;
@@ -1386,6 +1464,41 @@ function serverConnect() {
1464
}
1465
break;
1466
}
1467
+ case 'deviceShares': { // DEVICESHARING
1468
+ if (data.result != null) {
1469
+ console.log(data.result);
1470
+ } else {
1471
+ if ((data.deviceShares == null) || (data.deviceShares.length == 0)) {
1472
+ console.log('No device sharing links for this device.');
1473
+ } else {
1474
+ for (var i in data.deviceShares) {
1475
+ var share = data.deviceShares[i];
1476
+ var shareType = "Unknown";
1477
+ if (share.p == 1) { shareType = "Terminal"; }
1478
+ if (share.p == 2) { shareType = "Desktop"; }
1479
+ var consent = [];
1480
+ if ((share.consent & 0x0001) != 0) { consent.push("Desktop Notify"); }
1481
+ if ((share.consent & 0x0008) != 0) { consent.push("Desktop Prompt"); }
1482
+ if ((share.consent & 0x0040) != 0) { consent.push("Desktop Connection Toolbar"); }
1483
+ if ((share.consent & 0x0002) != 0) { consent.push("Terminal Notify"); }
1484
+ if ((share.consent & 0x0010) != 0) { consent.push("Terminal Prompt"); }
1485
+ if ((share.consent & 0x0004) != 0) { consent.push("Files Notify"); }
1486
+ if ((share.consent & 0x0020) != 0) { consent.push("Files Prompt"); }
1487
+ console.log('----------');
1488
+ console.log('Identifier: ' + share.publicid);
1489
+ console.log('Type: ' + shareType);
1490
+ console.log('UserId: ' + share.userid);
1491
+ console.log('Guest Name: ' + share.guestName);
1492
+ console.log('User Consent: ' + consent.join(', '));
1493
+ console.log('Start Time: ' + new Date(share.startTime).toLocaleString());
1494
+ console.log('Expire Time: ' + new Date(share.expireTime).toLocaleString());
1495
+ console.log('URL: ' + share.url);
1496
+ }
1497
+ }
1498
+ }
1499
+ process.exit();
1500
+ break;
1501
+ }
1502
case 'userinfo': { // USERINFO
1503
if (settings.cmd == 'userinfo') {
1504
if (args.json) {
@@ -1441,6 +1554,8 @@ function serverConnect() {
1554
case 'runcommands':
1555
case 'addusertousergroup':
1556
case 'removeuserfromusergroup':
1557
+ case 'removeDeviceShare':
1558
+ case 'createDeviceShareLink':
1559
case 'userbroadcast': { // BROADCAST
1560
if ((settings.cmd == 'shell') || (settings.cmd == 'upload') || (settings.cmd == 'download')) return;
1561
if ((settings.multiresponse != null) && (settings.multiresponse > 1)) { settings.multiresponse--; break; }
meshdesktopmultiplex.js
+1
-1
@@ -836,7 +836,7 @@ module.exports.CreateMeshRelay = function (parent, ws, req, domain, user, cookie
836
function CreateMeshRelayEx(parent, ws, req, domain, user, cookie) {
837
// Do validation work
838
if (cookie) {
839
- if ((typeof cookie.expire == 'number') && (cookie.expire <= currentTime)) { delete req.query.nodeid; }
839
+ if ((typeof cookie.expire == 'number') && (cookie.expire <= Date.now())) { delete req.query.nodeid; }
840
else if (typeof cookie.nid == 'string') { req.query.nodeid = cookie.nid; }
841
}
842
if ((req.query.nodeid == null) || (req.query.p != '2') || (req.query.id == null) || (domain == null)) { try { ws.close(); } catch (e) { } return; } // Not is not a valid remote desktop connection.
meshuser.js
+32
-7
@@ -4661,7 +4661,11 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
4661
}
4662
case 'deviceShares': {
4663
var err = null;
4664
+
4665
+ // Argument validation
4666
if (common.validateString(command.nodeid, 8, 128) == false) { err = 'Invalid node id'; } // Check the nodeid
4667
+ else if (command.nodeid.indexOf('/') == -1) { command.nodeid = 'node/' + domain.id + '/' + command.nodeid; }
4668
+ else if ((command.nodeid.split('/').length != 3) || (command.nodeid.split('/')[1] != domain.id)) { err = 'Invalid domain'; } // Invalid domain, operation only valid for current domain
4669
4670
// Handle any errors
4671
if (err != null) {
@@ -4672,7 +4676,10 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
4676
// Get the device rights
4677
parent.GetNodeWithRights(domain, user, command.nodeid, function (node, rights, visible) {
4678
// If node not found or we don't have remote control, reject.
4675
- if ((node == null) || ((rights & 8) == 0)) return;
4679
+ if ((node == null) || ((rights & 8) == 0)) {
4680
+ if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'deviceShares', responseid: command.responseid, result: 'Invalid node id' })); } catch (ex) { } }
4681
+ return;
4682
+ }
4683
4684
// If there is MESHRIGHT_DESKLIMITEDINPUT or MESHRIGHT_REMOTEVIEWONLY on this account, reject this request.
4685
if ((rights != 0xFFFFFFFF) && ((rights & 4352) != 0)) return;
@@ -4705,8 +4712,12 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
4712
}
4713
case 'removeDeviceShare': {
4714
var err = null;
4715
+
4716
+ // Argument validation
4717
if (common.validateString(command.nodeid, 8, 128) == false) { err = 'Invalid node id'; } // Check the nodeid
4709
- else if (common.validateString(command.publicid, 1, 128) == false) { err = 'Invalid public id'; } // Check the public identifier
4718
+ else if (command.nodeid.indexOf('/') == -1) { command.nodeid = 'node/' + domain.id + '/' + command.nodeid; }
4719
+ else if ((command.nodeid.split('/').length != 3) || (command.nodeid.split('/')[1] != domain.id)) { err = 'Invalid domain'; } // Invalid domain, operation only valid for current domain
4720
+ if (common.validateString(command.publicid, 1, 128) == false) { err = 'Invalid public id'; } // Check the public identifier
4721
4722
// Handle any errors
4723
if (err != null) {
@@ -4717,8 +4728,11 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
4728
// Get the device rights
4729
parent.GetNodeWithRights(domain, user, command.nodeid, function (node, rights, visible) {
4730
// If node not found or we don't have remote control, reject.
4720
- if ((node == null) || ((rights & 8) == 0)) return;
4721
-
4731
+ if ((node == null) || ((rights & 8) == 0)) {
4732
+ if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'removeDeviceShare', responseid: command.responseid, result: 'Invalid node id' })); } catch (ex) { } }
4733
+ return;
4734
+ }
4735
+
4736
// If there is MESHRIGHT_DESKLIMITEDINPUT or MESHRIGHT_REMOTEVIEWONLY on this account, reject this request.
4737
if ((rights != 0xFFFFFFFF) && ((rights & 4352) != 0)) return;
4738
@@ -4752,6 +4766,9 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
4766
if (removed == true) {
4767
var targets = parent.CreateNodeDispatchTargets(node.meshid, node._id, ['server-users', user._id]);
4768
parent.parent.DispatchEvent(targets, obj, { etype: 'node', nodeid: node._id, action: 'deviceShareUpdate', domain: domain.id, deviceShares: okDocs, nolog: 1 });
4769
+ if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'removeDeviceShare', responseid: command.responseid, result: 'OK' })); } catch (ex) { } }
4770
+ } else {
4771
+ if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'removeDeviceShare', responseid: command.responseid, result: 'Invalid device share identifier.' })); } catch (ex) { } }
4772
}
4773
});
4774
});
@@ -4759,8 +4776,12 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
4776
}
4777
case 'createDeviceShareLink': {
4778
var err = null;
4779
+
4780
+ // Argument validation
4781
if (common.validateString(command.nodeid, 8, 128) == false) { err = 'Invalid node id'; } // Check the nodeid
4763
- else if (common.validateString(command.guestname, 1, 128) == false) { err = 'Invalid guest name'; } // Check the guest name
4782
+ else if (command.nodeid.indexOf('/') == -1) { command.nodeid = 'node/' + domain.id + '/' + command.nodeid; }
4783
+ else if ((command.nodeid.split('/').length != 3) || (command.nodeid.split('/')[1] != domain.id)) { err = 'Invalid domain'; } // Invalid domain, operation only valid for current domain
4784
+ if (common.validateString(command.guestname, 1, 128) == false) { err = 'Invalid guest name'; } // Check the guest name
4785
else if ((command.expire != null) && (typeof command.expire != 'number')) { err = 'Invalid expire time'; } // Check the expire time in hours
4786
else if ((command.start != null) && (typeof command.start != 'number')) { err = 'Invalid start time'; } // Check the start time in seconds
4787
else if ((command.end != null) && (typeof command.end != 'number')) { err = 'Invalid end time'; } // Check the end time in seconds
@@ -4782,7 +4803,10 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
4803
// Get the device rights
4804
parent.GetNodeWithRights(domain, user, command.nodeid, function (node, rights, visible) {
4805
// If node not found or we don't have remote control, reject.
4785
- if ((node == null) || ((rights & 8) == 0)) return;
4806
+ if ((node == null) || ((rights & 8) == 0)) {
4807
+ if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'createDeviceShareLink', responseid: command.responseid, result: 'Invalid node id' })); } catch (ex) { } }
4808
+ return;
4809
+ }
4810
4811
// If there is MESHRIGHT_DESKLIMITEDINPUT or MESHRIGHT_REMOTEVIEWONLY on this account, reject this request.
4812
if ((rights != 0xFFFFFFFF) && ((rights & 4352) != 0)) return;
@@ -4813,7 +4837,8 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
4837
var url = 'https://' + serverName + ':' + httpsPort + '/' + xdomain + page + '?c=' + inviteCookie;
4838
if (serverName.split('.') == 1) { url = '/' + xdomain + page + '?c=' + inviteCookie; }
4839
command.url = url;
4816
- ws.send(JSON.stringify(command));
4840
+ if (command.responseid != null) { command.result = 'OK'; }
4841
+ try { ws.send(JSON.stringify(command)); } catch (ex) { }
4842
4843
// Create a device sharing database entry
4844
parent.db.Set({ _id: 'deviceshare-' + publicid, type: 'deviceshare', nodeid: node._id, p: command.p, domain: node.domain, publicid: publicid, startTime: startTime, expireTime: expireTime, userid: user._id, guestName: command.guestname, consent: command.consent, url: url });
views/default.handlebars
+7
-1
@@ -6156,7 +6156,12 @@
6156
var dshare = deviceShares[i];
6157
var trash = '<a href="' + dshare.url + '" rel="noreferrer noopener" target=_blank title="' + "Device Sharing Link" + '" style=cursor:pointer><img src=images/link2.png border=0 height=10 width=10></a> <a href=# onclick=\'return p30removeDeviceSharing(event,"' + encodeURIComponentEx(currentNode._id) + '","' + encodeURIComponentEx(dshare.publicid) + '","' + encodeURIComponentEx(dshare.guestName) + '")\' title="' + "Remove device sharing" + '" style=cursor:pointer><img src=images/trash.png border=0 height=10 width=10></a>';
6158
var details = format("{0}, {1} to {2}", ((dshare.p == 1)?"Terminal":"Desktop"), printFlexDateTime(new Date(dshare.startTime)), printFlexDateTime(new Date(dshare.expireTime)));
6159
- if (dshare.consent) { if (((dshare.consent & 8) != 0) || ((dshare.consent & 16) != 0)) { details += ", Prompt for consent"; } }
6159
+ if (dshare.consent != null) {
6160
+ if (dshare.consent == 0) { details += ", No Consent"; } else {
6161
+ if (((dshare.consent & 8) != 0) || ((dshare.consent & 16) != 0)) { details += ", Prompt for consent"; }
6162
+ if ((dshare.consent & 0x40) != 0) { details += ", Toolbar"; }
6163
+ }
6164
+ }
6165
x += '<tr ' + (((++count % 2) == 0) ? 'style=background-color:#DDD' : '') + '><td style=width:30%><div class=m' + 2 + '></div><div> ' + dshare.guestName + '<div></div></div></td><td style=width:70%><div style=float:right>' + trash + '</div><div>' + details + '</div></td></tr>';
6166
}
6167
x += '</tbody></table>';
@@ -6352,6 +6357,7 @@
6357
6358
QE('idx_dlgOkButton', ok);
6359
}
6360
+
6361
function showShareDeviceEx(b, tag) {
6362
var consent = 0;
6363
if (currentNode.agent.caps & 1) {