From 9e06a24975ff6e1fc49d00b1c4e579a34e89033c Mon Sep 17 00:00:00 2001 From: Ylian Saint-Hilaire Date: Mon, 8 Feb 2021 19:31:29 -0800 Subject: [PATCH] Fixed OTP domain, started web push. --- meshcentral.js | 18 ++++++++++++++++++ meshuser.js | 6 +++++- views/default.handlebars | 10 ++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/meshcentral.js b/meshcentral.js index 0fa7caab..0150ee67 100644 --- a/meshcentral.js +++ b/meshcentral.js @@ -1544,6 +1544,21 @@ function CreateMeshCentralServer(config, args) { if ((obj.smsserver != null) && (obj.args.lanonly == true)) { addServerWarning("SMS gateway has limited use in LAN mode."); } } + // Setup web based push notifications + if ((typeof config.settings.webpush == 'object') && (typeof config.settings.webpush.email == 'string')) { + obj.webpush = require('web-push'); + var vapidKeys = null; + try { vapidKeys = JSON.parse(obj.fs.readFileSync(obj.path.join(obj.datapath, 'vapid.json')).toString()); } catch (ex) { } + if ((vapidKeys == null) || (typeof vapidKeys.publicKey != 'string') || (typeof vapidKeys.privateKey != 'string')) { + console.log("Generating web push VAPID keys..."); + vapidKeys = obj.webpush.generateVAPIDKeys(); + obj.fs.writeFileSync(obj.path.join(obj.datapath, 'vapid.json'), JSON.stringify(vapidKeys)); + } + obj.webpush.vapidPublicKey = vapidKeys.publicKey; + obj.webpush.setVapidDetails('mailto:' + config.settings.webpush.email, vapidKeys.publicKey, vapidKeys.privateKey); + if (typeof config.settings.webpush.gcmapi == 'string') { webpush.setGCMAPIKey(config.settings.webpush.gcmapi); } + } + // Setup Firebase if ((config.firebase != null) && (typeof config.firebase.senderid == 'string') && (typeof config.firebase.serverkey == 'string')) { const NodeJSVer = Number(process.version.match(/^v(\d+\.\d+)/)[1]); @@ -3065,6 +3080,9 @@ function mainStart() { if (NodeJSVer < 8) { console.log("SMS Plivo support requires Node v8 or above, current version is " + process.version + "."); } else { modules.push('plivo'); } } + // Setup web based push notifications + if ((typeof config.settings.webpush == 'object') && (typeof config.settings.webpush.email == 'string')) { modules.push('web-push'); } + // Firebase Support if (config.firebase != null) { const NodeJSVer = Number(process.version.match(/^v(\d+\.\d+)/)[1]); diff --git a/meshuser.js b/meshuser.js index da894d9d..5a5de462 100644 --- a/meshuser.js +++ b/meshuser.js @@ -463,6 +463,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use 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 }); } } } serverinfo.https = true; serverinfo.redirport = args.redirport; + if (parent.parent.webpush != null) { serverinfo.vapidpublickey = parent.parent.webpush.vapidPublicKey; } // Web push public key // Build the mobile agent URL, this is used to connect mobile devices var agentServerName = parent.getWebServerName(domain); @@ -4254,7 +4255,10 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use try { otplib = require('otplib'); } catch (ex) { } if (otplib == null) { break; } const secret = otplib.authenticator.generateSecret(); // TODO: Check the random source of this value. - ws.send(JSON.stringify({ action: 'otpauth-request', secret: secret, url: otplib.authenticator.keyuri(user.name, parent.certificates.CommonName, secret) })); + + var domainName = parent.certificates.CommonName; + if (domain.dns != null) { domainName = domain.dns; } + ws.send(JSON.stringify({ action: 'otpauth-request', secret: secret, url: otplib.authenticator.keyuri(user.name, domainName, secret) })); } break; } diff --git a/views/default.handlebars b/views/default.handlebars index 52db6ce4..9b66b548 100644 --- a/views/default.handlebars +++ b/views/default.handlebars @@ -14449,6 +14449,16 @@ function round(value, precision) { var multiplier = Math.pow(10, precision || 0); return Math.round(value * multiplier) / multiplier; } function safeNewWindow(url, target) { var newWindow = window.open(url, target, 'noopener,noreferrer'); if (newWindow) { newWindow.opener = null; } } + // Used to convert Base64 public VAPID key to bytearray. + function urlBase64ToUint8Array(base64String) { + const padding = '='.repeat((4 - base64String.length % 4) % 4); + const base64 = (base64String + padding).replace(/-/g, '+').replace(/_/g, '/'); + const rawData = window.atob(base64); + const outputArray = new Uint8Array(rawData.length); + for (let i = 0; i < rawData.length; ++i) { outputArray[i] = rawData.charCodeAt(i); } + return outputArray; + } + // Webkit seems to have a problem with "download" tag causing "network error", but openning the download in a hidden frame fixes it. // So we do that for all browsers except FireFox function downloadFile(link, name, closeDialog) {