CinemaPress/lib/CP_nginx.js

88 lines
2.5 KiB
JavaScript
Raw Normal View History

2019-10-01 21:34:02 +02:00
'use strict';
/**
* Configuration dependencies.
*/
var config = require('../config/production/config');
2020-09-16 03:47:49 +02:00
Object.keys(config).length === 0 &&
(config = require('../config/production/config.backup'));
2019-10-01 21:34:02 +02:00
/**
* Node dependencies.
*/
var path = require('path');
module.exports = function() {
return function(req, res, next) {
2020-01-16 05:03:19 +01:00
var originalUrl = '';
try {
originalUrl = decodeURIComponent(req.originalUrl);
} catch (e) {}
var redirect =
originalUrl &&
config.redirect &&
2021-01-24 22:57:57 +01:00
config.redirect.from &&
2020-01-16 05:03:19 +01:00
config.redirect.from.length === config.redirect.to.length
? config.redirect.from.indexOf(originalUrl)
: -1;
2021-01-24 22:57:57 +01:00
var redirect_to = redirect + 1 ? config.redirect.to[redirect] : '';
if (
!redirect_to &&
config.redirect &&
config.redirect.from &&
config.redirect.from.length
) {
config.redirect.from.forEach(function(url, i) {
if (redirect_to || url.indexOf('*') === -1 || !config.redirect.to[i])
return;
var url_regex = new RegExp('^' + url + '$', 'i');
if (url_regex.test(originalUrl)) {
redirect_to = originalUrl.replace(url_regex, config.redirect.to[i]);
}
});
}
2019-10-01 21:34:02 +02:00
var admin = /^\/admin$/i.test(req.originalUrl);
var balancer = /\/balancer\/[0-9]{1,10}\.mp4/i.test(req.originalUrl);
2020-03-22 21:45:44 +01:00
var forbidden = /^\/(tv-version\/|mobile-version\/|)(doc|Dockerfile|config|modules|node_modules|lib|log|routes|core|app\.js|optimal\.js|package\.json|package-lock\.json|process\.json|restart\.server|LICENSE\.txt|README\.md|.*\.sh|.*\.conf|.*\.log|.*\.jade|.*\.ejs|\.git)($|\/)/i.test(
2019-10-01 21:34:02 +02:00
req.originalUrl
);
if (admin) {
return res.render('secret', {
search: config.urls.search,
status: 401,
message: /^admin-secret$/i.test(config.urls.admin) ? 'secret' : '',
language: config.language
});
} else if (forbidden) {
return res.status(404).render('error', {
search: config.urls.search,
status: 404,
message: config.l.notFound,
language: config.language
});
} else if (balancer) {
return res.sendFile(
path.join(
path.dirname(__filename),
'..',
'..',
'..',
'var',
'local',
'balancer',
'bbb.mp4'
)
);
2021-01-24 22:57:57 +01:00
} else if (redirect_to) {
2020-01-16 05:03:19 +01:00
return next({
status: 301,
2021-01-24 22:57:57 +01:00
message: redirect_to
2020-01-16 05:03:19 +01:00
});
2019-10-01 21:34:02 +02:00
} else {
return next();
}
};
};