Skip to content

Support proxying to unix sockets #592

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 32 additions & 25 deletions lib/configproxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,34 @@ export class ConfigurableProxy extends EventEmitter {
});
}

proxyOptsForTarget(target, reqUrl) {
var proxyOptions = { target };

if (target.protocol.startsWith("unix")) {
proxyOptions.secure = false;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No support for SSL on unix sockets; this is not really needed anyway.

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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
16 changes: 13 additions & 3 deletions lib/testutil.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions test/proxy_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});