mirror of
https://github.com/shadowsocks/ShadowsocksX-NG.git
synced 2026-11-23 22:36:55 +00:00
App/ 根目录原有 73 个散落源文件,已无法从目录读出任何分组信息。 按「目录即分层」重组为 Composition / Application / Presentation / UI / PlatformEffects / Resources 六个目录,UI 层再按子视图分目录(Workspace、 Home、Servers(含 Plugin)、Subscriptions、Rules、Settings、Diagnostics、 StatusMenu)。纯移动:类型名、模块名与 import 均未改动,App/ 根层不再有 散落文件。 配套改动: - project.yml:Core 测试镜像的 excludes 由裸文件名改为 `**/` 递归形式。 XcodeGen 的 exclude 不跨 `/`,裸名只匹配 App 根层;MainApp.swift 下移后 旧写法会静默失效,把 @main 组合根编进测试镜像。info.path 与 entitlements.path 指向 App/Resources/;并排除 `**/*.md`——App/ 下任何 非源码文件都会被 XcodeGen 收进 Resources 阶段并封进签名后的 bundle。 - SkeletonTests 的架构守卫由「默认扫描全部 App/ + 7 个豁免文件 + 1 个豁免 前缀 + 4 个豁免目录」改为正面圈定 App/UI/,豁免登记全部删除;新增运行时 适配器放进 Application/ 即自动落在扫描范围外,无需再登记。 - README 工程结构说明同步;根 .gitignore 的生成物路径随 Resources/ 归位。 验证:clean build 0 error;task test 1022 用例全通过(含两个架构守卫); task format 无改动(证明纯移动);task lint 与移动前同为 21 条内容型 warning;packaging-gate.sh ALL CHECKS PASSED,确认 plist/entitlements 归位 后签名与 entitlements 语义不变。 注意:迁移时须先删旧的生成物再 xcodegen generate,否则遗留的 App/Info.plist 会被当作普通资源扫进 Resources 阶段。
103 lines
3.5 KiB
Swift
103 lines
3.5 KiB
Swift
import Foundation
|
||
import ServiceManagement
|
||
|
||
/// LaunchAgent 管理缝(spec #21 D2):控制器经此协议启停 agent,单元测试以
|
||
/// fake 替换(真实注册会改动用户登录项,不进单测)。
|
||
protocol LaunchAgentControlling {
|
||
var status: LaunchAgentStatus { get }
|
||
func register() throws
|
||
func unregister() throws
|
||
}
|
||
|
||
/// 生产实现。用 `launchctl bootstrap` 注册 `~/Library/LaunchAgents/` 下带
|
||
/// 绝对路径的 plist(SMAppService 对相对 ProgramArguments 的解析在
|
||
/// DerivedData 等非 /Applications 路径下不可靠,launchd 报 `inferred
|
||
/// program` + `partial import` 并以 EX_CONFIG 拒绝 spawn)。SMAppService
|
||
/// 仅用于读取 BTM 批准状态;批准流仍走系统设置 → 登录项。
|
||
struct SMAppLaunchAgentService: LaunchAgentControlling {
|
||
static let label = "com.qiuyuzhou.ShadowsocksX-NG2.agent"
|
||
|
||
private let service = SMAppService.agent(
|
||
plistName: "com.qiuyuzhou.ShadowsocksX-NG2.agent.plist")
|
||
|
||
var status: LaunchAgentStatus {
|
||
if isLoadedInLaunchd { return .registered }
|
||
switch service.status {
|
||
case .requiresApproval:
|
||
return .requiresApproval
|
||
case .enabled:
|
||
return .registered
|
||
default:
|
||
return .notRegistered
|
||
}
|
||
}
|
||
|
||
func register() throws {
|
||
try writeUserPlist()
|
||
try runLaunchctl(["bootstrap", "gui/\(getuid())", userPlistURL.path])
|
||
}
|
||
|
||
func unregister() throws {
|
||
_ = try? runLaunchctl(["bootout", "gui/\(getuid())/\(Self.label)"])
|
||
try? FileManager.default.removeItem(at: userPlistURL)
|
||
}
|
||
|
||
// MARK: - Implementation
|
||
|
||
private var userPlistURL: URL {
|
||
FileManager.default.homeDirectoryForCurrentUser
|
||
.appendingPathComponent("Library/LaunchAgents/\(Self.label).plist")
|
||
}
|
||
|
||
private var agentBinaryURL: URL {
|
||
Bundle.main.bundleURL.appendingPathComponent("Contents/MacOS/ShadowsocksX-NG2Agent")
|
||
}
|
||
|
||
private var isLoadedInLaunchd: Bool {
|
||
(try? runLaunchctl(["print", "gui/\(getuid())/\(Self.label)"])) != nil
|
||
}
|
||
|
||
/// manifest 字典单一构造点:writeUserPlist(launchctl bootstrap 用)与
|
||
/// 打包 plist 的一致性测试共用。KeepAlive/ThrottleInterval 承载 crash
|
||
/// 重放协议(Agent 崩溃 → 非零退出 → launchd 重启重放最后有效快照)。
|
||
static func manifestDictionary(programArguments: [String]) -> [String: Any] {
|
||
[
|
||
"Label": Self.label,
|
||
"ProgramArguments": programArguments,
|
||
"KeepAlive": ["SuccessfulExit": false],
|
||
"ThrottleInterval": 2,
|
||
]
|
||
}
|
||
|
||
private func writeUserPlist() throws {
|
||
let plist = Self.manifestDictionary(programArguments: [agentBinaryURL.path])
|
||
let data = try PropertyListSerialization.data(
|
||
fromPropertyList: plist, format: .xml, options: 0)
|
||
try FileManager.default.createDirectory(
|
||
at: userPlistURL.deletingLastPathComponent(), withIntermediateDirectories: true)
|
||
try data.write(to: userPlistURL, options: .atomic)
|
||
}
|
||
|
||
@discardableResult
|
||
private func runLaunchctl(_ arguments: [String]) throws -> String {
|
||
let process = Process()
|
||
process.executableURL = URL(fileURLWithPath: "/bin/launchctl")
|
||
process.arguments = arguments
|
||
let stderr = Pipe()
|
||
process.standardError = stderr
|
||
try process.run()
|
||
process.waitUntilExit()
|
||
let errorOutput =
|
||
String(
|
||
data: stderr.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8) ?? ""
|
||
guard process.terminationStatus == 0 else {
|
||
throw LaunchAgentRegistrationError.launchctlFailed(errorOutput)
|
||
}
|
||
return errorOutput
|
||
}
|
||
}
|
||
|
||
enum LaunchAgentRegistrationError: Error, Equatable {
|
||
case launchctlFailed(String)
|
||
}
|