Skip to content

Commit 826f766

Browse files
authored
Merge pull request #4589 from easyops-cn/steve/v3-fix-proxy
fix(serve): create an http agent that always use IPv4
2 parents feb9579 + 0c4dcd6 commit 826f766

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

packages/brick-container/serve/getProxy.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import http from "node:http";
2+
import https from "node:https";
13
import { responseInterceptor } from "http-proxy-middleware";
24
import _ from "lodash";
35
import jsYaml from "js-yaml";
@@ -8,6 +10,17 @@ import { injectIndexHtml } from "./utils/injectIndexHtml.js";
810
import { getProcessedPublicDeps } from "./utils/getProcessedPublicDeps.js";
911
import { concatBrickPackages } from "./utils/concatBrickPackages.js";
1012

13+
// Create an http agent that always use IPv4
14+
let agent;
15+
const getAgent = (server) => {
16+
if (!agent) {
17+
agent = new (server.startsWith("https:") ? https : http).Agent({
18+
family: 4,
19+
});
20+
}
21+
return agent;
22+
};
23+
1124
const { safeDump, JSON_SCHEMA } = jsYaml;
1225

1326
export default function getProxy(env, getRawIndexHtml) {
@@ -71,7 +84,11 @@ export default function getProxy(env, getRawIndexHtml) {
7184
// Reset the origin header to the remote server
7285
if (
7386
req.headers["origin"] ===
74-
`http${env.https ? "s" : ""}://${host}:${port}`
87+
`http${env.https ? "s" : ""}://${host}:${port}` ||
88+
// Always reset origin when host is 0.0.0.0
89+
(host === "0.0.0.0" &&
90+
req.headers["origin"] &&
91+
req.headers["origin"] !== server)
7592
) {
7693
proxyReq.setHeader("origin", server);
7794
}
@@ -459,6 +476,7 @@ function getBasicProxyOptions({ baseHref, server }, subPath) {
459476
: {
460477
[`^${_.escapeRegExp(baseHref)}${subPath}`]: `/next/${subPath}`,
461478
},
479+
agent: getAgent(server),
462480
};
463481
}
464482

0 commit comments

Comments
 (0)