Local device group improvements.

Ylian Saint-Hilaire committed Apr 28, 2021 at 00:10 UTC c4a60f4adcd9bd8c331eab0c97b59d73758c9e09
4 files changed +23 -9
meshrelay.js
+16 -6
@@ -958,6 +958,12 @@ function CreateLocalRelayEx(parent, ws, req, domain, user, cookie) {
958 // If there is no authentication, drop this connection
959 if (obj.user == null) { try { ws.close(); parent.parent.debug('relay', 'Relay: Connection with no authentication'); } catch (e) { console.log(e); } return; }
960
961 + // Use cookie values when present
962 + if (cookie != null) {
963 + if (cookie.nodeid) { req.query.nodeid = cookie.nodeid; }
964 + if (cookie.tcpport) { req.query.tcpport = cookie.tcpport; }
965 + }
966 +
967 // Check for nodeid and tcpport
968 if ((req.query == null) || (req.query.nodeid == null) || (req.query.tcpport == null)) { try { ws.close(); parent.parent.debug('relay', 'Relay: Connection with invalid arguments'); } catch (e) { console.log(e); } return; }
969 const tcpport = parseInt(req.query.tcpport);
@@ -1057,16 +1063,20 @@ function CreateLocalRelayEx(parent, ws, req, domain, user, cookie) {
1063
1064 // Setup TCP client
1065 obj.client = new net.Socket();
1060 - obj.client.connect(obj.tcpport, node.host, function () { ws.send('c'); ws._socket.resume(); });
1066 + obj.client.connect(obj.tcpport, node.host, function () {
1067 + // Log the start of the connection
1068 + obj.time = Date.now();
1069 + var event = { etype: 'relay', action: 'relaylog', domain: domain.id, userid: obj.user._id, username: obj.user.name, msgid: 13, msgArgs: [obj.id, obj.req.clientIp, obj.host], msg: 'Started relay session \"' + obj.id + '\" from ' + obj.req.clientIp + ' to ' + obj.host, nodeid: req.query.nodeid };
1070 + parent.parent.DispatchEvent(['*', obj.user._id, obj.meshid, obj.nodeid], obj, event);
1071 +
1072 + // Start the session
1073 + ws.send('c');
1074 + ws._socket.resume();
1075 + });
1076 obj.client.on('data', function (data) { try { this.pause(); ws.send(data, this.clientResume); } catch (ex) { console.log(ex); } }); // Perform relay
1077 obj.client.on('close', function () { obj.close(); });
1078 obj.client.on('error', function (err) { obj.close(); });
1079 obj.client.clientResume = function () { try { obj.client.resume(); } catch (ex) { console.log(ex); } };
1065 -
1066 - // Log the start of the connection
1067 - obj.time = Date.now();
1068 - var event = { etype: 'relay', action: 'relaylog', domain: domain.id, userid: obj.user._id, username: obj.user.name, msgid: 13, msgArgs: [obj.id, obj.req.clientIp, obj.host], msg: 'Started relay session \"' + obj.id + '\" from ' + obj.req.clientIp + ' to ' + obj.host, nodeid: req.query.nodeid };
1069 - parent.parent.DispatchEvent(['*', obj.user._id, obj.meshid, obj.nodeid], obj, event);
1080 });
1081 }
1082
meshuser.js
+1 -1
@@ -4315,9 +4315,9 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
4315 if (command.tcpaddr) { cookieContent.tcpaddr = command.tcpaddr; } // Indicates the browser want to agent to TCP connect to a remote address
4316 if (command.tcpport) { cookieContent.tcpport = command.tcpport; } // Indicates the browser want to agent to TCP connect to a remote port
4317 if (command.ip) { cookieContent.ip = command.ip; } // Indicates the browser want to agent to relay a TCP connection to a IP:port
4318 + if (node.mtype == 3) { cookieContent.lc = 1; command.localRelay = true; } // Indicate this is for a local connection
4319 command.cookie = parent.parent.encodeCookie(cookieContent, parent.parent.loginCookieEncryptionKey);
4320 command.trustedCert = parent.isTrustedCert(domain);
4320 - if (node.mtype == 3) { command.localRelay = true; }
4321 try { ws.send(JSON.stringify(command)); } catch (ex) { }
4322 });
4323 break;
mstsc.js
+5 -1
@@ -60,6 +60,10 @@ module.exports.CreateMstscRelay = function (parent, db, ws, req, args, domain) {
60 obj.relaySocket.on('end', function () { obj.close(); });
61 obj.relaySocket.on('error', function (err) { obj.close(); });
62
63 + // Decode the authentication cookie
64 + var cookie = parent.parent.decodeCookie(obj.infos.ip, parent.parent.loginCookieEncryptionKey);
65 + if (cookie == null) return;
66 +
67 // Setup the correct URL with domain and use TLS only if needed.
68 var options = { rejectUnauthorized: false };
69 if (domain.dns != null) { options.servername = domain.dns; }
@@ -67,7 +71,7 @@ module.exports.CreateMstscRelay = function (parent, db, ws, req, args, domain) {
71 if (args.tlsoffload) { protocol = 'ws'; }
72 var domainadd = '';
73 if ((domain.dns == null) && (domain.id != '')) { domainadd = domain.id + '/' }
70 - var url = protocol + '://127.0.0.1:' + args.port + '/' + domainadd + 'meshrelay.ashx?noping=1&auth=' + obj.infos.ip;
74 + var url = protocol + '://127.0.0.1:' + args.port + '/' + domainadd + ((cookie.lc == 1)?'local':'mesh') + 'relay.ashx?noping=1&auth=' + obj.infos.ip;
75 parent.parent.debug('relay', 'RDP: Connection websocket to ' + url);
76 obj.wsClient = new WebSocket(url, options);
77 obj.wsClient.on('open', function () { parent.parent.debug('relay', 'RDP: Relay websocket open'); });
views/default.handlebars
+1 -1
@@ -2476,7 +2476,7 @@
2476 var rdpurl = window.location.origin + domainUrl + 'mstsc.html?ws=' + message.cookie + (urlargs.key?('&key=' + urlargs.key):'');
2477 var node = getNodeFromId(message.nodeid);
2478 if (node != null) { rdpurl += '&name=' + encodeURIComponentEx(node.name); }
2479 - if (message.localRelay) { url += '&local=1'; }
2479 + if (message.localRelay) { rdpurl += '&local=1'; }
2480 safeNewWindow(rdpurl, 'mcmstsc/' + message.nodeid);
2481 }
2482 break;