-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
93 lines (75 loc) · 2.82 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
require('dotenv').config({ silent: true });
const path = require('path');
const { promisify } = require('util');
const fs = require('fs');
const express = require('express');
const enforce = require('express-sslify');
const compression = require('compression');
const historyApiFallback = require('connect-history-api-fallback');
const isBot = require('isbot-fast');
const blockNetworks = require('./block_networks.json');
const { IpFilter } = require('express-ipfilter');
const basicAuth = require('./auth');
const getOpengraphTags = require('./opengraph')
const readFileAsync = promisify(fs.readFile);
const ipFilter = (req, res, next) => {
// custom next function to handle errors
const _next = (error) => {
if (!error) return next();
res.status(error.status);
return next(error.message);
}
const detectIp = function (req) {
let { connection: { remoteAddress } } = req;
let ip = remoteAddress;
// fix for IPv6 Dotted-quad notation
if (remoteAddress.includes(':') && remoteAddress.includes('.')) ip = remoteAddress.split(':').pop();
return ip;
};
return IpFilter(blockNetworks, { logLevel: 'deny', mode: 'deny', detectIp })(req, res, _next);
};
const app = express();
app.use(ipFilter);
if (process.env.AUTH_PASSWORD) app.use(basicAuth);
app.use(compression());
app.use(historyApiFallback());
app.use(enforce.HTTPS({ trustProtoHeader: true }));
// https-only, nosniff, no iframing, no xss
app.use((req, res, next) => {
res.append(
'Strict-Transport-Security',
'max-age=63072000; includeSubDomains; preload',
);
res.append('X-Content-Type-Options', 'nosniff');
res.append('X-Frame-Options', 'DENY');
res.append('X-XSS-Protection', '1; mode=block');
next();
});
// don't cache service-worker
app.get('/service-worker.js', async (req, res, next) => {
res.header('Cache-Control', 'no-store, no-cache, must-revalidate');
return next();
});
// everything goes to index.html
app.get('/index.html', async (req, res) => {
const indexPath = path.join(__dirname, 'build', 'index.html');
let index = (await readFileAsync(indexPath)).toString('utf8');
// if this is a bot, replace og-tags before response
if (isBot(req.get('User-Agent'))) {
const tags = await getOpengraphTags(req.originalUrl);
const meta = Object.keys(tags).map((k) => {
if (k === 'twitter:card' || k === 'twitter:site') {
return `<meta name="${k}" value="${tags[k]}" />`;
}
return `<meta property="${k}" value="${tags[k]}" />`;
});
index = index.replace('<meta name="opengraph"/>', meta.join('\n'));
}
return res.send(index);
});
app.use(express.static(path.join(__dirname, 'build')));
const server = app.listen(process.env.PORT || 3000, () => {
const host = server.address().address;
const port = server.address().port;
console.info('Serving on http://%s:%s', host, port);
});