Removed hcaptcha
parent
33be91e6d5
commit
839ea3799a
|
@ -1,5 +1,4 @@
|
||||||
docker-compose.yml
|
docker-compose.yml
|
||||||
node_modules
|
node_modules
|
||||||
npm-debug.log
|
npm-debug.log
|
||||||
.c9/
|
|
||||||
.env
|
.env
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
docker-compose.yml
|
|
||||||
.c9/
|
|
||||||
node_modules/
|
node_modules/
|
||||||
|
|
||||||
|
docker-compose.yml
|
||||||
.env
|
.env
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
This is a PGP-enabled contact form that you can use. It has two parts:
|
This is a PGP-enabled contact form that you can use. It has two parts:
|
||||||
|
|
||||||
- A static web form that uses javascript to encrypt a message and send it to an api endpoint
|
- A static web form that uses javascript to encrypt a message and send it to an api endpoint
|
||||||
- An API which checks the captcha and sends the message by email
|
- An API which sends the message by email
|
||||||
|
|
||||||
## Setting up a server
|
## Setting up a server
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,6 @@ services:
|
||||||
container_name: mailapi
|
container_name: mailapi
|
||||||
environment:
|
environment:
|
||||||
- PORT=8080
|
- PORT=8080
|
||||||
- HCAPTCHA_SECRET=0xXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
|
|
||||||
- MAIL_FROM="My contact form <mailer@myserver.tld>"
|
- MAIL_FROM="My contact form <mailer@myserver.tld>"
|
||||||
- MAIL_TO=me@myserver.tld
|
- MAIL_TO=me@myserver.tld
|
||||||
- MAIL_SERVER=mail.myserver.tld
|
- MAIL_SERVER=mail.myserver.tld
|
||||||
|
|
|
@ -7,11 +7,9 @@
|
||||||
<p><input type="text" id="subject-input" placeholder="Subject"></p>
|
<p><input type="text" id="subject-input" placeholder="Subject"></p>
|
||||||
<p><textarea id="message-input" placeholder="Your message"></textarea></p>
|
<p><textarea id="message-input" placeholder="Your message"></textarea></p>
|
||||||
|
|
||||||
<p><button id="send-button" class="h-captcha" data-sitekey="<YOUR HCAPTCHA SITE KEY>" data-callback="sendClicked">Send</button></p>
|
<p><button id="send-button">Send</button></p>
|
||||||
|
|
||||||
<p>This page is protected by <a href="https://www.hcaptcha.com/">hCaptcha</a> so its <a href="https://hcaptcha.com/privacy">Privacy Policy</a> and <a href="https://hcaptcha.com/terms">Terms of Service</a> apply.</p>
|
|
||||||
|
|
||||||
<script src="https://js.hcaptcha.com/1/api.js" async defer></script>
|
|
||||||
<script src="/PATH/TO/LOCAL/COPY/OF/openpgp.min.js"></script>
|
<script src="/PATH/TO/LOCAL/COPY/OF/openpgp.min.js"></script>
|
||||||
<script>/* global openpgp fetch */
|
<script>/* global openpgp fetch */
|
||||||
let send = document.getElementById('send-button')
|
let send = document.getElementById('send-button')
|
||||||
|
@ -22,8 +20,7 @@
|
||||||
|
|
||||||
const API_URL = "https://mailapi.mydomain.tld/"
|
const API_URL = "https://mailapi.mydomain.tld/"
|
||||||
|
|
||||||
async function sendClicked (captchaToken) {
|
async function sendClicked () {
|
||||||
if (captchaToken) {
|
|
||||||
send.disabled = true
|
send.disabled = true
|
||||||
send.innerHTML = `Sending... `
|
send.innerHTML = `Sending... `
|
||||||
let res; try {
|
let res; try {
|
||||||
|
@ -32,7 +29,6 @@
|
||||||
// cache: 'no-cache',
|
// cache: 'no-cache',
|
||||||
headers: {'content-type': 'application/json'},
|
headers: {'content-type': 'application/json'},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
token: captchaToken,
|
|
||||||
name: name.value,
|
name: name.value,
|
||||||
subj: subj.value,
|
subj: subj.value,
|
||||||
email: email.value,
|
email: email.value,
|
||||||
|
@ -63,12 +59,9 @@ lk6lY0ktTb+vRnndyN3m+XW1mYdv3xUZMjQwMBtgdZbfY43pq8+N55tSTycF
|
||||||
if (res.status===200) {
|
if (res.status===200) {
|
||||||
text.value = ''; subj.value = ''; name.value = ''; email.value = ''
|
text.value = ''; subj.value = ''; name.value = ''; email.value = ''
|
||||||
alert(Sent!')
|
alert(Sent!')
|
||||||
} else if (res.status===403)
|
} else if (res.status===500)
|
||||||
alert('hCaptcha failed! Please try again.')
|
|
||||||
else if (res.status===500)
|
|
||||||
alert('Backend failed! Please try again. If the problem persists, please email hostmaster@[this domain].')
|
alert('Backend failed! Please try again. If the problem persists, please email hostmaster@[this domain].')
|
||||||
else alert('Unknown error! Please try again. If the problem persists, please email hostmaster@[this domain].')
|
else alert('Unknown error! Please try again. If the problem persists, please email hostmaster@[this domain].')
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
</script>
|
</script>
|
||||||
|
|
31
index.js
31
index.js
|
@ -1,36 +1,22 @@
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
require('dotenv').config()
|
require('dotenv').config()
|
||||||
const express = require('express')
|
const express = require('express')
|
||||||
const app = express()
|
const app = express()
|
||||||
const {verify} = require('hcaptcha')
|
|
||||||
const PORT = process.env.PORT || 8080
|
const PORT = process.env.PORT || 8080
|
||||||
|
|
||||||
const mailer = require('nodemailer').createTransport({
|
const mailer = require('nodemailer').createTransport({
|
||||||
host: process.env.MAIL_SERVER,
|
host: process.env.MAIL_SERVER,
|
||||||
port: 587,
|
port: (process.env.MAIL_USER!=null)?25:587,
|
||||||
auth: {
|
auth: (process.env.MAIL_USER!=null)?null:{
|
||||||
user: process.env.MAIL_USER,
|
user: process.env.MAIL_USER,
|
||||||
pass: process.env.MAIL_PASS,
|
pass: process.env.MAIL_PASS,
|
||||||
},
|
},
|
||||||
tls: {
|
tls: (process.env.MAIL_USER!=null)?null:{
|
||||||
rejectUnauthorized: false,
|
rejectUnauthorized: false,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
app.use(express.json())
|
app.use(express.json()).post('/', async (req, res) => {
|
||||||
app.post('/', async (req, res) => {
|
|
||||||
// console.log(`Received token: ${req.body['token']}`)
|
|
||||||
|
|
||||||
// Check token
|
|
||||||
let data
|
|
||||||
try {
|
|
||||||
data = await verify(process.env.HCAPTCHA_SECRET, req.body['token'])
|
|
||||||
} catch (err) {
|
|
||||||
console.error(`Failed to check hcaptcha\n${err}`)
|
|
||||||
return res.sendStatus(500)
|
|
||||||
}
|
|
||||||
if (data.success === true) {
|
|
||||||
|
|
||||||
// Parse from address
|
// Parse from address
|
||||||
let from
|
let from
|
||||||
|
@ -57,13 +43,6 @@ app.post('/', async (req, res) => {
|
||||||
console.log(`Sent email ${mail_res.messageId}`)
|
console.log(`Sent email ${mail_res.messageId}`)
|
||||||
return res.sendStatus(200)
|
return res.sendStatus(200)
|
||||||
|
|
||||||
// hcaptcha failed
|
}).listen(PORT, () => {
|
||||||
} else {
|
|
||||||
console.log(`Failed hCaptcha with errors: ${data['error-codes']}`)
|
|
||||||
return res.sendStatus(403)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
app.listen(PORT, () => {
|
|
||||||
console.log(`API listening on ${PORT}`)
|
console.log(`API listening on ${PORT}`)
|
||||||
})
|
})
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -10,10 +10,9 @@
|
||||||
"author": "Keith Irwin",
|
"author": "Keith Irwin",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"body-parser": "^1.19.0",
|
"body-parser": "^1.20.2",
|
||||||
"dotenv": "^10.0.0",
|
"dotenv": "^10.0.0",
|
||||||
"express": "^4.17.1",
|
"express": "^4.18.2",
|
||||||
"hcaptcha": "^0.1.0",
|
"nodemailer": "^6.9.1"
|
||||||
"nodemailer": "^6.7.1"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue