|
| 1 | +/*! |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import vscode from 'vscode' |
| 7 | +import { getLogger } from '../logger/logger' |
| 8 | + |
| 9 | +interface ProxyConfig { |
| 10 | + proxyUrl: string | undefined |
| 11 | + certificateAuthority: string | undefined |
| 12 | +} |
| 13 | + |
| 14 | +/** |
| 15 | + * Utility class for handling proxy configuration |
| 16 | + */ |
| 17 | +export class ProxyUtil { |
| 18 | + private static readonly logger = getLogger('proxyUtil') |
| 19 | + |
| 20 | + /** |
| 21 | + * Sets proxy environment variables based on VS Code settings for use with the Flare Language Server |
| 22 | + * |
| 23 | + * See documentation here for setting the environement variables which are inherited by Flare LS process: |
| 24 | + * https://github.com/aws/language-server-runtimes/blob/main/runtimes/docs/proxy.md |
| 25 | + */ |
| 26 | + public static configureProxyForLanguageServer(): void { |
| 27 | + try { |
| 28 | + const proxyConfig = this.getProxyConfiguration() |
| 29 | + |
| 30 | + this.setProxyEnvironmentVariables(proxyConfig) |
| 31 | + } catch (err) { |
| 32 | + this.logger.error(`Failed to configure proxy: ${err}`) |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Gets proxy configuration from VS Code settings |
| 38 | + */ |
| 39 | + private static getProxyConfiguration(): ProxyConfig { |
| 40 | + const httpConfig = vscode.workspace.getConfiguration('http') |
| 41 | + const proxyUrl = httpConfig.get<string>('proxy') |
| 42 | + this.logger.debug(`Proxy URL Setting in VSCode Settings: ${proxyUrl}`) |
| 43 | + |
| 44 | + const amazonQConfig = vscode.workspace.getConfiguration('amazonQ') |
| 45 | + const proxySettings = amazonQConfig.get<{ |
| 46 | + certificateAuthority?: string |
| 47 | + }>('proxy', {}) |
| 48 | + |
| 49 | + return { |
| 50 | + proxyUrl, |
| 51 | + certificateAuthority: proxySettings.certificateAuthority, |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Sets environment variables based on proxy configuration |
| 57 | + */ |
| 58 | + private static setProxyEnvironmentVariables(config: ProxyConfig): void { |
| 59 | + const proxyUrl = config.proxyUrl |
| 60 | + |
| 61 | + // Always enable experimental proxy support for better handling of both explicit and transparent proxies |
| 62 | + process.env.EXPERIMENTAL_HTTP_PROXY_SUPPORT = 'true' |
| 63 | + // Add OpenSSL certificate store support |
| 64 | + process.env.NODE_OPTIONS = '--use-openssl-ca' |
| 65 | + |
| 66 | + // Set proxy environment variables |
| 67 | + if (proxyUrl) { |
| 68 | + process.env.HTTPS_PROXY = proxyUrl |
| 69 | + process.env.HTTP_PROXY = proxyUrl |
| 70 | + this.logger.debug(`Set proxy environment variables: ${proxyUrl}`) |
| 71 | + } |
| 72 | + |
| 73 | + // Set certificate bundle environment variables if configured |
| 74 | + if (config.certificateAuthority) { |
| 75 | + process.env.NODE_EXTRA_CA_CERTS = config.certificateAuthority |
| 76 | + process.env.AWS_CA_BUNDLE = config.certificateAuthority |
| 77 | + this.logger.debug(`Set certificate bundle path: ${config.certificateAuthority}`) |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments