Skip to content

Commit 4337046

Browse files
committed
Fix proxying to unix socket:
- no need to add request's url to the request path. This already exists and leads to duplication. - fix custom error routes with unix socket by setting target hostname to localhost.
1 parent 669c18c commit 4337046

File tree

3 files changed

+41
-13
lines changed

3 files changed

+41
-13
lines changed

lib/configproxy.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -388,15 +388,14 @@ export class ConfigurableProxy extends EventEmitter {
388388
});
389389
}
390390

391-
proxyOptsForTarget(target, reqUrl) {
391+
proxyOptsForTarget(target) {
392392
var proxyOptions = { target };
393393

394394
if (target.protocol.startsWith("unix")) {
395395
// No need for agents for unix sockets
396396
// No support for https for unix sockets
397397
proxyOptions.secure = false;
398398
proxyOptions.target.socketPath = decodeURIComponent(target.host);
399-
proxyOptions.target.pathname = (target.pathname ? target.pathname + "/" : "") + reqUrl;
400399
} else if (target.protocol.startsWith("https")) {
401400
proxyOptions.secure = true;
402401
proxyOptions.agent = this.httpsAgent;
@@ -495,17 +494,24 @@ export class ConfigurableProxy extends EventEmitter {
495494
return;
496495
}
497496
if (this.errorTarget) {
498-
var urlSpec = new URL(this.errorTarget);
499497
// error request is $errorTarget/$code?url=$requestUrl
500-
urlSpec.searchParams.set("url", req.url);
501-
urlSpec.pathname = urlSpec.pathname + code.toString();
502-
var url = urlSpec.toString();
503-
this.log.debug("Requesting custom error page: %s", url);
498+
var options = this.proxyOptsForTarget(new URL(this.errorTarget));
504499

505-
var options = this.proxyOptsForTarget(urlSpec, req.url);
506500
options.method = "GET";
507501

508-
var errorRequest = (options.secure ? https : http).request(url, options, (upstream) => {
502+
options.target.searchParams.set("url", req.url);
503+
options.target.pathname = options.target.pathname + code.toString()
504+
505+
if (options.target.socketPath) {
506+
options.target.hostname = 'localhost'
507+
var url = options.target.toString().substring(5) // chop off unix+
508+
} else {
509+
var url = options.target.toString()
510+
}
511+
512+
this.log.debug("Requesting custom error page: %s", url);
513+
514+
var errorRequest = (options.secure ? https : http).request(url, options.target, (upstream) => {
509515
if (res.writableEnded) {
510516
// response already done
511517
// make sure to consume upstream;
@@ -588,7 +594,7 @@ export class ConfigurableProxy extends EventEmitter {
588594
}
589595

590596
target = new URL(target);
591-
var proxyOptions = this.proxyOptsForTarget(target, req.url);
597+
var proxyOptions = this.proxyOptsForTarget(target);
592598

593599
args.push(proxyOptions);
594600

lib/testutil.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import https from "node:https";
55
import { WebSocketServer } from "ws";
66
import { ConfigurableProxy } from "./configproxy.js";
77
import { defaultLogger } from "./log.js";
8+
import { error } from "node:console";
89

910
var servers = [];
1011

@@ -102,7 +103,7 @@ export function addTargets(proxy, paths, port) {
102103
export function setupProxy(port, options, paths) {
103104
options = options || {};
104105
options.authToken = "secret";
105-
options.log = defaultLogger({ level: "error" });
106+
//options.log = defaultLogger({ level: "error" });
106107

107108
var proxy = new ConfigurableProxy(options);
108109
proxy._setup_timestamp = new Date(new Date().getTime() - 60000);
@@ -133,7 +134,12 @@ export function setupProxy(port, options, paths) {
133134
});
134135
errorServer.on("listening", onlisten);
135136
const errorUrl = new URL(options.errorTarget);
136-
errorServer.listen(errorUrl.port, ip);
137+
if (errorUrl.href.startsWith("unix+")) {
138+
console.log(decodeURIComponent(errorUrl.hostname));
139+
errorServer.listen(decodeURIComponent(errorUrl.hostname));
140+
} else {
141+
errorServer.listen(errorUrl.port, ip);
142+
}
137143
servers.push(errorServer);
138144
}
139145

test/proxy_spec.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,14 +513,30 @@ describe("Proxy Tests", function () {
513513
.then(done);
514514
});
515515

516+
it("custom error target with unix socket", function (done) {
517+
var proxyPort = 55550;
518+
util
519+
.setupProxy(proxyPort, { errorTarget: "unix+http://%2Ftmp%2Ftest.sock" }, [])
520+
.then(() => fetch("http://127.0.0.1:" + proxyPort + "/foo/bar"))
521+
.then((res) => {
522+
expect(res.status).toEqual(404);
523+
expect(res.headers.get("content-type")).toEqual("text/plain");
524+
return res.text();
525+
})
526+
.then((body) => {
527+
expect(body).toEqual("/foo/bar");
528+
})
529+
.then(done);
530+
});
531+
516532
it("proxy to unix socket test", function (done) {
517533
var proxyPort = 55557;
518534
var unixSocketUri = "%2Ftmp%2Ftest.sock";
519535

520536
util
521537
.setupProxy(proxyPort, {}, [])
522538
.then((proxy) => util.addTarget(proxy, "/unix", 0, false, null, null, unixSocketUri))
523-
.then(() => fetch("http://127.0.0.1:" + proxyPort + "/unix"))
539+
.then(() => fetch("http://127.0.0.1:" + proxyPort + "/unix/foo"))
524540
.then((res) => {
525541
expect(res.status).toEqual(200);
526542
})

0 commit comments

Comments
 (0)