-
Notifications
You must be signed in to change notification settings - Fork 43
fix: propagate proxy settings to SSH environment #1012
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a75a51a
fix: propagate proxy settings to SSH environment
EhabY 7b2e166
feat: warn when useLocalServer disables proxy propagation
EhabY 5c870da
refactor: extract DismissibleNotifier and harden proxy warning
EhabY 2da64c3
fix: pass proxy environment through to ssh
EhabY ad86f9a
feat: propagate proxy to terminal-mode SSH
EhabY b970419
Last round of review
EhabY File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| import { joinNoProxy } from "../api/proxy"; | ||
|
|
||
| import type { | ||
| GlobalEnvironmentVariableCollection, | ||
| WorkspaceConfiguration, | ||
| } from "vscode"; | ||
|
|
||
| type Environment = Record<string, string | undefined>; | ||
| type SshEnvironment = Partial< | ||
| Record<"HTTP_PROXY" | "HTTPS_PROXY" | "NO_PROXY", string> | ||
| >; | ||
|
|
||
| /** | ||
| * The settings {@link getSshProxyEnvironment} reads, paired with display titles. | ||
| * Watch these to prompt for a reload when the SSH proxy environment changes. | ||
| */ | ||
| export const SSH_PROXY_SETTINGS: ReadonlyArray<{ | ||
| setting: string; | ||
| title: string; | ||
| }> = [ | ||
| { setting: "http.proxy", title: "HTTP Proxy" }, | ||
| { setting: "http.noProxy", title: "HTTP No Proxy" }, | ||
| { setting: "coder.proxyBypass", title: "Proxy Bypass" }, | ||
| ]; | ||
|
|
||
| /** | ||
| * Apply the SSH environment that the spawned `coder ssh` ProxyCommand inherits. | ||
| * Currently just the proxy config (HTTP_PROXY/HTTPS_PROXY/NO_PROXY), read by the | ||
| * coder CLI like any Go HTTP client. Applied via both process.env (ssh spawned as | ||
| * a child, `remote.SSH.useLocalServer=true`) and the terminal env collection (ssh | ||
| * spawned in a terminal, `useLocalServer=false`, which can't see process.env), | ||
| * since the mode isn't knowable up front. Mutating env rather than the SSH config | ||
| * keeps credentialed URLs off disk and windows independent. Disposable restores | ||
| * both. | ||
| */ | ||
| export function applySshEnvironment( | ||
| cfg: Pick<WorkspaceConfiguration, "get">, | ||
| collection: Pick< | ||
| GlobalEnvironmentVariableCollection, | ||
| "persistent" | "replace" | "clear" | ||
| >, | ||
| env: Environment = process.env, | ||
| ): { dispose(): void } { | ||
| const values = getSshProxyEnvironment(cfg); | ||
| const restoreEnv = applyEnvironment(values, env); | ||
|
|
||
| collection.persistent = false; | ||
| // Drop stale vars from a prior connect (e.g. NO_PROXY set last time, not now). | ||
| collection.clear(); | ||
| for (const [key, value] of Object.entries(values)) { | ||
| if (value) { | ||
| collection.replace(key, value); | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| dispose() { | ||
| restoreEnv.dispose(); | ||
| collection.clear(); | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| /** The proxy portion of the SSH environment, derived from VS Code's settings. */ | ||
| export function getSshProxyEnvironment( | ||
| cfg: Pick<WorkspaceConfiguration, "get">, | ||
| ): SshEnvironment { | ||
| const httpProxy = trimmed(cfg.get<string | null>("http.proxy")); | ||
| const noProxy = | ||
| trimmed(cfg.get<string | null>("coder.proxyBypass")) ?? | ||
| joinNoProxy(cfg.get<string[]>("http.noProxy")); | ||
|
|
||
| return { | ||
| HTTP_PROXY: httpProxy, | ||
| HTTPS_PROXY: httpProxy, | ||
| NO_PROXY: noProxy, | ||
| }; | ||
| } | ||
|
|
||
| function applyEnvironment( | ||
| values: SshEnvironment, | ||
| env: Environment, | ||
| ): { dispose(): void } { | ||
| // Stored `undefined` means the key was absent and should be deleted on cleanup. | ||
| const previous: Environment = {}; | ||
| for (const [key, value] of Object.entries(values)) { | ||
| if (value === undefined) { | ||
| continue; | ||
| } | ||
| previous[key] = env[key]; | ||
| env[key] = value; | ||
| } | ||
|
|
||
| let disposed = false; | ||
| return { | ||
| dispose: () => { | ||
| if (disposed) { | ||
| return; | ||
| } | ||
| disposed = true; | ||
| for (const [key, value] of Object.entries(previous)) { | ||
| if (value === undefined) { | ||
| delete env[key]; | ||
| } else { | ||
| env[key] = value; | ||
| } | ||
| } | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| function trimmed(value: string | null | undefined): string | undefined { | ||
| return typeof value === "string" ? value.trim() || undefined : undefined; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.