From bd17264b05dd59ffaf446608b592145227941746 Mon Sep 17 00:00:00 2001 From: Ylian Saint-Hilaire Date: Sun, 31 Jan 2021 04:31:32 -0800 Subject: [PATCH] Added device console push notification support. --- firebase.js | 24 ++++++++++++++++++++---- meshcentral.js | 2 +- meshrelay.js | 9 ++++----- meshuser.js | 10 ++++------ views/default.handlebars | 2 +- 5 files changed, 30 insertions(+), 17 deletions(-) diff --git a/firebase.js b/firebase.js index 1bf752cb..71a11613 100644 --- a/firebase.js +++ b/firebase.js @@ -17,16 +17,26 @@ // Construct the Firebase object module.exports.CreateFirebase = function (parent, senderid, serverkey) { var obj = {}; - obj.messageId = 1; + obj.messageId = 0; const Sender = require('node-xcs').Sender; const Message = require('node-xcs').Message; const Notification = require('node-xcs').Notification; const xcs = new Sender(senderid, serverkey); + var tokenToNodeMap = {} // Token --> { nid: nodeid, mid: meshid } + // Messages received from client (excluding receipts) xcs.on('message', function (messageId, from, data, category) { - console.log('Firebase-Message', messageId, from, data, category); + //console.log('Firebase-Message', messageId, from, data, category); + + // Lookup node information from the cache + var ninfo = tokenToNodeMap[from]; + if (ninfo == null) return; + + if ((data != null) && (data.con != null) && (data.s != null)) { // Console command + parent.webserver.routeAgentCommand({ action: 'msg', type: 'console', value: data.con, sessionid: data.s }, ninfo.did, ninfo.nid, ninfo.mid); + } }); // Only fired for messages where options.delivery_receipt_requested = true @@ -41,11 +51,17 @@ module.exports.CreateFirebase = function (parent, senderid, serverkey) { xcs.start(); + // EXAMPLE //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' // Send an outbound push notification - obj.sendToDevice = function (token, payload, options, func) { + obj.sendToDevice = function (node, payload, options, func) { + if ((node == null) || (typeof node.pmt != 'string')) return; + + // Fill in our lookup table + tokenToNodeMap[node.pmt] = { nid: node._id, mid: node.meshid, did: node.domain } + // Built the on-screen notification var notification = null; if (payload.notification) { @@ -65,7 +81,7 @@ module.exports.CreateFirebase = function (parent, senderid, serverkey) { // Send the message function callback(result) { callback.func(result.getMessageId(), result.getError(), result.getErrorDescription()) } callback.func = func; - xcs.sendNoRetry(message, token, callback); + xcs.sendNoRetry(message, node.pmt, callback); } return obj; diff --git a/meshcentral.js b/meshcentral.js index 712ccb9f..3146bfa5 100644 --- a/meshcentral.js +++ b/meshcentral.js @@ -1546,7 +1546,7 @@ function CreateMeshCentralServer(config, args) { // Setup Firebase if ((config.firebase != null) && (typeof config.firebase.senderid == 'string') && (typeof config.firebase.serverkey == 'string')) { - obj.firebase = require('./firebase').CreateFirebase(this, config.firebase.senderid, config.firebase.serverkey); + obj.firebase = require('./firebase').CreateFirebase(obj, config.firebase.senderid, config.firebase.serverkey); } // Start periodic maintenance diff --git a/meshrelay.js b/meshrelay.js index aec63528..89391ec7 100644 --- a/meshrelay.js +++ b/meshrelay.js @@ -407,8 +407,7 @@ function CreateMeshRelayEx(parent, ws, req, domain, user, cookie) { 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; + obj.node = nodes[0]; // 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 } @@ -486,12 +485,12 @@ function CreateMeshRelayEx(parent, ws, req, domain, user, cookie) { 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.sendToDevice(obj.pmt, payload, options, function (id, err, errdesc) { + parent.parent.firebase.sendToDevice(obj.node, payload, options, function (id, err, errdesc) { if (err == null) { - parent.parent.debug('email', 'Successfully send push message to device ' + obj.nodename + ', title: ' + command.title + ', msg: ' + command.msg); + parent.parent.debug('email', 'Successfully send push message to device ' + obj.node.name + ', title: ' + command.title + ', msg: ' + command.msg); try { ws.send(JSON.stringify({ action: 'ctrl', value: 1 })); } catch (ex) { } // Push notification success } else { - parent.parent.debug('email', 'Failed to send push message to device ' + obj.nodename + ', title: ' + command.title + ', msg: ' + command.msg + ', error: ' + errdesc); + parent.parent.debug('email', 'Failed to send push message to device ' + obj.node.name + ', title: ' + command.title + ', msg: ' + command.msg + ', error: ' + errdesc); try { ws.send(JSON.stringify({ action: 'ctrl', value: 2 })); } catch (ex) { } // Push notification failed } }); diff --git a/meshuser.js b/meshuser.js index 52fdb590..139c8b42 100644 --- a/meshuser.js +++ b/meshuser.js @@ -5207,7 +5207,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use // 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 - parent.parent.firebase.sendToDevice(node.pmt, payload, options, function (id, err, errdesc) { + parent.parent.firebase.sendToDevice(node, payload, options, function (id, err, errdesc) { if (err == null) { parent.parent.debug('email', 'Successfully send push message to device ' + node.name + ', title: ' + command.title + ', msg: ' + command.msg); } else { @@ -5229,12 +5229,10 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use 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 payload = { data: { con: command.console, s: ws.sessionId } }; var options = { priority: "Normal", timeToLive: 60 }; // TTL: 1 minutes, priority 'Normal' or 'High' - parent.parent.firebase.sendToDevice(node.pmt, payload, options, function (id, err, errdesc) { - if (err == null) { - try { ws.send(JSON.stringify({ action: 'msg', type: 'console', nodeid: node._id, value: 'OK' })); } catch (ex) { } - } else { + parent.parent.firebase.sendToDevice(node, payload, options, function (id, err, errdesc) { + if (err != null) { try { ws.send(JSON.stringify({ action: 'msg', type: 'console', nodeid: node._id, value: 'Failed: ' + errdesc })); } catch (ex) { } parent.parent.debug('email', 'Failed to send push console message to device ' + node.name + ', command: ' + command.console + ', error: ' + errdesc); } diff --git a/views/default.handlebars b/views/default.handlebars index 4e12f4eb..8eb2ab85 100644 --- a/views/default.handlebars +++ b/views/default.handlebars @@ -9391,7 +9391,7 @@ t = '
' + "PUSH" + '> ' + EscapeHtml(v) + '
'; consoleNode.consoleText += t; meshserver.send({ action: 'pushconsole', nodeid: consoleNode._id, console: v }); - } else if ((consoleNode.conn & 1) == 0) { + } 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 });