forked from REIJI007/AdBlock_Rule_For_Clash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadblock_rule_generator_txt.ps1
115 lines (101 loc) · 5.2 KB
/
adblock_rule_generator_txt.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# Title: AdBlock_Rule_For_Clash
# Description: 适用于Clash的域名拦截规则集,每20分钟更新一次,确保即时同步上游减少误杀
# Homepage: https://github.com/REIJI007/AdBlock_Rule_For_Clash
# LICENSE1: https://github.com/REIJI007/AdBlock_Rule_For_Clash/blob/main/LICENSE-GPL3.0
# LICENSE2: https://github.com/REIJI007/AdBlock_Rule_For_Clash/blob/main/LICENSE-CC%20BY-NC-SA%204.0
# 定义广告过滤器URL列表
$urlList = @(
"https://big.oisd.nl/",
"https://raw.githubusercontent.com/Cats-Team/AdRules/main/adblock_plus.txt",
"https://raw.githubusercontent.com/Cats-Team/AdRules/main/dns.txt",
"https://raw.githubusercontent.com/8680/GOODBYEADS/master/rules.txt",
"https://raw.githubusercontent.com/8680/GOODBYEADS/master/dns.txt",
"https://raw.githubusercontent.com/8680/GOODBYEADS/master/allow.txt",
"https://raw.githubusercontent.com/greatcoolge/HyperADRules/master/list/allow1.txt",
"https://raw.githubusercontent.com/Bibaiji/ad-rules/main/rule/local-rule.txt",
"https://raw.githubusercontent.com/hagezi/dns-blocklists/main/adblock/whitelist-referral.txt",
"https://raw.githubusercontent.com/hagezi/dns-blocklists/main/adblock/whitelist-urlshortener.txt"
"https://raw.githubusercontent.com/jerryn70/GoodbyeAds/master/Formats/GoodbyeAds-AdBlock-Filter.txt",
"https://raw.githubusercontent.com/hagezi/dns-blocklists/main/adblock/multi.txt",
"https://raw.githubusercontent.com/neodevpro/neodevhost/master/lite_host",
"https://raw.githubusercontent.com/neodevpro/neodevhost/master/lite_adblocker"
)
# 日志文件路径
$logFilePath = "$PSScriptRoot/adblock_log.txt"
# 创建两个HashSet来存储唯一的规则和排除的域名
$uniqueRules = [System.Collections.Generic.HashSet[string]]::new()
$excludedDomains = [System.Collections.Generic.HashSet[string]]::new()
# 创建WebClient对象用于下载规则
$webClient = New-Object System.Net.WebClient
$webClient.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")
foreach ($url in $urlList) {
Write-Host "正在处理: $url"
Add-Content -Path $logFilePath -Value "正在处理: $url"
try
{
$content = $webClient.DownloadString($url)
$lines = $content -split "`n"
foreach ($line in $lines)
{
# 匹配所有以 @@|| 开头的规则,并提取域名
if ($line -match '^@@\|\|([a-zA-Z0-9.-]+\.[a-zA-Z]{2,})') {
$excludedDomain = $Matches[1]
$excludedDomains.Add($excludedDomain) | Out-Null
}
else
{
# 匹配 Adblock/Easylist 格式的规则
if ($line -match '^\|\|([a-zA-Z0-9.-]+\.[a-zA-Z]{2,})\^$') {
$domain = $Matches[1]
$uniqueRules.Add($domain) | Out-Null
}
# 匹配 Hosts 文件格式的规则
elseif ($line -match '^(0\.0\.0\.0|127\.0\.0\.1)\s+([a-zA-Z0-9.-]+\.[a-zA-Z]{2,})$') {
$domain = $Matches[2]
$uniqueRules.Add($domain) | Out-Null
}
# 匹配 Dnsmasq 格式的规则
elseif ($line -match '^address=/([a-zA-Z0-9.-]+\.[a-zA-Z]{2,})/$') {
$domain = $Matches[1]
$uniqueRules.Add($domain) | Out-Null
}
# 匹配通配符匹配格式的规则
elseif ($line -match '^\|\|([a-zA-Z0-9.-]+\.[a-zA-Z]{2,})\^$') {
$domain = $Matches[1]
$uniqueRules.Add($domain) | Out-Null
}
}
}
}
catch {
Write-Host "处理 $url 时出错: $_"
Add-Content -Path $logFilePath -Value "处理 $url 时出错: $_"
}
}
# 排除以 @@|| 开头规则中提取的域名
$finalRules = $uniqueRules | Where-Object { -not $excludedDomains.Contains($_) }
# 对规则进行排序并格式化
$formattedRules = $finalRules | Sort-Object | ForEach-Object {"- '+.$_'"}
# 统计生成的规则条目数量
$ruleCount = $finalRules.Count
# 获取当前时间并转换为东八区时间
$generationTime = (Get-Date).ToUniversalTime().AddHours(8).ToString("yyyy-MM-dd HH:mm:ss")
# 创建文本格式的字符串
$textContent = @"
# Title: AdBlock_Rule_For_Clash
# Description: 适用于Clash的域名拦截规则集,每20分钟更新一次,确保即时同步上游减少误杀
# Homepage: https://github.com/greatcoolge/AdBlock_Rule_For_Clash
# LICENSE1: https://github.com/greatcoolge/AdBlock_Rule_For_Clash/blob/main/LICENSE-GPL3.0
# LICENSE2: https://github.com/greatcoolge/AdBlock_Rule_For_Clash/blob/main/LICENSE-CC%20BY-NC-SA%204.0
# Generated on: $generationTime
# Generated AdBlock rules
# Total entries: $ruleCount
payload:
$($formattedRules -join "`n")
"@
# 定义输出文件路径
$outputPath = "$PSScriptRoot/adblock_reject.txt"
$textContent | Out-File -FilePath $outputPath -Encoding utf8
# 输出生成的有效规则总数
Write-Host "生成的有效规则总数: $ruleCount"
Add-Content -Path $logFilePath -Value "Total entries: $ruleCount"