feat: 实现账号设备分组与命令日志
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"git.asio.asia/TapRemly/server/internal/auth"
|
||||
"git.asio.asia/TapRemly/server/internal/store"
|
||||
)
|
||||
|
||||
const (
|
||||
maxCommandLogs = 20
|
||||
maxLogTextRunes = 200
|
||||
)
|
||||
|
||||
func (h *Handler) listLogs(w http.ResponseWriter, r *http.Request) {
|
||||
user, ok := h.requireUser(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
id, ok := parseID(r.PathValue("id"))
|
||||
if !ok {
|
||||
writeError(w, http.StatusNotFound, "not_found", "找不到这台设备")
|
||||
return
|
||||
}
|
||||
items, err := h.store.LogsForDevice(r.Context(), user.ID, id)
|
||||
if errors.Is(err, store.ErrNotFound) {
|
||||
writeError(w, http.StatusNotFound, "not_found", "找不到这台设备")
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
h.log.Error("list logs", "err", err)
|
||||
writeError(w, http.StatusInternalServerError, "internal", "出了点问题,请稍后再试")
|
||||
return
|
||||
}
|
||||
out := make([]map[string]any, 0, len(items))
|
||||
for _, item := range items {
|
||||
out = append(out, map[string]any{
|
||||
"at_ms": item.AtMs,
|
||||
"summary": item.Summary,
|
||||
"ok": item.OK,
|
||||
"result": item.Result,
|
||||
})
|
||||
}
|
||||
writeOK(w, map[string]any{"items": out})
|
||||
}
|
||||
|
||||
func (h *Handler) replaceLogs(w http.ResponseWriter, r *http.Request) {
|
||||
password := bearer(r)
|
||||
if password == "" {
|
||||
writeError(w, http.StatusUnauthorized, "unauthorized", "凭证无效")
|
||||
return
|
||||
}
|
||||
var req struct {
|
||||
EndpointID string `json:"endpoint_id"`
|
||||
Items json.RawMessage `json:"items"`
|
||||
}
|
||||
if !decodeJSON(w, r, maxJSONBody, &req) {
|
||||
return
|
||||
}
|
||||
endpointID := strings.TrimSpace(req.EndpointID)
|
||||
if !validEndpointID(endpointID) || len(req.Items) == 0 || string(req.Items) == "null" {
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "请提供这台设备最近的记录")
|
||||
return
|
||||
}
|
||||
var rawItems []struct {
|
||||
AtMs int64 `json:"at_ms"`
|
||||
Summary string `json:"summary"`
|
||||
OK bool `json:"ok"`
|
||||
Result string `json:"result"`
|
||||
}
|
||||
dec := json.NewDecoder(strings.NewReader(string(req.Items)))
|
||||
dec.DisallowUnknownFields()
|
||||
if err := dec.Decode(&rawItems); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "记录内容不正确")
|
||||
return
|
||||
}
|
||||
if len(rawItems) > maxCommandLogs {
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "最多保留 20 条")
|
||||
return
|
||||
}
|
||||
items := make([]store.LogItem, 0, len(rawItems))
|
||||
for _, item := range rawItems {
|
||||
summary := strings.TrimSpace(item.Summary)
|
||||
result := strings.TrimSpace(item.Result)
|
||||
if item.AtMs <= 0 || summary == "" || utf8.RuneCountInString(summary) > maxLogTextRunes || utf8.RuneCountInString(result) > maxLogTextRunes {
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "记录内容不正确")
|
||||
return
|
||||
}
|
||||
items = append(items, store.LogItem{
|
||||
AtMs: item.AtMs,
|
||||
Summary: summary,
|
||||
OK: item.OK,
|
||||
Result: result,
|
||||
})
|
||||
}
|
||||
hash, err := h.store.CredentialHash(r.Context(), endpointID)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusUnauthorized, "unauthorized", "凭证无效")
|
||||
return
|
||||
}
|
||||
ok, err := auth.VerifyPassword(password, hash)
|
||||
if err != nil || !ok {
|
||||
writeError(w, http.StatusUnauthorized, "unauthorized", "凭证无效")
|
||||
return
|
||||
}
|
||||
if err := h.store.ReplaceLogs(r.Context(), endpointID, items); err != nil {
|
||||
h.log.Error("replace logs", "err", err)
|
||||
writeError(w, http.StatusInternalServerError, "internal", "出了点问题,请稍后再试")
|
||||
return
|
||||
}
|
||||
writeOK(w, map[string]any{})
|
||||
}
|
||||
Reference in New Issue
Block a user