-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat(proxy): macOS system proxy auto-discovery #5893
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
Closed
Closed
Changes from all commits
Commits
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
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
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,65 @@ | ||
| import { execFileSync } from "node:child_process"; | ||
| import { isIP } from "node:net"; | ||
|
|
||
| export type MacOSProxyReader = () => string | null; | ||
| type MacOSSystemProxyResult = | ||
| | { kind: "proxy"; httpUrl?: string; httpsUrl?: string; noProxy: string[] } | ||
| | { kind: "disabled" | "unreadable" }; | ||
|
|
||
| function readScutilProxy(): string { | ||
| return execFileSync("/usr/sbin/scutil", ["--proxy"], { | ||
| encoding: "utf8", | ||
| stdio: ["ignore", "pipe", "ignore"], | ||
| timeout: 2000, | ||
| maxBuffer: 64 * 1024, | ||
| }); | ||
| } | ||
|
|
||
| function proxyUrl(host: string | undefined, port: string | undefined): string | undefined { | ||
| if (!host || !port || !/^\d+$/.test(port) || +port < 1 || +port > 65535) return undefined; | ||
| const bareHost = host.startsWith("[") && host.endsWith("]") ? host.slice(1, -1) : host; | ||
| if (!isIP(bareHost) && !/^[a-z0-9](?:[a-z0-9.-]*[a-z0-9])?\.?$/i.test(host)) return undefined; | ||
| try { | ||
| const url = new URL(`http://${isIP(bareHost) === 6 ? `[${bareHost}]` : host}:${port}`); | ||
| return url.origin; | ||
| } catch { | ||
| return undefined; | ||
| } | ||
| } | ||
|
|
||
| /** Read only the effective top-level dictionary; scoped/supplemental proxies are not global. */ | ||
| export function readMacOSSystemProxy(reader: MacOSProxyReader = readScutilProxy): MacOSSystemProxyResult { | ||
| try { | ||
| const output = reader(); | ||
| if (!output || !/^\s*<dictionary>\s*\{/.test(output)) return { kind: "unreadable" }; | ||
| const values = new Map<string, string>(); | ||
| const noProxy: string[] = []; | ||
| let depth = 0; | ||
| let exceptions = false; | ||
| for (const row of output.split(/\r?\n/)) { | ||
| const line = row.trim(); | ||
| if (line.endsWith("{")) { | ||
| if (depth === 1) exceptions = /^ExceptionsList\s*:\s*<array>\s*\{$/.test(line); | ||
| depth++; | ||
| } else if (line === "}") { | ||
| if (--depth < 0) return { kind: "unreadable" }; | ||
| if (depth === 1) exceptions = false; | ||
| } else { | ||
| const entry = line.match(/^([^:]+)\s*:\s*(.*?)\s*$/); | ||
| if (!entry) continue; | ||
| if (depth === 1) values.set(entry[1]!.trim(), entry[2]!); | ||
| if (depth === 2 && exceptions && /^\d+$/.test(entry[1]!.trim())) { | ||
| const host = entry[2]!; | ||
| // Keep each exception one entry; never turn malformed output into additional bypasses. | ||
| if (host && !/[\s,{}]/.test(host)) noProxy.push(host); | ||
| } | ||
| } | ||
| } | ||
| if (depth !== 0) return { kind: "unreadable" }; | ||
| const httpUrl = values.get("HTTPEnable") === "1" ? proxyUrl(values.get("HTTPProxy"), values.get("HTTPPort")) : undefined; | ||
| const httpsUrl = values.get("HTTPSEnable") === "1" ? proxyUrl(values.get("HTTPSProxy"), values.get("HTTPSPort")) : undefined; | ||
| return httpUrl || httpsUrl ? { kind: "proxy", httpUrl, httpsUrl, noProxy } : { kind: "disabled" }; | ||
| } catch { | ||
| return { kind: "unreadable" }; | ||
| } | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: lidge-jun/opencodex
Length of output: 13981
🏁 Script executed:
Repository: lidge-jun/opencodex
Length of output: 13800
Qualify the no-
proxyenvironment-preservation claim in all seven translations.When
proxyis unset,applyProxyEnvWithpreserves inherited proxy variables. It can still add loopback entries toNO_PROXYfor an inherited SOCKS or HTTP(S) proxy, and may also update lowercaseno_proxy.The wording “the existing environment is preserved” implies that no environment variable changes. Replace it in:
docs-site/src/content/docs/fr/reference/configuration/server.md:276docs-site/src/content/docs/ja/reference/configuration/server.md:187docs-site/src/content/docs/ko/reference/configuration/server.md:246docs-site/src/content/docs/ru/reference/configuration/server.md:235docs-site/src/content/docs/tr/reference/configuration/server.md:307docs-site/src/content/docs/zh-cn/reference/configuration/server.md:201docs-site/src/content/docs/zh-tw/reference/configuration/server.md:220Use wording that limits preservation to inherited proxy variables and states that loopback entries may be added to
NO_PROXY. The English canonical page already uses this narrower meaning.🧰 Tools
🪛 LanguageTool
[typographical] ~276-~276: Caractère d’apostrophe incorrect.
Context: ... pas celui du terminal interactif. Sans
proxy, l’environnement existant est conservé ...(APOS_INCORRECT)
[style] ~276-~276: Cette structure peut être allégée afin de devenir plus percutante.
Context: ... et les changements à chaud ne sont pas pris en compte automatiquement. Un succès avec TUN ne ...
(PRENDRE_EN_COMPTE)
🤖 Prompt for AI Agents