refactor: simplify n8n routing by removing path allowlist and defaulting proxy behavior
Browse files- health-server.js +32 -57
health-server.js
CHANGED
|
@@ -608,66 +608,41 @@ const server = http.createServer(async (req, res) => {
|
|
| 608 |
);
|
| 609 |
}
|
| 610 |
|
| 611 |
-
// 2. n8n
|
| 612 |
-
//
|
| 613 |
-
const
|
| 614 |
-
|
| 615 |
-
|
| 616 |
-
|
| 617 |
-
|
| 618 |
-
|
| 619 |
-
|
| 620 |
-
pathname.startsWith("/auth/") ||
|
| 621 |
-
pathname.startsWith("/login") ||
|
| 622 |
-
pathname.startsWith("/signup") ||
|
| 623 |
-
pathname.startsWith("/logout") ||
|
| 624 |
-
pathname.startsWith("/nodes/") ||
|
| 625 |
-
pathname.startsWith("/templates/") ||
|
| 626 |
-
pathname.startsWith("/workflow") ||
|
| 627 |
-
pathname.startsWith("/healthz");
|
| 628 |
-
|
| 629 |
-
|
| 630 |
-
|
| 631 |
-
if (isN8nPath) {
|
| 632 |
-
const proxyHeaders = {
|
| 633 |
-
...req.headers,
|
| 634 |
-
host: `127.0.0.1:${TARGET_PORT}`,
|
| 635 |
-
"x-forwarded-for": req.socket.remoteAddress,
|
| 636 |
-
"x-forwarded-host": req.headers.host,
|
| 637 |
-
"x-forwarded-proto": "https",
|
| 638 |
-
};
|
| 639 |
|
| 640 |
-
|
| 641 |
-
|
| 642 |
-
|
| 643 |
-
|
| 644 |
-
|
| 645 |
-
|
| 646 |
-
|
| 647 |
-
|
| 648 |
-
|
| 649 |
-
|
| 650 |
-
|
| 651 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 652 |
);
|
|
|
|
| 653 |
|
| 654 |
-
|
| 655 |
-
res.writeHead(503, { "Content-Type": "application/json" });
|
| 656 |
-
res.end(
|
| 657 |
-
JSON.stringify({
|
| 658 |
-
status: "starting",
|
| 659 |
-
message: "n8n is initializing...",
|
| 660 |
-
}),
|
| 661 |
-
);
|
| 662 |
-
});
|
| 663 |
-
|
| 664 |
-
req.pipe(proxyReq);
|
| 665 |
-
return;
|
| 666 |
-
}
|
| 667 |
-
|
| 668 |
-
// 3. Fallback: Redirect anything else to Dashboard
|
| 669 |
-
res.writeHead(302, { Location: "/" });
|
| 670 |
-
res.end();
|
| 671 |
});
|
| 672 |
|
| 673 |
server.on("upgrade", (req, socket, head) => {
|
|
|
|
| 608 |
);
|
| 609 |
}
|
| 610 |
|
| 611 |
+
// 2. n8n Proxy Logic
|
| 612 |
+
// Any path that isn't a dashboard route gets proxied to n8n.
|
| 613 |
+
const proxyHeaders = {
|
| 614 |
+
...req.headers,
|
| 615 |
+
host: `127.0.0.1:${TARGET_PORT}`,
|
| 616 |
+
"x-forwarded-for": req.socket.remoteAddress,
|
| 617 |
+
"x-forwarded-host": req.headers.host,
|
| 618 |
+
"x-forwarded-proto": "https",
|
| 619 |
+
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 620 |
|
| 621 |
+
const proxyReq = http.request(
|
| 622 |
+
{
|
| 623 |
+
hostname: TARGET_HOST,
|
| 624 |
+
port: TARGET_PORT,
|
| 625 |
+
path: pathname + url.search,
|
| 626 |
+
method: req.method,
|
| 627 |
+
headers: proxyHeaders,
|
| 628 |
+
},
|
| 629 |
+
(proxyRes) => {
|
| 630 |
+
res.writeHead(proxyRes.statusCode, proxyRes.headers);
|
| 631 |
+
proxyRes.pipe(res);
|
| 632 |
+
},
|
| 633 |
+
);
|
| 634 |
+
|
| 635 |
+
proxyReq.on("error", () => {
|
| 636 |
+
res.writeHead(503, { "Content-Type": "application/json" });
|
| 637 |
+
res.end(
|
| 638 |
+
JSON.stringify({
|
| 639 |
+
status: "starting",
|
| 640 |
+
message: "n8n is initializing...",
|
| 641 |
+
}),
|
| 642 |
);
|
| 643 |
+
});
|
| 644 |
|
| 645 |
+
req.pipe(proxyReq);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 646 |
});
|
| 647 |
|
| 648 |
server.on("upgrade", (req, socket, head) => {
|