web异常修复

This commit is contained in:
Alex Yang
2025-11-25 15:17:45 +08:00
parent aea162a616
commit e86c3db45f
10 changed files with 385 additions and 290 deletions

View File

@@ -195,31 +195,23 @@ func (s *Server) handleHourlyStats(w http.ResponseWriter, r *http.Request) {
func (s *Server) handleShield(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
// 返回屏蔽规则的基本配置信息
// 返回屏蔽规则的基本配置信息和统计数据,不返回完整规则列表
switch r.Method {
case http.MethodGet:
// 获取规则统计信息
stats := s.shieldManager.GetStats()
shieldInfo := map[string]interface{}{
"updateInterval": s.globalConfig.Shield.UpdateInterval,
"blockMethod": s.globalConfig.Shield.BlockMethod,
"blacklistCount": len(s.globalConfig.Shield.Blacklists),
"domainRulesCount": stats["domainRules"],
"domainExceptionsCount": stats["domainExceptions"],
"regexRulesCount": stats["regexRules"],
"regexExceptionsCount": stats["regexExceptions"],
"hostsRulesCount": stats["hostsRules"],
}
json.NewEncoder(w).Encode(shieldInfo)
default:
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
// 处理远程黑名单管理子路由
if strings.HasPrefix(r.URL.Path, "/shield/blacklists") {
s.handleShieldBlacklists(w, r)
return
}
switch r.Method {
case http.MethodGet:
// 获取完整规则列表
rules := s.shieldManager.GetRules()
json.NewEncoder(w).Encode(rules)
case http.MethodPost:
// 添加屏蔽规则
var req struct {
@@ -237,7 +229,7 @@ func (s *Server) handleShield(w http.ResponseWriter, r *http.Request) {
}
json.NewEncoder(w).Encode(map[string]string{"status": "success"})
return
case http.MethodDelete:
// 删除屏蔽规则
var req struct {
@@ -255,7 +247,7 @@ func (s *Server) handleShield(w http.ResponseWriter, r *http.Request) {
}
json.NewEncoder(w).Encode(map[string]string{"status": "success"})
return
case http.MethodPut:
// 重新加载规则
if err := s.shieldManager.LoadRules(); err != nil {
@@ -263,9 +255,10 @@ func (s *Server) handleShield(w http.ResponseWriter, r *http.Request) {
return
}
json.NewEncoder(w).Encode(map[string]string{"status": "success", "message": "规则重新加载成功"})
return
default:
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
}