api/shield接口中添加了本地和远程屏蔽规则数量的统计功能
This commit is contained in:
@@ -46,6 +46,8 @@ type ShieldManager struct {
|
|||||||
updateCtx context.Context
|
updateCtx context.Context
|
||||||
updateCancel context.CancelFunc
|
updateCancel context.CancelFunc
|
||||||
updateRunning bool
|
updateRunning bool
|
||||||
|
localRulesCount int // 本地规则数量
|
||||||
|
remoteRulesCount int // 远程规则数量
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewShieldManager 创建屏蔽管理器实例
|
// NewShieldManager 创建屏蔽管理器实例
|
||||||
@@ -62,6 +64,8 @@ func NewShieldManager(config *config.ShieldConfig) *ShieldManager {
|
|||||||
resolvedDomainsCount: make(map[string]int),
|
resolvedDomainsCount: make(map[string]int),
|
||||||
updateCtx: ctx,
|
updateCtx: ctx,
|
||||||
updateCancel: cancel,
|
updateCancel: cancel,
|
||||||
|
localRulesCount: 0,
|
||||||
|
remoteRulesCount: 0,
|
||||||
}
|
}
|
||||||
|
|
||||||
// 加载已保存的计数数据
|
// 加载已保存的计数数据
|
||||||
@@ -81,6 +85,8 @@ func (m *ShieldManager) LoadRules() error {
|
|||||||
m.regexRules = []regexRule{}
|
m.regexRules = []regexRule{}
|
||||||
m.regexExceptions = []regexRule{}
|
m.regexExceptions = []regexRule{}
|
||||||
m.hostsMap = make(map[string]string)
|
m.hostsMap = make(map[string]string)
|
||||||
|
m.localRulesCount = 0
|
||||||
|
m.remoteRulesCount = 0
|
||||||
// 保留计数数据,不随规则重新加载而清空
|
// 保留计数数据,不随规则重新加载而清空
|
||||||
|
|
||||||
// 加载本地规则文件
|
// 加载本地规则文件
|
||||||
@@ -118,6 +124,10 @@ func (m *ShieldManager) loadLocalRules() error {
|
|||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
|
// 记录加载前的规则数量,用于计算本地规则数量
|
||||||
|
beforeDomainRules := len(m.domainRules)
|
||||||
|
beforeRegexRules := len(m.regexRules)
|
||||||
|
|
||||||
scanner := bufio.NewScanner(file)
|
scanner := bufio.NewScanner(file)
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
line := strings.TrimSpace(scanner.Text())
|
line := strings.TrimSpace(scanner.Text())
|
||||||
@@ -127,6 +137,9 @@ func (m *ShieldManager) loadLocalRules() error {
|
|||||||
m.parseRule(line)
|
m.parseRule(line)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 更新本地规则计数
|
||||||
|
m.localRulesCount = (len(m.domainRules) - beforeDomainRules) + (len(m.regexRules) - beforeRegexRules)
|
||||||
|
|
||||||
return scanner.Err()
|
return scanner.Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -237,6 +250,10 @@ func (m *ShieldManager) loadCachedRules(filePath string) error {
|
|||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
|
// 记录加载前的规则数量,用于计算远程规则数量
|
||||||
|
beforeDomainRules := len(m.domainRules)
|
||||||
|
beforeRegexRules := len(m.regexRules)
|
||||||
|
|
||||||
body, err := ioutil.ReadAll(file)
|
body, err := ioutil.ReadAll(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -251,6 +268,10 @@ func (m *ShieldManager) loadCachedRules(filePath string) error {
|
|||||||
m.parseRule(line)
|
m.parseRule(line)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 更新远程规则计数
|
||||||
|
remoteRulesAdded := (len(m.domainRules) - beforeDomainRules) + (len(m.regexRules) - beforeRegexRules)
|
||||||
|
m.remoteRulesCount += remoteRulesAdded
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1117,6 +1138,9 @@ func (m *ShieldManager) GetRules() map[string]interface{} {
|
|||||||
hostsRulesList = append(hostsRulesList, ip+"\t"+domain)
|
hostsRulesList = append(hostsRulesList, ip+"\t"+domain)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 计算总规则数量
|
||||||
|
totalRulesCount := len(m.domainRules) + len(m.regexRules)
|
||||||
|
|
||||||
return map[string]interface{}{
|
return map[string]interface{}{
|
||||||
"domainRules": domainRulesList,
|
"domainRules": domainRulesList,
|
||||||
"domainExceptions": domainExceptionsList,
|
"domainExceptions": domainExceptionsList,
|
||||||
@@ -1124,5 +1148,8 @@ func (m *ShieldManager) GetRules() map[string]interface{} {
|
|||||||
"regexExceptions": regexExceptionsList,
|
"regexExceptions": regexExceptionsList,
|
||||||
"hostsRules": hostsRulesList,
|
"hostsRules": hostsRulesList,
|
||||||
"blacklists": m.config.Blacklists,
|
"blacklists": m.config.Blacklists,
|
||||||
|
"localRulesCount": m.localRulesCount,
|
||||||
|
"remoteRulesCount": m.remoteRulesCount,
|
||||||
|
"totalRulesCount": totalRulesCount,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user