Added email to console for easy debugging.

Ylian Saint-Hilaire committed Sep 2, 2022 at 15:27 UTC 9738848dc5c60c5a60cf78aafb2037d4ceeb6a48
2 files changed +59 -43
meshcentral.js
+2 -2
@@ -3878,7 +3878,7 @@ function mainStart() {
3878 if (domainCount == 0) { allsspi = false; }
3879 for (var i in config.domains) {
3880 if (i.startsWith('_')) continue;
3881 - if ((config.domains[i].smtp != null) || (config.domains[i].sendmail != null)) { nodemailer = true; }
3881 + if (((config.domains[i].smtp != null) && (config.domains[i].smtp.name != 'console')) || (config.domains[i].sendmail != null)) { nodemailer = true; }
3882 if (config.domains[i].sendgrid != null) { sendgrid = true; }
3883 if (config.domains[i].yubikey != null) { yubikey = true; }
3884 if (config.domains[i].auth == 'ldap') { ldap = true; }
@@ -3921,7 +3921,7 @@ function mainStart() {
3921 if (config.settings.plugins != null) { modules.push('semver'); } // Required for version compat testing and update checks
3922 if ((config.settings.plugins != null) && (config.settings.plugins.proxy != null)) { modules.push('https-proxy-agent'); } // Required for HTTP/HTTPS proxy support
3923 else if (config.settings.xmongodb != null) { modules.push('mongojs'); } // Add MongoJS, old driver.
3924 - if (nodemailer || (config.smtp != null) || (config.sendmail != null)) { modules.push('nodemailer'); } // Add SMTP support
3924 + if (nodemailer || ((config.smtp != null) && (config.smtp.name != 'console')) || (config.sendmail != null)) { modules.push('nodemailer'); } // Add SMTP support
3925 if (sendgrid || (config.sendgrid != null)) { modules.push('@sendgrid/mail'); } // Add SendGrid support
3926 if (args.translate) { modules.push('jsdom'); modules.push('esprima'); modules.push('minify-js'); modules.push('html-minifier'); } // Translation support
3927 if (typeof config.settings.crowdsec == 'object') { modules.push('@crowdsec/express-bouncer'); } // Add CrowdSec bounser module (https://www.npmjs.com/package/@crowdsec/express-bouncer)
meshmail.js
+57 -41
@@ -43,26 +43,31 @@ module.exports.CreateMeshMail = function (parent, domain) {
43 if (obj.config.sendgrid.verifyemail == true) { obj.verifyemail = true; }
44 } else if (obj.config.smtp != null) {
45 // Setup SMTP mail server
46 - const nodemailer = require('nodemailer');
47 - var options = { name: obj.config.smtp.name, host: obj.config.smtp.host, secure: (obj.config.smtp.tls == true), tls: {} };
48 - //var options = { host: obj.config.smtp.host, secure: (obj.config.smtp.tls == true), tls: { secureProtocol: 'SSLv23_method', ciphers: 'RSA+AES:!aNULL:!MD5:!DSS', secureOptions: constants.SSL_OP_NO_SSLv2 | constants.SSL_OP_NO_SSLv3 | constants.SSL_OP_NO_COMPRESSION | constants.SSL_OP_CIPHER_SERVER_PREFERENCE, rejectUnauthorized: false } };
49 - if (obj.config.smtp.port != null) { options.port = obj.config.smtp.port; }
50 - if (obj.config.smtp.tlscertcheck === false) { options.tls.rejectUnauthorized = false; }
51 - if (obj.config.smtp.tlsstrict === true) { options.tls.secureProtocol = 'SSLv23_method'; options.tls.ciphers = 'RSA+AES:!aNULL:!MD5:!DSS'; options.tls.secureOptions = constants.SSL_OP_NO_SSLv2 | constants.SSL_OP_NO_SSLv3 | constants.SSL_OP_NO_COMPRESSION | constants.SSL_OP_CIPHER_SERVER_PREFERENCE; }
52 - if ((obj.config.smtp.auth != null) && (typeof obj.config.smtp.auth == 'object')) {
53 - var user = obj.config.smtp.from;
54 - if ((user == null) && (obj.config.smtp.user != null)) { user = obj.config.smtp.user; }
55 - if ((obj.config.smtp.auth.user != null) && (typeof obj.config.smtp.auth.user == 'string')) { user = obj.config.smtp.auth.user; }
56 - if (user.toLowerCase().endsWith('@gmail.com')) { options = { service: 'gmail', auth: { user: user } }; obj.config.smtp.host = 'gmail'; } else { options.auth = { user: user } }
57 - if (obj.config.smtp.auth.type) { options.auth.type = obj.config.smtp.auth.type; }
58 - if (obj.config.smtp.auth.clientid) { options.auth.clientId = obj.config.smtp.auth.clientid; options.auth.type = 'OAuth2'; }
59 - if (obj.config.smtp.auth.clientsecret) { options.auth.clientSecret = obj.config.smtp.auth.clientsecret; }
60 - if (obj.config.smtp.auth.refreshtoken) { options.auth.refreshToken = obj.config.smtp.auth.refreshtoken; }
61 - }
62 - else if ((obj.config.smtp.user != null) && (obj.config.smtp.pass != null)) { options.auth = { user: obj.config.smtp.user, pass: obj.config.smtp.pass }; }
63 - if (obj.config.smtp.verifyemail == true) { obj.verifyemail = true; }
46 + if (obj.config.smtp.name == 'console') {
47 + // This is for debugging, the mails will be displayed on the console
48 + obj.smtpServer = 'console';
49 + } else {
50 + const nodemailer = require('nodemailer');
51 + var options = { name: obj.config.smtp.name, host: obj.config.smtp.host, secure: (obj.config.smtp.tls == true), tls: {} };
52 + //var options = { host: obj.config.smtp.host, secure: (obj.config.smtp.tls == true), tls: { secureProtocol: 'SSLv23_method', ciphers: 'RSA+AES:!aNULL:!MD5:!DSS', secureOptions: constants.SSL_OP_NO_SSLv2 | constants.SSL_OP_NO_SSLv3 | constants.SSL_OP_NO_COMPRESSION | constants.SSL_OP_CIPHER_SERVER_PREFERENCE, rejectUnauthorized: false } };
53 + if (obj.config.smtp.port != null) { options.port = obj.config.smtp.port; }
54 + if (obj.config.smtp.tlscertcheck === false) { options.tls.rejectUnauthorized = false; }
55 + if (obj.config.smtp.tlsstrict === true) { options.tls.secureProtocol = 'SSLv23_method'; options.tls.ciphers = 'RSA+AES:!aNULL:!MD5:!DSS'; options.tls.secureOptions = constants.SSL_OP_NO_SSLv2 | constants.SSL_OP_NO_SSLv3 | constants.SSL_OP_NO_COMPRESSION | constants.SSL_OP_CIPHER_SERVER_PREFERENCE; }
56 + if ((obj.config.smtp.auth != null) && (typeof obj.config.smtp.auth == 'object')) {
57 + var user = obj.config.smtp.from;
58 + if ((user == null) && (obj.config.smtp.user != null)) { user = obj.config.smtp.user; }
59 + if ((obj.config.smtp.auth.user != null) && (typeof obj.config.smtp.auth.user == 'string')) { user = obj.config.smtp.auth.user; }
60 + if (user.toLowerCase().endsWith('@gmail.com')) { options = { service: 'gmail', auth: { user: user } }; obj.config.smtp.host = 'gmail'; } else { options.auth = { user: user } }
61 + if (obj.config.smtp.auth.type) { options.auth.type = obj.config.smtp.auth.type; }
62 + if (obj.config.smtp.auth.clientid) { options.auth.clientId = obj.config.smtp.auth.clientid; options.auth.type = 'OAuth2'; }
63 + if (obj.config.smtp.auth.clientsecret) { options.auth.clientSecret = obj.config.smtp.auth.clientsecret; }
64 + if (obj.config.smtp.auth.refreshtoken) { options.auth.refreshToken = obj.config.smtp.auth.refreshtoken; }
65 + }
66 + else if ((obj.config.smtp.user != null) && (obj.config.smtp.pass != null)) { options.auth = { user: obj.config.smtp.user, pass: obj.config.smtp.pass }; }
67 + if (obj.config.smtp.verifyemail == true) { obj.verifyemail = true; }
68
65 - obj.smtpServer = nodemailer.createTransport(options);
69 + obj.smtpServer = nodemailer.createTransport(options);
70 + }
71 } else if (obj.config.sendmail != null) {
72 // Setup Sendmail
73 const nodemailer = require('nodemailer');
@@ -440,39 +445,50 @@ module.exports.CreateMeshMail = function (parent, domain) {
445 }
446 });
447 } else if (obj.smtpServer != null) {
443 - // SMTP send
448 parent.debug('email', 'SMTP sending mail to ' + mailToSend.to + '.');
445 - obj.smtpServer.sendMail(mailToSend, function (err, info) {
446 - parent.debug('email', 'SMTP response: ' + JSON.stringify(err) + ', ' + JSON.stringify(info));
449 + if (obj.smtpServer == 'console') {
450 + // Display the email on the console, this is for easy debugging
451 + if (mailToSend.from == null) { delete mailToSend.from; }
452 + if (mailToSend.html == null) { delete mailToSend.html; }
453 + console.log('Email', mailToSend);
454 obj.sendingMail = false;
448 - if (err == null) {
449 - // Send the next mail
450 - obj.pendingMails.shift();
451 - obj.retry = 0;
452 - sendNextMail();
453 - } else {
454 - obj.retry++;
455 - parent.debug('email', 'SMTP server failed (Retry:' + obj.retry + '): ' + JSON.stringify(err));
456 - console.log('SMTP server failed (Retry:' + obj.retry + '/3): ' + JSON.stringify(err));
457 - // Wait and try again
458 - if (obj.retry < 3) {
459 - setTimeout(sendNextMail, 10000);
460 - } else {
461 - // Failed, send the next mail
462 - parent.debug('email', 'SMTP server failed (Skipping): ' + JSON.stringify(err));
463 - console.log('SMTP server failed (Skipping): ' + JSON.stringify(err));
455 + obj.pendingMails.shift();
456 + obj.retry = 0;
457 + sendNextMail();
458 + } else {
459 + // SMTP send
460 + obj.smtpServer.sendMail(mailToSend, function (err, info) {
461 + parent.debug('email', 'SMTP response: ' + JSON.stringify(err) + ', ' + JSON.stringify(info));
462 + obj.sendingMail = false;
463 + if (err == null) {
464 + // Send the next mail
465 obj.pendingMails.shift();
466 obj.retry = 0;
467 sendNextMail();
468 + } else {
469 + obj.retry++;
470 + parent.debug('email', 'SMTP server failed (Retry:' + obj.retry + '): ' + JSON.stringify(err));
471 + console.log('SMTP server failed (Retry:' + obj.retry + '/3): ' + JSON.stringify(err));
472 + // Wait and try again
473 + if (obj.retry < 3) {
474 + setTimeout(sendNextMail, 10000);
475 + } else {
476 + // Failed, send the next mail
477 + parent.debug('email', 'SMTP server failed (Skipping): ' + JSON.stringify(err));
478 + console.log('SMTP server failed (Skipping): ' + JSON.stringify(err));
479 + obj.pendingMails.shift();
480 + obj.retry = 0;
481 + sendNextMail();
482 + }
483 }
468 - }
469 - });
484 + });
485 + }
486 }
487 }
488
489 // Send out the next mail in the pending list
490 obj.verify = function () {
475 - if (obj.smtpServer == null) return;
491 + if ((obj.smtpServer == null) || (obj.smtpServer == 'console')) return;
492 obj.smtpServer.verify(function (err, info) {
493 if (err == null) {
494 if (obj.config.smtp.host == 'gmail') {