Skip to content

Commit

Permalink
chore: just prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
JoaoOtavioS committed Jun 24, 2024
1 parent 30f7d75 commit 2ceec88
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 69 deletions.
32 changes: 17 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ const proxy = createProxyServer({});
const target = "http://example.com"; /* address of your proxy server here */

const server = createServer(async (req, res) => {
try {
await proxy.web(req, res, { target });
} catch (error) {
console.error(error);
res.statusCode = 500;
res.end("Proxy error: " + error.toString());
}
try {
await proxy.web(req, res, { target });
} catch (error) {
console.error(error);
res.statusCode = 500;
res.end("Proxy error: " + error.toString());
}
});

server.listen(80, () => console.log("Proxy is listening on http://localhost"));
Expand All @@ -69,16 +69,18 @@ const { createProxyServer } = require("@squarecloud/http-proxy");
const proxy = createProxyServer({ ws: true });
const target = "ws://example.com"; /* address of your proxy server here */

const server = createServer(async (req, res) => { /* ... */ });
const server = createServer(async (req, res) => {
/* ... */
});

server.on("upgrade", async (req, socket, head) => {
try {
// use proxy.ws() instead of proxy.web() for proxying WebSocket requests.
await proxy.ws(req, socket, head, { target });
} catch (error) {
console.error(error);
socket.end();
}
try {
// use proxy.ws() instead of proxy.web() for proxying WebSocket requests.
await proxy.ws(req, socket, head, { target });
} catch (error) {
console.error(error);
socket.end();
}
});

server.listen(80, () => console.log("Proxy is listening on http://localhost"));
Expand Down
14 changes: 10 additions & 4 deletions lib/http-proxy/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ common.isSSL = isSSL;
common.setupOutgoing = function (outgoing, options, req, forward) {
outgoing.port = options[forward || "target"].port || (isSSL.test(options[forward || "target"].protocol) ? 443 : 80);
["host", "hostname", "socketPath", "pfx", "key", "passphrase", "cert", "ca", "ciphers", "secureProtocol"].forEach(
(e) => (outgoing[e] = outgoing[e] || options[forward || "target"][e]),
(e) => (outgoing[e] = outgoing[e] || options[forward || "target"][e])
);

outgoing.method = options.method || req.method;
Expand All @@ -75,8 +75,10 @@ common.setupOutgoing = function (outgoing, options, req, forward) {
if (outgoing.port === 443) {
// Respect `NODE_TLS_REJECT_UNAUTHORIZED` environment variable (https://nodejs.org/docs/latest/api/cli.html#node_tls_reject_unauthorizedvalue)
const NODE_TLS_REJECT_UNAUTHORIZED = process.env.NODE_TLS_REJECT_UNAUTHORIZED;
const rejectUnauthorizedEnv = typeof NODE_TLS_REJECT_UNAUTHORIZED !== "undefined" ? NODE_TLS_REJECT_UNAUTHORIZED.toString() : undefined;
outgoing.rejectUnauthorized = typeof options.secure === "undefined" ? rejectUnauthorizedEnv !== "0" : options.secure;
const rejectUnauthorizedEnv =
typeof NODE_TLS_REJECT_UNAUTHORIZED !== "undefined" ? NODE_TLS_REJECT_UNAUTHORIZED.toString() : undefined;
outgoing.rejectUnauthorized =
typeof options.secure === "undefined" ? rejectUnauthorizedEnv !== "0" : options.secure;
}

outgoing.agent = options.agent || false;
Expand All @@ -98,7 +100,11 @@ common.setupOutgoing = function (outgoing, options, req, forward) {
const targetPath = target && options.prependPath !== false ? target.path || target.pathname || "" : "";
const pathToAppend = req.url.split("?")[0] || "";
const query = req.url.split("?").slice(1).join("?");
const outgoingPath = !options.ignorePath ? (!options.toProxy ? pathToAppend + (query ? `?${query}` : "") : req.url) : "";
const outgoingPath = !options.ignorePath
? !options.toProxy
? pathToAppend + (query ? `?${query}` : "")
: req.url
: "";

outgoing.path = common.urlJoin(targetPath, outgoingPath);

Expand Down
22 changes: 14 additions & 8 deletions lib/http-proxy/passes/web-incoming.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ module.exports = {

const encrypted = req.isSpdy || common.hasEncryptedConnection(req);
const values = {
For: req.headers["cf-connecting-ip"] || req.headers["x-forwarded-for"] || req.connection.remoteAddress || req.socket.remoteAddress,
For:
req.headers["cf-connecting-ip"] ||
req.headers["x-forwarded-for"] ||
req.connection.remoteAddress ||
req.socket.remoteAddress,
Port: common.getPort(req),
Proto: encrypted ? "https" : "http",
};
Expand Down Expand Up @@ -100,23 +104,25 @@ module.exports = {
if (options.forward) {
// If forward enable, so just pipe the request
const forwardReq = (options.forward.protocol === "https:" ? https : http).request(
common.setupOutgoing(options.ssl || {}, options, req, "forward"),
common.setupOutgoing(options.ssl || {}, options, req, "forward")
);
// error handler (e.g. ECONNRESET, ECONNREFUSED)
// Handle errors on incoming request as well as it makes sense to
const forwardError = createErrorHandler(forwardReq, options.forward);
req.on("error", forwardError);
forwardReq.on("error", forwardError);

pipeline(options.buffer || req, forwardReq, () => { });
pipeline(options.buffer || req, forwardReq, () => {});

if (!options.target) {
return res.end();
}
}

// Request initalization
const proxyReq = (options.target.protocol === "https:" ? https : http).request(common.setupOutgoing(options.ssl || {}, options, req));
const proxyReq = (options.target.protocol === "https:" ? https : http).request(
common.setupOutgoing(options.ssl || {}, options, req)
);

// Enable developers to modify the proxyReq before headers are sent
proxyReq.on("socket", (socket) => {
Expand All @@ -126,15 +132,15 @@ module.exports = {
if (server && !proxyReq.getHeader("expect")) {
server.emit("proxyReq", proxyReq, req, res, options);
}
pipeline(options.buffer || req, proxyReq, () => { });
pipeline(options.buffer || req, proxyReq, () => {});
});
} else {
if (server && !proxyReq.getHeader("expect")) {
server.emit("proxyReq", proxyReq, req, res, options);
}

req.on("close", proxyReq.destroy);
pipeline(options.buffer || req, proxyReq, () => { });
pipeline(options.buffer || req, proxyReq, () => {});
}
});

Expand Down Expand Up @@ -178,7 +184,7 @@ module.exports = {
if (!res.writableEnded) {
// Allow us to listen when the proxy has completed
proxyRes.on("end", () => (server ? server.emit("end", req, res, proxyRes) : null));
pipeline(proxyRes, res, () => { });
pipeline(proxyRes, res, () => {});
} else {
if (server) {
server.emit("end", req, res, proxyRes);
Expand All @@ -191,7 +197,7 @@ module.exports = {
if ((req.destroyed || req.socket?.destroyed) && err.code === "ECONNRESET") {
server.emit("econnreset", err, req, res, url);

if (typeof proxyReq.destroy === "function" && !proxyReq.destroyed) {
if (typeof proxyReq.destroy === "function" && !proxyReq.destroyed && !proxyReq.socket?.destroyed) {
proxyReq.destroy();
}

Expand Down
13 changes: 7 additions & 6 deletions lib/http-proxy/passes/ws-incoming.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ module.exports = {
if (!options.xfwd) return;

const remoteAddress =
req.headers["cf-connecting-ip"] || req.headers["x-forwarded-for"] || req.connection?.remoteAddress || req.socket?.remoteAddress;
req.headers["cf-connecting-ip"] ||
req.headers["x-forwarded-for"] ||
req.connection?.remoteAddress ||
req.socket?.remoteAddress;

const values = {
For: remoteAddress,
Expand Down Expand Up @@ -85,7 +88,7 @@ module.exports = {
}
return head;
},
[line],
[line]
)
.join("\r\n") + "\r\n\r\n"
);
Expand All @@ -96,7 +99,7 @@ module.exports = {
if (head && head.length) socket.unshift(head);

const proxyReq = (common.isSSL.test(options.target.protocol) ? https : http).request(
common.setupOutgoing(options.ssl || {}, options, req),
common.setupOutgoing(options.ssl || {}, options, req)
);

// Enable developers to modify the proxyReq before headers are sent
Expand All @@ -118,9 +121,7 @@ module.exports = {
proxySocket.on("error", onOutgoingError);

// Allow us to listen when the websocket has completed
proxySocket.on("end", () => {
server.emit("close", proxyRes, proxySocket, proxyHead);
});
proxySocket.on("end", () => server.emit("close", proxyRes, proxySocket, proxyHead));

// The pipe below will end proxySocket if socket closes cleanly, but not
// if it errors (eg, vanishes from the net and starts returning
Expand Down
40 changes: 20 additions & 20 deletions test/lib-http-proxy-common-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe("lib/http-proxy/common.js", () => {
method: "i",
url: "am",
headers: { pro: "xy", overwritten: false },
},
}
);

expect(outgoing.host).to.eql("hey");
Expand Down Expand Up @@ -61,7 +61,7 @@ describe("lib/http-proxy/common.js", () => {
method: "i",
url: "am",
headers: { pro: "xy", overwritten: false },
},
}
);
expect(outgoing.headers.connection).to.eql("upgrade");
});
Expand All @@ -84,7 +84,7 @@ describe("lib/http-proxy/common.js", () => {
method: "i",
url: "am",
headers: { pro: "xy", overwritten: false },
},
}
);
expect(outgoing.headers.connection).to.eql("keep-alive, upgrade");
});
Expand All @@ -108,7 +108,7 @@ describe("lib/http-proxy/common.js", () => {
method: "i",
url: "am",
headers: { pro: "xy", overwritten: false },
},
}
);
expect(outgoing.headers.connection).to.eql("close");
});
Expand All @@ -131,7 +131,7 @@ describe("lib/http-proxy/common.js", () => {
method: "i",
url: "am",
headers: { pro: "xy", overwritten: false },
},
}
);
expect(outgoing.headers.connection).to.eql("close");
});
Expand Down Expand Up @@ -159,7 +159,7 @@ describe("lib/http-proxy/common.js", () => {
method: "i",
url: "am",
headers: { pro: "xy" },
},
}
);

expect(outgoing.host).to.eql("how");
Expand Down Expand Up @@ -194,7 +194,7 @@ describe("lib/http-proxy/common.js", () => {
{
url: "am",
},
"forward",
"forward"
);

expect(outgoing.path).to.eql("some-path/am");
Expand All @@ -210,7 +210,7 @@ describe("lib/http-proxy/common.js", () => {
host: "whatever.com",
},
},
{ url: "/" },
{ url: "/" }
);

expect(outgoing.port).to.eql(443);
Expand All @@ -224,7 +224,7 @@ describe("lib/http-proxy/common.js", () => {
target: { path: "hellothere" },
prependPath: false,
},
{ url: "hi" },
{ url: "hi" }
);

expect(outgoing.path).to.eql("hi");
Expand All @@ -237,7 +237,7 @@ describe("lib/http-proxy/common.js", () => {
{
target: { path: "/forward" },
},
{ url: "/static/path" },
{ url: "/static/path" }
);

expect(outgoing.path).to.eql("/forward/static/path");
Expand All @@ -250,7 +250,7 @@ describe("lib/http-proxy/common.js", () => {
{
target: { path: "/forward" },
},
{ url: "/?foo=bar//&target=http://foobar.com/?a=1%26b=2&other=2" },
{ url: "/?foo=bar//&target=http://foobar.com/?a=1%26b=2&other=2" }
);

expect(outgoing.path).to.eql("/forward/?foo=bar//&target=http://foobar.com/?a=1%26b=2&other=2");
Expand All @@ -268,7 +268,7 @@ describe("lib/http-proxy/common.js", () => {
target: new URL("http://sometarget.com:80"),
toProxy: true,
},
{ url: google },
{ url: google }
);

expect(outgoing.path).to.eql("/" + google);
Expand All @@ -283,7 +283,7 @@ describe("lib/http-proxy/common.js", () => {
target: new URL("http://sometarget.com:80"),
toProxy: true,
},
{ url: google },
{ url: google }
);

expect(outgoing.path).to.eql("/" + google);
Expand All @@ -298,7 +298,7 @@ describe("lib/http-proxy/common.js", () => {
target: new URL("http://sometarget.com:80"),
toProxy: true,
},
{ url: google },
{ url: google }
);

expect(outgoing.path).to.eql("/" + google);
Expand All @@ -314,7 +314,7 @@ describe("lib/http-proxy/common.js", () => {
target: new URL(myEndpoint),
ignorePath: true,
},
{ url: "/more/crazy/pathness" },
{ url: "/more/crazy/pathness" }
);

expect(outgoing.path).to.eql("/some/crazy/path/whoooo");
Expand All @@ -330,7 +330,7 @@ describe("lib/http-proxy/common.js", () => {
ignorePath: true,
prependPath: false,
},
{ url: "/more/crazy/pathness" },
{ url: "/more/crazy/pathness" }
);

expect(outgoing.path).to.eql("");
Expand All @@ -347,7 +347,7 @@ describe("lib/http-proxy/common.js", () => {
target: new URL(myEndpoint),
changeOrigin: true,
},
{ url: "/" },
{ url: "/" }
);

expect(outgoing.headers.host).to.eql("mycouch.com:6984");
Expand All @@ -365,7 +365,7 @@ describe("lib/http-proxy/common.js", () => {
},
changeOrigin: true,
},
{ url: "/" },
{ url: "/" }
);
expect(outgoing.headers.host).to.eql("mycouch.com:6984");
});
Expand Down Expand Up @@ -394,7 +394,7 @@ describe("lib/http-proxy/common.js", () => {
{
method: "i",
url: "am",
},
}
);

expect(outgoing.pfx).eql("my-pfx");
Expand All @@ -414,7 +414,7 @@ describe("lib/http-proxy/common.js", () => {
target: new URL("https://whooooo.com"),
method: "POST",
},
{ method: "GET", url: "" },
{ method: "GET", url: "" }
);

expect(outgoing.method).eql("POST");
Expand Down
Loading

0 comments on commit 2ceec88

Please sign in to comment.