diff --git a/amt/amt-ider-module.js b/amt/amt-ider-module.js index 4808482e67..13b590201d 100644 --- a/amt/amt-ider-module.js +++ b/amt/amt-ider-module.js @@ -175,7 +175,7 @@ module.exports.CreateAmtRemoteIder = function (webserver, meshcentral) { var attributes = ((cmdid > 50) && (completed == true)) ? 2 : 0; if (dma) { attributes += 1; } var x = Buffer.concat([Buffer.from([cmdid, 0, 0, attributes]), IntToStrX(obj.outSequence++), data]); - obj.parent.xxSend(x); + try { obj.parent.xxSend(x); } catch (ex) { } obj.bytesToAmt += x.length; //if (cmdid != 0x4B) { console.log('IDER-SendData', x.length, x.toString('hex')); } } @@ -651,4 +651,4 @@ function ReadShort(v, p) { return (v[p] << 8) + v[p + 1]; } function ReadShortX(v, p) { return (v[p + 1] << 8) + v[p]; } function ReadInt(v, p) { return (v[p] * 0x1000000) + (v[p + 1] << 16) + (v[p + 2] << 8) + v[p + 3]; } // We use "*0x1000000" instead of "<<24" because the shift converts the number to signed int32. function ReadSInt(v, p) { return (v[p] << 24) + (v[p + 1] << 16) + (v[p + 2] << 8) + v[p + 3]; } -function ReadIntX(v, p) { return (v[p + 3] * 0x1000000) + (v[p + 2] << 16) + (v[p + 1] << 8) + v[p]; } \ No newline at end of file +function ReadIntX(v, p) { return (v[p + 3] * 0x1000000) + (v[p + 2] << 16) + (v[p + 1] << 8) + v[p]; } diff --git a/interceptor.js b/interceptor.js index c6e4a87820..eb84c670b9 100644 --- a/interceptor.js +++ b/interceptor.js @@ -37,11 +37,13 @@ module.exports.CreateHttpInterceptor = function (args) { // Process data coming from Intel AMT obj.processAmtData = function (data) { - obj.amt.acc += data.toString('binary'); // Add data to accumulator - data = ''; - var datalen = 0; - do { datalen = data.length; data += obj.processAmtDataEx(); } while (datalen != data.length); // Process as much data as possible - return Buffer.from(data, 'binary'); + try { + obj.amt.acc += data.toString('binary'); // Add data to accumulator + data = ''; + var datalen = 0; + do { datalen = data.length; data += obj.processAmtDataEx(); } while (datalen != data.length); // Process as much data as possible + return Buffer.from(data, 'binary'); + } catch (ex) { obj.Debug('processAmtData exception: ' + ex); return Buffer.from('', 'binary'); } }; // Process data coming from AMT in the accumulator @@ -86,7 +88,7 @@ module.exports.CreateHttpInterceptor = function (args) { } else if (obj.amt.mode == 1) { // Length Body Mode // Send the body of content-length size var rl = obj.amt.count; - if (rl < obj.amt.acc.length) rl = obj.amt.acc.length; + if (rl > obj.amt.acc.length) rl = obj.amt.acc.length; r = obj.amt.acc.substring(0, rl); obj.amt.acc = obj.amt.acc.substring(rl); obj.amt.count -= rl; @@ -119,11 +121,13 @@ module.exports.CreateHttpInterceptor = function (args) { // Process data coming from the Browser obj.processBrowserData = function (data) { - obj.ws.acc += data.toString('binary'); // Add data to accumulator - data = ''; - var datalen = 0; - do { datalen = data.length; data += obj.processBrowserDataEx(); } while (datalen != data.length); // Process as much data as possible - return Buffer.from(data, 'binary'); + try { + obj.ws.acc += data.toString('binary'); // Add data to accumulator + data = ''; + var datalen = 0; + do { datalen = data.length; data += obj.processBrowserDataEx(); } while (datalen != data.length); // Process as much data as possible + return Buffer.from(data, 'binary'); + } catch (ex) { obj.Debug('processBrowserData exception: ' + ex); return Buffer.from('', 'binary'); } }; // Process data coming from the Browser in the accumulator @@ -197,7 +201,7 @@ module.exports.CreateHttpInterceptor = function (args) { } else if (obj.ws.mode == 1) { // Length Body Mode // Send the body of content-length size var rl = obj.ws.count; - if (rl < obj.ws.acc.length) rl = obj.ws.acc.length; + if (rl > obj.ws.acc.length) rl = obj.ws.acc.length; r = obj.ws.acc.substring(0, rl); obj.ws.acc = obj.ws.acc.substring(rl); obj.ws.count -= rl; @@ -286,12 +290,14 @@ module.exports.CreateRedirInterceptor = function (args) { // Process data coming from Intel AMT obj.processAmtData = function (data) { - if ((obj.amt.direct == true) && (obj.amt.acc == '')) { return data; } // Interceptor fast path - obj.amt.acc += data.toString('binary'); // Add data to accumulator - data = ''; - var datalen = 0; - do { datalen = data.length; data += obj.processAmtDataEx(); } while (datalen != data.length); // Process as much data as possible - return Buffer.from(data, 'binary'); + try { + if ((obj.amt.direct == true) && (obj.amt.acc == '')) { return data; } // Interceptor fast path + obj.amt.acc += data.toString('binary'); // Add data to accumulator + data = ''; + var datalen = 0; + do { datalen = data.length; data += obj.processAmtDataEx(); } while (datalen != data.length); // Process as much data as possible + return Buffer.from(data, 'binary'); + } catch (ex) { obj.Debug('processAmtData exception: ' + ex); return Buffer.from('', 'binary'); } }; // Process data coming from AMT in the accumulator @@ -354,12 +360,14 @@ module.exports.CreateRedirInterceptor = function (args) { // Process data coming from the Browser obj.processBrowserData = function (data) { - if ((obj.ws.direct == true) && (obj.ws.acc == '')) { return data; } // Interceptor fast path - obj.ws.acc += data.toString('binary'); // Add data to accumulator - data = ''; - var datalen = 0; - do { datalen = data.length; data += obj.processBrowserDataEx(); } while (datalen != data.length); // Process as much data as possible - return Buffer.from(data, 'binary'); + try { + if ((obj.ws.direct == true) && (obj.ws.acc == '')) { return data; } // Interceptor fast path + obj.ws.acc += data.toString('binary'); // Add data to accumulator + data = ''; + var datalen = 0; + do { datalen = data.length; data += obj.processBrowserDataEx(); } while (datalen != data.length); // Process as much data as possible + return Buffer.from(data, 'binary'); + } catch (ex) { obj.Debug('processBrowserData exception: ' + ex); return Buffer.from('', 'binary'); } }; // Process data coming from the Browser in the accumulator @@ -457,4 +465,4 @@ module.exports.CreateRedirInterceptor = function (args) { }; return obj; -}; \ No newline at end of file +}; diff --git a/meshbot.js b/meshbot.js index 5ff0de9b33..3ad67a2801 100644 --- a/meshbot.js +++ b/meshbot.js @@ -108,7 +108,7 @@ function serverConnect() { console.log('Connected at user: ' + data.userinfo.name); if ((args.targetuser != null) || (args.targetsession != null)) { console.log('Sending interuser message...'); - ws.send(JSON.stringify({ action: 'interuser', userid: args.targetuser, sessionid: args.targetsession, data: 'Hello!!!' })); // Send a hello message + try { ws.send(JSON.stringify({ action: 'interuser', userid: args.targetuser, sessionid: args.targetsession, data: 'Hello!!!' })); } catch (ex) { } // Send a hello message } break; } @@ -116,10 +116,11 @@ function serverConnect() { console.log('Got InterUser Message', data); if ((args.targetuser == null) && (args.targetsession == null) && (typeof data.data == 'string')) { // For testing, echo back the original message. console.log('Sending interuser echo...'); - ws.send(JSON.stringify({ action: 'interuser', sessionid: data.sessionid, data: 'ECHO: ' + data.data })); + try { ws.send(JSON.stringify({ action: 'interuser', sessionid: data.sessionid, data: 'ECHO: ' + data.data })); } catch (ex) { } } break; } } }); } + diff --git a/meshdevicefile.js b/meshdevicefile.js index 695a0f65b9..6098175b8d 100644 --- a/meshdevicefile.js +++ b/meshdevicefile.js @@ -22,6 +22,7 @@ module.exports.CreateMeshDeviceFile = function (parent, ws, res, req, domain, us obj.req = req; // Used in multi-server.js obj.id = req.query.id; obj.file = req.query.f; + if ((obj.file != null) && (obj.file.indexOf('..') >= 0)) { obj.file = null; } // Check relay authentication if ((user == null) && (obj.req.query != null) && (obj.req.query.rauth != null)) { @@ -58,8 +59,8 @@ module.exports.CreateMeshDeviceFile = function (parent, ws, res, req, domain, us // Disconnect obj.close = function (arg) { if (obj.ws != null) { - if ((arg == 1) || (arg == null)) { try { obj.ws.close(); parent.parent.debug('relay', 'FileRelay: Soft disconnect (' + obj.req.clientIp + ')'); } catch (ex) { console.log(e); } } // Soft close, close the websocket - if (arg == 2) { try { obj.ws._socket._parent.end(); parent.parent.debug('relay', 'FileRelay: Hard disconnect (' + obj.req.clientIp + ')'); } catch (ex) { console.log(e); } } // Hard close, close the TCP socket + if ((arg == 1) || (arg == null)) { try { obj.ws.close(); parent.parent.debug('relay', 'FileRelay: Soft disconnect (' + obj.req.clientIp + ')'); } catch (ex) { console.log(ex); } } // Soft close, close the websocket + if (arg == 2) { try { obj.ws._socket._parent.end(); parent.parent.debug('relay', 'FileRelay: Hard disconnect (' + obj.req.clientIp + ')'); } catch (ex) { console.log(ex); } } // Hard close, close the TCP socket } else if (obj.res != null) { try { res.sendStatus(404); } catch (ex) { } } @@ -132,7 +133,7 @@ module.exports.CreateMeshDeviceFile = function (parent, ws, res, req, domain, us // Check that at least one connection is authenticated if ((obj.authenticated != true) && (relayinfo.peer1.authenticated != true)) { - if (ws) { ws.close(); } + if (obj.ws) { obj.ws.close(); } parent.parent.debug('relay', 'FileRelay without-auth: ' + obj.id + ' (' + obj.req.clientIp + ')'); delete obj.id; delete obj.ws; @@ -256,7 +257,7 @@ module.exports.CreateMeshDeviceFile = function (parent, ws, res, req, domain, us // Disconnect the peer try { if (peer.relaySessionCounted) { parent.relaySessionCount--; delete peer.relaySessionCounted; } } catch (ex) { console.log(ex); } parent.parent.debug('relay', 'FileRelay disconnect: ' + obj.id + ' (' + obj.req.clientIp + ' --> ' + peer.req.clientIp + ')'); - if (peer.ws) { try { peer.ws.close(); } catch (e) { } try { peer.ws._socket._parent.end(); } catch (e) { } } + if (peer.ws) { try { peer.ws.close(); } catch (ex) { console.log(ex); } try { peer.ws._socket._parent.end(); } catch (ex) { console.log(ex); } } if (peer.res) { try { peer.res.end(); } catch (ex) { } } // Aggressive peer cleanup @@ -310,3 +311,4 @@ module.exports.CreateMeshDeviceFile = function (parent, ws, res, req, domain, us performRelay(); return obj; }; + diff --git a/public/mstsc/client.js b/public/mstsc/client.js index bc26d72ba7..cab17e3597 100644 --- a/public/mstsc/client.js +++ b/public/mstsc/client.js @@ -69,7 +69,7 @@ self.mouseNagleData = ['mouse', e.clientX - rect.left, e.clientY - rect.top, 0, false]; if (self.mouseNagleTimer == null) { //console.log('sending', self.mouseNagleData); - self.mouseNagleTimer = setTimeout(function () { self.socket.send(JSON.stringify(self.mouseNagleData)); self.mouseNagleTimer = null; }, 50); + self.mouseNagleTimer = setTimeout(function () { try { self.socket.send(JSON.stringify(self.mouseNagleData)); } catch (ex) { } self.mouseNagleTimer = null; }, 50); } //self.socket.send(JSON.stringify(this.mouseNagleData)); e.preventDefault(); @@ -79,7 +79,7 @@ if (!self.socket || !self.activeSession) return; if (self.mouseNagleTimer != null) { clearTimeout(self.mouseNagleTimer); self.mouseNagleTimer = null; } var rect = e.target.getBoundingClientRect(); - self.socket.send(JSON.stringify(['mouse', e.clientX - rect.left, e.clientY - rect.top, mouseButtonMap(e.button), true])); + try { self.socket.send(JSON.stringify(['mouse', e.clientX - rect.left, e.clientY - rect.top, mouseButtonMap(e.button), true])); } catch (ex) { } e.preventDefault(); return false; }); @@ -87,7 +87,7 @@ if (!self.socket || !self.activeSession) return; if (self.mouseNagleTimer != null) { clearTimeout(self.mouseNagleTimer); self.mouseNagleTimer = null; } var rect = e.target.getBoundingClientRect(); - self.socket.send(JSON.stringify(['mouse', e.clientX - rect.left, e.clientY - rect.top, mouseButtonMap(e.button), false])); + try { self.socket.send(JSON.stringify(['mouse', e.clientX - rect.left, e.clientY - rect.top, mouseButtonMap(e.button), false])); } catch (ex) { } e.preventDefault(); return false; }); @@ -95,7 +95,7 @@ if (!self.socket || !self.activeSession) return; if (self.mouseNagleTimer != null) { clearTimeout(self.mouseNagleTimer); self.mouseNagleTimer = null; } var rect = e.target.getBoundingClientRect(); - self.socket.send(JSON.stringify(['mouse', e.clientX - rect.left, e.clientY - rect.top, mouseButtonMap(e.button), false])); + try { self.socket.send(JSON.stringify(['mouse', e.clientX - rect.left, e.clientY - rect.top, mouseButtonMap(e.button), false])); } catch (ex) { } e.preventDefault(); return false; }); @@ -109,7 +109,7 @@ var step = 128; //console.log('DOMMouseScroll', delta, step, e.detail); var rect = e.target.getBoundingClientRect(); - self.socket.send(JSON.stringify(['wheel', e.clientX - rect.left, e.clientY - rect.top, step, delta > 0, isHorizontal])); + try { self.socket.send(JSON.stringify(['wheel', e.clientX - rect.left, e.clientY - rect.top, step, delta > 0, isHorizontal])); } catch (ex) { } e.preventDefault(); return false; }); @@ -122,7 +122,7 @@ var step = 128; //console.log('mousewheel', delta, step, e); var rect = e.target.getBoundingClientRect(); - self.socket.send(JSON.stringify(['wheel', e.clientX - rect.left, e.clientY - rect.top, step, delta > 0, isHorizontal])); + try { self.socket.send(JSON.stringify(['wheel', e.clientX - rect.left, e.clientY - rect.top, step, delta > 0, isHorizontal])); } catch (ex) { } e.preventDefault(); return false; }); @@ -130,13 +130,13 @@ // Bind keyboard event window.addEventListener('keydown', function (e) { if (!self.socket || !self.activeSession) return; - self.socket.send(JSON.stringify(['scancode', Mstsc.scancode(e), true])); + try { self.socket.send(JSON.stringify(['scancode', Mstsc.scancode(e), true])); } catch (ex) { } e.preventDefault(); return false; }); window.addEventListener('keyup', function (e) { if (!self.socket || !self.activeSession) return; - self.socket.send(JSON.stringify(['scancode', Mstsc.scancode(e), false])); + try { self.socket.send(JSON.stringify(['scancode', Mstsc.scancode(e), false])); } catch (ex) { } e.preventDefault(); return false; }); @@ -164,19 +164,21 @@ this.socket.binaryType = 'arraybuffer'; this.socket.onopen = function () { //console.log("WS-OPEN"); - self.socket.send(JSON.stringify(['infos', { - ip: ip, - port: 3389, - screen: { - width: self.canvas.width, - height: self.canvas.height - }, - domain: domain, - username: username, - password: password, - options: options, - locale: Mstsc.locale() - }])); + try { + self.socket.send(JSON.stringify(['infos', { + ip: ip, + port: 3389, + screen: { + width: self.canvas.width, + height: self.canvas.height + }, + domain: domain, + username: username, + password: password, + options: options, + locale: Mstsc.locale() + }])); + } catch (ex) { } self.prevClipboardText = null; self.clipboardReadTimer = setInterval(function(){ if(navigator.clipboard.readText != null){ @@ -185,7 +187,7 @@ .then(function(data){ if(data != self.prevClipboard){ self.prevClipboard = data; - if (self.socket) { self.socket.send(JSON.stringify(['clipboard', data])); } + if (self.socket) { try { self.socket.send(JSON.stringify(['clipboard', data])); } catch (ex) { } } } }) .catch(function(){ }); diff --git a/swarmserver.js b/swarmserver.js index 4bd1ae075f..e8039d2880 100644 --- a/swarmserver.js +++ b/swarmserver.js @@ -162,7 +162,7 @@ module.exports.CreateSwarmServer = function (parent, db, args, certificates) { // Relay this connection to the main TLS port this.pause(); - var relaySocket = tls.connect(obj.args.port, { rejectUnauthorized: false }, function () { this.write(this.parentSocket.tag.accumulator); this.parentSocket.resume(); }); + var relaySocket = tls.connect(obj.args.port, { rejectUnauthorized: false }, function () { try { this.write(this.parentSocket.tag.accumulator); this.parentSocket.resume(); } catch (ex) { } }); relaySocket.on('data', function (data) { try { var rs = this; this.pause(); this.parentSocket.write(data, 'binary', function () { rs.resume(); }); } catch (ex) { } }); relaySocket.on('error', function (err) { try { this.parentSocket.end(); } catch (ex) { } }); relaySocket.on('end', function () { try { this.parentSocket.end(); } catch (ex) { } }); @@ -176,7 +176,7 @@ module.exports.CreateSwarmServer = function (parent, db, args, certificates) { // A client certificate is required if ((this.tag.clientCert == null) || (this.tag.clientCert.subject == null)) { /*console.log("Swarm Connection, no client cert: " + socket.remoteAddress);*/ - this.write('HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nConnection: close\r\n\r\nMeshCentral2 legacy swarm server.\r\nNo client certificate given.'); + try { this.write('HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nConnection: close\r\n\r\nMeshCentral2 legacy swarm server.\r\nNo client certificate given.'); } catch (ex) { } //this.end(); // If we don't close the connection, it may lead to less reconnection traffic. return; } @@ -410,14 +410,16 @@ module.exports.CreateSwarmServer = function (parent, db, args, certificates) { function Write(socket, data) { obj.stats.bytesOut += data.length; - if (args.swarmdebug) { - // Print out sent bytes - var buf = Buffer.from(data, "binary"); - console.log('SWARM --> (' + buf.length + '):' + buf.toString('hex')); - socket.write(buf); - } else { - socket.write(Buffer.from(data, "binary")); - } + try { + if (args.swarmdebug) { + // Print out sent bytes + var buf = Buffer.from(data, "binary"); + console.log('SWARM --> (' + buf.length + '):' + buf.toString('hex')); + socket.write(buf); + } else { + socket.write(Buffer.from(data, "binary")); + } + } catch (ex) { } } // Check if the source IP address is allowed for a given allowed list, return false if not @@ -432,3 +434,4 @@ module.exports.CreateSwarmServer = function (parent, db, args, certificates) { return obj; }; +