mirror of
				https://github.com/Ylianst/MeshCentral.git
				synced 2025-10-29 23:35:02 -04:00 
			
		
		
		
	Updated to MeshCentral Firebase support, updated to using firebase-admin module.
This commit is contained in:
		
							parent
							
								
									832d11739b
								
							
						
					
					
						commit
						c2eb1f2516
					
				
							
								
								
									
										192
									
								
								firebase.js
									
									
									
									
									
								
							
							
						
						
									
										192
									
								
								firebase.js
									
									
									
									
									
								
							| @ -1,7 +1,6 @@ | |||||||
| /** | /** | ||||||
| * @description MeshCentral Firebase communication module | * @description MeshCentral Firebase communication module | ||||||
| * @author Ylian Saint-Hilaire | * @author Ylian Saint-Hilaire | ||||||
| * @copyright Intel Corporation 2018-2022 |  | ||||||
| * @license Apache-2.0 | * @license Apache-2.0 | ||||||
| * @version v0.0.1 | * @version v0.0.1 | ||||||
| */ | */ | ||||||
| @ -14,31 +13,31 @@ | |||||||
| /*jshint esversion: 6 */ | /*jshint esversion: 6 */ | ||||||
| "use strict"; | "use strict"; | ||||||
| 
 | 
 | ||||||
| // Construct the Firebase object
 | // Initialize the Firebase Admin SDK
 | ||||||
| module.exports.CreateFirebase = function (parent, senderid, serverkey) { | module.exports.CreateFirebase = function (parent, serviceAccount) { | ||||||
|     var obj = {}; |      | ||||||
|  |     // Import the Firebase Admin SDK
 | ||||||
|  |     const admin = require('firebase-admin'); | ||||||
|  |      | ||||||
|  |     const obj = {}; | ||||||
|     obj.messageId = 0; |     obj.messageId = 0; | ||||||
|     obj.relays = {}; |     obj.relays = {}; | ||||||
|     obj.stats = { |     obj.stats = { | ||||||
|         mode: "Real", |         mode: 'Real', | ||||||
|         sent: 0, |         sent: 0, | ||||||
|         sendError: 0, |         sendError: 0, | ||||||
|         received: 0, |         received: 0, | ||||||
|         receivedNoRoute: 0, |         receivedNoRoute: 0, | ||||||
|         receivedBadArgs: 0 |         receivedBadArgs: 0 | ||||||
|  |     }; | ||||||
|  |      | ||||||
|  |     const tokenToNodeMap = {}; // Token --> { nid: nodeid, mid: meshid }
 | ||||||
|  |      | ||||||
|  |     // Initialize Firebase Admin with server key and project ID
 | ||||||
|  |     if (!admin.apps.length) { | ||||||
|  |         admin.initializeApp({ credential: admin.credential.cert(serviceAccount) }); | ||||||
|     } |     } | ||||||
|      |      | ||||||
|     // In NodeJS v23, add util.isNullOrUndefined() to make node-xcs work correctly.
 |  | ||||||
|     // Remove this when node-xcs moves to support NodeJS v23
 |  | ||||||
|     if (require('util').isNullOrUndefined == null) { require('util').isNullOrUndefined = function (v) { return v == null; } } |  | ||||||
|      |  | ||||||
|     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 }
 |  | ||||||
| 
 |  | ||||||
|     // Setup logging
 |     // Setup logging
 | ||||||
|     if (parent.config.firebase && (parent.config.firebase.log === true)) { |     if (parent.config.firebase && (parent.config.firebase.log === true)) { | ||||||
|         obj.logpath = parent.path.join(parent.datapath, 'firebase.txt'); |         obj.logpath = parent.path.join(parent.datapath, 'firebase.txt'); | ||||||
| @ -47,133 +46,85 @@ module.exports.CreateFirebase = function (parent, senderid, serverkey) { | |||||||
|         obj.log = function () { } |         obj.log = function () { } | ||||||
|     } |     } | ||||||
|      |      | ||||||
|     // Messages received from client (excluding receipts)
 |     // Function to send notifications
 | ||||||
|     xcs.on('message', function (messageId, from, data, category) { |  | ||||||
|         const jsonData = JSON.stringify(data); |  | ||||||
|         obj.log('Firebase-Message: ' + jsonData); |  | ||||||
|         parent.debug('email', 'Firebase-Message: ' + jsonData); |  | ||||||
| 
 |  | ||||||
|         if (typeof data.r == 'string') { |  | ||||||
|             // Lookup push relay server
 |  | ||||||
|             parent.debug('email', 'Firebase-RelayRoute: ' + data.r); |  | ||||||
|             const wsrelay = obj.relays[data.r]; |  | ||||||
|             if (wsrelay != null) { |  | ||||||
|                 delete data.r; |  | ||||||
|                 try { wsrelay.send(JSON.stringify({ from: from, data: data, category: category })); } catch (ex) { } |  | ||||||
|             } |  | ||||||
|         } else { |  | ||||||
|             // Lookup node information from the cache
 |  | ||||||
|             var ninfo = tokenToNodeMap[from]; |  | ||||||
|             if (ninfo == null) { obj.stats.receivedNoRoute++; return; } |  | ||||||
| 
 |  | ||||||
|             if ((data != null) && (data.con != null) && (data.s != null)) { // Console command
 |  | ||||||
|                 obj.stats.received++; |  | ||||||
|                 parent.webserver.routeAgentCommand({ action: 'msg', type: 'console', value: data.con, sessionid: data.s }, ninfo.did, ninfo.nid, ninfo.mid); |  | ||||||
|             } else { |  | ||||||
|                 obj.stats.receivedBadArgs++; |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|     }); |  | ||||||
| 
 |  | ||||||
|     // Only fired for messages where options.delivery_receipt_requested = true
 |  | ||||||
|     /* |  | ||||||
|     xcs.on('receipt', function (messageId, from, data, category) { console.log('Firebase-Receipt', messageId, from, data, category); }); |  | ||||||
|     xcs.on('connected', function () { console.log('Connected'); }); |  | ||||||
|     xcs.on('disconnected', function () { console.log('disconnected'); }); |  | ||||||
|     xcs.on('online', function () { console.log('online'); }); |  | ||||||
|     xcs.on('error', function (e) { console.log('error', e); }); |  | ||||||
|     xcs.on('message-error', function (e) { console.log('message-error', e); }); |  | ||||||
|     */ |  | ||||||
| 
 |  | ||||||
|     xcs.start(); |  | ||||||
| 
 |  | ||||||
|     obj.log('CreateFirebase-Setup'); |  | ||||||
|     parent.debug('email', 'CreateFirebase-Setup'); |  | ||||||
| 
 |  | ||||||
|     // 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'
 |  | ||||||
| 
 |  | ||||||
|     obj.sendToDevice = function (node, payload, options, func) { |     obj.sendToDevice = function (node, payload, options, func) { | ||||||
|         if (typeof node == 'string') { |         if (typeof node === 'string') { | ||||||
|             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'); } }) |             parent.db.Get(node, function (err, docs) { | ||||||
|  |                 if (!err && docs && docs.length === 1) { | ||||||
|  |                     obj.sendToDeviceEx(docs[0], payload, options, func); | ||||||
|  |                 } else { | ||||||
|  |                     func(0, 'error'); | ||||||
|  |                 } | ||||||
|  |             }); | ||||||
|         } else { |         } else { | ||||||
|             obj.sendToDeviceEx(node, payload, options, func); |             obj.sendToDeviceEx(node, payload, options, func); | ||||||
|         } |         } | ||||||
|     } |     }; | ||||||
|      |      | ||||||
|     // Send an outbound push notification
 |     // Send an outbound push notification
 | ||||||
|     obj.sendToDeviceEx = function (node, payload, options, func) { |     obj.sendToDeviceEx = function (node, payload, options, func) { | ||||||
|         parent.debug('email', 'Firebase-sendToDevice'); |         if (!node || typeof node.pmt !== 'string') { | ||||||
|         if ((node == null) || (typeof node.pmt != 'string')) return; |             func(0, 'error'); | ||||||
|  |             return; | ||||||
|  |         } | ||||||
|  |          | ||||||
|         obj.log('sendToDevice, node:' + node._id + ', payload: ' + JSON.stringify(payload) + ', options: ' + JSON.stringify(options)); |         obj.log('sendToDevice, node:' + node._id + ', payload: ' + JSON.stringify(payload) + ', options: ' + JSON.stringify(options)); | ||||||
|          |          | ||||||
|         // Fill in our lookup table
 |         // Fill in our lookup table
 | ||||||
|         if (node._id != null) { tokenToNodeMap[node.pmt] = { nid: node._id, mid: node.meshid, did: node.domain } } |         if (node._id) { | ||||||
| 
 |             tokenToNodeMap[node.pmt] = { | ||||||
|         // Built the on-screen notification
 |                 nid: node._id, | ||||||
|         var notification = null; |                 mid: node.meshid, | ||||||
|         if (payload.notification) { |                 did: node.domain | ||||||
|             var notification = new Notification('ic_message') |             }; | ||||||
|                 .title(payload.notification.title) |  | ||||||
|                 .body(payload.notification.body) |  | ||||||
|                 .build(); |  | ||||||
|         } |         } | ||||||
|          |          | ||||||
|         // Build the message
 |         const message = { | ||||||
|         var message = new Message('msg_' + (++obj.messageId)); |             token: node.pmt, | ||||||
|         if (options.priority) { message.priority(options.priority); } |             notification: payload.notification, | ||||||
|         if (payload.data) { for (var i in payload.data) { message.addData(i, payload.data[i]); } } |             data: payload.data, | ||||||
|         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.
 |             android: { | ||||||
|         if (notification) { message.notification(notification) } |                 priority: options.priority || 'high', | ||||||
|         message.build(); |                 ttl: options.timeToLive ? options.timeToLive * 1000 : undefined | ||||||
|  |             } | ||||||
|  |         }; | ||||||
|          |          | ||||||
|         // Send the message
 |         admin.messaging().send(message).then(function (response) { | ||||||
|         function callback(result) { |             obj.stats.sent++; | ||||||
|             if (result.getError() == null) { obj.stats.sent++; obj.log('Success'); } else { obj.stats.sendError++; obj.log('Fail'); } |             obj.log('Success'); | ||||||
|             callback.func(result.getMessageId(), result.getError(), result.getErrorDescription()) |             func(response); | ||||||
|         } |         }).catch(function (error) { | ||||||
|         callback.func = func; |             obj.stats.sendError++; | ||||||
|         parent.debug('email', 'Firebase-sending'); |             obj.log('Fail: ' + error); | ||||||
|         xcs.sendNoRetry(message, node.pmt, callback); |             func(0, error); | ||||||
|     } |         }); | ||||||
|  |     }; | ||||||
|      |      | ||||||
|     // Setup a two way relay
 |     // Setup a two way relay
 | ||||||
|     obj.setupRelay = function (ws) { |     obj.setupRelay = function (ws) { | ||||||
|         // Select and set a relay identifier
 |  | ||||||
|         ws.relayId = getRandomPassword(); |         ws.relayId = getRandomPassword(); | ||||||
|         while (obj.relays[ws.relayId] != null) { ws.relayId = getRandomPassword(); } |         while (obj.relays[ws.relayId]) { ws.relayId = getRandomPassword(); } | ||||||
|         obj.relays[ws.relayId] = ws; |         obj.relays[ws.relayId] = ws; | ||||||
|          |          | ||||||
|         // On message, parse it
 |  | ||||||
|         ws.on('message', function (msg) { |         ws.on('message', function (msg) { | ||||||
|             parent.debug('email', 'FBWS-Data(' + this.relayId + '): ' + msg); |             parent.debug('email', 'FBWS-Data(' + this.relayId + '): ' + msg); | ||||||
|             if (typeof msg == 'string') { |             if (typeof msg === 'string') { | ||||||
|                 obj.log('Relay: ' + msg); |                 obj.log('Relay: ' + msg); | ||||||
|                  |                  | ||||||
|                 // Parse the incoming push request
 |                 let data; | ||||||
|                 var data = null; |                 try { data = JSON.parse(msg); } catch (ex) { return; } | ||||||
|                 try { data = JSON.parse(msg) } catch (ex) { return; } |                 if (typeof data !== 'object') return; | ||||||
|                 if (typeof data != 'object') return; |                 if (!parent.common.validateObjectForMongo(data, 4096)) return; | ||||||
|                 if (parent.common.validateObjectForMongo(data, 4096) == false) return; // Perform sanity checking on this object.
 |                 if (typeof data.pmt !== 'string' || typeof data.payload !== 'object') return; | ||||||
|                 if (typeof data.pmt != 'string') return; |  | ||||||
|                 if (typeof data.payload != 'object') return; |  | ||||||
|                 if (typeof data.payload.notification == 'object') { |  | ||||||
|                     if (typeof data.payload.notification.title != 'string') return; |  | ||||||
|                     if (typeof data.payload.notification.body != 'string') return; |  | ||||||
|                 } |  | ||||||
|                 if (typeof data.options != 'object') return; |  | ||||||
|                 if ((data.options.priority != 'Normal') && (data.options.priority != 'High')) return; |  | ||||||
|                 if ((typeof data.options.timeToLive != 'number') || (data.options.timeToLive < 1)) return; |  | ||||||
|                 if (typeof data.payload.data != 'object') { data.payload.data = {}; } |  | ||||||
|                 data.payload.data.r = ws.relayId; // Set the relay id.
 |  | ||||||
|                  |                  | ||||||
|                 // Send the push notification
 |                 data.payload.data = data.payload.data || {}; | ||||||
|                 obj.sendToDevice({ pmt: data.pmt }, data.payload, data.options, function (id, err, errdesc) { |                 data.payload.data.r = ws.relayId; | ||||||
|                     if (err == null) { |                  | ||||||
|                         try { wsrelay.send(JSON.stringify({ sent: true })); } catch (ex) { } |                 obj.sendToDevice({ pmt: data.pmt }, data.payload, data.options, function (id, err) { | ||||||
|  |                     if (!err) { | ||||||
|  |                         try { ws.send(JSON.stringify({ sent: true })); } catch (ex) { } | ||||||
|                     } else { |                     } else { | ||||||
|                         try { wsrelay.send(JSON.stringify({ sent: false })); } catch (ex) { } |                         try { ws.send(JSON.stringify({ sent: false })); } catch (ex) { } | ||||||
|                     } |                     } | ||||||
|                 }); |                 }); | ||||||
|             } |             } | ||||||
| @ -190,11 +141,12 @@ module.exports.CreateFirebase = function (parent, senderid, serverkey) { | |||||||
|             parent.debug('email', 'FBWS-Close(' + this.relayId + ')'); |             parent.debug('email', 'FBWS-Close(' + this.relayId + ')'); | ||||||
|             delete obj.relays[this.relayId]; |             delete obj.relays[this.relayId]; | ||||||
|         }); |         }); | ||||||
|  |     }; | ||||||
|      |      | ||||||
|  |     function getRandomPassword() { | ||||||
|  |         return Buffer.from(parent.crypto.randomBytes(9), 'binary').toString('base64').replace(/\//g, '@'); | ||||||
|     } |     } | ||||||
|      |      | ||||||
|     function getRandomPassword() { return Buffer.from(parent.crypto.randomBytes(9), 'binary').toString('base64').split('/').join('@'); } |  | ||||||
| 
 |  | ||||||
|     return obj; |     return obj; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -1998,25 +1998,17 @@ function CreateMeshCentralServer(config, args) { | |||||||
| 
 | 
 | ||||||
|                     // Setup Firebase
 |                     // Setup Firebase
 | ||||||
|                     if ((config.firebase != null) && (typeof config.firebase.senderid == 'string') && (typeof config.firebase.serverkey == 'string')) { |                     if ((config.firebase != null) && (typeof config.firebase.senderid == 'string') && (typeof config.firebase.serverkey == 'string')) { | ||||||
|                         if (nodeVersion >= 23) { |                         addServerWarning('Firebase now requires a service account JSON file, Firebase disabled.', 27); | ||||||
|                             addServerWarning('Firebase is not supported on this version of NodeJS.', 27); |                     } else if ((config.firebase != null) && (typeof config.firebase.serviceaccountfile == 'string')) { | ||||||
|                         } else { |                         var serviceAccount; | ||||||
|                             obj.firebase = require('./firebase').CreateFirebase(obj, config.firebase.senderid, config.firebase.serverkey); |                         try { serviceAccount = JSON.parse(obj.fs.readFileSync(obj.path.join(obj.datapath, config.firebase.serviceaccountfile)).toString()); } catch (ex) { console.log(ex); } | ||||||
|                         } |                         if (serviceAccount != null) { obj.firebase = require('./firebase').CreateFirebase(obj, serviceAccount); } | ||||||
|                     } else if ((typeof config.firebaserelay == 'object') && (typeof config.firebaserelay.url == 'string')) { |                     } else if ((typeof config.firebaserelay == 'object') && (typeof config.firebaserelay.url == 'string')) { | ||||||
|                         if (nodeVersion >= 23) { |                         // Setup the push messaging relay
 | ||||||
|                             addServerWarning('Firebase is not supported on this version of NodeJS.', 27); |                         obj.firebase = require('./firebase').CreateFirebaseRelay(obj, config.firebaserelay.url, config.firebaserelay.key); | ||||||
|                         } else { |  | ||||||
|                             // Setup the push messaging relay
 |  | ||||||
|                             obj.firebase = require('./firebase').CreateFirebaseRelay(obj, config.firebaserelay.url, config.firebaserelay.key); |  | ||||||
|                         } |  | ||||||
|                     } else if (obj.config.settings.publicpushnotifications === true) { |                     } else if (obj.config.settings.publicpushnotifications === true) { | ||||||
|                         if (nodeVersion >= 23) { |                         // Setup the Firebase push messaging relay using https://alt.meshcentral.com, this is the public push notification server.
 | ||||||
|                             addServerWarning('Firebase is not supported on this version of NodeJS.', 27); |                         obj.firebase = require('./firebase').CreateFirebaseRelay(obj, 'https://alt.meshcentral.com/firebaserelay.aspx'); | ||||||
|                         } else { |  | ||||||
|                             // Setup the Firebase push messaging relay using https://alt.meshcentral.com, this is the public push notification server.
 |  | ||||||
|                             obj.firebase = require('./firebase').CreateFirebaseRelay(obj, 'https://alt.meshcentral.com/firebaserelay.aspx'); |  | ||||||
|                         } |  | ||||||
|                     } |                     } | ||||||
| 
 | 
 | ||||||
|                     // Start periodic maintenance
 |                     // Start periodic maintenance
 | ||||||
| @ -4049,7 +4041,12 @@ function InstallModules(modules, args, func) { | |||||||
|             try { |             try { | ||||||
|                 // Does the module need a specific version?
 |                 // Does the module need a specific version?
 | ||||||
|                 if (moduleVersion) { |                 if (moduleVersion) { | ||||||
|                     if (require(`${moduleName}/package.json`).version != moduleVersion) { throw new Error(); } |                     var versionMatch = false; | ||||||
|  |                     try { versionMatch = (require(`${moduleName}/package.json`).version == moduleVersion) } catch (ex) { } | ||||||
|  |                     if (versionMatch == false) { | ||||||
|  |                         const packageJson = JSON.parse(require('fs').readFileSync(require('path').join(__dirname, 'node_modules', moduleName, 'package.json'), 'utf8')); | ||||||
|  |                         if (packageJson.version != moduleVersion) { throw new Error(); } | ||||||
|  |                     } | ||||||
|                 } else { |                 } else { | ||||||
|                     // For all other modules, do the check here.
 |                     // For all other modules, do the check here.
 | ||||||
|                     // Is the module in package.json? Install exact version.
 |                     // Is the module in package.json? Install exact version.
 | ||||||
| @ -4129,7 +4126,7 @@ var ServerWarnings = { | |||||||
|     24: "Unable to load agent logo file: {0}.", |     24: "Unable to load agent logo file: {0}.", | ||||||
|     25: "This NodeJS version does not support OpenID.", |     25: "This NodeJS version does not support OpenID.", | ||||||
|     26: "This NodeJS version does not support Discord.js.", |     26: "This NodeJS version does not support Discord.js.", | ||||||
|     27: "Firebase is not supported on this version of NodeJS." |     27: "Firebase now requires a service account JSON file, Firebase disabled." | ||||||
| }; | }; | ||||||
| */ | */ | ||||||
| 
 | 
 | ||||||
| @ -4301,8 +4298,7 @@ function mainStart() { | |||||||
|         if ((typeof config.settings.webpush == 'object') && (typeof config.settings.webpush.email == 'string')) { modules.push('web-push@3.6.6'); } |         if ((typeof config.settings.webpush == 'object') && (typeof config.settings.webpush.email == 'string')) { modules.push('web-push@3.6.6'); } | ||||||
| 
 | 
 | ||||||
|         // Firebase Support
 |         // Firebase Support
 | ||||||
|         // Avoid 0.1.8 due to bugs: https://github.com/guness/node-xcs/issues/43
 |         if ((config.firebase != null) && (typeof config.firebase.serviceaccountfile == 'string')) { modules.push('firebase-admin@12.7.0'); } | ||||||
|         if (config.firebase != null) { modules.push('node-xcs@0.1.8'); } |  | ||||||
| 
 | 
 | ||||||
|         // Syslog support
 |         // Syslog support
 | ||||||
|         if ((require('os').platform() != 'win32') && (config.settings.syslog || config.settings.syslogjson)) { modules.push('modern-syslog@1.2.0'); } |         if ((require('os').platform() != 'win32') && (config.settings.syslog || config.settings.syslogjson)) { modules.push('modern-syslog@1.2.0'); } | ||||||
|  | |||||||
| @ -2484,7 +2484,7 @@ | |||||||
|                             24: "Unable to load agent logo file: {0}.", |                             24: "Unable to load agent logo file: {0}.", | ||||||
|                             25: "This NodeJS version does not support OpenID.", |                             25: "This NodeJS version does not support OpenID.", | ||||||
|                             26: "This NodeJS version does not support Discord.js.", |                             26: "This NodeJS version does not support Discord.js.", | ||||||
|                             27: "Firebase is not supported on this version of NodeJS." |                             27: "Firebase now requires a service account JSON file, Firebase disabled." | ||||||
|                         }; |                         }; | ||||||
|                         var x = ''; |                         var x = ''; | ||||||
|                         for (var i in message.warnings) { |                         for (var i in message.warnings) { | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user