Files
ShadowsocksX-NG/ShadowsocksX-NG2/Domain/Rules/RuleAnalysis.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

112 lines
4.1 KiB
Swift

import Foundation
/// Coverage is explanatory, never a persistence rejection or a routing priority.
struct RuleRelationship: Equatable, Sendable {
enum Kind: Hashable, Sendable { case absorption, shadowing }
enum Extent: Sendable { case full, partial }
let rule: RuleIdentity
let kind: Kind
let extent: Extent
let covering: [RuleIdentity]
}
/// Analyze the complete candidate set. Optional subjects limit explanation work
/// to custom entries at the existing validation seam without narrowing context.
struct RuleAnalysis: Equatable, Sendable {
let identities: [RuleIdentity]
let relationships: [RuleRelationship]
var contentVersion: String {
String(
ProxyACLDocument.digest(identities.map(\.contentToken).joined(separator: "\n")).prefix(12))
}
init(rules: [ProxyRule], subjects: [ProxyRule]? = nil) {
let identities = Set(rules.map(\.identity)).map { ($0, $0.contentToken) }
.sorted { $0.1 < $1.1 }.map(\.0)
self.identities = identities
let targets = Set((subjects ?? rules).map(\.identity))
relationships = identities.filter { targets.contains($0) }.flatMap { target in
[RuleRelationship.Kind.absorption, .shadowing].compactMap { kind in
let covering = identities.filter { other in
guard other != target else { return false }
switch kind {
case .absorption:
guard other.action == target.action else { return false }
case .shadowing:
guard other.action != target.action,
RuleCoverage.winningAction(for: target.match) == other.action
else { return false }
}
return RuleCoverage.intersects(other.match, target.match)
}
guard !covering.isEmpty else { return nil }
return RuleRelationship(
rule: target, kind: kind,
extent: RuleCoverage.fullyCovered(target.match, by: covering.map(\.match))
? .full : .partial,
covering: covering)
}
}
}
}
/// Fixed policy includes a semantic simple-hostname condition, not an editable
/// regex identity. Coverage keeps that condition separate from ordinary rules.
struct FixedRuleCoverage: Equatable, Sendable {
let extent: RuleRelationship.Extent
let matches: [RuleMatch]
let includesSimpleHostname: Bool
}
extension RuleCoverage {
static let fixedLocalMatches: [RuleMatch] = {
FixedLocalProxyRanges.ipRanges.compactMap { value in
value.contains(":") ? try? RuleMatch(ipv6CIDR: value) : try? RuleMatch(ipv4CIDR: value)
} + [.domainSuffix("localhost"), .domainSuffix("local")]
}()
static func fixedLocalCoverage(of match: RuleMatch) -> FixedRuleCoverage? {
let matches = fixedLocalMatches.filter { intersects($0, match) }
let simple: Bool
let simpleIsFull: Bool
switch match {
case .domainExact(let value):
simple = !value.contains(".")
simpleIsFull = simple
case .domainSuffix(let value):
simple = !value.contains(".")
simpleIsFull = false
case .ipv4CIDR, .ipv6CIDR:
simple = false
simpleIsFull = false
}
guard simple || !matches.isEmpty else { return nil }
return FixedRuleCoverage(
extent: simpleIsFull || fullyCovered(match, by: matches) ? .full : .partial,
matches: matches, includesSimpleHostname: simple)
}
}
extension RuleAnalysis {
/// Select the actual ACL skeleton before calculating coverage.
static func runtimeCandidates(_ rules: [ProxyRule], defaultAction: RuleDefaultAction)
-> [ProxyRule]
{
let expressed = rules.filter { defaultAction != .proxyWhenUnmatched || $0.action == .direct }
let index = RulesOverlapIndex(rules: expressed)
return expressed.filter { rule in
let analysis = RuleAnalysis(rules: index.overlapping(rule.identity.match), subjects: [rule])
return !analysis.relationships.contains {
$0.extent == .full
&& ($0.kind == .shadowing
|| $0.covering.contains { other in
RuleCoverage.fullyCovered(rule.identity.match, by: [other.match])
&& (!RuleCoverage.fullyCovered(other.match, by: [rule.identity.match])
|| other.contentToken < rule.identity.contentToken)
})
}
}
}
}