redirect-https.js/index.js

95 lines
2.8 KiB
JavaScript
Raw Normal View History

2015-07-07 23:19:44 +00:00
'use strict';
module.exports = function (opts) {
var escapeHtml = require('escape-html');
if (!opts) {
opts = {};
}
if (!isFinite(opts.port)) {
2015-07-07 23:19:44 +00:00
opts.port = 443;
}
if (!opts.browsers) {
opts.browsers = 301;
}
if (!opts.apis) {
opts.apis = 'meta';
}
if (!Array.isArray(opts.paths)) {
opts.paths = [ { match: '/' } ];
}
2015-07-07 23:19:44 +00:00
if (!('body' in opts)) {
2018-08-17 02:50:50 +00:00
opts.body = "<!-- Hello Developer Person! We don't serve insecure resources around here."
2015-07-07 23:19:44 +00:00
+ "\n Please use HTTPS instead. -->";
}
2018-02-27 22:42:26 +00:00
opts.body = opts.body.replace(/{{\s+PORT\s+}}/ig, opts.port);
2015-07-07 23:19:44 +00:00
return function (req, res, next) {
if (req.connection.encrypted
|| 'https' === req.protocol
|| (opts.trustProxy && 'https' === req.headers['x-forwarded-proto'])
) {
next();
return;
}
2018-02-27 22:42:26 +00:00
var url = (req.originalUrl || req.url);
// We don't want chrome showing the "Not Secure" badge during the redirect.
var probablyBrowser = (0 === (req.headers['user-agent']||'').indexOf('Mozilla/'));
// But we don't want devs, APIs, or Bots to accidentally browse insecure.
var redirect = probablyBrowser ? opts.browsers : opts.apis;
2015-07-07 23:19:44 +00:00
var host = req.headers.host || '';
2016-11-21 22:45:05 +00:00
if (!/:\d+/.test(host) && 443 !== opts.port) {
// we are using standard port 80, but we aren't using standard port 443
host += ':80';
}
2015-07-07 23:19:44 +00:00
var newLocation = 'https://'
+ host.replace(/:\d+/, ':' + opts.port) + url
;
2018-09-07 19:30:29 +00:00
2015-07-07 23:19:44 +00:00
//var encodedLocation = encodeURI(newLocation);
var escapedLocation = escapeHtml(newLocation);
2018-02-27 22:42:26 +00:00
var decodedLocation;
2018-02-26 19:02:44 +00:00
try {
2018-02-27 22:42:26 +00:00
decodedLocation = decodeURIComponent(newLocation);
2018-02-26 19:02:44 +00:00
} catch(e) {
2018-02-27 22:42:26 +00:00
decodedLocation = newLocation; // "#/error/?error_message=" + e.toString();
2018-02-26 19:02:44 +00:00
}
2018-09-07 19:30:29 +00:00
2015-07-07 23:19:44 +00:00
var body = opts.body
2018-02-27 22:42:26 +00:00
.replace(/{{\s*HTML_URL\s*}}/ig, escapeHtml(decodedLocation))
2015-07-07 23:19:44 +00:00
.replace(/{{\s*URL\s*}}/ig, escapedLocation)
.replace(/{{\s*UNSAFE_URL\s*}}/ig, newLocation)
;
var metaRedirect = ''
+ '<html>'
+ '\n<head><META http-equiv="refresh" content="0;URL=\'' + escapedLocation + '\'"></head>'
+ '\n<body>' + body + '</body>'
+ '\n</html>\n'
2015-07-07 23:19:44 +00:00
;
var pathMatch;
2015-07-07 23:19:44 +00:00
opts.paths.some(function (p) {
if (!p.match) {
// ignore
} else if ('string' === typeof p.match) {
pathMatch = (url === p.match) && (p.redirect || 301);
} else {
pathMatch = p.match.test && p.match.test(url) && (p.redirect || 301);
}
if (pathMatch) {
redirect = pathMatch;
}
return pathMatch;
});
// If it's not a non-0 number (because null is 0) then 'meta' is assumed.
if (redirect && isFinite(redirect)) {
res.statusCode = redirect;
2018-09-07 19:30:29 +00:00
res.setHeader('Location', newLocation);
}
2015-07-07 23:19:44 +00:00
res.setHeader('Content-Type', 'text/html; charset=utf-8');
res.end(metaRedirect);
};
};