diff --git a/lib/configproxy.js b/lib/configproxy.js index c88fe9a..46652a5 100644 --- a/lib/configproxy.js +++ b/lib/configproxy.js @@ -388,6 +388,34 @@ export class ConfigurableProxy extends EventEmitter { }); } + proxyOptsForTarget(target, reqUrl) { + var proxyOptions = { target }; + + if (target.protocol.startsWith("unix")) { + proxyOptions.secure = false; + proxyOptions.target.socketPath = decodeURIComponent(target.host); + proxyOptions.target.pathname = (target.pathname ? target.pathname + "/" : "") + reqUrl; + } else { + // No need for agents for unix sockets + // No support for https for unix sockets + proxyOptions.secure = target.protocol.slice(-2) === "s:"; + + if (proxyOptions.secure) { + proxyOptions.agent = this.httpsAgent; + } else { + proxyOptions.agent = this.httpAgent; + } + } + + if (proxyOptions.secure && this.options.clientSsl) { + proxyOptions.target.key = this.options.clientSsl.key; + proxyOptions.target.cert = this.options.clientSsl.cert; + proxyOptions.target.ca = this.options.clientSsl.ca; + } + + return proxyOptions; + } + async targetForReq(req) { var metricsTimerEnd = this.metrics.findTargetForReqSummary.startTimer(); // return proxy target for a given url path @@ -463,23 +491,13 @@ export class ConfigurableProxy extends EventEmitter { // error request is $errorTarget/$code?url=$requestUrl urlSpec.searchParams.set("url", req.url); urlSpec.pathname = urlSpec.pathname + code.toString(); - var secure = /https/gi.test(urlSpec.protocol) ? true : false; var url = urlSpec.toString(); this.log.debug("Requesting custom error page: %s", url); - // construct request options - var options = { - method: "GET", - }; - - // add client SSL config if error target is using https - if (secure && this.options.clientSsl) { - options.key = this.options.clientSsl.key; - options.cert = this.options.clientSsl.cert; - options.ca = this.options.clientSsl.ca; - } + var options = this.proxyOptsForTarget(urlSpec, req.url); + options.method = "GET"; - var errorRequest = (secure ? https : http).request(url, options, function (upstream) { + var errorRequest = (options.secure ? https : http).request(url, options, function (upstream) { if (res.writableEnded) return; // response already done ["content-type", "content-encoding"].map(function (key) { if (!upstream.headers[key]) return; @@ -557,19 +575,8 @@ export class ConfigurableProxy extends EventEmitter { } target = new URL(target); - var proxyOptions = { target }; - if (that.options.clientSsl) { - target.key = that.options.clientSsl.key; - target.cert = that.options.clientSsl.cert; - target.ca = that.options.clientSsl.ca; - } + var proxyOptions = this.proxyOptsForTarget(target, req.url); - // add config argument - if (target.protocol.slice(-2) === "s:") { - proxyOptions.agent = that.httpsAgent; - } else { - proxyOptions.agent = that.httpAgent; - } args.push(proxyOptions); // add error handling diff --git a/lib/testutil.js b/lib/testutil.js index fac9e68..4216879 100644 --- a/lib/testutil.js +++ b/lib/testutil.js @@ -9,12 +9,22 @@ import { defaultLogger } from "./log.js"; var servers = []; // TODO: make this an options dict -export function addTarget(proxy, path, port, websocket, targetPath, sslOptions) { +export function addTarget(proxy, path, port, websocket, targetPath, sslOptions, unixSocketPath) { var proto = sslOptions ? "https" : "http"; - var target = proto + "://127.0.0.1:" + port; + var listenTarget; + var target; + + if (unixSocketPath) { + listenTarget = decodeURIComponent(unixSocketPath); + target = "unix+" + proto + "://" + unixSocketPath; + } else { + target = proto + "://" + "127.0.0.1:" + port; + listenTarget = port; + } if (targetPath) { target = target + targetPath; } + var server; var data = { target: target, @@ -48,7 +58,7 @@ export function addTarget(proxy, path, port, websocket, targetPath, sslOptions) }); } - server.listen(port); + server.listen(listenTarget); servers.push(server); return proxy.addRoute(path, { target: target }).then(() => { // routes are created with an activity timestamp artificially shifted into the past diff --git a/test/proxy_spec.js b/test/proxy_spec.js index eccb046..3246bd6 100644 --- a/test/proxy_spec.js +++ b/test/proxy_spec.js @@ -512,4 +512,21 @@ describe("Proxy Tests", function () { }) .then(done); }); + + it("proxy to unix socket test", function (done) { + var proxyPort = 55557; + var unixSocketUri = "%2Ftmp%2Ftest.sock"; + + util + .setupProxy(proxyPort, {}, []) + .then((proxy) => util.addTarget(proxy, "/unix", 0, false, null, null, unixSocketUri)) + .then(() => fetch("http://127.0.0.1:" + proxyPort + "/unix")) + .then((res) => { + expect(res.status).toEqual(200); + }) + .catch((err) => { + done.fail(err); + }) + .then(done); + }); });