Files
ShadowsocksX-NG/ShadowsocksX-NG2/App/ErrorPresentation+Runtime.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

95 lines
4.2 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
// MARK: - 运行时失败事实的呈现(RuntimeFailureFacts / AgentRunState / 系统代理应用态)
extension AppPresentation {
static func message(for failure: RuntimeFailureFacts) -> String {
switch failure {
case .firewallBlocked(let facts):
return
"macOS 防火墙已阻止 \(facts.executableName) 接受传入连接。请在系统设置中允许传入连接。"
case .launch(let facts): return launchFailure(facts)
case .service(let facts): return serviceFailure(facts)
case .activation(let error): return activation(error)
case .requiresApproval: return "请在系统设置的登录项中允许代理后台服务"
case .systemProxy(let facts): return systemProxyFailure(facts)
}
}
static func message(for state: ProxyRuntimeController.AgentRunState) -> String {
switch state {
case .off: return "代理未运行"
case .starting: return "代理正在启动"
case .running: return "代理运行中"
case .firewallBlocked(let facts):
return message(for: RuntimeFailureFacts.firewallBlocked(facts))
case .launchFailed(let facts): return message(for: RuntimeFailureFacts.launch(facts))
case .requiresApproval: return message(for: RuntimeFailureFacts.requiresApproval)
case .serviceFailed(let facts): return message(for: RuntimeFailureFacts.service(facts))
}
}
static func message(for application: SystemProxyApplicationFacts) -> String {
switch application {
case .idle: return "系统代理未应用"
case .pending: return "系统代理待应用:等待代理就绪或可用出口"
case .applied: return "系统代理已应用"
case .failed(let facts): return systemProxyFailure(facts)
}
}
private static func launchFailure(_ facts: LaunchFailureFacts) -> String {
switch facts {
case .missingRuntimeDocument: return "缺少运行时文档"
case .localEndpoint(let endpoint, let host, let port, let cause):
return "本地代理 \(endpoint.uppercased()) 端点 \(host):\(port) 未就绪(\(endpointFailure(cause)))"
case .unreadableSettings: return "本地代理配置无法读取,已停止代理以避免静默改用出厂端口"
}
}
private static func serviceFailure(_ facts: ServiceFailureFacts) -> String {
switch facts {
case .runtimeFile: return "运行时文件读写失败"
case .agent: return "代理后台服务管理失败"
case .missingDocument: return "缺少运行时文档"
case .persistence: return "代理运行状态保存失败"
case .unknown: return unknownError
}
}
private static func systemProxyFailure(_ facts: SystemProxyFailureFacts) -> String {
switch facts {
case .operation(let failure): return systemProxy(failure)
case .mode(let error): return proxyMode(error)
case .unknown: return "系统代理未能应用"
}
}
// swiftlint:disable:next cyclomatic_complexity
private static func systemProxy(_ failure: SystemProxyOperationFailure) -> String {
switch failure {
case .preferencesUnavailable: return "系统网络配置不可用"
case .preferencesBusy: return "系统网络配置正被其他设置操作占用"
case .noCurrentNetworkSet: return "没有当前网络位置"
case .noNetworkLocations: return "没有可检查的网络位置"
case .noProxyServices: return "当前网络位置没有可写入的网络服务"
case .unreadableService: return "无法读取网络服务的代理配置"
case .invalidStoredConfiguration: return "保存的系统代理配置无效"
case .cannotWriteService: return "无法写入网络服务的代理配置"
case .commitFailed: return "系统代理提交失败"
case .applyFailed: return "系统代理应用失败"
case .invalidRequest: return "系统代理助手收到了无效请求"
case .helperUnavailable: return "系统代理助手不可用;请确认已在登录项中允许"
}
}
private static func endpointFailure(_ outcome: RuntimeEndpointFailure) -> String {
switch outcome {
case .refused: return "连接被拒绝"
case .timedOut: return "连接超时"
case .invalidResponse: return "响应无效"
case .unknown: return "状态无法确定"
}
}
}