forked from nates/ward
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
66 lines (57 loc) · 2.3 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
// Packages
const express = require('express');
const path = require('path');
const axios = require('axios');
const https = require('https');
const fs = require('fs');
const { Signale } = require('signale');
const pool = require('./pool');
const discord = require('./discord');
// Config
const config = require('./config.json');
// Variables
const logger = new Signale({ scope: 'Express' });
const app = express();
const port = config.https ? 443 : 80;
// Define render engine and assets path
app.engine('html', require('ejs').renderFile);
app.use(express.static(path.join(__dirname, '/assets')));
app.use(express.json());
app.use(express.urlencoded({
extended: true
}));
// GET /verify/id
app.get('/verify/:verifyId?', (req, res) => {
if (!req.params.verifyId) return res.sendFile(path.join(__dirname, '/html/invalidLink.html'));
if (!pool.isValidLink(req.params.verifyId)) return res.sendFile(path.join(__dirname, '/html/invalidLink.html'));
res.render(path.join(__dirname, '/html/verify.html'), { publicKey: config.recaptcha['public-key'] });
});
// POST /verify/id
app.post('/verify/:verifyId?', async (req, res) => {
if (!req.body || !req.body['g-recaptcha-response']) return res.sendFile(path.join(__dirname, '/html/invalidLink.html'));
const response = await axios({
method: 'post',
url: `https://www.google.com/recaptcha/api/siteverify?secret=${config.recaptcha['secret-key']}&response=${req.body['g-recaptcha-response']}`,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
});
if (!response.data.success) return res.sendFile(path.join(__dirname, '/html/invalidCaptcha.html'));
if (!pool.isValidLink(req.params.verifyId)) return res.sendFile(path.join(__dirname, '/html/invalidLink.html'));
discord.addRole(pool.getDiscordId(req.params.verifyId));
pool.removeLink(req.params.verifyId);
res.sendFile(path.join(__dirname, '/html/valid.html'));
});
function main() {
if (config.https) {
https.createServer({
key: fs.readFileSync('private.pem'),
cert: fs.readFileSync('certificate.pem')
}, app).listen(port, () => logger.info(`Listening on port ${port}.`));
} else {
app.listen(port, () => logger.info(`Listening on port ${port}.`));
}
}
module.exports = {
run: main
};