import Foundation /// GUI-owned runtime and system-proxy manager. Agent and system-proxy switches /// persist independent intentions. System proxy writes use the privileged helper; /// current configuration is observed read-only and differs from write outcomes. /// Health/exit loss suspends system proxy settings, real recovery reapplies, and /// passive network events only inspect (ADR-0022 / issue #73). GUI exit ends these /// observations without stopping the LaunchAgent or adding helper responsibilities. @MainActor final class ProxyRuntimeController: ObservableObject { /// Agent(后台代理运行时)运行状态。系统代理结果不在此面呈现——它有 /// 独立的 `SystemProxyControlState`。 enum AgentRunState: Equatable { case off case starting case running /// 代理在本机运行,但主机地址态的入站被 macOS 防火墙拒绝。 case firewallBlocked(FirewallBlockedFacts) /// 启动失败:携带点名端点与端口的事实(D8;端口语义细节 #30 接线)。 case launchFailed(LaunchFailureFacts) /// 需要用户在系统设置-登录项中允许后台项。 case requiresApproval /// 服务管理或运行时文件本身失败。 case serviceFailed(ServiceFailureFacts) } /// Current configuration or operation result, independent of persisted intent. typealias SystemProxyControlState = SystemProxyApplicationFacts enum SystemProxyObservationMode: Equatable { case stopped case enabled case cleanup } @Published var state: AgentRunState = .off @Published var systemProxyState: SystemProxyControlState = .idle /// Approval remains actionable even after an off-intent clear failure. @Published var systemProxyApprovalRequired = false @Published var systemProxyInspection = SystemProxyInspectionFacts() var lastDesiredSystemProxyConfiguration: SystemProxyConfiguration? var knownSystemProxyServices: Set = [] var systemProxyOperationInProgress = false var systemProxyStartupInProgress = false var systemProxyInitialApplyPending = false var systemProxyWasUnavailable = false var systemProxyHealthTask: Task? var systemProxyReadGeneration = 0 @Published var settings: ProxySettings /// 当前活动目标(菜单栏状态摘要与级联只读呈现用,issue #31)。machine 是 /// 非发布值的普通结构体,代理关闭路径的激活动作不会触碰 state,菜单的 /// 「目标」行依赖这里的独立发布保持实时。 @Published var activeTargetID: NodeID? /// 最近一次激活预检在分组中跳过的服务器;仅记录 app 已知的本地阻塞原因。 @Published var skippedServers: [SkippedServer] = [] /// 最近一次激活拒绝或目标清除的点名原因。独立于运行状态呈现:agent 可 /// 能仍在监听,激活失败不代表运行时停止(issue #60)。 @Published var lastActivationFailure: ActivationFailure? var machine: ActivationStateMachine let catalogSnapshotReader: RuntimeCatalogSnapshotReading let activationFileStore: ActivationStateFileStore let runtimeFileStore: RuntimeFileStore let credentials: CredentialStoring let plugins: ManagedPluginProviding let settingsStore: ProxySettingsStoring /// 自定义规则持久化(issue #66):规则模式 ACL 合并的用户入口。 var isUpdatingRules = false let customRuleStore: CustomRuleStore let ruleDocuments: RuleDocumentSession let ruleSnapshots: BuiltinRuleSnapshots var runtimePreparationGeneration = 0 /// 监听设置不可读时的点名原因(D8「任何路径不静默改端口」);非 nil 时 /// 设置只是占位出厂默认,禁止部署(见 `deploy`)。 var listenSettingsUnreadable: Bool /// 新版偏好不可读时同样禁止部署,不以出厂端口静默替代用户配置。 var settingsUnreadable: Bool let agent: LaunchAgentControlling let probe: EndpointProbing let systemProxy: SystemProxyControlling let systemProxyHelper: SystemProxyHelperServicing let systemProxyNetworkChangeMonitor: SystemProxyNetworkChangeMonitoring let firewallChecker: FirewallStatusChecking let firewallExecutableURLs: [URL] /// 宿主 app bundle 缝(生产为 Bundle.main):内置规则快照与 LaunchDaemon /// 清单等打包资源的定位基准。去宿主化(ADR 0021)后单测运行于 xctest /// runner,`Bundle.main` 不再是 app,经此缝注入构建产物 bundle。 let appBundle: Bundle let firewallPollIntervalNanoseconds: UInt64 let launchHealthTimeoutSeconds: TimeInterval /// 注册清单漂移重注时,注销与重注的间隔(launchd 对节流中 job 的移除异步)。 let systemProxyHealthPollIntervalNanoseconds: UInt64 let helperRefreshDelayNanoseconds: UInt64 /// 信号发送缝(默认 kill),单测观测 SIGUSR1 投递。 let sendSignal: @Sendable (Int32, Int32) -> Int32 let processIsAlive: @Sendable (Int32) -> Bool /// 并发流代际:停止请求可使进行中的启动探测立即失效。 var flowGeneration = 0 var modeChangeGeneration = 0 var lastDocument: SslocalRuntimeDocument? var firewallObservationTask: Task? var systemProxyObservationMode = SystemProxyObservationMode.stopped var systemProxyCleanupRescanRequested = false var systemProxyCleanupTask: Task? var systemProxyInspectionScheduled = false @Published var proxyMode: ProxyMode /// 规则模式子选项(issue #63):从设置快照投影。 var ruleDefaultAction: RuleDefaultAction { settings.ruleDefaultAction } init( catalogSnapshotReader: RuntimeCatalogSnapshotReading, activationFileStore: ActivationStateFileStore = ActivationStateFileStore( fileURL: ActivationStateFileStore.defaultFileURL()), runtimeFileStore: RuntimeFileStore = RuntimeFileStore(), credentials: CredentialStoring = KeychainCredentialStore(), plugins: ManagedPluginProviding = BundleManagedPluginProvider(), listenRestore: RestoredListenSettings = ListenSettingsFileStore.restored(), settingsStore: ProxySettingsStoring = ProxySettingsFileStore(), customRuleStore: CustomRuleStore = CustomRuleStore(), appBundle: Bundle = .main, ruleSnapshots: BuiltinRuleSnapshots? = nil, settingsRestore: RestoredProxySettings? = nil, agent: LaunchAgentControlling = SMAppLaunchAgentService(), probe: EndpointProbing = SystemEndpointProbe(), systemProxy: SystemProxyControlling = XPCSystemProxyController(), systemProxyHelper: SystemProxyHelperServicing = SMAppServiceSystemProxyHelper(), systemProxyNetworkChangeMonitor: SystemProxyNetworkChangeMonitoring = NoopSystemProxyNetworkChangeMonitor(), proxyMode: ProxyMode? = nil, firewallChecker: FirewallStatusChecking = SocketFilterFirewallChecker(), firewallExecutableURLs: [URL]? = nil, firewallPollIntervalNanoseconds: UInt64 = 2_000_000_000, launchHealthTimeoutSeconds: TimeInterval = 15, systemProxyHealthPollIntervalNanoseconds: UInt64 = 2_000_000_000, helperRefreshDelayNanoseconds: UInt64 = 15_000_000_000, sendSignal: @escaping @Sendable (Int32, Int32) -> Int32 = { kill($0, $1) }, processIsAlive: @escaping @Sendable (Int32) -> Bool = { $0 > 0 && kill($0, 0) == 0 } ) { self.catalogSnapshotReader = catalogSnapshotReader self.activationFileStore = activationFileStore self.runtimeFileStore = runtimeFileStore self.credentials = credentials self.plugins = plugins self.settingsStore = settingsStore self.customRuleStore = customRuleStore self.ruleDocuments = RuleDocumentSession(store: customRuleStore) self.ruleSnapshots = ruleSnapshots ?? BuiltinRuleSnapshots(bundle: appBundle) self.appBundle = appBundle let restoredSettings = settingsRestore ?? RestoredProxySettings( settings: ProxySettings(listen: listenRestore.settings), unreadableError: listenRestore.unreadableError.map { .legacyListenSettings($0) }) settings = restoredSettings.settings listenSettingsUnreadable = settingsRestore == nil && listenRestore.unreadableError != nil settingsUnreadable = restoredSettings.unreadableError != nil self.agent = agent self.probe = probe self.systemProxy = systemProxy self.systemProxyHelper = systemProxyHelper self.systemProxyNetworkChangeMonitor = systemProxyNetworkChangeMonitor self.firewallChecker = firewallChecker self.firewallExecutableURLs = firewallExecutableURLs ?? Self.defaultFirewallExecutableURLs self.firewallPollIntervalNanoseconds = firewallPollIntervalNanoseconds self.launchHealthTimeoutSeconds = launchHealthTimeoutSeconds self.systemProxyHealthPollIntervalNanoseconds = systemProxyHealthPollIntervalNanoseconds self.helperRefreshDelayNanoseconds = helperRefreshDelayNanoseconds self.sendSignal = sendSignal self.processIsAlive = processIsAlive let persistedTarget = try? activationFileStore.loadActiveTargetID() machine = ActivationStateMachine(activeTargetID: persistedTarget) activeTargetID = machine.activeTargetID self.proxyMode = proxyMode ?? Self.makeProxyMode(from: restoredSettings.settings) } }