Add support for HTTP upgrades

This commit is contained in:
Dorian Niemiec 2023-08-26 08:52:01 +02:00
parent f222cb476d
commit 4c9f2edece
2 changed files with 85 additions and 45 deletions

View file

@ -2,6 +2,8 @@ var fs = require("fs");
var os = require("os");
var http = require("http");
var https = require("https");
var net = require("net");
var tls = require("tls");
var proxyConfig = {};
if (fs.existsSync(__dirname + "/../../../reverse-proxy-config.json")) proxyConfig = JSON.parse(fs.readFileSync(__dirname + "/../../../reverse-proxy-config.json").toString());
@ -35,7 +37,7 @@ Mod.prototype.callback = function callback(req, res, serverconsole, responseEnd,
if (!port) port = 80;
if (!securePort && secureHostname) securePort = 443;
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;
}
try {
@ -53,7 +55,42 @@ Mod.prototype.callback = function callback(req, res, serverconsole, responseEnd,
delete hdrs[":authority"];
delete hdrs[":path"];
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 = {
hostname: (secureHostname && req.socket.encrypted) ? secureHostname : hostname,
port: (secureHostname && req.socket.encrypted) ? securePort : port,
@ -65,8 +102,10 @@ Mod.prototype.callback = function callback(req, res, serverconsole, responseEnd,
};
var proxy = ((secureHostname && req.socket.encrypted) ? https : http).request(options, function (sres) {
serverconsole.resmessage("Connected to back-end!");
if (String(hdrs["connection"]).toLowerCase() != "upgrade") {
delete sres.headers["connection"];
delete sres.headers["Connection"];
}
delete sres.headers["transfer-encoding"];
delete sres.headers["Transfer-Encoding"];
delete sres.headers["keep-alive"];
@ -82,11 +121,11 @@ Mod.prototype.callback = function callback(req, res, serverconsole, responseEnd,
proxy.on("error", (ex) => {
try {
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") {
callServerError(504, "reverse-proxy-mod/1.0.4", ex); //Server error
callServerError(504, "reverse-proxy-mod/1.1.0", ex); //Server error
} 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) {}
serverconsole.errmessage("Client fails to recieve content."); //Log into SVR.JS
@ -97,8 +136,9 @@ Mod.prototype.callback = function callback(req, res, serverconsole, responseEnd,
proxy.end();
} catch (ex) {}
});
}
} 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 {
elseCallback();
}

View file

@ -1,4 +1,4 @@
{
"name": "Reverse proxy mod for SVR.JS 3.x",
"version": "1.0.4"
"version": "1.1.0"
}