-
Notifications
You must be signed in to change notification settings - Fork 795
Expand file tree
/
Copy pathproxyUtil.ts
More file actions
106 lines (91 loc) · 3.85 KB
/
proxyUtil.ts
File metadata and controls
106 lines (91 loc) · 3.85 KB
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
/*!
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
import vscode from 'vscode'
import { getLogger } from '../logger/logger'
interface ProxyConfig {
proxyUrl: string | undefined
noProxy: string | undefined
proxyStrictSSL: boolean | true
certificateAuthority: string | undefined
isProxyAndCertAutoDiscoveryEnabled: boolean
}
/**
* Utility class for handling proxy configuration
*/
export class ProxyUtil {
private static readonly logger = getLogger('proxyUtil')
/**
* Sets proxy environment variables based on VS Code settings for use with the Flare Language Server
*
* See documentation here for setting the environement variables which are inherited by Flare LS process:
* https://github.com/aws/language-server-runtimes/blob/main/runtimes/docs/proxy.md
*/
public static async configureProxyForLanguageServer(): Promise<void> {
try {
const proxyConfig = this.getProxyConfiguration()
await this.setProxyEnvironmentVariables(proxyConfig)
} catch (err) {
this.logger.error(`Failed to configure proxy: ${err}`)
}
}
/**
* Gets proxy configuration from VS Code settings
*/
private static getProxyConfiguration(): ProxyConfig {
const httpConfig = vscode.workspace.getConfiguration('http')
const proxyUrl = httpConfig.get<string>('proxy')
this.logger.debug(`Proxy URL Setting in VSCode Settings: ${proxyUrl}`)
const noProxy = httpConfig.get<string>('noProxy')
if (noProxy) {
this.logger.info(`Using noProxy from VS Code settings: ${noProxy}`)
}
const proxyStrictSSL = httpConfig.get<boolean>('proxyStrictSSL', true)
const amazonQConfig = vscode.workspace.getConfiguration('amazonQ')
const proxySettings = amazonQConfig.get<{
certificateAuthority?: string
enableProxyAndCertificateAutoDiscovery: boolean
}>('proxy', { enableProxyAndCertificateAutoDiscovery: true })
return {
proxyUrl,
noProxy,
proxyStrictSSL,
certificateAuthority: proxySettings.certificateAuthority,
isProxyAndCertAutoDiscoveryEnabled: proxySettings.enableProxyAndCertificateAutoDiscovery,
}
}
/**
* Sets environment variables based on proxy configuration
*/
private static async setProxyEnvironmentVariables(config: ProxyConfig): Promise<void> {
// Set experimental proxy support based on user setting
process.env.EXPERIMENTAL_HTTP_PROXY_SUPPORT = config.isProxyAndCertAutoDiscoveryEnabled.toString()
const proxyUrl = config.proxyUrl
// Set proxy environment variables
if (proxyUrl) {
process.env.HTTPS_PROXY = proxyUrl
process.env.HTTP_PROXY = proxyUrl
this.logger.debug(`Set proxy environment variables: ${proxyUrl}`)
}
// set NO_PROXY vals
const noProxy = config.noProxy
if (noProxy) {
process.env.NO_PROXY = noProxy
this.logger.debug(`Set NO_PROXY environment variable: ${noProxy}`)
}
const strictSSL = config.proxyStrictSSL
// Handle SSL certificate verification
if (!strictSSL) {
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'
this.logger.info('SSL verification disabled via VS Code settings')
return // No need to set CA certs when SSL verification is disabled
}
// Set certificate bundle environment variables if user configured
if (config.certificateAuthority) {
process.env.NODE_EXTRA_CA_CERTS = config.certificateAuthority
process.env.AWS_CA_BUNDLE = config.certificateAuthority
this.logger.debug(`Set certificate bundle path: ${config.certificateAuthority}`)
}
}
}