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