More work on IP-KVM/PowerSwitch relay.

Ylian Saint-Hilaire committed Apr 18, 2022 at 16:06 UTC 4e2b334f025a7ed3e32fed10a08c1aae56f5057b
2 files changed +9 -6
meshipkvm.js
+2 -2
@@ -1005,9 +1005,9 @@ function CreateMiniRouter(parent, nodeid, targetHost, targetPort) {
1005 socket.on('end', function () { close(this); });
1006 socket.on('error', function (err) { close(this); });
1007
1008 - // Encode the device relay cookie. Note that there si no userid in this cookie.
1008 + // Encode the device relay cookie. Note that there is no userid in this cookie.
1009 const domainid = obj.nodeid.split('/')[1];
1010 - const cookie = parent.parent.encodeCookie({ domainid: domainid, nodeid: obj.nodeid, tcpaddr: obj.targetHost, tcpport: obj.targetPort }, parent.parent.loginCookieEncryptionKey);
1010 + const cookie = parent.parent.encodeCookie({ nouser: 1, domainid: domainid, nodeid: obj.nodeid, tcpaddr: obj.targetHost, tcpport: obj.targetPort }, parent.parent.loginCookieEncryptionKey);
1011 const domain = parent.parent.config.domains[domainid];
1012
1013 // Setup the correct URL with domain and use TLS only if needed.
meshrelay.js
+7 -4
@@ -867,16 +867,19 @@ function CreateMeshRelayEx(parent, ws, req, domain, user, cookie) {
867 const node = docs[0];
868
869 // Check if this user has permission to manage this computer
870 - if ((parent.GetNodeRights(user, node.meshid, node._id) & MESHRIGHT_REMOTECONTROL) == 0) { console.log('ERR: Access denied (1)'); try { obj.close(); } catch (e) { } return; }
870 + if ((obj.nouser !== true) && ((parent.GetNodeRights(user, node.meshid, node._id) & MESHRIGHT_REMOTECONTROL) == 0)) { console.log('ERR: Access denied (1)'); try { obj.close(); } catch (e) { } return; }
871
872 // Set nodeid and meshid
873 obj.nodeid = node._id;
874 obj.meshid = node.meshid;
875
876 // Send connection request to agent
877 - const rcookie = parent.parent.encodeCookie({ ruserid: user._id }, parent.parent.loginCookieEncryptionKey);
877 + const rcookieData = {};
878 + if (user != null) { rcookieData.ruserid = user._id; } else if (obj.nouser === true) { rcookieData.nouser = 1; }
879 + const rcookie = parent.parent.encodeCookie(rcookieData, parent.parent.loginCookieEncryptionKey);
880 if (obj.id == null) { obj.id = ('' + Math.random()).substring(2); } // If there is no connection id, generate one.
879 - const command = { nodeid: cookie.nodeid, action: 'msg', type: 'tunnel', userid: user._id, value: '*/' + xdomain + 'meshrelay.ashx?id=' + obj.id + '&rauth=' + rcookie, tcpport: cookie.tcpport, tcpaddr: cookie.tcpaddr, soptions: {} };
881 + const command = { nodeid: cookie.nodeid, action: 'msg', type: 'tunnel', value: '*/' + xdomain + 'meshrelay.ashx?id=' + obj.id + '&rauth=' + rcookie, tcpport: cookie.tcpport, tcpaddr: cookie.tcpaddr, soptions: {} };
882 + if (user) { command.userid = user._id; }
883 if (typeof domain.consentmessages == 'object') {
884 if (typeof domain.consentmessages.title == 'string') { command.soptions.consentTitle = domain.consentmessages.title; }
885 if (typeof domain.consentmessages.desktop == 'string') { command.soptions.consentMsgDesktop = domain.consentmessages.desktop; }
@@ -892,7 +895,7 @@ function CreateMeshRelayEx(parent, ws, req, domain, user, cookie) {
895 if (typeof domain.notificationmessages.files == 'string') { command.soptions.notifyMsgFiles = domain.notificationmessages.files; }
896 }
897 parent.parent.debug('relay', 'Relay: Sending agent tunnel command: ' + JSON.stringify(command));
895 - if (obj.sendAgentMessage(command, user._id, cookie.domainid) == false) { delete obj.id; parent.parent.debug('relay', 'Relay: Unable to contact this agent (' + obj.req.clientIp + ')'); }
898 + if (obj.sendAgentMessage(command, user?user._id:null, cookie.domainid) == false) { delete obj.id; parent.parent.debug('relay', 'Relay: Unable to contact this agent (' + obj.req.clientIp + ')'); }
899 performRelay();
900 });
901 return obj;