- Added better anti DoS protection - Added better security measures (HTTP headers, etc.) - Added TLS support - Added support for configurable rate limiting - Added default 404 and error handling - Updated proxy settings - Updated env naming
72 lines
1.6 KiB
JavaScript
72 lines
1.6 KiB
JavaScript
import nodemailer from 'nodemailer';
|
|
import { random } from './random';
|
|
import { log } from './logManager';
|
|
|
|
const transporter = nodemailer.createTransport({
|
|
host: process.env.MAIL_SERVER,
|
|
port: 465,
|
|
secure: true,
|
|
auth: {
|
|
user: `${process.env.MAIL_ADDRESS}`,
|
|
pass: `${process.env.PASSWORD}`,
|
|
},
|
|
tls: { rejectUnauthorized: false },
|
|
});
|
|
|
|
function sendMail(email, head, body) {
|
|
try {
|
|
const mailOptions = {
|
|
from: `"HSP-GDH" <${process.env.MAIL}>`,
|
|
to: email,
|
|
subject: head,
|
|
text: body,
|
|
};
|
|
transporter.sendMail(mailOptions, (error, info) => {
|
|
if (error) {
|
|
log('Retrying connection to SMTP');
|
|
if (sendMail(email, head, body)) return true;
|
|
}
|
|
else {
|
|
log('Email sent: ' + info.response);
|
|
return true;
|
|
}
|
|
});
|
|
}
|
|
catch (err) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
async function sendVerification(email, userId, type = 'register', code = null) {
|
|
try {
|
|
code ? code : code = random(100000, 999999);
|
|
let title, body;
|
|
switch (type) {
|
|
case 'email':
|
|
title = 'Your verification code for HSP-GDH';
|
|
body = `Verification code: ${code}\nLink: ${process.env.DOMAIN}/email/verify?code=${code}`;
|
|
break;
|
|
case 'password':
|
|
title = 'Your password reset code for HSP-GDH';
|
|
body = `Verification code: ${code}\nLink: ${process.env.DOMAIN}/password/reset?u=${userId}&c=${code}`;
|
|
break;
|
|
case '2fa':
|
|
title = 'Your 2FA code for HSP-GDH';
|
|
body = `Verification code: ${code}`;
|
|
break;
|
|
default:
|
|
return false;
|
|
}
|
|
await sendMail(email, title, body);
|
|
return code;
|
|
|
|
}
|
|
catch (err) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export {
|
|
sendMail,
|
|
sendVerification,
|
|
}; |