-
Notifications
You must be signed in to change notification settings - Fork 82
/
proxySettings.ts
184 lines (157 loc) · 5.87 KB
/
proxySettings.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
* Copyright (c) 2021 Red Hat, Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*/
import { workspace } from 'vscode';
/**
* Represents the information needed to communicate through a proxy
*/
export class ProxySettings {
private _host: string;
private _port: string;
private _auth?: ProxyAuthorization;
constructor(host: string, port: string, auth?: ProxyAuthorization) {
this._host = host;
this._port = port;
this._auth = auth;
}
get host(): string {
return this._host;
}
get port(): string {
return this._port;
}
get auth(): ProxyAuthorization {
return this._auth;
}
}
/**
* Represents the information needed to authenticate with the proxy
*/
export class ProxyAuthorization {
private _username: string;
private _password: string;
constructor(username: string, password: string) {
this._username = username;
this._password = password;
}
get username(): string {
return this._username
}
get password(): string {
return this._password;
}
}
/**
* Returns the proxy settings that are declared in the VS Code settings, or null if no proxy is configured
*
* @throws if the proxy settings aren't in the expected format
* @returns the proxy settings that are declared in the VS Code settings, or null if no proxy is configured
*/
export function getProxySettings(): ProxySettings {
const proxyAddress = getProxyAddress();
if (!proxyAddress) {
return null;
}
const regexResult = HOST_AND_PORT_EXTRACTOR.exec(proxyAddress);
if (!regexResult[1]) {
return null;
}
const host: string = regexResult[1];
const port: string = regexResult[2] ? regexResult[2] : "80";
const proxyAuthorizationString = getProxyAuthorization();
if (!proxyAuthorizationString) {
return new ProxySettings(host, port);
}
if (proxyAuthorizationString.indexOf(' ') === -1) {
throw new Error('A space is expected in the Authorization header between the authorization method and encoded username/password');
}
const encodedUserAndPass = proxyAuthorizationString.split(' ').pop();
const decodedUserAndPass: string = Buffer.from(encodedUserAndPass, 'base64').toString('utf8');
if (decodedUserAndPass.indexOf(':') === -1) {
throw new Error('Authorization header is not in the expected format');
}
const [uriEncodedUsername, uriEncodedPassword] = decodedUserAndPass.split(':');
const proxyAuthorization = new ProxyAuthorization(decodeURIComponent(uriEncodedUsername), decodeURIComponent(uriEncodedPassword));
return new ProxySettings(host, port, proxyAuthorization);
}
/**
* Returns the proxy settings as arguments for the JVM
*
* eg. -Dhttp.proxyHost=<proxy_host> -Dhttp.proxyPort=<proxy_port> -Dhttp.proxyUser=<user> -Dhttp.proxyPassword=<password>
*
* @param proxySettings the proxy settings to convert into JVM args
*/
export function getProxySettingsAsJVMArgs(proxySettings: ProxySettings): string {
// Java doesn't recognize localhost in the proxy settings
const adaptedHostName = 'localhost'.startsWith(proxySettings.host) ? '127.0.0.1' : proxySettings.host;
let proxyJVMArgs: string = ` -Dhttp.proxyHost=${adaptedHostName} -Dhttp.proxyPort=${proxySettings.port}`
+ ` -Dhttps.proxyHost=${adaptedHostName} -Dhttps.proxyPort=${proxySettings.port} `;
if (proxySettings.auth) {
proxyJVMArgs += ` -Dhttp.proxyUser=${proxySettings.auth.username} -Dhttp.proxyPassword=${proxySettings.auth.password}`
+ ` -Dhttps.proxyUser=${proxySettings.auth.username} -Dhttps.proxyPassword=${proxySettings.auth.password} `;
}
return proxyJVMArgs;
}
/**
* Returns the proxy settings as environment variables for LemMinX
*
* @param proxySettings the proxy settings to convert into environment variables
* @returns the proxy settings as environment variables for LemMinX
*/
export function getProxySettingsAsEnvironmentVariables(proxySettings: ProxySettings): Record<string, string> {
// process.env inherits from Object, so it's okay to do so here as well
const proxyEnv: Record<string, string> = {};
proxyEnv['HTTP_PROXY_HOST'] = proxySettings.host;
proxyEnv['HTTP_PROXY_PORT'] = proxySettings.port;
if (proxySettings.auth) {
proxyEnv['HTTP_PROXY_USERNAME'] = proxySettings.auth.username;
proxyEnv['HTTP_PROXY_PASSWORD'] = proxySettings.auth.password;
}
return proxyEnv;
}
/**
* Checks if the given JVM arguments contain any proxy configuration
*
* @param jvmArgs the arguments being passed to the JVM
*/
export function jvmArgsContainsProxySettings(jvmArgs: string): boolean {
return (
[JVM_PROXY_HOST, JVM_PROXY_PORT, JVM_PROXY_USER, JVM_PROXY_PASS] //
.map(prop => jvmArgs.indexOf(`-D${prop}`) !== -1)
.reduce((a, b, _index, _array) => a || b, false)
);
}
const HOST_AND_PORT_EXTRACTOR = /https?:\/\/([^:/]+)(?::([0-9]+))?/;
const JVM_PROXY_HOST = 'http.proxyHost';
const JVM_PROXY_PORT = 'http.proxyPort';
const JVM_PROXY_USER = 'http.proxyUser';
const JVM_PROXY_PASS = 'http.proxyPassword';
/**
* Returns the address of the proxy
*
* @returns the address of the proxy
*/
function getProxyAddress(): string {
const fromSettings = workspace.getConfiguration('http').get('proxy', undefined);
if (fromSettings) {
return fromSettings;
}
// VS Code allows you to set up the proxy using the environment variables http_proxy and https_proxy
// Use this as fallback if the `"http.proxy"` setting isn't set
return process.env['http_proxy'];
}
/**
* Returns the Proxy-Authorization to use to access the proxy
*
* @returns The Proxy-Authorization to use to access the proxy
*/
function getProxyAuthorization(): string {
return workspace.getConfiguration('http').get('proxyAuthorization', undefined);
}