package web import ( "encoding/json" "github.com/yuanyuanxiang/SimpleRemoter/server/go/auditquery" ) // Audit-log query support for the admin-only "Audit Log" page. // // The scanner itself (file tail, filters, paging) lives in package // auditquery so the MCP get_audit_log tool can share it; this file keeps only // the WebSocket command handler and the reply shaping specific to the UI. // auditQueryMaxLimit caps the page size of the audit page. It is tighter than // auditquery.MaxLimit (the protocol-wide cap the MCP tool uses) because the // admin table is meant to be paged, not downloaded. const auditQueryMaxLimit = 500 // handleQueryAudit implements the admin-only audit query command. The reply is // `{cmd:"audit_result", ok, events, total, ...}`; events are newest-first. func (h *wsHub) handleQueryAudit(c *wsClient, raw []byte) { const replyCmd = "audit_result" if !h.requireAdmin(c, raw, replyCmd) { return } var in auditquery.Query if err := json.Unmarshal(raw, &in); err != nil { c.queue(mustJSON(map[string]any{"cmd": replyCmd, "ok": false, "msg": "Invalid request"})) return } in.Limit = auditquery.ClampLimit(in.Limit, auditQueryMaxLimit) in.Offset = auditquery.ClampOffset(in.Offset) path := h.log.AuditPath() if path == "" { h.audit(c, true, auditQuery, map[string]any{ "ok": false, "reason": "audit_log_disabled", }) c.queue(mustJSON(map[string]any{ "cmd": replyCmd, "ok": false, "msg": "Audit log file is disabled on this server (YAMA_AUDIT_LOG=off)", })) return } res, err := auditquery.Scan(path, in) if err != nil { h.audit(c, true, auditQuery, map[string]any{ "ok": false, "reason": "read_failed", "error": err.Error(), }) c.queue(mustJSON(map[string]any{ "cmd": replyCmd, "ok": false, "msg": "Failed to read audit log: " + err.Error(), })) return } // Record the query itself: an audit viewer is privileged, and reading the // trail is itself an auditable action. Filter terms are included so an // investigator can see what was searched for. h.audit(c, false, auditQuery, map[string]any{ "ok": true, "event_filter": in.Event, "actor_filter": in.Actor, "ip_filter": in.IP, "device_filter": in.DeviceID, "keyword": in.Keyword, "warn_only": in.WarnOnly, "matches": res.Total, "returned": len(res.Events), "offset": in.Offset, }) c.queue(mustJSON(map[string]any{ "cmd": replyCmd, "ok": true, "events": res.Events, "total": res.Total, "offset": in.Offset, "limit": in.Limit, "file": path, "file_exists": res.FileExists, "file_size": res.FileSize, "scanned_bytes": res.ScannedBytes, "truncated": res.Truncated, })) }