Added device console push notification support.
Ylian Saint-Hilaire committed
Jan 31, 2021 at 04:31 UTC
bd17264b05dd59ffaf446608b592145227941746
5 files changed
+30
-17
firebase.js
+20
-4
@@ -17,16 +17,26 @@
17
// Construct the Firebase object
18
module.exports.CreateFirebase = function (parent, senderid, serverkey) {
19
var obj = {};
20
- obj.messageId = 1;
20
+ obj.messageId = 0;
21
22
const Sender = require('node-xcs').Sender;
23
const Message = require('node-xcs').Message;
24
const Notification = require('node-xcs').Notification;
25
const xcs = new Sender(senderid, serverkey);
26
27
+ var tokenToNodeMap = {} // Token --> { nid: nodeid, mid: meshid }
28
+
29
// Messages received from client (excluding receipts)
30
xcs.on('message', function (messageId, from, data, category) {
29
- console.log('Firebase-Message', messageId, from, data, category);
31
+ //console.log('Firebase-Message', messageId, from, data, category);
32
+
33
+ // Lookup node information from the cache
34
+ var ninfo = tokenToNodeMap[from];
35
+ if (ninfo == null) return;
36
+
37
+ if ((data != null) && (data.con != null) && (data.s != null)) { // Console command
38
+ parent.webserver.routeAgentCommand({ action: 'msg', type: 'console', value: data.con, sessionid: data.s }, ninfo.did, ninfo.nid, ninfo.mid);
39
+ }
40
});
41
42
// Only fired for messages where options.delivery_receipt_requested = true
@@ -41,11 +51,17 @@ module.exports.CreateFirebase = function (parent, senderid, serverkey) {
51
52
xcs.start();
53
54
+ // EXAMPLE
55
//var payload = { notification: { title: command.title, body: command.msg }, data: { url: obj.msgurl } };
56
//var options = { priority: 'High', timeToLive: 5 * 60 }; // TTL: 5 minutes, priority 'Normal' or 'High'
57
58
// Send an outbound push notification
48
- obj.sendToDevice = function (token, payload, options, func) {
59
+ obj.sendToDevice = function (node, payload, options, func) {
60
+ if ((node == null) || (typeof node.pmt != 'string')) return;
61
+
62
+ // Fill in our lookup table
63
+ tokenToNodeMap[node.pmt] = { nid: node._id, mid: node.meshid, did: node.domain }
64
+
65
// Built the on-screen notification
66
var notification = null;
67
if (payload.notification) {
@@ -65,7 +81,7 @@ module.exports.CreateFirebase = function (parent, senderid, serverkey) {
81
// Send the message
82
function callback(result) { callback.func(result.getMessageId(), result.getError(), result.getErrorDescription()) }
83
callback.func = func;
68
- xcs.sendNoRetry(message, token, callback);
84
+ xcs.sendNoRetry(message, node.pmt, callback);
85
}
86
87
return obj;
meshcentral.js
+1
-1
@@ -1546,7 +1546,7 @@ function CreateMeshCentralServer(config, args) {
1546
1547
// Setup Firebase
1548
if ((config.firebase != null) && (typeof config.firebase.senderid == 'string') && (typeof config.firebase.serverkey == 'string')) {
1549
- obj.firebase = require('./firebase').CreateFirebase(this, config.firebase.senderid, config.firebase.serverkey);
1549
+ obj.firebase = require('./firebase').CreateFirebase(obj, config.firebase.senderid, config.firebase.serverkey);
1550
}
1551
1552
// Start periodic maintenance
meshrelay.js
+4
-5
@@ -407,8 +407,7 @@ function CreateMeshRelayEx(parent, ws, req, domain, user, cookie) {
407
parent.db.Get(nodeid, function (err, nodes) {
408
if ((err == null) && (nodes != null) && (nodes.length == 1) && (typeof nodes[0].pmt == 'string')) {
409
if ((parent.GetNodeRights(obj.user, nodes[0].meshid, nodes[0]._id) & MESHRIGHT_CHATNOTIFY) != 0) {
410
- obj.pmt = nodes[0].pmt;
411
- obj.nodename = nodes[0].name;
410
+ obj.node = nodes[0];
411
// Create the peer connection URL, we will include that in push messages
412
obj.msgurl = req.headers.origin + (req.url.split('/.websocket')[0].split('/meshrelay.ashx').join('/messenger')) + '?id=' + req.query.id
413
}
@@ -486,12 +485,12 @@ function CreateMeshRelayEx(parent, ws, req, domain, user, cookie) {
485
command.title = (domain.title ? domain.title : 'MeshCentral');
486
var payload = { notification: { title: command.title, body: command.msg }, data: { url: obj.msgurl } };
487
var options = { priority: 'High', timeToLive: 5 * 60 }; // TTL: 5 minutes, priority 'Normal' or 'High'
489
- parent.parent.firebase.sendToDevice(obj.pmt, payload, options, function (id, err, errdesc) {
488
+ parent.parent.firebase.sendToDevice(obj.node, payload, options, function (id, err, errdesc) {
489
if (err == null) {
491
- parent.parent.debug('email', 'Successfully send push message to device ' + obj.nodename + ', title: ' + command.title + ', msg: ' + command.msg);
490
+ parent.parent.debug('email', 'Successfully send push message to device ' + obj.node.name + ', title: ' + command.title + ', msg: ' + command.msg);
491
try { ws.send(JSON.stringify({ action: 'ctrl', value: 1 })); } catch (ex) { } // Push notification success
492
} else {
494
- parent.parent.debug('email', 'Failed to send push message to device ' + obj.nodename + ', title: ' + command.title + ', msg: ' + command.msg + ', error: ' + errdesc);
493
+ parent.parent.debug('email', 'Failed to send push message to device ' + obj.node.name + ', title: ' + command.title + ', msg: ' + command.msg + ', error: ' + errdesc);
494
try { ws.send(JSON.stringify({ action: 'ctrl', value: 2 })); } catch (ex) { } // Push notification failed
495
}
496
});
meshuser.js
+4
-6
@@ -5207,7 +5207,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
5207
// Send out a push message to the device
5208
var payload = { notification: { title: command.title, body: command.msg } };
5209
var options = { priority: "Normal", timeToLive: 5 * 60 }; // TTL: 5 minutes
5210
- parent.parent.firebase.sendToDevice(node.pmt, payload, options, function (id, err, errdesc) {
5210
+ parent.parent.firebase.sendToDevice(node, payload, options, function (id, err, errdesc) {
5211
if (err == null) {
5212
parent.parent.debug('email', 'Successfully send push message to device ' + node.name + ', title: ' + command.title + ', msg: ' + command.msg);
5213
} else {
@@ -5229,12 +5229,10 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
5229
const node = nodes[0];
5230
if ((parent.GetNodeRights(user, node.meshid, node._id) == MESHRIGHT_ADMIN) && (typeof node.pmt == 'string')) {
5231
// Send out a push message to the device
5232
- var payload = { data: { console: command.console, session: ws.sessionId } };
5232
+ var payload = { data: { con: command.console, s: ws.sessionId } };
5233
var options = { priority: "Normal", timeToLive: 60 }; // TTL: 1 minutes, priority 'Normal' or 'High'
5234
- parent.parent.firebase.sendToDevice(node.pmt, payload, options, function (id, err, errdesc) {
5235
- if (err == null) {
5236
- try { ws.send(JSON.stringify({ action: 'msg', type: 'console', nodeid: node._id, value: 'OK' })); } catch (ex) { }
5237
- } else {
5234
+ parent.parent.firebase.sendToDevice(node, payload, options, function (id, err, errdesc) {
5235
+ if (err != null) {
5236
try { ws.send(JSON.stringify({ action: 'msg', type: 'console', nodeid: node._id, value: 'Failed: ' + errdesc })); } catch (ex) { }
5237
parent.parent.debug('email', 'Failed to send push console message to device ' + node.name + ', command: ' + command.console + ', error: ' + errdesc);
5238
}
views/default.handlebars
+1
-1
@@ -9391,7 +9391,7 @@
9391
t = '<div style=color:violet>' + "PUSH" + '> ' + EscapeHtml(v) + '<br/></div>';
9392
consoleNode.consoleText += t;
9393
meshserver.send({ action: 'pushconsole', nodeid: consoleNode._id, console: v });
9394
- } else if ((consoleNode.conn & 1) == 0) {
9394
+ } else if ((consoleNode.conn & 1) != 0) {
9395
// Send the command to the mesh agent
9396
consoleNode.consoleText += t;
9397
meshserver.send({ action: 'msg', type: 'console', nodeid: consoleNode._id, value: v });