Files
ShadowsocksX-NG/ShadowsocksX-NG2/Domain/SystemProxyHelperContract.swift
T
Qiu Yuzhou 43ea00cb44 fix(system-proxy): use BundleProgram in daemon plist and refresh stale registration
launchd 对 ProgramArguments[0] 的相对路径按 _PATH_STDPATH(PATH)解析而非
bundle:系统域 daemon 的 job 因此永远 spawn 失败(EX_CONFIG 节流循环,
launchctl print 显示 job state = spawn failed、runs 持续累加),客户端 XPC
调用只能等到超时。agent 之所以正常,是 GUI 域导入时被解析成绝对路径,system
域 submitted job 没有这一步。SMAppService 清单的正确键是 BundleProgram
(bundle 相对,launchd.plist(5)),改用之。

launchd 同时沿用注册提交时的定义快照:app 更新改动 LaunchDaemon 清单后旧
定义不会自动刷新(register() 对已注册服务返回 alreadyRegistered),必须注销
重注。门禁在已批准分支按清单内容 SHA-256 指纹(UserDefaults)检测漂移,漂移
才注销重注——审批与 bundle 签名绑定,重注不重新弹授权;首次注册成功即记指
纹,失败不记(下次启动重试),指纹一致为一次哈希比对的 no-op。
2026-09-30 18:54:19 +08:00

74 lines
3.0 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"
/// App bundle 内 LaunchDaemon 清单内容(注册漂移指纹的基准)。helper 进程
/// 内没有 bundle,返回 nil,调用方按「无基准不动作」处理。Contents/Library
/// 不是资源目录,Bundle 资源 API 搜不到,按固定路径直读。
static var launchDaemonPlistData: Data? {
let url = Bundle.main.bundleURL
.appendingPathComponent("Contents/Library/LaunchDaemons")
.appendingPathComponent(plistName)
return try? Data(contentsOf: url)
}
}
/// 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))
}
}
}