firebase.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /**
  2. * @description MeshCentral Firebase communication module
  3. * @author Ylian Saint-Hilaire
  4. * @copyright Intel Corporation 2018-2022
  5. * @license Apache-2.0
  6. * @version v0.0.1
  7. */
  8. /*xjslint node: true */
  9. /*xjslint plusplus: true */
  10. /*xjslint maxlen: 256 */
  11. /*jshint node: true */
  12. /*jshint strict: false */
  13. /*jshint esversion: 6 */
  14. "use strict";
  15. // Construct the Firebase object
  16. module.exports.CreateFirebase = function (parent, senderid, serverkey) {
  17. var obj = {};
  18. obj.messageId = 0;
  19. obj.relays = {};
  20. obj.stats = {
  21. mode: "Real",
  22. sent: 0,
  23. sendError: 0,
  24. received: 0,
  25. receivedNoRoute: 0,
  26. receivedBadArgs: 0
  27. }
  28. const Sender = require('node-xcs').Sender;
  29. const Message = require('node-xcs').Message;
  30. const Notification = require('node-xcs').Notification;
  31. const xcs = new Sender(senderid, serverkey);
  32. var tokenToNodeMap = {} // Token --> { nid: nodeid, mid: meshid }
  33. // Setup logging
  34. if (parent.config.firebase && (parent.config.firebase.log === true)) {
  35. obj.logpath = parent.path.join(parent.datapath, 'firebase.txt');
  36. obj.log = function (msg) { try { parent.fs.appendFileSync(obj.logpath, new Date().toLocaleString() + ': ' + msg + '\r\n'); } catch (ex) { console.log('ERROR: Unable to write to firebase.txt.'); } }
  37. } else {
  38. obj.log = function () { }
  39. }
  40. // Messages received from client (excluding receipts)
  41. xcs.on('message', function (messageId, from, data, category) {
  42. const jsonData = JSON.stringify(data);
  43. obj.log('Firebase-Message: ' + jsonData);
  44. parent.debug('email', 'Firebase-Message: ' + jsonData);
  45. if (typeof data.r == 'string') {
  46. // Lookup push relay server
  47. parent.debug('email', 'Firebase-RelayRoute: ' + data.r);
  48. const wsrelay = obj.relays[data.r];
  49. if (wsrelay != null) {
  50. delete data.r;
  51. try { wsrelay.send(JSON.stringify({ from: from, data: data, category: category })); } catch (ex) { }
  52. }
  53. } else {
  54. // Lookup node information from the cache
  55. var ninfo = tokenToNodeMap[from];
  56. if (ninfo == null) { obj.stats.receivedNoRoute++; return; }
  57. if ((data != null) && (data.con != null) && (data.s != null)) { // Console command
  58. obj.stats.received++;
  59. parent.webserver.routeAgentCommand({ action: 'msg', type: 'console', value: data.con, sessionid: data.s }, ninfo.did, ninfo.nid, ninfo.mid);
  60. } else {
  61. obj.stats.receivedBadArgs++;
  62. }
  63. }
  64. });
  65. // Only fired for messages where options.delivery_receipt_requested = true
  66. /*
  67. xcs.on('receipt', function (messageId, from, data, category) { console.log('Firebase-Receipt', messageId, from, data, category); });
  68. xcs.on('connected', function () { console.log('Connected'); });
  69. xcs.on('disconnected', function () { console.log('disconnected'); });
  70. xcs.on('online', function () { console.log('online'); });
  71. xcs.on('error', function (e) { console.log('error', e); });
  72. xcs.on('message-error', function (e) { console.log('message-error', e); });
  73. */
  74. xcs.start();
  75. obj.log('CreateFirebase-Setup');
  76. parent.debug('email', 'CreateFirebase-Setup');
  77. // EXAMPLE
  78. //var payload = { notification: { title: command.title, body: command.msg }, data: { url: obj.msgurl } };
  79. //var options = { priority: 'High', timeToLive: 5 * 60 }; // TTL: 5 minutes, priority 'Normal' or 'High'
  80. obj.sendToDevice = function (node, payload, options, func) {
  81. if (typeof node == 'string') {
  82. parent.db.Get(node, function (err, docs) { if ((err == null) && (docs != null) && (docs.length == 1)) { obj.sendToDeviceEx(docs[0], payload, options, func); } else { func(0, 'error'); } })
  83. } else {
  84. obj.sendToDeviceEx(node, payload, options, func);
  85. }
  86. }
  87. // Send an outbound push notification
  88. obj.sendToDeviceEx = function (node, payload, options, func) {
  89. parent.debug('email', 'Firebase-sendToDevice');
  90. if ((node == null) || (typeof node.pmt != 'string')) return;
  91. obj.log('sendToDevice, node:' + node._id + ', payload: ' + JSON.stringify(payload) + ', options: ' + JSON.stringify(options));
  92. // Fill in our lookup table
  93. if (node._id != null) { tokenToNodeMap[node.pmt] = { nid: node._id, mid: node.meshid, did: node.domain } }
  94. // Built the on-screen notification
  95. var notification = null;
  96. if (payload.notification) {
  97. var notification = new Notification('ic_message')
  98. .title(payload.notification.title)
  99. .body(payload.notification.body)
  100. .build();
  101. }
  102. // Build the message
  103. var message = new Message('msg_' + (++obj.messageId));
  104. if (options.priority) { message.priority(options.priority); }
  105. if (payload.data) { for (var i in payload.data) { message.addData(i, payload.data[i]); } }
  106. if ((payload.data == null) || (payload.data.shash == null)) { message.addData('shash', parent.webserver.agentCertificateHashBase64); } // Add the server agent hash, new Android agents will reject notifications that don't have this.
  107. if (notification) { message.notification(notification) }
  108. message.build();
  109. // Send the message
  110. function callback(result) {
  111. if (result.getError() == null) { obj.stats.sent++; obj.log('Success'); } else { obj.stats.sendError++; obj.log('Fail'); }
  112. callback.func(result.getMessageId(), result.getError(), result.getErrorDescription())
  113. }
  114. callback.func = func;
  115. parent.debug('email', 'Firebase-sending');
  116. xcs.sendNoRetry(message, node.pmt, callback);
  117. }
  118. // Setup a two way relay
  119. obj.setupRelay = function (ws) {
  120. // Select and set a relay identifier
  121. ws.relayId = getRandomPassword();
  122. while (obj.relays[ws.relayId] != null) { ws.relayId = getRandomPassword(); }
  123. obj.relays[ws.relayId] = ws;
  124. // On message, parse it
  125. ws.on('message', function (msg) {
  126. parent.debug('email', 'FBWS-Data(' + this.relayId + '): ' + msg);
  127. if (typeof msg == 'string') {
  128. obj.log('Relay: ' + msg);
  129. // Parse the incoming push request
  130. var data = null;
  131. try { data = JSON.parse(msg) } catch (ex) { return; }
  132. if (typeof data != 'object') return;
  133. if (parent.common.validateObjectForMongo(data, 4096) == false) return; // Perform sanity checking on this object.
  134. if (typeof data.pmt != 'string') return;
  135. if (typeof data.payload != 'object') return;
  136. if (typeof data.payload.notification == 'object') {
  137. if (typeof data.payload.notification.title != 'string') return;
  138. if (typeof data.payload.notification.body != 'string') return;
  139. }
  140. if (typeof data.options != 'object') return;
  141. if ((data.options.priority != 'Normal') && (data.options.priority != 'High')) return;
  142. if ((typeof data.options.timeToLive != 'number') || (data.options.timeToLive < 1)) return;
  143. if (typeof data.payload.data != 'object') { data.payload.data = {}; }
  144. data.payload.data.r = ws.relayId; // Set the relay id.
  145. // Send the push notification
  146. obj.sendToDevice({ pmt: data.pmt }, data.payload, data.options, function (id, err, errdesc) {
  147. if (err == null) {
  148. try { wsrelay.send(JSON.stringify({ sent: true })); } catch (ex) { }
  149. } else {
  150. try { wsrelay.send(JSON.stringify({ sent: false })); } catch (ex) { }
  151. }
  152. });
  153. }
  154. });
  155. // If error, close the relay
  156. ws.on('error', function (err) {
  157. parent.debug('email', 'FBWS-Error(' + this.relayId + '): ' + err);
  158. delete obj.relays[this.relayId];
  159. });
  160. // Close the relay
  161. ws.on('close', function () {
  162. parent.debug('email', 'FBWS-Close(' + this.relayId + ')');
  163. delete obj.relays[this.relayId];
  164. });
  165. }
  166. function getRandomPassword() { return Buffer.from(parent.crypto.randomBytes(9), 'binary').toString('base64').split('/').join('@'); }
  167. return obj;
  168. };
  169. // Construct the Firebase object
  170. module.exports.CreateFirebaseRelay = function (parent, url, key) {
  171. var obj = {};
  172. obj.messageId = 0;
  173. obj.stats = {
  174. mode: "Relay",
  175. sent: 0,
  176. sendError: 0,
  177. received: 0,
  178. receivedNoRoute: 0,
  179. receivedBadArgs: 0
  180. }
  181. const WebSocket = require('ws');
  182. const https = require('https');
  183. const querystring = require('querystring');
  184. const relayUrl = require('url').parse(url);
  185. parent.debug('email', 'CreateFirebaseRelay-Setup');
  186. // Setup logging
  187. if (parent.config.firebaserelay && (parent.config.firebaserelay.log === true)) {
  188. obj.logpath = parent.path.join(parent.datapath, 'firebaserelay.txt');
  189. obj.log = function (msg) { try { parent.fs.appendFileSync(obj.logpath, new Date().toLocaleString() + ': ' + msg + '\r\n'); } catch (ex) { console.log('ERROR: Unable to write to firebaserelay.txt.'); } }
  190. } else {
  191. obj.log = function () { }
  192. }
  193. obj.log('Starting relay to: ' + relayUrl.href);
  194. if (relayUrl.protocol == 'wss:') {
  195. // Setup two-way push notification channel
  196. obj.wsopen = false;
  197. obj.tokenToNodeMap = {}; // Token --> { nid: nodeid, mid: meshid }
  198. obj.backoffTimer = 0;
  199. obj.connectWebSocket = function () {
  200. if (obj.reconnectTimer != null) { try { clearTimeout(obj.reconnectTimer); } catch (ex) { } delete obj.reconnectTimer; }
  201. if (obj.wsclient != null) return;
  202. obj.wsclient = new WebSocket(relayUrl.href + (key ? ('?key=' + key) : ''), { rejectUnauthorized: false })
  203. obj.wsclient.on('open', function () {
  204. obj.lastConnect = Date.now();
  205. parent.debug('email', 'FBWS-Connected');
  206. obj.wsopen = true;
  207. });
  208. obj.wsclient.on('message', function (msg) {
  209. parent.debug('email', 'FBWS-Data(' + msg.length + '): ' + msg);
  210. obj.log('Received(' + msg.length + '): ' + msg);
  211. var data = null;
  212. try { data = JSON.parse(msg) } catch (ex) { }
  213. if (typeof data != 'object') return;
  214. if (typeof data.from != 'string') return;
  215. if (typeof data.data != 'object') return;
  216. if (typeof data.category != 'string') return;
  217. processMessage(data.messageId, data.from, data.data, data.category);
  218. });
  219. obj.wsclient.on('error', function (err) { obj.log('Error: ' + err); });
  220. obj.wsclient.on('close', function (a, b, c) {
  221. parent.debug('email', 'FBWS-Disconnected');
  222. obj.wsclient = null;
  223. obj.wsopen = false;
  224. // Compute the backoff timer
  225. if (obj.reconnectTimer == null) {
  226. if ((obj.lastConnect != null) && ((Date.now() - obj.lastConnect) > 10000)) { obj.backoffTimer = 0; }
  227. obj.backoffTimer += 1000;
  228. obj.backoffTimer = obj.backoffTimer * 2;
  229. if (obj.backoffTimer > 1200000) { obj.backoffTimer = 600000; } // Maximum 10 minutes backoff.
  230. obj.reconnectTimer = setTimeout(obj.connectWebSocket, obj.backoffTimer);
  231. }
  232. });
  233. }
  234. function processMessage(messageId, from, data, category) {
  235. // Lookup node information from the cache
  236. var ninfo = obj.tokenToNodeMap[from];
  237. if (ninfo == null) { obj.stats.receivedNoRoute++; return; }
  238. if ((data != null) && (data.con != null) && (data.s != null)) { // Console command
  239. obj.stats.received++;
  240. parent.webserver.routeAgentCommand({ action: 'msg', type: 'console', value: data.con, sessionid: data.s }, ninfo.did, ninfo.nid, ninfo.mid);
  241. } else {
  242. obj.stats.receivedBadArgs++;
  243. }
  244. }
  245. obj.sendToDevice = function (node, payload, options, func) {
  246. if (typeof node == 'string') {
  247. parent.db.Get(node, function (err, docs) { if ((err == null) && (docs != null) && (docs.length == 1)) { obj.sendToDeviceEx(docs[0], payload, options, func); } else { func(0, 'error'); } })
  248. } else {
  249. obj.sendToDeviceEx(node, payload, options, func);
  250. }
  251. }
  252. obj.sendToDeviceEx = function (node, payload, options, func) {
  253. parent.debug('email', 'Firebase-sendToDevice-webSocket');
  254. if ((node == null) || (typeof node.pmt != 'string')) { func(0, 'error'); return; }
  255. obj.log('sendToDevice, node:' + node._id + ', payload: ' + JSON.stringify(payload) + ', options: ' + JSON.stringify(options));
  256. // Fill in our lookup table
  257. if (node._id != null) { obj.tokenToNodeMap[node.pmt] = { nid: node._id, mid: node.meshid, did: node.domain } }
  258. // Fill in the server agent cert hash
  259. if (payload.data == null) { payload.data = {}; }
  260. if (payload.data.shash == null) { payload.data.shash = parent.webserver.agentCertificateHashBase64; } // Add the server agent hash, new Android agents will reject notifications that don't have this.
  261. // If the web socket is open, send now
  262. if (obj.wsopen == true) {
  263. try { obj.wsclient.send(JSON.stringify({ pmt: node.pmt, payload: payload, options: options })); } catch (ex) { func(0, 'error'); obj.stats.sendError++; return; }
  264. obj.stats.sent++;
  265. obj.log('Sent');
  266. func(1);
  267. } else {
  268. // TODO: Buffer the push messages until TTL.
  269. obj.stats.sendError++;
  270. obj.log('Error');
  271. func(0, 'error');
  272. }
  273. }
  274. obj.connectWebSocket();
  275. } else if (relayUrl.protocol == 'https:') {
  276. // Send an outbound push notification using an HTTPS POST
  277. obj.pushOnly = true;
  278. obj.sendToDevice = function (node, payload, options, func) {
  279. if (typeof node == 'string') {
  280. parent.db.Get(node, function (err, docs) { if ((err == null) && (docs != null) && (docs.length == 1)) { obj.sendToDeviceEx(docs[0], payload, options, func); } else { func(0, 'error'); } })
  281. } else {
  282. obj.sendToDeviceEx(node, payload, options, func);
  283. }
  284. }
  285. obj.sendToDeviceEx = function (node, payload, options, func) {
  286. parent.debug('email', 'Firebase-sendToDevice-httpPost');
  287. if ((node == null) || (typeof node.pmt != 'string')) return;
  288. // Fill in the server agent cert hash
  289. if (payload.data == null) { payload.data = {}; }
  290. if (payload.data.shash == null) { payload.data.shash = parent.webserver.agentCertificateHashBase64; } // Add the server agent hash, new Android agents will reject notifications that don't have this.
  291. obj.log('sendToDevice, node:' + node._id + ', payload: ' + JSON.stringify(payload) + ', options: ' + JSON.stringify(options));
  292. const querydata = querystring.stringify({ 'msg': JSON.stringify({ pmt: node.pmt, payload: payload, options: options }) });
  293. // Send the message to the relay
  294. const httpOptions = {
  295. hostname: relayUrl.hostname,
  296. port: relayUrl.port ? relayUrl.port : 443,
  297. path: relayUrl.path + (key ? ('?key=' + key) : ''),
  298. method: 'POST',
  299. //rejectUnauthorized: false, // DEBUG
  300. headers: {
  301. 'Content-Type': 'application/x-www-form-urlencoded',
  302. 'Content-Length': querydata.length
  303. }
  304. }
  305. const req = https.request(httpOptions, function (res) {
  306. obj.log('Response: ' + res.statusCode);
  307. if (res.statusCode == 200) { obj.stats.sent++; } else { obj.stats.sendError++; }
  308. if (func != null) { func(++obj.messageId, (res.statusCode == 200) ? null : 'error'); }
  309. });
  310. parent.debug('email', 'Firebase-sending');
  311. req.on('error', function (error) { obj.stats.sent++; func(++obj.messageId, 'error'); });
  312. req.write(querydata);
  313. req.end();
  314. }
  315. }
  316. return obj;
  317. };