2017-08-24 21:54:02 +00:00
|
|
|
const busboy = require('connect-busboy');
|
|
|
|
const helmet = require('helmet');
|
|
|
|
const bodyParser = require('body-parser');
|
|
|
|
const languages = require('../languages');
|
|
|
|
const storage = require('../storage');
|
|
|
|
const config = require('../config');
|
|
|
|
const pages = require('./pages');
|
2017-11-10 20:40:18 +00:00
|
|
|
const { negotiateLanguages } = require('fluent-langneg');
|
2017-08-29 18:19:21 +00:00
|
|
|
const IS_DEV = config.env === 'development';
|
2017-11-10 20:40:18 +00:00
|
|
|
const acceptLanguages = /(([a-zA-Z]+(-[a-zA-Z0-9]+){0,2})|\*)(;q=[0-1](\.[0-9]+)?)?/g;
|
|
|
|
const langData = require('cldr-core/supplemental/likelySubtags.json');
|
2018-02-06 01:21:32 +00:00
|
|
|
const idregx = '([0-9a-fA-F]{10})';
|
2017-08-24 21:54:02 +00:00
|
|
|
|
|
|
|
module.exports = function(app) {
|
2017-11-10 20:40:18 +00:00
|
|
|
app.use(function(req, res, next) {
|
|
|
|
const header = req.headers['accept-language'] || 'en-US';
|
|
|
|
if (header.length > 255) {
|
|
|
|
req.language = 'en-US';
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
const langs = header.replace(/\s/g, '').match(acceptLanguages);
|
|
|
|
const preferred = langs
|
|
|
|
.map(l => {
|
|
|
|
const parts = l.split(';');
|
|
|
|
return {
|
|
|
|
locale: parts[0],
|
|
|
|
q: parts[1] ? parseFloat(parts[1].split('=')[1]) : 1
|
|
|
|
};
|
|
|
|
})
|
|
|
|
.sort((a, b) => b.q - a.q)
|
|
|
|
.map(x => x.locale);
|
|
|
|
req.language = negotiateLanguages(preferred, languages, {
|
|
|
|
strategy: 'lookup',
|
|
|
|
likelySubtags: langData.supplemental.likelySubtags,
|
|
|
|
defaultLocale: 'en-US'
|
|
|
|
})[0];
|
|
|
|
next();
|
|
|
|
});
|
2017-08-24 21:54:02 +00:00
|
|
|
app.use(helmet());
|
|
|
|
app.use(
|
|
|
|
helmet.hsts({
|
|
|
|
maxAge: 31536000,
|
2017-08-29 18:19:21 +00:00
|
|
|
force: !IS_DEV
|
2017-08-24 21:54:02 +00:00
|
|
|
})
|
|
|
|
);
|
2017-11-30 21:41:09 +00:00
|
|
|
if (!IS_DEV) {
|
|
|
|
app.use(
|
|
|
|
helmet.contentSecurityPolicy({
|
|
|
|
directives: {
|
|
|
|
defaultSrc: ["'self'"],
|
|
|
|
connectSrc: [
|
|
|
|
"'self'",
|
|
|
|
'https://sentry.prod.mozaws.net',
|
|
|
|
'https://www.google-analytics.com'
|
|
|
|
],
|
|
|
|
imgSrc: ["'self'", 'https://www.google-analytics.com'],
|
|
|
|
scriptSrc: ["'self'"],
|
|
|
|
styleSrc: [
|
|
|
|
"'self'",
|
|
|
|
"'unsafe-inline'",
|
|
|
|
'https://code.cdn.mozilla.net'
|
|
|
|
],
|
|
|
|
fontSrc: ["'self'", 'https://code.cdn.mozilla.net'],
|
|
|
|
formAction: ["'none'"],
|
|
|
|
frameAncestors: ["'none'"],
|
|
|
|
objectSrc: ["'none'"],
|
|
|
|
reportUri: '/__cspreport__'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
2017-08-24 21:54:02 +00:00
|
|
|
app.use(
|
|
|
|
busboy({
|
|
|
|
limits: {
|
|
|
|
fileSize: config.max_file_size
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
2017-11-06 21:36:36 +00:00
|
|
|
app.use(function(req, res, next) {
|
|
|
|
res.set('Pragma', 'no-cache');
|
|
|
|
res.set('Cache-Control', 'no-cache');
|
|
|
|
next();
|
|
|
|
});
|
2017-08-24 21:54:02 +00:00
|
|
|
app.use(bodyParser.json());
|
|
|
|
app.get('/', pages.index);
|
|
|
|
app.get('/legal', pages.legal);
|
|
|
|
app.get('/jsconfig.js', require('./jsconfig'));
|
2018-02-06 01:21:32 +00:00
|
|
|
app.get(`/share/:id${idregx}`, pages.blank);
|
|
|
|
app.get(`/download/:id${idregx}`, pages.download);
|
2017-08-24 21:54:02 +00:00
|
|
|
app.get('/completed', pages.blank);
|
|
|
|
app.get('/unsupported/:reason', pages.unsupported);
|
2018-02-06 01:21:32 +00:00
|
|
|
app.get(`/api/download/:id${idregx}`, require('./download'));
|
|
|
|
app.get(`/api/exists/:id${idregx}`, require('./exists'));
|
|
|
|
app.get(`/api/metadata/:id${idregx}`, require('./metadata'));
|
2017-08-31 16:43:36 +00:00
|
|
|
app.post('/api/upload', require('./upload'));
|
2018-02-06 01:21:32 +00:00
|
|
|
app.post(`/api/delete/:id${idregx}`, require('./delete'));
|
|
|
|
app.post(`/api/password/:id${idregx}`, require('./password'));
|
|
|
|
app.post(`/api/params/:id${idregx}`, require('./params'));
|
|
|
|
app.post(`/api/info/:id${idregx}`, require('./info'));
|
2017-08-24 21:54:02 +00:00
|
|
|
|
|
|
|
app.get('/__version__', function(req, res) {
|
|
|
|
res.sendFile(require.resolve('../../dist/version.json'));
|
|
|
|
});
|
|
|
|
|
|
|
|
app.get('/__lbheartbeat__', function(req, res) {
|
|
|
|
res.sendStatus(200);
|
|
|
|
});
|
|
|
|
|
2017-08-25 17:03:49 +00:00
|
|
|
app.get('/__heartbeat__', async (req, res) => {
|
2017-08-24 21:54:02 +00:00
|
|
|
try {
|
|
|
|
await storage.ping();
|
|
|
|
res.sendStatus(200);
|
|
|
|
} catch (e) {
|
|
|
|
res.sendStatus(500);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|