forked from svrjs/svrjs
Completely rewrite HTTP to HTTPS redirect functionality
This commit is contained in:
parent
067d177321
commit
c20aa3d7bc
1 changed files with 12 additions and 57 deletions
69
svr.js
69
svr.js
|
@ -4219,67 +4219,22 @@ if (!cluster.isPrimary) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var urlp = parseURL("http://" + hostx);
|
var isPublicServer = !(req.socket.realRemoteAddress ? req.socket.realRemoteAddress : req.socket.remoteAddress).match(/^(?:localhost$|::1$|f[c-d][0-9a-f]{2}:|(?:::ffff:)?(?:(?:127|10)\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}|192\.168\.[0-9]{1,3}\.[0-9]{1,3}|172\.(?:1[6-9]|2[0-9]|3[0-1])\.[0-9]{1,3}\.[0-9]{1,3})$)/i);
|
||||||
try {
|
|
||||||
if (urlp.path.indexOf("//") == 0) {
|
|
||||||
urlp = parseURL("http:" + url.path);
|
|
||||||
}
|
|
||||||
} catch (err) {
|
|
||||||
// URL parse error...
|
|
||||||
}
|
|
||||||
|
|
||||||
if (urlp.host == "localhost" || urlp.host == "localhost:" + port.toString() || urlp.host == "127.0.0.1" || urlp.host == "127.0.0.1:" + port.toString() || urlp.host == "::1" || urlp.host == "::1:" + port.toString()) {
|
var destinationPort = 0;
|
||||||
urlp.protocol = "https:";
|
|
||||||
if (sport == 443) {
|
var parsedHostx = hostx.match(/(\[[^\]]*\]|[^:]*)(?::([0-9]+))?/);
|
||||||
urlp.host = urlp.hostname;
|
var hostname = parsedHostx[1];
|
||||||
} else {
|
var hostPort = parsedHostx[2] ? parseInt(parsedHostx[2]) : 80;
|
||||||
urlp.host = urlp.hostname + ":" + sport.toString();
|
if (isNaN(hostPort)) hostPort = 80;
|
||||||
urlp.port = sport.toString();
|
|
||||||
}
|
if (hostPort == port || (port == pubport && !isPublicServer)) {
|
||||||
} else if (urlp.host == (listenAddress ? listenAddress : host) || urlp.host == (listenAddress ? listenAddress : host) + ":" + port.toString()) {
|
destinationPort = sport;
|
||||||
urlp.protocol = "https:";
|
|
||||||
if (sport == 443) {
|
|
||||||
urlp.host = urlp.hostname;
|
|
||||||
} else {
|
|
||||||
urlp.host = urlp.hostname + ":" + sport.toString();
|
|
||||||
urlp.port = sport.toString();
|
|
||||||
}
|
|
||||||
} else if (urlp.host == pubip || urlp.host == pubip + ":" + pubport.toString()) {
|
|
||||||
urlp.protocol = "https:";
|
|
||||||
if (spubport == 443) {
|
|
||||||
urlp.host = urlp.hostname;
|
|
||||||
} else {
|
|
||||||
urlp.host = urlp.hostname + ":" + spubport.toString();
|
|
||||||
urlp.port = spubport.toString();
|
|
||||||
}
|
|
||||||
} else if (urlp.hostname == domain || urlp.hostname.indexOf(domain) != -1) {
|
|
||||||
urlp.protocol = "https:";
|
|
||||||
if (spubport == 443) {
|
|
||||||
urlp.host = urlp.hostname;
|
|
||||||
} else {
|
|
||||||
urlp.host = urlp.hostname + ":" + spubport.toString();
|
|
||||||
urlp.port = spubport.toString();
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
urlp.protocol = "https:";
|
destinationPort = spubport;
|
||||||
}
|
|
||||||
urlp.path = null;
|
|
||||||
urlp.pathname = null;
|
|
||||||
var lloc = url.format(urlp);
|
|
||||||
var requestURL = req.url;
|
|
||||||
try {
|
|
||||||
if (requestURL.split("/")[1].indexOf(".onion") != -1) {
|
|
||||||
requestURL = requestURL.split("/");
|
|
||||||
requestURL.shift();
|
|
||||||
requestURL.shift();
|
|
||||||
requestURL.unshift("");
|
|
||||||
requestURL = requestURL.join("/");
|
|
||||||
}
|
|
||||||
} catch (err) {
|
|
||||||
// Leave URL as it is...
|
|
||||||
}
|
}
|
||||||
|
|
||||||
redirect(lloc + requestURL);
|
redirect("https://" + hostname + (destinationPort == 443 ? "" : (":" + destinationPort)) + req.url);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in a new issue