Add support for reverse proxy paths
This commit is contained in:
parent
4c9f2edece
commit
6b24b28c3c
2 changed files with 25 additions and 11 deletions
34
index.js
34
index.js
|
@ -13,11 +13,16 @@ function Mod() {}
|
||||||
Mod.prototype.callback = function callback(req, res, serverconsole, responseEnd, href, ext, uobject, search, defaultpage, users, page404, head, foot, fd, elseCallback, configJSON, callServerError, getCustomHeaders, origHref, redirect, parsePostData) {
|
Mod.prototype.callback = function callback(req, res, serverconsole, responseEnd, href, ext, uobject, search, defaultpage, users, page404, head, foot, fd, elseCallback, configJSON, callServerError, getCustomHeaders, origHref, redirect, parsePostData) {
|
||||||
return function () {
|
return function () {
|
||||||
var hostnames = Object.keys(proxyConfig);
|
var hostnames = Object.keys(proxyConfig);
|
||||||
|
var isPath = false;
|
||||||
var matchingHostname = null;
|
var matchingHostname = null;
|
||||||
for (var i = 0; i < hostnames.length; i++) {
|
for (var i = 0; i < hostnames.length; i++) {
|
||||||
if (hostnames[i] == "*") {
|
if (hostnames[i] == "*") {
|
||||||
matchingHostname = "*";
|
matchingHostname = "*";
|
||||||
break;
|
break;
|
||||||
|
} else if (hostnames[i][0] == "/" && (href == hostnames[i] || href.indexOf(hostnames[i] + "/") == 0)) {
|
||||||
|
matchingHostname = hostnames[i];
|
||||||
|
isPath = true;
|
||||||
|
break;
|
||||||
} else if (req.headers.host && hostnames[i].indexOf("*.") == 0 && hostnames[i] != "*.") {
|
} else if (req.headers.host && hostnames[i].indexOf("*.") == 0 && hostnames[i] != "*.") {
|
||||||
var hostnamesRoot = hostnames[i].substr(2);
|
var hostnamesRoot = hostnames[i].substr(2);
|
||||||
if (req.headers.host == hostnamesRoot || req.headers.host.indexOf("." + hostnamesRoot) == req.headers.host.length - hostnamesRoot.length - 1) {
|
if (req.headers.host == hostnamesRoot || req.headers.host.indexOf("." + hostnamesRoot) == req.headers.host.length - hostnamesRoot.length - 1) {
|
||||||
|
@ -37,7 +42,7 @@ Mod.prototype.callback = function callback(req, res, serverconsole, responseEnd,
|
||||||
if (!port) port = 80;
|
if (!port) port = 80;
|
||||||
if (!securePort && secureHostname) securePort = 443;
|
if (!securePort && secureHostname) securePort = 443;
|
||||||
if (!hostname) {
|
if (!hostname) {
|
||||||
callServerError(500, "reverse-proxy-mod/1.1.0", new Error("Proxy server is misconfigured. Hostname property is missing."));
|
callServerError(500, "reverse-proxy-mod/1.1.1", new Error("Proxy server is misconfigured. Hostname property is missing."));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
|
@ -46,6 +51,15 @@ Mod.prototype.callback = function callback(req, res, serverconsole, responseEnd,
|
||||||
res.removeHeader(x[i]);
|
res.removeHeader(x[i]);
|
||||||
}
|
}
|
||||||
} catch (ex) {}
|
} catch (ex) {}
|
||||||
|
var preparedPath = req.url;
|
||||||
|
if(isPath) {
|
||||||
|
if(preparedPath == matchingHostname) {
|
||||||
|
preparedPath = "/";
|
||||||
|
} else {
|
||||||
|
preparedPath = preparedPath.replace(matchingHostname + "/","");
|
||||||
|
if(preparedPath == "") preparedPath = "/";
|
||||||
|
}
|
||||||
|
}
|
||||||
var hdrs = JSON.parse(JSON.stringify(req.headers));
|
var hdrs = JSON.parse(JSON.stringify(req.headers));
|
||||||
hdrs["x-forwarded-for"] = req.socket.remoteAddress;
|
hdrs["x-forwarded-for"] = req.socket.remoteAddress;
|
||||||
hdrs["x-forwarded-proto"] = req.socket.encrypted ? "https" : "http";
|
hdrs["x-forwarded-proto"] = req.socket.encrypted ? "https" : "http";
|
||||||
|
@ -64,7 +78,7 @@ Mod.prototype.callback = function callback(req, res, serverconsole, responseEnd,
|
||||||
}, function () {
|
}, function () {
|
||||||
serverconsole.resmessage("Connected to back-end!");
|
serverconsole.resmessage("Connected to back-end!");
|
||||||
socket.pipe(res.socket);
|
socket.pipe(res.socket);
|
||||||
socket.write(req.method + " " + req.url + " HTTP/1.1\r\n");
|
socket.write(req.method + " " + preparedPath + " HTTP/1.1\r\n");
|
||||||
Object.keys(hdrs).forEach(function (headerName) {
|
Object.keys(hdrs).forEach(function (headerName) {
|
||||||
var header = hdrs[headerName];
|
var header = hdrs[headerName];
|
||||||
if (typeof header === "object") {
|
if (typeof header === "object") {
|
||||||
|
@ -80,11 +94,11 @@ Mod.prototype.callback = function callback(req, res, serverconsole, responseEnd,
|
||||||
}).on("error", (ex) => {
|
}).on("error", (ex) => {
|
||||||
try {
|
try {
|
||||||
if (ex.code == "ENOTFOUND" || ex.code == "EHOSTUNREACH" || ex.code == "ECONNREFUSED") {
|
if (ex.code == "ENOTFOUND" || ex.code == "EHOSTUNREACH" || ex.code == "ECONNREFUSED") {
|
||||||
callServerError(503, "reverse-proxy-mod/1.1.0", ex); //Server error
|
callServerError(503, "reverse-proxy-mod/1.1.1", ex); //Server error
|
||||||
} else if (ex.code == "ETIMEDOUT") {
|
} else if (ex.code == "ETIMEDOUT") {
|
||||||
callServerError(504, "reverse-proxy-mod/1.1.0", ex); //Server error
|
callServerError(504, "reverse-proxy-mod/1.1.1", ex); //Server error
|
||||||
} else {
|
} else {
|
||||||
callServerError(502, "reverse-proxy-mod/1.1.0", ex); //Server error
|
callServerError(502, "reverse-proxy-mod/1.1.1", ex); //Server error
|
||||||
}
|
}
|
||||||
} catch (ex) {}
|
} catch (ex) {}
|
||||||
serverconsole.errmessage("Client fails to recieve content."); //Log into SVR.JS
|
serverconsole.errmessage("Client fails to recieve content."); //Log into SVR.JS
|
||||||
|
@ -94,7 +108,7 @@ Mod.prototype.callback = function callback(req, res, serverconsole, responseEnd,
|
||||||
var options = {
|
var options = {
|
||||||
hostname: (secureHostname && req.socket.encrypted) ? secureHostname : hostname,
|
hostname: (secureHostname && req.socket.encrypted) ? secureHostname : hostname,
|
||||||
port: (secureHostname && req.socket.encrypted) ? securePort : port,
|
port: (secureHostname && req.socket.encrypted) ? securePort : port,
|
||||||
path: req.url,
|
path: preparedPath,
|
||||||
method: req.method,
|
method: req.method,
|
||||||
headers: hdrs,
|
headers: hdrs,
|
||||||
joinDuplicateHeaders: true,
|
joinDuplicateHeaders: true,
|
||||||
|
@ -121,11 +135,11 @@ Mod.prototype.callback = function callback(req, res, serverconsole, responseEnd,
|
||||||
proxy.on("error", (ex) => {
|
proxy.on("error", (ex) => {
|
||||||
try {
|
try {
|
||||||
if (ex.code == "ENOTFOUND" || ex.code == "EHOSTUNREACH" || ex.code == "ECONNREFUSED") {
|
if (ex.code == "ENOTFOUND" || ex.code == "EHOSTUNREACH" || ex.code == "ECONNREFUSED") {
|
||||||
callServerError(503, "reverse-proxy-mod/1.1.0", ex); //Server error
|
callServerError(503, "reverse-proxy-mod/1.1.1", ex); //Server error
|
||||||
} else if (ex.code == "ETIMEDOUT") {
|
} else if (ex.code == "ETIMEDOUT") {
|
||||||
callServerError(504, "reverse-proxy-mod/1.1.0", ex); //Server error
|
callServerError(504, "reverse-proxy-mod/1.1.1", ex); //Server error
|
||||||
} else {
|
} else {
|
||||||
callServerError(502, "reverse-proxy-mod/1.1.0", ex); //Server error
|
callServerError(502, "reverse-proxy-mod/1.1.1", ex); //Server error
|
||||||
}
|
}
|
||||||
} catch (ex) {}
|
} catch (ex) {}
|
||||||
serverconsole.errmessage("Client fails to recieve content."); //Log into SVR.JS
|
serverconsole.errmessage("Client fails to recieve content."); //Log into SVR.JS
|
||||||
|
@ -138,7 +152,7 @@ Mod.prototype.callback = function callback(req, res, serverconsole, responseEnd,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} else if ((href == "/reverse-proxy-config.json" || (os.platform() == "win32" && href.toLowerCase() == "/reverse-proxy-config.json")) && path.normalize(__dirname + "/../../..") == process.cwd()) {
|
} else if ((href == "/reverse-proxy-config.json" || (os.platform() == "win32" && href.toLowerCase() == "/reverse-proxy-config.json")) && path.normalize(__dirname + "/../../..") == process.cwd()) {
|
||||||
callServerError(403, "reverse-proxy-mod/1.1.0");
|
callServerError(403, "reverse-proxy-mod/1.1.1");
|
||||||
} else {
|
} else {
|
||||||
elseCallback();
|
elseCallback();
|
||||||
}
|
}
|
||||||
|
|
2
mod.info
2
mod.info
|
@ -1,4 +1,4 @@
|
||||||
{
|
{
|
||||||
"name": "Reverse proxy mod for SVR.JS 3.x",
|
"name": "Reverse proxy mod for SVR.JS 3.x",
|
||||||
"version": "1.1.0"
|
"version": "1.1.1"
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue