Files
ShadowsocksX-NG/ShadowsocksX-NG2/Domain/ProxyMode.swift
T
Qiu Yuzhou d2a8d6105c feat(system-proxy): route writes through privileged LaunchDaemon helper
Implements issue #71 with ADR-0020 (supersedes ADR-0019's endpoint-signature
cleanup, startup cleanup, and health-gate cleanup).

- New ShadowsocksX-NG2SystemProxyHelper target: app-bundled root LaunchDaemon
  (SMAppService.daemon, on-demand via MachServices) that mechanically applies
  a closed typed configuration or executes an unconditional clear. Clients are
  accepted by code-signing bundle identifier only; requests run serially on a
  shared queue (last-writer-wins).
- SystemProxyConfiguration is now the closed typed apply payload (Codable over
  JSON XPC): explicit SOCKS/HTTP/HTTPS enable + endpoints, PAC and
  auto-discovery disabled with URL/JS removed, explicit simple-hostname
  exclusion, complete ExceptionsList. Unmodeled Proxies keys are preserved and
  equivalent values skip the commit.
- Cleanup is a side effect of a persisted system-proxy intent on-to-off
  transition only: the switch itself, or the agent-off cascade (persist both
  intents off, clear, then stop listeners; failed cascade persistence records
  no transition and clears nothing). Launch with the switch off, health-gate
  closure, invalid targets, and passive events never clear. Failed clears are
  surfaced without deferred retry.
- Endpoint-signature store, ownership migration, and direct Authorization
  Services writes are removed; old signature files stay inert. Helper
  unavailability or pending approval keeps an enabled intent pending and
  surfaces the Login Items approval path (HomeView button + menu label).
2026-09-30 12:26:40 +08:00

104 lines
3.1 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import Foundation
/// Persisted identity of a proxy mode. PAC is gone (issue #67): the product
/// exposes rule / global / direct only.
enum ProxyModeKind: String, Codable, CaseIterable, Equatable, Hashable, Sendable {
case rule
case global
case direct
}
/// The mutually exclusive ways in which 2.0 exposes the local proxy to macOS.
/// Hashable 供菜单栏模式选择的勾选态 Picker 使用(issue #31)。
/// 规则模式(issue #63)另持久化 `RuleDefaultAction` 子选项。
enum ProxyMode: Codable, Equatable, Hashable, Sendable {
case rule
case global
case direct
private enum CodingKeys: String, CodingKey {
case kind
}
private enum Kind: String, Codable {
case rule
case global
case direct
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
switch try container.decode(Kind.self, forKey: .kind) {
case .rule:
self = .rule
case .global:
self = .global
case .direct:
self = .direct
}
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
switch self {
case .rule:
try container.encode(Kind.rule, forKey: .kind)
case .global:
try container.encode(Kind.global, forKey: .kind)
case .direct:
try container.encode(Kind.direct, forKey: .kind)
}
}
var label: String {
switch self {
case .rule:
"规则"
case .global:
"全局"
case .direct:
"直连"
}
}
var kind: ProxyModeKind {
switch self {
case .rule: .rule
case .global: .global
case .direct: .direct
}
}
/// Returns every mode supported by the product in stable selector order.
static var availableModes: [ProxyMode] { [.rule, .global, .direct] }
/// Derives the only system-proxy state that this mode is allowed to own.
/// All three modes are projected as the local SOCKS and HTTP inbounds
/// (issue #59/#67):SOCKS 之外,系统 HTTP/HTTPS 代理指向恒开启的 HTTP 入站。
/// 投影即 closed typed 配置(issue #71):HTTPS 显式赋值、PAC/自动发现显式
/// 关闭、简单主机名排除显式开启、例外列表全量给出。
func systemProxyConfiguration(
for document: SslocalRuntimeDocument,
exceptions: [String] = []
) throws -> SystemProxyConfiguration {
guard ProxyPortRange.valid.contains(document.socksPort) else {
throw ProxyModeError.invalidSOCKSPort(document.socksPort)
}
guard ProxyPortRange.valid.contains(document.httpPort) else {
throw ProxyModeError.invalidHTTPPort(document.httpPort)
}
let loopback = document.listen.listenerMode.proxyLoopbackAddress
return SystemProxyConfiguration(
socks: .init(host: loopback, port: document.socksPort),
http: .init(host: loopback, port: document.httpPort),
https: .init(host: loopback, port: document.httpPort),
excludeSimpleHostnames: true,
exceptions: FixedLocalProxyRanges.systemProxyExceptions(including: exceptions))
}
}
enum ProxyModeError: Error, Equatable, Sendable {
case invalidSOCKSPort(Int)
case invalidHTTPPort(Int)
}