-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat: add global network proxy support #1706
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
+345
−4
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import CodexBarCore | ||
| import Foundation | ||
|
|
||
| /// Bridges the app-level proxy settings into ``ProviderHTTPClient`` so all provider traffic honors them. | ||
| @MainActor | ||
| enum ProxyConfigurator { | ||
| private static var applied: ProxyConfiguration? | ||
| private static var hasApplied = false | ||
|
|
||
| /// Resolves the configured proxy, or `nil` when disabled / empty / invalid. | ||
| static func resolve(from settings: SettingsStore) -> ProxyConfiguration? { | ||
| guard settings.proxyEnabled else { return nil } | ||
| let trimmed = settings.proxyURL.trimmingCharacters(in: .whitespacesAndNewlines) | ||
| guard !trimmed.isEmpty else { return nil } | ||
| return try? ProxyConfiguration.parse(from: trimmed) | ||
| } | ||
|
|
||
| /// Applies the current settings to the shared HTTP client. | ||
| /// | ||
| /// Safe to call liberally (focus loss, submit, disappear): the session is only rebuilt when the | ||
| /// resolved configuration actually changes. | ||
| static func apply(from settings: SettingsStore) { | ||
| let config = self.resolve(from: settings) | ||
| guard !self.hasApplied || config != self.applied else { return } | ||
| self.applied = config | ||
| self.hasApplied = true | ||
| ProviderHTTPClient.shared.applyProxyConfiguration(config) | ||
| } | ||
| } |
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
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,110 @@ | ||
| import Foundation | ||
| #if canImport(FoundationNetworking) | ||
| import FoundationNetworking | ||
| #endif | ||
|
|
||
| /// A parsed, validated outbound proxy configuration applied globally to ``ProviderHTTPClient``. | ||
| /// | ||
| /// Authentication is intentionally unsupported: credentials embedded in the URL are ignored. | ||
| public struct ProxyConfiguration: Sendable, Equatable { | ||
| public enum ProxyType: Sendable, Equatable { | ||
| case http | ||
| case socks | ||
| } | ||
|
|
||
| public let type: ProxyType | ||
| public let host: String | ||
| public let port: Int | ||
|
|
||
| public init(type: ProxyType, host: String, port: Int) { | ||
| self.type = type | ||
| self.host = host | ||
| self.port = port | ||
| } | ||
|
|
||
| /// Parses a proxy URL such as `http://127.0.0.1:8080` or `socks5://127.0.0.1:1080`. | ||
| /// Any user-info component is ignored — proxy authentication is not supported. | ||
| public static func parse(from urlString: String) throws -> ProxyConfiguration { | ||
| let trimmed = urlString.trimmingCharacters(in: .whitespacesAndNewlines) | ||
| guard !trimmed.isEmpty else { throw ProxyConfigurationError.empty } | ||
|
|
||
| guard let components = URLComponents(string: trimmed), | ||
| let scheme = components.scheme?.lowercased(), !scheme.isEmpty | ||
| else { | ||
| throw ProxyConfigurationError.badScheme("") | ||
| } | ||
|
|
||
| let type: ProxyType | ||
| let defaultPort: Int | ||
| switch scheme { | ||
| case "http", "https": | ||
| type = .http | ||
| defaultPort = 8080 | ||
| case "socks", "socks5": | ||
| type = .socks | ||
| defaultPort = 1080 | ||
| default: | ||
| throw ProxyConfigurationError.badScheme(scheme) | ||
| } | ||
|
|
||
| guard let host = components.host, !host.isEmpty else { | ||
| throw ProxyConfigurationError.badHost | ||
| } | ||
|
|
||
| let port = components.port ?? defaultPort | ||
| guard (1...65535).contains(port) else { throw ProxyConfigurationError.badPort } | ||
|
|
||
| return ProxyConfiguration(type: type, host: host, port: port) | ||
| } | ||
|
|
||
| /// The `URLSessionConfiguration.connectionProxyDictionary` representation. | ||
| /// | ||
| /// For an HTTP proxy both the HTTP and HTTPS keys are set to the same host/port, because nearly all | ||
| /// provider endpoints are `https://`. | ||
| public func connectionProxyDictionary() -> [AnyHashable: Any] { | ||
| #if os(Linux) | ||
| // Linux/CLI relies on http_proxy/https_proxy environment variables instead. | ||
| return [:] | ||
| #else | ||
| switch self.type { | ||
| case .http: | ||
| return [ | ||
| kCFNetworkProxiesHTTPEnable as String: true, | ||
| kCFNetworkProxiesHTTPProxy as String: self.host, | ||
| kCFNetworkProxiesHTTPPort as String: self.port, | ||
| kCFNetworkProxiesHTTPSEnable as String: true, | ||
| kCFNetworkProxiesHTTPSProxy as String: self.host, | ||
| kCFNetworkProxiesHTTPSPort as String: self.port, | ||
| ] | ||
| case .socks: | ||
| return [ | ||
| kCFNetworkProxiesSOCKSEnable as String: true, | ||
| kCFNetworkProxiesSOCKSProxy as String: self.host, | ||
| kCFNetworkProxiesSOCKSPort as String: self.port, | ||
| ] | ||
| } | ||
| #endif | ||
| } | ||
| } | ||
|
|
||
| public enum ProxyConfigurationError: LocalizedError, Equatable { | ||
| case empty | ||
| case badScheme(String) | ||
| case badHost | ||
| case badPort | ||
|
|
||
| public var errorDescription: String? { | ||
| switch self { | ||
| case .empty: | ||
| "Proxy URL is empty." | ||
| case let .badScheme(scheme): | ||
| scheme.isEmpty | ||
| ? "Proxy URL is missing a scheme. Use http://, https://, or socks5://." | ||
| : "Unsupported proxy scheme “\(scheme)”. Use http://, https://, or socks5://." | ||
| case .badHost: | ||
| "Proxy URL is missing a valid host." | ||
| case .badPort: | ||
| "Proxy URL has an invalid port." | ||
| } | ||
| } | ||
| } |
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.
With the switch already enabled, editing this field only persists
settings.proxyURL; the activeProviderHTTPClientis rebuilt only if the user presses Return. In the common flow of enabling the checkbox, typing a URL, then closing preferences or tabbing/clicking away, traffic continues using the previous/direct session even though the UI shows the new enabled proxy URL.Useful? React with 👍 / 👎.
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.
fixed in commit 10c153e