Files
ShadowsocksX-NG/ShadowsocksX-NG2/Domain/Rules/Custom/CustomRule.swift
T
Qiu Yuzhou 856d6b4272 refactor(domain): 按主题重组 Domain/ 源码目录
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 文件时两个用例均
失败并点名违规文件,清理后复验为绿。
2026-10-05 20:13:35 +08:00

127 lines
4.5 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
// MARK: - 自定义规则
/// 自定义规则(issue #66):动作 + 匹配条件 + 来源元数据 + 稳定身份。
/// 为后续管理 UI 提供持久入口;本票不实现完整编辑、排序和删除界面。
/// 有效的完整域名、后缀及 CIDR 规则参与两种规则子模式的 ACL 合并。
struct CustomRule: Codable, Equatable, Hashable, Sendable, Identifiable {
let id: UUID
let action: RuleAction
let match: RuleMatch
/// 来源元数据:kind 恒为 `custom`,upstreamVersion/label 供冲突说明与展示。
let source: RuleSourceIdentity
init(
id: UUID = UUID(),
action: RuleAction,
match: RuleMatch,
source: RuleSourceIdentity? = nil
) {
self.id = id
self.action = action
self.match = match
self.source =
source
?? RuleSourceIdentity(kind: .custom, upstreamVersion: "user", label: "自定义")
}
/// 折叠为来源无关的运行时规则,参与 ACL 合并。
var proxyRule: ProxyRule {
ProxyRule(action: action, match: identity.match)
}
var identity: RuleIdentity { RuleIdentity(action: action, match: match) }
/// 稳定的内容身份(不含 UUID),用于去重与诊断摘要。
var contentToken: String {
identity.contentToken
}
}
// MARK: - 持久化集合
/// 自定义规则持久化文档(issue #66):schema 版本 + 规则数组。
struct CustomRuleDocument: Codable, Equatable, Sendable {
static let currentSchemaVersion = 2
let schemaVersion: Int
let rules: [CustomRule]
let disabledIdentities: Set<RuleIdentity>
init(rules: [CustomRule], disabledIdentities: Set<RuleIdentity> = []) {
schemaVersion = Self.currentSchemaVersion
self.rules = rules
self.disabledIdentities = disabledIdentities
}
private enum CodingKeys: String, CodingKey { case schemaVersion, rules, disabledIdentities }
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
schemaVersion = try values.decode(Int.self, forKey: .schemaVersion)
guard [1, Self.currentSchemaVersion].contains(schemaVersion) else {
throw CustomRuleStoreError.schemaVersionMismatch(
found: schemaVersion, expected: Self.currentSchemaVersion)
}
rules = try values.decode([CustomRule].self, forKey: .rules)
let disabled =
schemaVersion == 1 ? [] : try values.decode([RuleIdentity].self, forKey: .disabledIdentities)
disabledIdentities = Set(disabled)
guard disabledIdentities.count == disabled.count,
Set(rules.map(\.id)).count == rules.count,
Set(rules.map(\.identity)).count == rules.count,
rules.allSatisfy({ $0.source.kind == .custom })
else { throw CustomRuleStoreError.corrupt(detail: "Duplicate or invalid user rule records") }
}
func encode(to encoder: Encoder) throws {
var values = encoder.container(keyedBy: CodingKeys.self)
try values.encode(Self.currentSchemaVersion, forKey: .schemaVersion)
try values.encode(rules, forKey: .rules)
try values.encode(
disabledIdentities.map { ($0, $0.contentToken) }.sorted { $0.1 < $1.1 }.map(\.0),
forKey: .disabledIdentities)
}
}
enum CustomRuleStoreError: Error, Equatable, Sendable {
case missing
case corrupt(detail: String)
case schemaVersionMismatch(found: Int, expected: Int)
case ioFailure(detail: String)
}
// MARK: - 诊断摘要
/// 自定义规则的安全诊断摘要(issue #66 AC5):只含数量与内容版本,
/// 不导出原始自定义域名或来源内容。
struct CustomRuleSummary: Equatable, Sendable {
/// 规则条数。
let count: Int
/// 内容版本:规则内容(动作+匹配)的稳定短摘要。
let contentVersion: String
static func summarizing(_ document: CustomRuleDocument) -> CustomRuleSummary {
let tokens =
document.rules.map(\.contentToken).sorted()
+ document.disabledIdentities.map { "disabled:" + $0.contentToken }.sorted()
return CustomRuleSummary(
count: document.rules.count,
contentVersion: String(ProxyACLDocument.digest(tokens.joined(separator: "\n")).prefix(12)))
}
static func summarizing(_ rules: [CustomRule]) -> CustomRuleSummary {
CustomRuleSummary(count: rules.count, contentVersion: contentVersion(of: rules))
}
/// 内容版本按 (action, match) 排序后摘要;不含 UUID,重排不改版本。
static func contentVersion(of rules: [CustomRule]) -> String {
let payload =
rules
.map(\.contentToken)
.sorted()
.joined(separator: "\n")
return String(ProxyACLDocument.digest(payload).prefix(12))
}
}