Files
ShadowsocksX-NG/ShadowsocksX-NG2/App/ProxyRuntimeController+Health.swift
T
Qiu Yuzhou eac30c4abd refactor(sysproxy): 系统代理策略机抽为 SystemProxyObserver 模块
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 测试全绿
2026-10-03 12:44:20 +08:00

267 lines
9.7 KiB
Swift

import Foundation
private struct LocalEndpointFailure {
let local: SslocalLocalDocument
let host: String
let outcome: EndpointHealthProbe.Outcome
}
private enum LaunchHealthAttempt {
case ready(RuntimeDeploymentReceipt?)
case retry(LocalEndpointFailure?)
case agentLost
}
private struct LaunchHealthTimeout {
let expectedDigest: String?
let endpointFailure: LocalEndpointFailure?
let generation: Int
let preparation: Int
let preserveProxyOnFailure: Bool
}
extension ProxyRuntimeController {
/// 启动健康呈现(D2/D7/D8):确认本地入站;直连模式额外要求
/// 回执持续指向当前健康子进程,防止旧监听通过共享端口冒充新实例。
@discardableResult
func presentLaunchHealth(
_ document: SslocalRuntimeDocument,
requiresReceipt: Bool = false,
convergeProxyOnSuccess: Bool = true,
preserveProxyOnFailure: Bool = false,
preparedContract: PreparedRuntimeContract? = nil
) async -> Bool {
guard preparedContract == nil || preparedContract?.document == document else {
return await reportRuntimeFileFailure(preserveProxyOnFailure: preserveProxyOnFailure)
}
let generation = flowGeneration
let preparation = runtimePreparationGeneration
let expectedDigest =
requiresReceipt ? (preparedContract?.sha256 ?? document.deploymentSHA256) : nil
guard !requiresReceipt || expectedDigest != nil else {
return await reportRuntimeFileFailure(preserveProxyOnFailure: preserveProxyOnFailure)
}
let deadline = Date().addingTimeInterval(launchHealthTimeoutSeconds)
var endpointFailure: LocalEndpointFailure?
while Date() < deadline {
guard generation == flowGeneration, preparation == runtimePreparationGeneration else {
return false
}
let attempt = await launchHealthAttempt(document, expectedDigest: expectedDigest)
guard generation == flowGeneration, preparation == runtimePreparationGeneration else {
return false
}
switch attempt {
case .ready(let receipt):
return await finishHealthyLaunch(
document,
receipt: receipt,
generation: (generation, preparation),
convergeProxyOnSuccess: convergeProxyOnSuccess,
preserveProxyOnFailure: preserveProxyOnFailure)
case .retry(let endpoint):
endpointFailure = endpoint
case .agentLost:
return await reportAgentLost(preserveProxyOnFailure: preserveProxyOnFailure)
}
try? await Task.sleep(nanoseconds: 200_000_000)
}
return await reportLaunchHealthTimeout(
LaunchHealthTimeout(
expectedDigest: expectedDigest,
endpointFailure: endpointFailure,
generation: generation, preparation: preparation,
preserveProxyOnFailure: preserveProxyOnFailure))
}
private func launchHealthAttempt(
_ document: SslocalRuntimeDocument,
expectedDigest: String?
) async -> LaunchHealthAttempt {
var receiptForHealth: RuntimeDeploymentReceipt?
if let expectedDigest {
guard let receipt = runtimeFileStore.readRuntimeReceipt(),
receipt.contractSHA256 == expectedDigest
else {
return .retry(nil)
}
guard receipt.processID > 0, processIsAlive(receipt.processID) else {
return .agentLost
}
receiptForHealth = receipt
}
if let endpointFailure = await unhealthyLocalEndpoint(in: document) {
return .retry(endpointFailure)
}
if let receiptForHealth {
guard runtimeFileStore.readRuntimeReceipt() == receiptForHealth else {
return .retry(nil)
}
guard processIsAlive(receiptForHealth.processID) else { return .agentLost }
}
return .ready(receiptForHealth)
}
private func finishHealthyLaunch(
_ document: SslocalRuntimeDocument,
receipt: RuntimeDeploymentReceipt?,
generation: (flow: Int, preparation: Int),
convergeProxyOnSuccess: Bool,
preserveProxyOnFailure: Bool
) async -> Bool {
guard generation.flow == flowGeneration,
generation.preparation == runtimePreparationGeneration
else { return false }
await presentFirewallStatus(for: document)
guard generation.flow == flowGeneration,
generation.preparation == runtimePreparationGeneration
else { return false }
if let receipt, !receiptIsCurrentAndLive(receipt) {
return await reportAgentLost(preserveProxyOnFailure: preserveProxyOnFailure)
}
if convergeProxyOnSuccess { await systemProxyObserver.convergeSystemProxy() }
return true
}
private func receiptIsCurrentAndLive(_ receipt: RuntimeDeploymentReceipt) -> Bool {
guard receipt.processID > 0,
runtimeFileStore.readRuntimeReceipt() == receipt
else { return false }
return processIsAlive(receipt.processID)
}
private func reportAgentLost(preserveProxyOnFailure: Bool) async -> Bool {
guard !preserveProxyOnFailure else { return false }
state = .serviceFailed(.agent)
await systemProxyObserver.holdSystemProxyIntent()
return false
}
private func reportRuntimeFileFailure(preserveProxyOnFailure: Bool) async -> Bool {
guard !preserveProxyOnFailure else { return false }
state = .serviceFailed(.runtimeFile)
await systemProxyObserver.holdSystemProxyIntent()
return false
}
private func reportLaunchHealthTimeout(_ timeout: LaunchHealthTimeout) async -> Bool {
guard timeout.generation == flowGeneration,
timeout.preparation == runtimePreparationGeneration
else { return false }
guard !timeout.preserveProxyOnFailure else { return false }
if let expectedDigest = timeout.expectedDigest,
!hasLiveReceipt(expectedDigest: expectedDigest)
{
return await reportAgentLost(preserveProxyOnFailure: false)
}
if let endpointFailure = timeout.endpointFailure {
presentEndpointFailure(endpointFailure)
} else {
state = .launchFailed(
.localEndpoint(endpoint: "SOCKS", host: "127.0.0.1", port: 0, cause: .unknown))
}
await systemProxyObserver.holdSystemProxyIntent()
return false
}
private func hasLiveReceipt(expectedDigest: String) -> Bool {
guard let receipt = runtimeFileStore.readRuntimeReceipt(),
receipt.contractSHA256 == expectedDigest
else { return false }
return receiptIsCurrentAndLive(receipt)
}
private func unhealthyLocalEndpoint(
in document: SslocalRuntimeDocument
) async -> LocalEndpointFailure? {
for local in document.locals {
let outcome = await probeAsync(
host: local.probeHost, port: local.localPort, timeout: 1.5)
if outcome != .reachable {
return LocalEndpointFailure(local: local, host: local.probeHost, outcome: outcome)
}
}
return nil
}
private func presentEndpointFailure(_ failure: LocalEndpointFailure) {
let detail: String
let cause: RuntimeEndpointFailure
switch failure.outcome {
case .reachable:
detail = "已连通"
cause = .unknown
case .refused(let reason):
detail = reason
cause = .refused
case .timedOut:
detail = "连接超时"
cause = .timedOut
}
RuntimeLog.emit(
.endpointProbeFailed(
host: failure.host, port: failure.local.localPort, detail: detail))
let endpointName = failure.local.inboundProtocol.uppercased()
state = .launchFailed(
.localEndpoint(
endpoint: endpointName, host: failure.host, port: failure.local.localPort, cause: cause))
}
}
extension ProxyRuntimeController {
/// Uses the existing local endpoint/receipt health seam while the GUI is alive.
/// Network path availability is deliberately absent from this decision.
/// 任务柄持有在观察机上(cleanup 与停止路径都要取消它);循环体留在本体,
/// 因为它同时承担运行时健康呈现,系统代理反应转发给观察机。
func startSystemProxyHealthObservation() {
guard systemProxyObserver.systemProxyHealthTask == nil else { return }
let interval = systemProxyHealthPollIntervalNanoseconds
systemProxyObserver.systemProxyHealthTask = Task { @MainActor [weak self] in
while !Task.isCancelled {
try? await Task.sleep(nanoseconds: interval)
guard !Task.isCancelled, let self,
settings.systemProxyEnabled || systemProxyState.hasOperationFailure
else { return }
systemProxyObserver.refreshSystemProxyApproval()
systemProxyObserver.updateSystemProxyActions()
guard settings.systemProxyEnabled, state != .starting,
!systemProxyObserver.systemProxyStartupInProgress, settings.agentEnabled,
let document = lastDocument
else { continue }
await inspectSystemProxyRuntimeHealth(document)
}
}
}
private func inspectSystemProxyRuntimeHealth(_ document: SslocalRuntimeDocument) async {
let generation = flowGeneration
let outcome = await launchHealthAttempt(
document, expectedDigest: document.aclRuntime != nil ? document.deploymentSHA256 : nil)
guard !Task.isCancelled, generation == flowGeneration, settings.systemProxyEnabled else {
return
}
switch outcome {
case .ready:
let wasUnavailable = systemProxyObserver.systemProxyWasUnavailable
switch state {
case .running, .firewallBlocked: break
default: await presentFirewallStatus(for: document)
}
if !systemProxyExitAvailable {
await systemProxyObserver.suspendSystemProxy()
} else if wasUnavailable || systemProxyObserver.systemProxyInitialApplyPending {
await systemProxyObserver.convergeSystemProxy()
}
case .agentLost:
state = .serviceFailed(.agent)
await systemProxyObserver.suspendSystemProxy()
case .retry(let failure):
if let failure { presentEndpointFailure(failure) } else { state = .serviceFailed(.agent) }
await systemProxyObserver.suspendSystemProxy()
}
}
}