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

128
index.js
View file

@ -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,52 +55,90 @@ 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 options = { var socket = ((secureHostname && req.socket.encrypted) ? tls : net).createConnection({
hostname: (secureHostname && req.socket.encrypted) ? secureHostname : hostname, host: (secureHostname && req.socket.encrypted) ? secureHostname : hostname,
port: (secureHostname && req.socket.encrypted) ? securePort : port, port: (secureHostname && req.socket.encrypted) ? securePort : port,
path: req.url, joinDuplicateHeaders: true,
method: req.method, rejectUnauthorized: false
headers: hdrs, }, function () {
joinDuplicateHeaders: true, serverconsole.resmessage("Connected to back-end!");
rejectUnauthorized: false socket.pipe(res.socket);
}; socket.write(req.method + " " + req.url + " HTTP/1.1\r\n");
var proxy = ((secureHostname && req.socket.encrypted) ? https : http).request(options, function(sres) { Object.keys(hdrs).forEach(function (headerName) {
serverconsole.resmessage("Connected to back-end!"); var header = hdrs[headerName];
delete sres.headers["connection"]; if (typeof header === "object") {
delete sres.headers["Connection"]; header.forEach(function (value) {
delete sres.headers["transfer-encoding"]; socket.write(headerName + ": " + value + "\r\n");
delete sres.headers["Transfer-Encoding"]; });
delete sres.headers["keep-alive"]; } else {
delete sres.headers["Keep-Alive"]; socket.write(headerName + ": " + header + "\r\n");
res.writeHead(sres.statusCode, sres.headers); }
sres.pipe(res); });
res.prependListener("end", function() { socket.write("\r\n");
req.socket.pipe(socket);
}).on("error", (ex) => {
try { try {
sres.end(); if (ex.code == "ENOTFOUND" || ex.code == "EHOSTUNREACH" || ex.code == "ECONNREFUSED") {
} catch(ex) {} 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 {
proxy.on("error", (ex) => { if (String(hdrs["connection"]).toLowerCase() != "upgrade") hdrs["connection"] = "close";
try { var options = {
if (ex.code == "ENOTFOUND" || ex.code == "EHOSTUNREACH" || ex.code == "ECONNREFUSED") { hostname: (secureHostname && req.socket.encrypted) ? secureHostname : hostname,
callServerError(503, "reverse-proxy-mod/1.0.4", ex); //Server error port: (secureHostname && req.socket.encrypted) ? securePort : port,
} else if (ex.code == "ETIMEDOUT") { path: req.url,
callServerError(504, "reverse-proxy-mod/1.0.4", ex); //Server error method: req.method,
} else { headers: hdrs,
callServerError(502, "reverse-proxy-mod/1.0.4", ex); //Server error joinDuplicateHeaders: true,
rejectUnauthorized: false
};
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"];
} }
} catch (ex) {} delete sres.headers["transfer-encoding"];
serverconsole.errmessage("Client fails to recieve content."); //Log into SVR.JS delete sres.headers["Transfer-Encoding"];
}); delete sres.headers["keep-alive"];
req.pipe(proxy); delete sres.headers["Keep-Alive"];
req.prependListener("end", function() { res.writeHead(sres.statusCode, sres.headers);
try { sres.pipe(res);
proxy.end(); res.prependListener("end", function () {
} catch(ex) {} try {
}); sres.end();
} catch (ex) {}
});
});
proxy.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
});
req.pipe(proxy);
req.prependListener("end", function () {
try {
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()) { } 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();
} }

View file

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