修复统计数据异常的问题
This commit is contained in:
55
config.json
55
config.json
@@ -39,12 +39,63 @@
|
|||||||
"name": "My GitHub Rules",
|
"name": "My GitHub Rules",
|
||||||
"url": "https://gitea.amazehome.xyz/AMAZEHOME/hosts-and-filters/raw/branch/main/rules/costomize.txt",
|
"url": "https://gitea.amazehome.xyz/AMAZEHOME/hosts-and-filters/raw/branch/main/rules/costomize.txt",
|
||||||
"enabled": true,
|
"enabled": true,
|
||||||
"lastUpdateTime": "2025-11-28T16:13:05.960Z"
|
"lastUpdateTime": "2025-11-29T17:05:40.283Z"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "CNList",
|
"name": "CNList",
|
||||||
"url": "https://gitea.amazehome.xyz/AMAZEHOME/hosts-and-filters/raw/branch/main/list/china.list",
|
"url": "https://gitea.amazehome.xyz/AMAZEHOME/hosts-and-filters/raw/branch/main/list/china.list",
|
||||||
"enabled": false
|
"enabled": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "大圣净化",
|
||||||
|
"url": "http://gitea.amazehome.xyz/AMAZEHOME/hosts-and-Filters/raw/branch/main/hosts/dsjh.txt",
|
||||||
|
"enabled": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Hate \u0026 Junk",
|
||||||
|
"url": "http://gitea.amazehome.xyz/AMAZEHOME/hosts-and-filters/raw/branch/main/hate-and-junk-extended.txt",
|
||||||
|
"enabled": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "My Gitlab Hosts",
|
||||||
|
"url": "http://gitea.amazehome.xyz/AMAZEHOME/hosts-and-filters/raw/branch/main/hosts/costomize.txt",
|
||||||
|
"enabled": true,
|
||||||
|
"lastUpdateTime": "2025-11-29T17:11:28.130Z"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Anti Remote Requests",
|
||||||
|
"url": "http://gitea.amazehome.xyz/AMAZEHOME/hosts-and-filters/raw/branch/main/hosts/anti-remoterequests.txt",
|
||||||
|
"enabled": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "URL-Based.txt",
|
||||||
|
"url": "http://gitea.amazehome.xyz/AMAZEHOME/hosts-and-filters/raw/branch/main/rules/url-based-adguard.txt",
|
||||||
|
"enabled": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "My Gitlab A/T Rules",
|
||||||
|
"url": "http://gitea.amazehome.xyz/AMAZEHOME/hosts-and-filters/raw/branch/main/rules/ads-and-trackers.txt",
|
||||||
|
"enabled": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "My Gitlab Malware List",
|
||||||
|
"url": "http://gitea.amazehome.xyz/AMAZEHOME/hosts-and-filters/raw/branch/main/rules/malware.txt",
|
||||||
|
"enabled": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "hosts",
|
||||||
|
"url": "http://gitea.amazehome.xyz/AMAZEHOME/hosts-and-Filters/raw/branch/main/hosts/costomize.txt",
|
||||||
|
"enabled": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "AWAvenue-Ads-Rule",
|
||||||
|
"url": "http://gitea.amazehome.xyz/AMAZEHOME/hosts-and-Filters/raw/branch/main/rules/AWAvenue-Ads-Rule.txt",
|
||||||
|
"enabled": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "诈骗域名",
|
||||||
|
"url": "https://gitea.amazehome.xyz/AMAZEHOME/hosts-and-filters/raw/branch/main/rules/cheat.txt",
|
||||||
|
"enabled": true
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"updateInterval": 3600,
|
"updateInterval": 3600,
|
||||||
|
|||||||
@@ -366,11 +366,6 @@ func (s *Server) handleBlockedResponse(w dns.ResponseWriter, r *dns.Msg, domain
|
|||||||
// 更新被屏蔽域名统计
|
// 更新被屏蔽域名统计
|
||||||
s.updateBlockedDomainStats(domain)
|
s.updateBlockedDomainStats(domain)
|
||||||
|
|
||||||
// 更新总体统计
|
|
||||||
s.updateStats(func(stats *Stats) {
|
|
||||||
stats.Blocked++
|
|
||||||
})
|
|
||||||
|
|
||||||
response := new(dns.Msg)
|
response := new(dns.Msg)
|
||||||
response.SetReply(r)
|
response.SetReply(r)
|
||||||
response.RecursionAvailable = true
|
response.RecursionAvailable = true
|
||||||
|
|||||||
@@ -43,6 +43,8 @@ type ShieldManager struct {
|
|||||||
domainExceptionsIsLocal map[string]bool // 标记域名排除规则是否为本地规则
|
domainExceptionsIsLocal map[string]bool // 标记域名排除规则是否为本地规则
|
||||||
domainRulesSource map[string]string // 标记域名规则来源
|
domainRulesSource map[string]string // 标记域名规则来源
|
||||||
domainExceptionsSource map[string]string // 标记域名排除规则来源
|
domainExceptionsSource map[string]string // 标记域名排除规则来源
|
||||||
|
domainRulesOriginal map[string]string // 存储域名规则的原始字符串
|
||||||
|
domainExceptionsOriginal map[string]string // 存储域名排除规则的原始字符串
|
||||||
regexRules []regexRule
|
regexRules []regexRule
|
||||||
regexExceptions []regexRule
|
regexExceptions []regexRule
|
||||||
hostsMap map[string]string
|
hostsMap map[string]string
|
||||||
@@ -67,6 +69,8 @@ func NewShieldManager(config *config.ShieldConfig) *ShieldManager {
|
|||||||
domainExceptionsIsLocal: make(map[string]bool),
|
domainExceptionsIsLocal: make(map[string]bool),
|
||||||
domainRulesSource: make(map[string]string),
|
domainRulesSource: make(map[string]string),
|
||||||
domainExceptionsSource: make(map[string]string),
|
domainExceptionsSource: make(map[string]string),
|
||||||
|
domainRulesOriginal: make(map[string]string),
|
||||||
|
domainExceptionsOriginal: make(map[string]string),
|
||||||
regexRules: []regexRule{},
|
regexRules: []regexRule{},
|
||||||
regexExceptions: []regexRule{},
|
regexExceptions: []regexRule{},
|
||||||
hostsMap: make(map[string]string),
|
hostsMap: make(map[string]string),
|
||||||
@@ -96,6 +100,8 @@ func (m *ShieldManager) LoadRules() error {
|
|||||||
m.domainExceptionsIsLocal = make(map[string]bool)
|
m.domainExceptionsIsLocal = make(map[string]bool)
|
||||||
m.domainRulesSource = make(map[string]string)
|
m.domainRulesSource = make(map[string]string)
|
||||||
m.domainExceptionsSource = make(map[string]string)
|
m.domainExceptionsSource = make(map[string]string)
|
||||||
|
m.domainRulesOriginal = make(map[string]string)
|
||||||
|
m.domainExceptionsOriginal = make(map[string]string)
|
||||||
m.regexRules = []regexRule{}
|
m.regexRules = []regexRule{}
|
||||||
m.regexExceptions = []regexRule{}
|
m.regexExceptions = []regexRule{}
|
||||||
m.hostsMap = make(map[string]string)
|
m.hostsMap = make(map[string]string)
|
||||||
@@ -333,6 +339,9 @@ func (m *ShieldManager) loadHosts() error {
|
|||||||
|
|
||||||
// parseRule 解析规则行
|
// parseRule 解析规则行
|
||||||
func (m *ShieldManager) parseRule(line string, isLocal bool, source string) {
|
func (m *ShieldManager) parseRule(line string, isLocal bool, source string) {
|
||||||
|
// 保存原始规则用于后续使用
|
||||||
|
originalLine := line
|
||||||
|
|
||||||
// 处理注释
|
// 处理注释
|
||||||
if strings.HasPrefix(line, "!") || strings.HasPrefix(line, "#") || line == "" {
|
if strings.HasPrefix(line, "!") || strings.HasPrefix(line, "#") || line == "" {
|
||||||
return
|
return
|
||||||
@@ -357,12 +366,12 @@ func (m *ShieldManager) parseRule(line string, isLocal bool, source string) {
|
|||||||
case strings.HasPrefix(line, "||") && strings.HasSuffix(line, "^"):
|
case strings.HasPrefix(line, "||") && strings.HasSuffix(line, "^"):
|
||||||
// AdGuardHome域名规则格式: ||example.com^
|
// AdGuardHome域名规则格式: ||example.com^
|
||||||
domain := strings.TrimSuffix(strings.TrimPrefix(line, "||"), "^")
|
domain := strings.TrimSuffix(strings.TrimPrefix(line, "||"), "^")
|
||||||
m.addDomainRule(domain, !isException, isLocal, source)
|
m.addDomainRule(domain, !isException, isLocal, source, originalLine)
|
||||||
|
|
||||||
case strings.HasPrefix(line, "||"):
|
case strings.HasPrefix(line, "||"):
|
||||||
// 精确域名匹配规则
|
// 精确域名匹配规则
|
||||||
domain := strings.TrimPrefix(line, "||")
|
domain := strings.TrimPrefix(line, "||")
|
||||||
m.addDomainRule(domain, !isException, isLocal, source)
|
m.addDomainRule(domain, !isException, isLocal, source, originalLine)
|
||||||
|
|
||||||
case strings.HasPrefix(line, "*"):
|
case strings.HasPrefix(line, "*"):
|
||||||
// 通配符规则,转换为正则表达式
|
// 通配符规则,转换为正则表达式
|
||||||
@@ -370,7 +379,7 @@ func (m *ShieldManager) parseRule(line string, isLocal bool, source string) {
|
|||||||
pattern = "^" + pattern + "$"
|
pattern = "^" + pattern + "$"
|
||||||
if re, err := regexp.Compile(pattern); err == nil {
|
if re, err := regexp.Compile(pattern); err == nil {
|
||||||
// 保存原始规则字符串
|
// 保存原始规则字符串
|
||||||
m.addRegexRule(re, line, !isException, isLocal, source)
|
m.addRegexRule(re, originalLine, !isException, isLocal, source)
|
||||||
}
|
}
|
||||||
|
|
||||||
case strings.HasPrefix(line, "/") && strings.HasSuffix(line, "/"):
|
case strings.HasPrefix(line, "/") && strings.HasSuffix(line, "/"):
|
||||||
@@ -380,7 +389,7 @@ func (m *ShieldManager) parseRule(line string, isLocal bool, source string) {
|
|||||||
// 对于像 /domain/ 这样的规则,应该匹配包含 domain 字符串的任何域名
|
// 对于像 /domain/ 这样的规则,应该匹配包含 domain 字符串的任何域名
|
||||||
if re, err := regexp.Compile("(?i).*" + regexp.QuoteMeta(pattern) + ".*"); err == nil {
|
if re, err := regexp.Compile("(?i).*" + regexp.QuoteMeta(pattern) + ".*"); err == nil {
|
||||||
// 保存原始规则字符串
|
// 保存原始规则字符串
|
||||||
m.addRegexRule(re, line, !isException, isLocal, source)
|
m.addRegexRule(re, originalLine, !isException, isLocal, source)
|
||||||
}
|
}
|
||||||
|
|
||||||
case strings.HasPrefix(line, "|") && strings.HasSuffix(line, "|"):
|
case strings.HasPrefix(line, "|") && strings.HasSuffix(line, "|"):
|
||||||
@@ -389,7 +398,7 @@ func (m *ShieldManager) parseRule(line string, isLocal bool, source string) {
|
|||||||
// 将URL模式转换为正则表达式
|
// 将URL模式转换为正则表达式
|
||||||
pattern := "^" + regexp.QuoteMeta(urlPattern) + "$"
|
pattern := "^" + regexp.QuoteMeta(urlPattern) + "$"
|
||||||
if re, err := regexp.Compile(pattern); err == nil {
|
if re, err := regexp.Compile(pattern); err == nil {
|
||||||
m.addRegexRule(re, line, !isException, isLocal, source)
|
m.addRegexRule(re, originalLine, !isException, isLocal, source)
|
||||||
}
|
}
|
||||||
|
|
||||||
case strings.HasPrefix(line, "|"):
|
case strings.HasPrefix(line, "|"):
|
||||||
@@ -397,7 +406,7 @@ func (m *ShieldManager) parseRule(line string, isLocal bool, source string) {
|
|||||||
urlPattern := strings.TrimPrefix(line, "|")
|
urlPattern := strings.TrimPrefix(line, "|")
|
||||||
pattern := "^" + regexp.QuoteMeta(urlPattern)
|
pattern := "^" + regexp.QuoteMeta(urlPattern)
|
||||||
if re, err := regexp.Compile(pattern); err == nil {
|
if re, err := regexp.Compile(pattern); err == nil {
|
||||||
m.addRegexRule(re, line, !isException, isLocal, source)
|
m.addRegexRule(re, originalLine, !isException, isLocal, source)
|
||||||
}
|
}
|
||||||
|
|
||||||
case strings.HasSuffix(line, "|"):
|
case strings.HasSuffix(line, "|"):
|
||||||
@@ -405,12 +414,12 @@ func (m *ShieldManager) parseRule(line string, isLocal bool, source string) {
|
|||||||
urlPattern := strings.TrimSuffix(line, "|")
|
urlPattern := strings.TrimSuffix(line, "|")
|
||||||
pattern := regexp.QuoteMeta(urlPattern) + "$"
|
pattern := regexp.QuoteMeta(urlPattern) + "$"
|
||||||
if re, err := regexp.Compile(pattern); err == nil {
|
if re, err := regexp.Compile(pattern); err == nil {
|
||||||
m.addRegexRule(re, line, !isException, isLocal, source)
|
m.addRegexRule(re, originalLine, !isException, isLocal, source)
|
||||||
}
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
// 默认作为普通域名规则
|
// 默认作为普通域名规则
|
||||||
m.addDomainRule(line, !isException, isLocal, source)
|
m.addDomainRule(line, !isException, isLocal, source, originalLine)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -434,7 +443,7 @@ func (m *ShieldManager) parseRuleOptions(optionsStr string) map[string]string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// addDomainRule 添加域名规则,支持是否为阻止规则
|
// addDomainRule 添加域名规则,支持是否为阻止规则
|
||||||
func (m *ShieldManager) addDomainRule(domain string, block bool, isLocal bool, source string) {
|
func (m *ShieldManager) addDomainRule(domain string, block bool, isLocal bool, source string, original string) {
|
||||||
if block {
|
if block {
|
||||||
// 如果是远程规则,检查是否已经存在本地规则,如果存在则不覆盖
|
// 如果是远程规则,检查是否已经存在本地规则,如果存在则不覆盖
|
||||||
if !isLocal {
|
if !isLocal {
|
||||||
@@ -446,6 +455,7 @@ func (m *ShieldManager) addDomainRule(domain string, block bool, isLocal bool, s
|
|||||||
m.domainRules[domain] = true
|
m.domainRules[domain] = true
|
||||||
m.domainRulesIsLocal[domain] = isLocal
|
m.domainRulesIsLocal[domain] = isLocal
|
||||||
m.domainRulesSource[domain] = source
|
m.domainRulesSource[domain] = source
|
||||||
|
m.domainRulesOriginal[domain] = original
|
||||||
} else {
|
} else {
|
||||||
// 添加到排除规则
|
// 添加到排除规则
|
||||||
// 如果是远程规则,检查是否已经存在本地规则,如果存在则不覆盖
|
// 如果是远程规则,检查是否已经存在本地规则,如果存在则不覆盖
|
||||||
@@ -458,6 +468,7 @@ func (m *ShieldManager) addDomainRule(domain string, block bool, isLocal bool, s
|
|||||||
m.domainExceptions[domain] = true
|
m.domainExceptions[domain] = true
|
||||||
m.domainExceptionsIsLocal[domain] = isLocal
|
m.domainExceptionsIsLocal[domain] = isLocal
|
||||||
m.domainExceptionsSource[domain] = source
|
m.domainExceptionsSource[domain] = source
|
||||||
|
m.domainExceptionsOriginal[domain] = original
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -529,7 +540,7 @@ func (m *ShieldManager) CheckDomainBlockDetails(domain string) map[string]interf
|
|||||||
// 检查域名排除规则
|
// 检查域名排除规则
|
||||||
if m.domainExceptions[domain] {
|
if m.domainExceptions[domain] {
|
||||||
result["excluded"] = true
|
result["excluded"] = true
|
||||||
result["excludeRule"] = domain
|
result["excludeRule"] = m.domainExceptionsOriginal[domain]
|
||||||
result["excludeRuleType"] = "exact_domain"
|
result["excludeRuleType"] = "exact_domain"
|
||||||
result["blocksource"] = m.domainExceptionsSource[domain]
|
result["blocksource"] = m.domainExceptionsSource[domain]
|
||||||
return result
|
return result
|
||||||
@@ -541,7 +552,7 @@ func (m *ShieldManager) CheckDomainBlockDetails(domain string) map[string]interf
|
|||||||
subdomain := strings.Join(parts[i:], ".")
|
subdomain := strings.Join(parts[i:], ".")
|
||||||
if m.domainExceptions[subdomain] {
|
if m.domainExceptions[subdomain] {
|
||||||
result["excluded"] = true
|
result["excluded"] = true
|
||||||
result["excludeRule"] = subdomain
|
result["excludeRule"] = m.domainExceptionsOriginal[subdomain]
|
||||||
result["excludeRuleType"] = "subdomain"
|
result["excludeRuleType"] = "subdomain"
|
||||||
result["blocksource"] = m.domainExceptionsSource[subdomain]
|
result["blocksource"] = m.domainExceptionsSource[subdomain]
|
||||||
return result
|
return result
|
||||||
@@ -563,7 +574,7 @@ func (m *ShieldManager) CheckDomainBlockDetails(domain string) map[string]interf
|
|||||||
// 检查精确域名匹配
|
// 检查精确域名匹配
|
||||||
if m.domainRules[domain] {
|
if m.domainRules[domain] {
|
||||||
result["blocked"] = true
|
result["blocked"] = true
|
||||||
result["blockRule"] = domain
|
result["blockRule"] = m.domainRulesOriginal[domain]
|
||||||
result["blockRuleType"] = "exact_domain"
|
result["blockRuleType"] = "exact_domain"
|
||||||
result["blocksource"] = m.domainRulesSource[domain]
|
result["blocksource"] = m.domainRulesSource[domain]
|
||||||
return result
|
return result
|
||||||
@@ -575,7 +586,7 @@ func (m *ShieldManager) CheckDomainBlockDetails(domain string) map[string]interf
|
|||||||
subdomain := strings.Join(parts[i:], ".")
|
subdomain := strings.Join(parts[i:], ".")
|
||||||
if m.domainRules[subdomain] {
|
if m.domainRules[subdomain] {
|
||||||
result["blocked"] = true
|
result["blocked"] = true
|
||||||
result["blockRule"] = subdomain
|
result["blockRule"] = m.domainRulesOriginal[subdomain]
|
||||||
result["blockRuleType"] = "subdomain"
|
result["blockRuleType"] = "subdomain"
|
||||||
result["blocksource"] = m.domainRulesSource[subdomain]
|
result["blocksource"] = m.domainRulesSource[subdomain]
|
||||||
return result
|
return result
|
||||||
|
|||||||
Reference in New Issue
Block a user