Fixed local protocol events, #2813

Ylian Saint-Hilaire committed Jun 28, 2021 at 19:26 UTC e373cec94350b6098eba546a1d43ec29e5cf4fc7
3 files changed +32 -10
apprelays.js
+11 -1
@@ -13,6 +13,16 @@
13 /*jshint esversion: 6 */
14 "use strict";
15
16 +
17 +/*
18 +Protocol numbers
19 +10 = RDP
20 +11 = SSH-TERM
21 +12 = VNC
22 +13 - SSH-FILES
23 +*/
24 +
25 +
26 // Construct a MSTSC Relay object, called upon connection
27 // This is a bit of a hack as we are going to run the RDP connection thru a loopback connection.
28 // If the "node-rdpjs-2" module supported passing a socket, we would do something different.
@@ -679,7 +689,7 @@ module.exports.CreateSshFilesRelay = function (parent, db, ws, req, domain, user
689 if (args.tlsoffload) { protocol = 'ws'; }
690 var domainadd = '';
691 if ((domain.dns == null) && (domain.id != '')) { domainadd = domain.id + '/' }
682 - 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
692 + 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
693 parent.parent.debug('relay', 'SSH: Connection websocket to ' + url);
694 obj.wsClient = new WebSocket(url, options);
695 obj.wsClient.on('open', function () { parent.parent.debug('relay', 'SSH: Relay websocket open'); });
meshrelay.js
+18 -8
@@ -1013,7 +1013,7 @@ function CreateLocalRelayEx(parent, ws, req, domain, user, cookie) {
1013 if (typeof protocolInUse != 'number') { protocolInUse = 0; }
1014
1015 // If there is no authentication, drop this connection
1016 - if (obj.user == null) { try { ws.close(); parent.parent.debug('relay', 'Relay: Connection with no authentication'); } catch (e) { console.log(e); } return; }
1016 + if (obj.user == null) { try { ws.close(); parent.parent.debug('relay', 'LocalRelay: Connection with no authentication'); } catch (e) { console.log(e); } return; }
1017
1018 // Use cookie values when present
1019 if (cookie != null) {
@@ -1022,11 +1022,11 @@ function CreateLocalRelayEx(parent, ws, req, domain, user, cookie) {
1022 }
1023
1024 // Check for nodeid and tcpport
1025 - 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; }
1025 + if ((req.query == null) || (req.query.nodeid == null) || (req.query.tcpport == null)) { try { ws.close(); parent.parent.debug('relay', 'LocalRelay: Connection with invalid arguments'); } catch (e) { console.log(e); } return; }
1026 const tcpport = parseInt(req.query.tcpport);
1027 - if ((typeof tcpport != 'number') || (tcpport < 1) || (tcpport > 65535)) { try { ws.close(); parent.parent.debug('relay', 'Relay: Connection with invalid arguments'); } catch (e) { console.log(e); } return; }
1027 + if ((typeof tcpport != 'number') || (tcpport < 1) || (tcpport > 65535)) { try { ws.close(); parent.parent.debug('relay', 'LocalRelay: Connection with invalid arguments'); } catch (e) { console.log(e); } return; }
1028 var nodeidsplit = req.query.nodeid.split('/');
1029 - if ((nodeidsplit.length != 3) || (nodeidsplit[0] != 'node') || (nodeidsplit[1] != domain.id) || (nodeidsplit[2].length < 10)) { try { ws.close(); parent.parent.debug('relay', 'Relay: Connection with invalid arguments'); } catch (e) { console.log(e); } return; }
1029 + if ((nodeidsplit.length != 3) || (nodeidsplit[0] != 'node') || (nodeidsplit[1] != domain.id) || (nodeidsplit[2].length < 10)) { try { ws.close(); parent.parent.debug('relay', 'LocalRelay: Connection with invalid arguments'); } catch (e) { console.log(e); } return; }
1030 obj.nodeid = req.query.nodeid;
1031 obj.tcpport = tcpport;
1032
@@ -1096,15 +1096,20 @@ function CreateLocalRelayEx(parent, ws, req, domain, user, cookie) {
1096 if (obj.client != null) { inTraffc += obj.client.bytesRead; outTraffc += obj.client.bytesWritten; }
1097
1098 // Close the web socket
1099 - if ((arg == 1) || (arg == null)) { try { obj.ws.close(); parent.parent.debug('relay', 'Relay: Soft disconnect'); } catch (e) { console.log(e); } } // Soft close, close the websocket
1100 - if (arg == 2) { try { obj.ws._socket._parent.end(); parent.parent.debug('relay', 'Relay: Hard disconnect'); } catch (e) { console.log(e); } } // Hard close, close the TCP socket
1099 + if ((arg == 1) || (arg == null)) { try { obj.ws.close(); parent.parent.debug('relay', 'LocalRelay: Soft disconnect'); } catch (e) { console.log(e); } } // Soft close, close the websocket
1100 + if (arg == 2) { try { obj.ws._socket._parent.end(); parent.parent.debug('relay', 'LocalRelay: Hard disconnect'); } catch (e) { console.log(e); } } // Hard close, close the TCP socket
1101
1102 // Update the relay session count
1103 if (obj.relaySessionCounted) { parent.relaySessionCount--; delete obj.relaySessionCounted; }
1104
1105 // Log the disconnection, traffic will be credited to the authenticated user
1106 if (obj.time) {
1107 - var event = { etype: 'relay', action: 'relaylog', domain: domain.id, userid: obj.user._id, username: obj.user.name, msgid: 9, msgArgs: [obj.id, obj.req.clientIp, obj.host, Math.floor((Date.now() - obj.time) / 1000)], msg: 'Ended relay session \"' + obj.id + '\" from ' + obj.req.clientIp + ' to ' + obj.host + ', ' + Math.floor((Date.now() - obj.time) / 1000) + ' second(s)', nodeid: obj.req.query.nodeid, protocol: req.query.p, in: inTraffc, out: outTraffc };
1107 + var protocolStr = req.query.p;
1108 + if (req.query.p == 10) { protocolStr = 'RDP'; }
1109 + else if (req.query.p == 11) { protocolStr = 'SSH-TERM'; }
1110 + else if (req.query.p == 12) { protocolStr = 'VNC'; }
1111 + else if (req.query.p == 13) { protocolStr = 'SSH-FILES'; }
1112 + var event = { etype: 'relay', action: 'relaylog', domain: domain.id, userid: obj.user._id, username: obj.user.name, msgid: 121, msgArgs: [obj.id, protocolStr, obj.host, Math.floor((Date.now() - obj.time) / 1000)], msg: 'Ended local relay session \"' + obj.id + '\", protocol ' + protocolStr + ' to ' + obj.host + ', ' + Math.floor((Date.now() - obj.time) / 1000) + ' second(s)', nodeid: obj.req.query.nodeid, protocol: req.query.p, in: inTraffc, out: outTraffc };
1113 parent.parent.DispatchEvent(['*', user._id], obj, event);
1114 }
1115
@@ -1152,8 +1157,13 @@ function CreateLocalRelayEx(parent, ws, req, domain, user, cookie) {
1157 obj.client.bytesWrittenEx = 0;
1158 obj.client.connect(obj.tcpport, node.host, function () {
1159 // Log the start of the connection
1160 + var protocolStr = req.query.p;
1161 + if (req.query.p == 10) { protocolStr = 'RDP'; }
1162 + else if (req.query.p == 11) { protocolStr = 'SSH-TERM'; }
1163 + else if (req.query.p == 12) { protocolStr = 'VNC'; }
1164 + else if (req.query.p == 13) { protocolStr = 'SSH-FILES'; }
1165 obj.time = Date.now();
1156 - 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, protocol: req.query.p };
1166 + var event = { etype: 'relay', action: 'relaylog', domain: domain.id, userid: obj.user._id, username: obj.user.name, msgid: 120, msgArgs: [obj.id, protocolStr, obj.host], msg: 'Started local relay session \"' + obj.id + '\", protocol ' + protocolStr + ' to ' + obj.host, nodeid: req.query.nodeid, protocol: req.query.p };
1167 parent.parent.DispatchEvent(['*', obj.user._id, obj.meshid, obj.nodeid], obj, event);
1168
1169 // Count the session
views/default.handlebars
+3 -1
@@ -12483,7 +12483,9 @@
12483 116: "Removed login token",
12484 117: "This is an old agent version, consider updating.",
12485 118: "This agent has an outstated certificate validation mechanism, consider updating.",
12486 - 119: "This agent is using insecure tunnels, consider updating."
12486 + 119: "This agent is using insecure tunnels, consider updating.",
12487 + 120: "Started local relay session \"{0}\", protocol {1} to {2}",
12488 + 121: "Ended local relay session \"{0}\", protocol {1} to {2}, {3} second(s)"
12489 };
12490
12491 // Highlights the device being hovered