Improvements to MeshCtrl.js DeviceSharing.

Ylian Saint-Hilaire committed Oct 8, 2021 at 18:35 UTC 335ca7881f29f092e4c845b84516db158036a662
1 file changed +79 -53
meshctrl.js
+79 -53
@@ -839,7 +839,7 @@ if (args['_'].length == 0) {
839 console.log(winRemoveSingleQuotes(" MeshCtrl DeviceSharing --id 'deviceid'"));
840 console.log(winRemoveSingleQuotes(" MeshCtrl DeviceSharing --id 'deviceid' --remove abcdef"));
841 console.log(winRemoveSingleQuotes(" MeshCtrl DeviceSharing --id 'deviceid' --add Guest --start " + localISOTime + " --duration 30"));
842 - console.log(winRemoveSingleQuotes(" MeshCtrl DeviceSharing --id 'deviceid' --add Guest --type terminal --consent prompt"));
842 + console.log(winRemoveSingleQuotes(" MeshCtrl DeviceSharing --id 'deviceid' --add Guest --type desktop,terminal --consent prompt"));
843 console.log("\r\nRequired arguments:\r\n");
844 if (process.platform == 'win32') {
845 console.log(" --id [deviceid] - The device identifier.");
@@ -847,13 +847,14 @@ if (args['_'].length == 0) {
847 console.log(" --id '[deviceid]' - The device identifier.");
848 }
849 console.log("\r\nOptional arguments:\r\n");
850 - console.log(" --remove [shareid] - Remove a device sharing link.");
851 - console.log(" --add [guestname] - Add a device sharing link.");
852 - console.log(" --type [desktop/terminal] - Type of sharing to add, default is desktop.");
853 - console.log(" --consent [notify,prompt] - Consent flags, default is notify.");
854 - console.log(" --start [yyyy-mm-ddThh:mm:ss] - Start time, default is now.");
855 - console.log(" --end [yyyy-mm-ddThh:mm:ss] - End time.");
856 - console.log(" --duration [minutes] - Duration of the share, default is 60 minutes.");
850 + console.log(" --remove [shareid] - Remove a device sharing link.");
851 + console.log(" --add [guestname] - Add a device sharing link.");
852 + console.log(" --type [desktop,terminal,files] - Type of sharing to add, can be combined. default is desktop.");
853 + console.log(" --viewonly - Make desktop sharing view only.");
854 + console.log(" --consent [notify,prompt] - Consent flags, default is notify.");
855 + console.log(" --start [yyyy-mm-ddThh:mm:ss] - Start time, default is now.");
856 + console.log(" --end [yyyy-mm-ddThh:mm:ss] - End time.");
857 + console.log(" --duration [minutes] - Duration of the share, default is 60 minutes.");
858 break;
859 }
860 case 'agentdownload': {
@@ -1560,42 +1561,60 @@ function serverConnect() {
1561 if (args.add.length == 0) { console.log("Invalid guest name."); process.exit(1); }
1562
1563 // Sharing type, desktop or terminal
1563 - var p = 2; // Desktop
1564 + var p = 0;
1565 if (args.type != null) {
1565 - if (args.type.toLowerCase() == 'terminal') { p = 1; }
1566 - else if (args.type.toLowerCase() == 'desktop') { p = 2; }
1567 - else { console.log("Unknown type."); process.exit(1); return; }
1566 + var shareTypes = args.type.toLowerCase().split(',');
1567 + for (var i in shareTypes) { if ((shareTypes[i] != 'terminal') && (shareTypes[i] != 'desktop') && (shareTypes[i] != 'files')) { console.log("Unknown sharing type: " + shareTypes[i]); process.exit(1); } }
1568 + if (shareTypes.indexOf('terminal') >= 0) { p |= 1; }
1569 + if (shareTypes.indexOf('desktop') >= 0) { p |= 2; }
1570 + if (shareTypes.indexOf('files') >= 0) { p |= 4; }
1571 }
1572 + if (p == 0) { p = 2; } // Desktop
1573 +
1574 + // Sharing view only
1575 + var viewOnly = false;
1576 + if (args.viewonly) { viewOnly = true; }
1577
1578 // User consent
1579 var consent = 0;
1580 if (args.consent == null) {
1573 - if (p == 1) { consent = 0x0002; } // Terminal notify
1574 - if (p == 2) { consent = 0x0001; } // Desktop notify
1581 + if ((p & 1) != 0) { consent = 0x0002; } // Terminal notify
1582 + if ((p & 2) != 0) { consent = 0x0001; } // Desktop notify
1583 + if ((p & 4) != 0) { consent = 0x0004; } // Files notify
1584 } else {
1576 - var flagStrs = args.consent.split(',');
1577 - for (var i in flagStrs) {
1578 - var flagStr = flagStrs[i].toLowerCase();
1579 - if (flagStr == 'none') { consent = 0; }
1580 - else if (flagStr == 'notify') {
1581 - if (p == 1) { consent |= 0x0002; } // Terminal notify
1582 - if (p == 2) { consent |= 0x0001; } // Desktop notify
1583 - } else if (flagStr == 'prompt') {
1584 - if (p == 1) { consent |= 0x0010; } // Terminal prompt
1585 - if (p == 2) { consent |= 0x0008; } // Desktop prompt
1586 - } else if (flagStr == 'bar') {
1587 - if (p == 2) { consent |= 0x0040; } // Desktop toolbar
1588 - } else { console.log("Unknown consent type."); process.exit(1); return; }
1585 + if (typeof args.consent == 'string') {
1586 + var flagStrs = args.consent.split(',');
1587 + for (var i in flagStrs) {
1588 + var flagStr = flagStrs[i].toLowerCase();
1589 + if (flagStr == 'none') { consent = 0; }
1590 + else if (flagStr == 'notify') {
1591 + if ((p & 1) != 0) { consent |= 0x0002; } // Terminal notify
1592 + if ((p & 2) != 0) { consent |= 0x0001; } // Desktop notify
1593 + if ((p & 4) != 0) { consent |= 0x0004; } // Files notify
1594 + } else if (flagStr == 'prompt') {
1595 + if ((p & 1) != 0) { consent |= 0x0010; } // Terminal prompt
1596 + if ((p & 2) != 0) { consent |= 0x0008; } // Desktop prompt
1597 + if ((p & 4) != 0) { consent |= 0x0020; } // Files prompt
1598 + } else if (flagStr == 'bar') {
1599 + if ((p & 2) != 0) { consent |= 0x0040; } // Desktop toolbar
1600 + } else { console.log("Unknown consent type."); process.exit(1); return; }
1601 + }
1602 }
1603 }
1604
1605 // Start and end time
1593 - var start = Math.floor(Date.now() / 1000), end = start + (60 * 60);
1606 + var start = null, end = null;
1607 if (args.start) { start = Math.floor(Date.parse(args.start) / 1000); end = start + (60 * 60); }
1595 - 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; } }
1596 - if (args.duration) { end = start + parseInt(args.duration * 60); }
1608 + 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; } }
1609 + if (args.duration) { if (start == null) { start = Math.floor(Date.now() / 1000) } end = start + parseInt(args.duration * 60); }
1610
1598 - ws.send(JSON.stringify({ action: 'createDeviceShareLink', nodeid: args.id, guestname: args.add, p: p, consent: consent, start: start, end: end, responseid: 'meshctrl' }));
1611 + if ((start == null) && (end == null)) {
1612 + // Unlimited sharing
1613 + ws.send(JSON.stringify({ action: 'createDeviceShareLink', nodeid: args.id, guestname: args.add, p: p, consent: consent, expire: 0, viewOnly: viewOnly, responseid: 'meshctrl' }));
1614 + } else {
1615 + // Time limited sharing
1616 + ws.send(JSON.stringify({ action: 'createDeviceShareLink', nodeid: args.id, guestname: args.add, p: p, consent: consent, start: start, end: end, viewOnly: viewOnly, responseid: 'meshctrl' }));
1617 + }
1618 } else if (args.remove) {
1619 ws.send(JSON.stringify({ action: 'removeDeviceShare', nodeid: args.id, publicid: args.remove, responseid: 'meshctrl' }));
1620 } else {
@@ -1761,28 +1780,35 @@ function serverConnect() {
1780 if ((data.deviceShares == null) || (data.deviceShares.length == 0)) {
1781 console.log('No device sharing links for this device.');
1782 } else {
1764 - for (var i in data.deviceShares) {
1765 - var share = data.deviceShares[i];
1766 - var shareType = "Unknown";
1767 - if (share.p == 1) { shareType = "Terminal"; }
1768 - if (share.p == 2) { shareType = "Desktop"; }
1769 - var consent = [];
1770 - if ((share.consent & 0x0001) != 0) { consent.push("Desktop Notify"); }
1771 - if ((share.consent & 0x0008) != 0) { consent.push("Desktop Prompt"); }
1772 - if ((share.consent & 0x0040) != 0) { consent.push("Desktop Connection Toolbar"); }
1773 - if ((share.consent & 0x0002) != 0) { consent.push("Terminal Notify"); }
1774 - if ((share.consent & 0x0010) != 0) { consent.push("Terminal Prompt"); }
1775 - if ((share.consent & 0x0004) != 0) { consent.push("Files Notify"); }
1776 - if ((share.consent & 0x0020) != 0) { consent.push("Files Prompt"); }
1777 - console.log('----------');
1778 - console.log('Identifier: ' + share.publicid);
1779 - console.log('Type: ' + shareType);
1780 - console.log('UserId: ' + share.userid);
1781 - console.log('Guest Name: ' + share.guestName);
1782 - console.log('User Consent: ' + consent.join(', '));
1783 - console.log('Start Time: ' + new Date(share.startTime).toLocaleString());
1784 - console.log('Expire Time: ' + new Date(share.expireTime).toLocaleString());
1785 - console.log('URL: ' + share.url);
1783 + if (args.json) {
1784 + console.log(data.deviceShares);
1785 + } else {
1786 + for (var i in data.deviceShares) {
1787 + var share = data.deviceShares[i];
1788 + var shareType = [];
1789 + if ((share.p & 1) != 0) { shareType.push("Terminal"); }
1790 + if ((share.p & 2) != 0) { if (share.viewOnly) { shareType.push("View Only Desktop"); } else { shareType.push("Desktop"); } }
1791 + if ((share.p & 4) != 0) { shareType.push("Files"); }
1792 + shareType = shareType.join(' + ');
1793 + if (shareType == '') { shareType = "Unknown"; }
1794 + var consent = [];
1795 + if ((share.consent & 0x0001) != 0) { consent.push("Desktop Notify"); }
1796 + if ((share.consent & 0x0008) != 0) { consent.push("Desktop Prompt"); }
1797 + if ((share.consent & 0x0040) != 0) { consent.push("Desktop Connection Toolbar"); }
1798 + if ((share.consent & 0x0002) != 0) { consent.push("Terminal Notify"); }
1799 + if ((share.consent & 0x0010) != 0) { consent.push("Terminal Prompt"); }
1800 + if ((share.consent & 0x0004) != 0) { consent.push("Files Notify"); }
1801 + if ((share.consent & 0x0020) != 0) { consent.push("Files Prompt"); }
1802 + console.log('----------');
1803 + console.log('Identifier: ' + share.publicid);
1804 + console.log('Type: ' + shareType);
1805 + console.log('UserId: ' + share.userid);
1806 + console.log('Guest Name: ' + share.guestName);
1807 + console.log('User Consent: ' + consent.join(', '));
1808 + if (share.startTime) { console.log('Start Time: ' + new Date(share.startTime).toLocaleString()); }
1809 + if (share.expireTime) { console.log('Expire Time: ' + new Date(share.expireTime).toLocaleString()); }
1810 + console.log('URL: ' + share.url);
1811 + }
1812 }
1813 }
1814 }