增加了API断电

This commit is contained in:
Alex Yang
2025-11-28 00:42:11 +08:00
parent b1c63f6713
commit 45ed4d0d6b
5 changed files with 206 additions and 87 deletions

View File

@@ -66,6 +66,24 @@ func (s *Server) Start() error {
if s.config.EnableAPI {
mux.HandleFunc("/api/stats", s.handleStats)
mux.HandleFunc("/api/shield", s.handleShield)
mux.HandleFunc("/api/shield/localrules", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if r.Method == http.MethodGet {
localRules := s.shieldManager.GetLocalRules()
json.NewEncoder(w).Encode(localRules)
return
}
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
})
mux.HandleFunc("/api/shield/remoterules", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if r.Method == http.MethodGet {
remoteRules := s.shieldManager.GetRemoteRules()
json.NewEncoder(w).Encode(remoteRules)
return
}
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
})
mux.HandleFunc("/api/shield/hosts", s.handleShieldHosts)
mux.HandleFunc("/api/shield/blacklists", s.handleShieldBlacklists)
mux.HandleFunc("/api/query", s.handleQuery)
@@ -596,6 +614,7 @@ func (s *Server) handleTopDomains(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:
// 检查是否需要返回完整规则列表
@@ -619,6 +638,8 @@ func (s *Server) handleShield(w http.ResponseWriter, r *http.Request) {
}
json.NewEncoder(w).Encode(shieldInfo)
return
}
switch r.Method {
case http.MethodPost:
// 添加屏蔽规则
var req struct {