@@ -2,17 +2,59 @@ import { Api } from "coder/site/src/api/api"
22import fs from "fs/promises"
33import * as os from "os"
44import { ProxyAgent } from "proxy-agent"
5- import { getProxyForUrl } from "proxy-from-env"
65import * as vscode from "vscode"
76import { CertificateError } from "./error"
7+ import { getProxyForUrl } from "./proxy"
88import { Storage } from "./storage"
99
1010// expandPath will expand ${userHome} in the input string.
11- const expandPath = ( input : string ) : string => {
11+ function expandPath ( input : string ) : string {
1212 const userHome = os . homedir ( )
1313 return input . replace ( / \$ { userHome} / g, userHome )
1414}
1515
16+ async function createHttpAgent ( ) : Promise < ProxyAgent > {
17+ const cfg = vscode . workspace . getConfiguration ( )
18+ const insecure = Boolean ( cfg . get ( "coder.insecure" ) )
19+ const certFile = expandPath ( String ( cfg . get ( "coder.tlsCertFile" ) ?? "" ) . trim ( ) )
20+ const keyFile = expandPath ( String ( cfg . get ( "coder.tlsKeyFile" ) ?? "" ) . trim ( ) )
21+ const caFile = expandPath ( String ( cfg . get ( "coder.tlsCaFile" ) ?? "" ) . trim ( ) )
22+
23+ return new ProxyAgent ( {
24+ // Called each time a request is made.
25+ getProxyForUrl : ( url : string ) => {
26+ const cfg = vscode . workspace . getConfiguration ( )
27+ return getProxyForUrl ( url , cfg . get ( "http.proxy" ) , cfg . get ( "coder.proxyBypass" ) )
28+ } ,
29+ cert : certFile === "" ? undefined : await fs . readFile ( certFile ) ,
30+ key : keyFile === "" ? undefined : await fs . readFile ( keyFile ) ,
31+ ca : caFile === "" ? undefined : await fs . readFile ( caFile ) ,
32+ // rejectUnauthorized defaults to true, so we need to explicitly set it to
33+ // false if we want to allow self-signed certificates.
34+ rejectUnauthorized : ! insecure ,
35+ } )
36+ }
37+
38+ let agent : Promise < ProxyAgent > | undefined = undefined
39+ async function getHttpAgent ( ) : Promise < ProxyAgent > {
40+ if ( ! agent ) {
41+ vscode . workspace . onDidChangeConfiguration ( ( e ) => {
42+ if (
43+ // http.proxy and coder.proxyBypass are read each time a request is
44+ // made, so no need to watch them.
45+ e . affectsConfiguration ( "coder.insecure" ) ||
46+ e . affectsConfiguration ( "coder.tlsCertFile" ) ||
47+ e . affectsConfiguration ( "coder.tlsKeyFile" ) ||
48+ e . affectsConfiguration ( "coder.tlsCaFile" )
49+ ) {
50+ agent = createHttpAgent ( )
51+ }
52+ } )
53+ agent = createHttpAgent ( )
54+ }
55+ return agent
56+ }
57+
1658/**
1759 * Create an sdk instance using the provided URL and token and hook it up to
1860 * configuration. The token may be undefined if some other form of
@@ -31,25 +73,10 @@ export async function makeCoderSdk(baseUrl: string, token: string | undefined, s
3173 config . headers [ key ] = value
3274 } )
3375
34- const cfg = vscode . workspace . getConfiguration ( )
35- const insecure = Boolean ( cfg . get ( "coder.insecure" ) )
36- const certFile = expandPath ( String ( cfg . get ( "coder.tlsCertFile" ) ?? "" ) . trim ( ) )
37- const keyFile = expandPath ( String ( cfg . get ( "coder.tlsKeyFile" ) ?? "" ) . trim ( ) )
38- const caFile = expandPath ( String ( cfg . get ( "coder.tlsCaFile" ) ?? "" ) . trim ( ) )
39-
4076 // Configure proxy and TLS.
41- const agent = new ProxyAgent ( {
42- // If the proxy setting exists, we always use it. Otherwise we follow the
43- // standard environment variables (no_proxy, http_proxy, etc).
44- getProxyForUrl : ( url : string ) => cfg . get ( "http.proxy" ) || getProxyForUrl ( url ) ,
45- cert : certFile === "" ? undefined : await fs . readFile ( certFile ) ,
46- key : keyFile === "" ? undefined : await fs . readFile ( keyFile ) ,
47- ca : caFile === "" ? undefined : await fs . readFile ( caFile ) ,
48- // rejectUnauthorized defaults to true, so we need to explicitly set it to
49- // false if we want to allow self-signed certificates.
50- rejectUnauthorized : ! insecure ,
51- } )
52-
77+ // Note that by default VS Code overrides the agent. To prevent this, set
78+ // `http.proxySupport` to `on` or `off`.
79+ const agent = await getHttpAgent ( )
5380 config . httpsAgent = agent
5481 config . httpAgent = agent
5582
0 commit comments