@@ -2,17 +2,59 @@ import { Api } from "coder/site/src/api/api"
2
2
import fs from "fs/promises"
3
3
import * as os from "os"
4
4
import { ProxyAgent } from "proxy-agent"
5
- import { getProxyForUrl } from "proxy-from-env"
6
5
import * as vscode from "vscode"
7
6
import { CertificateError } from "./error"
7
+ import { getProxyForUrl } from "./proxy"
8
8
import { Storage } from "./storage"
9
9
10
10
// expandPath will expand ${userHome} in the input string.
11
- const expandPath = ( input : string ) : string => {
11
+ function expandPath ( input : string ) : string {
12
12
const userHome = os . homedir ( )
13
13
return input . replace ( / \$ { userHome} / g, userHome )
14
14
}
15
15
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
+
16
58
/**
17
59
* Create an sdk instance using the provided URL and token and hook it up to
18
60
* 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
31
73
config . headers [ key ] = value
32
74
} )
33
75
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
-
40
76
// 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 ( )
53
80
config . httpsAgent = agent
54
81
config . httpAgent = agent
55
82
0 commit comments