mirror of
https://github.com/shadowsocks/ShadowsocksX-NG.git
synced 2026-11-22 13:34:00 +00:00
配置目录领域核心(spec #21 D3/D5,票 #25): - Domain/ 新增配置目录树:单父、显子序、无环;手动/订阅来源严格分离; 有效启用 = 节点及其全部祖先启用;手动节点携带子树移动与重排; 非空手动组递归删除(返回被删条目供 Keychain 清理);空组持久保留; 非法操作(跨来源、身份复用即共享父、成环、订阅节点结构改动、 订阅服务器直挂根)全部带明确原因拒绝且状态不动。 - 凭据引用:密码/插件参数只在 Keychain 存权威副本,配置树只持 CredentialReference;KeychainCredentialStore 写读一致、覆盖写、幂等删。 - CatalogFileStore:v2/catalog.json 目录 0700/文件 0600,临时文件原子替换; 缺失 → 空目录,损坏/版本未知/结构不一致(悬空、共享父、成环含根外孤环、 不可达)→ 命名明确的 corrupt,结构校验收敛在 ConfigurationCatalog.validated 工厂。 - 测试 43 项覆盖全部验收项;project.yml 增加 Domain/ 源码目录。 - .swiftlint.yml 关闭 trailing_comma(与 .swift-format 尾逗号基线冲突, 触发案例见文件内注释),README/AGENTS.md 同步 Domain/ 与工具说明。
79 lines
3.1 KiB
Swift
79 lines
3.1 KiB
Swift
import Foundation
|
|
import Security
|
|
|
|
/// 凭据存储缝(spec #21 D5):密码、插件参数、非空远程 URL 等秘密值的权威持久
|
|
/// 副本在 Keychain,配置树只持凭据引用。实现须保证写入/读出一致。
|
|
protocol CredentialStoring {
|
|
/// 写入即覆盖既有值(同一引用始终对应最新秘密)。
|
|
func save(_ secret: String, for reference: CredentialReference) throws
|
|
/// 无对应条目返回 `nil`,不视为错误。
|
|
func secret(for reference: CredentialReference) throws -> String?
|
|
/// 删除不存在的条目不视为错误(幂等)。
|
|
func delete(_ reference: CredentialReference) throws
|
|
}
|
|
|
|
enum CredentialStoreError: Error, Equatable {
|
|
/// Keychain 返回预期之外的状态码。
|
|
case keychainStatus(OSStatus)
|
|
/// 存储的秘密值不是合法 UTF-8 文本。
|
|
case secretNotUTF8
|
|
}
|
|
|
|
/// 生产实现:登录钥匙串 Generic Password。`service` 隔离本应用命名空间,
|
|
/// `account` 即凭据引用;可访问性取「首次解锁后」,兼顾代理运行时读取。
|
|
struct KeychainCredentialStore: CredentialStoring {
|
|
let service: String
|
|
|
|
init(service: String = "com.qiuyuzhou.ShadowsocksX-NG.credentials") {
|
|
self.service = service
|
|
}
|
|
|
|
func save(_ secret: String, for reference: CredentialReference) throws {
|
|
let data = Data(secret.utf8)
|
|
let query = baseQuery(for: reference)
|
|
let update: [String: Any] = [
|
|
kSecValueData as String: data,
|
|
kSecAttrAccessible as String: kSecAttrAccessibleAfterFirstUnlock,
|
|
]
|
|
var status = SecItemUpdate(query as CFDictionary, update as CFDictionary)
|
|
if status == errSecItemNotFound {
|
|
var addItem = query
|
|
addItem[kSecValueData as String] = data
|
|
addItem[kSecAttrAccessible as String] = kSecAttrAccessibleAfterFirstUnlock
|
|
status = SecItemAdd(addItem as CFDictionary, nil)
|
|
}
|
|
guard status == errSecSuccess else { throw CredentialStoreError.keychainStatus(status) }
|
|
}
|
|
|
|
func secret(for reference: CredentialReference) throws -> String? {
|
|
var query = baseQuery(for: reference)
|
|
query[kSecReturnData as String] = kCFBooleanTrue as Any
|
|
query[kSecMatchLimit as String] = kSecMatchLimitOne
|
|
var result: AnyObject?
|
|
let status = SecItemCopyMatching(query as CFDictionary, &result)
|
|
if status == errSecItemNotFound { return nil }
|
|
guard status == errSecSuccess, let data = result as? Data else {
|
|
throw CredentialStoreError.keychainStatus(status)
|
|
}
|
|
guard let secret = String(data: data, encoding: .utf8) else {
|
|
throw CredentialStoreError.secretNotUTF8
|
|
}
|
|
return secret
|
|
}
|
|
|
|
func delete(_ reference: CredentialReference) throws {
|
|
let status = SecItemDelete(baseQuery(for: reference) as CFDictionary)
|
|
guard status == errSecSuccess || status == errSecItemNotFound else {
|
|
throw CredentialStoreError.keychainStatus(status)
|
|
}
|
|
}
|
|
|
|
private func baseQuery(for reference: CredentialReference) -> [String: Any] {
|
|
[
|
|
kSecClass as String: kSecClassGenericPassword,
|
|
kSecAttrService as String: service,
|
|
kSecAttrAccount as String: reference.rawValue,
|
|
]
|
|
}
|
|
}
|