-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
188 lines (156 loc) · 4.88 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
This program is the API server for the Badger Spill website.
Copyright (C) 2023-2024 James Julich
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
const process = require("process");
const fs = require("fs");
const express = require("express");
const cors = require("cors");
const http = require("http");
const nodemailer = require("nodemailer");
require("dotenv").config();
// Config
const TIME_LOCALE = "en-US";
const TIMEZONE = "America/Chicago";
const CORS_WHITELIST = [
"https://thebadgerspill.com",
"https://badger-spill.github.io",
"http://localhost:4321",
]; // This is the list of domains that forms can be submitted from
// Environment Variables
const RECAPTCHA_SECRET_KEY = process.env.RECAPTCHA_SECRET_KEY;
const EMAIL_USERNAME = process.env.EMAIL_USERNAME;
const EMAIL_PASSWORD = process.env.EMAIL_PASSWORD;
const SMTP_HOST = process.env.SMTP_HOST;
const SMTP_PORT = process.env.SMTP_PORT;
const PORT = process.env.PORT;
const BEHIND_REVERSE_PROXY =
process.env.BEHIND_REVERSE_PROXY != undefined &&
process.env.BEHIND_REVERSE_PROXY.toLowerCase() == "true";
const transporter = nodemailer.createTransport({
host: SMTP_HOST,
port: SMTP_PORT,
secure: true,
requireTLS: true,
auth: {
user: EMAIL_USERNAME,
pass: EMAIL_PASSWORD,
},
logger: false, // Do not log information
debug: false, // Output nothing
});
/**
* This function contacts Google's servers and verifies the captcha response.
* @param {*} req the request object
* @returns true or false depending on captcha validity/presence
*/
async function validateCaptcha(req) {
if (!req.body["g-recaptcha-response"]) {
return false;
}
const responseKey = req.body["g-recaptcha-response"];
const url = `https://www.google.com/recaptcha/api/siteverify?secret=${RECAPTCHA_SECRET_KEY}&response=${responseKey}`;
try {
const googleResponse = await (await fetch(url, { method: "post" })).json();
return googleResponse.success == true;
} catch (error) {
return false;
}
}
/**
* This function processes a request object and sends a new spill email.
* @param {*} req the express request object
*/
async function emailSpill(req) {
const date = new Date();
const dateString = date.toLocaleString(TIME_LOCALE, { timeZone: TIMEZONE });
const mailOptions = {
from: EMAIL_USERNAME,
to: "[email protected]",
subject: `New Spill [${dateString}]`,
text: `Date/time received: ${dateString}
IP Address: ${req.ip}
---- Begin Message ----
${req.body["message"]}
---- End Message ----
**Keep confidential**
Sender email: ${req.body["email"]}
`,
};
// Send email
try {
const info = await transporter.sendMail(mailOptions);
console.log("Email sent: ", info.messageId);
} catch (error) {
console.error("Error sending email: ", error);
}
}
const app = express();
// Set up CORS headers
const corsOptions = {
origin: CORS_WHITELIST,
};
app.use(cors(corsOptions));
// Populate body for application/json requests
app.use(express.json());
// Make sure that IP forwarding works correctly if using a reverse proxy
app.set("trust proxy", BEHIND_REVERSE_PROXY);
app.get("/status", async (req, res) => {
res.status(200).send();
return;
});
app.post("/spill", async (req, res) => {
if (!req.body) {
res.status(400).send("No body attached to request.");
return;
}
// Verify captcha
if (!(await validateCaptcha(req))) {
res
.status(400)
.send(
'Please complete the captcha (the "I\'m not a robot" checkbox) and try again.'
);
return;
}
// Verify email is @wisc.edu
if (!req.body["email"]) {
res
.status(400)
.send(
"A wisc.edu email must be specified so that we can respond to your message."
);
return;
}
if (!req.body["email"].toLowerCase().endsWith("@wisc.edu")) {
res
.status(400)
.send(
"Badger Spill can only respond to students with valid wisc.edu emails."
);
return;
}
// Verify that a message is present
if (!req.body["message"]) {
res.status(400).send("No message was included.");
return;
}
// Send email
await emailSpill(req);
res.status(200).send("Your spill has been sent successfully!");
});
// Start server
const server = http.createServer(app);
server.listen(PORT, () => {
console.log(`Badger Spill API started on port ${PORT}.`);
});