Add support for HTTP upgrades
This commit is contained in:
parent
4a21ef9e6e
commit
1098d840dc
1 changed files with 84 additions and 44 deletions
64
index.js
64
index.js
|
@ -2,6 +2,8 @@ var fs = require("fs");
|
||||||
var os = require("os");
|
var os = require("os");
|
||||||
var http = require("http");
|
var http = require("http");
|
||||||
var https = require("https");
|
var https = require("https");
|
||||||
|
var net = require("net");
|
||||||
|
var tls = require("tls");
|
||||||
|
|
||||||
var proxyConfig = {};
|
var proxyConfig = {};
|
||||||
if (fs.existsSync(__dirname + "/../../../reverse-proxy-config.json")) proxyConfig = JSON.parse(fs.readFileSync(__dirname + "/../../../reverse-proxy-config.json").toString());
|
if (fs.existsSync(__dirname + "/../../../reverse-proxy-config.json")) proxyConfig = JSON.parse(fs.readFileSync(__dirname + "/../../../reverse-proxy-config.json").toString());
|
||||||
|
@ -9,7 +11,7 @@ else fs.writeFileSync(__dirname + "/../../../reverse-proxy-config.json", "{}");
|
||||||
|
|
||||||
function Mod() {}
|
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 matchingHostname = null;
|
var matchingHostname = null;
|
||||||
for (var i = 0; i < hostnames.length; i++) {
|
for (var i = 0; i < hostnames.length; i++) {
|
||||||
|
@ -35,7 +37,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.0.4", new Error("Proxy server is misconfigured. Hostname property is missing."));
|
callServerError(500, "reverse-proxy-mod/1.1.0", new Error("Proxy server is misconfigured. Hostname property is missing."));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
|
@ -53,7 +55,42 @@ Mod.prototype.callback = function callback(req, res, serverconsole, responseEnd,
|
||||||
delete hdrs[":authority"];
|
delete hdrs[":authority"];
|
||||||
delete hdrs[":path"];
|
delete hdrs[":path"];
|
||||||
delete hdrs["keep-alive"];
|
delete hdrs["keep-alive"];
|
||||||
hdrs["connection"] = "close";
|
if ((req.httpVersion == "1.1" || req.httpVersion == "1.0") && String(hdrs["connection"]).toLowerCase() == "upgrade") {
|
||||||
|
var socket = ((secureHostname && req.socket.encrypted) ? tls : net).createConnection({
|
||||||
|
host: (secureHostname && req.socket.encrypted) ? secureHostname : hostname,
|
||||||
|
port: (secureHostname && req.socket.encrypted) ? securePort : port,
|
||||||
|
joinDuplicateHeaders: true,
|
||||||
|
rejectUnauthorized: false
|
||||||
|
}, function () {
|
||||||
|
serverconsole.resmessage("Connected to back-end!");
|
||||||
|
socket.pipe(res.socket);
|
||||||
|
socket.write(req.method + " " + req.url + " HTTP/1.1\r\n");
|
||||||
|
Object.keys(hdrs).forEach(function (headerName) {
|
||||||
|
var header = hdrs[headerName];
|
||||||
|
if (typeof header === "object") {
|
||||||
|
header.forEach(function (value) {
|
||||||
|
socket.write(headerName + ": " + value + "\r\n");
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
socket.write(headerName + ": " + header + "\r\n");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
socket.write("\r\n");
|
||||||
|
req.socket.pipe(socket);
|
||||||
|
}).on("error", (ex) => {
|
||||||
|
try {
|
||||||
|
if (ex.code == "ENOTFOUND" || ex.code == "EHOSTUNREACH" || ex.code == "ECONNREFUSED") {
|
||||||
|
callServerError(503, "reverse-proxy-mod/1.1.0", ex); //Server error
|
||||||
|
} else if (ex.code == "ETIMEDOUT") {
|
||||||
|
callServerError(504, "reverse-proxy-mod/1.1.0", ex); //Server error
|
||||||
|
} else {
|
||||||
|
callServerError(502, "reverse-proxy-mod/1.1.0", ex); //Server error
|
||||||
|
}
|
||||||
|
} catch (ex) {}
|
||||||
|
serverconsole.errmessage("Client fails to recieve content."); //Log into SVR.JS
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
if (String(hdrs["connection"]).toLowerCase() != "upgrade") hdrs["connection"] = "close";
|
||||||
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,
|
||||||
|
@ -63,42 +100,45 @@ Mod.prototype.callback = function callback(req, res, serverconsole, responseEnd,
|
||||||
joinDuplicateHeaders: true,
|
joinDuplicateHeaders: true,
|
||||||
rejectUnauthorized: false
|
rejectUnauthorized: false
|
||||||
};
|
};
|
||||||
var proxy = ((secureHostname && req.socket.encrypted) ? https : http).request(options, function(sres) {
|
var proxy = ((secureHostname && req.socket.encrypted) ? https : http).request(options, function (sres) {
|
||||||
serverconsole.resmessage("Connected to back-end!");
|
serverconsole.resmessage("Connected to back-end!");
|
||||||
|
if (String(hdrs["connection"]).toLowerCase() != "upgrade") {
|
||||||
delete sres.headers["connection"];
|
delete sres.headers["connection"];
|
||||||
delete sres.headers["Connection"];
|
delete sres.headers["Connection"];
|
||||||
|
}
|
||||||
delete sres.headers["transfer-encoding"];
|
delete sres.headers["transfer-encoding"];
|
||||||
delete sres.headers["Transfer-Encoding"];
|
delete sres.headers["Transfer-Encoding"];
|
||||||
delete sres.headers["keep-alive"];
|
delete sres.headers["keep-alive"];
|
||||||
delete sres.headers["Keep-Alive"];
|
delete sres.headers["Keep-Alive"];
|
||||||
res.writeHead(sres.statusCode, sres.headers);
|
res.writeHead(sres.statusCode, sres.headers);
|
||||||
sres.pipe(res);
|
sres.pipe(res);
|
||||||
res.prependListener("end", function() {
|
res.prependListener("end", function () {
|
||||||
try {
|
try {
|
||||||
sres.end();
|
sres.end();
|
||||||
} catch(ex) {}
|
} catch (ex) {}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
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.0.4", ex); //Server error
|
callServerError(503, "reverse-proxy-mod/1.1.0", ex); //Server error
|
||||||
} else if (ex.code == "ETIMEDOUT") {
|
} else if (ex.code == "ETIMEDOUT") {
|
||||||
callServerError(504, "reverse-proxy-mod/1.0.4", ex); //Server error
|
callServerError(504, "reverse-proxy-mod/1.1.0", ex); //Server error
|
||||||
} else {
|
} else {
|
||||||
callServerError(502, "reverse-proxy-mod/1.0.4", ex); //Server error
|
callServerError(502, "reverse-proxy-mod/1.1.0", 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
|
||||||
});
|
});
|
||||||
req.pipe(proxy);
|
req.pipe(proxy);
|
||||||
req.prependListener("end", function() {
|
req.prependListener("end", function () {
|
||||||
try {
|
try {
|
||||||
proxy.end();
|
proxy.end();
|
||||||
} catch(ex) {}
|
} catch (ex) {}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
} 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.02");
|
callServerError(403, "reverse-proxy-mod/1.1.0");
|
||||||
} else {
|
} else {
|
||||||
elseCallback();
|
elseCallback();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue