swift workflow codecov License: MIT Issues Releases

SwiftPublicSuffixList

这个库是用 Swift 实现的必要代码,用于对照 Public Suffix List(公共后缀列表) 检查域名,并识别这些域名是否应该受到限制。

受限制的域名不应该被允许设置 Cookie,直接托管网站或发送/接收电子邮件。

截至 2022 年 1 月,该列表包含超过 9000 个条目。

性能考量

由于公共后缀列表中的条目数量非常多(>9k),你可能需要在应用程序启动后不久,在后台线程中预加载 PublicSuffixRulesRegistry.rules。列表的初始加载可能需要 100 毫秒到 900 毫秒,具体取决于宿主设备。

建议定期更新

上次更新时间:2022-07-12 19:31:00 EST

Shell 命令

你可以从命令行运行 Utilities/update-suffix.swift,以下载并处理包含公共后缀列表的文本文件,并重新生成 PublicSuffixRulesRegistry.swift 文件。

# swift update-suffix.swift

从 Swift 在运行时

为了在运行时更新 PublicSuffixList,你需要确保使用 PublicSuffixList 的实例,而不是静态函数。你可以这样做:

import SwiftPublicSuffixList

let pathToLocalRegistry = FileManager.default
    .urls(for: .cachesDirectory, in: .userDomainMask).first!
    .appendingPathComponent("public-suffix-list.json")

let publicSuffixList = await PublicSuffixList.list(from: .filePath(pathToLocalRegistry))

请求注册表更新:

let success: Bool = await publicSuffixList.updateUsingOnlineRegistry()

保存更新后的注册表:

try publicSuffixList.export(to: pathToLocalRegistry)

类 & 用法

PublicSuffixList

.match(_ candidate: String, rules: [[String]]) -> Match?

import SwiftPublicSuffixList

使用默认的内置公共后缀列表规则

if let match = PublicSuffixList.match("yahoo.com") {
    // match.isRestricted == false
}

// or using a PublicSuffixList instance…    
let publicSuffixList = PublicSuffixList()
if let match = publicSuffixList.match("yahoo.com") {
    // match.isRestricted == false
}

// or the async equivalent
let publicSuffixList = await PublicSuffixList.list()
if let match = await publicSuffixList.match("yahoo.com") {
    // match.isRestricted == false
}

使用单个自定义验证规则,要求域名以 .com 结尾,但允许 .com TLD 中的任何域名

if let match = PublicSuffixList.match("yahoo.com", rules: [["com"]]) {
    // match.isRestricted == false
    // match.prevailingRule == ["com"]
    // match.matchedRules == [["com"]]
}

// or using a PublicSuffixList instance…

let publicSuffixList = PublicSuffixList(source: .rules([["com"]]))
if let match = publicSuffixList.match("yahoo.com") {
    // match.isRestricted == false
    // match.prevailingRule == ["com"]
    // match.matchedRules == [["com"]]
}

使用单个自定义验证规则,限制以 .com 结尾的域名,但允许任何子域名

if let match = PublicSuffixList.match("yahoo.com", rules: [["*","com"]]) {
   // yahoo.com matches \*.com and so it is restricted
   // match.isRestricted == true
   // match.prevailingRule == ["*","com"]
   // match.matchedRules == [["*","com"]]
}

if let match = PublicSuffixList.match("www.yahoo.com", [["*","com"]]) {
   // While yahoo.com matches \*.com and is restricted, there are no
   // restrictions for subdomains such as www.yahoo.com
   // match.isRestricted == false
   // match.prevailingRule == ["*","com"]
   // match.matchedRules == [["*","com"]]
}

定义对更通用规则的例外情况

if let match = PublicSuffixList.match("yahoo.com", rules: [["*","com"],["!yahoo","com"]]) {
    // Even if yahoo.com matches *.com, since there is an exception
    // for this domain (defined using !) it will not be restricted
    // match.isRestricted == false
    // match.prevailingRule == ["!yahoo","com"]
    // match.matchedRules == [["*","com"],["!yahoo","com"]]
}

.isUnrestricted(_ candiate: String, rules: [[String]]) -> Bool

便捷函数,将尝试检索匹配项,然后返回 !match.isRestricted 的值。 如果未找到匹配项,将返回 false。

if PublicSuffixList.isUnrestricted("yahoo.com") {
    // true! yahoo.com is unrestricted by default
}

// or using PublicSuffixList instance…

let publicSuffixList = PublicSuffixList()
if publicSuffixList.isUnrestricted("yahoo.com") {
    // true! yahoo.com is unrestricted by default
}