|
4 | 4 | */
|
5 | 5 |
|
6 | 6 | import { ToolkitError } from '../../errors'
|
7 |
| -import { Logger } from '../../logger/logger' |
| 7 | +import { Logger, getLogger } from '../../logger/logger' |
8 | 8 | import { ChildProcess } from '../../utilities/processUtils'
|
9 | 9 | import { waitUntil } from '../../utilities/timeoutUtils'
|
10 | 10 | import { isDebugInstance } from '../../vscode/env'
|
| 11 | +import { tmpdir } from 'os' |
| 12 | +import { join } from 'path' |
| 13 | +import * as nodefs from 'fs' // eslint-disable-line no-restricted-imports |
| 14 | +import * as vscode from 'vscode' |
11 | 15 |
|
12 | 16 | export function getNodeExecutableName(): string {
|
13 | 17 | return process.platform === 'win32' ? 'node.exe' : 'node'
|
@@ -81,26 +85,112 @@ export async function validateNodeExe(nodePath: string[], lsp: string, args: str
|
81 | 85 | }
|
82 | 86 | }
|
83 | 87 |
|
| 88 | +/** |
| 89 | + * Gets proxy settings and certificates from VS Code |
| 90 | + */ |
| 91 | +export async function getVSCodeSettings(): Promise<{ proxyUrl?: string; certificatePath?: string }> { |
| 92 | + const result: { proxyUrl?: string; certificatePath?: string } = {} |
| 93 | + const logger = getLogger('amazonqLsp') |
| 94 | + |
| 95 | + try { |
| 96 | + // Get proxy settings from VS Code configuration |
| 97 | + const httpConfig = vscode.workspace.getConfiguration('http') |
| 98 | + const proxy = httpConfig.get<string>('proxy') |
| 99 | + if (proxy) { |
| 100 | + result.proxyUrl = proxy |
| 101 | + logger.info(`Using proxy from VS Code settings: ${proxy}`) |
| 102 | + } |
| 103 | + } catch (err) { |
| 104 | + logger.error(`Failed to get VS Code settings: ${err}`) |
| 105 | + return result |
| 106 | + } |
| 107 | + try { |
| 108 | + const tls = await import('tls') |
| 109 | + // @ts-ignore Get system certificates |
| 110 | + const systemCerts = tls.getCACertificates('system') |
| 111 | + // @ts-ignore Get any existing extra certificates |
| 112 | + const extraCerts = tls.getCACertificates('extra') |
| 113 | + const allCerts = [...systemCerts, ...extraCerts] |
| 114 | + if (allCerts && allCerts.length > 0) { |
| 115 | + logger.info(`Found ${allCerts.length} certificates in system's trust store`) |
| 116 | + |
| 117 | + const tempDir = join(tmpdir(), 'aws-toolkit-vscode') |
| 118 | + if (!nodefs.existsSync(tempDir)) { |
| 119 | + nodefs.mkdirSync(tempDir, { recursive: true }) |
| 120 | + } |
| 121 | + |
| 122 | + const certPath = join(tempDir, 'vscode-ca-certs.pem') |
| 123 | + const certContent = allCerts.join('\n') |
| 124 | + |
| 125 | + nodefs.writeFileSync(certPath, certContent) |
| 126 | + result.certificatePath = certPath |
| 127 | + logger.info(`Created certificate file at: ${certPath}`) |
| 128 | + } |
| 129 | + } catch (err) { |
| 130 | + logger.error(`Failed to extract certificates: ${err}`) |
| 131 | + } |
| 132 | + return result |
| 133 | +} |
| 134 | + |
84 | 135 | export function createServerOptions({
|
85 | 136 | encryptionKey,
|
86 | 137 | executable,
|
87 | 138 | serverModule,
|
88 | 139 | execArgv,
|
89 | 140 | warnThresholds,
|
| 141 | + env, |
90 | 142 | }: {
|
91 | 143 | encryptionKey: Buffer
|
92 | 144 | executable: string[]
|
93 | 145 | serverModule: string
|
94 | 146 | execArgv: string[]
|
95 | 147 | warnThresholds?: { cpu?: number; memory?: number }
|
| 148 | + env?: Record<string, string> |
96 | 149 | }) {
|
97 | 150 | return async () => {
|
98 | 151 | const bin = executable[0]
|
99 | 152 | const args = [...executable.slice(1), '--max-old-space-size=8196', serverModule, ...execArgv]
|
100 | 153 | if (isDebugInstance()) {
|
101 | 154 | args.unshift('--inspect=6080')
|
102 | 155 | }
|
103 |
| - const lspProcess = new ChildProcess(bin, args, { warnThresholds }) |
| 156 | + |
| 157 | + // Merge environment variables |
| 158 | + const processEnv = { ...process.env } |
| 159 | + if (env) { |
| 160 | + Object.assign(processEnv, env) |
| 161 | + } |
| 162 | + |
| 163 | + // Get settings from VS Code |
| 164 | + const settings = await getVSCodeSettings() |
| 165 | + const logger = getLogger('amazonqLsp') |
| 166 | + |
| 167 | + // Add proxy settings to the Node.js process |
| 168 | + if (settings.proxyUrl) { |
| 169 | + processEnv.HTTPS_PROXY = settings.proxyUrl |
| 170 | + } |
| 171 | + |
| 172 | + // Add certificate path if available |
| 173 | + if (settings.certificatePath) { |
| 174 | + processEnv.NODE_EXTRA_CA_CERTS = settings.certificatePath |
| 175 | + logger.info(`Using certificate file: ${settings.certificatePath}`) |
| 176 | + } |
| 177 | + |
| 178 | + // Get SSL verification settings |
| 179 | + const httpConfig = vscode.workspace.getConfiguration('http') |
| 180 | + const strictSSL = httpConfig.get<boolean>('proxyStrictSSL', true) |
| 181 | + |
| 182 | + // Handle SSL certificate verification |
| 183 | + if (!strictSSL) { |
| 184 | + processEnv.NODE_TLS_REJECT_UNAUTHORIZED = '0' |
| 185 | + logger.info('SSL verification disabled via VS Code settings') |
| 186 | + } |
| 187 | + |
| 188 | + const lspProcess = new ChildProcess(bin, args, { |
| 189 | + warnThresholds, |
| 190 | + spawnOptions: { |
| 191 | + env: processEnv, |
| 192 | + }, |
| 193 | + }) |
104 | 194 |
|
105 | 195 | // this is a long running process, awaiting it will never resolve
|
106 | 196 | void lspProcess.run()
|
|
0 commit comments