|
| 1 | +/*! |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * Copyright 2022 Sourcegraph, Inc. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + */ |
| 14 | +import * as vscode from 'vscode' |
| 15 | +import http from 'http' |
| 16 | +import https from 'https' |
| 17 | +import { getLogger } from '../../shared/logger' |
| 18 | + |
| 19 | +// The path to the exported class can be found in the npm contents |
| 20 | +// https://www.npmjs.com/package/@vscode/proxy-agent?activeTab=code |
| 21 | +const nodeModules = '_VSCODE_NODE_MODULES' |
| 22 | +const proxyAgentPath = '@vscode/proxy-agent/out/agent' |
| 23 | +const proxyAgent = 'PacProxyAgent' |
| 24 | +export const keepAliveHeader = 'keep-alive-codewhisperer' |
| 25 | +let userProxyUrl = '' |
| 26 | + |
| 27 | +export function updateUserProxyUrl() { |
| 28 | + userProxyUrl = vscode.workspace.getConfiguration('http').get('proxy') || '' |
| 29 | +} |
| 30 | + |
| 31 | +export function initializeNetworkAgent(): void { |
| 32 | + /** |
| 33 | + * We use keepAlive agents here to avoid excessive SSL/TLS handshakes for autocomplete requests. |
| 34 | + * Socket timeout at client is the same as service connection idle timeout |
| 35 | + */ |
| 36 | + const httpAgent = new http.Agent({ keepAlive: true, timeout: 60000 }) |
| 37 | + const httpsAgent = new https.Agent({ keepAlive: true, timeout: 60000 }) |
| 38 | + |
| 39 | + const customAgent = ({ protocol }: Pick<URL, 'protocol'>): http.Agent => { |
| 40 | + if (protocol === 'http:') { |
| 41 | + return httpAgent |
| 42 | + } |
| 43 | + return httpsAgent |
| 44 | + } |
| 45 | + updateUserProxyUrl() |
| 46 | + /** |
| 47 | + * This works around an issue in the default VS Code proxy agent code. When `http.proxySupport` |
| 48 | + * is set to its default value and no proxy setting is being used, the proxy library does not |
| 49 | + * properly reuse the agent set on the http(s) method and is instead always using a new agent |
| 50 | + * per request. |
| 51 | + * |
| 52 | + * To work around this, we patch the default proxy agent method and overwrite the |
| 53 | + * `originalAgent` value before invoking it for requests that want to keep their connection |
| 54 | + * alive only when user is not using their own http proxy and the request contains keepAliveHeader |
| 55 | + * |
| 56 | + * c.f. https://github.com/microsoft/vscode/issues/173861 |
| 57 | + * code reference: https://github.com/sourcegraph/cody/pull/868/files |
| 58 | + */ |
| 59 | + try { |
| 60 | + const PacProxyAgent = (globalThis as any)?.[nodeModules]?.[proxyAgentPath]?.[proxyAgent] ?? undefined |
| 61 | + if (PacProxyAgent) { |
| 62 | + const originalConnect = PacProxyAgent.prototype.connect |
| 63 | + // Patches the implementation defined here: |
| 64 | + // https://github.com/microsoft/vscode-proxy-agent/blob/d340b9d34684da494d6ebde3bcd18490a8bbd071/src/agent.ts#L53 |
| 65 | + PacProxyAgent.prototype.connect = function (req: http.ClientRequest, opts: { protocol: string }): any { |
| 66 | + try { |
| 67 | + const connectionHeader = req.getHeader('connection') |
| 68 | + const connectionHeaderHasKeepAlive = |
| 69 | + connectionHeader === keepAliveHeader || |
| 70 | + (Array.isArray(connectionHeader) && connectionHeader.includes(keepAliveHeader)) |
| 71 | + if (connectionHeaderHasKeepAlive && userProxyUrl === '') { |
| 72 | + this.opts.originalAgent = customAgent(opts) |
| 73 | + return originalConnect.call(this, req, opts) |
| 74 | + } |
| 75 | + return originalConnect.call(this, req, opts) |
| 76 | + } catch { |
| 77 | + return originalConnect.call(this, req, opts) |
| 78 | + } |
| 79 | + } |
| 80 | + } else { |
| 81 | + getLogger().info('PacProxyAgent not found') |
| 82 | + } |
| 83 | + } catch (error) { |
| 84 | + // Log any errors in the patching logic |
| 85 | + getLogger().error('Failed to patch http agent', error) |
| 86 | + } |
| 87 | +} |
0 commit comments