Add dummy SMTP mode for debugging, send acks in responses for all endpoints

This commit is contained in:
Nihanth Subramanya 2018-01-15 17:38:28 +05:30
Родитель 8df002a525
Коммит 855e4701fa
1 изменённых файлов: 40 добавлений и 24 удалений

Просмотреть файл

@ -18,57 +18,73 @@ app.get("/version", function(req, res) {
app.post("/user/add", function(req, res) { app.post("/user/add", function(req, res) {
gEmails.add(req.body.email); gEmails.add(req.body.email);
console.log(req.body.email); res.json(req.body.email);
res.send("");
}); });
app.post("/user/remove", function(req, res) { app.post("/user/remove", function(req, res) {
gEmails.delete(req.body.email); gEmails.delete(req.body.email);
res.send(""); res.json(req.body.email);
}); });
let gTransporter;
app.post("/user/breached", function(req, res) { app.post("/user/breached", function(req, res) {
let emails = req.body.emails; let emails = req.body.emails;
console.log(emails); let response = [];
let transporter = nodemailer.createTransport({
host: "smtp.gmail.com",
port: 465,
secure: true,
auth: {
user: kSMTPUsername,
pass: kSMTPPassword,
},
});
for (let e of emails) { for (let email of emails) {
if (gEmails.has(e)) { if (gEmails.has(email)) {
let mailOptions = { let mailOptions = {
from: "\"Firefox Breach Alerts\" <" + kSMTPUsername + ">", // sender address from: "\"Firefox Breach Alerts\" <" + kSMTPUsername + ">", // sender address
to: e, // list of receivers to: email, // list of receivers
subject: "Firefox Breach Alert", // Subject line subject: "Firefox Breach Alert", // Subject line
text: "Your credentials were compromised in a breach.", // plain text body text: "Your credentials were compromised in a breach.", // plain text body
}; };
transporter.sendMail(mailOptions, (error, info) => { gTransporter.sendMail(mailOptions, (error, info) => {
if (error) { if (error) {
console.log(error); console.log(error);
response.push([{ email, error }]);
}
if (info) {
response.push([{ email, info }]);
} }
}); });
} }
} }
res.send(""); res.json(response);
}); });
var port = process.env.PORT || 6060; var port = process.env.PORT || 6060;
console.log("Attempting to get SMTP credentials from environment..."); if (process.env.DEBUG_DUMMY_SMTP) {
kSMTPUsername = process.env.SMTP_USERNAME; console.log("Running in dummp SMTP mode, /user/breached will send a JSON response instead of sending emails.");
kSMTPPassword = process.env.SMTP_PASSWORD; gTransporter = {
if (kSMTPUsername && kSMTPPassword) { sendMail(options, callback) {
callback(null, "")
},
};
} else {
console.log("Attempting to get SMTP credentials from environment...");
kSMTPUsername = process.env.SMTP_USERNAME;
kSMTPPassword = process.env.SMTP_PASSWORD;
if (kSMTPUsername && kSMTPPassword) {
gTransporter = nodemailer.createTransport({
host: "smtp.gmail.com",
port: 465,
secure: true,
auth: {
user: kSMTPUsername,
pass: kSMTPPassword,
},
});
}
}
if (gTransporter) {
app.listen(port, function() { app.listen(port, function() {
console.log("Listening on " + port); console.log("Listening on " + port);
}); });
} } else {
else {
console.error("SMTP credentials couldn't be read from the environment, exiting."); console.error("SMTP credentials couldn't be read from the environment, exiting.");
} }