-
Notifications
You must be signed in to change notification settings - Fork 23
/
proxy-settings.ts
110 lines (95 loc) · 4.08 KB
/
proxy-settings.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*!
* Copyright (c) 2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
const { UrlWithStringQuery, parse } = require('url');
const HttpsProxyAgent = require('https-proxy-agent');
const HttpProxyAgent = require('http-proxy-agent');
class ProxyAgentOptionsBuilder {
private proxyUrl: typeof UrlWithStringQuery = {};
private proxyCredentials: any = {};
private constructor() {
/* intentionally empty */
}
static newInstance(options: ProxyOptions | undefined, environmentProxy: string | undefined): ProxyAgentOptionsBuilder {
return new ProxyAgentOptionsBuilder()
.parseUrlFromEnvironment(environmentProxy, options)
.parseUrlFromOptions(options)
.parseCredentialsFromOptions(options);
}
parseUrlFromOptions(options: ProxyOptions | undefined): ProxyAgentOptionsBuilder {
if (options !== undefined && options.url !== undefined) {
this.proxyUrl = parse(options.url);
}
return this;
}
parseUrlFromEnvironment(environmentProxy: string | undefined, options: ProxyOptions | undefined): ProxyAgentOptionsBuilder {
const shouldIgnoreProxyFromEnv = options !== undefined && options.ignoreProxyFromEnv;
if (environmentProxy !== undefined && !shouldIgnoreProxyFromEnv) {
this.proxyUrl = parse(environmentProxy);
}
return this;
}
parseCredentialsFromOptions(options: ProxyOptions | undefined): ProxyAgentOptionsBuilder {
if (options !== undefined && options.username !== undefined && options.password !== undefined) {
const credentials = `${options.username}:${options.password}`;
this.proxyCredentials = { headers: { 'Proxy-Authorization': `Basic ${Buffer.from(credentials).toString('base64')}` } };
}
return this;
}
isEmpty(): boolean {
// can ignore proxyCredentials here, as we can't send credentials if we don't know a proxy location
return 0 === Object.keys(this.proxyUrl).length;
}
getOptions(): any {
return {
...this.proxyUrl,
...this.proxyCredentials
};
}
}
function buildHttpsProxyAgent(options: ProxyOptions | undefined): typeof HttpsProxyAgent | undefined {
/* tslint:disable-next-line:strict-boolean-expressions */
const environmentProxy = process.env.https_proxy || process.env.HTTPS_PROXY;
const proxyOptions = ProxyAgentOptionsBuilder.newInstance(options, environmentProxy);
return proxyOptions.isEmpty() ? undefined : new HttpsProxyAgent(proxyOptions.getOptions());
}
function buildHttpProxyAgent(options: ProxyOptions | undefined): typeof HttpProxyAgent | undefined {
/* tslint:disable-next-line:strict-boolean-expressions */
const environmentProxy = process.env.http_proxy || process.env.HTTP_PROXY;
const proxyOptions = ProxyAgentOptionsBuilder.newInstance(options, environmentProxy);
return proxyOptions.isEmpty() ? undefined : new HttpProxyAgent(proxyOptions.getOptions());
}
/**
* Provider of an Agent that establishes a proxy connection.
*/
export class ProxyAgent {
/** The Agent that provides the proxy connection. */
public readonly proxyAgent?: typeof HttpsProxyAgent;
public readonly httpProxyAgent?: typeof HttpProxyAgent.HttpProxyAgent;
public constructor(options?: ProxyOptions | undefined) {
this.httpProxyAgent = buildHttpProxyAgent(options);
this.proxyAgent = buildHttpsProxyAgent(options);
}
}
/**
* Options for establishing a proxy connection.
*/
export interface ProxyOptions {
/** The url and port of the proxy server to connect to. It needs to be set like this: URL:PORT */
url?: string;
/** The username to authenticate to the proxy server with. */
username?: string;
/** The password to authenticate to the proxy server with. */
password?: string;
/** If proxy environment variables HTTPS_PROXY, https_proxy, HTTP_PROXY and http_proxy should be ignored. */
ignoreProxyFromEnv?: boolean;
}