Files
ShadowsocksX-NG/ShadowsocksX-NG2/App/LegacyAppController.swift
T
Qiu Yuzhou 8d1cdf27d1 feat(ng2): Legacy 交接第二阶段(#37)
与导入分离的「切换到 2.0」确认流程(spec #21 D12):

- Domain/LegacyHandoff:硬编码白名单三个 Legacy label(.local/.http/.kcptun),
  bootout 按 service target(覆盖 plist 缺失错位形态),print 退出码验证
  (0/113),`launchctl kill SIGTERM` 有文档后备,绝不泛匹配、不 pkill;
  残留 plist(含 KeepAlive 形态)读内容判活跃性后 disable,阻止下次登录复活;
  系统代理按特征值判定归属(Legacy 自身 off 模式的判定镜像:本地 PAC URL
  http://localhost:<port>/proxy.pac、SOCKS/HTTP 地址端口),仅清理归属为
  Legacy 的服务,未知所有者点名保留;端口释放门禁用无复用选项的 bind 探测
  (Legacy ss-local 带 --reuse-port),未释放不启动 2.0;Legacy 数据零删除。
- App 层:SystemConfigurationLegacyProxyCleaner(授权在最前、失败零影响、
  事务回滚);WorkspaceLegacyAppController(共享 bundle id 需排除自身进程);
  LegacyHandoffSheet 确认/进度/报告呈现,含旧登录项人工指引与残留提示;
  主窗口工具栏入口 + 导入完成后「切换到 2.0」入口;交接成功经正常启停入口
  启动 2.0(健康门禁走 #29 路径)。
- bootout 实弹验证(Further Notes #5)闭环:docs/research/
  legacy-handoff-bootout-live-fire.md——bootout 未加载 label 退出码为 3
  (非 print 的 113),KeepAlive 残留 kill 后复活、bootout 后不再加载,
  结论已回写产品代码与测试。
2026-09-21 12:49:49 +08:00

48 lines
1.9 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 AppKit
/// NSWorkspace 实现的 Legacy app 检测缝(issue #37):NG2 与 Legacy 共享
/// bundle id(D1 继承),「正在运行的旧版 app」= 该 bundle id 下除自身进程外
/// 的任何进程。NSRunningApplication 要求主线程,非主线程调用同步派发——
/// 调用方(交接检测在后台任务)挂起等待而非阻塞主线程,无死锁。
final class WorkspaceLegacyAppController: LegacyAppControlling {
static let legacyBundleIdentifier = "com.qiuyuzhou.ShadowsocksX-NG"
func isRunning() -> Bool {
MainActor.runUnsafelyIfNeeded { legacyApplication() != nil }
}
func isInstalled() -> Bool {
Self.bundleURLs.contains { FileManager.default.fileExists(atPath: $0.path) }
}
func requestGracefulQuit() -> Bool {
MainActor.runUnsafelyIfNeeded { legacyApplication()?.terminate() ?? false }
}
/// 常见安装位置(Legacy LaunchHelper 自身也按这些位置找主 app)。
private static let bundleURLs: [URL] = [
URL(fileURLWithPath: "/Applications/ShadowsocksX-NG.app"),
FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent(
"Applications/ShadowsocksX-NG.app", isDirectory: true),
]
private func legacyApplication() -> NSRunningApplication? {
NSRunningApplication
.runningApplications(withBundleIdentifier: Self.legacyBundleIdentifier)
.first { $0.processIdentifier != ProcessInfo.processInfo.processIdentifier }
}
}
extension MainActor {
/// 供非主线程上下文同步执行 AppKit 调用的小帮手;主线程上直接执行。
fileprivate static func runUnsafelyIfNeeded<T>(_ body: @MainActor () -> T) -> T {
if Thread.isMainThread {
// 主线程上已满足隔离要求,直接执行(MainActor.assumeIsolated)。
return MainActor.assumeIsolated(body)
}
return DispatchQueue.main.sync {
MainActor.assumeIsolated(body)
}
}
}