mirror of
https://github.com/shadowsocks/ShadowsocksX-NG.git
synced 2026-11-24 23:03:46 +00:00
ADR-0022 的观察/修复/挂起策略此前溶解在控制器 15 个可变字段里、由 Gate/Inspection/Commands/Health/Mode 五个文件交叉读写。现在策略机独立为 SystemProxyObserver:意图开关、出口可用性、期望配置与健康循环调度以 @MainActor 闭包注入,模块不反向依赖控制器;底层继续只依赖 SystemProxyControlling / helper 注册缝 / 网络变化监视三条既有缝。 - 3 个 @Published 呈现事实随迁,控制器留同名只读门面(测试与诊断面零改动); adapter 再发布链 merge 观察机 objectWillChange,主队列 hop 语义不变 - 健康循环留在控制器(循环体含运行时健康呈现),任务柄归观察机; cleanup-until-quiet、网络变化 inspection 调度与操作互斥全部内聚 - 方法体逐字搬运(settings.systemProxyEnabled→isIntentEnabled() 等机械替换), 语义零改动;既有 777 测试全绿
101 lines
3.9 KiB
Swift
101 lines
3.9 KiB
Swift
import Foundation
|
||
|
||
// MARK: - 只读事实投影与静态缝
|
||
|
||
extension ProxyRuntimeController {
|
||
var isActiveTargetPresent: Bool { machine.activeTargetID != nil }
|
||
|
||
/// Agent 开关意图(持久化事实;开关 UI 的绑定来源,issue #60)。
|
||
var agentIntentEnabled: Bool { settings.agentEnabled }
|
||
|
||
/// 系统代理开关意图(持久化事实;开关 UI 的绑定来源,issue #60)。
|
||
var systemProxyIntentEnabled: Bool { settings.systemProxyEnabled }
|
||
|
||
/// 系统代理观察机的呈现事实门面:存储与发布在 `SystemProxyObserver`,
|
||
/// 此处只读投影供既有测试与诊断面沿用原名。
|
||
var systemProxyState: SystemProxyApplicationFacts { systemProxyObserver.systemProxyState }
|
||
var systemProxyApprovalRequired: Bool { systemProxyObserver.systemProxyApprovalRequired }
|
||
var systemProxyInspection: SystemProxyInspectionFacts {
|
||
systemProxyObserver.systemProxyInspection
|
||
}
|
||
|
||
/// Desired system proxy configuration:当前保存设置要求的代理值(ADR-0022)。
|
||
/// 观察机经 init 闭包读取同一事实源。
|
||
var desiredSystemProxyConfiguration: SystemProxyConfiguration? {
|
||
guard let document = lastDocument else { return nil }
|
||
return try? proxyMode.systemProxyConfiguration(
|
||
for: document, exceptions: settings.proxyExceptionList)
|
||
}
|
||
|
||
/// Stable app-facing projection for status-menu presentation. The menu does
|
||
/// not depend on this controller's nested state representation.
|
||
var runtimeFacts: ProxyRuntimeFacts {
|
||
ProxyRuntimeFacts(state: state)
|
||
}
|
||
|
||
static var defaultFirewallExecutableURLs: [URL] {
|
||
let bundle = Bundle.main.bundleURL
|
||
return [
|
||
bundle.appendingPathComponent("Contents/MacOS/ShadowsocksX-NG2Agent"),
|
||
bundle.appendingPathComponent("Contents/Helpers/sslocal"),
|
||
]
|
||
}
|
||
|
||
/// 诊断只读面(issue #34):当前监听设置;监听地址在导出中只以回环/非回环
|
||
/// 两态呈现(D7)。
|
||
var listenSettings: SslocalListenSettings { settings.listen }
|
||
|
||
/// 当前已部署 runtime 的完整监听身份。已停止或启动失败时没有有效的
|
||
/// runtime 例外,不能拿最后一次设置快照冒充仍在监听。
|
||
var effectiveRuntimeListenFacts: RuntimeListenFacts? {
|
||
let isListening: Bool
|
||
switch state {
|
||
case .running, .firewallBlocked:
|
||
isListening = true
|
||
case .off, .starting, .launchFailed, .requiresApproval, .serviceFailed:
|
||
isListening = false
|
||
}
|
||
guard isListening, let lastDocument else { return nil }
|
||
return RuntimeListenFacts(document: lastDocument)
|
||
}
|
||
|
||
/// PID of the sslocal child that owns the active runtime document. A process
|
||
/// name alone is not sufficient to treat an occupied listener as replaceable.
|
||
var effectiveRuntimeListenerProcessID: Int32? {
|
||
guard effectiveRuntimeListenFacts != nil,
|
||
let lastDocument,
|
||
let receipt = runtimeFileStore.readRuntimeReceipt(),
|
||
receipt.contractSHA256 == lastDocument.deploymentSHA256,
|
||
processIsAlive(receipt.processID)
|
||
else { return nil }
|
||
return receipt.processID
|
||
}
|
||
|
||
/// 运行时契约的脱敏摘要(数量与协议元数据,D5);契约缺失或无效返回 nil。
|
||
/// 诊断导出不读契约内容,只携带此摘要。
|
||
func runtimeDocumentSummary() -> String? {
|
||
runtimeFileStore.loadDocument().map { Redactor.documentSummary($0) }
|
||
}
|
||
}
|
||
|
||
extension ProxyRuntimeController.AgentRunState {
|
||
/// Agent 运行状态的「在跑」投影(状态摘要与诊断口径):启动失败等未运行
|
||
/// 态视为未跑。
|
||
var isOn: Bool {
|
||
switch self {
|
||
case .off, .launchFailed, .serviceFailed:
|
||
false
|
||
case .starting, .running, .firewallBlocked, .requiresApproval:
|
||
true
|
||
}
|
||
}
|
||
}
|
||
|
||
extension ProxyRuntimeController: Activating {}
|
||
|
||
extension ProxyRuntimeController {
|
||
static func makeProxyMode(from settings: ProxySettings) -> ProxyMode {
|
||
ProxyMode.availableModes.first { $0.kind == settings.preferredMode } ?? .rule
|
||
}
|
||
}
|