Web优化
This commit is contained in:
@@ -752,7 +752,7 @@ func (s *Server) handleShieldBlacklists(w http.ResponseWriter, r *http.Request)
|
||||
}
|
||||
|
||||
if targetURLOrName == "" {
|
||||
http.Error(w, "黑名单标识不能为空", http.StatusBadRequest)
|
||||
json.NewEncoder(w).Encode(map[string]string{"status": "error", "message": "黑名单标识不能为空"})
|
||||
return
|
||||
}
|
||||
|
||||
@@ -767,7 +767,7 @@ func (s *Server) handleShieldBlacklists(w http.ResponseWriter, r *http.Request)
|
||||
}
|
||||
|
||||
if targetIndex == -1 {
|
||||
http.Error(w, "黑名单不存在", http.StatusNotFound)
|
||||
json.NewEncoder(w).Encode(map[string]string{"status": "error", "message": "黑名单不存在"})
|
||||
return
|
||||
}
|
||||
|
||||
@@ -780,7 +780,7 @@ func (s *Server) handleShieldBlacklists(w http.ResponseWriter, r *http.Request)
|
||||
// 保存配置到文件
|
||||
if err := saveConfigToFile(s.globalConfig, "config.json"); err != nil {
|
||||
logger.Error("保存配置文件失败", "error", err)
|
||||
http.Error(w, "保存配置失败", http.StatusInternalServerError)
|
||||
json.NewEncoder(w).Encode(map[string]string{"status": "error", "message": "保存配置失败"})
|
||||
return
|
||||
}
|
||||
// 重新加载规则以获取最新的远程规则
|
||||
@@ -810,7 +810,7 @@ func (s *Server) handleShieldBlacklists(w http.ResponseWriter, r *http.Request)
|
||||
// 保存配置到文件
|
||||
if err := saveConfigToFile(s.globalConfig, "config.json"); err != nil {
|
||||
logger.Error("保存配置文件失败", "error", err)
|
||||
http.Error(w, "保存配置失败", http.StatusInternalServerError)
|
||||
json.NewEncoder(w).Encode(map[string]string{"status": "error", "message": "保存配置失败"})
|
||||
return
|
||||
}
|
||||
json.NewEncoder(w).Encode(map[string]string{"status": "success"})
|
||||
@@ -831,12 +831,12 @@ func (s *Server) handleShieldBlacklists(w http.ResponseWriter, r *http.Request)
|
||||
}
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
http.Error(w, "Invalid request body", http.StatusBadRequest)
|
||||
json.NewEncoder(w).Encode(map[string]string{"status": "error", "message": "无效的请求体"})
|
||||
return
|
||||
}
|
||||
|
||||
if req.Name == "" || req.URL == "" {
|
||||
http.Error(w, "Name and URL are required", http.StatusBadRequest)
|
||||
json.NewEncoder(w).Encode(map[string]string{"status": "error", "message": "名称和URL不能为空"})
|
||||
return
|
||||
}
|
||||
|
||||
@@ -846,11 +846,17 @@ func (s *Server) handleShieldBlacklists(w http.ResponseWriter, r *http.Request)
|
||||
// 检查是否已存在
|
||||
for _, list := range blacklists {
|
||||
if list.URL == req.URL {
|
||||
http.Error(w, "黑名单URL已存在", http.StatusConflict)
|
||||
json.NewEncoder(w).Encode(map[string]string{"status": "error", "message": "黑名单URL已存在"})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 检查URL是否存在且可访问
|
||||
if !checkURLExists(req.URL) {
|
||||
json.NewEncoder(w).Encode(map[string]string{"status": "error", "message": "URL不存在或无法访问"})
|
||||
return
|
||||
}
|
||||
|
||||
// 添加新黑名单
|
||||
newEntry := config.BlacklistEntry{
|
||||
Name: req.Name,
|
||||
@@ -865,7 +871,7 @@ func (s *Server) handleShieldBlacklists(w http.ResponseWriter, r *http.Request)
|
||||
// 保存配置到文件
|
||||
if err := saveConfigToFile(s.globalConfig, "config.json"); err != nil {
|
||||
logger.Error("保存配置文件失败", "error", err)
|
||||
http.Error(w, "保存配置失败", http.StatusInternalServerError)
|
||||
json.NewEncoder(w).Encode(map[string]string{"status": "error", "message": "保存配置失败"})
|
||||
return
|
||||
}
|
||||
|
||||
@@ -884,7 +890,7 @@ func (s *Server) handleShieldBlacklists(w http.ResponseWriter, r *http.Request)
|
||||
}
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(&updatedBlacklists); err != nil {
|
||||
http.Error(w, "Invalid request body", http.StatusBadRequest)
|
||||
json.NewEncoder(w).Encode(map[string]string{"status": "error", "message": "无效的请求体"})
|
||||
return
|
||||
}
|
||||
|
||||
@@ -906,7 +912,7 @@ func (s *Server) handleShieldBlacklists(w http.ResponseWriter, r *http.Request)
|
||||
// 保存配置到文件
|
||||
if err := saveConfigToFile(s.globalConfig, "config.json"); err != nil {
|
||||
logger.Error("保存配置文件失败", "error", err)
|
||||
http.Error(w, "保存配置失败", http.StatusInternalServerError)
|
||||
json.NewEncoder(w).Encode(map[string]string{"status": "error", "message": "保存配置失败"})
|
||||
return
|
||||
}
|
||||
// 重新加载规则
|
||||
@@ -915,7 +921,7 @@ func (s *Server) handleShieldBlacklists(w http.ResponseWriter, r *http.Request)
|
||||
json.NewEncoder(w).Encode(map[string]string{"status": "success"})
|
||||
|
||||
default:
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
json.NewEncoder(w).Encode(map[string]string{"status": "error", "message": "Method not allowed"})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1193,6 +1199,24 @@ func isValidIP(ip string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// checkURLExists 检查URL是否存在且可访问
|
||||
func checkURLExists(url string) bool {
|
||||
// 创建一个带有超时的HTTP客户端
|
||||
client := &http.Client{
|
||||
Timeout: 5 * time.Second,
|
||||
}
|
||||
|
||||
// 发送HEAD请求来检查URL是否存在
|
||||
resp, err := client.Head(url)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// 检查状态码,2xx和3xx表示成功
|
||||
return resp.StatusCode >= 200 && resp.StatusCode < 400
|
||||
}
|
||||
|
||||
// handleRestart 处理重启服务请求
|
||||
func (s *Server) handleRestart(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
|
||||
Reference in New Issue
Block a user