Fixed OTP domain, started web push.
Ylian Saint-Hilaire committed
Feb 8, 2021 at 19:31 UTC
9e06a24975ff6e1fc49d00b1c4e579a34e89033c
3 files changed
+33
-1
meshcentral.js
+18
@@ -1544,6 +1544,21 @@ function CreateMeshCentralServer(config, args) {
1544
if ((obj.smsserver != null) && (obj.args.lanonly == true)) { addServerWarning("SMS gateway has limited use in LAN mode."); }
1545
}
1546
1547
+ // Setup web based push notifications
1548
+ if ((typeof config.settings.webpush == 'object') && (typeof config.settings.webpush.email == 'string')) {
1549
+ obj.webpush = require('web-push');
1550
+ var vapidKeys = null;
1551
+ try { vapidKeys = JSON.parse(obj.fs.readFileSync(obj.path.join(obj.datapath, 'vapid.json')).toString()); } catch (ex) { }
1552
+ if ((vapidKeys == null) || (typeof vapidKeys.publicKey != 'string') || (typeof vapidKeys.privateKey != 'string')) {
1553
+ console.log("Generating web push VAPID keys...");
1554
+ vapidKeys = obj.webpush.generateVAPIDKeys();
1555
+ obj.fs.writeFileSync(obj.path.join(obj.datapath, 'vapid.json'), JSON.stringify(vapidKeys));
1556
+ }
1557
+ obj.webpush.vapidPublicKey = vapidKeys.publicKey;
1558
+ obj.webpush.setVapidDetails('mailto:' + config.settings.webpush.email, vapidKeys.publicKey, vapidKeys.privateKey);
1559
+ if (typeof config.settings.webpush.gcmapi == 'string') { webpush.setGCMAPIKey(config.settings.webpush.gcmapi); }
1560
+ }
1561
+
1562
// Setup Firebase
1563
if ((config.firebase != null) && (typeof config.firebase.senderid == 'string') && (typeof config.firebase.serverkey == 'string')) {
1564
const NodeJSVer = Number(process.version.match(/^v(\d+\.\d+)/)[1]);
@@ -3065,6 +3080,9 @@ function mainStart() {
3080
if (NodeJSVer < 8) { console.log("SMS Plivo support requires Node v8 or above, current version is " + process.version + "."); } else { modules.push('plivo'); }
3081
}
3082
3083
+ // Setup web based push notifications
3084
+ if ((typeof config.settings.webpush == 'object') && (typeof config.settings.webpush.email == 'string')) { modules.push('web-push'); }
3085
+
3086
// Firebase Support
3087
if (config.firebase != null) {
3088
const NodeJSVer = Number(process.version.match(/^v(\d+\.\d+)/)[1]);
meshuser.js
+5
-1
@@ -463,6 +463,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
463
if (Array.isArray(domain.altmessenging)) { serverinfo.altmessenging = []; for (var i in domain.altmessenging) { if ((typeof domain.altmessenging[i] == 'object') && (typeof domain.altmessenging[i].name == 'string') && (typeof domain.altmessenging[i].url == 'string')) { serverinfo.altmessenging.push({ name: domain.altmessenging[i].name, url: domain.altmessenging[i].url }); } } }
464
serverinfo.https = true;
465
serverinfo.redirport = args.redirport;
466
+ if (parent.parent.webpush != null) { serverinfo.vapidpublickey = parent.parent.webpush.vapidPublicKey; } // Web push public key
467
468
// Build the mobile agent URL, this is used to connect mobile devices
469
var agentServerName = parent.getWebServerName(domain);
@@ -4254,7 +4255,10 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
4255
try { otplib = require('otplib'); } catch (ex) { }
4256
if (otplib == null) { break; }
4257
const secret = otplib.authenticator.generateSecret(); // TODO: Check the random source of this value.
4257
- ws.send(JSON.stringify({ action: 'otpauth-request', secret: secret, url: otplib.authenticator.keyuri(user.name, parent.certificates.CommonName, secret) }));
4258
+
4259
+ var domainName = parent.certificates.CommonName;
4260
+ if (domain.dns != null) { domainName = domain.dns; }
4261
+ ws.send(JSON.stringify({ action: 'otpauth-request', secret: secret, url: otplib.authenticator.keyuri(user.name, domainName, secret) }));
4262
}
4263
break;
4264
}
views/default.handlebars
+10
@@ -14449,6 +14449,16 @@
14449
function round(value, precision) { var multiplier = Math.pow(10, precision || 0); return Math.round(value * multiplier) / multiplier; }
14450
function safeNewWindow(url, target) { var newWindow = window.open(url, target, 'noopener,noreferrer'); if (newWindow) { newWindow.opener = null; } }
14451
14452
+ // Used to convert Base64 public VAPID key to bytearray.
14453
+ function urlBase64ToUint8Array(base64String) {
14454
+ const padding = '='.repeat((4 - base64String.length % 4) % 4);
14455
+ const base64 = (base64String + padding).replace(/-/g, '+').replace(/_/g, '/');
14456
+ const rawData = window.atob(base64);
14457
+ const outputArray = new Uint8Array(rawData.length);
14458
+ for (let i = 0; i < rawData.length; ++i) { outputArray[i] = rawData.charCodeAt(i); }
14459
+ return outputArray;
14460
+ }
14461
+
14462
// Webkit seems to have a problem with "download" tag causing "network error", but openning the download in a hidden frame fixes it.
14463
// So we do that for all browsers except FireFox
14464
function downloadFile(link, name, closeDialog) {