firebase.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /**
  2. * @description MeshCentral Firebase communication module
  3. * @author Ylian Saint-Hilaire
  4. * @license Apache-2.0
  5. * @version v0.0.1
  6. */
  7. /*xjslint node: true */
  8. /*xjslint plusplus: true */
  9. /*xjslint maxlen: 256 */
  10. /*jshint node: true */
  11. /*jshint strict: false */
  12. /*jshint esversion: 6 */
  13. "use strict";
  14. // Initialize the Firebase Admin SDK
  15. module.exports.CreateFirebase = function (parent, serviceAccount) {
  16. // Import the Firebase Admin SDK
  17. const admin = require('firebase-admin');
  18. const obj = {};
  19. obj.messageId = 0;
  20. obj.relays = {};
  21. obj.stats = {
  22. mode: 'Real',
  23. sent: 0,
  24. sendError: 0,
  25. received: 0,
  26. receivedNoRoute: 0,
  27. receivedBadArgs: 0
  28. };
  29. const tokenToNodeMap = {}; // Token --> { nid: nodeid, mid: meshid }
  30. // Initialize Firebase Admin with server key and project ID
  31. if (!admin.apps.length) {
  32. admin.initializeApp({ credential: admin.credential.cert(serviceAccount) });
  33. }
  34. // Setup logging
  35. if (parent.config.firebase && (parent.config.firebase.log === true)) {
  36. obj.logpath = parent.path.join(parent.datapath, 'firebase.txt');
  37. 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.'); } }
  38. } else {
  39. obj.log = function () { }
  40. }
  41. // Function to send notifications
  42. obj.sendToDevice = function (node, payload, options, func) {
  43. if (typeof node === 'string') {
  44. parent.db.Get(node, function (err, docs) {
  45. if (!err && docs && docs.length === 1) {
  46. obj.sendToDeviceEx(docs[0], payload, options, func);
  47. } else {
  48. func(0, 'error');
  49. }
  50. });
  51. } else {
  52. obj.sendToDeviceEx(node, payload, options, func);
  53. }
  54. };
  55. // Send an outbound push notification
  56. obj.sendToDeviceEx = function (node, payload, options, func) {
  57. if (!node || typeof node.pmt !== 'string') {
  58. func(0, 'error');
  59. return;
  60. }
  61. obj.log('sendToDevice, node:' + node._id + ', payload: ' + JSON.stringify(payload) + ', options: ' + JSON.stringify(options));
  62. // Fill in our lookup table
  63. if (node._id) {
  64. tokenToNodeMap[node.pmt] = {
  65. nid: node._id,
  66. mid: node.meshid,
  67. did: node.domain
  68. };
  69. }
  70. const message = {
  71. token: node.pmt,
  72. notification: payload.notification,
  73. data: payload.data,
  74. android: {
  75. priority: options.priority || 'high',
  76. ttl: options.timeToLive ? options.timeToLive * 1000 : undefined
  77. }
  78. };
  79. admin.messaging().send(message).then(function (response) {
  80. obj.stats.sent++;
  81. obj.log('Success');
  82. func(response);
  83. }).catch(function (error) {
  84. obj.stats.sendError++;
  85. obj.log('Fail: ' + error);
  86. func(0, error);
  87. });
  88. };
  89. // Setup a two way relay
  90. obj.setupRelay = function (ws) {
  91. ws.relayId = getRandomPassword();
  92. while (obj.relays[ws.relayId]) { ws.relayId = getRandomPassword(); }
  93. obj.relays[ws.relayId] = ws;
  94. ws.on('message', function (msg) {
  95. parent.debug('email', 'FBWS-Data(' + this.relayId + '): ' + msg);
  96. if (typeof msg === 'string') {
  97. obj.log('Relay: ' + msg);
  98. let data;
  99. try { data = JSON.parse(msg); } catch (ex) { return; }
  100. if (typeof data !== 'object') return;
  101. if (!parent.common.validateObjectForMongo(data, 4096)) return;
  102. if (typeof data.pmt !== 'string' || typeof data.payload !== 'object') return;
  103. data.payload.data = data.payload.data || {};
  104. data.payload.data.r = ws.relayId;
  105. obj.sendToDevice({ pmt: data.pmt }, data.payload, data.options, function (id, err) {
  106. if (!err) {
  107. try { ws.send(JSON.stringify({ sent: true })); } catch (ex) { }
  108. } else {
  109. try { ws.send(JSON.stringify({ sent: false })); } catch (ex) { }
  110. }
  111. });
  112. }
  113. });
  114. // If error, close the relay
  115. ws.on('error', function (err) {
  116. parent.debug('email', 'FBWS-Error(' + this.relayId + '): ' + err);
  117. delete obj.relays[this.relayId];
  118. });
  119. // Close the relay
  120. ws.on('close', function () {
  121. parent.debug('email', 'FBWS-Close(' + this.relayId + ')');
  122. delete obj.relays[this.relayId];
  123. });
  124. };
  125. function getRandomPassword() {
  126. return Buffer.from(parent.crypto.randomBytes(9), 'binary').toString('base64').replace(/\//g, '@');
  127. }
  128. return obj;
  129. };
  130. // Construct the Firebase object
  131. module.exports.CreateFirebaseRelay = function (parent, url, key) {
  132. var obj = {};
  133. obj.messageId = 0;
  134. obj.stats = {
  135. mode: "Relay",
  136. sent: 0,
  137. sendError: 0,
  138. received: 0,
  139. receivedNoRoute: 0,
  140. receivedBadArgs: 0
  141. }
  142. const WebSocket = require('ws');
  143. const https = require('https');
  144. const querystring = require('querystring');
  145. const relayUrl = require('url').parse(url);
  146. parent.debug('email', 'CreateFirebaseRelay-Setup');
  147. // Setup logging
  148. if (parent.config.firebaserelay && (parent.config.firebaserelay.log === true)) {
  149. obj.logpath = parent.path.join(parent.datapath, 'firebaserelay.txt');
  150. 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.'); } }
  151. } else {
  152. obj.log = function () { }
  153. }
  154. obj.log('Starting relay to: ' + relayUrl.href);
  155. if (relayUrl.protocol == 'wss:') {
  156. // Setup two-way push notification channel
  157. obj.wsopen = false;
  158. obj.tokenToNodeMap = {}; // Token --> { nid: nodeid, mid: meshid }
  159. obj.backoffTimer = 0;
  160. obj.connectWebSocket = function () {
  161. if (obj.reconnectTimer != null) { try { clearTimeout(obj.reconnectTimer); } catch (ex) { } delete obj.reconnectTimer; }
  162. if (obj.wsclient != null) return;
  163. obj.wsclient = new WebSocket(relayUrl.href + (key ? ('?key=' + key) : ''), { rejectUnauthorized: false })
  164. obj.wsclient.on('open', function () {
  165. obj.lastConnect = Date.now();
  166. parent.debug('email', 'FBWS-Connected');
  167. obj.wsopen = true;
  168. });
  169. obj.wsclient.on('message', function (msg) {
  170. parent.debug('email', 'FBWS-Data(' + msg.length + '): ' + msg);
  171. obj.log('Received(' + msg.length + '): ' + msg);
  172. var data = null;
  173. try { data = JSON.parse(msg) } catch (ex) { }
  174. if (typeof data != 'object') return;
  175. if (typeof data.from != 'string') return;
  176. if (typeof data.data != 'object') return;
  177. if (typeof data.category != 'string') return;
  178. processMessage(data.messageId, data.from, data.data, data.category);
  179. });
  180. obj.wsclient.on('error', function (err) { obj.log('Error: ' + err); });
  181. obj.wsclient.on('close', function (a, b, c) {
  182. parent.debug('email', 'FBWS-Disconnected');
  183. obj.wsclient = null;
  184. obj.wsopen = false;
  185. // Compute the backoff timer
  186. if (obj.reconnectTimer == null) {
  187. if ((obj.lastConnect != null) && ((Date.now() - obj.lastConnect) > 10000)) { obj.backoffTimer = 0; }
  188. obj.backoffTimer += 1000;
  189. obj.backoffTimer = obj.backoffTimer * 2;
  190. if (obj.backoffTimer > 1200000) { obj.backoffTimer = 600000; } // Maximum 10 minutes backoff.
  191. obj.reconnectTimer = setTimeout(obj.connectWebSocket, obj.backoffTimer);
  192. }
  193. });
  194. }
  195. function processMessage(messageId, from, data, category) {
  196. // Lookup node information from the cache
  197. var ninfo = obj.tokenToNodeMap[from];
  198. if (ninfo == null) { obj.stats.receivedNoRoute++; return; }
  199. if ((data != null) && (data.con != null) && (data.s != null)) { // Console command
  200. obj.stats.received++;
  201. parent.webserver.routeAgentCommand({ action: 'msg', type: 'console', value: data.con, sessionid: data.s }, ninfo.did, ninfo.nid, ninfo.mid);
  202. } else {
  203. obj.stats.receivedBadArgs++;
  204. }
  205. }
  206. obj.sendToDevice = function (node, payload, options, func) {
  207. if (typeof node == 'string') {
  208. 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'); } })
  209. } else {
  210. obj.sendToDeviceEx(node, payload, options, func);
  211. }
  212. }
  213. obj.sendToDeviceEx = function (node, payload, options, func) {
  214. parent.debug('email', 'Firebase-sendToDevice-webSocket');
  215. if ((node == null) || (typeof node.pmt != 'string')) { func(0, 'error'); return; }
  216. obj.log('sendToDevice, node:' + node._id + ', payload: ' + JSON.stringify(payload) + ', options: ' + JSON.stringify(options));
  217. // Fill in our lookup table
  218. if (node._id != null) { obj.tokenToNodeMap[node.pmt] = { nid: node._id, mid: node.meshid, did: node.domain } }
  219. // Fill in the server agent cert hash
  220. if (payload.data == null) { payload.data = {}; }
  221. 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.
  222. // If the web socket is open, send now
  223. if (obj.wsopen == true) {
  224. try { obj.wsclient.send(JSON.stringify({ pmt: node.pmt, payload: payload, options: options })); } catch (ex) { func(0, 'error'); obj.stats.sendError++; return; }
  225. obj.stats.sent++;
  226. obj.log('Sent');
  227. func(1);
  228. } else {
  229. // TODO: Buffer the push messages until TTL.
  230. obj.stats.sendError++;
  231. obj.log('Error');
  232. func(0, 'error');
  233. }
  234. }
  235. obj.connectWebSocket();
  236. } else if (relayUrl.protocol == 'https:') {
  237. // Send an outbound push notification using an HTTPS POST
  238. obj.pushOnly = true;
  239. obj.sendToDevice = function (node, payload, options, func) {
  240. if (typeof node == 'string') {
  241. 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'); } })
  242. } else {
  243. obj.sendToDeviceEx(node, payload, options, func);
  244. }
  245. }
  246. obj.sendToDeviceEx = function (node, payload, options, func) {
  247. parent.debug('email', 'Firebase-sendToDevice-httpPost');
  248. if ((node == null) || (typeof node.pmt != 'string')) return;
  249. // Fill in the server agent cert hash
  250. if (payload.data == null) { payload.data = {}; }
  251. 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.
  252. obj.log('sendToDevice, node:' + node._id + ', payload: ' + JSON.stringify(payload) + ', options: ' + JSON.stringify(options));
  253. const querydata = querystring.stringify({ 'msg': JSON.stringify({ pmt: node.pmt, payload: payload, options: options }) });
  254. // Send the message to the relay
  255. const httpOptions = {
  256. hostname: relayUrl.hostname,
  257. port: relayUrl.port ? relayUrl.port : 443,
  258. path: relayUrl.path + (key ? ('?key=' + key) : ''),
  259. method: 'POST',
  260. //rejectUnauthorized: false, // DEBUG
  261. headers: {
  262. 'Content-Type': 'application/x-www-form-urlencoded',
  263. 'Content-Length': querydata.length
  264. }
  265. }
  266. const req = https.request(httpOptions, function (res) {
  267. obj.log('Response: ' + res.statusCode);
  268. if (res.statusCode == 200) { obj.stats.sent++; } else { obj.stats.sendError++; }
  269. if (func != null) { func(++obj.messageId, (res.statusCode == 200) ? null : 'error'); }
  270. });
  271. parent.debug('email', 'Firebase-sending');
  272. req.on('error', function (error) { obj.stats.sent++; func(++obj.messageId, 'error'); });
  273. req.write(querydata);
  274. req.end();
  275. }
  276. }
  277. return obj;
  278. };