diff --git a/meshrelay.js b/meshrelay.js index 5a11d19e..46f1bdac 100644 --- a/meshrelay.js +++ b/meshrelay.js @@ -13,6 +13,28 @@ /*jshint esversion: 6 */ "use strict"; +// Mesh Rights +const MESHRIGHT_EDITMESH = 0x00000001; +const MESHRIGHT_MANAGEUSERS = 0x00000002; +const MESHRIGHT_MANAGECOMPUTERS = 0x00000004; +const MESHRIGHT_REMOTECONTROL = 0x00000008; +const MESHRIGHT_AGENTCONSOLE = 0x00000010; +const MESHRIGHT_SERVERFILES = 0x00000020; +const MESHRIGHT_WAKEDEVICE = 0x00000040; +const MESHRIGHT_SETNOTES = 0x00000080; +const MESHRIGHT_REMOTEVIEWONLY = 0x00000100; +const MESHRIGHT_NOTERMINAL = 0x00000200; +const MESHRIGHT_NOFILES = 0x00000400; +const MESHRIGHT_NOAMT = 0x00000800; +const MESHRIGHT_DESKLIMITEDINPUT = 0x00001000; +const MESHRIGHT_LIMITEVENTS = 0x00002000; +const MESHRIGHT_CHATNOTIFY = 0x00004000; +const MESHRIGHT_UNINSTALL = 0x00008000; +const MESHRIGHT_NODESKTOP = 0x00010000; +const MESHRIGHT_REMOTECOMMAND = 0x00020000; +const MESHRIGHT_RESETOFF = 0x00040000; +const MESHRIGHT_GUESTSHARING = 0x00080000; +const MESHRIGHT_ADMIN = 0xFFFFFFFF; function checkDeviceSharePublicIdentifier(parent, domain, nodeid, pid, func) { // Check the public id @@ -166,7 +188,14 @@ function CreateMeshRelayEx(parent, ws, req, domain, user, cookie) { } } return false; - }; + } + + // Push any stored message to the peer + obj.pushStoredMessages = function () { + if ((obj.storedPushedMessages != null) && (this.peer != null)) { + for (var i in obj.storedPushedMessages) { try { this.peer.send(JSON.stringify({ action: 'chat', msg: obj.storedPushedMessages[i] })); } catch (ex) { } } + } + } // Send a PING/PONG message function sendPing() { @@ -277,6 +306,10 @@ function CreateMeshRelayEx(parent, ws, req, domain, user, cookie) { // Do not record the session, just send session start try { ws.send('c'); } catch (ex) { } // Send connect to both peers try { relayinfo.peer1.ws.send('c'); } catch (ex) { } + + // Send any stored push messages + obj.pushStoredMessages(); + relayinfo.peer1.pushStoredMessages(); return; } } @@ -300,6 +333,10 @@ function CreateMeshRelayEx(parent, ws, req, domain, user, cookie) { parent.parent.debug('relay', 'Relay: Unable to record to file: ' + recFullFilename); try { ws.send('c'); } catch (ex) { } // Send connect to both peers try { relayinfo.peer1.ws.send('c'); } catch (ex) { } + + // Send any stored push messages + obj.pushStoredMessages(); + relayinfo.peer1.pushStoredMessages(); } else { // Write the recording file header parent.parent.debug('relay', 'Relay: Started recoding to file: ' + recFullFilename); @@ -312,10 +349,17 @@ function CreateMeshRelayEx(parent, ws, req, domain, user, cookie) { try { relayinfo.peer1.ws.logfile = ws.logfile = logfile; } catch (ex) { try { ws.send('c'); } catch (ex) { } // Send connect to both peers, 'cr' indicates the session is being recorded. try { relayinfo.peer1.ws.send('c'); } catch (ex) { } + // Send any stored push messages + obj.pushStoredMessages(); + relayinfo.peer1.pushStoredMessages(); return; } try { ws.send('cr'); } catch (ex) { } // Send connect to both peers, 'cr' indicates the session is being recorded. try { relayinfo.peer1.ws.send('cr'); } catch (ex) { } + + // Send any stored push messages + obj.pushStoredMessages(); + relayinfo.peer1.pushStoredMessages(); }); } }); @@ -324,6 +368,10 @@ function CreateMeshRelayEx(parent, ws, req, domain, user, cookie) { // Send session start try { ws.send('c'); } catch (ex) { } // Send connect to both peers try { relayinfo.peer1.ws.send('c'); } catch (ex) { } + + // Send any stored push messages + obj.pushStoredMessages(); + relayinfo.peer1.pushStoredMessages(); } parent.parent.debug('relay', 'Relay connected: ' + obj.id + ' (' + obj.req.clientIp + ' --> ' + obj.peer.req.clientIp + ')'); @@ -348,9 +396,29 @@ function CreateMeshRelayEx(parent, ws, req, domain, user, cookie) { } } else { // Wait for other relay connection - ws._socket.pause(); // Hold traffic until the other connection + if ((obj.id.startsWith('meshmessenger/node/') == true) && obj.authenticated && (parent.parent.firebase != null)) { + // This is an authenticated messenger session, push messaging may be allowed. Don't hold traffic. + ws._socket.resume(); // Don't hold traffic, process push messages + parent.parent.debug('relay', 'Relay messenger waiting: ' + obj.id + ' (' + obj.req.clientIp + ') ' + (obj.authenticated ? 'Authenticated' : '')); + + // Fetch the Push Messaging Token + const idsplit = obj.id.split('/'); + const nodeid = idsplit[1] + '/' + idsplit[2] + '/' + idsplit[3]; + parent.db.Get(nodeid, function (err, nodes) { + if ((err == null) && (nodes != null) && (nodes.length == 1) && (typeof nodes[0].pmt == 'string')) { + if ((parent.GetNodeRights(obj.user, nodes[0].meshid, nodes[0]._id) & MESHRIGHT_CHATNOTIFY) != 0) { + obj.pmt = nodes[0].pmt; + obj.nodename = nodes[0].name; + // Create the peer connection URL, we will include that in push messages + obj.msgurl = req.headers.origin + (req.url.split('/.websocket')[0].split('/meshrelay.ashx').join('/messenger')) + '?id=' + req.query.id + } + } + }); + } else { + ws._socket.pause(); // Hold traffic until the other connection + parent.parent.debug('relay', 'Relay holding: ' + obj.id + ' (' + obj.req.clientIp + ') ' + (obj.authenticated ? 'Authenticated' : '')); + } parent.wsrelays[obj.id] = { peer1: obj, state: 1, timeout: setTimeout(closeBothSides, 30000) }; - parent.parent.debug('relay', 'Relay holding: ' + obj.id + ' (' + obj.req.clientIp + ') ' + (obj.authenticated ? 'Authenticated' : '')); // Check if a peer server has this connection if (parent.parent.multiServer != null) { @@ -372,7 +440,6 @@ function CreateMeshRelayEx(parent, ws, req, domain, user, cookie) { // When data is received from the mesh relay web socket ws.on('message', function (data) { - //console.log(typeof data, data.length); if (this.peer != null) { //if (typeof data == 'string') { console.log('Relay: ' + data); } else { console.log('Relay:' + data.length + ' byte(s)'); } if (this.peer.slowRelay == null) { @@ -403,6 +470,31 @@ function CreateMeshRelayEx(parent, ws, req, domain, user, cookie) { } } catch (ex) { console.log(ex); } } + } else { + if ((typeof data == 'string') && (obj.pmt != null)) { + var command = null; + try { command = JSON.parse(data); } catch (ex) { return; } + if ((typeof command != 'object') || (command.action != 'chat') || (typeof command.msg != 'string') || (command.msg == '')) return; + + // Store pushed messages + if (obj.storedPushedMessages == null) { obj.storedPushedMessages = []; } + obj.storedPushedMessages.push(obj.storedPushedMessages.push(command.msg)); + while (obj.storedPushedMessages.length > 50) { obj.storedPushedMessages.shift(); } // Only keep last 50 notifications + + // Send out a push message to the device + command.title = (domain.title ? domain.title : 'MeshCentral'); + var payload = { notification: { title: command.title, body: command.msg }, data: { url: obj.msgurl } }; + var options = { priority: 'High', timeToLive: 5 * 60 }; // TTL: 5 minutes, priority 'Normal' or 'High' + parent.parent.firebase.messaging().sendToDevice(obj.pmt, payload, options) + .then(function (response) { + parent.parent.debug('email', 'Successfully send push message to device ' + obj.nodename + ', title: ' + command.title + ', msg: ' + command.msg); + try { ws.send(JSON.stringify({ action: 'ctrl', value: 1 })); } catch (ex) { } // Push notification success + }) + .catch(function (error) { + parent.parent.debug('email', 'Failed to send push message to device ' + obj.nodename + ', title: ' + command.title + ', msg: ' + command.msg + ', error: ' + error); + try { ws.send(JSON.stringify({ action: 'ctrl', value: 2 })); } catch (ex) { } // Push notification failed + }); + } } }); diff --git a/meshuser.js b/meshuser.js index 0843cad5..260bfbc2 100644 --- a/meshuser.js +++ b/meshuser.js @@ -5203,7 +5203,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use db.Get(command.nodeid, function (err, nodes) { // TODO: Make a NodeRights(user) method that also does not do a db call if agent is connected (???) if ((err == null) && (nodes.length == 1)) { const node = nodes[0]; - if (((parent.GetMeshRights(user, node.meshid) & MESHRIGHT_REMOTECONTROL) != 0) && (typeof node.pmt == 'string')) { + if (((parent.GetNodeRights(user, node.meshid, node._id) & MESHRIGHT_CHATNOTIFY) != 0) && (typeof node.pmt == 'string')) { // Send out a push message to the device var payload = { notification: { title: command.title, body: command.msg } }; var options = { priority: "Normal", timeToLive: 5 * 60 }; // TTL: 5 minutes @@ -5219,6 +5219,31 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use }); break; } + case 'pushconsole': { + // Check if this user has rights on this nodeid + if (parent.parent.firebase == null) return; + if (common.validateString(command.nodeid, 1, 1024) == false) break; // Check nodeid + if (common.validateString(command.console, 1, 3000) == false) break; // Check console command + db.Get(command.nodeid, function (err, nodes) { // TODO: Make a NodeRights(user) method that also does not do a db call if agent is connected (???) + if ((err == null) && (nodes.length == 1)) { + const node = nodes[0]; + if ((parent.GetNodeRights(user, node.meshid, node._id) == MESHRIGHT_ADMIN) && (typeof node.pmt == 'string')) { + // Send out a push message to the device + var payload = { data: { console: command.console, session: ws.sessionId } }; + var options = { priority: "Normal", timeToLive: 60 }; // TTL: 1 minutes, priority 'Normal' or 'High' + parent.parent.firebase.messaging().sendToDevice(node.pmt, payload, options) + .then(function (response) { + try { ws.send(JSON.stringify({ action: 'msg', type: 'console', nodeid: node._id, value: 'OK' })); } catch (ex) { } + }) + .catch(function (error) { + try { ws.send(JSON.stringify({ action: 'msg', type: 'console', nodeid: node._id, value: 'Failed: ' + error })); } catch (ex) { } + parent.parent.debug('email', 'Failed to send push console message to device ' + node.name + ', command: ' + command.console + ', error: ' + error); + }); + } + } + }); + break; + } case 'print': { console.log(command.value); break; diff --git a/views/default.handlebars b/views/default.handlebars index 78a69408..5678851c 100644 --- a/views/default.handlebars +++ b/views/default.handlebars @@ -823,9 +823,10 @@   - + + + @@ -5976,7 +5977,7 @@ x += ''; if ((meshrights & 8) && ((connectivity & 1) || (node.pmt == 1))) { x += ''; } //if ((connectivity & 1) && (meshrights & 8) && (node.agent.id < 5)) { x += ''; } - if ((connectivity & 1) && (meshrights & 8) && (node.agent.id == 14)) { x += ''; } + if ((meshrights & 8) && (connectivity & 1) || (node.pmt == 1)) { x += ''; } if ((serverinfo.guestdevicesharing !== false) && (node.agent != null) && (node.agent.caps & 3) && (connectivity & 1) && (meshrights & 0x80008) && ((meshrights == 0xFFFFFFFF) || ((meshrights & 0x1000) == 0))) { x += ''; } // Custom UI @@ -6302,6 +6303,7 @@ if (xxdialogMode) return; var url = '/messenger?id=meshmessenger/' + encodeURIComponentEx(currentNode._id) + '/' + encodeURIComponentEx(userinfo._id) + '&title=' + currentNode.name; if ((authCookie != null) && (authCookie != '')) { url += '&auth=' + authCookie; } + if (currentNode.pmt == 1) { url += '&pmt=1'; } // Push messaging is possible for this device if (e && (e.shiftKey == true)) { safeNewWindow(url, 'meshmessenger:' + currentNode._id); } else { @@ -9333,9 +9335,20 @@ var onlineText = ((consoleNode.conn & 1) != 0) ? "Agent is online" : "Agent is offline" if ((consoleNode.conn & 16) != 0) { onlineText += ", MQTT is online" } QH('p15statetext', onlineText); - QE('p15consoleText', online); QE('p15uploadCore', ((consoleNode.conn & 1) != 0)); - QV('p15outputselecttd', (consoleNode.conn & 17) == 17); + QV('p15outputselecttd', ((consoleNode.conn & 16) != 0) || (currentNode.pmt == 1)); + QV('p15outputselect2', ((consoleNode.conn & 16) != 0)); // MQTT channel + QV('p15outputselect3', (currentNode.pmt == 1)); // Push Notification channel + + var c = Q('p15outputselect').value; + if (((consoleNode.conn & 16) == 0) && (c == 2)) { c = 1; Q('p15outputselect').value = 1; } + if ((currentNode.pmt != 1) && (c == 3)) { c = 1; Q('p15outputselect').value = 1; } + + var active = false; + if (((consoleNode.conn & 1) != 0) && (c == 1)) { active = true; } // Agent + if (((consoleNode.conn & 16) != 0) && (c == 2)) { active = true; } // MQTT + if ((consoleNode.pmt == 1) && (c == 3)) { active = true; } // Push + QE('p15consoleText', active); } else { QH('p15statetext', "Access Denied"); QE('p15consoleText', false); @@ -9368,12 +9381,17 @@ consoleServerText += t; meshserver.send({ action: 'serverconsole', value: v }); } else { - if (((consoleNode.conn & 16) != 0) && ((Q('p15outputselect').value == 2) || ((consoleNode.conn & 1) == 0))) { + if (((consoleNode.conn & 16) != 0) && (Q('p15outputselect').value == 2)) { // Send the command to MQTT t = '
' + "MQTT" + '> ' + EscapeHtml(v) + '
'; consoleNode.consoleText += t; meshserver.send({ action: 'sendmqttmsg', topic: 'console', nodeids: [ consoleNode._id ], msg: v }); - } else { + } else if ((consoleNode.pmt == 1) && (Q('p15outputselect').value == 3)) { + // Send the command using push notification + t = '
' + "PUSH" + '> ' + EscapeHtml(v) + '
'; + consoleNode.consoleText += t; + meshserver.send({ action: 'pushconsole', nodeid: consoleNode._id, console: v }); + } else if ((consoleNode.conn & 1) == 0) { // Send the command to the mesh agent consoleNode.consoleText += t; meshserver.send({ action: 'msg', type: 'console', nodeid: consoleNode._id, value: v }); diff --git a/views/messenger.handlebars b/views/messenger.handlebars index ded17169..813c7e1a 100644 --- a/views/messenger.handlebars +++ b/views/messenger.handlebars @@ -63,6 +63,7 @@ var args = XparseUriArgs(); if (args.key && (isAlphaNumeric(args.key) == false)) { delete args.key; } if (args.locale && (isAlphaNumeric(args.locale) == false)) { delete args.locale; } + var pushMessaging = (args.pmt == 1); // WebRTC sessions and data, audio and video channels var random = Math.random(); // Selected random, larger value initiates WebRTC. @@ -144,7 +145,7 @@ document.onkeypress = function ondockeypress(e) { if (Notification) { QV('notifyButton', Notification.permission != 'granted'); } if (notification != null) { notification.close(); notification = null; } - if (state == 2) { + if ((state == 2) || pushMessaging) { if (e.keyCode == 13) { // Return xsend(e); @@ -229,7 +230,7 @@ chatTextSession += ((new Date()).toLocaleTimeString() + ' - ' + "Local" + '> ' + outtext + '\r\n'); QA('xmsg', '
' + EscapeHtml(outtext) + '
'); Q('xmsgparent').scrollTop = Q('xmsgparent').scrollHeight; - send({ action: 'chat', msg: outtext }); + if ((state == 2) || pushMessaging) { send({ action: 'chat', msg: outtext }); } Q('xouttext').value = ''; Q('xouttext').focus(); } @@ -239,9 +240,9 @@ // Update user controls function updateControls() { - QE('sendButton', state == 2); - QE('clearButton', state == 2); - QE('xouttext', state == 2); + QE('sendButton', (state == 2) || pushMessaging); + QE('clearButton', (state == 2) || pushMessaging); + QE('xouttext', (state == 2) || pushMessaging); QV('fileButton', state == 2); QV('camButton', webchannel && webchannel.ok && !localStream && (userMediaSupport == 2)); QV('micButton', webchannel && webchannel.ok && !localStream && (userMediaSupport > 0)); @@ -348,7 +349,7 @@ // Send data over the current transport (WebRTC first) function send(data) { - if (state != 2) return; // If not in connected state, ignore this. + if ((state != 2) && (pushMessaging == false)) return; // If not in connected state, ignore this. if (typeof data == 'object') { data = JSON.stringify(data); } // If this is an object, convert it to a string. if (webchannel && webchannel.ok) { if (webchannel.xoutBuffer != null) { webchannel.xoutBuffer.push(data); } else { webchannel.send(data); } } // If WebRTC channel is possible, use it or hold until we can use it. else { if (socket != null) { try { socket.send(data); } catch (ex) { } } } // If a websocket channel is present, use that. @@ -372,6 +373,12 @@ //console.log('RECV', data); switch (data.action) { case 'chat': { displayRemote(data.msg); break; } // Incoming chat message. + case 'ctrl': { + if (data.value == 1) { displayControl("Sent as push notification."); } + else if (data.value == 2) { displayControl("Push notification failed."); } + if (data.msg != null) { displayControl(msg); } + break; + } case 'random': { if (random > data.random) { startWebRTC(0, true); } break; } // If we have a larger random value, we start WebRTC. case 'webRtcSdp': { if (!webrtcSessions[webRtcIdSwitch(data.id)]) { startWebRTC(webRtcIdSwitch(data.id), false); } webRtcHandleOffer(webRtcIdSwitch(data.id), data.sdp); break; } // Remote WebRTC offer or answer. case 'webRtcIce': { var webrtc = webrtcSessions[webRtcIdSwitch(data.id)]; if (webrtc) { try { webrtc.addIceCandidate(new RTCIceCandidate(data.ice)); } catch (ex) { } } break; } // Remote ICE candidate @@ -683,7 +690,7 @@ sendws({ action: 'random', random: random }); // Send a random number. Higher number starts the WebRTC session. return; } - if (state == 2) { processMessage(msg.data, 1); } + if (msg.data[0] == '{') { processMessage(msg.data, 1); } } } else { displayControl("Error: No connection key specified.");