-
Notifications
You must be signed in to change notification settings - Fork 9
feat: implement proxy support CF-2437 #196
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
Open
nedaKaighobadi
wants to merge
6
commits into
main
Choose a base branch
from
add-proxy-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f9c639e
implement proxy
nedaKaighobadi 84c8713
fix proxy for http connection
nedaKaighobadi ef37153
add test for the proxy config
nedaKaighobadi 8553e39
implement suggestions
nedaKaighobadi 7852baf
implement no_proxy functionality
nedaKaighobadi e8d70d6
remove redundant checks
nedaKaighobadi 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,131 @@ | ||
| import * as vscode from 'vscode' | ||
| import * as tunnel from 'tunnel' | ||
| import { HttpProxyAgent } from 'http-proxy-agent' | ||
| import axios from 'axios' | ||
| import type { HTTPClient, HTTPClientRequest, HTTPResponse } from '@segment/analytics-node' | ||
|
|
||
| function resolveProxyUrl(): string | undefined { | ||
| // VS Code setting takes precedence; an empty string means "no proxy" | ||
| const vscodeProxy = vscode.workspace.getConfiguration('http').get<string>('proxy') | ||
| if (vscodeProxy !== undefined) { | ||
| return vscodeProxy | ||
| } | ||
|
|
||
| return process.env.HTTPS_PROXY || process.env.https_proxy || process.env.HTTP_PROXY || process.env.http_proxy | ||
| } | ||
|
|
||
| function resolveStrictSSL(): boolean { | ||
| return vscode.workspace.getConfiguration('http').get<boolean>('proxyStrictSSL', true) | ||
| } | ||
|
|
||
| function resolveProxyAuthorization(): string | undefined { | ||
| return vscode.workspace.getConfiguration('http').get<string | null>('proxyAuthorization') ?? undefined | ||
| } | ||
|
|
||
| function resolveNoProxy(): string[] { | ||
| const raw = process.env.NO_PROXY || process.env.no_proxy || '' | ||
| return raw | ||
| .split(',') | ||
| .map((s) => s.trim()) | ||
| .filter(Boolean) | ||
| } | ||
|
|
||
| function hostMatchesNoProxy(hostname: string, entry: string): boolean { | ||
| if (entry === '*') return true | ||
| const normalized = entry.startsWith('.') ? entry.slice(1) : entry | ||
| return hostname === normalized || hostname.endsWith('.' + normalized) | ||
| } | ||
|
|
||
| function shouldBypassProxy(url: string, noProxyList: string[]): boolean { | ||
| try { | ||
| const { hostname } = new URL(url) | ||
| return noProxyList.some((entry) => hostMatchesNoProxy(hostname, entry)) | ||
| } catch { | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| // Ensures the URL has a protocol so `new URL()` doesn't throw for bare host:port strings | ||
| function normalizeProxyUrl(url: string): string { | ||
| return url.includes('://') ? url : `http://${url}` | ||
| } | ||
|
|
||
| let noProxyInterceptorId: number | null = null | ||
|
|
||
| /** | ||
| * Applies proxy settings from VS Code config and environment variables to the global axios instance | ||
| */ | ||
| export function configureAxiosProxy(): void { | ||
| if (noProxyInterceptorId !== null) { | ||
| axios.interceptors.request.eject(noProxyInterceptorId) | ||
| noProxyInterceptorId = null | ||
| } | ||
|
|
||
| const proxyUrl = resolveProxyUrl() | ||
|
|
||
| if (proxyUrl) { | ||
| const strictSSL = resolveStrictSSL() | ||
| const authHeader = resolveProxyAuthorization() | ||
|
|
||
| const normalizedUrl = normalizeProxyUrl(proxyUrl) | ||
| const parsed = new URL(normalizedUrl) | ||
| const proxyHost = parsed.hostname | ||
| const proxyPort = parseInt(parsed.port || (parsed.protocol === 'https:' ? '443' : '80'), 10) | ||
| const proxyHeaders = authHeader ? { 'Proxy-Authorization': authHeader } : undefined | ||
| const proxyOpts = { host: proxyHost, port: proxyPort, ...(proxyHeaders ? { headers: proxyHeaders } : {}) } | ||
|
|
||
| // Branch on the proxy protocol so TLS proxies work too. | ||
| if (parsed.protocol === 'https:') { | ||
| axios.defaults.httpsAgent = tunnel.httpsOverHttps({ rejectUnauthorized: strictSSL, proxy: proxyOpts }) | ||
| axios.defaults.httpAgent = tunnel.httpOverHttps({ proxy: proxyOpts }) | ||
| } else { | ||
| axios.defaults.httpsAgent = tunnel.httpsOverHttp({ rejectUnauthorized: strictSSL, proxy: proxyOpts }) | ||
| axios.defaults.httpAgent = new HttpProxyAgent(normalizedUrl) | ||
| } | ||
| // Disable axios's built-in proxy parsing so the agents take full control | ||
| axios.defaults.proxy = false | ||
|
|
||
| // Per-connection rejectUnauthorized on the tunnel agent is not sufficient | ||
| // in all Node.js versions to suppress TLS errors from an intercepting | ||
| // proxy cert. Mirror the VS Code proxyStrictSSL setting via the | ||
| // process-level flag so it is reliably honoured. | ||
| if (!strictSSL) { | ||
| process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0' | ||
|
nedaKaighobadi marked this conversation as resolved.
|
||
| } else { | ||
| delete process.env['NODE_TLS_REJECT_UNAUTHORIZED'] | ||
| } | ||
|
|
||
| const noProxyList = resolveNoProxy() | ||
| if (noProxyList.length > 0) { | ||
| noProxyInterceptorId = axios.interceptors.request.use((config) => { | ||
| if (config.url && shouldBypassProxy(config.url, noProxyList)) { | ||
| config.httpsAgent = undefined | ||
| config.httpAgent = undefined | ||
| config.proxy = undefined | ||
| } | ||
| return config | ||
| }) | ||
| } | ||
| } else { | ||
| axios.defaults.httpsAgent = undefined | ||
| axios.defaults.httpAgent = undefined | ||
| axios.defaults.proxy = undefined | ||
| // Restore TLS verification when proxy is removed or strictSSL is re-enabled | ||
| delete process.env['NODE_TLS_REJECT_UNAUTHORIZED'] | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * An HTTPClient implementation for @segment/analytics-node that routes | ||
| * requests through the proxy configured in axios defaults (if any). | ||
| */ | ||
| export class ProxiedSegmentHTTPClient implements HTTPClient { | ||
| async makeRequest(options: HTTPClientRequest): Promise<HTTPResponse> { | ||
| const response = await axios.post(options.url, options.data, { | ||
| headers: options.headers, | ||
| timeout: options.httpRequestTimeout, | ||
| validateStatus: () => true, | ||
| }) | ||
| return { status: response.status, statusText: response.statusText } | ||
|
nedaKaighobadi marked this conversation as resolved.
|
||
| } | ||
|
nedaKaighobadi marked this conversation as resolved.
|
||
| } | ||
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
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.