Skip to content
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
65 changes: 38 additions & 27 deletions lib/configproxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,15 +388,14 @@ export class ConfigurableProxy extends EventEmitter {
});
}

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

if (target.protocol.startsWith("unix")) {
// No need for agents for unix sockets
// No support for https for unix sockets
proxyOptions.secure = false;
proxyOptions.target.socketPath = decodeURIComponent(target.host);
proxyOptions.target.pathname = (target.pathname ? target.pathname + "/" : "") + reqUrl;
} else if (target.protocol.startsWith("https")) {
proxyOptions.secure = true;
proxyOptions.agent = this.httpsAgent;
Expand Down Expand Up @@ -495,35 +494,47 @@ export class ConfigurableProxy extends EventEmitter {
return;
}
if (this.errorTarget) {
var urlSpec = new URL(this.errorTarget);
// error request is $errorTarget/$code?url=$requestUrl
urlSpec.searchParams.set("url", req.url);
urlSpec.pathname = urlSpec.pathname + code.toString();
var url = urlSpec.toString();
this.log.debug("Requesting custom error page: %s", url);
var options = this.proxyOptsForTarget(new URL(this.errorTarget));

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

var errorRequest = (options.secure ? https : http).request(url, options, (upstream) => {
if (res.writableEnded) {
// response already done
// make sure to consume upstream;
upstream.resume();
return;
options.target.searchParams.set("url", req.url);
options.target.pathname = options.target.pathname + code.toString();

var url = "";
if (options.target.socketPath) {
options.target.hostname = "localhost";
url = options.target.toString().substring(5); // chop off unix+
} else {
url = options.target.toString();
}

this.log.debug("Requesting custom error page: %s", url);

var errorRequest = (options.secure ? https : http).request(
url,
options.target,
(upstream) => {
if (res.writableEnded) {
// response already done
// make sure to consume upstream;
upstream.resume();
return;
}
["content-type", "content-encoding"].map((key) => {
if (!upstream.headers[key]) return;
if (res.setHeader) res.setHeader(key, upstream.headers[key]);
});
if (res.writeHead) res.writeHead(code);
upstream.on("data", (data) => {
if (res.write && !res.writableEnded) res.write(data);
});
upstream.on("end", () => {
if (res.end) res.end();
});
}
["content-type", "content-encoding"].map((key) => {
if (!upstream.headers[key]) return;
if (res.setHeader) res.setHeader(key, upstream.headers[key]);
});
if (res.writeHead) res.writeHead(code);
upstream.on("data", (data) => {
if (res.write && !res.writableEnded) res.write(data);
});
upstream.on("end", () => {
if (res.end) res.end();
});
});
);
errorRequest.on("error", (e) => {
// custom error failed, fallback on default
this.log.error("Failed to get custom error page: %s", e);
Expand Down Expand Up @@ -588,7 +599,7 @@ export class ConfigurableProxy extends EventEmitter {
}

target = new URL(target);
var proxyOptions = this.proxyOptsForTarget(target, req.url);
var proxyOptions = this.proxyOptsForTarget(target);

args.push(proxyOptions);

Expand Down
10 changes: 7 additions & 3 deletions lib/testutil.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import http from "node:http";
import https from "node:https";
import { WebSocketServer } from "ws";
import { ConfigurableProxy } from "./configproxy.js";
import { defaultLogger } from "./log.js";

var servers = [];

Expand Down Expand Up @@ -102,7 +101,7 @@ export function addTargets(proxy, paths, port) {
export function setupProxy(port, options, paths) {
options = options || {};
options.authToken = "secret";
options.log = defaultLogger({ level: "error" });
//options.log = defaultLogger({ level: "error" });
Copy link
Member

Choose a reason for hiding this comment

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

Can this be restored? Or was there a reason it was removed/


var proxy = new ConfigurableProxy(options);
proxy._setup_timestamp = new Date(new Date().getTime() - 60000);
Expand Down Expand Up @@ -133,7 +132,12 @@ export function setupProxy(port, options, paths) {
});
errorServer.on("listening", onlisten);
const errorUrl = new URL(options.errorTarget);
errorServer.listen(errorUrl.port, ip);
if (errorUrl.href.startsWith("unix+")) {
console.log(decodeURIComponent(errorUrl.hostname));
errorServer.listen(decodeURIComponent(errorUrl.hostname));
} else {
errorServer.listen(errorUrl.port, ip);
}
servers.push(errorServer);
}

Expand Down
18 changes: 17 additions & 1 deletion test/proxy_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -513,14 +513,30 @@ describe("Proxy Tests", function () {
.then(done);
});

it("custom error target with unix socket", function (done) {
var proxyPort = 55550;
util
.setupProxy(proxyPort, { errorTarget: "unix+http://%2Ftmp%2Ftest.sock" }, [])
.then(() => fetch("http://127.0.0.1:" + proxyPort + "/foo/bar"))
.then((res) => {
expect(res.status).toEqual(404);
expect(res.headers.get("content-type")).toEqual("text/plain");
return res.text();
})
.then((body) => {
expect(body).toEqual("/foo/bar");
})
.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(() => fetch("http://127.0.0.1:" + proxyPort + "/unix/foo"))
.then((res) => {
expect(res.status).toEqual(200);
})
Expand Down