-
Notifications
You must be signed in to change notification settings - Fork 193
/
Copy pathproxyProvider.ts
56 lines (49 loc) · 1.36 KB
/
proxyProvider.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
import {
HttpsProxyAgent,
HttpsProxyAgentOptions,
} from "https-proxy-agent/dist";
import { URL } from "url";
import { workspace } from "vscode";
import tabnineExtensionProperties from "./globals/tabnineExtensionProperties";
type ProxyAgentOptions = {
ignoreCertificateErrors?: boolean;
ca: Buffer | undefined;
};
export default function getHttpsProxyAgent(
options: ProxyAgentOptions
): HttpsProxyAgent | undefined {
const proxySettings = getProxySettings();
if (!proxySettings || !tabnineExtensionProperties.useProxySupport) {
return undefined;
}
const proxyUrl = new URL(proxySettings);
const proxyOptions: HttpsProxyAgentOptions = {
protocol: proxyUrl.protocol,
port: proxyUrl.port,
hostname: proxyUrl.hostname,
pathname: proxyUrl.pathname,
ca: options.ca,
rejectUnauthorized: !options.ignoreCertificateErrors,
};
try {
return new HttpsProxyAgent(proxyOptions);
} catch (e) {
return undefined;
}
}
export function getProxySettings(): string | undefined {
let proxy: string | undefined = workspace
.getConfiguration()
.get<string>("http.proxy");
if (!proxy) {
proxy =
process.env.HTTPS_PROXY ||
process.env.https_proxy ||
process.env.HTTP_PROXY ||
process.env.http_proxy;
}
if (proxy?.endsWith("/")) {
proxy = proxy.substr(0, proxy.length - 1);
}
return proxy;
}