Skip to content

Commit 89c74ce

Browse files
authored
feat(cubejs-server-core): support proxy in http agent transport (#9263)
1 parent 52a664e commit 89c74ce

File tree

3 files changed

+60
-7
lines changed

3 files changed

+60
-7
lines changed

packages/cubejs-server-core/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
"codesandbox-import-utils": "^2.1.12",
4141
"cross-spawn": "^7.0.1",
4242
"fs-extra": "^8.1.0",
43+
"http-proxy-agent": "^7.0.2",
44+
"https-proxy-agent": "^7.0.6",
4345
"is-docker": "^2.1.1",
4446
"joi": "^17.8.3",
4547
"jsonwebtoken": "^9.0.2",

packages/cubejs-server-core/src/core/agentCollect.ts

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { getEnv } from '@cubejs-backend/shared';
22
import http from 'http';
33
import https from 'https';
4+
import { HttpsProxyAgent } from 'https-proxy-agent';
5+
import { HttpProxyAgent } from 'http-proxy-agent';
46
import fetch from 'node-fetch';
57
import crypto from 'crypto';
68
import WebSocket from 'ws';
@@ -50,12 +52,12 @@ class WebSocketTransport implements AgentTransport {
5052
clearTimeout(this.pingTimeout);
5153
this.onClose();
5254
});
53-
55+
5456
this.wsClient.on('error', e => {
5557
connectionPromiseReject(e);
5658
this.logger('Agent Error', { error: (e.stack || e).toString() });
5759
});
58-
60+
5961
this.wsClient.on('message', (data: WebSocket.Data) => {
6062
try {
6163
const { method, params } = JSON.parse(data.toString());
@@ -103,17 +105,45 @@ class WebSocketTransport implements AgentTransport {
103105
}
104106
}
105107

108+
function isOnNoProxyList(url: string): boolean {
109+
const noProxy = process.env.NO_PROXY || process.env.no_proxy;
110+
if (!noProxy) {
111+
return false;
112+
}
113+
114+
const parsedUrl = new URL(url);
115+
const { hostname } = parsedUrl;
116+
const noProxyList = noProxy.split(',').map((entry) => entry.trim());
117+
118+
return noProxyList.some((entry) => {
119+
if (entry === '*') {
120+
return true;
121+
}
122+
if (entry.startsWith('.')) {
123+
return hostname.endsWith(entry);
124+
}
125+
126+
return hostname === entry;
127+
});
128+
}
129+
106130
class HttpTransport implements AgentTransport {
107-
private agent: http.Agent | https.Agent;
131+
private agent: http.Agent | https.Agent | HttpProxyAgent<string> | HttpsProxyAgent<string>;
108132

109133
public constructor(
110134
private readonly endpointUrl: string
111135
) {
112-
const AgentClass = endpointUrl.startsWith('https') ? https.Agent : http.Agent;
113-
this.agent = new AgentClass({
136+
const agentParams = {
114137
keepAlive: true,
115138
maxSockets: getEnv('agentMaxSockets')
116-
});
139+
};
140+
if (!isOnNoProxyList(endpointUrl) && (process.env.http_proxy || process.env.https_proxy)) {
141+
this.agent = endpointUrl.startsWith('https') ?
142+
new HttpsProxyAgent(process.env.https_proxy, agentParams) :
143+
new HttpProxyAgent(process.env.http_proxy, agentParams);
144+
} else {
145+
this.agent = endpointUrl.startsWith('https') ? new https.Agent(agentParams) : new http.Agent(agentParams);
146+
}
117147
}
118148

119149
public ready() {
@@ -167,7 +197,7 @@ export default async (event: Record<string, any>, endpointUrl: string, logger: a
167197
const sentAt = new Date().toJSON();
168198
const result = await transport.send(toFlush.map(r => ({ ...r, sentAt })));
169199
if (!result && retries > 0) return flush(toFlush, retries - 1);
170-
200+
171201
return true;
172202
} catch (e: any) {
173203
if (retries > 0) return flush(toFlush, retries - 1);

yarn.lock

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11335,6 +11335,11 @@ agent-base@^7.0.2, agent-base@^7.1.0:
1133511335
dependencies:
1133611336
debug "^4.3.4"
1133711337

11338+
agent-base@^7.1.2:
11339+
version "7.1.3"
11340+
resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-7.1.3.tgz#29435eb821bc4194633a5b89e5bc4703bafc25a1"
11341+
integrity sha512-jRR5wdylq8CkOe6hei19GGZnxM6rBGwFl3Bg0YItGDimvjGtAvdZk4Pu6Cl4u4Igsws4a1fd1Vq3ezrhn4KmFw==
11342+
1133811343
agentkeepalive@^4.1.3:
1133911344
version "4.1.4"
1134011345
resolved "https://registry.yarnpkg.com/agentkeepalive/-/agentkeepalive-4.1.4.tgz#d928028a4862cb11718e55227872e842a44c945b"
@@ -18204,6 +18209,14 @@ http-proxy-agent@^7.0.0:
1820418209
agent-base "^7.1.0"
1820518210
debug "^4.3.4"
1820618211

18212+
http-proxy-agent@^7.0.2:
18213+
version "7.0.2"
18214+
resolved "https://registry.yarnpkg.com/http-proxy-agent/-/http-proxy-agent-7.0.2.tgz#9a8b1f246866c028509486585f62b8f2c18c270e"
18215+
integrity sha512-T1gkAiYYDWYx3V5Bmyu7HcfcvL7mUrTWiM6yOfa3PIphViJ/gFPbvidQ+veqSOHci/PxBcDabeUNCzpOODJZig==
18216+
dependencies:
18217+
agent-base "^7.1.0"
18218+
debug "^4.3.4"
18219+
1820718220
http-proxy-middleware@^2.0.0, http-proxy-middleware@^2.0.3:
1820818221
version "2.0.7"
1820918222
resolved "https://registry.yarnpkg.com/http-proxy-middleware/-/http-proxy-middleware-2.0.7.tgz#915f236d92ae98ef48278a95dedf17e991936ec6"
@@ -18278,6 +18291,14 @@ https-proxy-agent@^7.0.1:
1827818291
agent-base "^7.0.2"
1827918292
debug "4"
1828018293

18294+
https-proxy-agent@^7.0.6:
18295+
version "7.0.6"
18296+
resolved "https://registry.yarnpkg.com/https-proxy-agent/-/https-proxy-agent-7.0.6.tgz#da8dfeac7da130b05c2ba4b59c9b6cd66611a6b9"
18297+
integrity sha512-vK9P5/iUfdl95AI+JVyUuIcVtd4ofvtrOr3HNtM2yxC9bnMbEdp3x01OhQNnjb8IJYi38VlTE3mBXwcfvywuSw==
18298+
dependencies:
18299+
agent-base "^7.1.2"
18300+
debug "4"
18301+
1828118302
human-signals@^1.1.1:
1828218303
version "1.1.1"
1828318304
resolved "https://registry.yarnpkg.com/human-signals/-/human-signals-1.1.1.tgz#c5b1cd14f50aeae09ab6c59fe63ba3395fe4dfa3"

0 commit comments

Comments
 (0)