Firebase improvements.
Ylian Saint-Hilaire committed
Mar 1, 2021 at 12:24 UTC
97d5e5352d2e410f6f5c5ce25cea57fe2964d230
4 files changed
+50
-11
firebase.js
+47
-11
@@ -35,9 +35,19 @@ module.exports.CreateFirebase = function (parent, senderid, serverkey) {
35
36
var tokenToNodeMap = {} // Token --> { nid: nodeid, mid: meshid }
37
38
+ // Setup logging
39
+ if (parent.config.firebase && (parent.config.firebase.log === true)) {
40
+ obj.logpath = parent.path.join(parent.datapath, 'firebase.txt');
41
+ 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.'); } }
42
+ } else {
43
+ obj.log = function () { }
44
+ }
45
+
46
// Messages received from client (excluding receipts)
47
xcs.on('message', function (messageId, from, data, category) {
40
- parent.debug('email', 'Firebase-Message: ' + JSON.stringify(data));
48
+ const jsonData = JSON.stringify(data);
49
+ obj.log('Firebase-Message: ' + jsonData);
50
+ parent.debug('email', 'Firebase-Message: ' + jsonData);
51
52
if (typeof data.r == 'string') {
53
// Lookup push relay server
@@ -73,6 +83,7 @@ module.exports.CreateFirebase = function (parent, senderid, serverkey) {
83
84
xcs.start();
85
86
+ obj.log('CreateFirebase-Setup');
87
parent.debug('email', 'CreateFirebase-Setup');
88
89
// EXAMPLE
@@ -83,6 +94,7 @@ module.exports.CreateFirebase = function (parent, senderid, serverkey) {
94
obj.sendToDevice = function (node, payload, options, func) {
95
parent.debug('email', 'Firebase-sendToDevice');
96
if ((node == null) || (typeof node.pmt != 'string')) return;
97
+ obj.log('sendToDevice, node:' + node._id + ', payload: ' + JSON.stringify(payload) + ', options: ' + JSON.stringify(options));
98
99
// Fill in our lookup table
100
if (node._id != null) { tokenToNodeMap[node.pmt] = { nid: node._id, mid: node.meshid, did: node.domain } }
@@ -106,7 +118,7 @@ module.exports.CreateFirebase = function (parent, senderid, serverkey) {
118
119
// Send the message
120
function callback(result) {
109
- if (result.getError() == null) { obj.stats.sent++; } else { obj.stats.sendError++; }
121
+ if (result.getError() == null) { obj.stats.sent++; obj.log('Success'); } else { obj.stats.sendError++; obj.log('Fail'); }
122
callback.func(result.getMessageId(), result.getError(), result.getErrorDescription())
123
}
124
callback.func = func;
@@ -125,11 +137,13 @@ module.exports.CreateFirebase = function (parent, senderid, serverkey) {
137
ws.on('message', function (msg) {
138
parent.debug('email', 'FBWS-Data(' + this.relayId + '): ' + msg);
139
if (typeof msg == 'string') {
140
+ obj.log('Relay: ' + msg);
141
142
// Parse the incoming push request
143
var data = null;
144
try { data = JSON.parse(msg) } catch (ex) { return; }
145
if (typeof data != 'object') return;
146
+ if (parent.common.validateObjectForMongo(data, 4096) == false) return; // Perform sanity checking on this object.
147
if (typeof data.pmt != 'string') return;
148
if (typeof data.payload != 'object') return;
149
if (typeof data.payload.notification == 'object') {
@@ -191,19 +205,32 @@ module.exports.CreateFirebaseRelay = function (parent, url, key) {
205
const relayUrl = require('url').parse(url);
206
parent.debug('email', 'CreateFirebaseRelay-Setup');
207
208
+ // Setup logging
209
+ if (parent.config.firebaserelay && (parent.config.firebaserelay.log === true)) {
210
+ obj.logpath = parent.path.join(parent.datapath, 'firebaserelay.txt');
211
+ 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.'); } }
212
+ } else {
213
+ obj.log = function () { }
214
+ }
215
+
216
+ obj.log('Starting relay to: ' + relayUrl.href);
217
if (relayUrl.protocol == 'wss:') {
218
// Setup two-way push notification channel
219
obj.wsopen = false;
197
- obj.tokenToNodeMap = {} // Token --> { nid: nodeid, mid: meshid }
220
+ obj.tokenToNodeMap = {}; // Token --> { nid: nodeid, mid: meshid }
221
+ obj.backoffTimer = 0;
222
obj.connectWebSocket = function () {
223
+ if (obj.reconnectTimer != null) { try { clearTimeout(obj.reconnectTimer); } catch (ex) { } delete obj.reconnectTimer; }
224
if (obj.wsclient != null) return;
225
obj.wsclient = new WebSocket(relayUrl.href + (key ? ('?key=' + key) : ''), { rejectUnauthorized: false })
226
obj.wsclient.on('open', function () {
227
+ obj.lastConnect = Date.now();
228
parent.debug('email', 'FBWS-Connected');
229
obj.wsopen = true;
230
});
231
obj.wsclient.on('message', function (msg) {
232
parent.debug('email', 'FBWS-Data(' + msg.length + '): ' + msg);
233
+ obj.log('Received(' + msg.length + '): ' + msg);
234
var data = null;
235
try { data = JSON.parse(msg) } catch (ex) { }
236
if (typeof data != 'object') return;
@@ -212,16 +239,20 @@ module.exports.CreateFirebaseRelay = function (parent, url, key) {
239
if (typeof data.category != 'string') return;
240
processMessage(data.messageId, data.from, data.data, data.category);
241
});
215
- obj.wsclient.on('error', function (err) {
216
- obj.wsclient = null;
217
- obj.wsopen = false;
218
- setTimeout(obj.connectWebSocket, 2000);
219
- });
220
- obj.wsclient.on('close', function () {
242
+ obj.wsclient.on('error', function (err) { obj.log('Error: ' + err); });
243
+ obj.wsclient.on('close', function (a, b, c) {
244
parent.debug('email', 'FBWS-Disconnected');
245
obj.wsclient = null;
246
obj.wsopen = false;
224
- setTimeout(obj.connectWebSocket, 2000);
247
+
248
+ // Compute the backoff timer
249
+ if (obj.reconnectTimer == null) {
250
+ if ((obj.lastConnect != null) && ((Date.now() - obj.lastConnect) > 10000)) { obj.backoffTimer = 0; }
251
+ obj.backoffTimer += 1000;
252
+ obj.backoffTimer = obj.backoffTimer * 2;
253
+ if (obj.backoffTimer > 1200000) { obj.backoffTimer = 600000; } // Maximum 10 minutes backoff.
254
+ obj.reconnectTimer = setTimeout(obj.connectWebSocket, obj.backoffTimer);
255
+ }
256
});
257
}
258
@@ -241,6 +272,7 @@ module.exports.CreateFirebaseRelay = function (parent, url, key) {
272
obj.sendToDevice = function (node, payload, options, func) {
273
parent.debug('email', 'Firebase-sendToDevice-webSocket');
274
if ((node == null) || (typeof node.pmt != 'string')) { func(0, 'error'); return; }
275
+ obj.log('sendToDevice, node:' + node._id + ', payload: ' + JSON.stringify(payload) + ', options: ' + JSON.stringify(options));
276
277
// Fill in our lookup table
278
if (node._id != null) { obj.tokenToNodeMap[node.pmt] = { nid: node._id, mid: node.meshid, did: node.domain } }
@@ -253,11 +285,13 @@ module.exports.CreateFirebaseRelay = function (parent, url, key) {
285
if (obj.wsopen == true) {
286
try { obj.wsclient.send(JSON.stringify({ pmt: node.pmt, payload: payload, options: options })); } catch (ex) { func(0, 'error'); obj.stats.sendError++; return; }
287
obj.stats.sent++;
288
+ obj.log('Sent');
289
func(1);
290
} else {
291
// TODO: Buffer the push messages until TTL.
259
- func(0, 'error');
292
obj.stats.sendError++;
293
+ obj.log('Error');
294
+ func(0, 'error');
295
}
296
}
297
obj.connectWebSocket();
@@ -268,6 +302,7 @@ module.exports.CreateFirebaseRelay = function (parent, url, key) {
302
parent.debug('email', 'Firebase-sendToDevice-httpPost');
303
if ((node == null) || (typeof node.pmt != 'string')) return;
304
305
+ obj.log('sendToDevice, node:' + node._id + ', payload: ' + JSON.stringify(payload) + ', options: ' + JSON.stringify(options));
306
const querydata = querystring.stringify({ 'msg': JSON.stringify({ pmt: node.pmt, payload: payload, options: options }) });
307
308
// Fill in the server agent cert hash
@@ -287,6 +322,7 @@ module.exports.CreateFirebaseRelay = function (parent, url, key) {
322
}
323
}
324
const req = https.request(httpOptions, function (res) {
325
+ obj.log('Response: ' + res.statusCode);
326
if (res.statusCode == 200) { obj.stats.sent++; } else { obj.stats.sendError++; }
327
if (func != null) { func(++obj.messageId, (res.statusCode == 200) ? null : 'error'); }
328
});
node11.bat
new
+1
@@ -0,0 +1 @@
1
+@%LOCALAPPDATA%\..\Roaming\nvm\v11.10.1\node meshcentral %1 %2 %3 %4 %5 %6 %7 %8 %9
\ No newline at end of file
node15.bat
new
+1
@@ -0,0 +1 @@
1
+@%LOCALAPPDATA%\..\Roaming\nvm\v15.0.1\node meshcentral %1 %2 %3 %4 %5 %6 %7 %8 %9
\ No newline at end of file
node8.bat
new
+1
@@ -0,0 +1 @@
1
+@%LOCALAPPDATA%\..\Roaming\nvm\v8.0.0\node meshcentral %1 %2 %3 %4 %5 %6 %7 %8 %9
\ No newline at end of file