Skip to content

Commit b55a13e

Browse files
fix:add s3 proxy when env config (#152)
* fix:add s3 proxy when env config * fix: add process.env and setup fetch proxy * chore: adjust the code * fix: use undiciFetch instead of fetch --------- Co-authored-by: FinleyGe <[email protected]>
1 parent 43eeb28 commit b55a13e

File tree

7 files changed

+49
-4
lines changed

7 files changed

+49
-4
lines changed

bun.lock

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@
2323
"comlink": "^4.4.2",
2424
"date-fns": "^4.1.0",
2525
"express": "^5.1.0",
26+
"http-proxy-agent": "^7.0.2",
27+
"https-proxy-agent": "^7.0.6",
2628
"json5": "^2.2.3",
2729
"minio": "^8.0.5",
2830
"nanoid": "^5.1.5",
31+
"undici": "^7.13.0",
2932
"uuid": "^11.1.0",
3033
"zod": "^3.24.3",
3134
},
@@ -429,7 +432,7 @@
429432
},
430433
"sdk": {
431434
"name": "@fastgpt-sdk/plugin",
432-
"version": "0.1.3",
435+
"version": "0.1.7",
433436
"dependencies": {
434437
"@fortaine/fetch-event-source": "^3.0.6",
435438
"@ts-rest/core": "^3.52.1",
@@ -1891,7 +1894,7 @@
18911894

18921895
"ufo": ["[email protected]", "", {}, "sha512-9a4/uxlTWJ4+a5i0ooc1rU7C7YOw3wT+UGqdeNNHWnOF9qcMBgLRS+4IYUqbczewFx4mLEig6gawh7X6mFlEkA=="],
18931896

1894-
"undici": ["undici@7.12.0", "", {}, "sha512-GrKEsc3ughskmGA9jevVlIOPMiiAHJ4OFUtaAH+NhfTUSiZ1wMPIQqQvAJUrJspFXJt3EBWgpAeoHEDVT1IBug=="],
1897+
"undici": ["undici@7.13.0", "", {}, "sha512-l+zSMssRqrzDcb3fjMkjjLGmuiiK2pMIcV++mJaAc9vhjSGpvM7h43QgP+OAMb1GImHmbPyG2tBXeuyG5iY4gA=="],
18951898

18961899
"undici-types": ["[email protected]", "", {}, "sha512-9UJ2xGDvQ43tYyVMpuHlsgApydB8ZKfVYTsLDhXkFL/6gfkp+U8xTGdh8pMJv1SpZna0zxG1DwsKZsreLbXBxw=="],
18971900

@@ -2067,6 +2070,8 @@
20672070

20682071
"bl/readable-stream": ["[email protected]", "", { "dependencies": { "abort-controller": "^3.0.0", "buffer": "^6.0.3", "events": "^3.3.0", "process": "^0.11.10", "string_decoder": "^1.3.0" } }, "sha512-oIGGmcpTLwPga8Bn6/Z75SVaH1z5dUut2ibSyAMVhmUggWpmDn2dapB0n7f8nwaSiRtepAsfJyfXIO5DCVAODg=="],
20692072

2073+
"cheerio/undici": ["[email protected]", "", {}, "sha512-GrKEsc3ughskmGA9jevVlIOPMiiAHJ4OFUtaAH+NhfTUSiZ1wMPIQqQvAJUrJspFXJt3EBWgpAeoHEDVT1IBug=="],
2074+
20702075
"cli-truncate/string-width": ["[email protected]", "", { "dependencies": { "eastasianwidth": "^0.2.0", "emoji-regex": "^9.2.2", "strip-ansi": "^7.0.1" } }, "sha512-HnLOCR3vjcY8beoNLtcjZ5/nxn2afmME6lhrDrebokqMap+XbeW8n9TXpPDOqdGK5qcI3oT0GKTW6wC7EMiVqA=="],
20712076

20722077
"cliui/strip-ansi": ["[email protected]", "", { "dependencies": { "ansi-regex": "^5.0.1" } }, "sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A=="],

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,12 @@
3636
"comlink": "^4.4.2",
3737
"date-fns": "^4.1.0",
3838
"express": "^5.1.0",
39+
"http-proxy-agent": "^7.0.2",
40+
"https-proxy-agent": "^7.0.6",
3941
"json5": "^2.2.3",
4042
"minio": "^8.0.5",
4143
"nanoid": "^5.1.5",
44+
"undici": "^7.13.0",
4245
"uuid": "^11.1.0",
4346
"zod": "^3.24.3"
4447
},

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { isProd } from './constants';
77
import { initS3Server } from './s3/config';
88
import { connectSignoz } from './utils/signoz';
99
import { initModels } from '@model/init';
10+
import { setupProxy } from './utils/setupProxy';
1011

1112
const app = express().use(
1213
express.json(),
@@ -19,6 +20,7 @@ connectSignoz();
1920
// System
2021
initOpenAPI(app);
2122
initRouter(app);
23+
setupProxy();
2224

2325
// DB
2426
try {

src/s3/controller.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import { z } from 'zod';
77
import { addLog } from '@/utils/log';
88
import { getErrText } from '@tool/utils/err';
99
import { catchError } from '@/utils/catch';
10+
import { HttpProxyAgent } from 'http-proxy-agent';
11+
import { HttpsProxyAgent } from 'https-proxy-agent';
1012

1113
export const FileInputSchema = z
1214
.object({
@@ -52,7 +54,12 @@ export class S3Service {
5254
port: this.config.port,
5355
useSSL: this.config.useSSL,
5456
accessKey: this.config.accessKey,
55-
secretKey: this.config.secretKey
57+
secretKey: this.config.secretKey,
58+
transportAgent: process.env.HTTP_PROXY
59+
? new HttpProxyAgent(process.env.HTTP_PROXY)
60+
: process.env.HTTPS_PROXY
61+
? new HttpsProxyAgent(process.env.HTTPS_PROXY)
62+
: undefined
5663
});
5764
}
5865

src/utils/setupProxy.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { ProxyAgent, setGlobalDispatcher, fetch as undiciFetch } from 'undici';
2+
import { addLog } from './log';
3+
import { isProd } from '@/constants';
4+
const httpProxy = process.env.HTTP_PROXY;
5+
const httpsProxy = process.env.HTTPS_PROXY;
6+
7+
export function setupProxy() {
8+
const proxy = httpProxy || httpsProxy;
9+
if (proxy) {
10+
const proxyAgent = new ProxyAgent(proxy);
11+
setGlobalDispatcher(proxyAgent);
12+
13+
// Replace global fetch with undici's fetch to ensure proxy is used
14+
if (isProd) {
15+
// Node
16+
global.fetch = ((input: any, init: any) => {
17+
return undiciFetch(input, init);
18+
}) as any;
19+
}
20+
21+
addLog.info(`Using proxy: ${proxy}`);
22+
}
23+
}

src/worker/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,10 @@ export class WorkerPool<Props = Record<string, unknown>, Response = unknown> {
9393
// Create a new worker and push it queue.
9494
const workerId = `${Date.now()}${Math.random()}`;
9595
const worker = new Worker('./worker.js', {
96-
env: {},
96+
env: {
97+
...(process.env.HTTP_PROXY ? { HTTP_PROXY: process.env.HTTP_PROXY } : {}),
98+
...(process.env.HTTPS_PROXY ? { HTTPS_PROXY: process.env.HTTPS_PROXY } : {})
99+
},
97100
resourceLimits: {
98101
maxOldGenerationSizeMb: parseInt(process.env.MAX_MEMORYMB || '1024')
99102
}

src/worker/worker.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { LoadToolsByFilename } from '@tool/init';
44
import { isProd } from '@/constants';
55
import { getErrText } from '@tool/utils/err';
66
import type { Main2WorkerMessageType } from './type';
7+
import { setupProxy } from '@/utils/setupProxy';
8+
setupProxy();
79

810
// rewrite console.log to send to parent
911
console.log = (...args: any[]) => {

0 commit comments

Comments
 (0)