Files
ShadowsocksX-NG/ShadowsocksX-NG2/App/ProxyRuntimeFacts.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

129 lines
3.8 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
/// Stable runtime status categories exposed to app presentation surfaces.
/// 系统代理结果不在此列:它有独立的 `SystemProxyApplicationFacts`
/// (issue #60 拆分两个状态面)。
enum ProxyRuntimeStatus: Equatable, Sendable {
case off
case starting
case running
case firewallBlocked
case launchFailed
case requiresApproval
case serviceFailed
}
/// Domain-neutral runtime projection for status-menu presentation. The
/// controller's state representation stays behind the adapter below.
struct ProxyRuntimeFacts: Equatable, Sendable {
let status: ProxyRuntimeStatus
let isOn: Bool
let failure: RuntimeFailureFacts?
init(
status: ProxyRuntimeStatus,
isOn: Bool,
failure: RuntimeFailureFacts? = nil
) {
self.status = status
self.isOn = isOn
self.failure = failure
}
}
/// 系统代理实际作用的固定类别(issue #60):意图持久化为开关事实,这里
/// 呈现 NG2 对系统设置的真实作用,与 agent 运行状态互不代替。
enum SystemProxyApplicationFacts: Equatable, Sendable {
/// 意图关闭:系统代理清理完成。
case idle
/// 意图开启,等待 agent 健康或可用出口;条件恢复后自动收敛。
case pending
/// 已应用系统代理配置。
case applied
/// 写入或恢复失败(typed)。
case failed(SystemProxyFailureFacts)
}
/// Safe endpoint health categories used by runtime state and presentation.
enum RuntimeEndpointFailure: Equatable, Sendable {
case refused
case timedOut
case invalidResponse
case unknown
}
struct FirewallBlockedFacts: Equatable, Sendable {
let executableName: String
}
enum LaunchFailureFacts: Equatable, Sendable {
case missingRuntimeDocument
case localEndpoint(
endpoint: String, host: String, port: Int, cause: RuntimeEndpointFailure)
case unreadableSettings
}
enum ServiceFailureFacts: Equatable, Sendable {
case runtimeFile
case agent
case missingDocument
case persistence
case unknown
}
enum SystemProxyOperationFailure: Equatable, Sendable {
case preferencesUnavailable
case preferencesBusy
case noCurrentNetworkSet
case noNetworkLocations
case noProxyServices
case unreadableService
case invalidStoredConfiguration
case cannotWriteService
case commitFailed
case applyFailed
case invalidRequest
case helperUnavailable
}
enum SystemProxyFailureFacts: Equatable, Sendable {
case operation(SystemProxyOperationFailure)
case mode(ProxyModeError)
case unknown
}
/// Structured runtime failure projection used by runtime synchronization and
/// app presentation. No arbitrary platform detail is carried into this state.
/// `.systemProxy` 只用于把系统代理失败携带进结构化 outcome(设置工作流与
/// 目录收敛结果);呈现面走 `SystemProxyApplicationFacts.failed`。
enum RuntimeFailureFacts: Error, Equatable, Sendable {
case firewallBlocked(FirewallBlockedFacts)
case launch(LaunchFailureFacts)
case service(ServiceFailureFacts)
case activation(ActivationFailure)
case requiresApproval
case systemProxy(SystemProxyFailureFacts)
}
extension ProxyRuntimeFacts {
init(state: ProxyRuntimeController.AgentRunState) {
switch state {
case .off:
self.init(status: .off, isOn: false)
case .starting:
self.init(status: .starting, isOn: true)
case .running:
self.init(status: .running, isOn: true)
case .firewallBlocked(let facts):
self.init(
status: .firewallBlocked, isOn: true, failure: .firewallBlocked(facts))
case .launchFailed(let facts):
self.init(status: .launchFailed, isOn: false, failure: .launch(facts))
case .requiresApproval:
self.init(status: .requiresApproval, isOn: true, failure: .requiresApproval)
case .serviceFailed(let facts):
self.init(status: .serviceFailed, isOn: false, failure: .service(facts))
}
}
}