Files
ShadowsocksX-NG/ShadowsocksX-NG2/Domain/SystemProxyHelperContract.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

64 lines
2.5 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
/// GUI 与特权系统代理 helper 的共享 XPC 契约(issue #71)。类型同时编入 GUI、
/// helper 与测试 target;payload 以 JSON 编码保持 closed typed 语义。
@objc protocol SystemProxyHelperControlling {
/// 应用 closed typed 配置到活动网络位置的每个服务。
func apply(_ payload: Data, withReply reply: @escaping (Data) -> Void)
/// 无条件清除全部网络位置全部服务的完整 Proxies 字典。
func clear(withReply reply: @escaping (Data) -> Void)
}
/// helper 的固定身份:MachService 名、LaunchDaemon plist 名与客户端校验基准。
/// 客户端只按 2.0 的 bundle identifier(代码签名 identifier)接受,不限
/// Team ID 或账号(issue #71)。
enum SystemProxyHelperIdentity {
static let machServiceName = "com.qiuyuzhou.ShadowsocksX-NG2.systemproxy"
static let plistName = "com.qiuyuzhou.ShadowsocksX-NG2.systemproxy.plist"
static let launchdLabel = "com.qiuyuzhou.ShadowsocksX-NG2.systemproxy"
static let clientCodeSigningIdentifier = "com.qiuyuzhou.ShadowsocksX-NG2"
}
/// helper XPC 应答:成功携带 typed 结果,失败携带 typed 错误族。
enum SystemProxyHelperResponse: Equatable, Sendable, Codable {
case applied(SystemProxyWriteOutcome)
case cleared
case failure(SystemProxyError)
}
enum SystemProxyHelperWireError: Error, Equatable, Sendable {
case undecodableResponse(String)
}
enum SystemProxyHelperWire {
/// SystemProxyHelperResponse 全部由简单可编码类型组成,JSON 编码不可失败;
/// 若未来加入不可编码载荷,显式失败好过发出无法解码的应答。
static func encode(_ response: SystemProxyHelperResponse) -> Data {
do {
return try JSONEncoder().encode(response)
} catch {
preconditionFailure("system proxy helper response encode failed: \(error)")
}
}
static func decode(_ payload: Data) throws -> SystemProxyHelperResponse {
do {
return try JSONDecoder().decode(SystemProxyHelperResponse.self, from: payload)
} catch {
throw SystemProxyHelperWireError.undecodableResponse(String(describing: error))
}
}
static func encodeConfiguration(_ configuration: SystemProxyConfiguration) throws -> Data {
try JSONEncoder().encode(configuration)
}
static func decodeConfiguration(_ payload: Data) throws -> SystemProxyConfiguration {
do {
return try JSONDecoder().decode(SystemProxyConfiguration.self, from: payload)
} catch {
throw SystemProxyError.invalidRequest(String(describing: error))
}
}
}