Skip to content

Commit 6d12090

Browse files
jeanp413mustard-mh
authored andcommitted
Respect NO_PROXY rules when doing requests
Tool: gitpod/catfood.gitpod.cloud
1 parent 80ecd82 commit 6d12090

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

src/vs/platform/request/node/proxy.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,42 @@ function getSystemProxyURI(requestURL: Url, env: typeof process.env): string | n
1818
return null;
1919
}
2020

21+
function applySystemNoProxyRules(requestURL: Url, proxyURl: string | null, env: typeof process.env): string | null {
22+
const noProxy = env.NO_PROXY || env.no_proxy || null;
23+
if (!noProxy) {
24+
return proxyURl;
25+
}
26+
27+
const rules = noProxy.split(/[\s,]+/);
28+
if (rules[0] === '*') {
29+
return null;
30+
}
31+
32+
for (const rule of rules) {
33+
const ruleMatch = rule.match(/^(.+?)(?::(\d+))?$/);
34+
if (!ruleMatch || !ruleMatch[1]) {
35+
continue;
36+
}
37+
38+
const ruleHost = ruleMatch[1].replace(/^\.*/, '.');
39+
const rulePort = ruleMatch[2];
40+
const requestURLHost = requestURL.hostname!.replace(/^\.*/, '.');
41+
if (requestURLHost.endsWith(ruleHost) && (!rulePort || requestURL.port && requestURL.port === rulePort)) {
42+
return null;
43+
}
44+
}
45+
46+
return proxyURl;
47+
}
48+
2149
export interface IOptions {
2250
proxyUrl?: string;
2351
strictSSL?: boolean;
2452
}
2553

2654
export async function getProxyAgent(rawRequestURL: string, env: typeof process.env, options: IOptions = {}): Promise<Agent> {
2755
const requestURL = parseUrl(rawRequestURL);
28-
const proxyURL = options.proxyUrl || getSystemProxyURI(requestURL, env);
56+
const proxyURL = options.proxyUrl || applySystemNoProxyRules(requestURL, getSystemProxyURI(requestURL, env), env);
2957

3058
if (!proxyURL) {
3159
return null;

src/vs/platform/request/node/requestService.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,7 @@ export class RequestService extends AbstractRequestService implements IRequestSe
8383
...process.env,
8484
...shellEnv
8585
};
86-
const agent = options.agent ? options.agent : await getProxyAgent(options.url || '', env, { proxyUrl, strictSSL });
8786

88-
options.agent = agent;
8987
options.strictSSL = strictSSL;
9088

9189
if (this.authorization) {
@@ -95,7 +93,7 @@ export class RequestService extends AbstractRequestService implements IRequestSe
9593
};
9694
}
9795

98-
return this.logAndRequest(options, () => nodeRequest(options, token));
96+
return this.logAndRequest(options, () => nodeRequest(options, proxyUrl, env, token));
9997
}
10098

10199
async resolveProxy(url: string): Promise<string | undefined> {
@@ -149,21 +147,23 @@ async function getNodeRequest(options: IRequestOptions): Promise<IRawRequestFunc
149147
return module.request;
150148
}
151149

152-
export async function nodeRequest(options: NodeRequestOptions, token: CancellationToken): Promise<IRequestContext> {
150+
export async function nodeRequest(options: NodeRequestOptions, proxyUrl: string | undefined, env: { [key: string]: string | undefined }, token: CancellationToken): Promise<IRequestContext> {
153151
return Promises.withAsyncBody<IRequestContext>(async (resolve, reject) => {
154152
const endpoint = parseUrl(options.url!);
155153
const rawRequest = options.getRawRequest
156154
? options.getRawRequest(options)
157155
: await getNodeRequest(options);
158156

157+
const proxyAgent = options.agent ? options.agent : await getProxyAgent(options.url || '', env, { proxyUrl, strictSSL: options.strictSSL });
158+
159159
const opts: https.RequestOptions & { cache?: 'default' | 'no-store' | 'reload' | 'no-cache' | 'force-cache' | 'only-if-cached' } = {
160160
hostname: endpoint.hostname,
161161
port: endpoint.port ? parseInt(endpoint.port) : (endpoint.protocol === 'https:' ? 443 : 80),
162162
protocol: endpoint.protocol,
163163
path: endpoint.path,
164164
method: options.type || 'GET',
165165
headers: options.headers,
166-
agent: options.agent,
166+
agent: proxyAgent,
167167
rejectUnauthorized: isBoolean(options.strictSSL) ? options.strictSSL : true
168168
};
169169

@@ -182,7 +182,7 @@ export async function nodeRequest(options: NodeRequestOptions, token: Cancellati
182182
...options,
183183
url: res.headers['location'],
184184
followRedirects: followRedirects - 1
185-
}, token).then(resolve, reject);
185+
}, proxyUrl, env, token).then(resolve, reject);
186186
} else {
187187
let stream: streams.ReadableStreamEvents<Uint8Array> = res;
188188

0 commit comments

Comments
 (0)