src/vs/platform/agentHost/node/agentHostProxyResolver.ts

167 LOC · 69 covered · 98 uncovered · 9 ranges · 398 concepts · 1 introducers · 177 tests

File neighbourhood

The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.

1 > /*--------------------------------------------------------------------------------------------- agentHostProxyResolver.ts ×9
2 > * Copyright (c) Microsoft Corporation. All rights reserved.
3 > * Licensed under the MIT License. See License.txt in the project root for license information.
4 > *--------------------------------------------------------------------------------------------*/
5 >
6 > import { LogLevel as ProxyLogLevel, ProxyAgentParams, ProxySupportSetting, createFetchPatch, createProxyAuthorizationLookup, createProxyResolver, loadSystemCertificates } from '@vscode/proxy-agent';
7 > import { IDisposable, toDisposable } from '../../../base/common/lifecycle.js';
8 > import { IConfigurationService } from '../../configuration/common/configuration.js';
9 > import { createDecorator } from '../../instantiation/common/instantiation.js';
10 > import { ILogService, LogLevel } from '../../log/common/log.js';
11 > import { AuthInfo, Credentials, systemCertificatesNodeDefault } from '../../request/common/request.js';
12 > import { IAgentHostClientProxyConnection } from '../common/agentHostClientProxyChannel.js';
13 >
14 > export const IAgentHostProxyResolver = createDecorator<IAgentHostProxyResolver>('agentHostProxyResolver');
15 >
16 > /**
17 > * Node-side registry of renderer {@link IAgentHostClientProxyConnection}s keyed
18 > * by client id. Populated by the agent host's connection lifecycle (one entry
19 > * per connected renderer) and consumed by {@link CopilotAgent} to resolve the
20 > * CAPI proxy through VS Code's Electron session before spawning the Copilot SDK.
21 > *
22 > * Proxy configuration is a property of the machine, not of a particular window,
23 > * so any connected renderer can serve the lookup; the resolver calls the first
24 > * available connection and falls through to the next on failure.
25 > */
26 > export interface IAgentHostProxyResolver {
27 > readonly _serviceBrand: undefined;
28 >
29 > /** Register a renderer connection. Disposing the result removes it. */
30 > register(clientId: string, connection: IAgentHostClientProxyConnection): IDisposable;
31 >
32 > /**
33 > * Resolve the proxy URL for `url` (e.g. `http://host:port`), or `undefined`
34 > * for a direct connection. Reuses `@vscode/proxy-agent`'s `resolveProxyURL`
35 > * so the same precedence as the rest of VS Code applies: `http.noProxy` →
36 > * `http.proxy` setting → `HTTP(S)_PROXY` env vars → the host proxy resolution
37 > * that runs in VS Code (Electron session) via the reverse channel.
38 > */
39 > resolveProxy(url: string): Promise<string | undefined>;
40 >
41 > /** Fetch using the same proxy, certificate, and host/PAC resolution as {@link resolveProxy}. */
42 > fetch(input: string | URL | Request, init?: RequestInit): Promise<Response>;
43 > }
44 >
45 > export class AgentHostProxyResolver implements IAgentHostProxyResolver {
46 >
47 > declare readonly _serviceBrand: undefined;
48 >
49 > private readonly _connections = new Map<string, IAgentHostClientProxyConnection>();
50 > private _proxyResolver: ReturnType<typeof createProxyResolver> | undefined;
51 > private _proxyAgentParams: ProxyAgentParams | undefined;
52 > private _fetch: typeof globalThis.fetch | undefined;
53 >
54 > constructor(
55 @IConfigurationService private readonly _configurationService: IConfigurationService,
56 @ILogService private readonly _logService: ILogService,
57 ) { }
59 > register(clientId: string, connection: IAgentHostClientProxyConnection): IDisposable {
60 this._connections.set(clientId, connection);
61 return toDisposable(() => {
62 if (this._connections.get(clientId) === connection) {
63 this._connections.delete(clientId);
64 }
65 });
66 }
68 > resolveProxy(url: string): Promise<string | undefined> {
69 return this._getProxyResolver().resolveProxyURL(url);
70 }
72 > fetch(input: string | URL | Request, init?: RequestInit): Promise<Response> {
73 if (!this._fetch) {
74 const proxyResolver = this._getProxyResolver();
75 this._fetch = createFetchPatch(this._proxyAgentParams!, globalThis.fetch, proxyResolver.resolveProxyURL);
76 }
77 return this._fetch(input, init);
78 }
80 > private _getProxyResolver(): ReturnType<typeof createProxyResolver> {
81 if (!this._proxyResolver) {
82 // Mirror `workbench/api/node/proxyResolver.ts`.
83 const config = <T>(key: string): T | undefined => this._configurationService.getValue<T>(key);
84 const systemCertificatesV2 = () => config<boolean>('http.experimental.systemCertificatesV2') ?? false;
85 const systemCertificates = () => !!config<boolean>('http.systemCertificates');
86 const params: ProxyAgentParams = {
87 // The host proxy resolution runs in VS Code: reverse-call a connected
88 // renderer, whose IRequestService.resolveProxy hits the Electron
89 // session (system settings / PAC scripts).
90 resolveProxy: (url) => this._hostResolveProxy(url),
91 lookupProxyAuthorization: createProxyAuthorizationLookup({
92 log: this._logService,
93 lookupAuthorization: authInfo => this._hostLookupAuthorization(authInfo),
94 lookupKerberosAuthorization: url => this._hostLookupKerberosAuthorization(url),
95 }),
96 getProxyURL: () => config<string>('http.proxy'),
97 getProxySupport: () => config<ProxySupportSetting>('http.proxySupport') || 'off',
98 getNoProxyConfig: () => config<string[]>('http.noProxy') || [],
99 isAdditionalFetchSupportEnabled: () => config<boolean>('http.fetchAdditionalSupport') ?? true,
100 isWebSocketPatchEnabled: () => config<boolean>('http.webSocketAdditionalSupport') ?? true,
101 addCertificatesV1: () => !systemCertificatesV2() && systemCertificates(),
102 addCertificatesV2: () => systemCertificatesV2() && systemCertificates(),
103 loadSystemCertificatesFromNode: () => config<boolean>('http.systemCertificatesNode') ?? systemCertificatesNodeDefault,
104 loadAdditionalCertificates: async () => loadSystemCertificates({
105 loadSystemCertificatesFromNode: () => config<boolean>('http.systemCertificatesNode') ?? systemCertificatesNodeDefault,
106 log: this._logService,
107 }),
108 log: this._logService,
109 getLogLevel: () => {
110 switch (this._logService.getLevel()) {
111 case LogLevel.Trace: return ProxyLogLevel.Trace;
112 case LogLevel.Debug: return ProxyLogLevel.Debug;
113 case LogLevel.Info: return ProxyLogLevel.Info;
114 case LogLevel.Warning: return ProxyLogLevel.Warning;
115 case LogLevel.Error: return ProxyLogLevel.Error;
116 case LogLevel.Off: return ProxyLogLevel.Off;
117 default: return ProxyLogLevel.Info;
118 }
119 },
120 proxyResolveTelemetry: () => { },
121 // Only the local agent host wires the reverse proxy channel
122 // and we want to look up the client's proxy settings only
123 // when the agent host is local (i.e., on the same machine as
124 // the client).
125 isUseHostProxyEnabled: () => this._connections.size > 0,
126 getNetworkInterfaceCheckInterval: () => (config<number>('http.experimental.networkInterfaceCheckInterval') ?? 300) * 1000,
127 env: process.env,
128 };
129 this._proxyAgentParams = params;
130 this._proxyResolver = createProxyResolver(params);
131 }
132 return this._proxyResolver;
133 }
135 > private async _hostResolveProxy(url: string): Promise<string | undefined> {
136 for (const connection of this._connections.values()) {
137 try {
138 return await connection.resolveProxy(url);
139 } catch {
140 // This renderer could not serve the lookup; try the next one.
141 }
142 }
143 return undefined;
144 }
146 > private async _hostLookupAuthorization(authInfo: AuthInfo): Promise<Credentials | undefined> {
147 for (const connection of this._connections.values()) {
148 try {
149 return await connection.lookupAuthorization(authInfo);
150 } catch {
151 // This renderer could not serve the lookup; try the next one.
152 }
153 }
154 return undefined;
155 }
157 > private async _hostLookupKerberosAuthorization(url: string): Promise<string | undefined> {
158 for (const connection of this._connections.values()) {
159 try {
160 return await connection.lookupKerberosAuthorization(url);
161 } catch {
162 // This renderer could not serve the lookup; try the next one.
163 }
164 }
165 return undefined;
166 }