mirror of
https://github.com/shadowsocks/ShadowsocksX-NG.git
synced 2026-11-15 18:59:52 +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 文件时两个用例均 失败并点名违规文件,清理后复验为绿。
151 lines
5.8 KiB
Swift
151 lines
5.8 KiB
Swift
import Foundation
|
|
|
|
/// 白名单订阅资料,不含服务器连接字段、凭据或原始响应。
|
|
struct SubscriptionInformation: Codable, Equatable, Sendable {
|
|
var bytesUsed: UInt64?
|
|
var bytesRemaining: UInt64?
|
|
var expiresAt: Date?
|
|
var trafficResetAt: Date?
|
|
|
|
var isEmpty: Bool {
|
|
bytesUsed == nil && bytesRemaining == nil && expiresAt == nil && trafficResetAt == nil
|
|
}
|
|
|
|
/// 与服务器及分组扩展的解码分别处理;无效附加资料只影响该项。
|
|
static func parse(_ data: Data) -> Self {
|
|
let root = rootFields(data)
|
|
return Self(
|
|
bytesUsed: byteCount(root["bytes_used"]),
|
|
bytesRemaining: byteCount(root["bytes_remaining"]),
|
|
expiresAt: date(root["expires_at"]),
|
|
trafficResetAt: date(root["traffic_reset_at"]))
|
|
}
|
|
|
|
private static func byteCount(_ value: Data?) -> UInt64? {
|
|
guard let value, let raw = String(bytes: value, encoding: .utf8) else { return nil }
|
|
let text = raw.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
guard !text.isEmpty, text.utf8.allSatisfy({ (48...57).contains($0) }) else { return nil }
|
|
return UInt64(text)
|
|
}
|
|
|
|
private static func date(_ value: Data?) -> Date? {
|
|
guard let value, let text = try? JSONDecoder().decode(String.self, from: value) else {
|
|
return nil
|
|
}
|
|
let normalized = text.uppercased()
|
|
let pattern =
|
|
#"\A[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])T"#
|
|
+ #"([01][0-9]|2[0-3]):[0-5][0-9]:([0-5][0-9]|60)(\.[0-9]+)?"#
|
|
+ #"(Z|[+-]([01][0-9]|2[0-3]):[0-5][0-9])\z"#
|
|
guard normalized.range(of: pattern, options: .regularExpression) != nil else { return nil }
|
|
// RFC 3339 的偏移可达 23:59,超出 Foundation 时区解析器的 18 小时范围。
|
|
// 先解析原地时间,再应用数值偏移;不创建会失败的 TimeZone。
|
|
let localText = normalized.hasSuffix("Z") ? normalized : String(normalized.dropLast(6)) + "Z"
|
|
guard
|
|
let local = try? Date.ISO8601FormatStyle(
|
|
includingFractionalSeconds: normalized.contains(".")
|
|
).parse(localText)
|
|
else { return nil }
|
|
let parsed = local.addingTimeInterval(-Double(offsetSeconds(normalized)))
|
|
// Foundation 会把 2 月 30 日等无效日期向后归一化;按原偏移回读以拒绝这些值。
|
|
let parts = normalized.prefix(19).split(whereSeparator: { "-T:".contains($0) }).compactMap {
|
|
Int($0)
|
|
}
|
|
var calendar = Calendar(identifier: .gregorian)
|
|
calendar.timeZone = .gmt
|
|
let isLeapSecond = parts[5] == 60
|
|
let checked = isLeapSecond ? local.addingTimeInterval(-1) : local
|
|
let fields: Set<Calendar.Component> = [.year, .month, .day, .hour, .minute, .second]
|
|
let components = calendar.dateComponents(fields, from: checked)
|
|
guard components.year == parts[0], components.month == parts[1],
|
|
components.day == parts[2], components.hour == parts[3], components.minute == parts[4],
|
|
components.second == (isLeapSecond ? 59 : parts[5])
|
|
else { return nil }
|
|
if isLeapSecond {
|
|
let utc = calendar.dateComponents(fields, from: parsed.addingTimeInterval(-1))
|
|
guard utc.hour == 23, utc.minute == 59,
|
|
(utc.month == 6 && utc.day == 30) || (utc.month == 12 && utc.day == 31)
|
|
else { return nil }
|
|
}
|
|
return parsed
|
|
}
|
|
|
|
private static func offsetSeconds(_ text: String) -> Int {
|
|
guard !text.hasSuffix("Z") else { return 0 }
|
|
let offset = text.suffix(6)
|
|
let hours = Int(offset.dropFirst().prefix(2))!
|
|
let minutes = Int(offset.suffix(2))!
|
|
return (hours * 60 + minutes) * 60 * (offset.first == "-" ? -1 : 1)
|
|
}
|
|
/// JSON 结构已由订阅文档解析器校验。这里只切出根级白名单值的原始片段,
|
|
/// 不把整个正文转换成 Any;某项超大数字因此不会损坏其他项的解码。
|
|
private static func rootFields(_ data: Data) -> [String: Data] {
|
|
let bytes = Array(data)
|
|
let names: Set<String> = ["bytes_used", "bytes_remaining", "expires_at", "traffic_reset_at"]
|
|
var result: [String: Data] = [:]
|
|
// 与现有 JSONDecoder 一致,接受可选的 UTF-8 BOM。
|
|
var cursor = bytes.starts(with: [0xEF, 0xBB, 0xBF]) ? 3 : 0
|
|
skipWhitespace(bytes, cursor: &cursor)
|
|
guard cursor < bytes.count, bytes[cursor] == 123 else { return result }
|
|
cursor += 1
|
|
while cursor < bytes.count {
|
|
skipWhitespace(bytes, cursor: &cursor)
|
|
guard cursor < bytes.count, bytes[cursor] == 34 else { break }
|
|
let keyStart = cursor
|
|
skipString(bytes, cursor: &cursor)
|
|
let key = try? JSONDecoder().decode(String.self, from: Data(bytes[keyStart..<cursor]))
|
|
skipWhitespace(bytes, cursor: &cursor)
|
|
guard cursor < bytes.count, bytes[cursor] == 58 else { break }
|
|
cursor += 1
|
|
let valueStart = cursor
|
|
skipValue(bytes, cursor: &cursor)
|
|
if let key, names.contains(key) {
|
|
result[key] = Data(bytes[valueStart..<cursor])
|
|
}
|
|
guard cursor < bytes.count, bytes[cursor] == 44 else { break }
|
|
cursor += 1
|
|
}
|
|
return result
|
|
}
|
|
|
|
private static func skipValue(_ bytes: [UInt8], cursor: inout Int) {
|
|
var depth = 0
|
|
while cursor < bytes.count {
|
|
switch bytes[cursor] {
|
|
case 34:
|
|
skipString(bytes, cursor: &cursor)
|
|
continue
|
|
case 123, 91:
|
|
depth += 1
|
|
case 125, 93:
|
|
guard depth > 0 else { return }
|
|
depth -= 1
|
|
case 44:
|
|
if depth == 0 { return }
|
|
default:
|
|
break
|
|
}
|
|
cursor += 1
|
|
}
|
|
}
|
|
|
|
private static func skipWhitespace(_ bytes: [UInt8], cursor: inout Int) {
|
|
while cursor < bytes.count, [9, 10, 13, 32].contains(bytes[cursor]) { cursor += 1 }
|
|
}
|
|
|
|
private static func skipString(_ bytes: [UInt8], cursor: inout Int) {
|
|
cursor += 1
|
|
while cursor < bytes.count {
|
|
if bytes[cursor] == 92 {
|
|
cursor = min(cursor + 2, bytes.count)
|
|
} else if bytes[cursor] == 34 {
|
|
cursor += 1
|
|
return
|
|
} else {
|
|
cursor += 1
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|