Files
mmw-agent/internal/handler/pull_api_handler.go
T

112 lines
2.7 KiB
Go
Raw Normal View History

2026-01-28 13:13:58 +08:00
package handler
import (
"encoding/json"
"log"
"net/http"
"strings"
"mmw-agent/internal/agent"
2026-04-10 15:25:21 +08:00
"mmw-agent/internal/constants"
2026-01-28 13:13:58 +08:00
)
2026-04-10 15:25:21 +08:00
// APIHandler 处理来自主控端的请求(拉取模式)。
2026-01-28 13:13:58 +08:00
type APIHandler struct {
client *agent.Client
configToken string
}
2026-04-10 15:25:21 +08:00
// 创建 API 处理器。
2026-01-28 13:13:58 +08:00
func NewAPIHandler(client *agent.Client, configToken string) *APIHandler {
return &APIHandler{
client: client,
configToken: configToken,
}
}
2026-04-10 15:25:21 +08:00
// 返回流量数据。
2026-01-28 13:13:58 +08:00
func (h *APIHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
if !h.authenticate(r) {
2026-04-10 15:25:21 +08:00
w.Header().Set(constants.HeaderContentType, constants.ContentTypeJSON)
2026-01-28 13:13:58 +08:00
w.WriteHeader(http.StatusUnauthorized)
json.NewEncoder(w).Encode(map[string]interface{}{
"success": false,
"error": "Unauthorized",
})
return
}
stats, err := h.client.GetStats()
if err != nil {
log.Printf("[API] Failed to get stats: %v", err)
2026-04-10 15:25:21 +08:00
w.Header().Set(constants.HeaderContentType, constants.ContentTypeJSON)
2026-01-28 13:13:58 +08:00
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(map[string]interface{}{
"success": false,
"error": "Failed to collect stats",
})
return
}
2026-04-10 15:25:21 +08:00
w.Header().Set(constants.HeaderContentType, constants.ContentTypeJSON)
2026-01-28 13:13:58 +08:00
json.NewEncoder(w).Encode(map[string]interface{}{
"success": true,
"stats": stats,
})
}
2026-04-10 15:25:21 +08:00
// 返回速率数据。
2026-01-28 13:13:58 +08:00
func (h *APIHandler) ServeSpeedHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
if !h.authenticate(r) {
2026-04-10 15:25:21 +08:00
w.Header().Set(constants.HeaderContentType, constants.ContentTypeJSON)
2026-01-28 13:13:58 +08:00
w.WriteHeader(http.StatusUnauthorized)
json.NewEncoder(w).Encode(map[string]interface{}{
"success": false,
"error": "Unauthorized",
})
return
}
uploadSpeed, downloadSpeed := h.client.GetSpeed()
2026-04-10 15:25:21 +08:00
w.Header().Set(constants.HeaderContentType, constants.ContentTypeJSON)
2026-01-28 13:13:58 +08:00
json.NewEncoder(w).Encode(map[string]interface{}{
"success": true,
"upload_speed": uploadSpeed,
"download_speed": downloadSpeed,
})
}
2026-04-10 15:25:21 +08:00
// 校验请求身份(token + User-Agent)。
2026-01-28 13:13:58 +08:00
func (h *APIHandler) authenticate(r *http.Request) bool {
2026-04-10 15:25:21 +08:00
if r.Header.Get(constants.HeaderUserAgent) != constants.AgentUserAgent {
2026-03-12 16:13:50 +08:00
return false
}
2026-01-28 13:13:58 +08:00
if h.configToken == "" {
return true
}
2026-04-10 15:25:21 +08:00
auth := r.Header.Get(constants.HeaderAuthorization)
2026-01-28 13:13:58 +08:00
if auth == "" {
return false
}
2026-04-10 15:25:21 +08:00
if strings.HasPrefix(auth, constants.BearerPrefix) {
token := strings.TrimPrefix(auth, constants.BearerPrefix)
2026-01-28 13:13:58 +08:00
return token == h.configToken
}
return auth == h.configToken
}