Files
ShadowsocksX-NG/ShadowsocksX-NG2/Domain/Runtime/ProxyPortSemantics.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

79 lines
3.1 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.
/// 本地代理端点的端口语义(spec #21 D8,issue #30):端口是显式用户配置,
/// 出厂默认与 Legacy 隔离(SOCKS5 11086 / HTTP 11087);系统任何路径
/// 都不静默改端口——建议端口只产出候选,必须经用户确认保存后才生效;激活
/// 是否成功以 runtime 实际绑定为准(健康门禁),本层的占用校验只是设置编辑
/// 期尽力而为的即时提示。
/// 本地代理端点;显示名与 D8 及诊断报告的措辞一致。PAC 端点已随 issue #67 移除。
enum ProxyEndpointKind: Hashable, Sendable, CaseIterable {
case socks
case http
var displayName: String {
switch self {
case .socks: "SOCKS5"
case .http: "HTTP"
}
}
}
/// 端口配置校验错误:点名端点与端口(D8 错误信息要求)。
enum PortSettingError: Error, Hashable, Sendable {
case portOutOfRange(endpoint: ProxyEndpointKind, port: Int)
/// 两个端点的配置端口相同;`endpoint` 在枚举序(socks → http)中在前。
case duplicatePort(endpoint: ProxyEndpointKind, otherEndpoint: ProxyEndpointKind, port: Int)
}
/// 建议空闲端口算法(D8):32768–65535 高位段升序扫描,跳过排除集合与不
/// 空闲端口。空闲判定由调用方注入,保证主缝可测。
enum PortSuggestion {
static let range = 32768...65535
static func firstFree(excluding excludedPorts: Set<Int>, isFree: (Int) -> Bool) -> Int? {
for candidate in range where !excludedPorts.contains(candidate) {
if isFree(candidate) { return candidate }
}
return nil
}
}
extension SslocalListenSettings {
/// 端点的配置端口。
func configuredPort(for endpoint: ProxyEndpointKind) -> Int {
switch endpoint {
case .socks: socksPort
case .http: httpPort
}
}
/// 端口配置校验:范围 1000–65535、两端点配置值互异。错误按固定次序
/// (socks/http 范围检查,再 socks-http 冲突)。
func portValidationErrors() -> [PortSettingError] {
var errors: [PortSettingError] = []
for endpoint in ProxyEndpointKind.allCases {
let port = configuredPort(for: endpoint)
if !ProxyPortRange.valid.contains(port) {
errors.append(.portOutOfRange(endpoint: endpoint, port: port))
}
}
for pair in Self.endpointPairs {
let first = configuredPort(for: pair.0)
if first == configuredPort(for: pair.1) {
errors.append(.duplicatePort(endpoint: pair.0, otherEndpoint: pair.1, port: first))
}
}
return errors
}
/// 为 `endpoint` 建议一个空闲端口:高位段升序首个空闲且不等于另个端点
/// 配置值的候选;整段无可用返回 nil。只产出候选,绝不替用户改配置。
func suggestedPort(for endpoint: ProxyEndpointKind, isFree: (Int) -> Bool) -> Int? {
let others = Set(ProxyEndpointKind.allCases.filter { $0 != endpoint }.map(configuredPort))
return PortSuggestion.firstFree(excluding: others, isFree: isFree)
}
private static let endpointPairs: [(ProxyEndpointKind, ProxyEndpointKind)] = [
(.socks, .http)
]
}