2021-11-24 11:02:52 -07:00
|
|
|
'use strict'
|
|
|
|
|
2021-11-24 11:19:34 -07:00
|
|
|
require('dotenv').config()
|
2021-11-24 12:20:12 -07:00
|
|
|
const express = require('express')
|
2021-11-24 17:32:01 -07:00
|
|
|
const app = express()
|
2021-11-24 11:19:34 -07:00
|
|
|
const {verify} = require('hcaptcha')
|
2021-11-26 16:58:39 -07:00
|
|
|
const PORT = process.env.PORT || 8080
|
2021-11-24 11:19:34 -07:00
|
|
|
|
2021-11-24 12:37:26 -07:00
|
|
|
const mailer = require('nodemailer').createTransport({
|
|
|
|
host: process.env.MAIL_SERVER,
|
|
|
|
port: 587,
|
|
|
|
auth: {
|
|
|
|
user: process.env.MAIL_USER,
|
|
|
|
pass: process.env.MAIL_PASS,
|
|
|
|
},
|
2021-11-25 10:47:37 -07:00
|
|
|
tls: {
|
|
|
|
rejectUnauthorized: false,
|
|
|
|
},
|
2021-11-24 12:37:26 -07:00
|
|
|
})
|
|
|
|
|
2021-11-25 10:47:37 -07:00
|
|
|
app.use(express.json())
|
2021-11-24 17:32:01 -07:00
|
|
|
app.post('/', async (req, res) => {
|
2021-12-26 16:00:05 -07:00
|
|
|
console.log(`Received token: ${req.body['token']}`)
|
2021-11-24 17:25:56 -07:00
|
|
|
|
2021-11-24 11:19:34 -07:00
|
|
|
// Check token
|
2021-11-24 17:44:34 -07:00
|
|
|
let data
|
2021-11-24 11:19:34 -07:00
|
|
|
try {
|
2021-11-24 17:44:34 -07:00
|
|
|
data = await verify(process.env.HCAPTCHA_SECRET, req.body['token'])
|
2021-11-24 13:57:54 -07:00
|
|
|
} catch (err) {
|
|
|
|
console.error(`Failed to check hcaptcha\n${err}`)
|
|
|
|
return res.sendStatus(500)
|
|
|
|
}
|
2021-11-24 12:37:26 -07:00
|
|
|
if (data.success === true) {
|
2021-11-25 15:50:34 -07:00
|
|
|
|
|
|
|
// Parse from address
|
2021-11-24 12:37:26 -07:00
|
|
|
let from
|
|
|
|
if (req.body['name'] && req.body['email']) from = `${req.body['name']} <${req.body['email']}>`
|
|
|
|
else if (req.body['name']) from = req.body['name']
|
|
|
|
else if (req.body['email']) from = req.body['email']
|
|
|
|
else from = 'Anonymous'
|
2021-11-24 11:02:52 -07:00
|
|
|
|
2021-11-24 12:37:26 -07:00
|
|
|
// Send email
|
2021-11-24 18:25:23 -07:00
|
|
|
let mail_res;
|
2021-11-24 12:37:26 -07:00
|
|
|
try {
|
2021-11-25 10:47:37 -07:00
|
|
|
console.log(`Sending email from ${from} to ${process.env.MAIL_TO}...`)
|
2021-11-24 18:25:23 -07:00
|
|
|
mail_res = await mailer.sendMail({
|
2021-11-26 16:54:51 -07:00
|
|
|
from: process.env.MAIL_FROM,
|
2021-11-25 10:47:37 -07:00
|
|
|
replyTo: from,
|
2021-11-24 12:37:26 -07:00
|
|
|
to: process.env.MAIL_TO,
|
|
|
|
subject: req.body['subj'],
|
|
|
|
text: req.body['msg'],
|
|
|
|
})
|
2021-11-24 13:57:54 -07:00
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
2021-12-26 15:58:13 -07:00
|
|
|
return res.sendStatus(500)
|
2021-11-25 15:52:05 -07:00
|
|
|
}
|
|
|
|
console.log(`Sent email ${mail_res.messageId}`)
|
|
|
|
return res.sendStatus(200)
|
2021-11-24 13:57:54 -07:00
|
|
|
|
|
|
|
// hcaptcha failed
|
2021-12-26 15:51:55 -07:00
|
|
|
} else {
|
2021-12-26 16:03:03 -07:00
|
|
|
console.log(`Failed hCaptcha with errors: ${data['error-codes']`)
|
2021-12-26 15:51:55 -07:00
|
|
|
return res.sendStatus(403)
|
|
|
|
}
|
2021-11-24 17:32:01 -07:00
|
|
|
})
|
|
|
|
|
2021-11-26 16:58:39 -07:00
|
|
|
app.listen(PORT, () => {
|
|
|
|
console.log(`API listening on ${PORT}`)
|
2021-11-24 11:02:52 -07:00
|
|
|
})
|