mirror of
https://github.com/shadowsocks/ShadowsocksX-NG.git
synced 2026-11-22 05:25:41 +00:00
Domain/ 根目录原有 62 个平铺源文件,无法从目录读出主题划分。 按「目录即主题」重组为 11 个目录(Rules/Custom 为嵌套子目录):Catalog / Subscription / Rules / Runtime / Activation / Settings / SystemProxy / Plugin / Credentials / LegacyImport / Diagnostics。纯移动:类型名、模块名 与 import 均未改动,Domain/ 根下不再有散落文件。目录口径取自 GLOSSARY.md 的章节划分(配置与订阅、代理规则、代理控制与网络、设置、 凭据与诊断),而非新造分类。 配套改动: - project.yml:wrapper 与 helper 两个 target 按文件引用的 15 条显式路径 随目录改名,并补注释固定这一约定——这 15 条指向不存在的文件时, xcodegen generate 以 "missing source directory" 直接失败(实测,不静默)。 - SkeletonTests 新增 DomainLayoutTests:断言 Domain/ 根下无平铺 .swift、 树下只有 .swift。后者防的是与 App/ 同源的坑——Domain/ 整目录是 app 与 Core 的 source path,任何非源码文件都会被 XcodeGen 收进 Resources 阶段 并封进签名后的 bundle。 - README 工程结构说明同步;docs/design 与 docs/research 属历史文档不改。 验证:xcodegen generate 后 app 164 / Core 163 源文件数与整理前一致;Agent 恰好 9 个、Helper 恰好 6 个 Domain 文件(无缺失无多出);app 与 Core 各覆盖 全部 62 个。clean build 0 error;task test 1024 用例全通过(整理前 1022, +2 为新增守卫);task format 未改写任何被移动文件(证明纯移动);task lint 与整理前同为 21 条内容型 warning;packaging-gate.sh ALL CHECKS PASSED。 布局守卫已做变异验证:临时投放松散 .swift 与非 .swift 文件时两个用例均 失败并点名违规文件,清理后复验为绿。
63 lines
2.3 KiB
Swift
63 lines
2.3 KiB
Swift
import Foundation
|
|
|
|
/// 活动目标持久化(spec #21 D3/D5):落盘 `~/Library/Application Support/
|
|
/// ShadowsocksX-NG2/activation.json`,权限与原子替换基线同 catalog.json。
|
|
/// 只含目标身份,永不落秘密明文。损坏或版本未知按安全侧恢复为「无活动目标」
|
|
/// (代理保持停止,由状态机重校验同步);文件系统级错误仍抛出。
|
|
struct ActivationStateFileStore {
|
|
enum PersistenceError: Error, Equatable {
|
|
/// 读取或写入时的文件系统错误。
|
|
case ioFailure(detail: String)
|
|
}
|
|
|
|
private static let currentVersion = 1
|
|
private static let jsonEncoder: JSONEncoder = {
|
|
let encoder = JSONEncoder()
|
|
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
|
|
return encoder
|
|
}()
|
|
|
|
let fileURL: URL
|
|
|
|
/// 默认位置:`~/Library/Application Support/ShadowsocksX-NG2/activation.json`。
|
|
static func defaultFileURL() -> URL {
|
|
RuntimePaths.activationStateFileURL()
|
|
}
|
|
|
|
/// 文件缺失/损坏/版本未知 → `nil`(安全侧:无活动目标)。
|
|
func loadActiveTargetID() throws -> NodeID? {
|
|
guard FileManager.default.fileExists(atPath: fileURL.path) else { return nil }
|
|
let data: Data
|
|
do {
|
|
data = try Data(contentsOf: fileURL)
|
|
} catch {
|
|
throw PersistenceError.ioFailure(detail: String(describing: error))
|
|
}
|
|
let payload = try? JSONDecoder().decode(ActivationStatePayload.self, from: data)
|
|
guard let payload, payload.version == Self.currentVersion else { return nil }
|
|
return payload.activeTargetID
|
|
}
|
|
|
|
/// 整体重写并原子替换;失败时保留原文件。目录按 D5 基线强制 0700(含自愈)。
|
|
func save(activeTargetID: NodeID?) throws {
|
|
let data: Data
|
|
do {
|
|
data = try Self.jsonEncoder.encode(
|
|
ActivationStatePayload(version: Self.currentVersion, activeTargetID: activeTargetID))
|
|
} catch {
|
|
throw PersistenceError.ioFailure(detail: String(describing: error))
|
|
}
|
|
do {
|
|
try AtomicFileWriter.write(data, to: fileURL)
|
|
} catch {
|
|
throw PersistenceError.ioFailure(detail: String(describing: error))
|
|
}
|
|
}
|
|
}
|
|
|
|
/// 落盘文档形态:版本号 + 活动目标身份(可为 null)。
|
|
private struct ActivationStatePayload: Codable {
|
|
var version: Int
|
|
var activeTargetID: NodeID?
|
|
}
|