Skip to content

Commit 6bb5dab

Browse files
committed
Add HTTP proxy support
1 parent d28be47 commit 6bb5dab

File tree

4 files changed

+191
-7
lines changed

4 files changed

+191
-7
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
- Workspace and agent statuses now show in the sidebar. These are updated every
88
five seconds.
9+
- Support http.proxy setting and proxy environment variables.
910

1011
## [v1.0.2](https://github.com/coder/vscode-coder/releases/tag/v1.0.2) (2024-06-12)
1112

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@
249249
"@types/glob": "^7.1.3",
250250
"@types/ndjson": "^2.0.1",
251251
"@types/node": "^18.0.0",
252+
"@types/proxy-from-env": "^1.0.4",
252253
"@types/vscode": "^1.73.0",
253254
"@types/which": "^2.0.1",
254255
"@types/ws": "^8.5.10",
@@ -267,6 +268,7 @@
267268
"glob": "^7.1.6",
268269
"nyc": "^15.1.0",
269270
"prettier": "^3.2.5",
271+
"proxy-agent": "^6.4.0",
270272
"ts-loader": "^9.5.1",
271273
"tsc-watch": "^6.2.0",
272274
"typescript": "^5.4.5",
@@ -289,6 +291,7 @@
289291
"ndjson": "^2.0.0",
290292
"node-forge": "^1.3.1",
291293
"pretty-bytes": "^6.0.0",
294+
"proxy-from-env": "^1.1.0",
292295
"semver": "^7.6.0",
293296
"tar-fs": "^2.1.1",
294297
"ua-parser-js": "^1.0.37",

src/api.ts

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { Api } from "coder/site/src/api/api"
22
import fs from "fs/promises"
3-
import * as https from "https"
43
import * as os from "os"
4+
import { ProxyAgent } from "proxy-agent"
5+
import { getProxyForUrl } from "proxy-from-env"
56
import * as vscode from "vscode"
67
import { CertificateError } from "./error"
78
import { Storage } from "./storage"
@@ -30,22 +31,28 @@ export async function makeCoderSdk(baseUrl: string, token: string | undefined, s
3031
config.headers[key] = value
3132
})
3233

33-
// Configure TLS.
3434
const cfg = vscode.workspace.getConfiguration()
3535
const insecure = Boolean(cfg.get("coder.insecure"))
3636
const certFile = expandPath(String(cfg.get("coder.tlsCertFile") ?? "").trim())
3737
const keyFile = expandPath(String(cfg.get("coder.tlsKeyFile") ?? "").trim())
3838
const caFile = expandPath(String(cfg.get("coder.tlsCaFile") ?? "").trim())
3939

40-
config.httpsAgent = new https.Agent({
40+
// 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),
4145
cert: certFile === "" ? undefined : await fs.readFile(certFile),
4246
key: keyFile === "" ? undefined : await fs.readFile(keyFile),
4347
ca: caFile === "" ? undefined : await fs.readFile(caFile),
44-
// rejectUnauthorized defaults to true, so we need to explicitly set it to false
45-
// if we want to allow self-signed certificates.
48+
// rejectUnauthorized defaults to true, so we need to explicitly set it to
49+
// false if we want to allow self-signed certificates.
4650
rejectUnauthorized: !insecure,
4751
})
4852

53+
config.httpsAgent = agent
54+
config.httpAgent = agent
55+
4956
return config
5057
})
5158

0 commit comments

Comments
 (0)