mirror of
https://github.com/shadowsocks/ShadowsocksX-NG.git
synced 2026-11-22 13:34:00 +00:00
feat(proxy): add GFWList proxy candidates for direct-when-unmatched rule mode
This commit is contained in:
+2
-2
@@ -14,7 +14,7 @@
|
||||
- **Proxy mode**: The user-facing way traffic is routed through the local proxy: rule mode, local PAC, global (public targets default through Shadowsocks, fixed local targets stay direct via a minimal ACL), or direct mode through an ACL-backed local SOCKS endpoint.
|
||||
- **Rule mode**: The ACL-backed routing mode in which built-in (and later custom) rules choose proxy versus direct per target; the unmatched default action is a separate persisted sub-option.
|
||||
- **Rule default action**: The rule-mode sub-option that decides proxy versus direct for targets no rule matches. Factory default is proxy-when-unmatched. _Avoid_: fallback (ambiguous).
|
||||
- **Built-in rule snapshot**: An app-shipped, pinned rule set produced by an explicit maintainer update; ordinary builds only read local snapshots and never fetch or convert. Two sources ship today: **geolocation-cn** (China domain direct candidates) and **china-ipv4** (China mainland IPv4 CIDR direct candidates from gaoyifan/china-operator-ip). Each snapshot carries its own source identity, digest, license, and attribution.
|
||||
- **Built-in rule snapshot**: An app-shipped, pinned rule set produced by an explicit maintainer update; ordinary builds only read local snapshots and never fetch or convert. Three sources ship today: **geolocation-cn** (China domain direct candidates), **china-ipv4** (China mainland IPv4 CIDR direct candidates from gaoyifan/china-operator-ip), and **gfwlist** (proxy candidates from the official Base64 AutoProxy GFWList; only rules losslessly expressible as target domains, with `@@` exceptions shadowed by a broader proxy rule omitted and reported). Each snapshot carries its own source identity, digest, license, attribution, and conversion loss report.
|
||||
- **Local PAC**: The PAC document generated and served by the 2.0 proxy runtime; it is distinct from a user-provided remote PAC URL.
|
||||
- **Proxy mode selector**: The common user action for choosing local PAC, global, or direct mode; it is distinct from enabling or disabling the proxy runtime, from the system proxy switch, and from editing PAC user rules. Its choice persists across GUI restarts.
|
||||
- **Agent switch**: The user's independent on/off choice for the proxy agent (the per-user background proxy runtime). It defaults to on, so a first run starts the agent and keeps local SOCKS/HTTP listening even before any active target exists; an explicit off is persisted, survives GUI restarts, and quitting the GUI never stops the launchd-hosted agent. Turning the agent off first restores the system proxy settings 2.0 holds and then stops the local listeners. _Avoid_: proxy switch (ambiguous with the system proxy switch).
|
||||
@@ -78,7 +78,7 @@
|
||||
- Exported GUI event lines are an allowlisted, field-cleaned subset of runtime events; raw event detail remains available only through the explicit diagnostic log view.
|
||||
- A diagnostic report presents fixed proxy-state categories rather than free-form failure detail. If its safe projection cannot be constructed, no report is emitted; an unavailable individual fact source remains a marked partial fact instead.
|
||||
- A diagnostic export is considered complete only after the user-selected report file is successfully written; opening the export panel or preparing report text is not an export completion.
|
||||
- Proxy modes are mutually exclusive: rule mode with proxy-when-unmatched writes the local SOCKS proxy and runs sslocal with a `[proxy_all]` ACL whose bypass list is the fixed local safety rules plus China domain and China IPv4 CIDR direct candidates from the built-in snapshots; local PAC writes the runtime PAC URL; global writes the local SOCKS proxy and runs sslocal with a `[proxy_all]` ACL whose bypass list is only the fixed local safety rules (no China list, GFWList, or custom rules); direct mode writes the local SOCKS proxy and runs sslocal with `[bypass_all]`, so SOCKS and HTTP requests go direct without a Shadowsocks server. IP-CIDR matching may cause sslocal to issue local DNS queries for unmatched hostnames; the product does not promise that all DNS queries travel through the remote Shadowsocks server. In both ACL-backed modes the SystemConfiguration exceptions include the same fixed local scope — IPv4 loopback, RFC1918, link-local, IPv6 loopback, link-local, unique-local, `localhost`, `.local`, and no-dot hostnames — merged with the user's exception list; CGNAT is excluded. IPv6 CIDR strings in system exceptions are not a verified bypass; the ACL is the routing guarantee for requests that enter sslocal, including LAN-sharing clients. ACL path, content, digest, or summary changes require a full sslocal restart. The wrapper publishes the ACL-backed receipt only after the child owns every configured TCP listener; the controller retains the previous mode, runtime, and applied system proxy until this instance is verified, and restores the previous runtime on verification failure. System proxy writes are allowed only while the system proxy switch is on and the agent is healthy with an available exit; 2.0 stores the complete per-service Proxies dictionaries it owns and restores them only while the applied values still match, otherwise it reports an ownership conflict. When a PAC/global active target becomes invalid, the agent keeps listening but the held system proxy configuration is safely withdrawn while the system proxy intent stays on.
|
||||
- Proxy modes are mutually exclusive: rule mode with proxy-when-unmatched writes the local SOCKS proxy and runs sslocal with a `[proxy_all]` ACL whose bypass list is the fixed local safety rules plus China domain and China IPv4 CIDR direct candidates from the built-in snapshots; rule mode with direct-when-unmatched runs sslocal with a `[bypass_all]` ACL whose proxy list holds only expressible, unshadowed GFWList proxy candidates (the two default actions never unconditionally union all built-in sources), so unmatched targets and IP literals with no matching rule go direct; local PAC writes the runtime PAC URL; global writes the local SOCKS proxy and runs sslocal with a `[proxy_all]` ACL whose bypass list is only the fixed local safety rules (no China list, GFWList, or custom rules); direct mode writes the local SOCKS proxy and runs sslocal with `[bypass_all]`, so SOCKS and HTTP requests go direct without a Shadowsocks server. IP-CIDR matching may cause sslocal to issue local DNS queries for unmatched hostnames; the product does not promise that all DNS queries travel through the remote Shadowsocks server. In both ACL-backed modes the SystemConfiguration exceptions include the same fixed local scope — IPv4 loopback, RFC1918, link-local, IPv6 loopback, link-local, unique-local, `localhost`, `.local`, and no-dot hostnames — merged with the user's exception list; CGNAT is excluded. IPv6 CIDR strings in system exceptions are not a verified bypass; the ACL is the routing guarantee for requests that enter sslocal, including LAN-sharing clients. ACL path, content, digest, or summary changes require a full sslocal restart. The wrapper publishes the ACL-backed receipt only after the child owns every configured TCP listener; the controller retains the previous mode, runtime, and applied system proxy until this instance is verified, and restores the previous runtime on verification failure. System proxy writes are allowed only while the system proxy switch is on and the agent is healthy with an available exit; 2.0 stores the complete per-service Proxies dictionaries it owns and restores them only while the applied values still match, otherwise it reports an ownership conflict. When a PAC/global active target becomes invalid, the agent keeps listening but the held system proxy configuration is safely withdrawn while the system proxy intent stays on.
|
||||
- The proxy mode selector's choice and the rule default action persist with the settings snapshot and survive GUI restarts; a persistence failure keeps the previous mode and sub-option in force and names the reason. Rule, local PAC, global, and direct mode are always available, and user-provided remote PAC URLs are not accepted as proxy settings. Resetting preferences returns the mode to local PAC and the rule default action to proxy-when-unmatched.
|
||||
- The GUI is the user-facing manager; the Shadowsocks tunnel service is an external runtime boundary rather than part of the GUI's domain model.
|
||||
- The presentation form is governed solely by the workspace window: opening it enters the foreground form, and closing it returns the app to the background form while the GUI process keeps running with the status menu available; hiding the window without closing it does not leave the foreground form.
|
||||
|
||||
@@ -92,17 +92,29 @@ extension ProxyRuntimeController {
|
||||
case .global:
|
||||
return document.replacingACL(.global(at: runtimeFileStore.aclFileURL))
|
||||
case .rule:
|
||||
let china = try chinaDirectRules()
|
||||
let candidates = try ruleModeCandidateRules()
|
||||
return document.replacingACL(
|
||||
.rule(
|
||||
at: runtimeFileStore.aclFileURL,
|
||||
defaultAction: settings.ruleDefaultAction,
|
||||
chinaRules: china))
|
||||
rules: candidates))
|
||||
case .pac:
|
||||
return document.replacingACL(nil)
|
||||
}
|
||||
}
|
||||
|
||||
/// 规则模式候选按默认动作取对应来源(issue #65):「未匹配时代理」用中国
|
||||
/// 直连候选;「未匹配时直连」用 GFWList 代理候选。两种默认动作不把全部
|
||||
/// 内置来源无条件并集。
|
||||
func ruleModeCandidateRules() throws -> [ProxyRule] {
|
||||
switch settings.ruleDefaultAction {
|
||||
case .proxyWhenUnmatched:
|
||||
return try chinaDirectRules()
|
||||
case .directWhenUnmatched:
|
||||
return try gfwlistRules()
|
||||
}
|
||||
}
|
||||
|
||||
/// 内置中国直连候选:geolocation-cn 域名 + china-operator-ip IPv4 CIDR
|
||||
/// (issue #63/#64)。快照缺失/损坏/版本不匹配时抛错,调用方必须失败
|
||||
/// 并保留旧 ACL,不得静默退化成全局(issue #63 AC4)。
|
||||
@@ -112,6 +124,12 @@ extension ProxyRuntimeController {
|
||||
return BuiltinRuleCatalog.chinaDirectRules(from: [geolocation, chinaIPv4])
|
||||
}
|
||||
|
||||
/// GFWList 候选(issue #65):可准确表达且未被更宽代理规则遮蔽的规则
|
||||
/// (含未遮蔽例外),参与 `bypass_all` ACL 编译。
|
||||
func gfwlistRules() throws -> [ProxyRule] {
|
||||
BuiltinRuleCatalog.gfwlistRules(from: try BuiltinRuleCatalog.loadGFWList())
|
||||
}
|
||||
|
||||
private func deployModeTransition(
|
||||
_ document: SslocalRuntimeDocument,
|
||||
snapshot: ModeTransitionSnapshot,
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import Foundation
|
||||
|
||||
/// 内置规则目录(issue #63/#64):从 bundle 资源加载已固定的 geolocation-cn
|
||||
/// 与 china-ipv4 快照。普通构建只读本地快照,运行时不抓取或转换;快照缺失/
|
||||
/// 损坏/版本不匹配时加载失败,不生成空规则 ACL。
|
||||
/// 内置规则目录(issue #63/#64/#65):从 bundle 资源加载已固定的 geolocation-cn、
|
||||
/// china-ipv4 与 gfwlist 快照。普通构建只读本地快照,运行时不抓取或转换;
|
||||
/// 快照缺失/损坏/版本不匹配时加载失败,不生成空规则 ACL。
|
||||
struct BuiltinRuleCatalog {
|
||||
/// 生产加载缝:bundle 内固定快照(`rules/<source>/snapshot.json`)。
|
||||
static func loadGeolocationCN(from bundle: Bundle = .main) throws -> RuleSnapshot {
|
||||
@@ -14,6 +14,11 @@ struct BuiltinRuleCatalog {
|
||||
try loadSnapshot(named: "china-ipv4", from: bundle)
|
||||
}
|
||||
|
||||
/// GFWList 代理候选(issue #65):仅含可准确表达且未被更宽代理规则遮蔽的规则。
|
||||
static func loadGFWList(from bundle: Bundle = .main) throws -> RuleSnapshot {
|
||||
try loadSnapshot(named: "gfwlist", from: bundle)
|
||||
}
|
||||
|
||||
private static func loadSnapshot(named name: String, from bundle: Bundle) throws -> RuleSnapshot {
|
||||
// folder reference 保留 `rules/<source>/snapshot.json` 目录结构。
|
||||
guard
|
||||
@@ -34,4 +39,10 @@ struct BuiltinRuleCatalog {
|
||||
static func chinaDirectRules(from snapshots: [RuleSnapshot]) -> [ProxyRule] {
|
||||
snapshots.flatMap { chinaDirectRules(from: $0) }
|
||||
}
|
||||
|
||||
/// 供 ACL 编译的 GFWList 候选(issue #65):可准确表达且未被遮蔽的规则
|
||||
/// (代理候选 + 未遮蔽例外),两种动作都参与编译。
|
||||
static func gfwlistRules(from snapshot: RuleSnapshot) -> [ProxyRule] {
|
||||
snapshot.rules
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,394 @@
|
||||
import Foundation
|
||||
|
||||
// MARK: - AutoProxy 条目
|
||||
|
||||
/// GFWList / AutoProxy 0.2.9 单行规则的分类结果。
|
||||
/// 只有可按目标域名无损表达的规则进入领域模型;已知不可表达形式按类计数。
|
||||
enum AutoProxyEntry: Equatable, Sendable {
|
||||
case header
|
||||
case comment
|
||||
case blank
|
||||
/// `||host` / `||host^`:域名及其子域 → domainSuffix。
|
||||
case domainProxy(host: String, original: String)
|
||||
/// `@@||host` / `@@||host^`:例外(直连)→ domainSuffix。
|
||||
case domainException(host: String, original: String)
|
||||
/// `|http://...` 等 URL 锚点(含协议条件),不扩大为整域名。
|
||||
case urlPrefix(original: String, isException: Bool)
|
||||
/// `||host/path` 或 `@@||host/path`:URL 路径条件,不扩大为整域名。
|
||||
case urlPath(original: String, isException: Bool)
|
||||
/// `||host$option`:过滤选项条件。
|
||||
case filterOption(original: String, isException: Bool)
|
||||
/// 通配符模式(`*`)。
|
||||
case wildcard(original: String, isException: Bool)
|
||||
/// `/regex/`。
|
||||
case regexp(original: String, isException: Bool)
|
||||
/// 裸文本(AutoProxy 子串匹配),不是域名规则。
|
||||
case plainText(original: String, isException: Bool)
|
||||
/// `||x` 单标签域名前缀:AutoProxy 语义是前缀匹配,不等价于 domainSuffix。
|
||||
case singleLabelPrefix(original: String, isException: Bool)
|
||||
/// `||1.2.3.4` 等 IP 字面量域名规则:不是 CIDR,不得猜测成 IP 代理规则。
|
||||
case ipLiteral(original: String, isException: Bool)
|
||||
}
|
||||
|
||||
/// AutoProxy 0.2.9 行解析器。未知语法抛错,阻止更新。
|
||||
enum AutoProxyParser {
|
||||
static func parseLine(_ rawLine: String) throws -> AutoProxyEntry {
|
||||
let line = rawLine.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
if line.isEmpty { return .blank }
|
||||
if line.hasPrefix("!") { return .comment }
|
||||
if line.hasPrefix("[") && line.hasSuffix("]") { return .header }
|
||||
|
||||
let isException = line.hasPrefix("@@")
|
||||
let pattern = isException ? String(line.dropFirst(2)) : line
|
||||
|
||||
if pattern.hasPrefix("||") {
|
||||
return try parseDomainPattern(
|
||||
String(pattern.dropFirst(2)), original: line, isException: isException)
|
||||
}
|
||||
if pattern.hasPrefix("|") {
|
||||
// URL 锚点:`|http://...` 含协议条件,不能无损映射为域名/IP。
|
||||
return .urlPrefix(original: line, isException: isException)
|
||||
}
|
||||
if pattern.hasPrefix("/"), pattern.hasSuffix("/"), pattern.count >= 3 {
|
||||
return .regexp(original: line, isException: isException)
|
||||
}
|
||||
if pattern.contains("*") {
|
||||
return .wildcard(original: line, isException: isException)
|
||||
}
|
||||
if looksLikeIPLiteral(pattern) {
|
||||
return .ipLiteral(original: line, isException: isException)
|
||||
}
|
||||
if isPlainDomainish(pattern) {
|
||||
return .plainText(original: line, isException: isException)
|
||||
}
|
||||
throw GFWListConverter.ConversionError.unknownSyntax(line)
|
||||
}
|
||||
|
||||
private static func parseDomainPattern(
|
||||
_ hostPart: String, original: String, isException: Bool
|
||||
) throws -> AutoProxyEntry {
|
||||
// `||host/path`:路径条件(优先于通配符,便于损失分类)。
|
||||
if hostPart.contains("/") {
|
||||
return .urlPath(original: original, isException: isException)
|
||||
}
|
||||
if hostPart.contains("*") {
|
||||
return .wildcard(original: original, isException: isException)
|
||||
}
|
||||
if hostPart.contains("$") {
|
||||
return .filterOption(original: original, isException: isException)
|
||||
}
|
||||
// `||host^` 或 `||host^rest`:`^` 是分隔符锚点;仅允许结尾单个 `^`。
|
||||
var host = hostPart
|
||||
if host.hasSuffix("^") {
|
||||
host = String(host.dropLast())
|
||||
if host.contains("^") {
|
||||
throw GFWListConverter.ConversionError.unknownSyntax(original)
|
||||
}
|
||||
}
|
||||
if looksLikeIPLiteral(host) {
|
||||
return .ipLiteral(original: original, isException: isException)
|
||||
}
|
||||
// 单标签(如 `google`)是域名前缀匹配,不等价于 domainSuffix。
|
||||
if !host.contains(".") {
|
||||
return .singleLabelPrefix(original: original, isException: isException)
|
||||
}
|
||||
guard isValidDomainHost(host) else {
|
||||
throw GFWListConverter.ConversionError.unknownSyntax(original)
|
||||
}
|
||||
if isException {
|
||||
return .domainException(host: host.lowercased(), original: original)
|
||||
}
|
||||
return .domainProxy(host: host.lowercased(), original: original)
|
||||
}
|
||||
|
||||
private static func isValidDomainHost(_ host: String) -> Bool {
|
||||
!host.isEmpty && !host.hasPrefix(".") && !host.hasSuffix(".")
|
||||
&& host.allSatisfy { character in
|
||||
character.isASCII
|
||||
&& (character.isLetter || character.isNumber || character == "-" || character == ".")
|
||||
}
|
||||
}
|
||||
|
||||
private static func looksLikeIPLiteral(_ text: String) -> Bool {
|
||||
var address = in_addr()
|
||||
if text.withCString({ inet_pton(AF_INET, $0, &address) }) == 1 { return true }
|
||||
var address6 = in6_addr()
|
||||
if text.withCString({ inet_pton(AF_INET6, $0, &address6) }) == 1 { return true }
|
||||
// `1.2.3.4` 形态(非法数字也按 IP 字面量意图处理,避免落成 plainText)。
|
||||
let parts = text.split(separator: ".")
|
||||
return parts.count == 4 && parts.allSatisfy { $0.allSatisfy(\.isNumber) }
|
||||
}
|
||||
|
||||
private static func isPlainDomainish(_ text: String) -> Bool {
|
||||
text.contains(".")
|
||||
&& text.allSatisfy { character in
|
||||
character.isASCII
|
||||
&& (character.isLetter || character.isNumber || character == "-" || character == ".")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - 转换器
|
||||
|
||||
/// GFWList 转换器(issue #65):解析官方 Base64 AutoProxy 列表,只转换可按
|
||||
/// 目标域名无损表达的规则;不把 URL 路径或协议条件扩大为整域名。被更宽代理
|
||||
/// 规则遮蔽的 `@@` 例外不写入无效 ACL 项,保留代理规则并逐项报告。未知语法、
|
||||
/// 损坏输入或异常缩小阻止更新;普通构建离线读取已固定快照。
|
||||
struct GFWListConverter {
|
||||
/// 转换异常规模阈值。
|
||||
static let minimumRuleCount = 100
|
||||
static let maximumRuleCount = 50_000
|
||||
/// 相对上一份快照的规模漂移阈值(issue #65 异常缩小)。
|
||||
static let minimumScaleRatio = 0.5
|
||||
static let maximumScaleRatio = 2.0
|
||||
|
||||
let source: RuleSourceIdentity
|
||||
let minimumRuleCount: Int
|
||||
let maximumRuleCount: Int
|
||||
|
||||
init(
|
||||
source: RuleSourceIdentity,
|
||||
minimumRuleCount: Int = GFWListConverter.minimumRuleCount,
|
||||
maximumRuleCount: Int = GFWListConverter.maximumRuleCount
|
||||
) {
|
||||
self.source = source
|
||||
self.minimumRuleCount = minimumRuleCount
|
||||
self.maximumRuleCount = maximumRuleCount
|
||||
}
|
||||
|
||||
enum ConversionError: Error, Equatable, Sendable {
|
||||
case emptyInput
|
||||
case corruptInput
|
||||
case unknownSyntax(String)
|
||||
case abnormalRuleCount(Int)
|
||||
case abnormalScaleChange(found: Int, previous: Int)
|
||||
}
|
||||
|
||||
/// 快照元数据中由人工输入提供、与具体输入文本无关的部分。
|
||||
struct Provenance: Sendable {
|
||||
let fetchedAt: Date
|
||||
let upstreamReference: String
|
||||
let license: String
|
||||
let attribution: String
|
||||
}
|
||||
|
||||
/// 解码官方 Base64 分发物后再转换。
|
||||
func convert(
|
||||
base64Document: String,
|
||||
provenance: Provenance,
|
||||
previousRuleCount: Int? = nil
|
||||
) throws -> RuleSnapshot {
|
||||
let compact = base64Document.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
guard let decoded = Data(base64Encoded: compact, options: [.ignoreUnknownCharacters]),
|
||||
let document = String(data: decoded, encoding: .utf8)
|
||||
else {
|
||||
throw ConversionError.corruptInput
|
||||
}
|
||||
return try convert(
|
||||
document: document, provenance: provenance, previousRuleCount: previousRuleCount)
|
||||
}
|
||||
|
||||
func convert(
|
||||
document: String,
|
||||
provenance: Provenance,
|
||||
previousRuleCount: Int? = nil
|
||||
) throws -> RuleSnapshot {
|
||||
var report = RuleConversionLossReport()
|
||||
var proxyRules: [ProxyRule] = []
|
||||
var exceptionRules: [ProxyRule] = []
|
||||
var sawCodeLine = false
|
||||
|
||||
for rawLine in document.split(
|
||||
omittingEmptySubsequences: false, whereSeparator: \.isNewline)
|
||||
{
|
||||
let entry = try AutoProxyParser.parseLine(String(rawLine))
|
||||
try accumulate(
|
||||
entry, proxyRules: &proxyRules, exceptionRules: &exceptionRules,
|
||||
sawCodeLine: &sawCodeLine, report: &report)
|
||||
}
|
||||
guard sawCodeLine else { throw ConversionError.emptyInput }
|
||||
|
||||
// 被更宽(或同宽)代理规则遮蔽的 @@ 例外不写入无效 ACL 项。
|
||||
let shadowing = ShadowingAnalyzer.split(
|
||||
proxyRules: proxyRules, exceptionRules: exceptionRules)
|
||||
report.incrementSkipped("shadowedException", by: shadowing.shadowed.count)
|
||||
for (exception, shadowedBy) in shadowing.shadowed {
|
||||
report.notes.append(
|
||||
"shadowed-exception: \(exception.conflict.originalEntry) shadowed-by \(shadowedBy.conflict.originalEntry)"
|
||||
)
|
||||
}
|
||||
|
||||
var rules = RuleSet(rules: shadowing.keptProxy + shadowing.keptExceptions).rules
|
||||
report.convertedCount = rules.count
|
||||
report.absorbedCount = shadowing.shadowed.count
|
||||
try validateRuleCount(rules.count, previousRuleCount: previousRuleCount)
|
||||
|
||||
let metadata = RuleSnapshotMetadata(
|
||||
source: source,
|
||||
upstreamReference: provenance.upstreamReference,
|
||||
inputDigest: ProxyACLDocument.digest(Data(document.utf8)),
|
||||
fetchedAt: provenance.fetchedAt,
|
||||
license: provenance.license,
|
||||
attribution: provenance.attribution)
|
||||
return RuleSnapshot(
|
||||
metadata: metadata,
|
||||
rules: rules,
|
||||
absorbed: shadowing.shadowed.map(\.exception),
|
||||
lossReport: report)
|
||||
}
|
||||
|
||||
/// 累计单条分类结果:可表达规则进列表,已知不可表达按类计数并报告。
|
||||
private func accumulate(
|
||||
_ entry: AutoProxyEntry,
|
||||
proxyRules: inout [ProxyRule],
|
||||
exceptionRules: inout [ProxyRule],
|
||||
sawCodeLine: inout Bool,
|
||||
report: inout RuleConversionLossReport
|
||||
) throws {
|
||||
switch entry {
|
||||
case .blank:
|
||||
report.incrementSkipped("blank")
|
||||
case .comment:
|
||||
report.incrementSkipped("comment")
|
||||
case .header:
|
||||
report.incrementSkipped("header")
|
||||
case .domainProxy(let host, let original):
|
||||
sawCodeLine = true
|
||||
proxyRules.append(
|
||||
makeRule(action: .proxy, match: try RuleMatch(domainSuffix: host), original: original))
|
||||
case .domainException(let host, let original):
|
||||
sawCodeLine = true
|
||||
exceptionRules.append(
|
||||
makeRule(action: .direct, match: try RuleMatch(domainSuffix: host), original: original))
|
||||
case .urlPrefix, .urlPath, .filterOption, .wildcard, .regexp, .plainText,
|
||||
.singleLabelPrefix, .ipLiteral:
|
||||
sawCodeLine = true
|
||||
let label = unexpressibleLabel(for: entry)
|
||||
recordUnexpressible(
|
||||
label.category, note: label.note, original: label.original, report: &report)
|
||||
}
|
||||
}
|
||||
|
||||
private struct UnexpressibleLabel {
|
||||
let category: String
|
||||
let note: String
|
||||
let original: String
|
||||
}
|
||||
|
||||
private func unexpressibleLabel(for entry: AutoProxyEntry) -> UnexpressibleLabel {
|
||||
switch entry {
|
||||
case .urlPrefix(let original, _):
|
||||
return UnexpressibleLabel(category: "urlPrefix", note: "url-prefix", original: original)
|
||||
case .urlPath(let original, _):
|
||||
return UnexpressibleLabel(category: "urlPath", note: "url-path", original: original)
|
||||
case .filterOption(let original, _):
|
||||
return UnexpressibleLabel(category: "filterOption", note: "filter-option", original: original)
|
||||
case .wildcard(let original, _):
|
||||
return UnexpressibleLabel(category: "wildcard", note: "wildcard", original: original)
|
||||
case .regexp(let original, _):
|
||||
return UnexpressibleLabel(category: "regexp", note: "regexp", original: original)
|
||||
case .plainText(let original, _):
|
||||
return UnexpressibleLabel(category: "plainText", note: "plain-text", original: original)
|
||||
case .singleLabelPrefix(let original, _):
|
||||
return UnexpressibleLabel(
|
||||
category: "singleLabelPrefix", note: "single-label-prefix", original: original)
|
||||
case .ipLiteral(let original, _):
|
||||
return UnexpressibleLabel(category: "ipLiteral", note: "ip-literal", original: original)
|
||||
default:
|
||||
return UnexpressibleLabel(category: "", note: "", original: "")
|
||||
}
|
||||
}
|
||||
|
||||
private func recordUnexpressible(
|
||||
_ category: String, note: String, original: String, report: inout RuleConversionLossReport
|
||||
) {
|
||||
report.incrementSkipped(category)
|
||||
report.notes.append("unexpressible-\(note): \(original)")
|
||||
}
|
||||
|
||||
private func makeRule(action: RuleAction, match: RuleMatch, original: String) -> ProxyRule {
|
||||
ProxyRule(
|
||||
action: action,
|
||||
match: match,
|
||||
source: source,
|
||||
conflict: RuleConflictMetadata(originalEntry: original))
|
||||
}
|
||||
|
||||
private func validateRuleCount(_ ruleCount: Int, previousRuleCount: Int?) throws {
|
||||
guard ruleCount >= minimumRuleCount else {
|
||||
throw ConversionError.abnormalRuleCount(ruleCount)
|
||||
}
|
||||
guard ruleCount <= maximumRuleCount else {
|
||||
throw ConversionError.abnormalRuleCount(ruleCount)
|
||||
}
|
||||
try validateScale(ruleCount: ruleCount, previousRuleCount: previousRuleCount)
|
||||
}
|
||||
|
||||
private func validateScale(ruleCount: Int, previousRuleCount: Int?) throws {
|
||||
guard let previous = previousRuleCount, previous > 0 else { return }
|
||||
let lower = Int(Double(previous) * Self.minimumScaleRatio)
|
||||
let upper = Int(Double(previous) * Self.maximumScaleRatio)
|
||||
guard ruleCount >= lower, ruleCount <= upper else {
|
||||
throw ConversionError.abnormalScaleChange(found: ruleCount, previous: previous)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - 例外遮蔽
|
||||
|
||||
/// 域名优先下(proxy_list 先于 bypass_list),被更宽代理规则覆盖的 `@@` 例外
|
||||
/// 写入 ACL 也不会生效。保留代理规则,例外逐项报告并不进入规则列表。
|
||||
enum ShadowingAnalyzer {
|
||||
struct Result {
|
||||
let keptProxy: [ProxyRule]
|
||||
let keptExceptions: [ProxyRule]
|
||||
let shadowed: [(exception: ProxyRule, shadowedBy: ProxyRule)]
|
||||
}
|
||||
|
||||
static func split(proxyRules: [ProxyRule], exceptionRules: [ProxyRule]) -> Result {
|
||||
var keptExceptions: [ProxyRule] = []
|
||||
var shadowed: [(ProxyRule, ProxyRule)] = []
|
||||
for exception in exceptionRules {
|
||||
if let blocker = proxyRules.first(where: { covers(proxy: $0, exception: exception) }) {
|
||||
let marked = ProxyRule(
|
||||
action: exception.action,
|
||||
match: exception.match,
|
||||
source: exception.source,
|
||||
conflict: RuleConflictMetadata(
|
||||
originalEntry: exception.conflict.originalEntry,
|
||||
absorbedBy: blocker.match,
|
||||
notes: exception.conflict.notes + ["shadowed-by-broader-proxy"]))
|
||||
shadowed.append((marked, blocker))
|
||||
} else {
|
||||
keptExceptions.append(exception)
|
||||
}
|
||||
}
|
||||
return Result(
|
||||
keptProxy: proxyRules, keptExceptions: keptExceptions,
|
||||
shadowed: shadowed.map { (exception: $0.0, shadowedBy: $0.1) })
|
||||
}
|
||||
|
||||
/// 代理规则是否在域名匹配上覆盖例外(相等或更宽的后缀)。
|
||||
static func covers(proxy: ProxyRule, exception: ProxyRule) -> Bool {
|
||||
switch (proxy.match, exception.match) {
|
||||
case (.domainSuffix(let broad), .domainSuffix(let narrow)):
|
||||
return domainSuffixCovers(broad: broad, narrow: narrow)
|
||||
case (.domainSuffix(let broad), .domainExact(let narrow)):
|
||||
return domainSuffixCovers(broad: broad, narrow: narrow)
|
||||
case (.domainExact(let broad), .domainExact(let narrow)):
|
||||
return broad.lowercased() == narrow.lowercased()
|
||||
case (.domainExact(let broad), .domainSuffix(let narrow)):
|
||||
// 精确匹配只覆盖同名主机;后缀例外范围更宽,不被其遮蔽。
|
||||
return broad.lowercased() == narrow.lowercased()
|
||||
default:
|
||||
// IP 规则的优先级方向不同(bypass 先于 proxy),此处不按域名遮蔽处理。
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
static func domainSuffixCovers(broad: String, narrow: String) -> Bool {
|
||||
let broadHost = broad.lowercased()
|
||||
let narrowHost = narrow.lowercased()
|
||||
return narrowHost == broadHost || narrowHost.hasSuffix("." + broadHost)
|
||||
}
|
||||
}
|
||||
@@ -75,29 +75,35 @@ struct ProxyACLDocument: Codable, Equatable, Sendable {
|
||||
at: fileURL, header: "[proxy_all]", summary: "global")
|
||||
}
|
||||
|
||||
/// 规则模式(issue #63/#64):`.cn`、geolocation-cn 域名与中国 IPv4 CIDR
|
||||
/// 候选直连,其余目标默认代理(「未匹配时代理」)。固定本地绕过优先;
|
||||
/// CIDR 判定可能触发本地 DNS 查询,产品不承诺 DNS 均经远端。
|
||||
/// 规则模式(issue #63/#64/#65):默认动作决定 ACL 骨架,调用方按默认动作
|
||||
/// 传入对应来源的候选(两种默认动作不把全部内置来源无条件并集)。
|
||||
///
|
||||
/// - 「未匹配时代理」:`proxy_all` + 中国域名/CIDR 直连候选进 `bypass_list`。
|
||||
/// - 「未匹配时直连」:`bypass_all` + GFWList 可生效代理规则进 `proxy_list`;
|
||||
/// 未匹配目标与无可用规则命中的 IP 字面目标直连。
|
||||
///
|
||||
/// 固定本地绕过优先;CIDR 判定可能触发本地 DNS 查询。
|
||||
static func rule(
|
||||
at fileURL: URL,
|
||||
defaultAction: RuleDefaultAction,
|
||||
chinaRules: [ProxyRule]
|
||||
rules: [ProxyRule]
|
||||
) -> ProxyACLDocument {
|
||||
switch defaultAction {
|
||||
case .proxyWhenUnmatched:
|
||||
// proxy_all + 中国域名进 bypass_list(直连候选)。
|
||||
// proxy_all + 直连候选进 bypass_list(中国域名 / IPv4 CIDR)。
|
||||
let bypass =
|
||||
FixedLocalProxyRanges.aclBypassRules
|
||||
+ chinaRules.filter { $0.action == .direct }.map { $0.aclLine }
|
||||
+ rules.filter { $0.action == .direct }.map { $0.aclLine }
|
||||
return makeDocument(
|
||||
at: fileURL, header: "[proxy_all]", summary: "rule-proxy-default",
|
||||
bypassRules: bypass, proxyRules: [])
|
||||
case .directWhenUnmatched:
|
||||
// bypass_all + 可生效的代理规则进 proxy_list(GFWList 由后续票据提供)。
|
||||
// bypass_all + GFWList 可生效规则:代理候选进 proxy_list,未遮蔽例外
|
||||
// 进 bypass_list。两种默认动作不无条件并集中国来源。
|
||||
let bypass =
|
||||
FixedLocalProxyRanges.aclBypassRules
|
||||
+ chinaRules.filter { $0.action == .direct }.map { $0.aclLine }
|
||||
let proxy = chinaRules.filter { $0.action == .proxy }.map { $0.aclLine }
|
||||
+ rules.filter { $0.action == .direct }.map { $0.aclLine }
|
||||
let proxy = rules.filter { $0.action == .proxy }.map { $0.aclLine }
|
||||
return makeDocument(
|
||||
at: fileURL, header: "[bypass_all]", summary: "rule-direct-default",
|
||||
bypassRules: bypass, proxyRules: proxy)
|
||||
|
||||
@@ -46,7 +46,7 @@ struct RuleSnapshotMetadata: Codable, Equatable, Sendable {
|
||||
struct RuleConversionLossReport: Codable, Equatable, Sendable {
|
||||
/// 成功进入规则模型的条目数。
|
||||
var convertedCount: Int
|
||||
/// 被 `.cn` 后缀吸收的条目数。
|
||||
/// 被吸收或遮蔽而不进入运行时规则的条目数(`.cn` 吸收、GFWList `@@` 遮蔽)。
|
||||
var absorbedCount: Int
|
||||
/// 按类型跳过的计数(keyword / regexp / attribute / include 等)。
|
||||
var skipped: [String: Int]
|
||||
|
||||
@@ -17,7 +17,8 @@ ShadowsocksX-NG 的现代化重写版本。新工程的全部源码与构建配
|
||||
- `Vendor/<name>/manifest.json` — 外部二进制的固定供应链清单(tag + 资产 URL + 归档 SHA-256 + bundle 内位置 + 签名 identifier);二进制本体与 `.fetched.sha256` 戳是构建缓存,不入库。
|
||||
- `Vendor/rules/geolocation-cn/` — 内置中国域名规则快照(issue #63):`snapshot.json`(规范化规则 + 元数据 + 损失报告)、`manifest.json`(上游版本与快照 SHA-256)、`NOTICE`(许可证与归属)。普通构建只读本地快照,绝不抓取或转换。
|
||||
- `Vendor/rules/china-ipv4/` — 内置中国 IPv4 CIDR 直连候选快照(issue #64):同上三件套,来源为 [gaoyifan/china-operator-ip](https://github.com/gaoyifan/china-operator-ip) `china.txt` 固定 commit。规则模式的 `proxy_all` ACL 同时编入中国域名与 IPv4 CIDR 直连候选。
|
||||
- `Scripts/` — 供应链脚本(见下节)与打包门槛断言;另有 `update-geolocation-cn.sh` / `update-china-ipv4.sh`(显式维护动作,抓取固定版上游并转换)与 `verify-rule-snapshots.sh`(构建前离线校验快照完整性)。
|
||||
- `Vendor/rules/gfwlist/` — 内置 GFWList 代理候选快照(issue #65):同上三件套,来源为 [gfwlist/gfwlist](https://github.com/gfwlist/gfwlist) 官方 Base64 AutoProxy `gfwlist.txt` 固定 commit。只转换可按目标域名无损表达的规则;「未匹配时直连」的 `bypass_all` ACL 编入这些代理候选。
|
||||
- `Scripts/` — 供应链脚本(见下节)与打包门槛断言;另有 `update-geolocation-cn.sh` / `update-china-ipv4.sh` / `update-gfwlist.sh`(显式维护动作,抓取固定版上游并转换)与 `verify-rule-snapshots.sh`(构建前离线校验快照完整性)。
|
||||
|
||||
## 外部二进制供应链
|
||||
|
||||
@@ -79,10 +80,11 @@ Scripts/packaging-gate.sh \
|
||||
|
||||
- **geolocation-cn**(issue #63):人工复验上游后运行 `Scripts/update-geolocation-cn.sh`,从固定版 [Loyalsoldier/domain-list-custom](https://github.com/Loyalsoldier/domain-list-custom) `geosite.dat` 解析 typed 条目并生成 `Vendor/rules/geolocation-cn/snapshot.json`。
|
||||
- **china-ipv4**(issue #64):运行 `Scripts/update-china-ipv4.sh`,从固定版 [gaoyifan/china-operator-ip](https://github.com/gaoyifan/china-operator-ip) `china.txt` 规范化并去重 IPv4 CIDR,生成 `Vendor/rules/china-ipv4/snapshot.json`。
|
||||
- **gfwlist**(issue #65):运行 `Scripts/update-gfwlist.sh`,从固定版 [gfwlist/gfwlist](https://github.com/gfwlist/gfwlist) `gfwlist.txt` 解码官方 Base64 AutoProxy 列表并生成 `Vendor/rules/gfwlist/snapshot.json`。只转换可按目标域名无损表达的 `||host` / `||host^` 规则;URL 路径、协议条件、通配符、正则、单标签前缀与 IP 字面量域名规则逐类计入损失报告,绝不扩大为整域名。被更宽代理规则遮蔽的 `@@` 例外不写入无效 ACL 项(sslocal 域名匹配 proxy_list 优先于 bypass_list),保留代理规则并逐项报告。
|
||||
|
||||
更新失败保留上一份有效快照。geolocation-cn 转换会因未知语法/损坏输入失败;china-ipv4 转换还会因异常格式(拒绝率过高)、全部失效或相对上一份快照的异常规模变化失败。普通构建由 `Scripts/verify-rule-snapshots.sh` 离线校验快照存在、摘要匹配且 schema/转换器版本一致,缺失或损坏即构建失败。分发物必须携带各自的 `NOTICE`(许可证与归属)。
|
||||
更新失败保留上一份有效快照。geolocation-cn 转换会因未知语法/损坏输入失败;china-ipv4 转换还会因异常格式(拒绝率过高)、全部失效或相对上一份快照的异常规模变化失败;gfwlist 转换因未知语法、损坏 Base64、异常规模或相对上一份快照的异常规模变化失败。普通构建由 `Scripts/verify-rule-snapshots.sh` 离线校验快照存在、摘要匹配且 schema/转换器版本一致,缺失或损坏即构建失败。分发物必须携带各自的 `NOTICE`(许可证与归属)。
|
||||
|
||||
规则模式「未匹配时代理」生成的 `proxy_all` ACL 同时包含中国域名与中国 IPv4 CIDR 直连候选,固定本地绕过优先。**CIDR 判定可能触发本地 DNS 查询**:sslocal 为未命中域名做 IP 匹配时可能发起本地 DNS 查询,产品不承诺 DNS 查询均经远端 Shadowsocks 服务器。
|
||||
规则模式「未匹配时代理」生成的 `proxy_all` ACL 同时包含中国域名与中国 IPv4 CIDR 直连候选,固定本地绕过优先。规则模式「未匹配时直连」生成的 `bypass_all` ACL 只编入 GFWList 可生效代理候选;两种默认动作不把全部内置来源无条件并集。未匹配目标与无可用规则命中的 IP 字面目标直连。**CIDR 判定可能触发本地 DNS 查询**:sslocal 为未命中域名做 IP 匹配时可能发起本地 DNS 查询,产品不承诺 DNS 查询均经远端 Shadowsocks 服务器。
|
||||
|
||||
## 依赖升级
|
||||
|
||||
|
||||
Executable
+357
@@ -0,0 +1,357 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Convert the official Base64 GFWList AutoProxy list into an NG2 rule snapshot
|
||||
(issue #65).
|
||||
|
||||
Only rules that can be expressed losslessly as a target domain are converted.
|
||||
URL-path and protocol conditions are never expanded into whole-domain rules.
|
||||
`@@` exceptions shadowed by a broader proxy rule are kept out of the ACL and
|
||||
reported item by item; the broader proxy rules stay. Unknown syntax, corrupt
|
||||
input, or abnormal shrinkage block the update and leave the previous snapshot.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import base64
|
||||
import binascii
|
||||
import datetime as dt
|
||||
import hashlib
|
||||
import ipaddress
|
||||
import json
|
||||
import re
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
CONVERTER_VERSION = "1.0.0"
|
||||
SCHEMA_VERSION = 1
|
||||
|
||||
MINIMUM_RULE_COUNT = 100
|
||||
MAXIMUM_RULE_COUNT = 50_000
|
||||
MINIMUM_SCALE_RATIO = 0.5
|
||||
MAXIMUM_SCALE_RATIO = 2.0
|
||||
|
||||
DOMAIN_HOST_RE = re.compile(r"^[A-Za-z0-9]([A-Za-z0-9.-]*[A-Za-z0-9])?$")
|
||||
|
||||
|
||||
class UnknownSyntax(Exception):
|
||||
def __init__(self, line: str) -> None:
|
||||
super().__init__(f"unknown AutoProxy syntax: {line}")
|
||||
self.line = line
|
||||
|
||||
|
||||
def looks_like_ip(text: str) -> bool:
|
||||
try:
|
||||
ipaddress.ip_address(text)
|
||||
return True
|
||||
except ValueError:
|
||||
pass
|
||||
parts = text.split(".")
|
||||
return len(parts) == 4 and all(p.isdigit() for p in parts)
|
||||
|
||||
|
||||
def is_valid_domain_host(host: str) -> bool:
|
||||
return bool(host) and "." in host and DOMAIN_HOST_RE.fullmatch(host) is not None
|
||||
|
||||
|
||||
def parse_line(raw_line: str) -> dict:
|
||||
"""Classify one AutoProxy line. Raises UnknownSyntax on unrecognized forms."""
|
||||
line = raw_line.strip()
|
||||
if not line:
|
||||
return {"kind": "blank"}
|
||||
if line.startswith("!"):
|
||||
return {"kind": "comment"}
|
||||
if line.startswith("[") and line.endswith("]"):
|
||||
return {"kind": "header"}
|
||||
|
||||
is_exception = line.startswith("@@")
|
||||
pattern = line[2:] if is_exception else line
|
||||
|
||||
if pattern.startswith("||"):
|
||||
return parse_domain_pattern(pattern[2:], line, is_exception)
|
||||
if pattern.startswith("|"):
|
||||
return {"kind": "urlPrefix", "original": line, "isException": is_exception}
|
||||
if len(pattern) >= 3 and pattern.startswith("/") and pattern.endswith("/"):
|
||||
return {"kind": "regexp", "original": line, "isException": is_exception}
|
||||
if "*" in pattern:
|
||||
return {"kind": "wildcard", "original": line, "isException": is_exception}
|
||||
if looks_like_ip(pattern):
|
||||
return {"kind": "ipLiteral", "original": line, "isException": is_exception}
|
||||
if "." in pattern and DOMAIN_HOST_RE.fullmatch(pattern):
|
||||
return {"kind": "plainText", "original": line, "isException": is_exception}
|
||||
raise UnknownSyntax(line)
|
||||
|
||||
|
||||
def parse_domain_pattern(host_part: str, original: str, is_exception: bool) -> dict:
|
||||
if "/" in host_part:
|
||||
return {"kind": "urlPath", "original": original, "isException": is_exception}
|
||||
if "*" in host_part:
|
||||
return {"kind": "wildcard", "original": original, "isException": is_exception}
|
||||
if "$" in host_part:
|
||||
return {"kind": "filterOption", "original": original, "isException": is_exception}
|
||||
|
||||
host = host_part
|
||||
if host.endswith("^"):
|
||||
host = host[:-1]
|
||||
if "^" in host:
|
||||
raise UnknownSyntax(original)
|
||||
if looks_like_ip(host):
|
||||
return {"kind": "ipLiteral", "original": original, "isException": is_exception}
|
||||
if "." not in host:
|
||||
return {
|
||||
"kind": "singleLabelPrefix",
|
||||
"original": original,
|
||||
"isException": is_exception,
|
||||
}
|
||||
if not is_valid_domain_host(host):
|
||||
raise UnknownSyntax(original)
|
||||
kind = "domainException" if is_exception else "domainProxy"
|
||||
return {"kind": kind, "host": host.lower(), "original": original}
|
||||
|
||||
|
||||
def domain_suffix_covers(broad: str, narrow: str) -> bool:
|
||||
broad_host = broad.lower()
|
||||
narrow_host = narrow.lower()
|
||||
return narrow_host == broad_host or narrow_host.endswith("." + broad_host)
|
||||
|
||||
|
||||
def split_shadowed(
|
||||
proxy_rules: list[dict], exception_rules: list[dict]
|
||||
) -> tuple[list[dict], list[dict], list[tuple[dict, dict]]]:
|
||||
"""Return (kept_proxy, kept_exceptions, shadowed_pairs).
|
||||
|
||||
A proxy rule covers an exception when its domain suffix is equal or broader.
|
||||
sslocal matches domain proxy_list before bypass_list, so a covered exception
|
||||
would be an ineffective ACL entry.
|
||||
"""
|
||||
kept_exceptions: list[dict] = []
|
||||
shadowed: list[tuple[dict, dict]] = []
|
||||
for exception in exception_rules:
|
||||
blocker = None
|
||||
for proxy in proxy_rules:
|
||||
if domain_suffix_covers(
|
||||
proxy["match"]["value"], exception["match"]["value"]
|
||||
):
|
||||
blocker = proxy
|
||||
break
|
||||
if blocker is None:
|
||||
kept_exceptions.append(exception)
|
||||
else:
|
||||
marked = dict(exception)
|
||||
marked["conflict"] = {
|
||||
"originalEntry": exception["conflict"]["originalEntry"],
|
||||
"absorbedBy": blocker["match"],
|
||||
"notes": list(exception["conflict"]["notes"]) + ["shadowed-by-broader-proxy"],
|
||||
}
|
||||
shadowed.append((marked, blocker))
|
||||
return proxy_rules, kept_exceptions, shadowed
|
||||
|
||||
|
||||
def make_rule(action: str, match: dict, original: str, source: dict) -> dict:
|
||||
return {
|
||||
"action": action,
|
||||
"match": match,
|
||||
"source": source,
|
||||
"conflict": {"originalEntry": original, "absorbedBy": None, "notes": []},
|
||||
}
|
||||
|
||||
|
||||
def convert_document(document: str, source: dict) -> tuple[list[dict], list[dict], dict]:
|
||||
"""Return (rules, absorbed, lossReport)."""
|
||||
skipped: dict[str, int] = {}
|
||||
notes: list[str] = []
|
||||
proxy_rules: list[dict] = []
|
||||
exception_rules: list[dict] = []
|
||||
saw_code_line = False
|
||||
|
||||
def bump(category: str) -> None:
|
||||
skipped[category] = skipped.get(category, 0) + 1
|
||||
|
||||
for raw_line in document.splitlines():
|
||||
entry = parse_line(raw_line)
|
||||
kind = entry["kind"]
|
||||
if kind == "blank":
|
||||
bump("blank")
|
||||
elif kind == "comment":
|
||||
bump("comment")
|
||||
elif kind == "header":
|
||||
bump("header")
|
||||
elif kind == "domainProxy":
|
||||
saw_code_line = True
|
||||
proxy_rules.append(
|
||||
make_rule(
|
||||
"proxy",
|
||||
{"kind": "domainSuffix", "value": entry["host"]},
|
||||
entry["original"],
|
||||
source,
|
||||
)
|
||||
)
|
||||
elif kind == "domainException":
|
||||
saw_code_line = True
|
||||
exception_rules.append(
|
||||
make_rule(
|
||||
"direct",
|
||||
{"kind": "domainSuffix", "value": entry["host"]},
|
||||
entry["original"],
|
||||
source,
|
||||
)
|
||||
)
|
||||
elif kind in {
|
||||
"urlPrefix",
|
||||
"urlPath",
|
||||
"filterOption",
|
||||
"wildcard",
|
||||
"regexp",
|
||||
"plainText",
|
||||
"singleLabelPrefix",
|
||||
"ipLiteral",
|
||||
}:
|
||||
saw_code_line = True
|
||||
bump(kind)
|
||||
notes.append(f"unexpressible-{kind}: {entry['original']}")
|
||||
else:
|
||||
raise UnknownSyntax(entry.get("original", raw_line))
|
||||
|
||||
if not saw_code_line:
|
||||
raise SystemExit("empty input")
|
||||
|
||||
kept_proxy, kept_exceptions, shadowed = split_shadowed(proxy_rules, exception_rules)
|
||||
if shadowed:
|
||||
skipped["shadowedException"] = len(shadowed)
|
||||
for exception, blocker in shadowed:
|
||||
notes.append(
|
||||
"shadowed-exception: "
|
||||
f"{exception['conflict']['originalEntry']} "
|
||||
f"shadowed-by {blocker['conflict']['originalEntry']}"
|
||||
)
|
||||
|
||||
rules = kept_proxy + kept_exceptions
|
||||
# Deduplicate by (action, match.kind, match.value), keeping first.
|
||||
seen: set[tuple[str, str, str]] = set()
|
||||
deduped: list[dict] = []
|
||||
for rule in rules:
|
||||
key = (rule["action"], rule["match"]["kind"], rule["match"]["value"])
|
||||
if key in seen:
|
||||
continue
|
||||
seen.add(key)
|
||||
deduped.append(rule)
|
||||
rules = deduped
|
||||
|
||||
report = {
|
||||
"convertedCount": len(rules),
|
||||
"absorbedCount": len(shadowed),
|
||||
"skipped": skipped,
|
||||
"rejected": {},
|
||||
"notes": notes,
|
||||
}
|
||||
return rules, [item for item, _ in shadowed], report
|
||||
|
||||
|
||||
def build_snapshot(
|
||||
document: str,
|
||||
source: dict,
|
||||
upstream_reference: str,
|
||||
license_name: str,
|
||||
attribution: str,
|
||||
previous_rule_count: int | None,
|
||||
) -> dict:
|
||||
rules, absorbed, report = convert_document(document, source)
|
||||
rule_count = len(rules)
|
||||
if rule_count < MINIMUM_RULE_COUNT:
|
||||
raise SystemExit(f"abnormal rule count {rule_count} (min {MINIMUM_RULE_COUNT})")
|
||||
if rule_count > MAXIMUM_RULE_COUNT:
|
||||
raise SystemExit(f"abnormal rule count {rule_count} (max {MAXIMUM_RULE_COUNT})")
|
||||
if previous_rule_count is not None and previous_rule_count > 0:
|
||||
lower = int(previous_rule_count * MINIMUM_SCALE_RATIO)
|
||||
upper = int(previous_rule_count * MAXIMUM_SCALE_RATIO)
|
||||
if not (lower <= rule_count <= upper):
|
||||
raise SystemExit(
|
||||
f"abnormal scale change: {rule_count} vs previous {previous_rule_count} "
|
||||
f"(allowed {lower}..{upper})"
|
||||
)
|
||||
|
||||
return {
|
||||
"schemaVersion": SCHEMA_VERSION,
|
||||
"metadata": {
|
||||
"source": source,
|
||||
"upstreamReference": upstream_reference,
|
||||
"inputDigest": hashlib.sha256(document.encode("utf-8")).hexdigest(),
|
||||
"fetchedAt": dt.datetime.now(dt.timezone.utc)
|
||||
.replace(microsecond=0)
|
||||
.isoformat()
|
||||
.replace("+00:00", "Z"),
|
||||
"converterVersion": CONVERTER_VERSION,
|
||||
"license": license_name,
|
||||
"attribution": attribution,
|
||||
},
|
||||
"rules": rules,
|
||||
"absorbed": absorbed,
|
||||
"lossReport": report,
|
||||
}
|
||||
|
||||
|
||||
def decode_autoproxy(text: str) -> str:
|
||||
compact = "".join(text.split())
|
||||
try:
|
||||
raw = base64.b64decode(compact, validate=False)
|
||||
except (binascii.Error, ValueError) as exc:
|
||||
raise SystemExit(f"corrupt Base64 input: {exc}") from exc
|
||||
try:
|
||||
return raw.decode("utf-8")
|
||||
except UnicodeDecodeError as exc:
|
||||
raise SystemExit(f"corrupt AutoProxy payload: {exc}") from exc
|
||||
|
||||
|
||||
def main() -> int:
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("input", type=Path, help="path to official Base64 gfwlist.txt")
|
||||
parser.add_argument("--upstream", required=True, help="pinned upstream commit")
|
||||
parser.add_argument(
|
||||
"--upstream-reference",
|
||||
required=True,
|
||||
help="upstream repo/product identifier including the pinned commit or URL",
|
||||
)
|
||||
parser.add_argument("--license", required=True)
|
||||
parser.add_argument("--attribution", required=True)
|
||||
parser.add_argument("--out", type=Path, required=True, help="output snapshot.json")
|
||||
parser.add_argument(
|
||||
"--previous-count",
|
||||
type=int,
|
||||
default=None,
|
||||
help="previous snapshot rule count for scale-change detection",
|
||||
)
|
||||
args = parser.parse_args()
|
||||
|
||||
base64_text = args.input.read_text(encoding="utf-8")
|
||||
document = decode_autoproxy(base64_text)
|
||||
source = {
|
||||
"kind": "gfwlist",
|
||||
"upstreamVersion": args.upstream,
|
||||
"label": "GFWList",
|
||||
}
|
||||
try:
|
||||
snapshot = build_snapshot(
|
||||
document,
|
||||
source,
|
||||
args.upstream_reference,
|
||||
args.license,
|
||||
args.attribution,
|
||||
args.previous_count,
|
||||
)
|
||||
except UnknownSyntax as exc:
|
||||
raise SystemExit(str(exc)) from exc
|
||||
|
||||
args.out.parent.mkdir(parents=True, exist_ok=True)
|
||||
tmp = args.out.with_suffix(".json.tmp")
|
||||
tmp.write_text(json.dumps(snapshot, indent=2, sort_keys=True) + "\n", encoding="utf-8")
|
||||
tmp.replace(args.out)
|
||||
loss = snapshot["lossReport"]
|
||||
print(
|
||||
f"wrote {args.out}: {loss['convertedCount']} rules, "
|
||||
f"skipped={loss['skipped']}, "
|
||||
f"shadowed={loss['skipped'].get('shadowedException', 0)}"
|
||||
)
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
Executable
+168
@@ -0,0 +1,168 @@
|
||||
#!/usr/bin/env bash
|
||||
# Explicit maintainer update for the built-in GFWList AutoProxy rule snapshot
|
||||
# (issue #65). Normal builds never run this script — they only read the local
|
||||
# pinned snapshot. On any failure the previous valid snapshot is kept.
|
||||
set -euo pipefail
|
||||
|
||||
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
|
||||
RULES_DIR="${ROOT}/Vendor/rules/gfwlist"
|
||||
SNAPSHOT="${RULES_DIR}/snapshot.json"
|
||||
MANIFEST="${RULES_DIR}/manifest.json"
|
||||
NOTICE="${RULES_DIR}/NOTICE"
|
||||
|
||||
# Pinned upstream: gfwlist/gfwlist master commit. Bump these together after
|
||||
# human review of the new commit.
|
||||
UPSTREAM_VERSION="c19292511da4c5af8f6193fd32d666eaaa0312f9"
|
||||
UPSTREAM_URL="https://raw.githubusercontent.com/gfwlist/gfwlist/${UPSTREAM_VERSION}/gfwlist.txt"
|
||||
UPSTREAM_MIRROR_URL="https://cdn.jsdelivr.net/gh/gfwlist/gfwlist@${UPSTREAM_VERSION}/gfwlist.txt"
|
||||
UPSTREAM_SHA256="750d34564363075fafa12d8ab9739f269e2fbd2bf3c5f4ab8cfd859bfd3441df"
|
||||
LICENSE="LGPL-2.1"
|
||||
ATTRIBUTION="GFWList AutoProxy rules derived from gfwlist/gfwlist gfwlist.txt at commit ${UPSTREAM_VERSION}. Official Base64 AutoProxy 0.2.9 list. Only rules losslessly expressible as target domains are converted; URL-path, protocol, wildcard, and regex conditions are reported as losses and never expanded into whole-domain rules. List is candidate routing data, not a guarantee of reachability or completeness."
|
||||
|
||||
usage() {
|
||||
cat <<'EOF'
|
||||
Usage: Scripts/update-gfwlist.sh [--fetch-only]
|
||||
|
||||
Explicitly refresh Vendor/rules/gfwlist/snapshot.json from the pinned
|
||||
gfwlist/gfwlist commit. This is a maintainer action; ordinary builds are
|
||||
offline and only read the local snapshot.
|
||||
|
||||
--fetch-only Download and verify gfwlist.txt without converting.
|
||||
EOF
|
||||
}
|
||||
|
||||
FETCH_ONLY=0
|
||||
if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then
|
||||
usage
|
||||
exit 0
|
||||
fi
|
||||
if [[ "${1:-}" == "--fetch-only" ]]; then
|
||||
FETCH_ONLY=1
|
||||
elif [[ $# -gt 0 ]]; then
|
||||
usage >&2
|
||||
exit 2
|
||||
fi
|
||||
|
||||
TMP="$(mktemp -d)"
|
||||
cleanup() { rm -rf "${TMP}"; }
|
||||
trap cleanup EXIT
|
||||
|
||||
echo "Fetching gfwlist.txt ${UPSTREAM_VERSION} …"
|
||||
fetch_ok=0
|
||||
if command -v curl >/dev/null 2>&1; then
|
||||
if curl -fsSL --retry 1 --max-time 20 -o "${TMP}/gfwlist.txt" "${UPSTREAM_URL}"; then
|
||||
fetch_ok=1
|
||||
echo "Fetched from ${UPSTREAM_URL}"
|
||||
elif curl -fsSL --retry 2 --max-time 45 -o "${TMP}/gfwlist.txt" "${UPSTREAM_MIRROR_URL}"; then
|
||||
fetch_ok=1
|
||||
echo "Fetched from mirror ${UPSTREAM_MIRROR_URL}"
|
||||
fi
|
||||
fi
|
||||
if [[ "${fetch_ok}" -ne 1 ]]; then
|
||||
echo "error: failed to fetch gfwlist.txt from pinned upstream or mirror" >&2
|
||||
echo "previous snapshot kept"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
ACTUAL="$(shasum -a 256 "${TMP}/gfwlist.txt" | awk '{print $1}')"
|
||||
if [[ "${ACTUAL}" != "${UPSTREAM_SHA256}" ]]; then
|
||||
echo "error: gfwlist.txt sha256 mismatch" >&2
|
||||
echo " expected ${UPSTREAM_SHA256}" >&2
|
||||
echo " actual ${ACTUAL}" >&2
|
||||
echo "previous snapshot kept"
|
||||
exit 1
|
||||
fi
|
||||
echo "Verified gfwlist.txt sha256"
|
||||
|
||||
if [[ "${FETCH_ONLY}" -eq 1 ]]; then
|
||||
echo "fetch-only: leaving snapshot untouched"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
mkdir -p "${RULES_DIR}"
|
||||
|
||||
# Previous rule count feeds scale-change detection; missing snapshot is first run.
|
||||
PREVIOUS_COUNT=""
|
||||
if [[ -f "${SNAPSHOT}" ]]; then
|
||||
PREVIOUS_COUNT="$(python3 -c 'import json,sys; print(len(json.load(open(sys.argv[1]))["rules"]))' "${SNAPSHOT}")"
|
||||
fi
|
||||
|
||||
CONVERT_ARGS=(
|
||||
"${TMP}/gfwlist.txt"
|
||||
--upstream "${UPSTREAM_VERSION}"
|
||||
--upstream-reference "gfwlist/gfwlist gfwlist.txt @ ${UPSTREAM_VERSION}"
|
||||
--license "${LICENSE}"
|
||||
--attribution "${ATTRIBUTION}"
|
||||
--out "${TMP}/snapshot.json"
|
||||
)
|
||||
if [[ -n "${PREVIOUS_COUNT}" ]]; then
|
||||
CONVERT_ARGS+=(--previous-count "${PREVIOUS_COUNT}")
|
||||
fi
|
||||
|
||||
# Convert into a temporary snapshot first; only replace on success.
|
||||
if ! python3 "${ROOT}/Scripts/convert-gfwlist.py" "${CONVERT_ARGS[@]}"; then
|
||||
echo "error: conversion failed; previous snapshot kept"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Sanity: snapshot must load as JSON and carry required metadata keys.
|
||||
python3 - "${TMP}/snapshot.json" <<'PY'
|
||||
import json, sys
|
||||
from pathlib import Path
|
||||
data = json.loads(Path(sys.argv[1]).read_text(encoding="utf-8"))
|
||||
meta = data.get("metadata") or {}
|
||||
for key in ("source", "upstreamReference", "inputDigest", "fetchedAt", "converterVersion", "license", "attribution"):
|
||||
if key not in meta:
|
||||
raise SystemExit(f"snapshot metadata missing {key}")
|
||||
if not data.get("rules"):
|
||||
raise SystemExit("snapshot has no rules")
|
||||
print(f"snapshot ok: {len(data['rules'])} rules, "
|
||||
f"shadowed={data.get('lossReport', {}).get('skipped', {}).get('shadowedException', 0)}")
|
||||
PY
|
||||
|
||||
cp "${TMP}/snapshot.json" "${SNAPSHOT}"
|
||||
|
||||
# Refresh manifest (build-time integrity anchor).
|
||||
python3 - "${SNAPSHOT}" "${MANIFEST}" "${UPSTREAM_VERSION}" "${UPSTREAM_URL}" "${UPSTREAM_SHA256}" <<'PY'
|
||||
import hashlib, json, sys
|
||||
from pathlib import Path
|
||||
snapshot_path, manifest_path, version, url, sha256 = sys.argv[1:6]
|
||||
data = Path(snapshot_path).read_bytes()
|
||||
digest = hashlib.sha256(data).hexdigest()
|
||||
manifest = {
|
||||
"name": "gfwlist",
|
||||
"upstreamVersion": version,
|
||||
"upstreamURL": url,
|
||||
"upstreamArchiveSHA256": sha256,
|
||||
"snapshotSHA256": digest,
|
||||
"converterVersion": json.loads(data)["metadata"]["converterVersion"],
|
||||
}
|
||||
Path(manifest_path).write_text(json.dumps(manifest, indent=2, sort_keys=True) + "\n", encoding="utf-8")
|
||||
print(f"wrote {manifest_path}")
|
||||
PY
|
||||
|
||||
# NOTICE: license and attribution accompany the distributed snapshot.
|
||||
cat > "${NOTICE}" <<EOF
|
||||
gfwlist rule snapshot
|
||||
=====================
|
||||
|
||||
Upstream: gfwlist/gfwlist gfwlist.txt
|
||||
Upstream commit: ${UPSTREAM_VERSION}
|
||||
Upstream URL: ${UPSTREAM_URL}
|
||||
Upstream archive SHA-256: ${UPSTREAM_SHA256}
|
||||
|
||||
License: ${LICENSE}
|
||||
|
||||
${ATTRIBUTION}
|
||||
|
||||
This list is candidate routing data. It is not a guarantee of network
|
||||
reachability, geographic ownership, or privacy. Rules that cannot be expressed
|
||||
losslessly as target domains (URL paths, protocol conditions, wildcards, regex)
|
||||
are counted and reported as conversion losses and are never expanded into
|
||||
whole-domain proxy rules. \`@@\` exceptions shadowed by a broader proxy rule are
|
||||
reported item by item and omitted from the generated ACL because sslocal would
|
||||
ignore them.
|
||||
EOF
|
||||
|
||||
echo "Updated ${SNAPSHOT}"
|
||||
echo "Remember to review the diff and commit the snapshot together with its NOTICE and manifest."
|
||||
@@ -88,5 +88,7 @@ PY
|
||||
verify_one "geolocation-cn" 100 200000
|
||||
# china-ipv4: China mainland IPv4 CIDR (issue #64).
|
||||
verify_one "china-ipv4" 100 50000
|
||||
# gfwlist: GFWList AutoProxy proxy candidates (issue #65).
|
||||
verify_one "gfwlist" 100 50000
|
||||
|
||||
echo "verify-rule-snapshots: ok"
|
||||
|
||||
@@ -77,6 +77,39 @@ final class BuiltinRuleCatalogTests: XCTestCase {
|
||||
}, "china-ipv4 快照应只含 IPv4 CIDR 直连候选")
|
||||
}
|
||||
|
||||
/// gfwlist 快照(issue #65):从 bundle 或源码树加载,代理候选为域名后缀。
|
||||
func testGFWListSnapshotLoadsFromBundleOrSourceTree() throws {
|
||||
let snapshot: RuleSnapshot
|
||||
do {
|
||||
snapshot = try BuiltinRuleCatalog.loadGFWList()
|
||||
} catch {
|
||||
snapshot = try loadSourceTreeSnapshot(path: "Vendor/rules/gfwlist/snapshot.json")
|
||||
}
|
||||
|
||||
XCTAssertEqual(snapshot.schemaVersion, RuleSnapshot.currentSchemaVersion)
|
||||
XCTAssertEqual(snapshot.metadata.source.kind, .gfwlist)
|
||||
XCTAssertEqual(
|
||||
snapshot.metadata.converterVersion, RuleSnapshotMetadata.currentConverterVersion)
|
||||
XCTAssertFalse(snapshot.metadata.license.isEmpty)
|
||||
XCTAssertFalse(snapshot.metadata.attribution.isEmpty)
|
||||
XCTAssertFalse(snapshot.metadata.inputDigest.isEmpty)
|
||||
XCTAssertFalse(snapshot.metadata.upstreamReference.isEmpty)
|
||||
|
||||
let rules = BuiltinRuleCatalog.gfwlistRules(from: snapshot)
|
||||
XCTAssertFalse(rules.isEmpty)
|
||||
XCTAssertTrue(
|
||||
rules.allSatisfy {
|
||||
if case .domainSuffix = $0.match { return true }
|
||||
return false
|
||||
}, "gfwlist 快照候选应为域名后缀")
|
||||
|
||||
// 损失报告保留来源、摘要、许可证/归属与转换损失(含遮蔽例外)。
|
||||
XCTAssertGreaterThanOrEqual(snapshot.lossReport.skipped["shadowedException"] ?? 0, 1)
|
||||
XCTAssertFalse(snapshot.lossReport.notes.isEmpty)
|
||||
XCTAssertEqual(snapshot.absorbed.count, snapshot.lossReport.skipped["shadowedException"] ?? 0)
|
||||
XCTAssertEqual(snapshot.lossReport.absorbedCount, snapshot.absorbed.count)
|
||||
}
|
||||
|
||||
/// 合并投影(issue #64):域名 + CIDR 直连候选一起进入 ACL 编译输入。
|
||||
func testChinaDirectRulesMergeDomainAndCIDRSnapshots() throws {
|
||||
let geolocation = try loadSourceTreeSnapshot(path: "Vendor/rules/geolocation-cn/snapshot.json")
|
||||
|
||||
@@ -0,0 +1,135 @@
|
||||
import Foundation
|
||||
import XCTest
|
||||
|
||||
@testable import ShadowsocksX_NG2
|
||||
|
||||
/// GFWList 转换失败条件(issue #65):未知语法、空输入、异常规模与 Base64。
|
||||
final class GFWListConverterFailureTests: XCTestCase {
|
||||
private let source = RuleSourceIdentity(
|
||||
kind: .gfwlist,
|
||||
upstreamVersion: "test-version",
|
||||
label: "GFWList")
|
||||
|
||||
private let fetchedAt = Date(timeIntervalSince1970: 1_764_000_000)
|
||||
|
||||
private func converter(
|
||||
minimumRuleCount: Int = 1,
|
||||
maximumRuleCount: Int = 10_000
|
||||
) -> GFWListConverter {
|
||||
GFWListConverter(
|
||||
source: source,
|
||||
minimumRuleCount: minimumRuleCount,
|
||||
maximumRuleCount: maximumRuleCount)
|
||||
}
|
||||
|
||||
private func convert(
|
||||
_ document: String,
|
||||
previousRuleCount: Int? = nil,
|
||||
converter: GFWListConverter? = nil
|
||||
) throws -> RuleSnapshot {
|
||||
try (converter ?? self.converter()).convert(
|
||||
document: document,
|
||||
provenance: .init(
|
||||
fetchedAt: fetchedAt,
|
||||
upstreamReference: "gfwlist/gfwlist@test",
|
||||
license: "LGPL-2.1",
|
||||
attribution: "test attribution"),
|
||||
previousRuleCount: previousRuleCount)
|
||||
}
|
||||
|
||||
private func proxyHosts(_ snapshot: RuleSnapshot) -> [String] {
|
||||
snapshot.rules.compactMap { rule in
|
||||
guard rule.action == .proxy else { return nil }
|
||||
if case .domainSuffix(let value) = rule.match { return value }
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
private func directHosts(_ snapshot: RuleSnapshot) -> [String] {
|
||||
snapshot.rules.compactMap { rule in
|
||||
guard rule.action == .direct else { return nil }
|
||||
if case .domainSuffix(let value) = rule.match { return value }
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - 失败条件
|
||||
|
||||
func testUnknownSyntaxBlocksUpdate() {
|
||||
XCTAssertThrowsError(
|
||||
try convert(
|
||||
"""
|
||||
||keep.example.com
|
||||
???bogus-rule???
|
||||
""")
|
||||
) { error in
|
||||
XCTAssertEqual(
|
||||
error as? GFWListConverter.ConversionError,
|
||||
.unknownSyntax("???bogus-rule???"))
|
||||
}
|
||||
}
|
||||
|
||||
func testEmptyInputBlocksUpdate() {
|
||||
XCTAssertThrowsError(try convert("\n! only comments\n")) { error in
|
||||
XCTAssertEqual(error as? GFWListConverter.ConversionError, .emptyInput)
|
||||
}
|
||||
}
|
||||
|
||||
func testAbnormalRuleCountBlocksUpdate() {
|
||||
let strict = converter(minimumRuleCount: 10, maximumRuleCount: 100)
|
||||
XCTAssertThrowsError(try convert("||only.example.com\n", converter: strict)) { error in
|
||||
XCTAssertEqual(error as? GFWListConverter.ConversionError, .abnormalRuleCount(1))
|
||||
}
|
||||
}
|
||||
|
||||
func testAbnormalShrinkageBlocksUpdate() {
|
||||
XCTAssertThrowsError(try convert("||only.example.com\n", previousRuleCount: 100)) { error in
|
||||
XCTAssertEqual(
|
||||
error as? GFWListConverter.ConversionError,
|
||||
.abnormalScaleChange(found: 1, previous: 100))
|
||||
}
|
||||
}
|
||||
|
||||
func testGrowthWithinScaleRatioIsAccepted() throws {
|
||||
let snapshot = try convert(
|
||||
"""
|
||||
||a.example.com
|
||||
||b.example.com
|
||||
||c.example.com
|
||||
""",
|
||||
previousRuleCount: 4)
|
||||
XCTAssertEqual(snapshot.rules.count, 3)
|
||||
}
|
||||
|
||||
// MARK: - Base64 官方分发格式
|
||||
|
||||
func testDecodesOfficialBase64AutoProxyPayload() throws {
|
||||
let plain = "[AutoProxy 0.2.9]\n||b64.example.com\n@@||direct.example.com\n"
|
||||
let encoded = Data(plain.utf8).base64EncodedString()
|
||||
let snapshot = try converter().convert(
|
||||
base64Document: encoded,
|
||||
provenance: .init(
|
||||
fetchedAt: fetchedAt,
|
||||
upstreamReference: "gfwlist/gfwlist@test",
|
||||
license: "LGPL-2.1",
|
||||
attribution: "test attribution"),
|
||||
previousRuleCount: nil)
|
||||
XCTAssertEqual(proxyHosts(snapshot), ["b64.example.com"])
|
||||
XCTAssertEqual(directHosts(snapshot), ["direct.example.com"])
|
||||
}
|
||||
|
||||
func testCorruptBase64BlocksUpdate() {
|
||||
XCTAssertThrowsError(
|
||||
try converter().convert(
|
||||
base64Document: "not!!!valid@@@base64",
|
||||
provenance: .init(
|
||||
fetchedAt: fetchedAt,
|
||||
upstreamReference: "gfwlist/gfwlist@test",
|
||||
license: "LGPL-2.1",
|
||||
attribution: "test attribution"),
|
||||
previousRuleCount: nil)
|
||||
) { error in
|
||||
XCTAssertEqual(error as? GFWListConverter.ConversionError, .corruptInput)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,237 @@
|
||||
import Foundation
|
||||
import XCTest
|
||||
|
||||
@testable import ShadowsocksX_NG2
|
||||
|
||||
/// GFWList AutoProxy 转换器(issue #65):Base64 解码、可表达域名规则、
|
||||
/// 已知不可表达分类、`@@` 例外遮蔽、损失报告、来源元数据与异常缩小失败。
|
||||
final class GFWListConverterTests: XCTestCase {
|
||||
private let source = RuleSourceIdentity(
|
||||
kind: .gfwlist,
|
||||
upstreamVersion: "test-version",
|
||||
label: "GFWList")
|
||||
|
||||
private let fetchedAt = Date(timeIntervalSince1970: 1_764_000_000)
|
||||
|
||||
private func converter(
|
||||
minimumRuleCount: Int = 1,
|
||||
maximumRuleCount: Int = 10_000
|
||||
) -> GFWListConverter {
|
||||
GFWListConverter(
|
||||
source: source,
|
||||
minimumRuleCount: minimumRuleCount,
|
||||
maximumRuleCount: maximumRuleCount)
|
||||
}
|
||||
|
||||
private func convert(
|
||||
_ document: String,
|
||||
previousRuleCount: Int? = nil,
|
||||
converter: GFWListConverter? = nil
|
||||
) throws -> RuleSnapshot {
|
||||
try (converter ?? self.converter()).convert(
|
||||
document: document,
|
||||
provenance: .init(
|
||||
fetchedAt: fetchedAt,
|
||||
upstreamReference: "gfwlist/gfwlist@test",
|
||||
license: "LGPL-2.1",
|
||||
attribution: "test attribution"),
|
||||
previousRuleCount: previousRuleCount)
|
||||
}
|
||||
|
||||
private func proxyHosts(_ snapshot: RuleSnapshot) -> [String] {
|
||||
snapshot.rules.compactMap { rule in
|
||||
guard rule.action == .proxy else { return nil }
|
||||
if case .domainSuffix(let value) = rule.match { return value }
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
private func directHosts(_ snapshot: RuleSnapshot) -> [String] {
|
||||
snapshot.rules.compactMap { rule in
|
||||
guard rule.action == .direct else { return nil }
|
||||
if case .domainSuffix(let value) = rule.match { return value }
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - 可表达域名规则
|
||||
|
||||
func testConvertsDomainSuffixProxyRules() throws {
|
||||
let snapshot = try convert(
|
||||
"""
|
||||
[AutoProxy 0.2.9]
|
||||
! comment
|
||||
||example.com
|
||||
||sub.example.org^
|
||||
||Foo.Bar.COM
|
||||
""")
|
||||
XCTAssertEqual(
|
||||
Set(proxyHosts(snapshot)),
|
||||
["example.com", "sub.example.org", "foo.bar.com"])
|
||||
XCTAssertTrue(directHosts(snapshot).isEmpty)
|
||||
XCTAssertEqual(snapshot.lossReport.convertedCount, 3)
|
||||
XCTAssertTrue(snapshot.rules.allSatisfy { $0.action == .proxy })
|
||||
XCTAssertEqual(snapshot.metadata.source.kind, .gfwlist)
|
||||
XCTAssertEqual(snapshot.metadata.converterVersion, RuleSnapshotMetadata.currentConverterVersion)
|
||||
XCTAssertFalse(snapshot.metadata.inputDigest.isEmpty)
|
||||
XCTAssertFalse(snapshot.metadata.license.isEmpty)
|
||||
XCTAssertFalse(snapshot.metadata.attribution.isEmpty)
|
||||
XCTAssertEqual(snapshot.metadata.fetchedAt, fetchedAt)
|
||||
}
|
||||
|
||||
func testCaretAndBareDomainFormsShareDomainSuffixSemantics() throws {
|
||||
let snapshot = try convert(
|
||||
"""
|
||||
||example.com
|
||||
||example.com^
|
||||
||www.example.com
|
||||
""")
|
||||
// 去重后 example.com 只保留一条。
|
||||
XCTAssertEqual(proxyHosts(snapshot), ["example.com", "www.example.com"])
|
||||
}
|
||||
|
||||
// MARK: - 已知不可表达:不扩大为整域名
|
||||
|
||||
func testDoesNotExpandURLPathOrProtocolConditions() throws {
|
||||
// 夹具只含不可表达条目;把下限调 0 以便只验证分类与不扩大。
|
||||
let snapshot = try convert(
|
||||
"""
|
||||
|http://news.example.com/path
|
||||
|https://secure.example.org
|
||||
||path.example.net/foo/bar
|
||||
||path.example.net/*-*/deep
|
||||
||option.example.edu$third-party
|
||||
||cdn*.example.io
|
||||
/regex-rule/
|
||||
plain-substring.example
|
||||
||keep.example.com
|
||||
""",
|
||||
converter: converter(minimumRuleCount: 0))
|
||||
XCTAssertEqual(proxyHosts(snapshot), ["keep.example.com"])
|
||||
XCTAssertEqual(snapshot.lossReport.skipped["urlPrefix"], 2)
|
||||
XCTAssertEqual(snapshot.lossReport.skipped["urlPath"], 2)
|
||||
XCTAssertEqual(snapshot.lossReport.skipped["filterOption"], 1)
|
||||
XCTAssertEqual(snapshot.lossReport.skipped["wildcard"], 1)
|
||||
XCTAssertEqual(snapshot.lossReport.skipped["regexp"], 1)
|
||||
XCTAssertEqual(snapshot.lossReport.skipped["plainText"], 1)
|
||||
XCTAssertEqual(snapshot.lossReport.rejected, [:])
|
||||
}
|
||||
|
||||
func testClassifiesHeaderCommentsAndBlanksWithoutFailing() throws {
|
||||
let snapshot = try convert(
|
||||
"""
|
||||
[AutoProxy 0.2.9]
|
||||
! Title: GFWList
|
||||
|
||||
||keep.example.com
|
||||
""")
|
||||
XCTAssertEqual(proxyHosts(snapshot), ["keep.example.com"])
|
||||
XCTAssertNil(snapshot.lossReport.skipped["unknownSyntax"])
|
||||
XCTAssertEqual(snapshot.lossReport.skipped["header"], 1)
|
||||
XCTAssertEqual(snapshot.lossReport.skipped["comment"], 1)
|
||||
XCTAssertGreaterThanOrEqual(snapshot.lossReport.skipped["blank"] ?? 0, 1)
|
||||
}
|
||||
|
||||
func testSingleLabelDomainPrefixIsNotLossless() throws {
|
||||
// `||google` 在 AutoProxy 是域名前缀匹配,不等价于 domainSuffix("google")。
|
||||
let snapshot = try convert(
|
||||
"""
|
||||
||google
|
||||
||goog
|
||||
||gle
|
||||
||google.com
|
||||
""")
|
||||
XCTAssertEqual(proxyHosts(snapshot), ["google.com"])
|
||||
XCTAssertEqual(snapshot.lossReport.skipped["singleLabelPrefix"], 3)
|
||||
}
|
||||
|
||||
func testRejectsIPLiteralDomainStyleRulesAsNotCIDR() throws {
|
||||
// `||1.2.3.4` 是主机名字面量域名规则,不是 CIDR;不得猜测成 IP 代理规则。
|
||||
let snapshot = try convert(
|
||||
"""
|
||||
||1.2.3.4
|
||||
|http://8.8.8.8/
|
||||
||valid.example.com
|
||||
""")
|
||||
XCTAssertEqual(proxyHosts(snapshot), ["valid.example.com"])
|
||||
XCTAssertEqual(snapshot.lossReport.skipped["ipLiteral"], 1)
|
||||
XCTAssertEqual(snapshot.lossReport.skipped["urlPrefix"], 1)
|
||||
}
|
||||
|
||||
// MARK: - @@ 例外与遮蔽
|
||||
|
||||
func testUnshadowedExceptionBecomesDirectRule() throws {
|
||||
let snapshot = try convert(
|
||||
"""
|
||||
||blocked.example
|
||||
@@||other.example
|
||||
""")
|
||||
XCTAssertEqual(proxyHosts(snapshot), ["blocked.example"])
|
||||
XCTAssertEqual(directHosts(snapshot), ["other.example"])
|
||||
XCTAssertEqual(snapshot.lossReport.skipped["shadowedException"] ?? 0, 0)
|
||||
}
|
||||
|
||||
func testExceptionShadowedByBroaderProxyIsNotWritten() throws {
|
||||
let snapshot = try convert(
|
||||
"""
|
||||
||example.com
|
||||
||gstatic.com
|
||||
@@||fonts.gstatic.com
|
||||
@@||www.example.com
|
||||
@@||unrelated.example.org
|
||||
""")
|
||||
// 遮蔽的例外不写入无效 ACL 项;更宽代理规则保留。
|
||||
XCTAssertEqual(Set(proxyHosts(snapshot)), ["example.com", "gstatic.com"])
|
||||
XCTAssertEqual(directHosts(snapshot), ["unrelated.example.org"])
|
||||
XCTAssertEqual(snapshot.lossReport.skipped["shadowedException"], 2)
|
||||
XCTAssertEqual(snapshot.lossReport.absorbedCount, 2, "遮蔽例外计入吸收/遮蔽条目数")
|
||||
let notes = snapshot.lossReport.notes.joined(separator: "\n")
|
||||
XCTAssertTrue(notes.contains("@@||fonts.gstatic.com"), "逐项报告被遮蔽的例外")
|
||||
XCTAssertTrue(notes.contains("@@||www.example.com"))
|
||||
XCTAssertTrue(notes.contains("||gstatic.com") || notes.contains("||example.com"))
|
||||
// 被遮蔽例外保留在 absorbed 供审计。
|
||||
XCTAssertEqual(snapshot.absorbed.count, 2)
|
||||
XCTAssertTrue(
|
||||
snapshot.absorbed.allSatisfy {
|
||||
$0.action == .direct && $0.conflict.notes.contains("shadowed-by-broader-proxy")
|
||||
})
|
||||
}
|
||||
|
||||
func testEqualMatchExceptionIsShadowedByProxy() throws {
|
||||
let snapshot = try convert(
|
||||
"""
|
||||
||same.example
|
||||
@@||same.example
|
||||
""")
|
||||
XCTAssertEqual(proxyHosts(snapshot), ["same.example"])
|
||||
XCTAssertTrue(directHosts(snapshot).isEmpty)
|
||||
XCTAssertEqual(snapshot.lossReport.skipped["shadowedException"], 1)
|
||||
}
|
||||
|
||||
func testNarrowerProxyDoesNotShadowBroaderException() throws {
|
||||
// 更窄代理规则不遮蔽更宽例外:两者都保留;sslocal 域名优先级由 proxy_list 先匹配。
|
||||
let snapshot = try convert(
|
||||
"""
|
||||
||sub.example.com
|
||||
@@||example.com
|
||||
""")
|
||||
XCTAssertEqual(proxyHosts(snapshot), ["sub.example.com"])
|
||||
XCTAssertEqual(directHosts(snapshot), ["example.com"])
|
||||
XCTAssertEqual(snapshot.lossReport.skipped["shadowedException"] ?? 0, 0)
|
||||
}
|
||||
|
||||
func testExceptionComplexFormsAreClassifiedNotShadowed() throws {
|
||||
let snapshot = try convert(
|
||||
"""
|
||||
||example.com
|
||||
@@||*.tokenplus.app
|
||||
@@|https://cdn.example.net/path
|
||||
@@/regex-exception/
|
||||
""")
|
||||
XCTAssertEqual(proxyHosts(snapshot), ["example.com"])
|
||||
XCTAssertTrue(directHosts(snapshot).isEmpty)
|
||||
XCTAssertEqual(snapshot.lossReport.skipped["wildcard"], 1)
|
||||
XCTAssertEqual(snapshot.lossReport.skipped["urlPrefix"], 1)
|
||||
XCTAssertEqual(snapshot.lossReport.skipped["regexp"], 1)
|
||||
}
|
||||
}
|
||||
@@ -88,7 +88,7 @@ final class ProxyACLTests: XCTestCase {
|
||||
let acl = ProxyACLDocument.rule(
|
||||
at: URL(fileURLWithPath: "/tmp/ssxng-test/sslocal-active.acl"),
|
||||
defaultAction: .proxyWhenUnmatched,
|
||||
chinaRules: rules)
|
||||
rules: rules)
|
||||
|
||||
XCTAssertTrue(acl.isWellFormed)
|
||||
XCTAssertEqual(acl.summary, "rule-proxy-default")
|
||||
@@ -105,26 +105,64 @@ final class ProxyACLTests: XCTestCase {
|
||||
XCTAssertFalse(acl.content.contains("100.64.0.0/10"), "不加入 CGNAT")
|
||||
}
|
||||
|
||||
/// 规则+未匹配时直连:bypass_all + 固定本地 + 中国直连 + 可生效代理规则。
|
||||
func testRuleDirectDefaultACLUsesBypassAll() throws {
|
||||
let source = RuleSourceIdentity(
|
||||
kind: .geolocationCN, upstreamVersion: "v1", label: "geolocation-cn")
|
||||
/// 规则+未匹配时直连(issue #65):bypass_all + 固定本地 + GFWList 可生效
|
||||
/// 代理规则。两种默认动作不把全部内置来源无条件并集,因此不混入中国直连项。
|
||||
func testRuleDirectDefaultACLUsesBypassAllAndGFWListProxyCandidates() throws {
|
||||
let gfwSource = RuleSourceIdentity(
|
||||
kind: .gfwlist, upstreamVersion: "v1", label: "GFWList")
|
||||
let rules = [
|
||||
ProxyRule(action: .direct, match: try RuleMatch(nationalDomainSuffix: "cn"), source: source),
|
||||
ProxyRule(
|
||||
action: .proxy, match: try RuleMatch(domainSuffix: "blocked.example"), source: source),
|
||||
action: .proxy, match: try RuleMatch(domainSuffix: "blocked.example"), source: gfwSource),
|
||||
ProxyRule(
|
||||
action: .proxy, match: try RuleMatch(domainSuffix: "also-blocked.example"),
|
||||
source: gfwSource),
|
||||
ProxyRule(
|
||||
action: .direct, match: try RuleMatch(domainSuffix: "unshadowed.example"), source: gfwSource
|
||||
),
|
||||
]
|
||||
let acl = ProxyACLDocument.rule(
|
||||
at: URL(fileURLWithPath: "/tmp/ssxng-test/sslocal-active.acl"),
|
||||
defaultAction: .directWhenUnmatched,
|
||||
chinaRules: rules)
|
||||
rules: rules)
|
||||
|
||||
XCTAssertTrue(acl.isWellFormed)
|
||||
XCTAssertEqual(acl.summary, "rule-direct-default")
|
||||
XCTAssertTrue(acl.content.hasPrefix("[bypass_all]\n[bypass_list]\n"))
|
||||
XCTAssertTrue(acl.content.contains("[proxy_list]\n||blocked.example\n"))
|
||||
XCTAssertTrue(acl.content.contains("||cn\n"))
|
||||
XCTAssertTrue(acl.content.contains("||also-blocked.example\n"))
|
||||
XCTAssertTrue(acl.content.contains("||unshadowed.example\n"), "未遮蔽例外参与编译进 bypass_list")
|
||||
XCTAssertFalse(acl.content.contains("[proxy_all]"))
|
||||
// 不无条件并集中国来源。
|
||||
XCTAssertFalse(acl.content.contains("||cn\n"))
|
||||
// 固定本地绕过仍在。
|
||||
XCTAssertTrue(acl.content.contains("127.0.0.0/8\n"))
|
||||
XCTAssertTrue(acl.content.contains("||localhost\n"))
|
||||
// 未匹配目标走 bypass_all 默认直连。
|
||||
XCTAssertTrue(acl.content.hasPrefix("[bypass_all]"))
|
||||
}
|
||||
|
||||
/// 规则+未匹配时直连:IP 字面目标无可用规则命中时直连(无 IP 代理规则时
|
||||
/// proxy_list 只有域名项)。
|
||||
func testRuleDirectDefaultACLLeavesIPLiteralsUnmatchedDirect() throws {
|
||||
let gfwSource = RuleSourceIdentity(
|
||||
kind: .gfwlist, upstreamVersion: "v1", label: "GFWList")
|
||||
let rules = [
|
||||
ProxyRule(
|
||||
action: .proxy, match: try RuleMatch(domainSuffix: "blocked.example"), source: gfwSource)
|
||||
]
|
||||
let acl = ProxyACLDocument.rule(
|
||||
at: URL(fileURLWithPath: "/tmp/ssxng-test/sslocal-active.acl"),
|
||||
defaultAction: .directWhenUnmatched,
|
||||
rules: rules)
|
||||
|
||||
let proxySection =
|
||||
(acl.content.components(separatedBy: "[proxy_list]\n").last ?? "")
|
||||
.split(whereSeparator: \.isNewline).map(String.init)
|
||||
XCTAssertTrue(
|
||||
proxySection.allSatisfy { line in
|
||||
line.hasPrefix("||") || line.hasPrefix("|") || line.hasPrefix("^")
|
||||
},
|
||||
"GFWList 代理候选只含域名规则;IP 字面目标未命中即直连")
|
||||
}
|
||||
|
||||
/// 规则+未匹配时代理(issue #64):proxy_all 同时含中国域名与 IPv4 CIDR
|
||||
@@ -145,7 +183,7 @@ final class ProxyACLTests: XCTestCase {
|
||||
let acl = ProxyACLDocument.rule(
|
||||
at: URL(fileURLWithPath: "/tmp/ssxng-test/sslocal-active.acl"),
|
||||
defaultAction: .proxyWhenUnmatched,
|
||||
chinaRules: rules)
|
||||
rules: rules)
|
||||
|
||||
XCTAssertTrue(acl.isWellFormed)
|
||||
XCTAssertTrue(acl.content.hasPrefix("[proxy_all]\n[bypass_list]\n"))
|
||||
|
||||
@@ -120,7 +120,7 @@ extension RealSslocalSmokeTests {
|
||||
acl: .rule(
|
||||
at: workDir.appendingPathComponent("sslocal-active.acl"),
|
||||
defaultAction: .proxyWhenUnmatched,
|
||||
chinaRules: chinaRules))
|
||||
rules: chinaRules))
|
||||
XCTAssertTrue(document.isWellFormed)
|
||||
let wrapper = try launchWrapper(document)
|
||||
defer {
|
||||
@@ -162,7 +162,7 @@ extension RealSslocalSmokeTests {
|
||||
acl: .rule(
|
||||
at: workDir.appendingPathComponent("sslocal-active.acl"),
|
||||
defaultAction: .proxyWhenUnmatched,
|
||||
chinaRules: try chinaCIDRSmokeRules()))
|
||||
rules: try chinaCIDRSmokeRules()))
|
||||
XCTAssertTrue(document.isWellFormed)
|
||||
let wrapper = try launchWrapper(document)
|
||||
defer {
|
||||
|
||||
@@ -0,0 +1,201 @@
|
||||
import Darwin
|
||||
import XCTest
|
||||
|
||||
@testable import ShadowsocksX_NG2
|
||||
|
||||
extension RealSslocalSmokeTests {
|
||||
/// 规则+未匹配时直连(issue #65):bypass_all + GFWList 代理候选。命中
|
||||
/// GFWList 域名走代理;未匹配域名与 IP 字面目标直连;SOCKS 与 HTTP 共用 ACL。
|
||||
func testRuleDirectDefaultACLRoutesGFWListProxyAndUnmatchedDirect() throws {
|
||||
let echoServer = try LoopbackEchoServer()
|
||||
let fakeSSServer = try ConnectionCountingServer()
|
||||
let selectedPorts = try grabThreeListenPorts(excluding: [echoServer.port, fakeSSServer.port])
|
||||
let listen = SslocalListenSettings(
|
||||
socksPort: selectedPorts[0], httpPort: selectedPorts[1], pacPort: selectedPorts[2])
|
||||
let gfwSource = RuleSourceIdentity(
|
||||
kind: .gfwlist, upstreamVersion: "test", label: "GFWList")
|
||||
let gfwRules = [
|
||||
ProxyRule(
|
||||
action: .proxy, match: try RuleMatch(domainSuffix: "blocked.example"),
|
||||
source: gfwSource),
|
||||
ProxyRule(
|
||||
action: .proxy, match: try RuleMatch(domainSuffix: "cdn.blocked.example"),
|
||||
source: gfwSource),
|
||||
]
|
||||
let document = SslocalRuntimeDocument(
|
||||
servers: [
|
||||
SslocalServerDocument(
|
||||
id: "gfw-smoke-server",
|
||||
remarks: "gfw-smoke",
|
||||
server: "127.0.0.1",
|
||||
serverPort: fakeSSServer.port,
|
||||
password: "smoke-password",
|
||||
method: "aes-256-gcm",
|
||||
plugin: nil,
|
||||
pluginOpts: nil)
|
||||
],
|
||||
listen: listen,
|
||||
acl: .rule(
|
||||
at: workDir.appendingPathComponent("sslocal-active.acl"),
|
||||
defaultAction: .directWhenUnmatched,
|
||||
rules: gfwRules))
|
||||
XCTAssertTrue(document.isWellFormed)
|
||||
let wrapper = try launchWrapper(document)
|
||||
defer {
|
||||
if wrapper.isRunning { kill(wrapper.processIdentifier, SIGTERM) }
|
||||
}
|
||||
|
||||
try awaitGlobalInboundsReady(listen: listen, document: document)
|
||||
try assertLocalTargetsBypass(listen: listen, echoPort: echoServer.port, fakeSS: fakeSSServer)
|
||||
try assertGFWListDirectDefaultRouting(listen: listen, fakeSS: fakeSSServer)
|
||||
|
||||
stopWrapperAndAssertCleanExit(wrapper, description: "gfwlist rule wrapper exits")
|
||||
}
|
||||
|
||||
/// 原版 sslocal ACL 域名/IP 优先级(issue #65 AC5):域名 proxy_list 先于
|
||||
/// bypass_list,IP bypass_list 先于 proxy_list;两个入站共用同一 ACL。
|
||||
func testRealSslocalDomainAndIPACLPriorityOnSOCKSAndHTTP() throws {
|
||||
let echoServer = try LoopbackEchoServer()
|
||||
let fakeSSServer = try ConnectionCountingServer()
|
||||
let selectedPorts = try grabThreeListenPorts(excluding: [echoServer.port, fakeSSServer.port])
|
||||
let listen = SslocalListenSettings(
|
||||
socksPort: selectedPorts[0], httpPort: selectedPorts[1], pacPort: selectedPorts[2])
|
||||
let acl = priorityProbeACL()
|
||||
let document = SslocalRuntimeDocument(
|
||||
servers: [
|
||||
SslocalServerDocument(
|
||||
id: "priority-smoke-server",
|
||||
remarks: "priority-smoke",
|
||||
server: "127.0.0.1",
|
||||
serverPort: fakeSSServer.port,
|
||||
password: "smoke-password",
|
||||
method: "aes-256-gcm",
|
||||
plugin: nil,
|
||||
pluginOpts: nil)
|
||||
],
|
||||
listen: listen,
|
||||
acl: acl)
|
||||
XCTAssertTrue(document.isWellFormed)
|
||||
let wrapper = try launchWrapper(document)
|
||||
defer {
|
||||
if wrapper.isRunning { kill(wrapper.processIdentifier, SIGTERM) }
|
||||
}
|
||||
|
||||
try awaitGlobalInboundsReady(listen: listen, document: document)
|
||||
try assertDomainPriorityBeatsBypassList(listen: listen, fakeSS: fakeSSServer)
|
||||
try assertIPPriorityBeatsProxyList(listen: listen, fakeSS: fakeSSServer)
|
||||
|
||||
let afterHTTPBase = fakeSSServer.connectionCount
|
||||
performHTTPConnect(httpPort: listen.httpPort, targetHost: "sub.example.com", targetPort: 443)
|
||||
XCTAssertTrue(
|
||||
try waitForCondition(timeout: 5) { fakeSSServer.connectionCount > afterHTTPBase },
|
||||
"HTTP 入站应应用同一域名优先级")
|
||||
|
||||
stopWrapperAndAssertCleanExit(wrapper, description: "priority wrapper exits")
|
||||
}
|
||||
|
||||
/// 双列表优先级探针 ACL:域名与 IP 各有冲突项,用于观测 sslocal 真实匹配顺序。
|
||||
private func priorityProbeACL() -> ProxyACLDocument {
|
||||
let aclContent = """
|
||||
[proxy_all]
|
||||
[bypass_list]
|
||||
127.0.0.0/8
|
||||
10.0.0.0/8
|
||||
172.16.0.0/12
|
||||
192.168.0.0/16
|
||||
169.254.0.0/16
|
||||
::1/128
|
||||
fe80::/10
|
||||
fc00::/7
|
||||
||localhost
|
||||
||local
|
||||
^[^.]+$
|
||||
8.8.8.8/32
|
||||
||sub.example.com
|
||||
[proxy_list]
|
||||
||example.com
|
||||
8.8.8.0/24
|
||||
"""
|
||||
let acl = ProxyACLDocument(
|
||||
path: workDir.appendingPathComponent("sslocal-active.acl").standardizedFileURL.path,
|
||||
summary: "priority-probe",
|
||||
content: aclContent)
|
||||
XCTAssertTrue(acl.isWellFormed)
|
||||
return acl
|
||||
}
|
||||
|
||||
/// 域名:proxy_list 的 `||example.com` 覆盖 bypass_list 的 `||sub.example.com`。
|
||||
private func assertDomainPriorityBeatsBypassList(
|
||||
listen: SslocalListenSettings, fakeSS: ConnectionCountingServer
|
||||
) throws {
|
||||
let beforeDomain = fakeSS.connectionCount
|
||||
performSocksConnectReply(
|
||||
socksPort: listen.socksPort, targetHost: "sub.example.com", targetPort: 443)
|
||||
XCTAssertTrue(
|
||||
try waitForCondition(timeout: 5) { fakeSS.connectionCount > beforeDomain },
|
||||
"域名优先级:proxy_list 应优先于 bypass_list")
|
||||
}
|
||||
|
||||
/// IP:bypass_list 的 `8.8.8.8/32` 覆盖 proxy_list 的 `8.8.8.0/24`;
|
||||
/// 同 CIDR 内未单列直连的 IP 仍走 proxy_list。
|
||||
private func assertIPPriorityBeatsProxyList(
|
||||
listen: SslocalListenSettings, fakeSS: ConnectionCountingServer
|
||||
) throws {
|
||||
let beforeIP = fakeSS.connectionCount
|
||||
performSocksConnectReply(
|
||||
socksPort: listen.socksPort,
|
||||
request: socksIPv4ConnectRequest([8, 8, 8, 8], port: 53))
|
||||
Thread.sleep(forTimeInterval: 0.5)
|
||||
XCTAssertEqual(
|
||||
fakeSS.connectionCount, beforeIP,
|
||||
"IP 优先级:bypass_list 应优先于 proxy_list")
|
||||
|
||||
performSocksConnectReply(
|
||||
socksPort: listen.socksPort,
|
||||
request: socksIPv4ConnectRequest([8, 8, 8, 1], port: 53))
|
||||
XCTAssertTrue(
|
||||
try waitForCondition(timeout: 5) { fakeSS.connectionCount > beforeIP },
|
||||
"未在 bypass_list 单列的 IP 应命中 proxy_list CIDR")
|
||||
}
|
||||
|
||||
/// GFWList「未匹配时直连」路由断言:命中代理候选走 SS;未匹配域名与 IP
|
||||
/// 字面目标不触达 SS(bypass_all 默认直连)。
|
||||
func assertGFWListDirectDefaultRouting(
|
||||
listen: SslocalListenSettings, fakeSS: ConnectionCountingServer
|
||||
) throws {
|
||||
let beforeHit = fakeSS.connectionCount
|
||||
performSocksConnectReply(
|
||||
socksPort: listen.socksPort, targetHost: "blocked.example", targetPort: 443)
|
||||
XCTAssertTrue(
|
||||
try waitForCondition(timeout: 5) { fakeSS.connectionCount > beforeHit },
|
||||
"GFWList 代理候选应连接 Shadowsocks 出口")
|
||||
|
||||
let afterHit = fakeSS.connectionCount
|
||||
performSocksConnectReply(
|
||||
socksPort: listen.socksPort, targetHost: "cdn.blocked.example", targetPort: 443)
|
||||
XCTAssertTrue(
|
||||
try waitForCondition(timeout: 5) { fakeSS.connectionCount > afterHit },
|
||||
"GFWList 子域代理候选应连接 Shadowsocks 出口")
|
||||
|
||||
let afterMatched = fakeSS.connectionCount
|
||||
performSocksConnectReply(
|
||||
socksPort: listen.socksPort, targetHost: "unmatched.example.org", targetPort: 443)
|
||||
Thread.sleep(forTimeInterval: 0.5)
|
||||
XCTAssertEqual(
|
||||
fakeSS.connectionCount, afterMatched,
|
||||
"未匹配域名在 bypass_all 下应直连,不得触达 Shadowsocks 出口")
|
||||
|
||||
performSocksConnectReply(
|
||||
socksPort: listen.socksPort,
|
||||
request: socksIPv4ConnectRequest([1, 1, 1, 1], port: 443))
|
||||
Thread.sleep(forTimeInterval: 0.5)
|
||||
XCTAssertEqual(
|
||||
fakeSS.connectionCount, afterMatched,
|
||||
"未命中规则的 IP 字面目标应直连")
|
||||
|
||||
performHTTPConnect(httpPort: listen.httpPort, targetHost: "blocked.example", targetPort: 443)
|
||||
XCTAssertTrue(
|
||||
try waitForCondition(timeout: 5) { fakeSS.connectionCount > afterMatched },
|
||||
"HTTP 入站应应用同一 GFWList ACL")
|
||||
}
|
||||
}
|
||||
@@ -132,6 +132,16 @@ extension RealSslocalSmokeTests {
|
||||
return request
|
||||
}
|
||||
|
||||
/// SOCKS5 CONNECT 到 IPv4 字面量(ATYP=0x01),用于域名/IP 优先级断言。
|
||||
func socksIPv4ConnectRequest(_ octets: [UInt8], port: Int) -> [UInt8] {
|
||||
precondition(octets.count == 4, "IPv4 request needs 4 octets")
|
||||
var request: [UInt8] = [0x05, 0x01, 0x00, 0x01]
|
||||
request.append(contentsOf: octets)
|
||||
let portBytes = UInt16(port).bigEndian
|
||||
withUnsafeBytes(of: portBytes) { request.append(contentsOf: $0) }
|
||||
return request
|
||||
}
|
||||
|
||||
/// 对公网目标发起 HTTP CONNECT(应答头读到即返回,不要求成功)。
|
||||
func performHTTPConnect(
|
||||
httpPort: Int, targetHost: String, targetPort: Int
|
||||
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
gfwlist rule snapshot
|
||||
=====================
|
||||
|
||||
Upstream: gfwlist/gfwlist gfwlist.txt
|
||||
Upstream commit: c19292511da4c5af8f6193fd32d666eaaa0312f9
|
||||
Upstream URL: https://raw.githubusercontent.com/gfwlist/gfwlist/c19292511da4c5af8f6193fd32d666eaaa0312f9/gfwlist.txt
|
||||
Upstream archive SHA-256: 750d34564363075fafa12d8ab9739f269e2fbd2bf3c5f4ab8cfd859bfd3441df
|
||||
|
||||
License: LGPL-2.1
|
||||
|
||||
GFWList AutoProxy rules derived from gfwlist/gfwlist gfwlist.txt at commit c19292511da4c5af8f6193fd32d666eaaa0312f9. Official Base64 AutoProxy 0.2.9 list. Only rules losslessly expressible as target domains are converted; URL-path, protocol, wildcard, and regex conditions are reported as losses and never expanded into whole-domain rules. List is candidate routing data, not a guarantee of reachability or completeness.
|
||||
|
||||
This list is candidate routing data. It is not a guarantee of network
|
||||
reachability, geographic ownership, or privacy. Rules that cannot be expressed
|
||||
losslessly as target domains (URL paths, protocol conditions, wildcards, regex)
|
||||
are counted and reported as conversion losses and are never expanded into
|
||||
whole-domain proxy rules. `@@` exceptions shadowed by a broader proxy rule are
|
||||
reported item by item and omitted from the generated ACL because sslocal would
|
||||
ignore them.
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"converterVersion": "1.0.0",
|
||||
"name": "gfwlist",
|
||||
"snapshotSHA256": "cad162e57a5f75e5173d971cdc7db167b1689230458783f03052b274dc0b698c",
|
||||
"upstreamArchiveSHA256": "750d34564363075fafa12d8ab9739f269e2fbd2bf3c5f4ab8cfd859bfd3441df",
|
||||
"upstreamURL": "https://raw.githubusercontent.com/gfwlist/gfwlist/c19292511da4c5af8f6193fd32d666eaaa0312f9/gfwlist.txt",
|
||||
"upstreamVersion": "c19292511da4c5af8f6193fd32d666eaaa0312f9"
|
||||
}
|
||||
+69826
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user