Skip to content

Commit

Permalink
Add HTTP proxy support
Browse files Browse the repository at this point in the history
  • Loading branch information
code-asher committed Jun 17, 2024
1 parent d28be47 commit 6bb5dab
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

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

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

Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@
"@types/glob": "^7.1.3",
"@types/ndjson": "^2.0.1",
"@types/node": "^18.0.0",
"@types/proxy-from-env": "^1.0.4",
"@types/vscode": "^1.73.0",
"@types/which": "^2.0.1",
"@types/ws": "^8.5.10",
Expand All @@ -267,6 +268,7 @@
"glob": "^7.1.6",
"nyc": "^15.1.0",
"prettier": "^3.2.5",
"proxy-agent": "^6.4.0",
"ts-loader": "^9.5.1",
"tsc-watch": "^6.2.0",
"typescript": "^5.4.5",
Expand All @@ -289,6 +291,7 @@
"ndjson": "^2.0.0",
"node-forge": "^1.3.1",
"pretty-bytes": "^6.0.0",
"proxy-from-env": "^1.1.0",
"semver": "^7.6.0",
"tar-fs": "^2.1.1",
"ua-parser-js": "^1.0.37",
Expand Down
17 changes: 12 additions & 5 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Api } from "coder/site/src/api/api"
import fs from "fs/promises"
import * as https from "https"
import * as os from "os"
import { ProxyAgent } from "proxy-agent"
import { getProxyForUrl } from "proxy-from-env"
import * as vscode from "vscode"
import { CertificateError } from "./error"
import { Storage } from "./storage"
Expand Down Expand Up @@ -30,22 +31,28 @@ export async function makeCoderSdk(baseUrl: string, token: string | undefined, s
config.headers[key] = value
})

// Configure TLS.
const cfg = vscode.workspace.getConfiguration()
const insecure = Boolean(cfg.get("coder.insecure"))
const certFile = expandPath(String(cfg.get("coder.tlsCertFile") ?? "").trim())
const keyFile = expandPath(String(cfg.get("coder.tlsKeyFile") ?? "").trim())
const caFile = expandPath(String(cfg.get("coder.tlsCaFile") ?? "").trim())

config.httpsAgent = new https.Agent({
// Configure proxy and TLS.
const agent = new ProxyAgent({
// If the proxy setting exists, we always use it. Otherwise we follow the
// standard environment variables (no_proxy, http_proxy, etc).
getProxyForUrl: (url: string) => cfg.get("http.proxy") || getProxyForUrl(url),
cert: certFile === "" ? undefined : await fs.readFile(certFile),
key: keyFile === "" ? undefined : await fs.readFile(keyFile),
ca: caFile === "" ? undefined : await fs.readFile(caFile),
// rejectUnauthorized defaults to true, so we need to explicitly set it to false
// if we want to allow self-signed certificates.
// rejectUnauthorized defaults to true, so we need to explicitly set it to
// false if we want to allow self-signed certificates.
rejectUnauthorized: !insecure,
})

config.httpsAgent = agent
config.httpAgent = agent

return config
})

Expand Down
Loading

0 comments on commit 6bb5dab

Please sign in to comment.