Files
SimpleRemoter/server/go/web/audit_query.go
T
yuanyuanxianganddeepseek-flash 774c0d2545 Feature(Go): four local MCP read tools and a shared audit query package
Implement milestone M2 of the minimal MCP plan: the tools that answer from
the server's own state instead of opening a device channel.

- Extract the audit-log scanner from web into package auditquery so the
  admin page and get_audit_log share one implementation. The web handler
  keeps its 500-row cap, the MCP tool uses the protocol's 1000-row cap.
  Query gains json tags for the web snake_case wire format (device_id,
  warn_only) and reports TotalAll before filters.
- Add package buildinfo and inject Version/Commit with -X from build.ps1
  and the Makefile; go-winres refreshes Windows resources from the git tag
  when available. An unversioned build reports "", and only initialize
  substitutes "dev".
- list_hosts: online/offline pools, include_offline merge, every
  documented filter, limit/offset paging with total/totalAll/hasMore, and
  schema-named host fields (authStatus/tls/remark are never fabricated).
- get_host_detail: single id or a batch of up to 50, online lookup then
  the last history record, capabilities from protocol.SupportsCap plus the
  Windows/registry/virtual-desktop rules; v2 is not listed.
- get_server_status: version, uptime, online/offline counts, port and
  bind. sessions stays absent until the MCP session table lands in M3.
- get_audit_log: case-insensitive event substring, keyword, paging;
  hasMore turns false at the MaxOffset clamp so paging cannot loop.

Wire hub, history, audit path and listen address through mcp.Config from
cmd/main.go. Tests cover pools/filters/paging, capability bits, batch
failures, audit paging and the unversioned build.

Co-Authored-By: deepseek-flash
2026-11-12 20:55:18 +01:00

92 lines
2.7 KiB
Go

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,
}))
}