Completed initial version of agent-less deivce group reached thru a agent, #3867

Ylian Saint-Hilaire committed Apr 13, 2022 at 18:58 UTC 740fbb1e56e61d8e287d2693301a7eb82ebc884f
2 files changed +71 -11
apprelays.js
+67 -9
@@ -105,7 +105,7 @@ module.exports.CreateMstscRelay = function (parent, db, ws, req, args, domain) {
105 var protocol = (args.tlsoffload) ? 'ws' : 'wss';
106 var domainadd = '';
107 if ((domain.dns == null) && (domain.id != '')) { domainadd = domain.id + '/' }
108 - var url = protocol + '://127.0.0.1:' + args.port + '/' + domainadd + ((obj.cookie.lc == 1) ? 'local' : 'mesh') + 'relay.ashx?noping=1&p=10&auth=' + obj.infos.ip; // Protocol 10 is Web-RDP
108 + var url = protocol + '://127.0.0.1:' + args.port + '/' + domainadd + (((obj.mtype == 3) && (obj.relaynodeid == null)) ? 'local' : 'mesh') + 'relay.ashx?noping=1&p=10&auth=' + obj.infos.ip; // Protocol 10 is Web-RDP
109 parent.parent.debug('relay', 'RDP: Connection websocket to ' + url);
110 obj.wsClient = new WebSocket(url, options);
111 obj.wsClient.on('open', function () { parent.parent.debug('relay', 'RDP: Relay websocket open'); });
@@ -210,7 +210,20 @@ module.exports.CreateMstscRelay = function (parent, db, ws, req, args, domain) {
210 parent.parent.db.Get(obj.nodeid, function (err, nodes) {
211 if ((err != null) || (nodes == null) || (nodes.length != 1)) { obj.close(); return; }
212 const node = nodes[0];
213 - obj.meshid = node.meshid;
213 + obj.mtype = node.mtype; // Store the device group type
214 + obj.meshid = node.meshid; // Store the MeshID
215 +
216 + // Check if we need to relay thru a different agent
217 + // TODO: Check if we have rights to the relayid device
218 + var mesh = parent.meshes[obj.meshid];
219 + if (mesh && mesh.relayid) {
220 + obj.relaynodeid = mesh.relayid;
221 + obj.tcpaddr = node.host;
222 +
223 + // Re-encode a cookie with a device relay
224 + const cookieContent = { userid: obj.cookie.userid, domainid: obj.cookie.domainid, nodeid: mesh.relayid, tcpaddr: node.host, tcpport: obj.cookie.tcpport };
225 + obj.infos.ip = parent.parent.encodeCookie(cookieContent, parent.parent.loginCookieEncryptionKey);
226 + }
227
228 // Check if we need to load server stored credentials
229 if ((typeof obj.infos.options == 'object') && (obj.infos.options.useServerCreds == true)) {
@@ -382,7 +395,7 @@ module.exports.CreateSshRelay = function (parent, db, ws, req, args, domain) {
395 if (args.tlsoffload) { protocol = 'ws'; }
396 var domainadd = '';
397 if ((domain.dns == null) && (domain.id != '')) { domainadd = domain.id + '/' }
385 - var url = protocol + '://127.0.0.1:' + args.port + '/' + domainadd + ((obj.cookie.lc == 1) ? 'local' : 'mesh') + 'relay.ashx?noping=1&p=11&auth=' + req.query.auth; // Protocol 11 is Web-SSH
398 + var url = protocol + '://127.0.0.1:' + args.port + '/' + domainadd + (((obj.mtype == 3) && (obj.relaynodeid == null)) ? 'local' : 'mesh') + 'relay.ashx?noping=1&p=11&auth=' + obj.xcookie; // Protocol 11 is Web-SSH
399 parent.parent.debug('relay', 'SSH: Connection websocket to ' + url);
400 obj.wsClient = new WebSocket(url, options);
401 obj.wsClient.on('open', function () { parent.parent.debug('relay', 'SSH: Relay websocket open'); });
@@ -533,6 +546,21 @@ module.exports.CreateSshRelay = function (parent, db, ws, req, args, domain) {
546 const node = nodes[0];
547 obj.nodeid = node._id; // Store the NodeID
548 obj.meshid = node.meshid; // Store the MeshID
549 + obj.mtype = node.mtype; // Store the device group type
550 +
551 + // Check if we need to relay thru a different agent
552 + // TODO: Check if we have rights to the relayid device
553 + var mesh = parent.meshes[obj.meshid];
554 + if (mesh && mesh.relayid) {
555 + obj.relaynodeid = mesh.relayid;
556 + obj.tcpaddr = node.host;
557 +
558 + // Re-encode a cookie with a device relay
559 + const cookieContent = { userid: obj.cookie.userid, domainid: obj.cookie.domainid, nodeid: mesh.relayid, tcpaddr: node.host, tcpport: obj.cookie.tcpport };
560 + obj.xcookie = parent.parent.encodeCookie(cookieContent, parent.parent.loginCookieEncryptionKey);
561 + } else {
562 + obj.xcookie = req.query.auth;
563 + }
564 });
565
566 return obj;
@@ -650,7 +678,7 @@ module.exports.CreateSshTerminalRelay = function (parent, db, ws, req, domain, u
678 if (args.tlsoffload) { protocol = 'ws'; }
679 var domainadd = '';
680 if ((domain.dns == null) && (domain.id != '')) { domainadd = domain.id + '/' }
653 - var url = protocol + '://127.0.0.1:' + args.port + '/' + domainadd + ((obj.mtype == 3) ? 'local' : 'mesh') + 'relay.ashx?noping=1&p=11&auth=' + authCookie // Protocol 11 is Web-SSH
681 + var url = protocol + '://127.0.0.1:' + args.port + '/' + domainadd + (((obj.mtype == 3) && (obj.relaynodeid == null)) ? 'local' : 'mesh') + 'relay.ashx?noping=1&p=11&auth=' + authCookie // Protocol 11 is Web-SSH
682 parent.parent.debug('relay', 'SSH: Connection websocket to ' + url);
683 obj.wsClient = new WebSocket(url, options);
684 obj.wsClient.on('open', function () { parent.parent.debug('relay', 'SSH: Relay websocket open'); });
@@ -753,7 +781,12 @@ module.exports.CreateSshTerminalRelay = function (parent, db, ws, req, domain, u
781
782 // Create a mesh relay authentication cookie
783 var cookieContent = { userid: user._id, domainid: user.domain, nodeid: obj.nodeid, tcpport: obj.tcpport };
756 - if (obj.mtype == 3) { cookieContent.lc = 1; } // This is a local device
784 + if (obj.relaynodeid) {
785 + cookieContent.nodeid = obj.relaynodeid;
786 + cookieContent.tcpaddr = obj.tcpaddr;
787 + } else {
788 + if (obj.mtype == 3) { cookieContent.lc = 1; } // This is a local device
789 + }
790 startRelayConnection(parent.parent.encodeCookie(cookieContent, parent.parent.loginCookieEncryptionKey));
791 break;
792 }
@@ -766,7 +799,12 @@ module.exports.CreateSshTerminalRelay = function (parent, db, ws, req, domain, u
799
800 // Create a mesh relay authentication cookie
801 var cookieContent = { userid: user._id, domainid: user.domain, nodeid: obj.nodeid, tcpport: obj.tcpport };
769 - if (obj.mtype == 3) { cookieContent.lc = 1; } // This is a local device
802 + if (obj.relaynodeid) {
803 + cookieContent.nodeid = obj.relaynodeid;
804 + cookieContent.tcpaddr = obj.tcpaddr;
805 + } else {
806 + if (obj.mtype == 3) { cookieContent.lc = 1; } // This is a local device
807 + }
808 startRelayConnection(parent.parent.encodeCookie(cookieContent, parent.parent.loginCookieEncryptionKey));
809 break;
810 }
@@ -806,6 +844,11 @@ module.exports.CreateSshTerminalRelay = function (parent, db, ws, req, domain, u
844 obj.tcpport = 22;
845 if (typeof node.sshport == 'number') { obj.tcpport = node.sshport; }
846
847 + // Check if we need to relay thru a different agent
848 + // TODO: Check if we have rights to the relayid device
849 + var mesh = parent.meshes[obj.meshid];
850 + if (mesh && mesh.relayid) { obj.relaynodeid = mesh.relayid; obj.tcpaddr = node.host; }
851 +
852 // We are all set, start receiving data
853 ws._socket.resume();
854
@@ -943,7 +986,7 @@ module.exports.CreateSshFilesRelay = function (parent, db, ws, req, domain, user
986 if (args.tlsoffload) { protocol = 'ws'; }
987 var domainadd = '';
988 if ((domain.dns == null) && (domain.id != '')) { domainadd = domain.id + '/' }
946 - var url = protocol + '://127.0.0.1:' + args.port + '/' + domainadd + ((obj.mtype == 3) ? 'local' : 'mesh') + 'relay.ashx?noping=1&p=13&auth=' + authCookie // Protocol 13 is Web-SSH-Files
989 + var url = protocol + '://127.0.0.1:' + args.port + '/' + domainadd + (((obj.mtype == 3) && (obj.relaynodeid == null)) ? 'local' : 'mesh') + 'relay.ashx?noping=1&p=13&auth=' + authCookie // Protocol 13 is Web-SSH-Files
990 parent.parent.debug('relay', 'SSH: Connection websocket to ' + url);
991 obj.wsClient = new WebSocket(url, options);
992 obj.wsClient.on('open', function () { parent.parent.debug('relay', 'SSH: Relay websocket open'); });
@@ -1221,7 +1264,12 @@ module.exports.CreateSshFilesRelay = function (parent, db, ws, req, domain, user
1264
1265 // Create a mesh relay authentication cookie
1266 var cookieContent = { userid: user._id, domainid: user.domain, nodeid: obj.nodeid, tcpport: obj.tcpport };
1224 - if (obj.mtype == 3) { cookieContent.lc = 1; } // This is a local device
1267 + if (obj.relaynodeid) {
1268 + cookieContent.nodeid = obj.relaynodeid;
1269 + cookieContent.tcpaddr = obj.tcpaddr;
1270 + } else {
1271 + if (obj.mtype == 3) { cookieContent.lc = 1; } // This is a local device
1272 + }
1273 startRelayConnection(parent.parent.encodeCookie(cookieContent, parent.parent.loginCookieEncryptionKey));
1274 break;
1275 }
@@ -1279,6 +1327,11 @@ module.exports.CreateSshFilesRelay = function (parent, db, ws, req, domain, user
1327 obj.tcpport = 22;
1328 if (typeof node.sshport == 'number') { obj.tcpport = node.sshport; }
1329
1330 + // Check if we need to relay thru a different agent
1331 + // TODO: Check if we have rights to the relayid device
1332 + var mesh = parent.meshes[obj.meshid];
1333 + if (mesh && mesh.relayid) { obj.relaynodeid = mesh.relayid; obj.tcpaddr = node.host; }
1334 +
1335 // We are all set, start receiving data
1336 ws._socket.resume();
1337
@@ -1302,7 +1355,12 @@ module.exports.CreateSshFilesRelay = function (parent, db, ws, req, domain, user
1355
1356 // Create a mesh relay authentication cookie
1357 var cookieContent = { userid: user._id, domainid: user.domain, nodeid: obj.nodeid, tcpport: obj.tcpport };
1305 - if (obj.mtype == 3) { cookieContent.lc = 1; } // This is a local device
1358 + if (obj.relaynodeid) {
1359 + cookieContent.nodeid = obj.relaynodeid;
1360 + cookieContent.tcpaddr = obj.tcpaddr;
1361 + } else {
1362 + if (obj.mtype == 3) { cookieContent.lc = 1; } // This is a local device
1363 + }
1364 startRelayConnection(parent.parent.encodeCookie(cookieContent, parent.parent.loginCookieEncryptionKey));
1365 }
1366 });
views/default.handlebars
+4 -2
@@ -7932,7 +7932,8 @@
7932
7933 function p10MCRouter(nodeid, protocol, port, ip, localport) {
7934 var node = getNodeFromId(nodeid);
7935 - if ((protocol == 3) && (port == null)) { if (node.rdpport != null) { port = node.rdpport; } else { port = 3389; } }
7935 + if ((protocol == 3) && (port == null)) { if (node.rdpport != null) { port = node.rdpport; } else { port = 3389; } } // Adjust RDP port if needed
7936 + if (node.mtype == 3) { var mesh = meshes[node.meshid]; if (mesh.relayid) { nodeid = mesh.relayid; ip = node.host; } } // Setup device replay if needed
7937 meshserver.send({ action: 'getcookie', nodeid: nodeid, tcpport: port, ip: ip, tag: 'MCRouter', protocol: protocol, localport: localport }); // Protocol: 0 = Custom, 1 = HTTP, 2 = HTTPS, 3 = RDP, 4 = PuTTY, 5 = WinSCP, 6 = MCRDesktop, 7 = MCRFiles
7938 return false;
7939 }
@@ -7940,12 +7941,13 @@
7941 function p10rfb(nodeid, port) {
7942 var node = getNodeFromId(nodeid);
7943 if (port == null) { if (node.rfbport != null) { port = node.rfbport; } else { port = 5900; } }
7944 + if (node.mtype == 3) { var mesh = meshes[node.meshid]; if (mesh.relayid) { nodeid = mesh.relayid; ip = node.host; } } // Setup device replay if needed
7945 meshserver.send({ action: 'getcookie', nodeid: nodeid, tcpport: port, tag: 'novnc' });
7946 }
7947
7948 function p10mstsc(nodeid, port) {
7949 var node = getNodeFromId(nodeid);
7948 - if (port == null) { if (node.rdpport != null) { port = node.rdpport; } else { port = 3389; } }
7950 + if (port == null) { if (node.rdpport != null) { port = node.rdpport; } else { port = 3389; } } // Adjust RDP port if needed
7951 meshserver.send({ action: 'getcookie', nodeid: nodeid, tcpport: port, tag: 'mstsc' });
7952 }
7953